Skip to content

Commit

Permalink
v24.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
rjrodger committed Jun 29, 2023
1 parent 00f3032 commit dac117d
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 62 deletions.
2 changes: 1 addition & 1 deletion dist/lib/make_entity.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/lib/make_entity.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "seneca-entity",
"main": "dist/entity.js",
"version": "23.0.1",
"version": "24.0.0",
"type": "commonjs",
"types": "dist/entity.d.ts",
"description": "Entity plugin for seneca",
Expand Down
68 changes: 34 additions & 34 deletions src/lib/make_entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class Entity implements Record<string, any> {
constructor(canon: any, seneca: any, options: any) {
const private$: any = this.private$

private$.get_instance = function() {
private$.get_instance = function () {
return seneca
}
private$.canon = canon
Expand Down Expand Up @@ -157,18 +157,18 @@ class Entity implements Record<string, any> {
for (const p in props) {
if (Object.prototype.hasOwnProperty.call(props, p)) {
if (!~p.indexOf('$')) {
; (entity as any)[p] = props[p]
;(entity as any)[p] = props[p]
} else if (p.length > 2 && p.slice(-2) === '_$') {
; (entity as any)[p.slice(0, -2)] = props[p]
;(entity as any)[p.slice(0, -2)] = props[p]
}
}
}

if (Object.prototype.hasOwnProperty.call(props, 'id$')) {
; (entity as any).id$ = props.id$
;(entity as any).id$ = props.id$
}

; (self as any).log$ &&
;(self as any).log$ &&
(self as any).log$('make', entity.canon$({ string: true }), entity)

return entity
Expand Down Expand Up @@ -400,7 +400,7 @@ class Entity implements Record<string, any> {

const promise = self.private$.promise && !done$

; (self as any).log$ && (self as any).log$('close')
;(self as any).log$ && (self as any).log$('close')

return promise ? si.post(entmsg) : (si.act(entmsg, done$), self)
}
Expand Down Expand Up @@ -465,21 +465,21 @@ class Entity implements Record<string, any> {

return null == opt || opt.string || opt.string$
? // ? [
// (opt && opt.string$ ? '$' : '') +
// (null == canon.zone ? '-' : canon.zone),
// null == canon.base ? '-' : canon.base,
// null == canon.name ? '-' : canon.name,
// ].join('/') // TODO: make joiner an option
(opt && opt.string$ ? '$' : '') + canonstr(canon)
// (opt && opt.string$ ? '$' : '') +
// (null == canon.zone ? '-' : canon.zone),
// null == canon.base ? '-' : canon.base,
// null == canon.name ? '-' : canon.name,
// ].join('/') // TODO: make joiner an option
(opt && opt.string$ ? '$' : '') + canonstr(canon)
: opt.array
? [canon.zone, canon.base, canon.name]
: opt.array$
? [canon.zone, canon.base, canon.name]
: opt.object
? { zone: canon.zone, base: canon.base, name: canon.name }
: opt.object$
? { zone$: canon.zone, base$: canon.base, name$: canon.name }
: [canon.zone, canon.base, canon.name]
? [canon.zone, canon.base, canon.name]
: opt.array$
? [canon.zone, canon.base, canon.name]
: opt.object
? { zone: canon.zone, base: canon.base, name: canon.name }
: opt.object$
? { zone$: canon.zone, base$: canon.base, name$: canon.name }
: [canon.zone, canon.base, canon.name]
}

// data = object, or true|undef = include $, false = exclude $
Expand Down Expand Up @@ -579,14 +579,14 @@ function entityPromise(si: any, entmsg: any) {
err
? rej((attachMeta ? (err.meta$ = meta) : null, err))
: res(
(attachMeta
? ((out?.entity$
? proto(out)
: out || (out = { entity$: null })
).meta$ = meta)
: null,
(attachMeta
? ((out?.entity$
? proto(out)
: out || (out = { entity$: null })
).meta$ = meta)
: null,
out)
)
)
})
})
}
Expand All @@ -613,7 +613,7 @@ function emptyQuery(q: any): boolean {
function normalize_query(qin: any, ent: any, flags?: { inject_id: boolean }) {
let q = qin

let inject_id = flags ? false === flags.inject_id ? false : true : true
let inject_id = flags ? (false === flags.inject_id ? false : true) : true

if (inject_id) {
if ((null == qin || 'function' === typeof qin) && ent.id != null) {
Expand Down Expand Up @@ -709,12 +709,12 @@ function handle_options(entopts: any, seneca: any): any {

if (false === entopts.meta?.provide) {
// Drop meta argument from callback
; (Entity.prototype as any).done$ = (done: any) => {
;(Entity.prototype as any).done$ = (done: any) => {
return null == done
? undefined
: function(this: any, err: any, out: any) {
done.call(this, err, out)
}
: function (this: any, err: any, out: any) {
done.call(this, err, out)
}
}
}

Expand All @@ -741,7 +741,7 @@ function make_toString(

hidden_fields.push('id')

return function(this: any) {
return function (this: any) {
return [
'$',
canon_str || this.canon$({ string: true }),
Expand Down Expand Up @@ -777,7 +777,7 @@ function MakeEntity(canon: any, seneca: any, opts: any): Entity {
))
).bind(ent)

let custom$ = function(this: any, props: any) {
let custom$ = function (this: any, props: any) {
if (
null != props &&
('object' === typeof props || 'function' === typeof props)
Expand Down
46 changes: 21 additions & 25 deletions test/entity.test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
/* Copyright (c) 2010-2022 Richard Rodger and other contributors, MIT License */


// TODO: test for role:entity translates!


const Util = require('util')

const Seneca = require('seneca')
Expand Down Expand Up @@ -1253,63 +1251,61 @@ describe('entity', function () {
})
})


test('translation-role-entity', async function () {
const si = Seneca({ legacy: false }).use('promisify').use('..').test()

let f0 = await si.entity('foo').save$({f:0})
let f0 = await si.entity('foo').save$({ f: 0 })
let list = await si.post('role:entity,cmd:list,name:foo')
expect(list).toMatchObject([{f:0}])
expect(list).toMatchObject([{ f: 0 }])

si.message('role:entity,cmd:save', async function(msg) {
si.message('role:entity,cmd:save', async function (msg) {
msg.ent.r0 = 2
return this.prior(msg)
})

let f1 = await si.entity('foo').save$({f:1})
expect(f1).toMatchObject({f:1,r0:2})
si.message('role:entity,cmd:save', async function(msg) {
let f1 = await si.entity('foo').save$({ f: 1 })
expect(f1).toMatchObject({ f: 1, r0: 2 })

si.message('role:entity,cmd:save', async function (msg) {
msg.ent.r1 = 3
return this.prior(msg)
})

let f2 = await si.entity('foo').save$({f:2})
expect(f2).toMatchObject({f:2,r0:2,r1:3})
let f2 = await si.entity('foo').save$({ f: 2 })
expect(f2).toMatchObject({ f: 2, r0: 2, r1: 3 })

si.message('sys:entity,cmd:save', async function(msg) {
si.message('sys:entity,cmd:save', async function (msg) {
msg.ent.s0 = 4
return this.prior(msg)
})

let f3 = await si.entity('foo').save$({f:3})
expect(f3).toMatchObject({f:3,r0:2,r1:3,s0:4})
let f3 = await si.entity('foo').save$({ f: 3 })
expect(f3).toMatchObject({ f: 3, r0: 2, r1: 3, s0: 4 })

si.message('sys:entity,cmd:save', async function(msg) {
si.message('sys:entity,cmd:save', async function (msg) {
msg.ent.s1 = 5
return this.prior(msg)
})

let f4 = await si.entity('foo').save$({f:4})
expect(f4).toMatchObject({f:4,r0:2,r1:3,s0:4,s1:5})
let f4 = await si.entity('foo').save$({ f: 4 })
expect(f4).toMatchObject({ f: 4, r0: 2, r1: 3, s0: 4, s1: 5 })

si.message('role:entity,cmd:save', async function(msg) {
si.message('role:entity,cmd:save', async function (msg) {
msg.ent.r2 = 6
return this.prior(msg)
})

let f5 = await si.entity('foo').save$({f:5})
expect(f5).toMatchObject({f:5,r0:2,r1:3,s0:4,s1:5,r2:6})
let f5 = await si.entity('foo').save$({ f: 5 })
expect(f5).toMatchObject({ f: 5, r0: 2, r1: 3, s0: 4, s1: 5, r2: 6 })

si.message('sys:entity,cmd:save', async function(msg) {
si.message('sys:entity,cmd:save', async function (msg) {
msg.ent.s2 = 7
return this.prior(msg)
})

let f6 = await si.entity('foo').save$({f:6})
expect(f6).toMatchObject({f:6,r0:2,r1:3,s0:4,s1:5,r2:6,s2:7})
let f6 = await si.entity('foo').save$({ f: 6 })
expect(f6).toMatchObject({ f: 6, r0: 2, r1: 3, s0: 4, s1: 5, r2: 6, s2: 7 })
})

})

function jj(x) {
Expand Down

0 comments on commit dac117d

Please sign in to comment.