Skip to content

Commit

Permalink
Add a way to hide the entity type when parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
GeertWille committed Nov 2, 2020
1 parent 0a26210 commit e860bac
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 1 deletion.
32 changes: 32 additions & 0 deletions __tests__/entity-creation.unit.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,38 @@ describe('Entity creation', ()=> {
expect(TestEntity._etAlias).toBe('entity')
}) // creates entity w/ table

it('creates entity w/ table w/ overriden type alias', async () => {

// Create basic table
const TestTable = new Table({
name: 'test-table',
partitionKey: 'pk',
DocumentClient
})

// Create basic entity
const TestEntity = new Entity({
name: 'TestEnt',
typeAlias: 'type',
typeHidden: true,
attributes: {
pk: { partitionKey: true }
},
table: TestTable,
timestamps: false
})

expect(TestEntity._etAlias).toBe('type')
expect(TestEntity.schema.attributes._et.alias).toBe('type')
expect(TestEntity.schema.attributes._et.hidden).toBe(true)

expect(TestEntity.parse({
_et: 'TestEnt',
pk: 'test',
})).toEqual({
pk: 'test',
})
})

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

Expand Down
2 changes: 2 additions & 0 deletions __tests__/parseEntity.unit.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const entity = {
modified: '_modified',
modifiedAlias: 'modifiedAlias',
typeAlias: 'typeAlias',
typeHidden: true,
attributes: {
pk: { partitionKey: true },
sk: { sortKey: true },
Expand All @@ -34,6 +35,7 @@ describe('parseEntity', () => {
expect(ent.autoExecute).toBe(true)
expect(ent.autoParse).toBe(true)
expect(ent._etAlias).toBe('typeAlias')
expect(ent.typeHidden).toBe(true)
})

it('fails on extra config option', async () => {
Expand Down
2 changes: 1 addition & 1 deletion classes/Entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class Entity {

// If an entity tracking field is enabled, add the attributes, alias and the default
if (table.Table.entityField) {
this.schema.attributes[table.Table.entityField] = { type: 'string', alias: this._etAlias, default: this.name }
this.schema.attributes[table.Table.entityField] = { type: 'string', hidden: this.typeHidden, alias: this._etAlias, default: this.name }
this.defaults[table.Table.entityField] = this.name
this.schema.attributes[this._etAlias] = { type: 'string', map: table.Table.entityField, default: this.name }
this.defaults[this._etAlias] = this.name
Expand Down
5 changes: 5 additions & 0 deletions lib/parseEntity.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ module.exports = (entity) => {
modified,
modifiedAlias,
typeAlias,
typeHidden,
attributes,
autoExecute,
autoParse,
Expand Down Expand Up @@ -65,6 +66,9 @@ module.exports = (entity) => {
typeAlias = typeof typeAlias === 'string'
&& typeAlias.trim().length > 0 ? typeAlias.trim()
: 'entity'

// Define 'typeHidden'
typeHidden = typeof typeHidden === 'boolean' ? typeHidden : false

// Sanity check the attributes
attributes = typeof attributes === 'object' && !Array.isArray(attributes) ?
Expand Down Expand Up @@ -94,6 +98,7 @@ module.exports = (entity) => {
linked: track.linked,
autoExecute,
autoParse,
typeHidden,
_etAlias: typeAlias
},
table ? { table } : {}
Expand Down

0 comments on commit e860bac

Please sign in to comment.