@zhin.js/database 1.0.2 → 1.0.4
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/CHANGELOG.md +12 -0
- package/lib/base/database.d.ts +7 -7
- package/lib/base/database.d.ts.map +1 -1
- package/lib/base/database.js +5 -5
- package/lib/base/database.js.map +1 -1
- package/lib/base/dialect.d.ts.map +1 -1
- package/lib/base/dialect.js.map +1 -1
- package/lib/base/model.d.ts +2 -2
- package/lib/base/model.d.ts.map +1 -1
- package/lib/base/model.js.map +1 -1
- package/lib/base/query-classes.d.ts +4 -4
- package/lib/base/query-classes.d.ts.map +1 -1
- package/lib/base/query-classes.js +4 -4
- package/lib/base/query-classes.js.map +1 -1
- package/lib/dialects/memory.d.ts +2 -2
- package/lib/dialects/memory.d.ts.map +1 -1
- package/lib/dialects/memory.js +5 -5
- package/lib/dialects/memory.js.map +1 -1
- package/lib/dialects/mongodb.d.ts +2 -2
- package/lib/dialects/mongodb.d.ts.map +1 -1
- package/lib/dialects/mongodb.js +5 -5
- package/lib/dialects/mongodb.js.map +1 -1
- package/lib/dialects/mysql.d.ts +2 -2
- package/lib/dialects/mysql.d.ts.map +1 -1
- package/lib/dialects/mysql.js +5 -5
- package/lib/dialects/mysql.js.map +1 -1
- package/lib/dialects/pg.d.ts +2 -2
- package/lib/dialects/pg.d.ts.map +1 -1
- package/lib/dialects/pg.js +5 -5
- package/lib/dialects/pg.js.map +1 -1
- package/lib/dialects/redis.d.ts +2 -2
- package/lib/dialects/redis.d.ts.map +1 -1
- package/lib/dialects/redis.js +5 -5
- package/lib/dialects/redis.js.map +1 -1
- package/lib/dialects/sqlite.d.ts +2 -2
- package/lib/dialects/sqlite.d.ts.map +1 -1
- package/lib/dialects/sqlite.js +5 -5
- package/lib/dialects/sqlite.js.map +1 -1
- package/lib/registry.d.ts +12 -12
- package/lib/registry.d.ts.map +1 -1
- package/lib/registry.js +2 -2
- package/lib/registry.js.map +1 -1
- package/lib/type/document/database.d.ts +2 -2
- package/lib/type/document/database.d.ts.map +1 -1
- package/lib/type/document/database.js +4 -4
- package/lib/type/document/database.js.map +1 -1
- package/lib/type/document/model.d.ts +1 -1
- package/lib/type/document/model.d.ts.map +1 -1
- package/lib/type/document/model.js +1 -1
- package/lib/type/document/model.js.map +1 -1
- package/lib/type/keyvalue/database.d.ts +2 -2
- package/lib/type/keyvalue/database.d.ts.map +1 -1
- package/lib/type/keyvalue/database.js +4 -4
- package/lib/type/keyvalue/database.js.map +1 -1
- package/lib/type/keyvalue/model.d.ts +1 -1
- package/lib/type/keyvalue/model.d.ts.map +1 -1
- package/lib/type/keyvalue/model.js +1 -1
- package/lib/type/keyvalue/model.js.map +1 -1
- package/lib/type/related/database.d.ts +4 -4
- package/lib/type/related/database.d.ts.map +1 -1
- package/lib/type/related/database.js +7 -7
- package/lib/type/related/database.js.map +1 -1
- package/lib/type/related/model.d.ts +1 -1
- package/lib/type/related/model.d.ts.map +1 -1
- package/lib/type/related/model.js +1 -1
- package/lib/type/related/model.js.map +1 -1
- package/lib/types.d.ts +8 -8
- package/lib/types.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/base/database.ts +7 -7
- package/src/base/dialect.ts +0 -26
- package/src/base/model.ts +2 -2
- package/src/base/query-classes.ts +4 -4
- package/src/dialects/memory.ts +7 -6
- package/src/dialects/mongodb.ts +6 -6
- package/src/dialects/mysql.ts +7 -7
- package/src/dialects/pg.ts +7 -7
- package/src/dialects/redis.ts +7 -7
- package/src/dialects/sqlite.ts +7 -7
- package/src/registry.ts +13 -13
- package/src/type/document/database.ts +4 -4
- package/src/type/document/model.ts +1 -1
- package/src/type/keyvalue/database.ts +4 -4
- package/src/type/keyvalue/model.ts +2 -2
- package/src/type/related/database.ts +13 -13
- package/src/type/related/model.ts +1 -1
- package/src/types.ts +8 -8
package/src/registry.ts
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import { Database } from './base/database.js';
|
|
2
|
-
import type {MongoDBDialectConfig} from './dialects/mongodb';
|
|
3
|
-
import {RelatedDatabase} from "./type/related/database";
|
|
4
|
-
import {MemoryConfig} from "./types";
|
|
5
|
-
import {DocumentDatabase} from "./type/document/database";
|
|
6
|
-
import {MySQLDialectConfig} from "./dialects/mysql";
|
|
7
|
-
import {PostgreSQLDialectConfig} from "./dialects/pg";
|
|
8
|
-
import {RedisDialectConfig} from "./dialects/redis";
|
|
9
|
-
import {SQLiteDialectConfig} from "./dialects/sqlite";
|
|
10
|
-
import {KeyValueDatabase} from "./type/keyvalue/database";
|
|
2
|
+
import type {MongoDBDialectConfig} from './dialects/mongodb.js';
|
|
3
|
+
import {RelatedDatabase} from "./type/related/database.js";
|
|
4
|
+
import {MemoryConfig} from "./types.js";
|
|
5
|
+
import {DocumentDatabase} from "./type/document/database.js";
|
|
6
|
+
import {MySQLDialectConfig} from "./dialects/mysql.js";
|
|
7
|
+
import {PostgreSQLDialectConfig} from "./dialects/pg.js";
|
|
8
|
+
import {RedisDialectConfig} from "./dialects/redis.js";
|
|
9
|
+
import {SQLiteDialectConfig} from "./dialects/sqlite.js";
|
|
10
|
+
import {KeyValueDatabase} from "./type/keyvalue/database.js";
|
|
11
11
|
|
|
12
12
|
/**
|
|
13
13
|
* 数据库注册表接口
|
|
@@ -25,8 +25,8 @@ export interface Databases<S extends Record<string, object> = Record<string, obj
|
|
|
25
25
|
/**
|
|
26
26
|
* 数据库注册表命名空间
|
|
27
27
|
*/
|
|
28
|
-
export type Creator<D, S extends Record<string, object>> = (config: D,
|
|
29
|
-
export type Constructor<D, S extends Record<string, object>> = new (config: D,
|
|
28
|
+
export type Creator<D, S extends Record<string, object>> = (config: D, definitions?: Database.Definitions<S>) => Database<any, S, any>;
|
|
29
|
+
export type Constructor<D, S extends Record<string, object>> = new (config: D, definitions?: Database.Definitions<S>) => Database<any, S, any>;
|
|
30
30
|
export type Factory<D, S extends Record<string, object>> = Creator<any, S> | Constructor<any, S>;
|
|
31
31
|
|
|
32
32
|
export namespace Registry {
|
|
@@ -44,13 +44,13 @@ export namespace Registry {
|
|
|
44
44
|
export function create<D extends string, S extends Record<string, object>>(
|
|
45
45
|
dialect: D,
|
|
46
46
|
config: any,
|
|
47
|
-
|
|
47
|
+
definitions?: Database.Definitions<S>
|
|
48
48
|
): any {
|
|
49
49
|
const factory = factories.get(dialect) as Factory<any, S> | undefined;
|
|
50
50
|
if (!factory) {
|
|
51
51
|
throw new Error(`database dialect ${dialect} not registered`);
|
|
52
52
|
}
|
|
53
|
-
return (isConstructor(factory) ? new factory(config,
|
|
53
|
+
return (isConstructor(factory) ? new factory(config, definitions) : factory(config, definitions)) as any;
|
|
54
54
|
}
|
|
55
55
|
|
|
56
56
|
export function isConstructor<D, S extends Record<string, object>>(fn: Factory<D, S>): fn is Constructor<D, S> {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Database, Dialect } from '../../base';
|
|
1
|
+
import { Database, Dialect } from '../../base/index.js';
|
|
2
2
|
import { DocumentModel } from './model.js';
|
|
3
3
|
import {
|
|
4
4
|
QueryParams,
|
|
@@ -25,9 +25,9 @@ export class DocumentDatabase<
|
|
|
25
25
|
|
|
26
26
|
constructor(
|
|
27
27
|
dialect: Dialect<D,DocumentQueryResult>,
|
|
28
|
-
|
|
28
|
+
definitions?: Database.Definitions<S>,
|
|
29
29
|
) {
|
|
30
|
-
super(dialect,
|
|
30
|
+
super(dialect, definitions);
|
|
31
31
|
}
|
|
32
32
|
|
|
33
33
|
protected async initialize(): Promise<void> {
|
|
@@ -277,7 +277,7 @@ export class DocumentDatabase<
|
|
|
277
277
|
* 获取所有模型名称
|
|
278
278
|
*/
|
|
279
279
|
getModelNames(): string[] {
|
|
280
|
-
return Object.keys(this.
|
|
280
|
+
return Object.keys(this.definitions || {});
|
|
281
281
|
}
|
|
282
282
|
}
|
|
283
283
|
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Database, Dialect } from '../../base';
|
|
1
|
+
import { Database, Dialect } from '../../base/index.js';
|
|
2
2
|
import { KeyValueModel } from './model.js';
|
|
3
3
|
import {
|
|
4
4
|
QueryParams,
|
|
@@ -26,9 +26,9 @@ export class KeyValueDatabase<
|
|
|
26
26
|
|
|
27
27
|
constructor(
|
|
28
28
|
dialect: Dialect<D,KeyValueQueryResult>,
|
|
29
|
-
|
|
29
|
+
definitions?: Database.Definitions<S>,
|
|
30
30
|
) {
|
|
31
|
-
super(dialect,
|
|
31
|
+
super(dialect, definitions);
|
|
32
32
|
}
|
|
33
33
|
|
|
34
34
|
protected async initialize(): Promise<void> {
|
|
@@ -255,7 +255,7 @@ export class KeyValueDatabase<
|
|
|
255
255
|
* 获取所有模型名称
|
|
256
256
|
*/
|
|
257
257
|
getModelNames(): string[] {
|
|
258
|
-
return Object.keys(this.
|
|
258
|
+
return Object.keys(this.definitions || {});
|
|
259
259
|
}
|
|
260
260
|
}
|
|
261
261
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { Model,
|
|
1
|
+
import { Model, } from '../../base/index.js';
|
|
2
2
|
import { KeyValueDatabase } from './database.js';
|
|
3
|
-
import {
|
|
3
|
+
import { KeyValueQueryResult } from '../../types.js';
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* 键值模型类
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Database,Dialect } from '../../base';
|
|
1
|
+
import { Database,Dialect } from '../../base/index.js';
|
|
2
2
|
import { RelatedModel } from './model.js';
|
|
3
3
|
import {
|
|
4
4
|
QueryParams,
|
|
@@ -13,9 +13,9 @@ import {
|
|
|
13
13
|
DropIndexQueryParams,
|
|
14
14
|
Condition,
|
|
15
15
|
Column,
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
16
|
+
AddDefinition,
|
|
17
|
+
ModifyDefinition,
|
|
18
|
+
DropDefinition,
|
|
19
19
|
isCreateQuery,
|
|
20
20
|
isSelectQuery,
|
|
21
21
|
isInsertQuery,
|
|
@@ -37,15 +37,15 @@ export class RelatedDatabase<
|
|
|
37
37
|
|
|
38
38
|
constructor(
|
|
39
39
|
dialect: Dialect<D,string>,
|
|
40
|
-
|
|
40
|
+
definitions?: Database.Definitions<S>,
|
|
41
41
|
) {
|
|
42
|
-
super(dialect,
|
|
42
|
+
super(dialect,definitions);
|
|
43
43
|
}
|
|
44
44
|
|
|
45
45
|
protected async initialize(): Promise<void> {
|
|
46
46
|
// 自动创建表
|
|
47
|
-
for (const [tableName,
|
|
48
|
-
await this.create(tableName,
|
|
47
|
+
for (const [tableName, definition] of Object.entries(this.definitions || {})) {
|
|
48
|
+
await this.create(tableName, definition);
|
|
49
49
|
}
|
|
50
50
|
}
|
|
51
51
|
|
|
@@ -77,7 +77,7 @@ export class RelatedDatabase<
|
|
|
77
77
|
// ========================================================================
|
|
78
78
|
|
|
79
79
|
protected buildCreateQuery<T extends object>(params: CreateQueryParams<T>): BuildQueryResult<string> {
|
|
80
|
-
const columnDefs = Object.entries(params.
|
|
80
|
+
const columnDefs = Object.entries(params.definition).map(([field, column]) => this.formatColumnDefinition(field,column as Column));
|
|
81
81
|
const query = this.dialect.formatCreateTable(params.tableName, columnDefs);
|
|
82
82
|
return { query, params: [] };
|
|
83
83
|
}
|
|
@@ -194,7 +194,7 @@ export class RelatedDatabase<
|
|
|
194
194
|
// ========================================================================
|
|
195
195
|
|
|
196
196
|
protected buildAlterQuery<T extends object>(params: AlterQueryParams<T>): BuildQueryResult<string> {
|
|
197
|
-
const alterations = Object.entries(params.alterations).map(([field,alteration]) => this.formatAlteration(field, alteration as
|
|
197
|
+
const alterations = Object.entries(params.alterations).map(([field,alteration]) => this.formatAlteration(field, alteration as AddDefinition<T> | ModifyDefinition<T> | DropDefinition));
|
|
198
198
|
const query = this.dialect.formatAlterTable(params.tableName, alterations);
|
|
199
199
|
return { query, params: [] };
|
|
200
200
|
}
|
|
@@ -234,8 +234,8 @@ export class RelatedDatabase<
|
|
|
234
234
|
|
|
235
235
|
return `${name} ${type}${length}${primary}${unique}${nullable}${defaultVal}`;
|
|
236
236
|
}
|
|
237
|
-
|
|
238
|
-
protected formatAlteration<T=any>(field:string,alteration:
|
|
237
|
+
|
|
238
|
+
protected formatAlteration<T=any>(field:string,alteration: AddDefinition<T> | ModifyDefinition<T> | DropDefinition): string {
|
|
239
239
|
const name = this.dialect.quoteIdentifier(field);
|
|
240
240
|
|
|
241
241
|
switch (alteration.action) {
|
|
@@ -387,6 +387,6 @@ export class RelatedDatabase<
|
|
|
387
387
|
* 获取所有模型名称
|
|
388
388
|
*/
|
|
389
389
|
getModelNames(): string[] {
|
|
390
|
-
return Object.keys(this.
|
|
390
|
+
return Object.keys(this.definitions || {});
|
|
391
391
|
}
|
|
392
392
|
}
|
package/src/types.ts
CHANGED
|
@@ -31,7 +31,7 @@ export interface Column<T = any> {
|
|
|
31
31
|
length?: number;
|
|
32
32
|
}
|
|
33
33
|
|
|
34
|
-
export type
|
|
34
|
+
export type Definition<T extends object=object> = {
|
|
35
35
|
[P in keyof T]: Column<T[P]>;
|
|
36
36
|
}
|
|
37
37
|
|
|
@@ -39,7 +39,7 @@ export type Schema<T extends object=object> = {
|
|
|
39
39
|
// Column Alteration Types
|
|
40
40
|
// ============================================================================
|
|
41
41
|
|
|
42
|
-
export interface
|
|
42
|
+
export interface AddDefinition<T = any> {
|
|
43
43
|
action: "add";
|
|
44
44
|
type: ColumnType;
|
|
45
45
|
nullable?: boolean;
|
|
@@ -48,7 +48,7 @@ export interface AddSchema<T = any> {
|
|
|
48
48
|
length?: number;
|
|
49
49
|
}
|
|
50
50
|
|
|
51
|
-
export interface
|
|
51
|
+
export interface ModifyDefinition<T = any> {
|
|
52
52
|
action: "modify";
|
|
53
53
|
type?: ColumnType;
|
|
54
54
|
nullable?: boolean;
|
|
@@ -56,12 +56,12 @@ export interface ModifySchema<T = any> {
|
|
|
56
56
|
length?: number;
|
|
57
57
|
}
|
|
58
58
|
|
|
59
|
-
export interface
|
|
59
|
+
export interface DropDefinition {
|
|
60
60
|
action: "drop";
|
|
61
61
|
}
|
|
62
62
|
|
|
63
|
-
export type
|
|
64
|
-
[P in keyof T]?:
|
|
63
|
+
export type AlterDefinition<T extends object> = {
|
|
64
|
+
[P in keyof T]?: AddDefinition<T[P]> | ModifyDefinition<T[P]> | DropDefinition
|
|
65
65
|
};
|
|
66
66
|
|
|
67
67
|
// ============================================================================
|
|
@@ -112,12 +112,12 @@ export interface BaseQueryParams {
|
|
|
112
112
|
|
|
113
113
|
export interface CreateQueryParams<T extends object = any> extends BaseQueryParams {
|
|
114
114
|
type: 'create';
|
|
115
|
-
|
|
115
|
+
definition: Definition<T>;
|
|
116
116
|
}
|
|
117
117
|
|
|
118
118
|
export interface AlterQueryParams<T extends object = any> extends BaseQueryParams {
|
|
119
119
|
type: 'alter';
|
|
120
|
-
alterations:
|
|
120
|
+
alterations: AlterDefinition<T>;
|
|
121
121
|
}
|
|
122
122
|
|
|
123
123
|
export interface DropTableQueryParams<T extends object = any> extends BaseQueryParams {
|