Skip to content

Commit e860bac

Browse files
committed
Add a way to hide the entity type when parsing
1 parent 0a26210 commit e860bac

File tree

4 files changed

+40
-1
lines changed

4 files changed

+40
-1
lines changed

__tests__/entity-creation.unit.test.js

+32
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,38 @@ describe('Entity creation', ()=> {
345345
expect(TestEntity._etAlias).toBe('entity')
346346
}) // creates entity w/ table
347347

348+
it('creates entity w/ table w/ overriden type alias', async () => {
349+
350+
// Create basic table
351+
const TestTable = new Table({
352+
name: 'test-table',
353+
partitionKey: 'pk',
354+
DocumentClient
355+
})
356+
357+
// Create basic entity
358+
const TestEntity = new Entity({
359+
name: 'TestEnt',
360+
typeAlias: 'type',
361+
typeHidden: true,
362+
attributes: {
363+
pk: { partitionKey: true }
364+
},
365+
table: TestTable,
366+
timestamps: false
367+
})
368+
369+
expect(TestEntity._etAlias).toBe('type')
370+
expect(TestEntity.schema.attributes._et.alias).toBe('type')
371+
expect(TestEntity.schema.attributes._et.hidden).toBe(true)
372+
373+
expect(TestEntity.parse({
374+
_et: 'TestEnt',
375+
pk: 'test',
376+
})).toEqual({
377+
pk: 'test',
378+
})
379+
})
348380

349381
it('creates entity composite key delimiter, prefix and suffix', async () => {
350382

__tests__/parseEntity.unit.test.js

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const entity = {
99
modified: '_modified',
1010
modifiedAlias: 'modifiedAlias',
1111
typeAlias: 'typeAlias',
12+
typeHidden: true,
1213
attributes: {
1314
pk: { partitionKey: true },
1415
sk: { sortKey: true },
@@ -34,6 +35,7 @@ describe('parseEntity', () => {
3435
expect(ent.autoExecute).toBe(true)
3536
expect(ent.autoParse).toBe(true)
3637
expect(ent._etAlias).toBe('typeAlias')
38+
expect(ent.typeHidden).toBe(true)
3739
})
3840

3941
it('fails on extra config option', async () => {

classes/Entity.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class Entity {
5555

5656
// If an entity tracking field is enabled, add the attributes, alias and the default
5757
if (table.Table.entityField) {
58-
this.schema.attributes[table.Table.entityField] = { type: 'string', alias: this._etAlias, default: this.name }
58+
this.schema.attributes[table.Table.entityField] = { type: 'string', hidden: this.typeHidden, alias: this._etAlias, default: this.name }
5959
this.defaults[table.Table.entityField] = this.name
6060
this.schema.attributes[this._etAlias] = { type: 'string', map: table.Table.entityField, default: this.name }
6161
this.defaults[this._etAlias] = this.name

lib/parseEntity.js

+5
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ module.exports = (entity) => {
2020
modified,
2121
modifiedAlias,
2222
typeAlias,
23+
typeHidden,
2324
attributes,
2425
autoExecute,
2526
autoParse,
@@ -65,6 +66,9 @@ module.exports = (entity) => {
6566
typeAlias = typeof typeAlias === 'string'
6667
&& typeAlias.trim().length > 0 ? typeAlias.trim()
6768
: 'entity'
69+
70+
// Define 'typeHidden'
71+
typeHidden = typeof typeHidden === 'boolean' ? typeHidden : false
6872

6973
// Sanity check the attributes
7074
attributes = typeof attributes === 'object' && !Array.isArray(attributes) ?
@@ -94,6 +98,7 @@ module.exports = (entity) => {
9498
linked: track.linked,
9599
autoExecute,
96100
autoParse,
101+
typeHidden,
97102
_etAlias: typeAlias
98103
},
99104
table ? { table } : {}

0 commit comments

Comments
 (0)