Skip to content

Commit

Permalink
feat(all): all entities are auto register
Browse files Browse the repository at this point in the history
  • Loading branch information
eyolas committed Oct 3, 2017
1 parent 607d047 commit 1aff6c0
Show file tree
Hide file tree
Showing 10 changed files with 151 additions and 16 deletions.
5 changes: 3 additions & 2 deletions packages/typeorm/package.json
Expand Up @@ -30,8 +30,9 @@
"@gabliam/core": "^2.1.0"
},
"dependencies": {
"caller": "^1.0.1",
"debug": "^2.6.1",
"typeorm": "^0.0.8"
"typeorm": "0.0.8"
},
"devDependencies": {
"@gabliam/core": "^2.1.0",
Expand All @@ -43,7 +44,7 @@
"rimraf": "^2.6.0",
"tslint": "^5.2.0",
"tslint-eslint-rules": "^4.0.0",
"typescript": "^2.3.2",
"typescript": "^2.4.1",
"vrsource-tslint-rules": "^5.1.0"
}
}
8 changes: 8 additions & 0 deletions packages/typeorm/src/constant.ts
@@ -1 +1,9 @@
export const ConnectionOptionsBeanId = Symbol('ConnectionOptionsBeanId');


export const TYPE = {
Entity: 'EntityTypeOrm'
};


export const ENTITIES_PATH = Symbol('TYPEORM/ENTITIES_PATH');
81 changes: 81 additions & 0 deletions packages/typeorm/src/decorators.ts
@@ -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);
};
}
12 changes: 11 additions & 1 deletion packages/typeorm/src/index.ts
@@ -1,12 +1,22 @@
import { interfaces, Scan, inversifyInterfaces, Registry } from '@gabliam/core';
import { Connection } from './typeorm';
import { TYPE, ENTITIES_PATH } from './constant';
import { TypeormRegistry } from './interface';

export * from './typeorm';
export * from './constant';


@Scan(__dirname)
export default class PluginsTypeOrm implements interfaces.GabliamPlugin {
bind(container: inversifyInterfaces.Container, registry: Registry) {
const entitiesPaths = registry
.get<TypeormRegistry>(TYPE.Entity)
.map(({ target }) => {
return target;
});
container.bind<any>(ENTITIES_PATH).toConstantValue(entitiesPaths);
}

async destroy(container: inversifyInterfaces.Container, registry: Registry) {
const connection = container.get<Connection>(Connection);
await connection.close();
Expand Down
5 changes: 5 additions & 0 deletions packages/typeorm/src/interface.ts
@@ -0,0 +1,5 @@
import { interfaces } from '@gabliam/core';

export interface TypeormRegistry extends interfaces.ValueRegistry {
filePath: string;
}
15 changes: 7 additions & 8 deletions packages/typeorm/src/pluginTypeormConfig.ts
@@ -1,6 +1,6 @@
import { PluginConfig, Value, optional, Bean, CORE_CONFIG, inject, interfaces } from '@gabliam/core';
import { PluginConfig, Value, optional, Bean, inject } from '@gabliam/core';
import { ConnectionOptions, Connection, createConnection } from './typeorm';
import { ConnectionOptionsBeanId } from './constant';
import { ConnectionOptionsBeanId, ENTITIES_PATH } from './constant';
import * as d from 'debug';

const debug = d('Gabliam:Plugin:Typeorm');
Expand All @@ -11,16 +11,15 @@ export class PluginTypeormConfig {
@Value('application.typeorm.connectionOptions')
connectionOptions: ConnectionOptions;

@Value('application.typeorm.entitiesPath')
entitiesPath: string;
entitiesPath: string[];

constructor(
@inject(ConnectionOptionsBeanId) @optional() connectionOptions: ConnectionOptions,
@inject(CORE_CONFIG) config: interfaces.GabliamConfig
@inject(ENTITIES_PATH) entitiesPath: string[]
) {
debug('constructor PluginTypeormConfig', connectionOptions, config);
debug('constructor PluginTypeormConfig', connectionOptions);
this.connectionOptions = connectionOptions;
this.entitiesPath = `${config.scanPath}/**/*{.js,.ts}`;
this.entitiesPath = entitiesPath;
}

@Bean(Connection)
Expand All @@ -31,7 +30,7 @@ export class PluginTypeormConfig {
}
const connectionOptions = this.connectionOptions;
const entities: any = connectionOptions.entities || [];
entities.push(this.entitiesPath);
entities.push(...this.entitiesPath);

return createConnection({
...connectionOptions,
Expand Down
18 changes: 18 additions & 0 deletions packages/typeorm/src/typeorm.ts
@@ -1 +1,19 @@
export * from 'typeorm';

import {
Entity,
AbstractEntity,
ClassEntityChild,
SingleEntityChild,
ClosureEntity,
EmbeddableEntity
} from './decorators';

export {
Entity,
AbstractEntity,
ClassEntityChild,
SingleEntityChild,
ClosureEntity,
EmbeddableEntity
};
4 changes: 3 additions & 1 deletion packages/typeorm/tsconfig.json
Expand Up @@ -6,10 +6,12 @@
"moduleResolution": "node",
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"skipDefaultLibCheck": true,
"skipLibCheck": true,
"declaration": true,
"strict": true,
"noUnusedLocals": true,
"sourceMap": false,
"outDir": "lib"
}
}
}
7 changes: 7 additions & 0 deletions packages/typeorm/typings/caller.d.ts
@@ -0,0 +1,7 @@
declare module 'caller' {
interface Caller {
(depths?: number): string;
}
var c: Caller;
export = c;
}
12 changes: 8 additions & 4 deletions packages/typeorm/yarn.lock
Expand Up @@ -196,6 +196,10 @@ builtin-modules@^1.0.0:
version "1.1.1"
resolved "https://registry.yarnpkg.com/builtin-modules/-/builtin-modules-1.1.1.tgz#270f076c5a72c02f5b65a47df94c5fe3a278892f"

caller@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/caller/-/caller-1.0.1.tgz#b851860f70e195db3d277395aa1a7e23ea30ecf5"

camelcase@^2.0.1:
version "2.1.1"
resolved "https://registry.yarnpkg.com/camelcase/-/camelcase-2.1.1.tgz#7c1d16d679a1bbe59ca02cacecfb011e201f5a1f"
Expand Down Expand Up @@ -1376,7 +1380,7 @@ tweetnacl@^0.14.3, tweetnacl@~0.14.0:
version "0.14.5"
resolved "https://registry.yarnpkg.com/tweetnacl/-/tweetnacl-0.14.5.tgz#5ae68177f192d4456269d108afa93ff8743f4f64"

typeorm@^0.0.8:
typeorm@0.0.8:
version "0.0.8"
resolved "https://registry.yarnpkg.com/typeorm/-/typeorm-0.0.8.tgz#1b541e0c11c0b856f358387c0f8d5ab224e810f9"
dependencies:
Expand All @@ -1386,9 +1390,9 @@ typeorm@^0.0.8:
yargonaut "^1.1.2"
yargs "^6.6.0"

typescript@^2.3.2:
version "2.3.2"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-2.3.2.tgz#f0f045e196f69a72f06b25fd3bd39d01c3ce9984"
typescript@^2.4.1:
version "2.4.1"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-2.4.1.tgz#c3ccb16ddaa0b2314de031e7e6fee89e5ba346bc"

uid-number@^0.0.6:
version "0.0.6"
Expand Down

0 comments on commit 1aff6c0

Please sign in to comment.