oak-db 1.0.7 → 2.0.3

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,20 +1,24 @@
1
- import { EntityDict, Context, DeduceCreateSingleOperation, DeduceRemoveOperation, DeduceUpdateOperation, OperateOption, OperationResult, SelectionResult, TxnOption, SelectRowShape, StorageSchema, DeduceCreateMultipleOperation, SelectOption } from 'oak-domain/lib/types';
1
+ import { EntityDict, DeduceCreateSingleOperation, DeduceRemoveOperation, DeduceUpdateOperation, OperateOption, OperationResult, TxnOption, StorageSchema, DeduceCreateMultipleOperation, SelectOption } from 'oak-domain/lib/types';
2
2
  import { EntityDict as BaseEntityDict } from 'oak-domain/lib/base-app-domain';
3
3
  import { CascadeStore } from 'oak-domain/lib/store/CascadeStore';
4
4
  import { MySQLConfiguration } from './types/Configuration';
5
5
  import { MySqlConnector } from './connector';
6
6
  import { MySqlTranslator, MySqlSelectOption, MysqlOperateOption } from './translator';
7
- export declare class MysqlStore<ED extends EntityDict & BaseEntityDict, Cxt extends Context<ED>> extends CascadeStore<ED, Cxt> {
7
+ import { AsyncContext, AsyncRowStore } from 'oak-domain/lib/store/AsyncRowStore';
8
+ import { SyncContext } from 'oak-domain/lib/store/SyncRowStore';
9
+ export declare class MysqlStore<ED extends EntityDict & BaseEntityDict, Cxt extends AsyncContext<ED>> extends CascadeStore<ED> implements AsyncRowStore<ED, Cxt> {
10
+ protected selectAbjointRow<T extends keyof ED, OP extends SelectOption>(entity: T, selection: ED[T]['Selection'], context: SyncContext<ED>, option: OP): Partial<ED[T]['Schema']>[];
11
+ protected updateAbjointRow<T extends keyof ED, OP extends OperateOption>(entity: T, operation: ED[T]['Operation'], context: SyncContext<ED>, option: OP): number;
8
12
  connector: MySqlConnector;
9
13
  translator: MySqlTranslator<ED>;
10
14
  constructor(storageSchema: StorageSchema<ED>, configuration: MySQLConfiguration);
11
15
  protected supportManyToOneJoin(): boolean;
12
16
  protected supportMultipleCreate(): boolean;
13
17
  private formResult;
14
- protected selectAbjointRow<T extends keyof ED, S extends ED[T]['Selection']>(entity: T, selection: S, context: Cxt, option?: MySqlSelectOption): Promise<SelectRowShape<ED[T]['Schema'], S['data']>[]>;
15
- protected updateAbjointRow<T extends keyof ED>(entity: T, operation: DeduceCreateMultipleOperation<ED[T]['Schema']> | DeduceCreateSingleOperation<ED[T]['Schema']> | DeduceUpdateOperation<ED[T]['Schema']> | DeduceRemoveOperation<ED[T]['Schema']>, context: Cxt, option?: MysqlOperateOption): Promise<number>;
18
+ protected selectAbjointRowAsync<T extends keyof ED>(entity: T, selection: ED[T]['Selection'], context: AsyncContext<ED>, option?: MySqlSelectOption): Promise<Partial<ED[T]['Schema']>[]>;
19
+ protected updateAbjointRowAsync<T extends keyof ED>(entity: T, operation: DeduceCreateMultipleOperation<ED[T]['Schema']> | DeduceCreateSingleOperation<ED[T]['Schema']> | DeduceUpdateOperation<ED[T]['Schema']> | DeduceRemoveOperation<ED[T]['Schema']>, context: AsyncContext<ED>, option?: MysqlOperateOption): Promise<number>;
16
20
  operate<T extends keyof ED>(entity: T, operation: ED[T]['Operation'], context: Cxt, option: OperateOption): Promise<OperationResult<ED>>;
17
- select<T extends keyof ED, S extends ED[T]['Selection']>(entity: T, selection: S, context: Cxt, option: SelectOption): Promise<SelectionResult<ED[T]['Schema'], S['data']>>;
21
+ select<T extends keyof ED>(entity: T, selection: ED[T]['Selection'], context: Cxt, option: SelectOption): Promise<Partial<ED[T]['Schema']>[]>;
18
22
  count<T extends keyof ED>(entity: T, selection: Pick<ED[T]['Selection'], 'filter' | 'count'>, context: Cxt, option: SelectOption): Promise<number>;
19
23
  begin(option?: TxnOption): Promise<string>;
20
24
  commit(txnId: string): Promise<void>;
@@ -28,6 +28,12 @@ var MysqlStore = /** @class */ (function (_super) {
28
28
  _this.translator = new translator_1.MySqlTranslator(storageSchema);
29
29
  return _this;
30
30
  }
31
+ MysqlStore.prototype.selectAbjointRow = function (entity, selection, context, option) {
32
+ throw new Error('MySQL store不支持同步取数据,不应该跑到这儿');
33
+ };
34
+ MysqlStore.prototype.updateAbjointRow = function (entity, operation, context, option) {
35
+ throw new Error('MySQL store不支持同步更新数据,不应该跑到这儿');
36
+ };
31
37
  MysqlStore.prototype.supportManyToOneJoin = function () {
32
38
  return true;
33
39
  };
@@ -44,24 +50,16 @@ var MysqlStore = /** @class */ (function (_super) {
44
50
  if (i !== -1) {
45
51
  var attrHead = attr.slice(0, i);
46
52
  var attrTail = attr.slice(i + 1);
53
+ if (!r[attrHead]) {
54
+ r[attrHead] = {};
55
+ }
47
56
  var rel = (0, relation_1.judgeRelation)(schema, entity2, attrHead);
48
- (0, assert_1.default)(rel === 2 || typeof rel === 'string');
49
57
  if (rel === 2) {
50
- if (r.entity === attrHead) {
51
- if (!r[attrHead]) {
52
- r[attrHead] = {};
53
- }
54
- resolveAttribute(attrHead, r[attrHead], attrTail, value);
55
- }
58
+ resolveAttribute(attrHead, r[attrHead], attrTail, value);
56
59
  }
57
60
  else {
58
61
  (0, assert_1.default)(typeof rel === 'string');
59
- if (typeof r["".concat(attrHead, "Id")] === 'string') {
60
- if (!r[attrHead]) {
61
- r[attrHead] = {};
62
- }
63
- resolveAttribute(rel, r[attrHead], attrTail, value);
64
- }
62
+ resolveAttribute(rel, r[attrHead], attrTail, value);
65
63
  }
66
64
  }
67
65
  else if (attributes[attr]) {
@@ -134,23 +132,32 @@ var MysqlStore = /** @class */ (function (_super) {
134
132
  _a));
135
133
  }
136
134
  }
137
- function formalizeNullObject(r, e) {
138
- var a2 = schema[e].attributes;
139
- var allowFormalize = true;
135
+ function removeNullObjects(r, e) {
136
+ (0, assert_1.default)(r.id && typeof r.id === 'string', "\u5BF9\u8C61".concat(e, "\u53D6\u6570\u636E\u65F6\u53D1\u73B0id\u4E3A\u975E\u6CD5\u503C").concat(r.id, ",rowId\u662F").concat(r.id));
140
137
  for (var attr in r) {
141
- if (typeof r[attr] === 'object' && a2[attr] && a2[attr].type === 'ref') {
142
- if (formalizeNullObject(r[attr], a2[attr].ref)) {
143
- r[attr] = null;
144
- }
145
- else {
146
- allowFormalize = false;
138
+ var rel = (0, relation_1.judgeRelation)(schema, e, attr);
139
+ if (rel === 2) {
140
+ // 边界,如果是toModi的对象,这里的外键确实有可能为空
141
+ (0, assert_1.default)(schema[e].toModi || r.entityId === r[attr].id, "\u5BF9\u8C61".concat(e, "\u53D6\u6570\u636E\u65F6\uFF0C\u53D1\u73B0entityId\u4E0E\u8FDE\u63A5\u7684\u5BF9\u8C61\u7684\u4E3B\u952E\u4E0D\u4E00\u81F4\uFF0CrowId\u662F").concat(r.id, "\uFF0C\u5176entityId\u503C\u4E3A").concat(r.entityId, "\uFF0C\u8FDE\u63A5\u7684\u5BF9\u8C61\u7684\u4E3B\u952E\u4E3A").concat(r[attr].id));
142
+ if (r[attr].id === null) {
143
+ (0, assert_1.default)(schema[e].toModi);
144
+ delete r[attr];
145
+ continue;
147
146
  }
147
+ (0, assert_1.default)(r.entity === attr, "\u5BF9\u8C61".concat(e, "\u53D6\u6570\u636E\u65F6\uFF0C\u53D1\u73B0entity\u503C\u4E0E\u8FDE\u63A5\u7684\u5916\u952E\u5BF9\u8C61\u4E0D\u4E00\u81F4\uFF0CrowId\u662F").concat(r.id, "\uFF0C\u5176entity\u503C\u4E3A").concat(r.entity, "\uFF0C\u8FDE\u63A5\u7684\u5BF9\u8C61\u4E3A").concat(attr));
148
+ removeNullObjects(r[attr], attr);
148
149
  }
149
- else if (r[attr] !== null) {
150
- allowFormalize = false;
150
+ else if (typeof rel === 'string') {
151
+ // 边界,如果是toModi的对象,这里的外键确实有可能为空
152
+ (0, assert_1.default)(schema[e].toModi || r["".concat(attr, "Id")] === r[attr].id, "\u5BF9\u8C61".concat(e, "\u53D6\u6570\u636E\u65F6\uFF0C\u53D1\u73B0\u5176\u5916\u952E\u4E0E\u8FDE\u63A5\u7684\u5BF9\u8C61\u7684\u4E3B\u952E\u4E0D\u4E00\u81F4\uFF0CrowId\u662F").concat(r.id, "\uFF0C\u5176").concat(attr, "Id\u503C\u4E3A").concat(r["".concat(attr, "Id")], "\uFF0C\u8FDE\u63A5\u7684\u5BF9\u8C61\u7684\u4E3B\u952E\u4E3A").concat(r[attr].id));
153
+ if (r[attr].id === null) {
154
+ (0, assert_1.default)(schema[e].toModi);
155
+ delete r[attr];
156
+ continue;
157
+ }
158
+ removeNullObjects(r[attr], rel);
151
159
  }
152
160
  }
153
- return allowFormalize;
154
161
  }
155
162
  function formSingleRow(r) {
156
163
  var result2 = {};
@@ -158,7 +165,7 @@ var MysqlStore = /** @class */ (function (_super) {
158
165
  var value = r[attr];
159
166
  resolveAttribute(entity, result2, attr, value);
160
167
  }
161
- formalizeNullObject(result2, entity);
168
+ removeNullObjects(result2, entity);
162
169
  return result2;
163
170
  }
164
171
  if (result instanceof Array) {
@@ -166,7 +173,7 @@ var MysqlStore = /** @class */ (function (_super) {
166
173
  }
167
174
  return formSingleRow(result);
168
175
  };
169
- MysqlStore.prototype.selectAbjointRow = function (entity, selection, context, option) {
176
+ MysqlStore.prototype.selectAbjointRowAsync = function (entity, selection, context, option) {
170
177
  return tslib_1.__awaiter(this, void 0, void 0, function () {
171
178
  var sql, result;
172
179
  return tslib_1.__generator(this, function (_a) {
@@ -181,7 +188,7 @@ var MysqlStore = /** @class */ (function (_super) {
181
188
  });
182
189
  });
183
190
  };
184
- MysqlStore.prototype.updateAbjointRow = function (entity, operation, context, option) {
191
+ MysqlStore.prototype.updateAbjointRowAsync = function (entity, operation, context, option) {
185
192
  return tslib_1.__awaiter(this, void 0, void 0, function () {
186
193
  var _a, translator, connector, action, txn, _b, data, sql, sql, sql;
187
194
  return tslib_1.__generator(this, function (_c) {
@@ -252,7 +259,7 @@ var MysqlStore = /** @class */ (function (_super) {
252
259
  case 0:
253
260
  action = operation.action;
254
261
  (0, assert_1.default)(!['select', 'download', 'stat'].includes(action), '现在不支持使用select operation');
255
- return [4 /*yield*/, this.cascadeUpdate(entity, operation, context, option)];
262
+ return [4 /*yield*/, this.cascadeUpdateAsync(entity, operation, context, option)];
256
263
  case 1: return [2 /*return*/, _a.sent()];
257
264
  }
258
265
  });
@@ -263,12 +270,10 @@ var MysqlStore = /** @class */ (function (_super) {
263
270
  var result;
264
271
  return tslib_1.__generator(this, function (_a) {
265
272
  switch (_a.label) {
266
- case 0: return [4 /*yield*/, this.cascadeSelect(entity, selection, context, option)];
273
+ case 0: return [4 /*yield*/, this.cascadeSelectAsync(entity, selection, context, option)];
267
274
  case 1:
268
275
  result = _a.sent();
269
- return [2 /*return*/, {
270
- result: result,
271
- }];
276
+ return [2 /*return*/, result];
272
277
  }
273
278
  });
274
279
  });
@@ -124,7 +124,7 @@ var MySqlTranslator = /** @class */ (function (_super) {
124
124
  }
125
125
  };
126
126
  MySqlTranslator.prototype.populateDataTypeDef = function (type, params) {
127
- if (['date', 'datetime', 'time'].includes(type)) {
127
+ if (['date', 'datetime', 'time', 'sequence'].includes(type)) {
128
128
  return 'bigint ';
129
129
  }
130
130
  if (['object', 'array'].includes(type)) {
@@ -250,6 +250,7 @@ var MySqlTranslator = /** @class */ (function (_super) {
250
250
  var schema = this.schema;
251
251
  var entityDef = schema[entity];
252
252
  var storageName = entityDef.storageName, attributes = entityDef.attributes, indexes = entityDef.indexes, view = entityDef.view;
253
+ var hasSequence = false;
253
254
  // todo view暂还不支持
254
255
  var entityType = view ? 'view' : 'table';
255
256
  var sql = "create ".concat(entityType, " ");
@@ -267,7 +268,7 @@ var MySqlTranslator = /** @class */ (function (_super) {
267
268
  // 翻译所有的属性
268
269
  Object.keys(attributes).forEach(function (attr, idx) {
269
270
  var attrDef = attributes[attr];
270
- var type = attrDef.type, params = attrDef.params, defaultValue = attrDef.default, unique = attrDef.unique, notNull = attrDef.notNull;
271
+ var type = attrDef.type, params = attrDef.params, defaultValue = attrDef.default, unique = attrDef.unique, notNull = attrDef.notNull, sequenceStart = attrDef.sequenceStart;
271
272
  sql += "`".concat(attr, "` ");
272
273
  sql += _this.populateDataTypeDef(type, params);
273
274
  if (notNull || type === 'geometry') {
@@ -276,6 +277,13 @@ var MySqlTranslator = /** @class */ (function (_super) {
276
277
  if (unique) {
277
278
  sql += ' unique ';
278
279
  }
280
+ if (sequenceStart) {
281
+ if (hasSequence) {
282
+ throw new Error("\u300C".concat(entity, "\u300D\u53EA\u80FD\u6709\u4E00\u4E2Asequence\u5217"));
283
+ }
284
+ hasSequence = sequenceStart;
285
+ sql += ' auto_increment unique ';
286
+ }
279
287
  if (defaultValue !== undefined) {
280
288
  (0, assert_1.default)(type !== 'ref');
281
289
  sql += " default ".concat(_this.translateAttrValue(type, defaultValue));
@@ -338,6 +346,9 @@ var MySqlTranslator = /** @class */ (function (_super) {
338
346
  }
339
347
  }
340
348
  sql += ')';
349
+ if (typeof hasSequence === 'number') {
350
+ sql += "auto_increment = ".concat(hasSequence);
351
+ }
341
352
  if (!replace) {
342
353
  return [sql];
343
354
  }
@@ -24,6 +24,10 @@ var SqlTranslator = /** @class */ (function () {
24
24
  length: 36,
25
25
  },
26
26
  },
27
+ $$seq$$: {
28
+ type: 'sequence',
29
+ sequenceStart: 10000,
30
+ },
27
31
  $$createAt$$: {
28
32
  type: 'datetime',
29
33
  notNull: true,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "oak-db",
3
- "version": "1.0.7",
3
+ "version": "2.0.3",
4
4
  "description": "oak-db",
5
5
  "main": "lib/index",
6
6
  "author": {
@@ -18,7 +18,7 @@
18
18
  "lodash": "^4.17.21",
19
19
  "mysql": "^2.18.1",
20
20
  "mysql2": "^2.3.3",
21
- "oak-domain": "^1.1.13",
21
+ "oak-domain": "^2.0.2",
22
22
  "uuid": "^8.3.2"
23
23
  },
24
24
  "license": "ISC",
@@ -29,7 +29,7 @@
29
29
  "@types/node": "^17.0.42",
30
30
  "@types/uuid": "^8.3.4",
31
31
  "mocha": "^10.0.0",
32
- "oak-general-business": "file:../oak-general-business",
32
+ "oak-general-business": "^2.0.2",
33
33
  "ts-node": "~10.9.1",
34
34
  "tslib": "^2.4.0",
35
35
  "typescript": "~4.7.4"