oak-db 1.0.0

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/.mocharc.json ADDED
@@ -0,0 +1,5 @@
1
+ {
2
+ "extension": ["ts"],
3
+ "spec": "test/test*.ts",
4
+ "require": "ts-node/register"
5
+ }
package/README.md ADDED
@@ -0,0 +1,37 @@
1
+ ### 3 分钟了解如何进入开发
2
+
3
+ 欢迎使用云效 Codeup,通过阅读以下内容,你可以快速熟悉 Codeup ,并立即开始今天的工作。
4
+
5
+ ### 提交**文件**
6
+
7
+ 首先,你需要了解在 Codeup 中如何提交代码文件,跟着文档「[__提交第一行代码__](https://thoughts.aliyun.com/sharespace/5e8c37eb546fd9001aee8242/docs/5e8c37e7546fd9001aee81fd)」一起操作试试看吧。
8
+
9
+ ### 开启扫描
10
+
11
+ 开发过程中,为了更好的管理你的代码资产,Codeup 内置了「[__代码规约扫描__](https://thoughts.aliyun.com/sharespace/5e8c37eb546fd9001aee8242/docs/5e8c37e8546fd9001aee821c)」和「[__敏感信息检测__](https://thoughts.aliyun.com/sharespace/5e8c37eb546fd9001aee8242/docs/5e8c37e8546fd9001aee821b)」服务,你可以在代码库设置-集成与服务中一键开启,开启后提交或合并请求的变更将自动触发扫描,并及时提供结果反馈。
12
+
13
+ ![](https://img.alicdn.com/tfs/TB1nRDatoz1gK0jSZLeXXb9kVXa-1122-380.png "")
14
+
15
+ ![](https://img.alicdn.com/tfs/TB1PrPatXY7gK0jSZKzXXaikpXa-1122-709.png "")
16
+
17
+ ### 代码评审
18
+
19
+ 功能开发完毕后,通常你需要发起「[__代码合并和评审__](https://thoughts.aliyun.com/sharespace/5e8c37eb546fd9001aee8242/docs/5e8c37e8546fd9001aee8216)」,Codeup 支持多人协作的代码评审服务,你可以通过「[__保护分支__](https://thoughts.aliyun.com/sharespace/5e8c37eb546fd9001aee8242/docs/5e8c37e9546fd9001aee8221)」策略及「[__合并请求设置__](https://thoughts.aliyun.com/sharespace/5e8c37eb546fd9001aee8242/docs/5e8c37e9546fd9001aee8224)」对合并过程进行流程化管控,同时提供 WebIDE 在线代码评审及冲突解决能力,让你的评审过程更加流畅。
20
+
21
+ ![](https://img.alicdn.com/tfs/TB1XHrctkP2gK0jSZPxXXacQpXa-1432-887.png "")
22
+
23
+ ![](https://img.alicdn.com/tfs/TB1V3fctoY1gK0jSZFMXXaWcVXa-1432-600.png "")
24
+
25
+ ### 编写文档
26
+
27
+ 项目推进过程中,你的经验和感悟可以直接记录到 Codeup 代码库的「[__文档__](https://thoughts.aliyun.com/sharespace/5e8c37eb546fd9001aee8242/docs/5e8c37e8546fd9001aee8213)」内,让智慧可视化。
28
+
29
+ ![](https://img.alicdn.com/tfs/TB1BN2ateT2gK0jSZFvXXXnFXXa-1432-700.png "")
30
+
31
+ ### 成员协作
32
+
33
+ 是时候邀请成员一起编写卓越的代码工程了,请点击右上角「成员」邀请你的小伙伴开始协作吧!
34
+
35
+ ### 更多
36
+
37
+ Git 使用教学、高级功能指引等更多说明,参见[__Codeup帮助文档__](https://thoughts.aliyun.com/sharespace/5e8c37eb546fd9001aee8242/docs/5e8c37e6546fd9001aee81fa)。
@@ -0,0 +1,15 @@
1
+ import mysql from 'mysql2';
2
+ import { TxnOption } from 'oak-domain/lib/types';
3
+ import { MySQLConfiguration } from './types/Configuration';
4
+ export declare class MySqlConnector {
5
+ pool?: mysql.Pool;
6
+ configuration: MySQLConfiguration;
7
+ txnDict: Record<string, mysql.PoolConnection>;
8
+ constructor(configuration: MySQLConfiguration);
9
+ connect(): void;
10
+ disconnect(): void;
11
+ startTransaction(option?: TxnOption): Promise<string>;
12
+ exec(sql: string, txn?: string): Promise<any>;
13
+ commitTransaction(txn: string): Promise<void>;
14
+ rollbackTransaction(txn: string): Promise<void>;
15
+ }
@@ -0,0 +1,162 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __generator = (this && this.__generator) || function (thisArg, body) {
12
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
+ function verb(n) { return function (v) { return step([n, v]); }; }
15
+ function step(op) {
16
+ if (f) throw new TypeError("Generator is already executing.");
17
+ while (_) try {
18
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
+ if (y = 0, t) op = [op[0] & 2, t.value];
20
+ switch (op[0]) {
21
+ case 0: case 1: t = op; break;
22
+ case 4: _.label++; return { value: op[1], done: false };
23
+ case 5: _.label++; y = op[1]; op = [0]; continue;
24
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
+ default:
26
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
+ if (t[2]) _.ops.pop();
31
+ _.trys.pop(); continue;
32
+ }
33
+ op = body.call(thisArg, _);
34
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
+ }
37
+ };
38
+ var __importDefault = (this && this.__importDefault) || function (mod) {
39
+ return (mod && mod.__esModule) ? mod : { "default": mod };
40
+ };
41
+ Object.defineProperty(exports, "__esModule", { value: true });
42
+ exports.MySqlConnector = void 0;
43
+ var mysql2_1 = __importDefault(require("mysql2"));
44
+ var uuid_1 = require("uuid");
45
+ var assert_1 = __importDefault(require("assert"));
46
+ var MySqlConnector = /** @class */ (function () {
47
+ function MySqlConnector(configuration) {
48
+ this.configuration = configuration;
49
+ this.txnDict = {};
50
+ }
51
+ MySqlConnector.prototype.connect = function () {
52
+ this.pool = mysql2_1.default.createPool(this.configuration);
53
+ };
54
+ MySqlConnector.prototype.disconnect = function () {
55
+ this.pool.end();
56
+ };
57
+ MySqlConnector.prototype.startTransaction = function (option) {
58
+ var _this = this;
59
+ return new Promise(function (resolve, reject) {
60
+ _this.pool.getConnection(function (err, connection) {
61
+ if (err) {
62
+ return reject(err);
63
+ }
64
+ var isolationLevel = (option || {}).isolationLevel;
65
+ var startTxn = function () {
66
+ var sql = 'START TRANSACTION;';
67
+ connection.query(sql, function (err2) {
68
+ var _a;
69
+ if (err2) {
70
+ connection.release();
71
+ return reject(err2);
72
+ }
73
+ var id = (0, uuid_1.v4)();
74
+ Object.assign(_this.txnDict, (_a = {},
75
+ _a[id] = connection,
76
+ _a));
77
+ resolve(id);
78
+ });
79
+ };
80
+ if (isolationLevel) {
81
+ connection.query("SET TRANSACTION ISOLATION LEVEL ".concat(isolationLevel, ";"), function (err2) {
82
+ if (err2) {
83
+ connection.release();
84
+ return reject(err2);
85
+ }
86
+ startTxn();
87
+ });
88
+ }
89
+ else {
90
+ startTxn();
91
+ }
92
+ });
93
+ });
94
+ };
95
+ MySqlConnector.prototype.exec = function (sql, txn) {
96
+ return __awaiter(this, void 0, void 0, function () {
97
+ var connection_1;
98
+ var _this = this;
99
+ return __generator(this, function (_a) {
100
+ if (process.env.NODE_ENV === 'development') {
101
+ console.log(sql);
102
+ }
103
+ if (txn) {
104
+ connection_1 = this.txnDict[txn];
105
+ (0, assert_1.default)(connection_1);
106
+ return [2 /*return*/, new Promise(function (resolve, reject) {
107
+ connection_1.query(sql, function (err, result) {
108
+ if (err) {
109
+ console.error("sql exec err: ".concat(sql), err);
110
+ return reject(err);
111
+ }
112
+ resolve(result);
113
+ });
114
+ })];
115
+ }
116
+ else {
117
+ return [2 /*return*/, new Promise(function (resolve, reject) {
118
+ // if (process.env.DEBUG) {
119
+ // console.log(sql);
120
+ //}
121
+ _this.pool.query(sql, function (err, result) {
122
+ if (err) {
123
+ console.error("sql exec err: ".concat(sql), err);
124
+ return reject(err);
125
+ }
126
+ resolve(result);
127
+ });
128
+ })];
129
+ }
130
+ return [2 /*return*/];
131
+ });
132
+ });
133
+ };
134
+ MySqlConnector.prototype.commitTransaction = function (txn) {
135
+ var connection = this.txnDict[txn];
136
+ (0, assert_1.default)(connection);
137
+ return new Promise(function (resolve, reject) {
138
+ connection.query('COMMIT;', function (err) {
139
+ if (err) {
140
+ return reject(err);
141
+ }
142
+ connection.release();
143
+ resolve();
144
+ });
145
+ });
146
+ };
147
+ MySqlConnector.prototype.rollbackTransaction = function (txn) {
148
+ var connection = this.txnDict[txn];
149
+ (0, assert_1.default)(connection);
150
+ return new Promise(function (resolve, reject) {
151
+ connection.query('ROLLBACK;', function (err) {
152
+ if (err) {
153
+ return reject(err);
154
+ }
155
+ connection.release();
156
+ resolve();
157
+ });
158
+ });
159
+ };
160
+ return MySqlConnector;
161
+ }());
162
+ exports.MySqlConnector = MySqlConnector;
@@ -0,0 +1,24 @@
1
+ import { EntityDict, Context, DeduceCreateSingleOperation, DeduceRemoveOperation, DeduceUpdateOperation, OperateOption, OperationResult, SelectionResult, TxnOption, SelectRowShape, StorageSchema, DeduceCreateMultipleOperation, SelectOption } from 'oak-domain/lib/types';
2
+ import { CascadeStore } from 'oak-domain/lib/store/CascadeStore';
3
+ import { MySQLConfiguration } from './types/Configuration';
4
+ import { MySqlConnector } from './connector';
5
+ import { MySqlTranslator, MySqlSelectOption, MysqlOperateOption } from './translator';
6
+ export declare class MysqlStore<ED extends EntityDict, Cxt extends Context<ED>> extends CascadeStore<ED, Cxt> {
7
+ connector: MySqlConnector;
8
+ translator: MySqlTranslator<ED>;
9
+ constructor(storageSchema: StorageSchema<ED>, configuration: MySQLConfiguration);
10
+ protected supportManyToOneJoin(): boolean;
11
+ protected supportMultipleCreate(): boolean;
12
+ private formResult;
13
+ 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
+ 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, params?: OperateOption): Promise<OperationResult<ED>>;
16
+ 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']>>;
17
+ count<T extends keyof ED>(entity: T, selection: Pick<ED[T]['Selection'], 'filter' | 'count'>, context: Cxt, option?: SelectOption): Promise<number>;
18
+ begin(option?: TxnOption): Promise<string>;
19
+ commit(txnId: string): Promise<void>;
20
+ rollback(txnId: string): Promise<void>;
21
+ connect(): void;
22
+ disconnect(): void;
23
+ initialize(dropIfExists?: boolean): Promise<void>;
24
+ }
@@ -0,0 +1,412 @@
1
+ "use strict";
2
+ var __extends = (this && this.__extends) || (function () {
3
+ var extendStatics = function (d, b) {
4
+ extendStatics = Object.setPrototypeOf ||
5
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
+ function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7
+ return extendStatics(d, b);
8
+ };
9
+ return function (d, b) {
10
+ if (typeof b !== "function" && b !== null)
11
+ throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12
+ extendStatics(d, b);
13
+ function __() { this.constructor = d; }
14
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
+ };
16
+ })();
17
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
18
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
19
+ return new (P || (P = Promise))(function (resolve, reject) {
20
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
21
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
22
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
23
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
24
+ });
25
+ };
26
+ var __generator = (this && this.__generator) || function (thisArg, body) {
27
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
28
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
29
+ function verb(n) { return function (v) { return step([n, v]); }; }
30
+ function step(op) {
31
+ if (f) throw new TypeError("Generator is already executing.");
32
+ while (_) try {
33
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
34
+ if (y = 0, t) op = [op[0] & 2, t.value];
35
+ switch (op[0]) {
36
+ case 0: case 1: t = op; break;
37
+ case 4: _.label++; return { value: op[1], done: false };
38
+ case 5: _.label++; y = op[1]; op = [0]; continue;
39
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
40
+ default:
41
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
42
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
43
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
44
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
45
+ if (t[2]) _.ops.pop();
46
+ _.trys.pop(); continue;
47
+ }
48
+ op = body.call(thisArg, _);
49
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
50
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
51
+ }
52
+ };
53
+ var __importDefault = (this && this.__importDefault) || function (mod) {
54
+ return (mod && mod.__esModule) ? mod : { "default": mod };
55
+ };
56
+ Object.defineProperty(exports, "__esModule", { value: true });
57
+ exports.MysqlStore = void 0;
58
+ var CascadeStore_1 = require("oak-domain/lib/store/CascadeStore");
59
+ var connector_1 = require("./connector");
60
+ var translator_1 = require("./translator");
61
+ var lodash_1 = require("lodash");
62
+ var assert_1 = __importDefault(require("assert"));
63
+ var relation_1 = require("oak-domain/lib/store/relation");
64
+ function convertGeoTextToObject(geoText) {
65
+ if (geoText.startsWith('POINT')) {
66
+ var coord = geoText.match((/(\d|\.)+(?=\)|\s)/g));
67
+ return {
68
+ type: 'Point',
69
+ coordinates: coord.map(function (ele) { return parseFloat(ele); }),
70
+ };
71
+ }
72
+ else {
73
+ throw new Error('only support Point now');
74
+ }
75
+ }
76
+ var MysqlStore = /** @class */ (function (_super) {
77
+ __extends(MysqlStore, _super);
78
+ function MysqlStore(storageSchema, configuration) {
79
+ var _this = _super.call(this, storageSchema) || this;
80
+ _this.connector = new connector_1.MySqlConnector(configuration);
81
+ _this.translator = new translator_1.MySqlTranslator(storageSchema);
82
+ return _this;
83
+ }
84
+ MysqlStore.prototype.supportManyToOneJoin = function () {
85
+ return true;
86
+ };
87
+ MysqlStore.prototype.supportMultipleCreate = function () {
88
+ return true;
89
+ };
90
+ MysqlStore.prototype.formResult = function (entity, result) {
91
+ var schema = this.getSchema();
92
+ function resolveAttribute(entity2, r, attr, value) {
93
+ var _a;
94
+ var _b = schema[entity2], attributes = _b.attributes, view = _b.view;
95
+ if (!view) {
96
+ var i = attr.indexOf(".");
97
+ if (i !== -1) {
98
+ var attrHead = attr.slice(0, i);
99
+ var attrTail = attr.slice(i + 1);
100
+ if (!r[attrHead]) {
101
+ r[attrHead] = {};
102
+ }
103
+ var rel = (0, relation_1.judgeRelation)(schema, entity2, attrHead);
104
+ (0, assert_1.default)(rel === 2 || typeof rel === 'string');
105
+ resolveAttribute(typeof rel === 'string' ? rel : attrHead, r[attrHead], attrTail, value);
106
+ }
107
+ else if (attributes[attr]) {
108
+ var type = attributes[attr].type;
109
+ switch (type) {
110
+ case 'date':
111
+ case 'time': {
112
+ if (value instanceof Date) {
113
+ r[attr] = value.valueOf();
114
+ }
115
+ else {
116
+ r[attr] = value;
117
+ }
118
+ break;
119
+ }
120
+ case 'geometry': {
121
+ if (typeof value === 'string') {
122
+ r[attr] = convertGeoTextToObject(value);
123
+ }
124
+ else {
125
+ r[attr] = value;
126
+ }
127
+ break;
128
+ }
129
+ case 'object':
130
+ case 'array': {
131
+ if (typeof value === 'string') {
132
+ r[attr] = JSON.parse(value.replace(/[\r]/g, '\\r').replace(/[\n]/g, '\\n'));
133
+ }
134
+ else {
135
+ r[attr] = value;
136
+ }
137
+ break;
138
+ }
139
+ case 'function': {
140
+ if (typeof value === 'string') {
141
+ // 函数的执行环境需要的参数只有创建函数者知悉,只能由上层再创建Function
142
+ r[attr] = "return ".concat(Buffer.from(value, 'base64').toString());
143
+ }
144
+ else {
145
+ r[attr] = value;
146
+ }
147
+ break;
148
+ }
149
+ case 'bool':
150
+ case 'boolean': {
151
+ if (value === 0) {
152
+ r[attr] = false;
153
+ }
154
+ else if (value === 1) {
155
+ r[attr] = true;
156
+ }
157
+ else {
158
+ r[attr] = value;
159
+ }
160
+ break;
161
+ }
162
+ default: {
163
+ r[attr] = value;
164
+ }
165
+ }
166
+ }
167
+ else {
168
+ r[attr] = value;
169
+ }
170
+ }
171
+ else {
172
+ (0, lodash_1.assign)(r, (_a = {},
173
+ _a[attr] = value,
174
+ _a));
175
+ }
176
+ }
177
+ function formalizeNullObject(r, e) {
178
+ var a2 = schema[e].attributes;
179
+ var allowFormalize = true;
180
+ for (var attr in r) {
181
+ if (typeof r[attr] === 'object' && a2[attr] && a2[attr].type === 'ref') {
182
+ if (formalizeNullObject(r[attr], a2[attr].ref)) {
183
+ r[attr] = null;
184
+ }
185
+ else {
186
+ allowFormalize = false;
187
+ }
188
+ }
189
+ else if (r[attr] !== null) {
190
+ allowFormalize = false;
191
+ }
192
+ }
193
+ return allowFormalize;
194
+ }
195
+ function formSingleRow(r) {
196
+ var result2 = {};
197
+ for (var attr in r) {
198
+ var value = r[attr];
199
+ resolveAttribute(entity, result2, attr, value);
200
+ }
201
+ formalizeNullObject(result2, entity);
202
+ return result2;
203
+ }
204
+ if (result instanceof Array) {
205
+ return result.map(function (r) { return formSingleRow(r); });
206
+ }
207
+ return formSingleRow(result);
208
+ };
209
+ MysqlStore.prototype.selectAbjointRow = function (entity, selection, context, option) {
210
+ return __awaiter(this, void 0, void 0, function () {
211
+ var sql, result;
212
+ return __generator(this, function (_a) {
213
+ switch (_a.label) {
214
+ case 0:
215
+ sql = this.translator.translateSelect(entity, selection, option);
216
+ return [4 /*yield*/, this.connector.exec(sql, context.getCurrentTxnId())];
217
+ case 1:
218
+ result = _a.sent();
219
+ return [2 /*return*/, this.formResult(entity, result)];
220
+ }
221
+ });
222
+ });
223
+ };
224
+ MysqlStore.prototype.updateAbjointRow = function (entity, operation, context, option) {
225
+ return __awaiter(this, void 0, void 0, function () {
226
+ var _a, translator, connector, action, txn, _b, data, sql, sql, sql;
227
+ return __generator(this, function (_c) {
228
+ switch (_c.label) {
229
+ case 0:
230
+ _a = this, translator = _a.translator, connector = _a.connector;
231
+ action = operation.action;
232
+ txn = context.getCurrentTxnId();
233
+ _b = action;
234
+ switch (_b) {
235
+ case 'create': return [3 /*break*/, 1];
236
+ case 'remove': return [3 /*break*/, 3];
237
+ }
238
+ return [3 /*break*/, 5];
239
+ case 1:
240
+ data = operation.data;
241
+ sql = translator.translateInsert(entity, data instanceof Array ? data : [data]);
242
+ return [4 /*yield*/, connector.exec(sql, txn)];
243
+ case 2:
244
+ _c.sent();
245
+ if (!(option === null || option === void 0 ? void 0 : option.dontCollect)) {
246
+ context.opRecords.push({
247
+ a: 'c',
248
+ d: data,
249
+ e: entity,
250
+ });
251
+ }
252
+ return [2 /*return*/, data instanceof Array ? data.length : 1];
253
+ case 3:
254
+ sql = translator.translateRemove(entity, operation, option);
255
+ return [4 /*yield*/, connector.exec(sql, txn)];
256
+ case 4:
257
+ _c.sent();
258
+ // todo 这里对sorter和indexfrom/count的支持不完整
259
+ if (!(option === null || option === void 0 ? void 0 : option.dontCollect)) {
260
+ context.opRecords.push({
261
+ a: 'r',
262
+ e: entity,
263
+ f: operation.filter,
264
+ });
265
+ }
266
+ return [2 /*return*/, 1];
267
+ case 5:
268
+ (0, assert_1.default)(!['select', 'download', 'stat'].includes(action));
269
+ sql = translator.translateUpdate(entity, operation, option);
270
+ return [4 /*yield*/, connector.exec(sql, txn)];
271
+ case 6:
272
+ _c.sent();
273
+ // todo 这里对sorter和indexfrom/count的支持不完整
274
+ if (!(option === null || option === void 0 ? void 0 : option.dontCollect)) {
275
+ context.opRecords.push({
276
+ a: 'u',
277
+ e: entity,
278
+ d: operation.data,
279
+ f: operation.filter,
280
+ });
281
+ }
282
+ return [2 /*return*/, 1];
283
+ }
284
+ });
285
+ });
286
+ };
287
+ MysqlStore.prototype.operate = function (entity, operation, context, params) {
288
+ return __awaiter(this, void 0, void 0, function () {
289
+ var action;
290
+ return __generator(this, function (_a) {
291
+ switch (_a.label) {
292
+ case 0:
293
+ action = operation.action;
294
+ (0, assert_1.default)(!['select', 'download', 'stat'].includes(action), '现在不支持使用select operation');
295
+ return [4 /*yield*/, this.cascadeUpdate(entity, operation, context, params)];
296
+ case 1: return [2 /*return*/, _a.sent()];
297
+ }
298
+ });
299
+ });
300
+ };
301
+ MysqlStore.prototype.select = function (entity, selection, context, option) {
302
+ return __awaiter(this, void 0, void 0, function () {
303
+ var result;
304
+ return __generator(this, function (_a) {
305
+ switch (_a.label) {
306
+ case 0: return [4 /*yield*/, this.cascadeSelect(entity, selection, context, option)];
307
+ case 1:
308
+ result = _a.sent();
309
+ return [2 /*return*/, {
310
+ result: result,
311
+ }];
312
+ }
313
+ });
314
+ });
315
+ };
316
+ MysqlStore.prototype.count = function (entity, selection, context, option) {
317
+ return __awaiter(this, void 0, void 0, function () {
318
+ var sql, result;
319
+ return __generator(this, function (_a) {
320
+ switch (_a.label) {
321
+ case 0:
322
+ sql = this.translator.translateCount(entity, selection, option);
323
+ return [4 /*yield*/, this.connector.exec(sql, context.getCurrentTxnId())];
324
+ case 1:
325
+ result = _a.sent();
326
+ return [2 /*return*/, result.count];
327
+ }
328
+ });
329
+ });
330
+ };
331
+ MysqlStore.prototype.begin = function (option) {
332
+ return __awaiter(this, void 0, void 0, function () {
333
+ var txn;
334
+ return __generator(this, function (_a) {
335
+ switch (_a.label) {
336
+ case 0: return [4 /*yield*/, this.connector.startTransaction(option)];
337
+ case 1:
338
+ txn = _a.sent();
339
+ return [2 /*return*/, txn];
340
+ }
341
+ });
342
+ });
343
+ };
344
+ MysqlStore.prototype.commit = function (txnId) {
345
+ return __awaiter(this, void 0, void 0, function () {
346
+ return __generator(this, function (_a) {
347
+ switch (_a.label) {
348
+ case 0: return [4 /*yield*/, this.connector.commitTransaction(txnId)];
349
+ case 1:
350
+ _a.sent();
351
+ return [2 /*return*/];
352
+ }
353
+ });
354
+ });
355
+ };
356
+ MysqlStore.prototype.rollback = function (txnId) {
357
+ return __awaiter(this, void 0, void 0, function () {
358
+ return __generator(this, function (_a) {
359
+ switch (_a.label) {
360
+ case 0: return [4 /*yield*/, this.connector.rollbackTransaction(txnId)];
361
+ case 1:
362
+ _a.sent();
363
+ return [2 /*return*/];
364
+ }
365
+ });
366
+ });
367
+ };
368
+ MysqlStore.prototype.connect = function () {
369
+ this.connector.connect();
370
+ };
371
+ MysqlStore.prototype.disconnect = function () {
372
+ this.connector.disconnect();
373
+ };
374
+ MysqlStore.prototype.initialize = function (dropIfExists) {
375
+ return __awaiter(this, void 0, void 0, function () {
376
+ var schema, _a, _b, _i, entity, sqls, _c, sqls_1, sql;
377
+ return __generator(this, function (_d) {
378
+ switch (_d.label) {
379
+ case 0:
380
+ schema = this.getSchema();
381
+ _a = [];
382
+ for (_b in schema)
383
+ _a.push(_b);
384
+ _i = 0;
385
+ _d.label = 1;
386
+ case 1:
387
+ if (!(_i < _a.length)) return [3 /*break*/, 6];
388
+ entity = _a[_i];
389
+ sqls = this.translator.translateCreateEntity(entity, { replace: dropIfExists });
390
+ _c = 0, sqls_1 = sqls;
391
+ _d.label = 2;
392
+ case 2:
393
+ if (!(_c < sqls_1.length)) return [3 /*break*/, 5];
394
+ sql = sqls_1[_c];
395
+ return [4 /*yield*/, this.connector.exec(sql)];
396
+ case 3:
397
+ _d.sent();
398
+ _d.label = 4;
399
+ case 4:
400
+ _c++;
401
+ return [3 /*break*/, 2];
402
+ case 5:
403
+ _i++;
404
+ return [3 /*break*/, 1];
405
+ case 6: return [2 /*return*/];
406
+ }
407
+ });
408
+ });
409
+ };
410
+ return MysqlStore;
411
+ }(CascadeStore_1.CascadeStore));
412
+ exports.MysqlStore = MysqlStore;