Skip to content

Commit 8b4ac95

Browse files
committed
Add option to transform a record
1 parent 20c0760 commit 8b4ac95

File tree

2 files changed

+95
-24
lines changed

2 files changed

+95
-24
lines changed

src/index.js

+48-24
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ class TableRecord {
363363
})
364364
)
365365
setIs(this, record, it, isNew)
366-
Object.freeze(this)
366+
// Object.freeze(this)
367367
}
368368

369369
get entity() {
@@ -1330,7 +1330,7 @@ module.exports = function (schemaName, schema, config) {
13301330
return db
13311331
.query(view.statement, view.params, options)
13321332
.then(function (res) {
1333-
return res.map(function (record) {
1333+
const recordset = res.map(function (record) {
13341334
return options.toPlainObject === true ||
13351335
options.fetchExternalDescription === true
13361336
? buildPlainObject(record, data)
@@ -1343,10 +1343,16 @@ module.exports = function (schemaName, schema, config) {
13431343
self
13441344
)
13451345
})
1346+
return config.transforms?.read
1347+
? recordset.map(config.transforms.read)
1348+
: recordset
13461349
})
13471350
})
13481351
},
13491352
create: function (entity, options) {
1353+
if (config.transforms?.write) {
1354+
entity = config.transforms.write(entity)
1355+
}
13501356
options = options || {}
13511357
var self = this
13521358
var isInstance =
@@ -1387,21 +1393,28 @@ module.exports = function (schemaName, schema, config) {
13871393
})
13881394
.then(function (record) {
13891395
orderAssociations(record, data)
1390-
return options.toPlainObject === true &&
1391-
!isInstance
1392-
? record
1393-
: buildEntity(
1394-
record,
1395-
data,
1396-
false,
1397-
false,
1398-
entity,
1399-
self
1400-
)
1396+
let res =
1397+
options.toPlainObject === true && !isInstance
1398+
? record
1399+
: buildEntity(
1400+
record,
1401+
data,
1402+
false,
1403+
false,
1404+
entity,
1405+
self
1406+
)
1407+
if (config.transforms?.read) {
1408+
res = config.transforms.read(res)
1409+
}
1410+
return res
14011411
})
14021412
})
14031413
},
14041414
update: function (entity, key, options) {
1415+
if (config.transforms?.write) {
1416+
entity = config.transforms.write(entity)
1417+
}
14051418
var self = this
14061419
key = key || entity[data.primaryKeyAttributes[0]]
14071420
if (!key) {
@@ -1509,17 +1522,21 @@ module.exports = function (schemaName, schema, config) {
15091522
})
15101523
.then(function (record) {
15111524
orderAssociations(record, data)
1512-
return options.toPlainObject === true &&
1513-
!isInstance
1514-
? record
1515-
: buildEntity(
1516-
record,
1517-
data,
1518-
false,
1519-
false,
1520-
entity,
1521-
self
1522-
)
1525+
let res =
1526+
options.toPlainObject === true && !isInstance
1527+
? record
1528+
: buildEntity(
1529+
record,
1530+
data,
1531+
false,
1532+
false,
1533+
entity,
1534+
self
1535+
)
1536+
if (config.transforms?.read) {
1537+
res = config.transforms.read(res)
1538+
}
1539+
return res
15231540
})
15241541
})
15251542
},
@@ -1685,6 +1702,13 @@ module.exports = function (schemaName, schema, config) {
16851702
rebuild()
16861703
return data.public
16871704
},
1705+
setTransforms: function ({read, write}) {
1706+
config.transforms = {
1707+
read,
1708+
write
1709+
}
1710+
return data.public
1711+
},
16881712
setScope: function (scope) {
16891713
data.scope = scope
16901714
rebuild()

test/spec.js

+47
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,17 @@ module.exports = function (options) {
100100
}
101101
})
102102
entityCadastro.setDialect(db.dialect)
103+
entityCadastro.setTransforms({
104+
read: record => {
105+
record.emails = ['hi_1@abc.com', 'hi_2@abc.com']
106+
return record
107+
},
108+
write: record => {
109+
record.EMAIL = record.emails?.[0]
110+
delete record.emails
111+
return record
112+
}
113+
})
103114
tableCadastro = entityCadastro.new(db)
104115
tableCadastro2 = entityCadastro.new(db2)
105116
tableCadastro
@@ -194,6 +205,42 @@ module.exports = function (options) {
194205
expect(record.id).to.not.equal(undefined)
195206
expect(record.updatedAt).to.not.equal(undefined)
196207
expect(end[1]).above(minNanoSecsToSave)
208+
expect(record.emails).to.deep.equal([
209+
'hi_1@abc.com',
210+
'hi_2@abc.com'
211+
])
212+
return tableCadastro2
213+
.update({...record, EMAIL: 'z@a.com'})
214+
.then(function (record) {
215+
end = process.hrtime(start)
216+
expect(record.id).to.not.equal(undefined)
217+
expect(record.updatedAt).to.not.equal(undefined)
218+
expect(end[1]).above(minNanoSecsToSave)
219+
expect(record.EMAIL).to.equal('hi_1@abc.com')
220+
expect(record.emails).to.deep.equal([
221+
'hi_1@abc.com',
222+
'hi_2@abc.com'
223+
])
224+
done()
225+
})
226+
})
227+
.catch(function (err) {
228+
done(err)
229+
})
230+
})
231+
it('record should be readable in db 2', function (done) {
232+
tableCadastro2
233+
.fetch()
234+
.then(function (recordset) {
235+
expect(recordset).to.be.a('array')
236+
expect(recordset.length).to.equal(1)
237+
expect(tableCadastro2.db).to.equal(db2)
238+
const record = recordset[0]
239+
expect(record.EMAIL).to.equal('hi_1@abc.com')
240+
expect(record.emails).to.deep.equal([
241+
'hi_1@abc.com',
242+
'hi_2@abc.com'
243+
])
197244
done()
198245
})
199246
.catch(function (err) {

0 commit comments

Comments
 (0)