@zhin.js/database 1.0.4 → 1.0.6
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/README.md +1360 -34
- package/lib/base/database.d.ts +71 -13
- package/lib/base/database.d.ts.map +1 -1
- package/lib/base/database.js +128 -4
- package/lib/base/database.js.map +1 -1
- package/lib/base/dialect.d.ts +27 -10
- package/lib/base/dialect.d.ts.map +1 -1
- package/lib/base/dialect.js +32 -0
- package/lib/base/dialect.js.map +1 -1
- package/lib/base/index.d.ts +1 -0
- package/lib/base/index.d.ts.map +1 -1
- package/lib/base/index.js +1 -0
- package/lib/base/index.js.map +1 -1
- package/lib/base/model.d.ts +105 -12
- package/lib/base/model.d.ts.map +1 -1
- package/lib/base/model.js +224 -3
- package/lib/base/model.js.map +1 -1
- package/lib/base/query-classes.d.ts +204 -33
- package/lib/base/query-classes.d.ts.map +1 -1
- package/lib/base/query-classes.js +276 -0
- package/lib/base/query-classes.js.map +1 -1
- package/lib/base/thenable.d.ts +7 -7
- package/lib/base/thenable.d.ts.map +1 -1
- package/lib/base/thenable.js +5 -4
- package/lib/base/thenable.js.map +1 -1
- package/lib/base/transaction.d.ts +46 -0
- package/lib/base/transaction.d.ts.map +1 -0
- package/lib/base/transaction.js +186 -0
- package/lib/base/transaction.js.map +1 -0
- package/lib/dialects/memory.d.ts +12 -7
- package/lib/dialects/memory.d.ts.map +1 -1
- package/lib/dialects/memory.js +7 -4
- package/lib/dialects/memory.js.map +1 -1
- package/lib/dialects/mongodb.d.ts +11 -7
- package/lib/dialects/mongodb.d.ts.map +1 -1
- package/lib/dialects/mongodb.js +18 -15
- package/lib/dialects/mongodb.js.map +1 -1
- package/lib/dialects/mysql.d.ts +35 -6
- package/lib/dialects/mysql.d.ts.map +1 -1
- package/lib/dialects/mysql.js +137 -18
- package/lib/dialects/mysql.js.map +1 -1
- package/lib/dialects/pg.d.ts +35 -6
- package/lib/dialects/pg.d.ts.map +1 -1
- package/lib/dialects/pg.js +137 -18
- package/lib/dialects/pg.js.map +1 -1
- package/lib/dialects/redis.d.ts +11 -6
- package/lib/dialects/redis.d.ts.map +1 -1
- package/lib/dialects/redis.js +11 -8
- package/lib/dialects/redis.js.map +1 -1
- package/lib/dialects/sqlite.d.ts +19 -6
- package/lib/dialects/sqlite.d.ts.map +1 -1
- package/lib/dialects/sqlite.js +63 -10
- package/lib/dialects/sqlite.js.map +1 -1
- package/lib/index.d.ts +1 -0
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +1 -0
- package/lib/index.js.map +1 -1
- package/lib/migration.d.ts +132 -0
- package/lib/migration.d.ts.map +1 -0
- package/lib/migration.js +475 -0
- package/lib/migration.js.map +1 -0
- package/lib/registry.d.ts +26 -23
- package/lib/registry.d.ts.map +1 -1
- package/lib/registry.js +1 -5
- package/lib/registry.js.map +1 -1
- package/lib/type/document/database.d.ts +11 -11
- package/lib/type/document/database.d.ts.map +1 -1
- package/lib/type/document/database.js.map +1 -1
- package/lib/type/document/model.d.ts +7 -7
- package/lib/type/document/model.d.ts.map +1 -1
- package/lib/type/document/model.js.map +1 -1
- package/lib/type/keyvalue/database.d.ts +11 -11
- package/lib/type/keyvalue/database.d.ts.map +1 -1
- package/lib/type/keyvalue/database.js.map +1 -1
- package/lib/type/keyvalue/model.d.ts +2 -2
- package/lib/type/keyvalue/model.d.ts.map +1 -1
- package/lib/type/keyvalue/model.js.map +1 -1
- package/lib/type/related/database.d.ts +48 -13
- package/lib/type/related/database.d.ts.map +1 -1
- package/lib/type/related/database.js +258 -27
- package/lib/type/related/database.js.map +1 -1
- package/lib/type/related/model.d.ts +251 -15
- package/lib/type/related/model.d.ts.map +1 -1
- package/lib/type/related/model.js +647 -22
- package/lib/type/related/model.js.map +1 -1
- package/lib/types.d.ts +475 -37
- package/lib/types.d.ts.map +1 -1
- package/lib/types.js +6 -0
- package/lib/types.js.map +1 -1
- package/package.json +14 -5
- package/src/base/database.ts +168 -24
- package/src/base/dialect.ts +49 -10
- package/src/base/index.ts +2 -1
- package/src/base/model.ts +258 -18
- package/src/base/query-classes.ts +471 -63
- package/src/base/thenable.ts +12 -11
- package/src/base/transaction.ts +213 -0
- package/src/dialects/memory.ts +14 -13
- package/src/dialects/mongodb.ts +40 -38
- package/src/dialects/mysql.ts +151 -22
- package/src/dialects/pg.ts +148 -21
- package/src/dialects/redis.ts +40 -38
- package/src/dialects/sqlite.ts +73 -15
- package/src/index.ts +1 -2
- package/src/migration.ts +544 -0
- package/src/registry.ts +33 -33
- package/src/type/document/database.ts +32 -32
- package/src/type/document/model.ts +14 -14
- package/src/type/keyvalue/database.ts +32 -32
- package/src/type/keyvalue/model.ts +18 -18
- package/src/type/related/database.ts +309 -34
- package/src/type/related/model.ts +800 -33
- package/src/types.ts +559 -44
- package/tests/database.test.ts +1738 -0
package/src/registry.ts
CHANGED
|
@@ -1,59 +1,59 @@
|
|
|
1
1
|
import { Database } from './base/database.js';
|
|
2
|
-
import
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
import {RedisDialectConfig} from "./dialects/redis.js";
|
|
9
|
-
import {SQLiteDialectConfig} from "./dialects/sqlite.js";
|
|
10
|
-
import {KeyValueDatabase} from "./type/keyvalue/database.js";
|
|
2
|
+
import { Memory } from './dialects/memory.js';
|
|
3
|
+
import { MongoDB } from './dialects/mongodb.js';
|
|
4
|
+
import {MySQL} from "./dialects/mysql.js";
|
|
5
|
+
import {PG} from "./dialects/pg.js";
|
|
6
|
+
import {Redis} from "./dialects/redis.js";
|
|
7
|
+
import {Sqlite} from "./dialects/sqlite.js";
|
|
11
8
|
|
|
12
9
|
/**
|
|
13
10
|
* 数据库注册表接口
|
|
14
11
|
* 支持模块声明扩展
|
|
15
12
|
*/
|
|
16
13
|
export interface Databases<S extends Record<string, object> = Record<string, object>> {
|
|
17
|
-
memory:
|
|
18
|
-
mongodb:
|
|
19
|
-
mysql:
|
|
20
|
-
pg:
|
|
21
|
-
redis:
|
|
22
|
-
sqlite:
|
|
14
|
+
memory: Memory<S>;
|
|
15
|
+
mongodb: MongoDB<S>;
|
|
16
|
+
mysql: MySQL<S>;
|
|
17
|
+
pg: PG<S>;
|
|
18
|
+
redis: Redis<S>;
|
|
19
|
+
sqlite: Sqlite<S>;
|
|
23
20
|
}
|
|
24
21
|
|
|
25
22
|
/**
|
|
26
23
|
* 数据库注册表命名空间
|
|
27
24
|
*/
|
|
28
|
-
export type
|
|
29
|
-
|
|
30
|
-
|
|
25
|
+
export type Factory<D, S extends Record<string, object>,R extends Database<D, S, any>> ={
|
|
26
|
+
new (config: D, definitions?: Database.DefinitionObj<S>): R;
|
|
27
|
+
}
|
|
31
28
|
|
|
32
29
|
export namespace Registry {
|
|
33
|
-
export const factories=new Map();
|
|
34
|
-
export
|
|
30
|
+
export const factories=new Map<string, Factory<any, any,any>>();
|
|
31
|
+
export interface Config{
|
|
32
|
+
memory:import('./dialects/memory.js').MemoryConfig
|
|
33
|
+
mongodb:import('./dialects/mongodb.js').MongoDBDialectConfig
|
|
34
|
+
mysql:import('./dialects/mysql.js').MySQLDialectConfig
|
|
35
|
+
pg:import('./dialects/pg.js').PostgreSQLDialectConfig
|
|
36
|
+
redis:import('./dialects/redis.js').RedisDialectConfig
|
|
37
|
+
sqlite:import('./dialects/sqlite.js').SQLiteDialectConfig
|
|
38
|
+
}
|
|
35
39
|
export type DatabaseType = 'related' | 'document' | 'keyvalue';
|
|
36
40
|
|
|
37
|
-
export function register<
|
|
41
|
+
export function register<S extends Record<string, object>,D extends keyof Databases<S>>(
|
|
38
42
|
dialect: D,
|
|
39
|
-
factory: Factory<
|
|
43
|
+
factory: Factory<Config[D], S,any>
|
|
40
44
|
): void {
|
|
41
|
-
factories.set(dialect, factory as Factory<
|
|
45
|
+
factories.set(dialect, factory as Factory<Config[D], S,any>);
|
|
42
46
|
}
|
|
43
47
|
|
|
44
|
-
export function create<
|
|
48
|
+
export function create<S extends Record<string, object>, D extends keyof Databases<S>>(
|
|
45
49
|
dialect: D,
|
|
46
|
-
config:
|
|
47
|
-
definitions?: Database.
|
|
48
|
-
):
|
|
49
|
-
const factory = factories.get(dialect)
|
|
50
|
+
config: Config[D],
|
|
51
|
+
definitions?: Database.DefinitionObj<S>
|
|
52
|
+
): Databases<S>[D] {
|
|
53
|
+
const factory = factories.get(dialect);
|
|
50
54
|
if (!factory) {
|
|
51
55
|
throw new Error(`database dialect ${dialect} not registered`);
|
|
52
56
|
}
|
|
53
|
-
return
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
export function isConstructor<D, S extends Record<string, object>>(fn: Factory<D, S>): fn is Constructor<D, S> {
|
|
57
|
-
return fn.prototype && fn.prototype.constructor === fn;
|
|
57
|
+
return new factory(config, definitions) as Databases<S>[D];
|
|
58
58
|
}
|
|
59
59
|
}
|
|
@@ -24,8 +24,8 @@ export class DocumentDatabase<
|
|
|
24
24
|
> extends Database<D, S, DocumentQueryResult> {
|
|
25
25
|
|
|
26
26
|
constructor(
|
|
27
|
-
dialect: Dialect<D,DocumentQueryResult>,
|
|
28
|
-
definitions?: Database.
|
|
27
|
+
dialect: Dialect<D, S, DocumentQueryResult>,
|
|
28
|
+
definitions?: Database.DefinitionObj<S>,
|
|
29
29
|
) {
|
|
30
30
|
super(dialect, definitions);
|
|
31
31
|
}
|
|
@@ -38,24 +38,24 @@ export class DocumentDatabase<
|
|
|
38
38
|
/**
|
|
39
39
|
* 构建查询(重写基类方法)
|
|
40
40
|
*/
|
|
41
|
-
buildQuery<
|
|
41
|
+
buildQuery<T extends keyof S>(params: QueryParams<S, T>): BuildQueryResult<DocumentQueryResult> {
|
|
42
42
|
switch (params.type) {
|
|
43
43
|
case 'create':
|
|
44
|
-
return this.buildCreateQuery(params as CreateQueryParams<
|
|
44
|
+
return this.buildCreateQuery(params as CreateQueryParams<S, T>);
|
|
45
45
|
case 'select':
|
|
46
|
-
return this.buildSelectQuery(params as SelectQueryParams<
|
|
46
|
+
return this.buildSelectQuery(params as SelectQueryParams<S, T>);
|
|
47
47
|
case 'insert':
|
|
48
|
-
return this.buildInsertQuery(params as InsertQueryParams<
|
|
48
|
+
return this.buildInsertQuery(params as InsertQueryParams<S, T>);
|
|
49
49
|
case 'update':
|
|
50
|
-
return this.buildUpdateQuery(params as UpdateQueryParams<
|
|
50
|
+
return this.buildUpdateQuery(params as UpdateQueryParams<S, T>);
|
|
51
51
|
case 'delete':
|
|
52
|
-
return this.buildDeleteQuery(params as DeleteQueryParams<
|
|
52
|
+
return this.buildDeleteQuery(params as DeleteQueryParams<S, T>);
|
|
53
53
|
case 'alter':
|
|
54
|
-
return this.buildAlterQuery(params as AlterQueryParams<
|
|
54
|
+
return this.buildAlterQuery(params as AlterQueryParams<S, T>);
|
|
55
55
|
case 'drop_table':
|
|
56
|
-
return this.buildDropTableQuery(params as DropTableQueryParams<
|
|
56
|
+
return this.buildDropTableQuery(params as DropTableQueryParams<S, T>);
|
|
57
57
|
case 'drop_index':
|
|
58
|
-
return this.buildDropIndexQuery(params as DropIndexQueryParams);
|
|
58
|
+
return this.buildDropIndexQuery(params as DropIndexQueryParams<S, T>);
|
|
59
59
|
default:
|
|
60
60
|
throw new Error(`Unsupported query type: ${(params as any).type}`);
|
|
61
61
|
}
|
|
@@ -64,10 +64,10 @@ export class DocumentDatabase<
|
|
|
64
64
|
/**
|
|
65
65
|
* 构建创建集合查询
|
|
66
66
|
*/
|
|
67
|
-
protected buildCreateQuery<T extends
|
|
67
|
+
protected buildCreateQuery<T extends keyof S>(params: CreateQueryParams<S, T>): BuildQueryResult<DocumentQueryResult> {
|
|
68
68
|
return {
|
|
69
69
|
query: {
|
|
70
|
-
collection: params.tableName,
|
|
70
|
+
collection: params.tableName as string,
|
|
71
71
|
filter: {},
|
|
72
72
|
projection: {}
|
|
73
73
|
},
|
|
@@ -78,7 +78,7 @@ export class DocumentDatabase<
|
|
|
78
78
|
/**
|
|
79
79
|
* 构建查询文档查询
|
|
80
80
|
*/
|
|
81
|
-
protected buildSelectQuery<T extends
|
|
81
|
+
protected buildSelectQuery<T extends keyof S>(params: SelectQueryParams<S, T>): BuildQueryResult<DocumentQueryResult> {
|
|
82
82
|
const filter: Record<string, any> = {};
|
|
83
83
|
|
|
84
84
|
// 转换条件为文档查询格式
|
|
@@ -87,7 +87,7 @@ export class DocumentDatabase<
|
|
|
87
87
|
}
|
|
88
88
|
|
|
89
89
|
const query: DocumentQueryResult = {
|
|
90
|
-
collection: params.tableName,
|
|
90
|
+
collection: params.tableName as string,
|
|
91
91
|
filter
|
|
92
92
|
};
|
|
93
93
|
|
|
@@ -122,10 +122,10 @@ export class DocumentDatabase<
|
|
|
122
122
|
/**
|
|
123
123
|
* 构建插入文档查询
|
|
124
124
|
*/
|
|
125
|
-
protected buildInsertQuery<T extends
|
|
125
|
+
protected buildInsertQuery<T extends keyof S>(params: InsertQueryParams<S, T>): BuildQueryResult<DocumentQueryResult> {
|
|
126
126
|
return {
|
|
127
127
|
query: {
|
|
128
|
-
collection: params.tableName,
|
|
128
|
+
collection: params.tableName as string,
|
|
129
129
|
filter: {},
|
|
130
130
|
projection: {}
|
|
131
131
|
},
|
|
@@ -136,7 +136,7 @@ export class DocumentDatabase<
|
|
|
136
136
|
/**
|
|
137
137
|
* 构建更新文档查询
|
|
138
138
|
*/
|
|
139
|
-
protected buildUpdateQuery<T extends
|
|
139
|
+
protected buildUpdateQuery<T extends keyof S>(params: UpdateQueryParams<S, T>): BuildQueryResult<DocumentQueryResult> {
|
|
140
140
|
const filter: Record<string, any> = {};
|
|
141
141
|
|
|
142
142
|
if (params.conditions) {
|
|
@@ -145,7 +145,7 @@ export class DocumentDatabase<
|
|
|
145
145
|
|
|
146
146
|
return {
|
|
147
147
|
query: {
|
|
148
|
-
collection: params.tableName,
|
|
148
|
+
collection: params.tableName as string,
|
|
149
149
|
filter,
|
|
150
150
|
projection: {}
|
|
151
151
|
},
|
|
@@ -156,7 +156,7 @@ export class DocumentDatabase<
|
|
|
156
156
|
/**
|
|
157
157
|
* 构建删除文档查询
|
|
158
158
|
*/
|
|
159
|
-
protected buildDeleteQuery<T extends
|
|
159
|
+
protected buildDeleteQuery<T extends keyof S>(params: DeleteQueryParams<S, T>): BuildQueryResult<DocumentQueryResult> {
|
|
160
160
|
const filter: Record<string, any> = {};
|
|
161
161
|
|
|
162
162
|
if (params.conditions) {
|
|
@@ -165,7 +165,7 @@ export class DocumentDatabase<
|
|
|
165
165
|
|
|
166
166
|
return {
|
|
167
167
|
query: {
|
|
168
|
-
collection: params.tableName,
|
|
168
|
+
collection: params.tableName as string,
|
|
169
169
|
filter,
|
|
170
170
|
projection: {}
|
|
171
171
|
},
|
|
@@ -176,10 +176,10 @@ export class DocumentDatabase<
|
|
|
176
176
|
/**
|
|
177
177
|
* 构建修改集合查询
|
|
178
178
|
*/
|
|
179
|
-
protected buildAlterQuery<T extends
|
|
179
|
+
protected buildAlterQuery<T extends keyof S>(params: AlterQueryParams<S, T>): BuildQueryResult<DocumentQueryResult> {
|
|
180
180
|
return {
|
|
181
181
|
query: {
|
|
182
|
-
collection: params.tableName,
|
|
182
|
+
collection: params.tableName as string,
|
|
183
183
|
filter: {},
|
|
184
184
|
projection: {}
|
|
185
185
|
},
|
|
@@ -190,10 +190,10 @@ export class DocumentDatabase<
|
|
|
190
190
|
/**
|
|
191
191
|
* 构建删除集合查询
|
|
192
192
|
*/
|
|
193
|
-
protected buildDropTableQuery<T extends
|
|
193
|
+
protected buildDropTableQuery<T extends keyof S>(params: DropTableQueryParams<S, T>): BuildQueryResult<DocumentQueryResult> {
|
|
194
194
|
return {
|
|
195
195
|
query: {
|
|
196
|
-
collection: params.tableName,
|
|
196
|
+
collection: params.tableName as string,
|
|
197
197
|
filter: {},
|
|
198
198
|
projection: {}
|
|
199
199
|
},
|
|
@@ -204,10 +204,10 @@ export class DocumentDatabase<
|
|
|
204
204
|
/**
|
|
205
205
|
* 构建删除索引查询
|
|
206
206
|
*/
|
|
207
|
-
protected buildDropIndexQuery(params: DropIndexQueryParams): BuildQueryResult<DocumentQueryResult> {
|
|
207
|
+
protected buildDropIndexQuery<T extends keyof S>(params: DropIndexQueryParams<S, T>): BuildQueryResult<DocumentQueryResult> {
|
|
208
208
|
return {
|
|
209
209
|
query: {
|
|
210
|
-
collection: params.tableName,
|
|
210
|
+
collection: params.tableName as string,
|
|
211
211
|
filter: {},
|
|
212
212
|
projection: {}
|
|
213
213
|
},
|
|
@@ -264,13 +264,13 @@ export class DocumentDatabase<
|
|
|
264
264
|
/**
|
|
265
265
|
* 获取模型
|
|
266
266
|
*/
|
|
267
|
-
model<T extends keyof S>(name: T): DocumentModel<S
|
|
268
|
-
let model = this.models.get(name as
|
|
267
|
+
model<T extends keyof S>(name: T): DocumentModel<D, S, T> {
|
|
268
|
+
let model = this.models.get(name) as DocumentModel<D, S, T> | undefined;
|
|
269
269
|
if (!model) {
|
|
270
|
-
model = new DocumentModel
|
|
271
|
-
this.models.set(name as
|
|
270
|
+
model = new DocumentModel<D, S, T>(this, name);
|
|
271
|
+
this.models.set(name, model as any);
|
|
272
272
|
}
|
|
273
|
-
return model as
|
|
273
|
+
return model as DocumentModel<D, S, T>;
|
|
274
274
|
}
|
|
275
275
|
|
|
276
276
|
/**
|
|
@@ -6,10 +6,10 @@ import { DocumentQueryResult, Condition } from '../../types.js';
|
|
|
6
6
|
* 文档型模型类
|
|
7
7
|
* 继承自 Model,提供文档型数据库特有的操作
|
|
8
8
|
*/
|
|
9
|
-
export class DocumentModel<
|
|
9
|
+
export class DocumentModel<D=any, S extends Record<string, object> = Record<string, object>, T extends keyof S = keyof S> extends Model<D, S, DocumentQueryResult, T> {
|
|
10
10
|
constructor(
|
|
11
|
-
database: DocumentDatabase<D>,
|
|
12
|
-
name:
|
|
11
|
+
database: DocumentDatabase<D, S>,
|
|
12
|
+
name: T
|
|
13
13
|
) {
|
|
14
14
|
super(database, name);
|
|
15
15
|
}
|
|
@@ -17,9 +17,9 @@ export class DocumentModel<T extends object = object, D =any> extends Model<D, T
|
|
|
17
17
|
/**
|
|
18
18
|
* 创建文档
|
|
19
19
|
*/
|
|
20
|
-
async create(document: T): Promise<T & { _id: string }>;
|
|
21
|
-
async create(documents: T[]): Promise<(T & { _id: string })[]>;
|
|
22
|
-
async create(documents: T | T[]): Promise<(T & { _id: string }) | (T & { _id: string })[]> {
|
|
20
|
+
async create(document: S[T]): Promise<S[T] & { _id: string }>;
|
|
21
|
+
async create(documents: S[T][]): Promise<(S[T] & { _id: string })[]>;
|
|
22
|
+
async create(documents: S[T] | S[T][]): Promise<(S[T] & { _id: string }) | (S[T] & { _id: string })[]> {
|
|
23
23
|
const isArray = Array.isArray(documents);
|
|
24
24
|
const docs = isArray ? documents : [documents];
|
|
25
25
|
|
|
@@ -33,7 +33,7 @@ export class DocumentModel<T extends object = object, D =any> extends Model<D, T
|
|
|
33
33
|
operation: 'insertOne',
|
|
34
34
|
filter: {},
|
|
35
35
|
projection: {},
|
|
36
|
-
collection: this.name,
|
|
36
|
+
collection: this.name as string,
|
|
37
37
|
},
|
|
38
38
|
[docWithId]
|
|
39
39
|
);
|
|
@@ -46,9 +46,9 @@ export class DocumentModel<T extends object = object, D =any> extends Model<D, T
|
|
|
46
46
|
/**
|
|
47
47
|
* 查找单个文档
|
|
48
48
|
*/
|
|
49
|
-
async selectOne<K extends keyof T>(...fields: Array<K>): Promise<(Pick<T, K> & { _id: string }) | null> {
|
|
49
|
+
async selectOne<K extends keyof S[T]>(...fields: Array<K>): Promise<(Pick<S[T], K> & { _id: string }) | null> {
|
|
50
50
|
const results = await this.select(...fields).limit(1);
|
|
51
|
-
return results.length > 0 ? results[0] as (Pick<T, K> & { _id: string }) : null;
|
|
51
|
+
return results.length > 0 ? results[0] as (Pick<S[T], K> & { _id: string }) : null;
|
|
52
52
|
}
|
|
53
53
|
|
|
54
54
|
/**
|
|
@@ -57,24 +57,24 @@ export class DocumentModel<T extends object = object, D =any> extends Model<D, T
|
|
|
57
57
|
async selectById(id: string){
|
|
58
58
|
return this.select('_id' as any).where({
|
|
59
59
|
_id: id,
|
|
60
|
-
}).limit(1).then((results: any) => results[0] || null);
|
|
60
|
+
} as Condition<S[T]>).limit(1).then((results: any) => results[0] || null);
|
|
61
61
|
}
|
|
62
62
|
/**
|
|
63
63
|
* 根据ID更新文档
|
|
64
64
|
*/
|
|
65
|
-
async updateById(id: string, update: Partial<T>): Promise<number> {
|
|
65
|
+
async updateById(id: string, update: Partial<S[T]>): Promise<number> {
|
|
66
66
|
return this.update(update).where({
|
|
67
67
|
_id: id,
|
|
68
|
-
} as Condition<T>)
|
|
68
|
+
} as Condition<S[T]>)
|
|
69
69
|
}
|
|
70
70
|
|
|
71
71
|
/**
|
|
72
72
|
* 根据ID删除文档
|
|
73
73
|
*/
|
|
74
|
-
async deleteById(id: string): Promise<number> {
|
|
74
|
+
async deleteById(id: string): Promise<S[T][] | number> {
|
|
75
75
|
return this.delete({
|
|
76
76
|
_id: id,
|
|
77
|
-
} as Condition<T>)
|
|
77
|
+
} as Condition<S[T]>) as Promise<S[T][] | number>;
|
|
78
78
|
}
|
|
79
79
|
|
|
80
80
|
/**
|
|
@@ -25,8 +25,8 @@ export class KeyValueDatabase<
|
|
|
25
25
|
> extends Database<D, S, KeyValueQueryResult> {
|
|
26
26
|
|
|
27
27
|
constructor(
|
|
28
|
-
dialect: Dialect<D,KeyValueQueryResult>,
|
|
29
|
-
definitions?: Database.
|
|
28
|
+
dialect: Dialect<D, S, KeyValueQueryResult>,
|
|
29
|
+
definitions?: Database.DefinitionObj<S>,
|
|
30
30
|
) {
|
|
31
31
|
super(dialect, definitions);
|
|
32
32
|
}
|
|
@@ -39,24 +39,24 @@ export class KeyValueDatabase<
|
|
|
39
39
|
/**
|
|
40
40
|
* 构建查询(重写基类方法)
|
|
41
41
|
*/
|
|
42
|
-
buildQuery<
|
|
42
|
+
buildQuery<T extends keyof S>(params: QueryParams<S, T>): BuildQueryResult<KeyValueQueryResult> {
|
|
43
43
|
switch (params.type) {
|
|
44
44
|
case 'create':
|
|
45
|
-
return this.buildCreateQuery(params as CreateQueryParams<
|
|
45
|
+
return this.buildCreateQuery(params as CreateQueryParams<S, T>);
|
|
46
46
|
case 'select':
|
|
47
|
-
return this.buildSelectQuery(params as SelectQueryParams<
|
|
47
|
+
return this.buildSelectQuery(params as SelectQueryParams<S, T>);
|
|
48
48
|
case 'insert':
|
|
49
|
-
return this.buildInsertQuery(params as InsertQueryParams<
|
|
49
|
+
return this.buildInsertQuery(params as InsertQueryParams<S, T>);
|
|
50
50
|
case 'update':
|
|
51
|
-
return this.buildUpdateQuery(params as UpdateQueryParams<
|
|
51
|
+
return this.buildUpdateQuery(params as UpdateQueryParams<S, T>);
|
|
52
52
|
case 'delete':
|
|
53
|
-
return this.buildDeleteQuery(params as DeleteQueryParams<
|
|
53
|
+
return this.buildDeleteQuery(params as DeleteQueryParams<S, T>);
|
|
54
54
|
case 'alter':
|
|
55
|
-
return this.buildAlterQuery(params as AlterQueryParams<
|
|
55
|
+
return this.buildAlterQuery(params as AlterQueryParams<S, T>);
|
|
56
56
|
case 'drop_table':
|
|
57
|
-
return this.buildDropTableQuery(params as DropTableQueryParams<
|
|
57
|
+
return this.buildDropTableQuery(params as DropTableQueryParams<S, T>);
|
|
58
58
|
case 'drop_index':
|
|
59
|
-
return this.buildDropIndexQuery(params as DropIndexQueryParams);
|
|
59
|
+
return this.buildDropIndexQuery(params as DropIndexQueryParams<S, T>);
|
|
60
60
|
default:
|
|
61
61
|
throw new Error(`Unsupported query type: ${(params as any).type}`);
|
|
62
62
|
}
|
|
@@ -65,10 +65,10 @@ export class KeyValueDatabase<
|
|
|
65
65
|
/**
|
|
66
66
|
* 构建创建桶查询
|
|
67
67
|
*/
|
|
68
|
-
protected buildCreateQuery<T extends
|
|
68
|
+
protected buildCreateQuery<T extends keyof S>(params: CreateQueryParams<S, T>): BuildQueryResult<KeyValueQueryResult> {
|
|
69
69
|
return {
|
|
70
70
|
query: {
|
|
71
|
-
bucket: params.tableName,
|
|
71
|
+
bucket: params.tableName as string,
|
|
72
72
|
operation: 'keys'
|
|
73
73
|
},
|
|
74
74
|
params: []
|
|
@@ -78,10 +78,10 @@ export class KeyValueDatabase<
|
|
|
78
78
|
/**
|
|
79
79
|
* 构建查询键值查询
|
|
80
80
|
*/
|
|
81
|
-
protected buildSelectQuery<T extends
|
|
81
|
+
protected buildSelectQuery<T extends keyof S>(params: SelectQueryParams<S, T>): BuildQueryResult<KeyValueQueryResult> {
|
|
82
82
|
// 键值数据库的查询通常是获取所有键或特定键
|
|
83
83
|
const query: KeyValueQueryResult = {
|
|
84
|
-
bucket: params.tableName,
|
|
84
|
+
bucket: params.tableName as string,
|
|
85
85
|
operation: 'keys'
|
|
86
86
|
};
|
|
87
87
|
|
|
@@ -103,12 +103,12 @@ export class KeyValueDatabase<
|
|
|
103
103
|
/**
|
|
104
104
|
* 构建插入键值查询
|
|
105
105
|
*/
|
|
106
|
-
protected buildInsertQuery<T extends
|
|
106
|
+
protected buildInsertQuery<T extends keyof S>(params: InsertQueryParams<S, T>): BuildQueryResult<KeyValueQueryResult> {
|
|
107
107
|
const key = this.extractKeyFromData(params.data);
|
|
108
108
|
|
|
109
109
|
return {
|
|
110
110
|
query: {
|
|
111
|
-
bucket: params.tableName,
|
|
111
|
+
bucket: params.tableName as string,
|
|
112
112
|
operation: 'set',
|
|
113
113
|
key: key || 'default',
|
|
114
114
|
value: params.data
|
|
@@ -120,12 +120,12 @@ export class KeyValueDatabase<
|
|
|
120
120
|
/**
|
|
121
121
|
* 构建更新键值查询
|
|
122
122
|
*/
|
|
123
|
-
protected buildUpdateQuery<T extends
|
|
123
|
+
protected buildUpdateQuery<T extends keyof S>(params: UpdateQueryParams<S, T>): BuildQueryResult<KeyValueQueryResult> {
|
|
124
124
|
const key = this.extractKeyFromCondition(params.conditions);
|
|
125
125
|
|
|
126
126
|
return {
|
|
127
127
|
query: {
|
|
128
|
-
bucket: params.tableName,
|
|
128
|
+
bucket: params.tableName as string,
|
|
129
129
|
operation: 'set',
|
|
130
130
|
key: key || 'default',
|
|
131
131
|
value: params.update
|
|
@@ -137,12 +137,12 @@ export class KeyValueDatabase<
|
|
|
137
137
|
/**
|
|
138
138
|
* 构建删除键值查询
|
|
139
139
|
*/
|
|
140
|
-
protected buildDeleteQuery<T extends
|
|
140
|
+
protected buildDeleteQuery<T extends keyof S>(params: DeleteQueryParams<S, T>): BuildQueryResult<KeyValueQueryResult> {
|
|
141
141
|
const key = this.extractKeyFromCondition(params.conditions);
|
|
142
142
|
|
|
143
143
|
return {
|
|
144
144
|
query: {
|
|
145
|
-
bucket: params.tableName,
|
|
145
|
+
bucket: params.tableName as string,
|
|
146
146
|
operation: 'delete',
|
|
147
147
|
key: key || 'default'
|
|
148
148
|
},
|
|
@@ -153,10 +153,10 @@ export class KeyValueDatabase<
|
|
|
153
153
|
/**
|
|
154
154
|
* 构建修改桶查询
|
|
155
155
|
*/
|
|
156
|
-
protected buildAlterQuery<T extends
|
|
156
|
+
protected buildAlterQuery<T extends keyof S>(params: AlterQueryParams<S, T>): BuildQueryResult<KeyValueQueryResult> {
|
|
157
157
|
return {
|
|
158
158
|
query: {
|
|
159
|
-
bucket: params.tableName,
|
|
159
|
+
bucket: params.tableName as string,
|
|
160
160
|
operation: 'keys'
|
|
161
161
|
},
|
|
162
162
|
params: [params.alterations]
|
|
@@ -166,10 +166,10 @@ export class KeyValueDatabase<
|
|
|
166
166
|
/**
|
|
167
167
|
* 构建删除桶查询
|
|
168
168
|
*/
|
|
169
|
-
protected buildDropTableQuery<T extends
|
|
169
|
+
protected buildDropTableQuery<T extends keyof S>(params: DropTableQueryParams<S, T>): BuildQueryResult<KeyValueQueryResult> {
|
|
170
170
|
return {
|
|
171
171
|
query: {
|
|
172
|
-
bucket: params.tableName,
|
|
172
|
+
bucket: params.tableName as string,
|
|
173
173
|
operation: 'clear'
|
|
174
174
|
},
|
|
175
175
|
params: []
|
|
@@ -179,10 +179,10 @@ export class KeyValueDatabase<
|
|
|
179
179
|
/**
|
|
180
180
|
* 构建删除索引查询
|
|
181
181
|
*/
|
|
182
|
-
protected buildDropIndexQuery(params: DropIndexQueryParams): BuildQueryResult<KeyValueQueryResult> {
|
|
182
|
+
protected buildDropIndexQuery<T extends keyof S>(params: DropIndexQueryParams<S, T>): BuildQueryResult<KeyValueQueryResult> {
|
|
183
183
|
return {
|
|
184
184
|
query: {
|
|
185
|
-
bucket: params.tableName,
|
|
185
|
+
bucket: params.tableName as string,
|
|
186
186
|
operation: 'keys'
|
|
187
187
|
},
|
|
188
188
|
params: [params.indexName]
|
|
@@ -242,13 +242,13 @@ export class KeyValueDatabase<
|
|
|
242
242
|
/**
|
|
243
243
|
* 获取模型
|
|
244
244
|
*/
|
|
245
|
-
model<T extends keyof S>(name: T): KeyValueModel<S
|
|
246
|
-
let model = this.models.get(name as
|
|
245
|
+
model<T extends keyof S>(name: T): KeyValueModel<D, S, T> {
|
|
246
|
+
let model = this.models.get(name) as KeyValueModel<D, S, T> | undefined;
|
|
247
247
|
if (!model) {
|
|
248
|
-
model = new KeyValueModel
|
|
249
|
-
this.models.set(name as
|
|
248
|
+
model = new KeyValueModel<D, S, T>(this, name);
|
|
249
|
+
this.models.set(name, model as any);
|
|
250
250
|
}
|
|
251
|
-
return model as
|
|
251
|
+
return model as KeyValueModel<D, S, T>;
|
|
252
252
|
}
|
|
253
253
|
|
|
254
254
|
/**
|
|
@@ -6,10 +6,10 @@ import { KeyValueQueryResult } from '../../types.js';
|
|
|
6
6
|
* 键值模型类
|
|
7
7
|
* 继承自 Model,提供键值数据库特有的操作
|
|
8
8
|
*/
|
|
9
|
-
export class KeyValueModel<
|
|
9
|
+
export class KeyValueModel<D=any, S extends Record<string, object> = Record<string, object>, T extends keyof S = keyof S> extends Model<D, S, KeyValueQueryResult, T> {
|
|
10
10
|
constructor(
|
|
11
|
-
database: KeyValueDatabase<D>,
|
|
12
|
-
name:
|
|
11
|
+
database: KeyValueDatabase<D, S>,
|
|
12
|
+
name: T
|
|
13
13
|
) {
|
|
14
14
|
super(database, name);
|
|
15
15
|
}
|
|
@@ -20,7 +20,7 @@ export class KeyValueModel<T extends object = object, D=any> extends Model<D, T,
|
|
|
20
20
|
async set(key: string, value: any, ttl?: number): Promise<void> {
|
|
21
21
|
await this.dialect.query({
|
|
22
22
|
operation: 'set',
|
|
23
|
-
bucket: this.name,
|
|
23
|
+
bucket: this.name as string,
|
|
24
24
|
key,
|
|
25
25
|
value,
|
|
26
26
|
ttl,
|
|
@@ -34,7 +34,7 @@ export class KeyValueModel<T extends object = object, D=any> extends Model<D, T,
|
|
|
34
34
|
const results = await this.dialect.query(
|
|
35
35
|
{
|
|
36
36
|
operation: 'get',
|
|
37
|
-
bucket: this.name,
|
|
37
|
+
bucket: this.name as string,
|
|
38
38
|
key,
|
|
39
39
|
},
|
|
40
40
|
[key]
|
|
@@ -62,7 +62,7 @@ export class KeyValueModel<T extends object = object, D=any> extends Model<D, T,
|
|
|
62
62
|
const result = await this.dialect.query(
|
|
63
63
|
{
|
|
64
64
|
operation: 'delete',
|
|
65
|
-
bucket: this.name,
|
|
65
|
+
bucket: this.name as string,
|
|
66
66
|
key,
|
|
67
67
|
},
|
|
68
68
|
[key]
|
|
@@ -77,7 +77,7 @@ export class KeyValueModel<T extends object = object, D=any> extends Model<D, T,
|
|
|
77
77
|
const results = await this.dialect.query(
|
|
78
78
|
{
|
|
79
79
|
operation: 'has',
|
|
80
|
-
bucket: this.name,
|
|
80
|
+
bucket: this.name as string,
|
|
81
81
|
key,
|
|
82
82
|
},
|
|
83
83
|
[key, Date.now()]
|
|
@@ -92,7 +92,7 @@ export class KeyValueModel<T extends object = object, D=any> extends Model<D, T,
|
|
|
92
92
|
const results = await this.dialect.query(
|
|
93
93
|
{
|
|
94
94
|
operation: 'keys',
|
|
95
|
-
bucket: this.name,
|
|
95
|
+
bucket: this.name as string,
|
|
96
96
|
},
|
|
97
97
|
[Date.now()]
|
|
98
98
|
);
|
|
@@ -106,7 +106,7 @@ export class KeyValueModel<T extends object = object, D=any> extends Model<D, T,
|
|
|
106
106
|
const results = await this.dialect.query(
|
|
107
107
|
{
|
|
108
108
|
operation: 'values',
|
|
109
|
-
bucket: this.name,
|
|
109
|
+
bucket: this.name as string,
|
|
110
110
|
},
|
|
111
111
|
[Date.now()]
|
|
112
112
|
);
|
|
@@ -120,7 +120,7 @@ export class KeyValueModel<T extends object = object, D=any> extends Model<D, T,
|
|
|
120
120
|
const results = await this.dialect.query(
|
|
121
121
|
{
|
|
122
122
|
operation: 'entries',
|
|
123
|
-
bucket: this.name,
|
|
123
|
+
bucket: this.name as string,
|
|
124
124
|
},
|
|
125
125
|
[Date.now()]
|
|
126
126
|
);
|
|
@@ -133,7 +133,7 @@ export class KeyValueModel<T extends object = object, D=any> extends Model<D, T,
|
|
|
133
133
|
async clear(): Promise<void> {
|
|
134
134
|
await this.dialect.query({
|
|
135
135
|
operation: 'clear',
|
|
136
|
-
bucket: this.name,
|
|
136
|
+
bucket: this.name as string,
|
|
137
137
|
});
|
|
138
138
|
}
|
|
139
139
|
|
|
@@ -144,7 +144,7 @@ export class KeyValueModel<T extends object = object, D=any> extends Model<D, T,
|
|
|
144
144
|
const results = await this.dialect.query(
|
|
145
145
|
{
|
|
146
146
|
operation: 'size',
|
|
147
|
-
bucket: this.name,
|
|
147
|
+
bucket: this.name as string,
|
|
148
148
|
},
|
|
149
149
|
[Date.now()]
|
|
150
150
|
);
|
|
@@ -161,7 +161,7 @@ export class KeyValueModel<T extends object = object, D=any> extends Model<D, T,
|
|
|
161
161
|
await this.dialect.query(
|
|
162
162
|
{
|
|
163
163
|
operation: 'set',
|
|
164
|
-
bucket: this.name,
|
|
164
|
+
bucket: this.name as string,
|
|
165
165
|
key,
|
|
166
166
|
value,
|
|
167
167
|
ttl,
|
|
@@ -179,7 +179,7 @@ export class KeyValueModel<T extends object = object, D=any> extends Model<D, T,
|
|
|
179
179
|
const result = await this.dialect.query(
|
|
180
180
|
{
|
|
181
181
|
operation: 'expire',
|
|
182
|
-
bucket: this.name,
|
|
182
|
+
bucket: this.name as string,
|
|
183
183
|
key,
|
|
184
184
|
ttl,
|
|
185
185
|
},
|
|
@@ -195,7 +195,7 @@ export class KeyValueModel<T extends object = object, D=any> extends Model<D, T,
|
|
|
195
195
|
const results = await this.dialect.query(
|
|
196
196
|
{
|
|
197
197
|
operation: 'ttl',
|
|
198
|
-
bucket: this.name,
|
|
198
|
+
bucket: this.name as string,
|
|
199
199
|
key,
|
|
200
200
|
},
|
|
201
201
|
[key]
|
|
@@ -221,7 +221,7 @@ export class KeyValueModel<T extends object = object, D=any> extends Model<D, T,
|
|
|
221
221
|
const result = await this.dialect.query(
|
|
222
222
|
{
|
|
223
223
|
operation: 'persist',
|
|
224
|
-
bucket: this.name,
|
|
224
|
+
bucket: this.name as string,
|
|
225
225
|
key,
|
|
226
226
|
},
|
|
227
227
|
[key]
|
|
@@ -236,7 +236,7 @@ export class KeyValueModel<T extends object = object, D=any> extends Model<D, T,
|
|
|
236
236
|
const result = await this.dialect.query(
|
|
237
237
|
{
|
|
238
238
|
operation: 'cleanup',
|
|
239
|
-
bucket: this.name,
|
|
239
|
+
bucket: this.name as string,
|
|
240
240
|
},
|
|
241
241
|
[Date.now()]
|
|
242
242
|
);
|
|
@@ -252,7 +252,7 @@ export class KeyValueModel<T extends object = object, D=any> extends Model<D, T,
|
|
|
252
252
|
const results = await this.dialect.query(
|
|
253
253
|
{
|
|
254
254
|
operation: 'keysByPattern',
|
|
255
|
-
bucket: this.name,
|
|
255
|
+
bucket: this.name as string,
|
|
256
256
|
pattern: sqlPattern,
|
|
257
257
|
},
|
|
258
258
|
[sqlPattern, Date.now()]
|