oak-db 1.0.1 → 1.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.
- package/lib/MySQL/store.d.ts +5 -4
- package/lib/MySQL/store.js +3 -3
- package/lib/MySQL/translator.js +20 -17
- package/lib/sqlTranslator.js +18 -14
- package/package.json +4 -4
- package/src/index.ts +0 -2
package/lib/MySQL/store.d.ts
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { EntityDict, Context, DeduceCreateSingleOperation, DeduceRemoveOperation, DeduceUpdateOperation, OperateOption, OperationResult, SelectionResult, TxnOption, SelectRowShape, StorageSchema, DeduceCreateMultipleOperation, SelectOption } from 'oak-domain/lib/types';
|
|
2
|
+
import { EntityDict as BaseEntityDict } from 'oak-domain/lib/base-app-domain';
|
|
2
3
|
import { CascadeStore } from 'oak-domain/lib/store/CascadeStore';
|
|
3
4
|
import { MySQLConfiguration } from './types/Configuration';
|
|
4
5
|
import { MySqlConnector } from './connector';
|
|
5
6
|
import { MySqlTranslator, MySqlSelectOption, MysqlOperateOption } from './translator';
|
|
6
|
-
export declare class MysqlStore<ED extends EntityDict, Cxt extends Context<ED>> extends CascadeStore<ED, Cxt> {
|
|
7
|
+
export declare class MysqlStore<ED extends EntityDict & BaseEntityDict, Cxt extends Context<ED>> extends CascadeStore<ED, Cxt> {
|
|
7
8
|
connector: MySqlConnector;
|
|
8
9
|
translator: MySqlTranslator<ED>;
|
|
9
10
|
constructor(storageSchema: StorageSchema<ED>, configuration: MySQLConfiguration);
|
|
@@ -12,9 +13,9 @@ export declare class MysqlStore<ED extends EntityDict, Cxt extends Context<ED>>
|
|
|
12
13
|
private formResult;
|
|
13
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']>[]>;
|
|
14
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>;
|
|
15
|
-
operate<T extends keyof ED>(entity: T, operation: ED[T]['Operation'], context: Cxt,
|
|
16
|
-
select<T extends keyof ED, S extends ED[T]['Selection']>(entity: T, selection: S, context: Cxt, option
|
|
17
|
-
count<T extends keyof ED>(entity: T, selection: Pick<ED[T]['Selection'], 'filter' | 'count'>, context: Cxt, option
|
|
16
|
+
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']>>;
|
|
18
|
+
count<T extends keyof ED>(entity: T, selection: Pick<ED[T]['Selection'], 'filter' | 'count'>, context: Cxt, option: SelectOption): Promise<number>;
|
|
18
19
|
begin(option?: TxnOption): Promise<string>;
|
|
19
20
|
commit(txnId: string): Promise<void>;
|
|
20
21
|
rollback(txnId: string): Promise<void>;
|
package/lib/MySQL/store.js
CHANGED
|
@@ -231,7 +231,7 @@ var MysqlStore = /** @class */ (function (_super) {
|
|
|
231
231
|
});
|
|
232
232
|
});
|
|
233
233
|
};
|
|
234
|
-
MysqlStore.prototype.operate = function (entity, operation, context,
|
|
234
|
+
MysqlStore.prototype.operate = function (entity, operation, context, option) {
|
|
235
235
|
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
|
236
236
|
var action;
|
|
237
237
|
return tslib_1.__generator(this, function (_a) {
|
|
@@ -239,7 +239,7 @@ var MysqlStore = /** @class */ (function (_super) {
|
|
|
239
239
|
case 0:
|
|
240
240
|
action = operation.action;
|
|
241
241
|
(0, assert_1.default)(!['select', 'download', 'stat'].includes(action), '现在不支持使用select operation');
|
|
242
|
-
return [4 /*yield*/, this.cascadeUpdate(entity, operation, context,
|
|
242
|
+
return [4 /*yield*/, this.cascadeUpdate(entity, operation, context, option)];
|
|
243
243
|
case 1: return [2 /*return*/, _a.sent()];
|
|
244
244
|
}
|
|
245
245
|
});
|
|
@@ -270,7 +270,7 @@ var MysqlStore = /** @class */ (function (_super) {
|
|
|
270
270
|
return [4 /*yield*/, this.connector.exec(sql, context.getCurrentTxnId())];
|
|
271
271
|
case 1:
|
|
272
272
|
result = _a.sent();
|
|
273
|
-
return [2 /*return*/, result.
|
|
273
|
+
return [2 /*return*/, result[0].cnt];
|
|
274
274
|
}
|
|
275
275
|
});
|
|
276
276
|
});
|
package/lib/MySQL/translator.js
CHANGED
|
@@ -125,6 +125,18 @@ var MySqlTranslator = /** @class */ (function (_super) {
|
|
|
125
125
|
}
|
|
126
126
|
};
|
|
127
127
|
MySqlTranslator.prototype.populateDataTypeDef = function (type, params) {
|
|
128
|
+
if (['date', 'datetime', 'time'].includes(type)) {
|
|
129
|
+
return 'bigint ';
|
|
130
|
+
}
|
|
131
|
+
if (['object', 'array'].includes(type)) {
|
|
132
|
+
return 'text ';
|
|
133
|
+
}
|
|
134
|
+
if (['image', 'function'].includes(type)) {
|
|
135
|
+
return 'text ';
|
|
136
|
+
}
|
|
137
|
+
if (type === 'ref') {
|
|
138
|
+
return 'char(36)';
|
|
139
|
+
}
|
|
128
140
|
if (MySqlTranslator.withLengthDataTypes.includes(type)) {
|
|
129
141
|
if (params) {
|
|
130
142
|
var length_1 = params.length;
|
|
@@ -172,18 +184,6 @@ var MySqlTranslator = /** @class */ (function (_super) {
|
|
|
172
184
|
}
|
|
173
185
|
}
|
|
174
186
|
}
|
|
175
|
-
if (['date'].includes(type)) {
|
|
176
|
-
return 'datetime';
|
|
177
|
-
}
|
|
178
|
-
if (['object', 'array'].includes(type)) {
|
|
179
|
-
return 'text ';
|
|
180
|
-
}
|
|
181
|
-
if (['image', 'function'].includes(type)) {
|
|
182
|
-
return 'text ';
|
|
183
|
-
}
|
|
184
|
-
if (type === 'ref') {
|
|
185
|
-
return 'char(36)';
|
|
186
|
-
}
|
|
187
187
|
return "".concat(type, " ");
|
|
188
188
|
};
|
|
189
189
|
MySqlTranslator.prototype.translateAttrProjection = function (dataType, alias, attr) {
|
|
@@ -197,21 +197,23 @@ var MySqlTranslator = /** @class */ (function (_super) {
|
|
|
197
197
|
}
|
|
198
198
|
};
|
|
199
199
|
MySqlTranslator.prototype.translateAttrValue = function (dataType, value) {
|
|
200
|
-
if (value === null) {
|
|
200
|
+
if (value === null || value === undefined) {
|
|
201
201
|
return 'null';
|
|
202
202
|
}
|
|
203
203
|
switch (dataType) {
|
|
204
204
|
case 'geometry': {
|
|
205
205
|
return transformGeoData(value);
|
|
206
206
|
}
|
|
207
|
+
case 'datetime':
|
|
208
|
+
case 'time':
|
|
207
209
|
case 'date': {
|
|
208
210
|
if (value instanceof Date) {
|
|
209
|
-
return
|
|
211
|
+
return "".concat(value.valueOf());
|
|
210
212
|
}
|
|
211
213
|
else if (typeof value === 'number') {
|
|
212
|
-
return
|
|
214
|
+
return "".concat(value);
|
|
213
215
|
}
|
|
214
|
-
return value;
|
|
216
|
+
return "'".concat((new Date(value)).valueOf(), "'");
|
|
215
217
|
}
|
|
216
218
|
case 'object':
|
|
217
219
|
case 'array': {
|
|
@@ -547,7 +549,8 @@ var MySqlTranslator = /** @class */ (function (_super) {
|
|
|
547
549
|
// todo using index
|
|
548
550
|
var alias = aliasDict['./'];
|
|
549
551
|
var now = luxon_1.DateTime.now().toFormat('yyyy-LL-dd HH:mm:ss');
|
|
550
|
-
|
|
552
|
+
(0, assert_1.default)(updateText);
|
|
553
|
+
var sql = "update ".concat(fromText, " set ").concat(updateText);
|
|
551
554
|
if (filterText) {
|
|
552
555
|
sql += " where ".concat(filterText);
|
|
553
556
|
}
|
package/lib/sqlTranslator.js
CHANGED
|
@@ -4,7 +4,6 @@ exports.SqlTranslator = void 0;
|
|
|
4
4
|
var tslib_1 = require("tslib");
|
|
5
5
|
var assert_1 = tslib_1.__importDefault(require("assert"));
|
|
6
6
|
var lodash_1 = require("lodash");
|
|
7
|
-
var luxon_1 = require("luxon");
|
|
8
7
|
var types_1 = require("oak-domain/lib/types");
|
|
9
8
|
var relation_1 = require("oak-domain/lib/store/relation");
|
|
10
9
|
;
|
|
@@ -26,21 +25,21 @@ var SqlTranslator = /** @class */ (function () {
|
|
|
26
25
|
},
|
|
27
26
|
},
|
|
28
27
|
$$createAt$$: {
|
|
29
|
-
type: '
|
|
28
|
+
type: 'datetime',
|
|
30
29
|
notNull: true,
|
|
31
30
|
},
|
|
32
31
|
$$updateAt$$: {
|
|
33
|
-
type: '
|
|
32
|
+
type: 'datetime',
|
|
34
33
|
notNull: true,
|
|
35
34
|
},
|
|
36
35
|
$$deleteAt$$: {
|
|
37
|
-
type: '
|
|
36
|
+
type: 'datetime',
|
|
38
37
|
},
|
|
39
38
|
$$triggerData$$: {
|
|
40
39
|
type: 'object',
|
|
41
40
|
},
|
|
42
41
|
$$triggerTimestamp$$: {
|
|
43
|
-
type: '
|
|
42
|
+
type: 'datetime',
|
|
44
43
|
},
|
|
45
44
|
});
|
|
46
45
|
// 增加默认的索引
|
|
@@ -113,15 +112,18 @@ var SqlTranslator = /** @class */ (function () {
|
|
|
113
112
|
var schema = this.schema;
|
|
114
113
|
var _a = schema[entity], attributes = _a.attributes, _b = _a.storageName, storageName = _b === void 0 ? entity : _b;
|
|
115
114
|
var sql = "insert into `".concat(storageName, "`(");
|
|
116
|
-
|
|
115
|
+
/**
|
|
116
|
+
* 这里的attrs要用所有行的union集合
|
|
117
|
+
*/
|
|
118
|
+
var dataFull = data.reduce(function (prev, cur) { return Object.assign({}, cur, prev); }, {});
|
|
119
|
+
var attrs = Object.keys(dataFull).filter(function (ele) { return attributes.hasOwnProperty(ele); });
|
|
117
120
|
attrs.forEach(function (attr, idx) {
|
|
118
121
|
sql += " `".concat(attr, "`");
|
|
119
122
|
if (idx < attrs.length - 1) {
|
|
120
123
|
sql += ',';
|
|
121
124
|
}
|
|
122
125
|
});
|
|
123
|
-
sql += '
|
|
124
|
-
var now = luxon_1.DateTime.now().toFormat('yyyy-LL-dd HH:mm:ss');
|
|
126
|
+
sql += ') values ';
|
|
125
127
|
data.forEach(function (d, dataIndex) {
|
|
126
128
|
sql += '(';
|
|
127
129
|
attrs.forEach(function (attr, attrIdx) {
|
|
@@ -133,9 +135,11 @@ var SqlTranslator = /** @class */ (function () {
|
|
|
133
135
|
sql += ',';
|
|
134
136
|
}
|
|
135
137
|
});
|
|
136
|
-
sql += ", '".concat(now, "', '").concat(now, "')");
|
|
137
138
|
if (dataIndex < data.length - 1) {
|
|
138
|
-
sql += ',';
|
|
139
|
+
sql += '),';
|
|
140
|
+
}
|
|
141
|
+
else {
|
|
142
|
+
sql += ')';
|
|
139
143
|
}
|
|
140
144
|
});
|
|
141
145
|
return sql;
|
|
@@ -537,7 +541,7 @@ var SqlTranslator = /** @class */ (function () {
|
|
|
537
541
|
currentNumber = cn2;
|
|
538
542
|
}
|
|
539
543
|
else {
|
|
540
|
-
var rel = (0, relation_1.judgeRelation)(_this.schema,
|
|
544
|
+
var rel = (0, relation_1.judgeRelation)(_this.schema, entity2, attr);
|
|
541
545
|
if (rel === 2) {
|
|
542
546
|
whereText += " ".concat(translateInner(attr, "".concat(path).concat(attr, "/"), filter2[attr]));
|
|
543
547
|
}
|
|
@@ -685,11 +689,11 @@ var SqlTranslator = /** @class */ (function () {
|
|
|
685
689
|
var _a = this.analyzeJoin(entity, {
|
|
686
690
|
filter: filter,
|
|
687
691
|
}), fromText = _a.from, aliasDict = _a.aliasDict, extraWhere = _a.extraWhere, filterRefAlias = _a.filterRefAlias, currentNumber = _a.currentNumber;
|
|
688
|
-
var projText = 'count(1)';
|
|
692
|
+
var projText = 'count(1) cnt';
|
|
689
693
|
var filterText = this.translateFilter(entity, selection, aliasDict, filterRefAlias, currentNumber, extraWhere, option).stmt;
|
|
690
694
|
if (count) {
|
|
691
|
-
var subQuerySql = this.populateSelectStmt('1', fromText, Object.assign({}, selection, { indexFrom: 0 }), aliasDict, filterText, undefined, undefined, undefined, option);
|
|
692
|
-
return "select count(1) from (".concat(subQuerySql, ")");
|
|
695
|
+
var subQuerySql = this.populateSelectStmt('1', fromText, Object.assign({}, selection, { indexFrom: 0, count: count }), aliasDict, filterText, undefined, undefined, undefined, option);
|
|
696
|
+
return "select count(1) cnt from (".concat(subQuerySql, ") __tmp");
|
|
693
697
|
}
|
|
694
698
|
return this.populateSelectStmt(projText, fromText, selection, aliasDict, filterText, undefined, undefined, undefined, option);
|
|
695
699
|
};
|
package/package.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "oak-db",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.3",
|
|
4
4
|
"description": "oak-db",
|
|
5
|
-
"main": "
|
|
5
|
+
"main": "lib/index.js",
|
|
6
6
|
"author": {
|
|
7
7
|
"name": "XuChang"
|
|
8
8
|
},
|
|
@@ -19,7 +19,7 @@
|
|
|
19
19
|
"luxon": "^2.4.0",
|
|
20
20
|
"mysql": "^2.18.1",
|
|
21
21
|
"mysql2": "^2.3.3",
|
|
22
|
-
"oak-domain": "
|
|
22
|
+
"oak-domain": "1.1.10",
|
|
23
23
|
"uuid": "^8.3.2"
|
|
24
24
|
},
|
|
25
25
|
"license": "ISC",
|
|
@@ -30,7 +30,7 @@
|
|
|
30
30
|
"@types/node": "^17.0.42",
|
|
31
31
|
"@types/uuid": "^8.3.4",
|
|
32
32
|
"mocha": "^10.0.0",
|
|
33
|
-
"oak-general-business": "
|
|
33
|
+
"oak-general-business": "1.0.13",
|
|
34
34
|
"ts-node": "~10.9.1",
|
|
35
35
|
"tslib": "^2.4.0",
|
|
36
36
|
"typescript": "~4.7.4"
|
package/src/index.ts
DELETED