Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(all): all entities are auto register
- Loading branch information
Showing
10 changed files
with
151 additions
and
16 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 | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,9 @@ | ||
export const ConnectionOptionsBeanId = Symbol('ConnectionOptionsBeanId'); | ||
|
||
|
||
export const TYPE = { | ||
Entity: 'EntityTypeOrm' | ||
}; | ||
|
||
|
||
export const ENTITIES_PATH = Symbol('TYPEORM/ENTITIES_PATH'); |
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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import { | ||
Entity as typeormEntity, | ||
AbstractEntity as typeormAbstractEntity, | ||
ClassEntityChild as typeormClassEntityChild, | ||
SingleEntityChild as typeormSingleEntityChild, | ||
ClosureEntity as typeormClosureEntity, | ||
EmbeddableEntity as typeormEmbeddableEntity, | ||
EntityOptions | ||
} from 'typeorm'; | ||
import { register } from '@gabliam/core'; | ||
import { TYPE } from './constant'; | ||
import * as caller from 'caller'; | ||
|
||
|
||
/** | ||
* This decorator is used to mark classes that will be an entity (table or document depend on database type). | ||
* Database schema will be created for all classes decorated with it, and Repository can be retrieved and used for it. | ||
*/ | ||
export function Entity(name?: string, options?: EntityOptions) { | ||
const filePath = caller(); | ||
return (target: Function) => { | ||
register(TYPE.Entity, { id: null, target, filePath })(target); | ||
typeormEntity(name, options)(target); | ||
}; | ||
} | ||
|
||
/** | ||
* Abstract entity is a class that contains columns and relations for all entities that will inherit this entity. | ||
* Database table for the abstract entity is not created. | ||
*/ | ||
export function AbstractEntity() { | ||
const filePath = caller(); | ||
return (target: Function) => { | ||
register(TYPE.Entity, { id: null, target, filePath })(target); | ||
typeormAbstractEntity()(target); | ||
}; | ||
} | ||
|
||
/** | ||
* Special type of the entity used in the class-table inherited tables. | ||
*/ | ||
export function ClassEntityChild(tableName?: string, options?: EntityOptions) { | ||
const filePath = caller(); | ||
return (target: Function) => { | ||
register(TYPE.Entity, { id: null, target, filePath })(target); | ||
typeormClassEntityChild(tableName, options)(target); | ||
}; | ||
} | ||
|
||
/** | ||
* Special type of the table used in the single-table inherited tables. | ||
*/ | ||
export function SingleEntityChild() { | ||
const filePath = caller(); | ||
return (target: Function) => { | ||
register(TYPE.Entity, { id: null, target, filePath })(target); | ||
typeormSingleEntityChild()(target); | ||
}; | ||
} | ||
|
||
/** | ||
* Used on a entities that stores its children in a tree using closure design pattern. | ||
*/ | ||
export function ClosureEntity(name?: string, options?: EntityOptions) { | ||
const filePath = caller(); | ||
return (target: Function) => { | ||
register(TYPE.Entity, { id: null, target, filePath })(target); | ||
typeormClosureEntity(name, options)(target); | ||
}; | ||
} | ||
|
||
/** | ||
* This decorator is used on the entities that must be embedded into another entities. | ||
*/ | ||
export function EmbeddableEntity() { | ||
const filePath = caller(); | ||
return (target: Function) => { | ||
register(TYPE.Entity, { id: null, target, filePath })(target); | ||
typeormEmbeddableEntity()(target); | ||
}; | ||
} |
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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { interfaces } from '@gabliam/core'; | ||
|
||
export interface TypeormRegistry extends interfaces.ValueRegistry { | ||
filePath: string; | ||
} |
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 | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,19 @@ | ||
export * from 'typeorm'; | ||
|
||
import { | ||
Entity, | ||
AbstractEntity, | ||
ClassEntityChild, | ||
SingleEntityChild, | ||
ClosureEntity, | ||
EmbeddableEntity | ||
} from './decorators'; | ||
|
||
export { | ||
Entity, | ||
AbstractEntity, | ||
ClassEntityChild, | ||
SingleEntityChild, | ||
ClosureEntity, | ||
EmbeddableEntity | ||
}; |
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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
declare module 'caller' { | ||
interface Caller { | ||
(depths?: number): string; | ||
} | ||
var c: Caller; | ||
export = c; | ||
} |
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