oak-db 1.0.6 → 2.0.2

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
  };
@@ -166,7 +172,7 @@ var MysqlStore = /** @class */ (function (_super) {
166
172
  }
167
173
  return formSingleRow(result);
168
174
  };
169
- MysqlStore.prototype.selectAbjointRow = function (entity, selection, context, option) {
175
+ MysqlStore.prototype.selectAbjointRowAsync = function (entity, selection, context, option) {
170
176
  return tslib_1.__awaiter(this, void 0, void 0, function () {
171
177
  var sql, result;
172
178
  return tslib_1.__generator(this, function (_a) {
@@ -181,7 +187,7 @@ var MysqlStore = /** @class */ (function (_super) {
181
187
  });
182
188
  });
183
189
  };
184
- MysqlStore.prototype.updateAbjointRow = function (entity, operation, context, option) {
190
+ MysqlStore.prototype.updateAbjointRowAsync = function (entity, operation, context, option) {
185
191
  return tslib_1.__awaiter(this, void 0, void 0, function () {
186
192
  var _a, translator, connector, action, txn, _b, data, sql, sql, sql;
187
193
  return tslib_1.__generator(this, function (_c) {
@@ -252,7 +258,7 @@ var MysqlStore = /** @class */ (function (_super) {
252
258
  case 0:
253
259
  action = operation.action;
254
260
  (0, assert_1.default)(!['select', 'download', 'stat'].includes(action), '现在不支持使用select operation');
255
- return [4 /*yield*/, this.cascadeUpdate(entity, operation, context, option)];
261
+ return [4 /*yield*/, this.cascadeUpdateAsync(entity, operation, context, option)];
256
262
  case 1: return [2 /*return*/, _a.sent()];
257
263
  }
258
264
  });
@@ -263,12 +269,10 @@ var MysqlStore = /** @class */ (function (_super) {
263
269
  var result;
264
270
  return tslib_1.__generator(this, function (_a) {
265
271
  switch (_a.label) {
266
- case 0: return [4 /*yield*/, this.cascadeSelect(entity, selection, context, option)];
272
+ case 0: return [4 /*yield*/, this.cascadeSelectAsync(entity, selection, context, option)];
267
273
  case 1:
268
274
  result = _a.sent();
269
- return [2 /*return*/, {
270
- result: result,
271
- }];
275
+ return [2 /*return*/, result];
272
276
  }
273
277
  });
274
278
  });
@@ -5,7 +5,6 @@ var tslib_1 = require("tslib");
5
5
  var assert_1 = tslib_1.__importDefault(require("assert"));
6
6
  var util_1 = require("util");
7
7
  var lodash_1 = require("lodash");
8
- var luxon_1 = require("luxon");
9
8
  var sqlTranslator_1 = require("../sqlTranslator");
10
9
  var GeoTypes = [
11
10
  {
@@ -125,7 +124,7 @@ var MySqlTranslator = /** @class */ (function (_super) {
125
124
  }
126
125
  };
127
126
  MySqlTranslator.prototype.populateDataTypeDef = function (type, params) {
128
- if (['date', 'datetime', 'time'].includes(type)) {
127
+ if (['date', 'datetime', 'time', 'sequence'].includes(type)) {
129
128
  return 'bigint ';
130
129
  }
131
130
  if (['object', 'array'].includes(type)) {
@@ -251,6 +250,7 @@ var MySqlTranslator = /** @class */ (function (_super) {
251
250
  var schema = this.schema;
252
251
  var entityDef = schema[entity];
253
252
  var storageName = entityDef.storageName, attributes = entityDef.attributes, indexes = entityDef.indexes, view = entityDef.view;
253
+ var hasSequence = false;
254
254
  // todo view暂还不支持
255
255
  var entityType = view ? 'view' : 'table';
256
256
  var sql = "create ".concat(entityType, " ");
@@ -268,7 +268,7 @@ var MySqlTranslator = /** @class */ (function (_super) {
268
268
  // 翻译所有的属性
269
269
  Object.keys(attributes).forEach(function (attr, idx) {
270
270
  var attrDef = attributes[attr];
271
- 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;
272
272
  sql += "`".concat(attr, "` ");
273
273
  sql += _this.populateDataTypeDef(type, params);
274
274
  if (notNull || type === 'geometry') {
@@ -277,6 +277,13 @@ var MySqlTranslator = /** @class */ (function (_super) {
277
277
  if (unique) {
278
278
  sql += ' unique ';
279
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
+ }
280
287
  if (defaultValue !== undefined) {
281
288
  (0, assert_1.default)(type !== 'ref');
282
289
  sql += " default ".concat(_this.translateAttrValue(type, defaultValue));
@@ -339,6 +346,9 @@ var MySqlTranslator = /** @class */ (function (_super) {
339
346
  }
340
347
  }
341
348
  sql += ')';
349
+ if (typeof hasSequence === 'number') {
350
+ sql += "auto_increment = ".concat(hasSequence);
351
+ }
342
352
  if (!replace) {
343
353
  return [sql];
344
354
  }
@@ -471,14 +481,14 @@ var MySqlTranslator = /** @class */ (function (_super) {
471
481
  var _this = this;
472
482
  var translateConstant = function (constant) {
473
483
  if (typeof constant === 'string') {
474
- return "'".concat(constant, "'");
484
+ return " ".concat(new Date(constant).valueOf());
475
485
  }
476
486
  else if (constant instanceof Date) {
477
- return "'".concat(luxon_1.DateTime.fromJSDate(constant).toFormat('yyyy-LL-dd HH:mm:ss'), "'");
487
+ return " ".concat(constant.valueOf());
478
488
  }
479
489
  else {
480
490
  (0, assert_1.default)(typeof constant === 'number');
481
- return "".concat(constant);
491
+ return " ".concat(constant);
482
492
  }
483
493
  };
484
494
  var translateInner = function (expr) {
@@ -547,8 +557,6 @@ var MySqlTranslator = /** @class */ (function (_super) {
547
557
  };
548
558
  MySqlTranslator.prototype.populateUpdateStmt = function (updateText, fromText, aliasDict, filterText, sorterText, indexFrom, count, option) {
549
559
  // todo using index
550
- var alias = aliasDict['./'];
551
- var now = luxon_1.DateTime.now().toFormat('yyyy-LL-dd HH:mm:ss');
552
560
  (0, assert_1.default)(updateText);
553
561
  var sql = "update ".concat(fromText, " set ").concat(updateText);
554
562
  if (filterText) {
@@ -566,7 +574,7 @@ var MySqlTranslator = /** @class */ (function (_super) {
566
574
  MySqlTranslator.prototype.populateRemoveStmt = function (removeText, fromText, aliasDict, filterText, sorterText, indexFrom, count, option) {
567
575
  // todo using index
568
576
  var alias = aliasDict['./'];
569
- var now = luxon_1.DateTime.now().toFormat('yyyy-LL-dd HH:mm:ss');
577
+ var now = Date.now();
570
578
  var sql = "update ".concat(fromText, " set `").concat(alias, "`.`$$deleteAt$$` = '").concat(now, "'");
571
579
  if (filterText) {
572
580
  sql += " where ".concat(filterText);
@@ -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,
@@ -707,7 +711,7 @@ var SqlTranslator = /** @class */ (function () {
707
711
  (0, assert_1.default)(!sorter, '当前remove不支持sorter行为');
708
712
  var _a = this.analyzeJoin(entity, { filter: filter, sorter: sorter }), aliasDict = _a.aliasDict, filterRefAlias = _a.filterRefAlias, extraWhere = _a.extraWhere, fromText = _a.from, currentNumber = _a.currentNumber;
709
713
  var alias = aliasDict['./'];
710
- var filterText = this.translateFilter(entity, operation, aliasDict, filterRefAlias, currentNumber, extraWhere).stmt;
714
+ var filterText = this.translateFilter(entity, operation, aliasDict, filterRefAlias, currentNumber, extraWhere, { includedDeleted: true }).stmt;
711
715
  // const sorterText = sorter && sorter.length > 0 ? this.translateSorter(entity, sorter, aliasDict) : undefined;
712
716
  return this.populateRemoveStmt(alias, fromText, aliasDict, filterText, /* sorterText */ undefined, indexFrom, count, option);
713
717
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "oak-db",
3
- "version": "1.0.6",
3
+ "version": "2.0.2",
4
4
  "description": "oak-db",
5
5
  "main": "lib/index",
6
6
  "author": {
@@ -16,10 +16,9 @@
16
16
  },
17
17
  "dependencies": {
18
18
  "lodash": "^4.17.21",
19
- "luxon": "^2.4.0",
20
19
  "mysql": "^2.18.1",
21
20
  "mysql2": "^2.3.3",
22
- "oak-domain": "^1.1.13",
21
+ "oak-domain": "^2.0.1",
23
22
  "uuid": "^8.3.2"
24
23
  },
25
24
  "license": "ISC",
@@ -30,7 +29,7 @@
30
29
  "@types/node": "^17.0.42",
31
30
  "@types/uuid": "^8.3.4",
32
31
  "mocha": "^10.0.0",
33
- "oak-general-business": "^1.0.14",
32
+ "oak-general-business": "^2.0.1",
34
33
  "ts-node": "~10.9.1",
35
34
  "tslib": "^2.4.0",
36
35
  "typescript": "~4.7.4"