This repository has been archived by the owner on Nov 25, 2020. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(project): add identifier decorator to support the duplicated res…
…ources
- Loading branch information
1 parent
ea7346f
commit 286453f
Showing
13 changed files
with
255 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,16 @@ | |||
import {OrmMetadata} from '../orm-metadata'; | |||
|
|||
/** | |||
* Set the 'identifierName' metadata on the entity | |||
* | |||
* @param {string} identifierName The name of the identifier | |||
* | |||
* @return {function} | |||
* | |||
* @decorator | |||
*/ | |||
export function identifier(identifierName) { | |||
return function(target) { | |||
OrmMetadata.forTarget(target).put('identifier', identifierName || target.name.toLowerCase()); | |||
}; | |||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,40 @@ | |||
import {WithIdentifier} from '../resources/entity/with-identifier'; | |||
import {FirstWithSameResource, SecondWithSameResource} from '../resources/entity/with-same-resource'; | |||
import {FirstWithSameResourceAndIdentifier, SecondWithSameResourceAndIdentifier} from '../resources/entity/with-same-resource-and-identifier'; | |||
import {OrmMetadata} from '../../src/orm-metadata'; | |||
import {Entity} from '../../src/entity'; | |||
import {EntityManager} from '../../src/entity-manager'; | |||
import {Container} from 'aurelia-dependency-injection'; | |||
|
|||
describe('@identifier()', function () { | |||
it('Should add identifier on the entity', function () { | |||
expect(OrmMetadata.forTarget(WithIdentifier).fetch('identifier')).toEqual('with-identifier'); | |||
}); | |||
|
|||
it('Should override the first entity when the same resource key is used without the identifier decorator', function () { | |||
let entityManager = new EntityManager(new Container()); | |||
|
|||
entityManager.registerEntities([FirstWithSameResource, SecondWithSameResource]); | |||
|
|||
expect(Object.keys(entityManager.entities).length).toBe(1); | |||
|
|||
expect(entityManager.entities).toEqual({ 'with-duplicated-resource': SecondWithSameResource }); | |||
expect(entityManager.entities).not.toEqual({ 'with-duplicated-resource': FirstWithSameResource }); | |||
}); | |||
|
|||
it('Should register two entities when the identifier decorator is applied with both the same resource name', function () { | |||
let entityManager = new EntityManager(new Container()); | |||
|
|||
entityManager.registerEntities([FirstWithSameResourceAndIdentifier, SecondWithSameResourceAndIdentifier]); | |||
|
|||
expect(Object.keys(entityManager.entities).length).toBe(2); | |||
|
|||
expect(OrmMetadata.forTarget(FirstWithSameResourceAndIdentifier).fetch('identifier')).toEqual('with-identifier-foo'); | |||
expect(OrmMetadata.forTarget(FirstWithSameResourceAndIdentifier).fetch('resource')).toEqual('with-duplicated-resource'); | |||
expect(OrmMetadata.forTarget(FirstWithSameResourceAndIdentifier).fetch('endpoint')).toEqual('with-endpoint-foo'); | |||
|
|||
expect(OrmMetadata.forTarget(SecondWithSameResourceAndIdentifier).fetch('identifier')).toEqual('with-identifier-bar'); | |||
expect(OrmMetadata.forTarget(SecondWithSameResourceAndIdentifier).fetch('resource')).toEqual('with-duplicated-resource'); | |||
expect(OrmMetadata.forTarget(SecondWithSameResourceAndIdentifier).fetch('endpoint')).toEqual('with-endpoint-bar'); | |||
}); | |||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,11 @@ | |||
import {identifier} from '../../../src/decorator/identifier'; | |||
import {resource} from '../../../src/decorator/resource'; | |||
import {idProperty} from '../../../src/decorator/idProperty'; | |||
import {Entity} from '../../../src/entity'; | |||
|
|||
@identifier('with-identifier') | |||
@resource('with-resource') | |||
@idProperty('idTag') | |||
export class WithIdentifier extends Entity { | |||
foo = null; | |||
} |
20 changes: 20 additions & 0 deletions
20
test/resources/entity/with-same-resource-and-identifier.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,20 @@ | |||
import {endpoint} from '../../../src/decorator/endpoint'; | |||
import {identifier} from '../../../src/decorator/identifier'; | |||
import {resource} from '../../../src/decorator/resource'; | |||
import {Entity} from '../../../src/entity'; | |||
|
|||
@identifier('with-identifier-foo') | |||
@resource('with-duplicated-resource') | |||
@endpoint('with-endpoint-foo') | |||
export class FirstWithSameResourceAndIdentifier extends Entity { | |||
foo = null; | |||
} | |||
|
|||
|
|||
@identifier('with-identifier-bar') | |||
@resource('with-duplicated-resource') | |||
@endpoint('with-endpoint-bar') | |||
export class SecondWithSameResourceAndIdentifier extends Entity { | |||
foo = null; | |||
} | |||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,18 @@ | |||
import {endpoint} from '../../../src/decorator/endpoint'; | |||
import {identifier} from '../../../src/decorator/identifier'; | |||
import {resource} from '../../../src/decorator/resource'; | |||
import {Entity} from '../../../src/entity'; | |||
|
|||
@resource('with-duplicated-resource') | |||
@endpoint('with-endpoint-foo') | |||
export class FirstWithSameResource extends Entity { | |||
foo = null; | |||
} | |||
|
|||
|
|||
@resource('with-duplicated-resource') | |||
@endpoint('with-endpoint-bar') | |||
export class SecondWithSameResource extends Entity { | |||
foo = null; | |||
} | |||
|