oak-db 3.3.11 → 3.3.12

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,15 +1,15 @@
1
- import mysql, { Pool } from 'mysql2/promise';
2
- import { TxnOption } from 'oak-domain/lib/types';
3
- import { MySQLConfiguration } from './types/Configuration';
4
- export declare class MySqlConnector {
5
- pool: Pool;
6
- configuration: MySQLConfiguration;
7
- txnDict: Record<string, mysql.PoolConnection>;
8
- constructor(configuration: MySQLConfiguration);
9
- connect(): void;
10
- disconnect(): Promise<void>;
11
- startTransaction(option?: TxnOption): Promise<string>;
12
- exec(sql: string, txn?: string): Promise<[mysql.RowDataPacket[] | mysql.RowDataPacket[][] | mysql.OkPacket | mysql.OkPacket[] | mysql.ResultSetHeader, mysql.FieldPacket[]]>;
13
- commitTransaction(txn: string): Promise<void>;
14
- rollbackTransaction(txn: string): Promise<void>;
15
- }
1
+ import mysql, { Pool } from 'mysql2/promise';
2
+ import { TxnOption } from 'oak-domain/lib/types';
3
+ import { MySQLConfiguration } from './types/Configuration';
4
+ export declare class MySqlConnector {
5
+ pool: Pool;
6
+ configuration: MySQLConfiguration;
7
+ txnDict: Record<string, mysql.PoolConnection>;
8
+ constructor(configuration: MySQLConfiguration);
9
+ connect(): void;
10
+ disconnect(): Promise<void>;
11
+ startTransaction(option?: TxnOption): Promise<string>;
12
+ exec(sql: string, txn?: string): Promise<[mysql.RowDataPacket[] | mysql.RowDataPacket[][] | mysql.OkPacket | mysql.OkPacket[] | mysql.ResultSetHeader, mysql.FieldPacket[]]>;
13
+ commitTransaction(txn: string): Promise<void>;
14
+ rollbackTransaction(txn: string): Promise<void>;
15
+ }
@@ -1,77 +1,77 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.MySqlConnector = void 0;
4
- const tslib_1 = require("tslib");
5
- const promise_1 = tslib_1.__importDefault(require("mysql2/promise"));
6
- const uuid_1 = require("uuid");
7
- const assert_1 = tslib_1.__importDefault(require("assert"));
8
- class MySqlConnector {
9
- pool;
10
- configuration;
11
- txnDict;
12
- constructor(configuration) {
13
- this.configuration = configuration;
14
- this.txnDict = {};
15
- this.pool = promise_1.default.createPool(this.configuration);
16
- }
17
- connect() {
18
- }
19
- disconnect() {
20
- return this.pool.end();
21
- }
22
- async startTransaction(option) {
23
- // 防止用户配置connection可复用
24
- const startInner = async () => {
25
- const connection = await this.pool.getConnection();
26
- // 分配出来的connection不能被别的事务占据
27
- for (const txn2 in this.txnDict) {
28
- if (this.txnDict[txn2] === connection) {
29
- return new Promise((resolve) => {
30
- this.pool.on('release', () => resolve(startInner()));
31
- });
32
- }
33
- }
34
- await connection.beginTransaction();
35
- const id = (0, uuid_1.v4)();
36
- // console.log('start_txn', id, connection.threadId);
37
- this.txnDict[id] = connection;
38
- if (option?.isolationLevel) {
39
- await connection.query(`SET TRANSACTION ISOLATION LEVEL ${option.isolationLevel};`);
40
- }
41
- return id;
42
- };
43
- return startInner();
44
- }
45
- async exec(sql, txn) {
46
- if (process.env.NODE_ENV === 'development') {
47
- // console.log(sql);
48
- }
49
- if (txn) {
50
- const connection = this.txnDict[txn];
51
- (0, assert_1.default)(connection);
52
- const result = await connection.query(sql);
53
- return result;
54
- }
55
- else {
56
- const result = await this.pool.query(sql);
57
- return result;
58
- }
59
- }
60
- async commitTransaction(txn) {
61
- const connection = this.txnDict[txn];
62
- (0, assert_1.default)(connection);
63
- delete this.txnDict[txn];
64
- // console.log('commit_txn', txn, connection.threadId);
65
- await connection.commit();
66
- connection.release();
67
- }
68
- async rollbackTransaction(txn) {
69
- const connection = this.txnDict[txn];
70
- (0, assert_1.default)(connection);
71
- // console.log('rollback_txn', txn, connection.threadId);
72
- await connection.rollback();
73
- delete this.txnDict[txn];
74
- connection.release();
75
- }
76
- }
77
- exports.MySqlConnector = MySqlConnector;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.MySqlConnector = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const promise_1 = tslib_1.__importDefault(require("mysql2/promise"));
6
+ const uuid_1 = require("uuid");
7
+ const assert_1 = tslib_1.__importDefault(require("assert"));
8
+ class MySqlConnector {
9
+ pool;
10
+ configuration;
11
+ txnDict;
12
+ constructor(configuration) {
13
+ this.configuration = configuration;
14
+ this.txnDict = {};
15
+ this.pool = promise_1.default.createPool(this.configuration);
16
+ }
17
+ connect() {
18
+ }
19
+ disconnect() {
20
+ return this.pool.end();
21
+ }
22
+ async startTransaction(option) {
23
+ // 防止用户配置connection可复用
24
+ const startInner = async () => {
25
+ const connection = await this.pool.getConnection();
26
+ // 分配出来的connection不能被别的事务占据
27
+ for (const txn2 in this.txnDict) {
28
+ if (this.txnDict[txn2] === connection) {
29
+ return new Promise((resolve) => {
30
+ this.pool.on('release', () => resolve(startInner()));
31
+ });
32
+ }
33
+ }
34
+ await connection.beginTransaction();
35
+ const id = (0, uuid_1.v4)();
36
+ // console.log('start_txn', id, connection.threadId);
37
+ this.txnDict[id] = connection;
38
+ if (option?.isolationLevel) {
39
+ await connection.query(`SET TRANSACTION ISOLATION LEVEL ${option.isolationLevel};`);
40
+ }
41
+ return id;
42
+ };
43
+ return startInner();
44
+ }
45
+ async exec(sql, txn) {
46
+ if (process.env.NODE_ENV === 'development') {
47
+ // console.log(`${sql}; \n`);
48
+ }
49
+ if (txn) {
50
+ const connection = this.txnDict[txn];
51
+ (0, assert_1.default)(connection);
52
+ const result = await connection.query(sql);
53
+ return result;
54
+ }
55
+ else {
56
+ const result = await this.pool.query(sql);
57
+ return result;
58
+ }
59
+ }
60
+ async commitTransaction(txn) {
61
+ const connection = this.txnDict[txn];
62
+ (0, assert_1.default)(connection);
63
+ delete this.txnDict[txn];
64
+ // console.log('commit_txn', txn, connection.threadId);
65
+ await connection.commit();
66
+ connection.release();
67
+ }
68
+ async rollbackTransaction(txn) {
69
+ const connection = this.txnDict[txn];
70
+ (0, assert_1.default)(connection);
71
+ // console.log('rollback_txn', txn, connection.threadId);
72
+ await connection.rollback();
73
+ delete this.txnDict[txn];
74
+ connection.release();
75
+ }
76
+ }
77
+ exports.MySqlConnector = MySqlConnector;
@@ -1,37 +1,63 @@
1
- import { EntityDict, OperateOption, OperationResult, TxnOption, StorageSchema, SelectOption, AggregationResult } from 'oak-domain/lib/types';
2
- import { EntityDict as BaseEntityDict } from 'oak-domain/lib/base-app-domain';
3
- import { CascadeStore } from 'oak-domain/lib/store/CascadeStore';
4
- import { MySQLConfiguration } from './types/Configuration';
5
- import { MySqlConnector } from './connector';
6
- import { MySqlTranslator, MySqlSelectOption, MysqlOperateOption } from './translator';
7
- import { AsyncContext, AsyncRowStore } from 'oak-domain/lib/store/AsyncRowStore';
8
- import { SyncContext } from 'oak-domain/lib/store/SyncRowStore';
9
- import { CreateEntityOption } from '../types/Translator';
10
- export declare class MysqlStore<ED extends EntityDict & BaseEntityDict, Cxt extends AsyncContext<ED>> extends CascadeStore<ED> implements AsyncRowStore<ED, Cxt> {
11
- protected countAbjointRow<T extends keyof ED, OP extends SelectOption, Cxt extends SyncContext<ED>>(entity: T, selection: Pick<ED[T]['Selection'], 'filter' | 'count'>, context: Cxt, option: OP): number;
12
- protected aggregateAbjointRowSync<T extends keyof ED, OP extends SelectOption, Cxt extends SyncContext<ED>>(entity: T, aggregation: ED[T]['Aggregation'], context: Cxt, option: OP): AggregationResult<ED[T]['Schema']>;
13
- protected selectAbjointRow<T extends keyof ED, OP extends SelectOption>(entity: T, selection: ED[T]['Selection'], context: SyncContext<ED>, option: OP): Partial<ED[T]['Schema']>[];
14
- protected updateAbjointRow<T extends keyof ED, OP extends OperateOption>(entity: T, operation: ED[T]['Operation'], context: SyncContext<ED>, option: OP): number;
15
- exec(script: string, txnId?: string): Promise<void>;
16
- connector: MySqlConnector;
17
- translator: MySqlTranslator<ED>;
18
- constructor(storageSchema: StorageSchema<ED>, configuration: MySQLConfiguration);
19
- checkRelationAsync<T extends keyof ED, Cxt extends AsyncContext<ED>>(entity: T, operation: Omit<ED[T]['Operation'] | ED[T]['Selection'], 'id'>, context: Cxt): Promise<void>;
20
- protected aggregateAbjointRowAsync<T extends keyof ED, OP extends SelectOption, Cxt extends AsyncContext<ED>>(entity: T, aggregation: ED[T]['Aggregation'], context: Cxt, option: OP): Promise<AggregationResult<ED[T]['Schema']>>;
21
- aggregate<T extends keyof ED, OP extends SelectOption>(entity: T, aggregation: ED[T]['Aggregation'], context: Cxt, option: OP): Promise<AggregationResult<ED[T]['Schema']>>;
22
- protected supportManyToOneJoin(): boolean;
23
- protected supportMultipleCreate(): boolean;
24
- private formResult;
25
- protected selectAbjointRowAsync<T extends keyof ED>(entity: T, selection: ED[T]['Selection'], context: AsyncContext<ED>, option?: MySqlSelectOption): Promise<Partial<ED[T]['Schema']>[]>;
26
- protected updateAbjointRowAsync<T extends keyof ED>(entity: T, operation: ED[T]['Operation'], context: AsyncContext<ED>, option?: MysqlOperateOption): Promise<number>;
27
- operate<T extends keyof ED>(entity: T, operation: ED[T]['Operation'], context: Cxt, option: OperateOption): Promise<OperationResult<ED>>;
28
- select<T extends keyof ED>(entity: T, selection: ED[T]['Selection'], context: Cxt, option: SelectOption): Promise<Partial<ED[T]['Schema']>[]>;
29
- protected countAbjointRowAsync<T extends keyof ED>(entity: T, selection: Pick<ED[T]['Selection'], 'filter' | 'count'>, context: AsyncContext<ED>, option: SelectOption): Promise<number>;
30
- count<T extends keyof ED>(entity: T, selection: Pick<ED[T]['Selection'], 'filter' | 'count'>, context: Cxt, option: SelectOption): Promise<number>;
31
- begin(option?: TxnOption): Promise<string>;
32
- commit(txnId: string): Promise<void>;
33
- rollback(txnId: string): Promise<void>;
34
- connect(): void;
35
- disconnect(): Promise<void>;
36
- initialize(option: CreateEntityOption): Promise<void>;
37
- }
1
+ import { EntityDict, OperateOption, OperationResult, TxnOption, StorageSchema, SelectOption, AggregationResult, Attribute, Index } from 'oak-domain/lib/types';
2
+ import { EntityDict as BaseEntityDict } from 'oak-domain/lib/base-app-domain';
3
+ import { CascadeStore } from 'oak-domain/lib/store/CascadeStore';
4
+ import { MySQLConfiguration } from './types/Configuration';
5
+ import { MySqlConnector } from './connector';
6
+ import { MySqlTranslator, MySqlSelectOption, MysqlOperateOption } from './translator';
7
+ import { AsyncContext } from 'oak-domain/lib/store/AsyncRowStore';
8
+ import { SyncContext } from 'oak-domain/lib/store/SyncRowStore';
9
+ import { CreateEntityOption } from '../types/Translator';
10
+ import { DbStore } from '../types/dbStore';
11
+ export declare class MysqlStore<ED extends EntityDict & BaseEntityDict, Cxt extends AsyncContext<ED>> extends CascadeStore<ED> implements DbStore<ED, Cxt> {
12
+ protected countAbjointRow<T extends keyof ED, OP extends SelectOption, Cxt extends SyncContext<ED>>(entity: T, selection: Pick<ED[T]['Selection'], 'filter' | 'count'>, context: Cxt, option: OP): number;
13
+ protected aggregateAbjointRowSync<T extends keyof ED, OP extends SelectOption, Cxt extends SyncContext<ED>>(entity: T, aggregation: ED[T]['Aggregation'], context: Cxt, option: OP): AggregationResult<ED[T]['Schema']>;
14
+ protected selectAbjointRow<T extends keyof ED, OP extends SelectOption>(entity: T, selection: ED[T]['Selection'], context: SyncContext<ED>, option: OP): Partial<ED[T]['Schema']>[];
15
+ protected updateAbjointRow<T extends keyof ED, OP extends OperateOption>(entity: T, operation: ED[T]['Operation'], context: SyncContext<ED>, option: OP): number;
16
+ exec(script: string, txnId?: string): Promise<void>;
17
+ connector: MySqlConnector;
18
+ translator: MySqlTranslator<ED>;
19
+ constructor(storageSchema: StorageSchema<ED>, configuration: MySQLConfiguration);
20
+ checkRelationAsync<T extends keyof ED, Cxt extends AsyncContext<ED>>(entity: T, operation: Omit<ED[T]['Operation'] | ED[T]['Selection'], 'id'>, context: Cxt): Promise<void>;
21
+ protected aggregateAbjointRowAsync<T extends keyof ED, OP extends SelectOption, Cxt extends AsyncContext<ED>>(entity: T, aggregation: ED[T]['Aggregation'], context: Cxt, option: OP): Promise<AggregationResult<ED[T]['Schema']>>;
22
+ aggregate<T extends keyof ED, OP extends SelectOption>(entity: T, aggregation: ED[T]['Aggregation'], context: Cxt, option: OP): Promise<AggregationResult<ED[T]['Schema']>>;
23
+ protected supportManyToOneJoin(): boolean;
24
+ protected supportMultipleCreate(): boolean;
25
+ private formResult;
26
+ protected selectAbjointRowAsync<T extends keyof ED>(entity: T, selection: ED[T]['Selection'], context: AsyncContext<ED>, option?: MySqlSelectOption): Promise<Partial<ED[T]['Schema']>[]>;
27
+ protected updateAbjointRowAsync<T extends keyof ED>(entity: T, operation: ED[T]['Operation'], context: AsyncContext<ED>, option?: MysqlOperateOption): Promise<number>;
28
+ operate<T extends keyof ED>(entity: T, operation: ED[T]['Operation'], context: Cxt, option: OperateOption): Promise<OperationResult<ED>>;
29
+ select<T extends keyof ED>(entity: T, selection: ED[T]['Selection'], context: Cxt, option: SelectOption): Promise<Partial<ED[T]['Schema']>[]>;
30
+ protected countAbjointRowAsync<T extends keyof ED>(entity: T, selection: Pick<ED[T]['Selection'], 'filter' | 'count'>, context: AsyncContext<ED>, option: SelectOption): Promise<number>;
31
+ count<T extends keyof ED>(entity: T, selection: Pick<ED[T]['Selection'], 'filter' | 'count'>, context: Cxt, option: SelectOption): Promise<number>;
32
+ begin(option?: TxnOption): Promise<string>;
33
+ commit(txnId: string): Promise<void>;
34
+ rollback(txnId: string): Promise<void>;
35
+ connect(): Promise<void>;
36
+ disconnect(): Promise<void>;
37
+ initialize(option: CreateEntityOption): Promise<void>;
38
+ readSchema(): Promise<StorageSchema<ED>>;
39
+ /**
40
+ * 根据载入的dataSchema,和数据库中原来的schema,决定如何来upgrade
41
+ * 制订出来的plan分为两阶段:增加阶段和削减阶段,在两个阶段之间,由用户来修正数据
42
+ */
43
+ makeUpgradePlan(): Promise<Plan>;
44
+ /**
45
+ * 比较两个schema的不同,这里计算的是new对old的增量
46
+ * @param schemaOld
47
+ * @param SchemaNew
48
+ */
49
+ diffSchema(schemaOld: StorageSchema<any>, schemaNew: StorageSchema<any>): Plan;
50
+ }
51
+ type Plan = {
52
+ newTables: Record<string, {
53
+ attributes: Record<string, Attribute>;
54
+ }>;
55
+ newIndexes: Record<string, Index<any>[]>;
56
+ updatedTables: Record<string, {
57
+ attributes: Record<string, Attribute & {
58
+ isNew: boolean;
59
+ }>;
60
+ }>;
61
+ updatedIndexes: Record<string, Index<any>[]>;
62
+ };
63
+ export {};