1 |
|
2 | import {DynamicSchema} from './dynamic-schema';
|
3 | import {repositoryMap} from '../core/exports';
|
4 | import * as Utils from '../core/utils';
|
5 | import {MetaUtils} from '../core/metadata/utils';
|
6 | import {Decorators as CoreDecorators} from '../core/constants';
|
7 | import {Decorators} from './constants';
|
8 | import {IEntityParams} from './decorators/interfaces/entity-params';
|
9 | import {IRepositoryParams} from '../core/decorators/interfaces/repository-params';
|
10 | import {pathRepoMap, updateModelEntity, getModel, repoFromModel} from '../core/dynamic/model-entity';
|
11 | import Mongoose = require('mongoose');
|
12 | import * as Enumerable from 'linq';
|
13 | import {sequelizeService} from './sequelizeService';
|
14 | import {GetRepositoryForName} from '../core/dynamic/dynamic-repository';
|
15 |
|
16 | export function generateSchema() {
|
17 | if (Utils.config().SqlConfig.isSqlEnabled == false)
|
18 | return;
|
19 |
|
20 |
|
21 | Utils.entityService(Decorators.ENTITY, sequelizeService);
|
22 | var entities = MetaUtils.getMetaDataForDecorators([CoreDecorators.ENTITY]);
|
23 | var allDynamicSchemas: Array<DynamicSchema> = new Array<DynamicSchema>();
|
24 | entities.forEach(x => {
|
25 | let entityMeta = x.metadata[0];
|
26 | let schemaName = (<IEntityParams>entityMeta.params).tableName;
|
27 | let schema = new DynamicSchema(entityMeta.target, schemaName, <IEntityParams>entityMeta.params);
|
28 | allDynamicSchemas.push(schema);
|
29 | let entitySchema = schema.getSchema();
|
30 |
|
31 | updateModelEntity(schemaName, entityMeta.target, entitySchema, schema);
|
32 | });
|
33 |
|
34 | allDynamicSchemas.forEach(schema => {
|
35 | schema.getRelations()[CoreDecorators.ONETOMANY].forEach(oneToManyRelation => {
|
36 | if(!oneToManyRelation.itemType){
|
37 | oneToManyRelation.itemType = getModel(oneToManyRelation.rel);
|
38 | }
|
39 | let sourceDynamicSchema = schema;
|
40 | let targetDynamicSchema = Enumerable.from(allDynamicSchemas)
|
41 | .where(dynamicSchema => dynamicSchema.schemaName == oneToManyRelation.rel).first();
|
42 | if(oneToManyRelation.properties){
|
43 | if(oneToManyRelation.properties.indexOf(targetDynamicSchema.getSchema().primaryKeyAttribute)<0){
|
44 | oneToManyRelation.properties.push(targetDynamicSchema.getSchema().primaryKeyAttribute);
|
45 | }
|
46 | }
|
47 | sequelizeService.addRelationInSchema(sourceDynamicSchema.getSchema(), targetDynamicSchema.getSchema(), CoreDecorators.ONETOMANY, oneToManyRelation);
|
48 | });
|
49 | })
|
50 |
|
51 | allDynamicSchemas.forEach(schema => {
|
52 | schema.getRelations()[CoreDecorators.MANYTOONE].forEach(manyToOne => {
|
53 | let sourceDynamicSchema = schema;
|
54 | if(!manyToOne.itemType){
|
55 | manyToOne.itemType = getModel(manyToOne.rel);
|
56 | }
|
57 | let targetDynamicSchema:any = Enumerable.from(allDynamicSchemas)
|
58 | .where(dynamicSchema => dynamicSchema.schemaName == manyToOne.rel).first();
|
59 | if(manyToOne.properties){
|
60 | if(manyToOne.properties.indexOf(targetDynamicSchema.getSchema().primaryKeyAttribute)<0){
|
61 | manyToOne.properties.push(targetDynamicSchema.getSchema().primaryKeyAttribute);
|
62 | }
|
63 | }
|
64 | sequelizeService.addRelationInSchema(sourceDynamicSchema.getSchema(), targetDynamicSchema.getSchema(), CoreDecorators.MANYTOONE, manyToOne);
|
65 |
|
66 | });
|
67 | })
|
68 |
|
69 | allDynamicSchemas.forEach(schema => {
|
70 | schema.getRelations()[CoreDecorators.ONETOONE].forEach(onetoone => {
|
71 | let sourceDynamicSchema = schema;
|
72 | if(!onetoone.itemType){
|
73 | onetoone.itemType = getModel(onetoone.rel);
|
74 | }
|
75 | let targetDynamicSchema:any = Enumerable.from(allDynamicSchemas)
|
76 | .where(dynamicSchema => dynamicSchema.schemaName == onetoone.rel).first();
|
77 | if(onetoone.properties){
|
78 | if(onetoone.properties.indexOf(targetDynamicSchema.getSchema().primaryKeyAttribute)<0){
|
79 | onetoone.properties.push(targetDynamicSchema.getSchema().primaryKeyAttribute);
|
80 | }
|
81 | }
|
82 | sequelizeService.addRelationInSchema(sourceDynamicSchema.getSchema(), targetDynamicSchema.getSchema(), CoreDecorators.ONETOONE, onetoone);
|
83 |
|
84 | });
|
85 | })
|
86 |
|
87 |
|
88 | var repositoryMetadata = MetaUtils.getMetaDataForDecorators([CoreDecorators.REPOSITORY]);
|
89 | repositoryMetadata.forEach(x => {
|
90 | if (!x.metadata || !x.metadata.length) {
|
91 | return;
|
92 | }
|
93 | let repositoryParams = <IRepositoryParams>x.metadata[0].params;
|
94 | let entity = (<IRepositoryParams>x.metadata[0].params).model;
|
95 | let meta = MetaUtils.getMetaData(entity, Decorators.ENTITY);
|
96 | if (meta && meta.length > 0) {
|
97 | let entityMeta = meta[0];
|
98 | if (entityMeta) {
|
99 | let schemaName = (<IEntityParams>entityMeta.params).tableName;
|
100 | pathRepoMap[repositoryParams.path] = { schemaName: schemaName, modelType: Decorators.ENTITY };
|
101 |
|
102 | }
|
103 | }
|
104 | });
|
105 | sequelizeService.init();
|
106 |
|
107 | }
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 |
|
131 |
|
132 |
|
133 |
|
134 |
|
135 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 |
|
145 |
|
146 |
|
147 |
|
148 |
|
149 |
|
150 |
|
\ | No newline at end of file |