oak-db 3.3.11 → 3.3.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,308 +1,430 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.MysqlStore = void 0;
4
- const tslib_1 = require("tslib");
5
- const CascadeStore_1 = require("oak-domain/lib/store/CascadeStore");
6
- const connector_1 = require("./connector");
7
- const translator_1 = require("./translator");
8
- const lodash_1 = require("lodash");
9
- const assert_1 = tslib_1.__importDefault(require("assert"));
10
- const relation_1 = require("oak-domain/lib/store/relation");
11
- function convertGeoTextToObject(geoText) {
12
- if (geoText.startsWith('POINT')) {
13
- const coord = geoText.match((/(\d|\.)+(?=\)|\s)/g));
14
- (0, assert_1.default)(coord.length === 2);
15
- return {
16
- type: 'point',
17
- coordinate: coord.map(ele => parseFloat(ele)),
18
- };
19
- }
20
- else {
21
- throw new Error('only support Point now');
22
- }
23
- }
24
- class MysqlStore extends CascadeStore_1.CascadeStore {
25
- countAbjointRow(entity, selection, context, option) {
26
- throw new Error('MySQL store不支持同步取数据,不应该跑到这儿');
27
- }
28
- aggregateAbjointRowSync(entity, aggregation, context, option) {
29
- throw new Error('MySQL store不支持同步取数据,不应该跑到这儿');
30
- }
31
- selectAbjointRow(entity, selection, context, option) {
32
- throw new Error('MySQL store不支持同步取数据,不应该跑到这儿');
33
- }
34
- updateAbjointRow(entity, operation, context, option) {
35
- throw new Error('MySQL store不支持同步更新数据,不应该跑到这儿');
36
- }
37
- async exec(script, txnId) {
38
- await this.connector.exec(script, txnId);
39
- }
40
- connector;
41
- translator;
42
- constructor(storageSchema, configuration) {
43
- super(storageSchema);
44
- this.connector = new connector_1.MySqlConnector(configuration);
45
- this.translator = new translator_1.MySqlTranslator(storageSchema);
46
- }
47
- checkRelationAsync(entity, operation, context) {
48
- throw new Error('Method not implemented.');
49
- }
50
- async aggregateAbjointRowAsync(entity, aggregation, context, option) {
51
- const sql = this.translator.translateAggregate(entity, aggregation, option);
52
- const result = await this.connector.exec(sql, context.getCurrentTxnId());
53
- return this.formResult(entity, result[0]);
54
- }
55
- aggregate(entity, aggregation, context, option) {
56
- return this.aggregateAsync(entity, aggregation, context, option);
57
- }
58
- supportManyToOneJoin() {
59
- return true;
60
- }
61
- supportMultipleCreate() {
62
- return true;
63
- }
64
- formResult(entity, result) {
65
- const schema = this.getSchema();
66
- /* function resolveObject(r: Record<string, any>, path: string, value: any) {
67
- const i = path.indexOf(".");
68
- const bs = path.indexOf('[');
69
- const be = path.indexOf(']');
70
- if (i === -1 && bs === -1) {
71
- r[i] = value;
72
- }
73
- else if (i === -1) {
74
-
75
- }
76
- else if (bs === -1) {
77
- const attrHead = path.slice(0, i);
78
- const attrTail = path.slice(i + 1);
79
- if (!r[attrHead]) {
80
- r[attrHead] = {};
81
- }
82
- resolveObject(r[attrHead], attrTail, value);
83
- }
84
- } */
85
- function resolveAttribute(entity2, r, attr, value) {
86
- const { attributes, view } = schema[entity2];
87
- if (!view) {
88
- const i = attr.indexOf(".");
89
- if (i !== -1) {
90
- const attrHead = attr.slice(0, i);
91
- const attrTail = attr.slice(i + 1);
92
- const rel = (0, relation_1.judgeRelation)(schema, entity2, attrHead);
93
- if (rel === 1) {
94
- (0, lodash_1.set)(r, attr, value);
95
- }
96
- else {
97
- if (!r[attrHead]) {
98
- r[attrHead] = {};
99
- }
100
- if (rel === 0) {
101
- resolveAttribute(entity2, r[attrHead], attrTail, value);
102
- }
103
- else if (rel === 2) {
104
- resolveAttribute(attrHead, r[attrHead], attrTail, value);
105
- }
106
- else {
107
- (0, assert_1.default)(typeof rel === 'string');
108
- resolveAttribute(rel, r[attrHead], attrTail, value);
109
- }
110
- }
111
- }
112
- else if (attributes[attr]) {
113
- const { type } = attributes[attr];
114
- switch (type) {
115
- case 'date':
116
- case 'time': {
117
- if (value instanceof Date) {
118
- r[attr] = value.valueOf();
119
- }
120
- else {
121
- r[attr] = value;
122
- }
123
- break;
124
- }
125
- case 'geometry': {
126
- if (typeof value === 'string') {
127
- r[attr] = convertGeoTextToObject(value);
128
- }
129
- else {
130
- r[attr] = value;
131
- }
132
- break;
133
- }
134
- case 'object':
135
- case 'array': {
136
- if (typeof value === 'string') {
137
- r[attr] = JSON.parse(value.replace(/[\r]/g, '\\r').replace(/[\n]/g, '\\n'));
138
- }
139
- else {
140
- r[attr] = value;
141
- }
142
- break;
143
- }
144
- case 'function': {
145
- if (typeof value === 'string') {
146
- // 函数的执行环境需要的参数只有创建函数者知悉,只能由上层再创建Function
147
- r[attr] = `return ${Buffer.from(value, 'base64').toString()}`;
148
- }
149
- else {
150
- r[attr] = value;
151
- }
152
- break;
153
- }
154
- case 'bool':
155
- case 'boolean': {
156
- if (value === 0) {
157
- r[attr] = false;
158
- }
159
- else if (value === 1) {
160
- r[attr] = true;
161
- }
162
- else {
163
- r[attr] = value;
164
- }
165
- break;
166
- }
167
- case 'decimal': {
168
- // mysql内部取回decimal是字符串
169
- if (typeof value === 'string') {
170
- r[attr] = parseFloat(value);
171
- }
172
- else {
173
- (0, assert_1.default)(value === null || typeof value === 'number');
174
- r[attr] = value;
175
- }
176
- break;
177
- }
178
- default: {
179
- r[attr] = value;
180
- }
181
- }
182
- }
183
- else {
184
- r[attr] = value;
185
- }
186
- }
187
- else {
188
- (0, lodash_1.assign)(r, {
189
- [attr]: value,
190
- });
191
- }
192
- }
193
- function removeNullObjects(r, e) {
194
- // assert(r.id && typeof r.id === 'string', `对象${<string>e}取数据时发现id为非法值${r.id},rowId是${r.id}`)
195
- for (let attr in r) {
196
- const rel = (0, relation_1.judgeRelation)(schema, e, attr);
197
- if (rel === 2) {
198
- // 边界,如果是toModi的对象,这里的外键确实有可能为空
199
- // assert(schema[e].toModi || r.entity !== attr || r.entityId === r[attr].id, `对象${<string>e}取数据时,发现entityId与连接的对象的主键不一致,rowId是${r.id},其entityId值为${r.entityId},连接的对象的主键为${r[attr].id}`);
200
- if (r[attr].id === null) {
201
- (0, assert_1.default)(schema[e].toModi || r.entity !== attr);
202
- delete r[attr];
203
- continue;
204
- }
205
- // assert(r.entity === attr, `对象${<string>e}取数据时,发现entity值与连接的外键对象不一致,rowId是${r.id},其entity值为${r.entity},连接的对象为${attr}`);
206
- removeNullObjects(r[attr], attr);
207
- }
208
- else if (typeof rel === 'string') {
209
- // 边界,如果是toModi的对象,这里的外键确实有可能为空
210
- // assert(schema[e].toModi || r[`${attr}Id`] === r[attr].id, `对象${<string>e}取数据时,发现其外键与连接的对象的主键不一致,rowId是${r.id},其${attr}Id值为${r[`${attr}Id`]},连接的对象的主键为${r[attr].id}`);
211
- if (r[attr].id === null) {
212
- (0, assert_1.default)(schema[e].toModi || r[`${attr}Id`] === null);
213
- delete r[attr];
214
- continue;
215
- }
216
- removeNullObjects(r[attr], rel);
217
- }
218
- }
219
- }
220
- function formSingleRow(r) {
221
- let result2 = {};
222
- for (let attr in r) {
223
- const value = r[attr];
224
- resolveAttribute(entity, result2, attr, value);
225
- }
226
- removeNullObjects(result2, entity);
227
- return result2;
228
- }
229
- if (result instanceof Array) {
230
- return result.map(r => formSingleRow(r));
231
- }
232
- return formSingleRow(result);
233
- }
234
- async selectAbjointRowAsync(entity, selection, context, option) {
235
- const sql = this.translator.translateSelect(entity, selection, option);
236
- const result = await this.connector.exec(sql, context.getCurrentTxnId());
237
- return this.formResult(entity, result[0]);
238
- }
239
- async updateAbjointRowAsync(entity, operation, context, option) {
240
- const { translator, connector } = this;
241
- const { action } = operation;
242
- const txn = context.getCurrentTxnId();
243
- switch (action) {
244
- case 'create': {
245
- const { data } = operation;
246
- const sql = translator.translateInsert(entity, data instanceof Array ? data : [data]);
247
- const result = await connector.exec(sql, txn);
248
- return result[0].affectedRows;
249
- }
250
- case 'remove': {
251
- const sql = translator.translateRemove(entity, operation, option);
252
- const result = await connector.exec(sql, txn);
253
- // todo 这里对sorter和indexfrom/count的支持不完整
254
- return result[0].changedRows;
255
- }
256
- default: {
257
- (0, assert_1.default)(!['select', 'download', 'stat'].includes(action));
258
- const sql = translator.translateUpdate(entity, operation, option);
259
- const result = await connector.exec(sql, txn);
260
- // todo 这里对sorter和indexfrom/count的支持不完整
261
- return result[0].changedRows;
262
- }
263
- }
264
- }
265
- async operate(entity, operation, context, option) {
266
- const { action } = operation;
267
- (0, assert_1.default)(!['select', 'download', 'stat'].includes(action), '现在不支持使用select operation');
268
- return await super.operateAsync(entity, operation, context, option);
269
- }
270
- async select(entity, selection, context, option) {
271
- const result = await super.selectAsync(entity, selection, context, option);
272
- return result;
273
- }
274
- async countAbjointRowAsync(entity, selection, context, option) {
275
- const sql = this.translator.translateCount(entity, selection, option);
276
- const result = await this.connector.exec(sql, context.getCurrentTxnId());
277
- return result[0][0].cnt;
278
- }
279
- async count(entity, selection, context, option) {
280
- return this.countAsync(entity, selection, context, option);
281
- }
282
- async begin(option) {
283
- const txn = await this.connector.startTransaction(option);
284
- return txn;
285
- }
286
- async commit(txnId) {
287
- await this.connector.commitTransaction(txnId);
288
- }
289
- async rollback(txnId) {
290
- await this.connector.rollbackTransaction(txnId);
291
- }
292
- connect() {
293
- this.connector.connect();
294
- }
295
- async disconnect() {
296
- await this.connector.disconnect();
297
- }
298
- async initialize(option) {
299
- const schema = this.getSchema();
300
- for (const entity in schema) {
301
- const sqls = this.translator.translateCreateEntity(entity, option);
302
- for (const sql of sqls) {
303
- await this.connector.exec(sql);
304
- }
305
- }
306
- }
307
- }
308
- exports.MysqlStore = MysqlStore;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.MysqlStore = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const CascadeStore_1 = require("oak-domain/lib/store/CascadeStore");
6
+ const connector_1 = require("./connector");
7
+ const translator_1 = require("./translator");
8
+ const lodash_1 = require("lodash");
9
+ const assert_1 = tslib_1.__importDefault(require("assert"));
10
+ const relation_1 = require("oak-domain/lib/store/relation");
11
+ function convertGeoTextToObject(geoText) {
12
+ if (geoText.startsWith('POINT')) {
13
+ const coord = geoText.match((/(\d|\.)+(?=\)|\s)/g));
14
+ (0, assert_1.default)(coord.length === 2);
15
+ return {
16
+ type: 'point',
17
+ coordinate: coord.map(ele => parseFloat(ele)),
18
+ };
19
+ }
20
+ else {
21
+ throw new Error('only support Point now');
22
+ }
23
+ }
24
+ class MysqlStore extends CascadeStore_1.CascadeStore {
25
+ countAbjointRow(entity, selection, context, option) {
26
+ throw new Error('MySQL store不支持同步取数据,不应该跑到这儿');
27
+ }
28
+ aggregateAbjointRowSync(entity, aggregation, context, option) {
29
+ throw new Error('MySQL store不支持同步取数据,不应该跑到这儿');
30
+ }
31
+ selectAbjointRow(entity, selection, context, option) {
32
+ throw new Error('MySQL store不支持同步取数据,不应该跑到这儿');
33
+ }
34
+ updateAbjointRow(entity, operation, context, option) {
35
+ throw new Error('MySQL store不支持同步更新数据,不应该跑到这儿');
36
+ }
37
+ async exec(script, txnId) {
38
+ await this.connector.exec(script, txnId);
39
+ }
40
+ connector;
41
+ translator;
42
+ constructor(storageSchema, configuration) {
43
+ super(storageSchema);
44
+ this.connector = new connector_1.MySqlConnector(configuration);
45
+ this.translator = new translator_1.MySqlTranslator(storageSchema);
46
+ }
47
+ checkRelationAsync(entity, operation, context) {
48
+ throw new Error('Method not implemented.');
49
+ }
50
+ async aggregateAbjointRowAsync(entity, aggregation, context, option) {
51
+ const sql = this.translator.translateAggregate(entity, aggregation, option);
52
+ const result = await this.connector.exec(sql, context.getCurrentTxnId());
53
+ return this.formResult(entity, result[0]);
54
+ }
55
+ aggregate(entity, aggregation, context, option) {
56
+ return this.aggregateAsync(entity, aggregation, context, option);
57
+ }
58
+ supportManyToOneJoin() {
59
+ return true;
60
+ }
61
+ supportMultipleCreate() {
62
+ return true;
63
+ }
64
+ formResult(entity, result) {
65
+ const schema = this.getSchema();
66
+ /* function resolveObject(r: Record<string, any>, path: string, value: any) {
67
+ const i = path.indexOf(".");
68
+ const bs = path.indexOf('[');
69
+ const be = path.indexOf(']');
70
+ if (i === -1 && bs === -1) {
71
+ r[i] = value;
72
+ }
73
+ else if (i === -1) {
74
+
75
+ }
76
+ else if (bs === -1) {
77
+ const attrHead = path.slice(0, i);
78
+ const attrTail = path.slice(i + 1);
79
+ if (!r[attrHead]) {
80
+ r[attrHead] = {};
81
+ }
82
+ resolveObject(r[attrHead], attrTail, value);
83
+ }
84
+ } */
85
+ function resolveAttribute(entity2, r, attr, value) {
86
+ const { attributes, view } = schema[entity2];
87
+ if (!view) {
88
+ const i = attr.indexOf(".");
89
+ if (i !== -1) {
90
+ const attrHead = attr.slice(0, i);
91
+ const attrTail = attr.slice(i + 1);
92
+ const rel = (0, relation_1.judgeRelation)(schema, entity2, attrHead);
93
+ if (rel === 1) {
94
+ (0, lodash_1.set)(r, attr, value);
95
+ }
96
+ else {
97
+ if (!r[attrHead]) {
98
+ r[attrHead] = {};
99
+ }
100
+ if (rel === 0) {
101
+ resolveAttribute(entity2, r[attrHead], attrTail, value);
102
+ }
103
+ else if (rel === 2) {
104
+ resolveAttribute(attrHead, r[attrHead], attrTail, value);
105
+ }
106
+ else {
107
+ (0, assert_1.default)(typeof rel === 'string');
108
+ resolveAttribute(rel, r[attrHead], attrTail, value);
109
+ }
110
+ }
111
+ }
112
+ else if (attributes[attr]) {
113
+ const { type } = attributes[attr];
114
+ switch (type) {
115
+ case 'date':
116
+ case 'time': {
117
+ if (value instanceof Date) {
118
+ r[attr] = value.valueOf();
119
+ }
120
+ else {
121
+ r[attr] = value;
122
+ }
123
+ break;
124
+ }
125
+ case 'geometry': {
126
+ if (typeof value === 'string') {
127
+ r[attr] = convertGeoTextToObject(value);
128
+ }
129
+ else {
130
+ r[attr] = value;
131
+ }
132
+ break;
133
+ }
134
+ case 'object':
135
+ case 'array': {
136
+ if (typeof value === 'string') {
137
+ r[attr] = JSON.parse(value.replace(/[\r]/g, '\\r').replace(/[\n]/g, '\\n'));
138
+ }
139
+ else {
140
+ r[attr] = value;
141
+ }
142
+ break;
143
+ }
144
+ case 'function': {
145
+ if (typeof value === 'string') {
146
+ // 函数的执行环境需要的参数只有创建函数者知悉,只能由上层再创建Function
147
+ r[attr] = `return ${Buffer.from(value, 'base64').toString()}`;
148
+ }
149
+ else {
150
+ r[attr] = value;
151
+ }
152
+ break;
153
+ }
154
+ case 'bool':
155
+ case 'boolean': {
156
+ if (value === 0) {
157
+ r[attr] = false;
158
+ }
159
+ else if (value === 1) {
160
+ r[attr] = true;
161
+ }
162
+ else {
163
+ r[attr] = value;
164
+ }
165
+ break;
166
+ }
167
+ case 'decimal': {
168
+ // mysql内部取回decimal是字符串
169
+ if (typeof value === 'string') {
170
+ r[attr] = parseFloat(value);
171
+ }
172
+ else {
173
+ (0, assert_1.default)(value === null || typeof value === 'number');
174
+ r[attr] = value;
175
+ }
176
+ break;
177
+ }
178
+ default: {
179
+ r[attr] = value;
180
+ }
181
+ }
182
+ }
183
+ else {
184
+ r[attr] = value;
185
+ }
186
+ }
187
+ else {
188
+ (0, lodash_1.assign)(r, {
189
+ [attr]: value,
190
+ });
191
+ }
192
+ }
193
+ function removeNullObjects(r, e) {
194
+ // assert(r.id && typeof r.id === 'string', `对象${<string>e}取数据时发现id为非法值${r.id},rowId是${r.id}`)
195
+ for (let attr in r) {
196
+ const rel = (0, relation_1.judgeRelation)(schema, e, attr);
197
+ if (rel === 2) {
198
+ // 边界,如果是toModi的对象,这里的外键确实有可能为空
199
+ // assert(schema[e].toModi || r.entity !== attr || r.entityId === r[attr].id, `对象${<string>e}取数据时,发现entityId与连接的对象的主键不一致,rowId是${r.id},其entityId值为${r.entityId},连接的对象的主键为${r[attr].id}`);
200
+ if (r[attr].id === null) {
201
+ (0, assert_1.default)(schema[e].toModi || r.entity !== attr);
202
+ delete r[attr];
203
+ continue;
204
+ }
205
+ // assert(r.entity === attr, `对象${<string>e}取数据时,发现entity值与连接的外键对象不一致,rowId是${r.id},其entity值为${r.entity},连接的对象为${attr}`);
206
+ removeNullObjects(r[attr], attr);
207
+ }
208
+ else if (typeof rel === 'string') {
209
+ // 边界,如果是toModi的对象,这里的外键确实有可能为空
210
+ // assert(schema[e].toModi || r[`${attr}Id`] === r[attr].id, `对象${<string>e}取数据时,发现其外键与连接的对象的主键不一致,rowId是${r.id},其${attr}Id值为${r[`${attr}Id`]},连接的对象的主键为${r[attr].id}`);
211
+ if (r[attr].id === null) {
212
+ (0, assert_1.default)(schema[e].toModi || r[`${attr}Id`] === null, `对象${String(e)}取数据时,发现其外键找不到目标对象,rowId是${r.id},其外键${attr}Id值为${r[`${attr}Id`]}`);
213
+ delete r[attr];
214
+ continue;
215
+ }
216
+ removeNullObjects(r[attr], rel);
217
+ }
218
+ }
219
+ }
220
+ function formSingleRow(r) {
221
+ let result2 = {};
222
+ for (let attr in r) {
223
+ const value = r[attr];
224
+ resolveAttribute(entity, result2, attr, value);
225
+ }
226
+ removeNullObjects(result2, entity);
227
+ return result2;
228
+ }
229
+ if (result instanceof Array) {
230
+ return result.map(r => formSingleRow(r));
231
+ }
232
+ return formSingleRow(result);
233
+ }
234
+ async selectAbjointRowAsync(entity, selection, context, option) {
235
+ const sql = this.translator.translateSelect(entity, selection, option);
236
+ const result = await this.connector.exec(sql, context.getCurrentTxnId());
237
+ return this.formResult(entity, result[0]);
238
+ }
239
+ async updateAbjointRowAsync(entity, operation, context, option) {
240
+ const { translator, connector } = this;
241
+ const { action } = operation;
242
+ const txn = context.getCurrentTxnId();
243
+ switch (action) {
244
+ case 'create': {
245
+ const { data } = operation;
246
+ const sql = translator.translateInsert(entity, data instanceof Array ? data : [data]);
247
+ const result = await connector.exec(sql, txn);
248
+ return result[0].affectedRows;
249
+ }
250
+ case 'remove': {
251
+ const sql = translator.translateRemove(entity, operation, option);
252
+ const result = await connector.exec(sql, txn);
253
+ // todo 这里对sorter和indexfrom/count的支持不完整
254
+ return result[0].changedRows;
255
+ }
256
+ default: {
257
+ (0, assert_1.default)(!['select', 'download', 'stat'].includes(action));
258
+ const sql = translator.translateUpdate(entity, operation, option);
259
+ const result = await connector.exec(sql, txn);
260
+ // todo 这里对sorter和indexfrom/count的支持不完整
261
+ return result[0].changedRows;
262
+ }
263
+ }
264
+ }
265
+ async operate(entity, operation, context, option) {
266
+ const { action } = operation;
267
+ (0, assert_1.default)(!['select', 'download', 'stat'].includes(action), '现在不支持使用select operation');
268
+ return await super.operateAsync(entity, operation, context, option);
269
+ }
270
+ async select(entity, selection, context, option) {
271
+ const result = await super.selectAsync(entity, selection, context, option);
272
+ return result;
273
+ }
274
+ async countAbjointRowAsync(entity, selection, context, option) {
275
+ const sql = this.translator.translateCount(entity, selection, option);
276
+ const result = await this.connector.exec(sql, context.getCurrentTxnId());
277
+ return result[0][0].cnt;
278
+ }
279
+ async count(entity, selection, context, option) {
280
+ return this.countAsync(entity, selection, context, option);
281
+ }
282
+ async begin(option) {
283
+ const txn = await this.connector.startTransaction(option);
284
+ return txn;
285
+ }
286
+ async commit(txnId) {
287
+ await this.connector.commitTransaction(txnId);
288
+ }
289
+ async rollback(txnId) {
290
+ await this.connector.rollbackTransaction(txnId);
291
+ }
292
+ async connect() {
293
+ await this.connector.connect();
294
+ }
295
+ async disconnect() {
296
+ await this.connector.disconnect();
297
+ }
298
+ async initialize(option) {
299
+ const schema = this.getSchema();
300
+ for (const entity in schema) {
301
+ const sqls = this.translator.translateCreateEntity(entity, option);
302
+ for (const sql of sqls) {
303
+ await this.connector.exec(sql);
304
+ }
305
+ }
306
+ }
307
+ // 从数据库中读取当前schema
308
+ readSchema() {
309
+ return this.translator.readSchema((sql) => this.connector.exec(sql));
310
+ }
311
+ /**
312
+ * 根据载入的dataSchema,和数据库中原来的schema,决定如何来upgrade
313
+ * 制订出来的plan分为两阶段:增加阶段和削减阶段,在两个阶段之间,由用户来修正数据
314
+ */
315
+ async makeUpgradePlan() {
316
+ const originSchema = await this.readSchema();
317
+ const plan = this.diffSchema(originSchema, this.translator.schema);
318
+ return plan;
319
+ }
320
+ /**
321
+ * 比较两个schema的不同,这里计算的是new对old的增量
322
+ * @param schemaOld
323
+ * @param SchemaNew
324
+ */
325
+ diffSchema(schemaOld, schemaNew) {
326
+ const plan = {
327
+ newTables: {},
328
+ newIndexes: {},
329
+ updatedIndexes: {},
330
+ updatedTables: {},
331
+ };
332
+ for (const table in schemaNew) {
333
+ // mysql数据字典不分大小写的
334
+ if (schemaOld[table] || schemaOld[table.toLowerCase()]) {
335
+ const { attributes, indexes } = schemaOld[table] || schemaOld[table.toLowerCase()];
336
+ const { attributes: attributesNew, indexes: indexesNew } = schemaNew[table];
337
+ const assignToUpdateTables = (attr, isNew) => {
338
+ if (!plan.updatedTables[table]) {
339
+ plan.updatedTables[table] = {
340
+ attributes: {
341
+ [attr]: {
342
+ ...attributesNew[attr],
343
+ isNew,
344
+ }
345
+ }
346
+ };
347
+ }
348
+ else {
349
+ plan.updatedTables[table].attributes[attr] = {
350
+ ...attributesNew[attr],
351
+ isNew,
352
+ };
353
+ }
354
+ };
355
+ for (const attr in attributesNew) {
356
+ if (attributes[attr]) {
357
+ // 因为反向无法复原原来定义的attribute类型,这里就比较两次创建的sql是不是一致。
358
+ const sql1 = this.translator.translateAttributeDef(attr, attributesNew[attr]);
359
+ const sql2 = this.translator.translateAttributeDef(attr, attributes[attr]);
360
+ if (!this.translator.compareSql(sql1, sql2)) {
361
+ assignToUpdateTables(attr, false);
362
+ }
363
+ }
364
+ else {
365
+ assignToUpdateTables(attr, true);
366
+ }
367
+ }
368
+ if (indexesNew) {
369
+ const assignToIndexes = (index, isNew) => {
370
+ if (isNew) {
371
+ if (plan.newIndexes[table]) {
372
+ plan.newIndexes[table].push(index);
373
+ }
374
+ else {
375
+ plan.newIndexes[table] = [index];
376
+ }
377
+ }
378
+ else {
379
+ if (plan.updatedIndexes[table]) {
380
+ plan.updatedIndexes[table].push(index);
381
+ }
382
+ else {
383
+ plan.updatedIndexes[table] = [index];
384
+ }
385
+ }
386
+ };
387
+ const compareConfig = (config1, config2) => {
388
+ const unique1 = config1?.unique || false;
389
+ const unique2 = config2?.unique || false;
390
+ if (unique1 !== unique2) {
391
+ return false;
392
+ }
393
+ const type1 = config1?.type || 'btree';
394
+ const type2 = config2?.type || 'btree';
395
+ // parser目前无法从mysql中读出来,所以不比了
396
+ return type1 === type2;
397
+ };
398
+ for (const index of indexesNew) {
399
+ const { name, config, attributes } = index;
400
+ const origin = indexes?.find(ele => ele.name === name);
401
+ if (origin) {
402
+ if (JSON.stringify(attributes) !== JSON.stringify(origin.attributes)) {
403
+ // todo,这里要细致比较,不能用json.stringify
404
+ assignToIndexes(index, false);
405
+ }
406
+ else {
407
+ if (!compareConfig(config, origin.config)) {
408
+ assignToIndexes(index, false);
409
+ }
410
+ }
411
+ }
412
+ else {
413
+ assignToIndexes(index, true);
414
+ }
415
+ }
416
+ }
417
+ }
418
+ else {
419
+ plan.newTables[table] = {
420
+ attributes: schemaNew[table].attributes,
421
+ };
422
+ if (schemaNew[table].indexes) {
423
+ plan.newIndexes[table] = schemaNew[table].indexes;
424
+ }
425
+ }
426
+ }
427
+ return plan;
428
+ }
429
+ }
430
+ exports.MysqlStore = MysqlStore;