适配了新的框架声明
This commit is contained in:
parent
a792fdd415
commit
a156b0cc14
|
|
@ -1,20 +1,24 @@
|
|||
import { EntityDict, Context, DeduceCreateSingleOperation, DeduceRemoveOperation, DeduceUpdateOperation, OperateOption, OperationResult, SelectionResult, TxnOption, SelectRowShape, StorageSchema, DeduceCreateMultipleOperation, SelectOption } from 'oak-domain/lib/types';
|
||||
import { EntityDict, DeduceCreateSingleOperation, DeduceRemoveOperation, DeduceUpdateOperation, OperateOption, OperationResult, TxnOption, StorageSchema, DeduceCreateMultipleOperation, SelectOption } from 'oak-domain/lib/types';
|
||||
import { EntityDict as BaseEntityDict } from 'oak-domain/lib/base-app-domain';
|
||||
import { CascadeStore } from 'oak-domain/lib/store/CascadeStore';
|
||||
import { MySQLConfiguration } from './types/Configuration';
|
||||
import { MySqlConnector } from './connector';
|
||||
import { MySqlTranslator, MySqlSelectOption, MysqlOperateOption } from './translator';
|
||||
export declare class MysqlStore<ED extends EntityDict & BaseEntityDict, Cxt extends Context<ED>> extends CascadeStore<ED, Cxt> {
|
||||
import { AsyncContext, AsyncRowStore } from 'oak-domain/lib/store/AsyncRowStore';
|
||||
import { SyncContext } from 'oak-domain/lib/store/SyncRowStore';
|
||||
export declare class MysqlStore<ED extends EntityDict & BaseEntityDict, Cxt extends AsyncContext<ED>> extends CascadeStore<ED> implements AsyncRowStore<ED, Cxt> {
|
||||
protected selectAbjointRow<T extends keyof ED, OP extends SelectOption>(entity: T, selection: ED[T]['Selection'], context: SyncContext<ED>, option: OP): Partial<ED[T]['Schema']>[];
|
||||
protected updateAbjointRow<T extends keyof ED, OP extends OperateOption>(entity: T, operation: ED[T]['Operation'], context: SyncContext<ED>, option: OP): number;
|
||||
connector: MySqlConnector;
|
||||
translator: MySqlTranslator<ED>;
|
||||
constructor(storageSchema: StorageSchema<ED>, configuration: MySQLConfiguration);
|
||||
protected supportManyToOneJoin(): boolean;
|
||||
protected supportMultipleCreate(): boolean;
|
||||
private formResult;
|
||||
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']>[]>;
|
||||
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>;
|
||||
protected selectAbjointRowAsync<T extends keyof ED>(entity: T, selection: ED[T]['Selection'], context: AsyncContext<ED>, option?: MySqlSelectOption): Promise<Partial<ED[T]['Schema']>[]>;
|
||||
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>;
|
||||
operate<T extends keyof ED>(entity: T, operation: ED[T]['Operation'], context: Cxt, option: OperateOption): Promise<OperationResult<ED>>;
|
||||
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']>>;
|
||||
select<T extends keyof ED>(entity: T, selection: ED[T]['Selection'], context: Cxt, option: SelectOption): Promise<Partial<ED[T]['Schema']>[]>;
|
||||
count<T extends keyof ED>(entity: T, selection: Pick<ED[T]['Selection'], 'filter' | 'count'>, context: Cxt, option: SelectOption): Promise<number>;
|
||||
begin(option?: TxnOption): Promise<string>;
|
||||
commit(txnId: string): Promise<void>;
|
||||
|
|
|
|||
|
|
@ -28,6 +28,12 @@ var MysqlStore = /** @class */ (function (_super) {
|
|||
_this.translator = new translator_1.MySqlTranslator(storageSchema);
|
||||
return _this;
|
||||
}
|
||||
MysqlStore.prototype.selectAbjointRow = function (entity, selection, context, option) {
|
||||
throw new Error('MySQL store不支持同步取数据,不应该跑到这儿');
|
||||
};
|
||||
MysqlStore.prototype.updateAbjointRow = function (entity, operation, context, option) {
|
||||
throw new Error('MySQL store不支持同步更新数据,不应该跑到这儿');
|
||||
};
|
||||
MysqlStore.prototype.supportManyToOneJoin = function () {
|
||||
return true;
|
||||
};
|
||||
|
|
@ -166,7 +172,7 @@ var MysqlStore = /** @class */ (function (_super) {
|
|||
}
|
||||
return formSingleRow(result);
|
||||
};
|
||||
MysqlStore.prototype.selectAbjointRow = function (entity, selection, context, option) {
|
||||
MysqlStore.prototype.selectAbjointRowAsync = function (entity, selection, context, option) {
|
||||
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
||||
var sql, result;
|
||||
return tslib_1.__generator(this, function (_a) {
|
||||
|
|
@ -181,7 +187,7 @@ var MysqlStore = /** @class */ (function (_super) {
|
|||
});
|
||||
});
|
||||
};
|
||||
MysqlStore.prototype.updateAbjointRow = function (entity, operation, context, option) {
|
||||
MysqlStore.prototype.updateAbjointRowAsync = function (entity, operation, context, option) {
|
||||
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
||||
var _a, translator, connector, action, txn, _b, data, sql, sql, sql;
|
||||
return tslib_1.__generator(this, function (_c) {
|
||||
|
|
@ -252,7 +258,7 @@ var MysqlStore = /** @class */ (function (_super) {
|
|||
case 0:
|
||||
action = operation.action;
|
||||
(0, assert_1.default)(!['select', 'download', 'stat'].includes(action), '现在不支持使用select operation');
|
||||
return [4 /*yield*/, this.cascadeUpdate(entity, operation, context, option)];
|
||||
return [4 /*yield*/, this.cascadeUpdateAsync(entity, operation, context, option)];
|
||||
case 1: return [2 /*return*/, _a.sent()];
|
||||
}
|
||||
});
|
||||
|
|
@ -263,12 +269,10 @@ var MysqlStore = /** @class */ (function (_super) {
|
|||
var result;
|
||||
return tslib_1.__generator(this, function (_a) {
|
||||
switch (_a.label) {
|
||||
case 0: return [4 /*yield*/, this.cascadeSelect(entity, selection, context, option)];
|
||||
case 0: return [4 /*yield*/, this.cascadeSelectAsync(entity, selection, context, option)];
|
||||
case 1:
|
||||
result = _a.sent();
|
||||
return [2 /*return*/, {
|
||||
result: result,
|
||||
}];
|
||||
return [2 /*return*/, result];
|
||||
}
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -124,7 +124,7 @@ var MySqlTranslator = /** @class */ (function (_super) {
|
|||
}
|
||||
};
|
||||
MySqlTranslator.prototype.populateDataTypeDef = function (type, params) {
|
||||
if (['date', 'datetime', 'time'].includes(type)) {
|
||||
if (['date', 'datetime', 'time', 'sequence'].includes(type)) {
|
||||
return 'bigint ';
|
||||
}
|
||||
if (['object', 'array'].includes(type)) {
|
||||
|
|
@ -250,6 +250,7 @@ var MySqlTranslator = /** @class */ (function (_super) {
|
|||
var schema = this.schema;
|
||||
var entityDef = schema[entity];
|
||||
var storageName = entityDef.storageName, attributes = entityDef.attributes, indexes = entityDef.indexes, view = entityDef.view;
|
||||
var hasSequence = false;
|
||||
// todo view暂还不支持
|
||||
var entityType = view ? 'view' : 'table';
|
||||
var sql = "create ".concat(entityType, " ");
|
||||
|
|
@ -267,7 +268,7 @@ var MySqlTranslator = /** @class */ (function (_super) {
|
|||
// 翻译所有的属性
|
||||
Object.keys(attributes).forEach(function (attr, idx) {
|
||||
var attrDef = attributes[attr];
|
||||
var type = attrDef.type, params = attrDef.params, defaultValue = attrDef.default, unique = attrDef.unique, notNull = attrDef.notNull;
|
||||
var type = attrDef.type, params = attrDef.params, defaultValue = attrDef.default, unique = attrDef.unique, notNull = attrDef.notNull, sequenceStart = attrDef.sequenceStart;
|
||||
sql += "`".concat(attr, "` ");
|
||||
sql += _this.populateDataTypeDef(type, params);
|
||||
if (notNull || type === 'geometry') {
|
||||
|
|
@ -276,6 +277,13 @@ var MySqlTranslator = /** @class */ (function (_super) {
|
|||
if (unique) {
|
||||
sql += ' unique ';
|
||||
}
|
||||
if (sequenceStart) {
|
||||
if (hasSequence) {
|
||||
throw new Error("\u300C".concat(entity, "\u300D\u53EA\u80FD\u6709\u4E00\u4E2Asequence\u5217"));
|
||||
}
|
||||
hasSequence = sequenceStart;
|
||||
sql += ' auto_increment unique ';
|
||||
}
|
||||
if (defaultValue !== undefined) {
|
||||
(0, assert_1.default)(type !== 'ref');
|
||||
sql += " default ".concat(_this.translateAttrValue(type, defaultValue));
|
||||
|
|
@ -338,6 +346,9 @@ var MySqlTranslator = /** @class */ (function (_super) {
|
|||
}
|
||||
}
|
||||
sql += ')';
|
||||
if (typeof hasSequence === 'number') {
|
||||
sql += "auto_increment = ".concat(hasSequence);
|
||||
}
|
||||
if (!replace) {
|
||||
return [sql];
|
||||
}
|
||||
|
|
|
|||
|
|
@ -24,6 +24,10 @@ var SqlTranslator = /** @class */ (function () {
|
|||
length: 36,
|
||||
},
|
||||
},
|
||||
$$seq$$: {
|
||||
type: 'sequence',
|
||||
sequenceStart: 10000,
|
||||
},
|
||||
$$createAt$$: {
|
||||
type: 'datetime',
|
||||
notNull: true,
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
import { EntityDict, Context, DeduceCreateSingleOperation, DeduceRemoveOperation, DeduceUpdateOperation, OperateOption, OperationResult, SelectionResult, TxnOption, SelectRowShape, StorageSchema, DeduceCreateMultipleOperation, SelectOption } from 'oak-domain/lib/types';
|
||||
import { EntityDict, DeduceCreateSingleOperation, DeduceRemoveOperation, DeduceUpdateOperation, OperateOption,
|
||||
OperationResult, TxnOption, StorageSchema, DeduceCreateMultipleOperation, SelectOption } from 'oak-domain/lib/types';
|
||||
import { EntityDict as BaseEntityDict } from 'oak-domain/lib/base-app-domain';
|
||||
import { CascadeStore } from 'oak-domain/lib/store/CascadeStore';
|
||||
import { MySQLConfiguration } from './types/Configuration';
|
||||
|
|
@ -7,6 +8,8 @@ import { MySqlTranslator, MySqlSelectOption, MysqlOperateOption } from './transl
|
|||
import { assign } from 'lodash';
|
||||
import assert from 'assert';
|
||||
import { judgeRelation } from 'oak-domain/lib/store/relation';
|
||||
import { AsyncContext, AsyncRowStore } from 'oak-domain/lib/store/AsyncRowStore';
|
||||
import { SyncContext } from 'oak-domain/lib/store/SyncRowStore';
|
||||
|
||||
|
||||
function convertGeoTextToObject(geoText: string): object {
|
||||
|
|
@ -25,7 +28,13 @@ function convertGeoTextToObject(geoText: string): object {
|
|||
}
|
||||
}
|
||||
|
||||
export class MysqlStore<ED extends EntityDict & BaseEntityDict, Cxt extends Context<ED>> extends CascadeStore<ED, Cxt> {
|
||||
export class MysqlStore<ED extends EntityDict & BaseEntityDict, Cxt extends AsyncContext<ED>> extends CascadeStore<ED> implements AsyncRowStore<ED, Cxt>{
|
||||
protected selectAbjointRow<T extends keyof ED, OP extends SelectOption>(entity: T, selection: ED[T]['Selection'], context: SyncContext<ED>, option: OP): Partial<ED[T]['Schema']>[] {
|
||||
throw new Error('MySQL store不支持同步取数据,不应该跑到这儿');
|
||||
}
|
||||
protected updateAbjointRow<T extends keyof ED, OP extends OperateOption>(entity: T, operation: ED[T]['Operation'], context: SyncContext<ED>, option: OP): number {
|
||||
throw new Error('MySQL store不支持同步更新数据,不应该跑到这儿');
|
||||
}
|
||||
connector: MySqlConnector;
|
||||
translator: MySqlTranslator<ED>;
|
||||
constructor(storageSchema: StorageSchema<ED>, configuration: MySQLConfiguration) {
|
||||
|
|
@ -178,21 +187,21 @@ export class MysqlStore<ED extends EntityDict & BaseEntityDict, Cxt extends Cont
|
|||
}
|
||||
return formSingleRow(result);
|
||||
}
|
||||
protected async selectAbjointRow<T extends keyof ED, S extends ED[T]['Selection']>(
|
||||
protected async selectAbjointRowAsync<T extends keyof ED>(
|
||||
entity: T,
|
||||
selection: S,
|
||||
context: Cxt,
|
||||
selection: ED[T]['Selection'],
|
||||
context: AsyncContext<ED>,
|
||||
option?: MySqlSelectOption
|
||||
): Promise<SelectRowShape<ED[T]['Schema'], S['data']>[]> {
|
||||
): Promise<Partial<ED[T]['Schema']>[]> {
|
||||
const sql = this.translator.translateSelect(entity, selection, option);
|
||||
const result = await this.connector.exec(sql, context.getCurrentTxnId());
|
||||
|
||||
return this.formResult(entity, result);
|
||||
}
|
||||
protected async updateAbjointRow<T extends keyof ED>(
|
||||
protected async 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: Cxt,
|
||||
context: AsyncContext<ED>,
|
||||
option?: MysqlOperateOption
|
||||
): Promise<number> {
|
||||
const { translator, connector } = this;
|
||||
|
|
@ -248,13 +257,11 @@ export class MysqlStore<ED extends EntityDict & BaseEntityDict, Cxt extends Cont
|
|||
async operate<T extends keyof ED>(entity: T, operation: ED[T]['Operation'], context: Cxt, option: OperateOption): Promise<OperationResult<ED>> {
|
||||
const { action } = operation;
|
||||
assert(!['select', 'download', 'stat'].includes(action), '现在不支持使用select operation');
|
||||
return await this.cascadeUpdate(entity, operation as any, context, option);
|
||||
return await this.cascadeUpdateAsync(entity, operation as any, context, option);
|
||||
}
|
||||
async 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']>> {
|
||||
const result = await this.cascadeSelect(entity, selection, context, option);
|
||||
return {
|
||||
result,
|
||||
};
|
||||
async select<T extends keyof ED>(entity: T, selection: ED[T]['Selection'], context: Cxt, option: SelectOption): Promise<Partial<ED[T]['Schema']>[]> {
|
||||
const result = await this.cascadeSelectAsync(entity, selection, context, option);
|
||||
return result;
|
||||
}
|
||||
async count<T extends keyof ED>(entity: T, selection: Pick<ED[T]['Selection'], 'filter' | 'count'>, context: Cxt, option: SelectOption): Promise<number> {
|
||||
const sql = this.translator.translateCount(entity, selection, option);
|
||||
|
|
|
|||
|
|
@ -297,7 +297,7 @@ export class MySqlTranslator<ED extends EntityDict> extends SqlTranslator<ED> {
|
|||
|
||||
maxAliasLength = 63;
|
||||
private populateDataTypeDef(type: DataType | Ref, params?: DataTypeParams): string{
|
||||
if (['date', 'datetime', 'time'].includes(type)) {
|
||||
if (['date', 'datetime', 'time', 'sequence'].includes(type)) {
|
||||
return 'bigint ';
|
||||
}
|
||||
if (['object', 'array'].includes(type)) {
|
||||
|
|
@ -305,7 +305,7 @@ export class MySqlTranslator<ED extends EntityDict> extends SqlTranslator<ED> {
|
|||
}
|
||||
if (['image', 'function'].includes(type)) {
|
||||
return 'text ';
|
||||
}
|
||||
}
|
||||
if (type === 'ref') {
|
||||
return 'char(36)';
|
||||
}
|
||||
|
|
@ -431,6 +431,7 @@ export class MySqlTranslator<ED extends EntityDict> extends SqlTranslator<ED> {
|
|||
const entityDef = schema[entity];
|
||||
const { storageName, attributes, indexes, view } = entityDef;
|
||||
|
||||
let hasSequence: boolean | number = false;
|
||||
// todo view暂还不支持
|
||||
const entityType = view ? 'view' : 'table';
|
||||
let sql = `create ${entityType} `;
|
||||
|
|
@ -456,6 +457,7 @@ export class MySqlTranslator<ED extends EntityDict> extends SqlTranslator<ED> {
|
|||
default: defaultValue,
|
||||
unique,
|
||||
notNull,
|
||||
sequenceStart,
|
||||
} = attrDef;
|
||||
sql += `\`${attr}\` `
|
||||
sql += this.populateDataTypeDef(type, params) as string;
|
||||
|
|
@ -466,6 +468,13 @@ export class MySqlTranslator<ED extends EntityDict> extends SqlTranslator<ED> {
|
|||
if (unique) {
|
||||
sql += ' unique ';
|
||||
}
|
||||
if (sequenceStart) {
|
||||
if (hasSequence) {
|
||||
throw new Error(`「${entity as string}」只能有一个sequence列`);
|
||||
}
|
||||
hasSequence = sequenceStart;
|
||||
sql += ' auto_increment unique ';
|
||||
}
|
||||
if (defaultValue !== undefined) {
|
||||
assert(type !== 'ref');
|
||||
sql += ` default ${this.translateAttrValue(type, defaultValue)}`;
|
||||
|
|
@ -535,6 +544,9 @@ export class MySqlTranslator<ED extends EntityDict> extends SqlTranslator<ED> {
|
|||
|
||||
|
||||
sql += ')';
|
||||
if (typeof hasSequence === 'number') {
|
||||
sql += `auto_increment = ${hasSequence}`;
|
||||
}
|
||||
|
||||
if (!replace) {
|
||||
return [sql];
|
||||
|
|
|
|||
|
|
@ -27,6 +27,10 @@ export abstract class SqlTranslator<ED extends EntityDict> {
|
|||
length: 36,
|
||||
},
|
||||
} as Attribute,
|
||||
$$seq$$: {
|
||||
type: 'sequence',
|
||||
sequenceStart: 10000,
|
||||
} as Attribute,
|
||||
$$createAt$$: {
|
||||
type: 'datetime',
|
||||
notNull: true,
|
||||
|
|
|
|||
Loading…
Reference in New Issue