1 | "use strict";
|
2 |
|
3 | const dynamic_schema_1 = require('./dynamic-schema');
|
4 | const Utils = require('../core/utils');
|
5 | const utils_1 = require('../core/metadata/utils');
|
6 | const constants_1 = require('../core/constants');
|
7 | const constants_2 = require('./constants');
|
8 | const model_entity_1 = require('../core/dynamic/model-entity');
|
9 | const Enumerable = require('linq');
|
10 | const sequelizeService_1 = require('./sequelizeService');
|
11 | function generateSchema() {
|
12 | if (Utils.config().SqlConfig.isSqlEnabled == false)
|
13 | return;
|
14 |
|
15 | Utils.entityService(constants_2.Decorators.ENTITY, sequelizeService_1.sequelizeService);
|
16 | var entities = utils_1.MetaUtils.getMetaDataForDecorators([constants_1.Decorators.ENTITY]);
|
17 | var allDynamicSchemas = new Array();
|
18 | entities.forEach(x => {
|
19 | let entityMeta = x.metadata[0];
|
20 | let schemaName = entityMeta.params.tableName;
|
21 | let schema = new dynamic_schema_1.DynamicSchema(entityMeta.target, schemaName, entityMeta.params);
|
22 | allDynamicSchemas.push(schema);
|
23 | let entitySchema = schema.getSchema();
|
24 |
|
25 | model_entity_1.updateModelEntity(schemaName, entityMeta.target, entitySchema, schema);
|
26 | });
|
27 | allDynamicSchemas.forEach(schema => {
|
28 | schema.getRelations()[constants_1.Decorators.ONETOMANY].forEach(oneToManyRelation => {
|
29 | if (!oneToManyRelation.itemType) {
|
30 | oneToManyRelation.itemType = model_entity_1.getModel(oneToManyRelation.rel);
|
31 | }
|
32 | let sourceDynamicSchema = schema;
|
33 | let targetDynamicSchema = Enumerable.from(allDynamicSchemas)
|
34 | .where(dynamicSchema => dynamicSchema.schemaName == oneToManyRelation.rel).first();
|
35 | if (oneToManyRelation.properties) {
|
36 | if (oneToManyRelation.properties.indexOf(targetDynamicSchema.getSchema().primaryKeyAttribute) < 0) {
|
37 | oneToManyRelation.properties.push(targetDynamicSchema.getSchema().primaryKeyAttribute);
|
38 | }
|
39 | }
|
40 | sequelizeService_1.sequelizeService.addRelationInSchema(sourceDynamicSchema.getSchema(), targetDynamicSchema.getSchema(), constants_1.Decorators.ONETOMANY, oneToManyRelation);
|
41 | });
|
42 | });
|
43 | allDynamicSchemas.forEach(schema => {
|
44 | schema.getRelations()[constants_1.Decorators.MANYTOONE].forEach(manyToOne => {
|
45 | let sourceDynamicSchema = schema;
|
46 | if (!manyToOne.itemType) {
|
47 | manyToOne.itemType = model_entity_1.getModel(manyToOne.rel);
|
48 | }
|
49 | let targetDynamicSchema = Enumerable.from(allDynamicSchemas)
|
50 | .where(dynamicSchema => dynamicSchema.schemaName == manyToOne.rel).first();
|
51 | if (manyToOne.properties) {
|
52 | if (manyToOne.properties.indexOf(targetDynamicSchema.getSchema().primaryKeyAttribute) < 0) {
|
53 | manyToOne.properties.push(targetDynamicSchema.getSchema().primaryKeyAttribute);
|
54 | }
|
55 | }
|
56 | sequelizeService_1.sequelizeService.addRelationInSchema(sourceDynamicSchema.getSchema(), targetDynamicSchema.getSchema(), constants_1.Decorators.MANYTOONE, manyToOne);
|
57 | });
|
58 | });
|
59 | allDynamicSchemas.forEach(schema => {
|
60 | schema.getRelations()[constants_1.Decorators.ONETOONE].forEach(onetoone => {
|
61 | let sourceDynamicSchema = schema;
|
62 | if (!onetoone.itemType) {
|
63 | onetoone.itemType = model_entity_1.getModel(onetoone.rel);
|
64 | }
|
65 | let targetDynamicSchema = Enumerable.from(allDynamicSchemas)
|
66 | .where(dynamicSchema => dynamicSchema.schemaName == onetoone.rel).first();
|
67 | if (onetoone.properties) {
|
68 | if (onetoone.properties.indexOf(targetDynamicSchema.getSchema().primaryKeyAttribute) < 0) {
|
69 | onetoone.properties.push(targetDynamicSchema.getSchema().primaryKeyAttribute);
|
70 | }
|
71 | }
|
72 | sequelizeService_1.sequelizeService.addRelationInSchema(sourceDynamicSchema.getSchema(), targetDynamicSchema.getSchema(), constants_1.Decorators.ONETOONE, onetoone);
|
73 | });
|
74 | });
|
75 | var repositoryMetadata = utils_1.MetaUtils.getMetaDataForDecorators([constants_1.Decorators.REPOSITORY]);
|
76 | repositoryMetadata.forEach(x => {
|
77 | if (!x.metadata || !x.metadata.length) {
|
78 | return;
|
79 | }
|
80 | let repositoryParams = x.metadata[0].params;
|
81 | let entity = x.metadata[0].params.model;
|
82 | let meta = utils_1.MetaUtils.getMetaData(entity, constants_2.Decorators.ENTITY);
|
83 | if (meta && meta.length > 0) {
|
84 | let entityMeta = meta[0];
|
85 | if (entityMeta) {
|
86 | let schemaName = entityMeta.params.tableName;
|
87 | model_entity_1.pathRepoMap[repositoryParams.path] = { schemaName: schemaName, modelType: constants_2.Decorators.ENTITY };
|
88 | }
|
89 | }
|
90 | });
|
91 | sequelizeService_1.sequelizeService.init();
|
92 | }
|
93 | exports.generateSchema = generateSchema;
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
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 |
|