@zhin.js/database 1.0.3 → 1.0.5

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.
Files changed (119) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/README.md +1360 -34
  3. package/lib/base/database.d.ts +71 -13
  4. package/lib/base/database.d.ts.map +1 -1
  5. package/lib/base/database.js +128 -4
  6. package/lib/base/database.js.map +1 -1
  7. package/lib/base/dialect.d.ts +27 -10
  8. package/lib/base/dialect.d.ts.map +1 -1
  9. package/lib/base/dialect.js +32 -0
  10. package/lib/base/dialect.js.map +1 -1
  11. package/lib/base/index.d.ts +1 -0
  12. package/lib/base/index.d.ts.map +1 -1
  13. package/lib/base/index.js +1 -0
  14. package/lib/base/index.js.map +1 -1
  15. package/lib/base/model.d.ts +105 -12
  16. package/lib/base/model.d.ts.map +1 -1
  17. package/lib/base/model.js +224 -3
  18. package/lib/base/model.js.map +1 -1
  19. package/lib/base/query-classes.d.ts +204 -33
  20. package/lib/base/query-classes.d.ts.map +1 -1
  21. package/lib/base/query-classes.js +276 -0
  22. package/lib/base/query-classes.js.map +1 -1
  23. package/lib/base/thenable.d.ts +7 -7
  24. package/lib/base/thenable.d.ts.map +1 -1
  25. package/lib/base/thenable.js +5 -4
  26. package/lib/base/thenable.js.map +1 -1
  27. package/lib/base/transaction.d.ts +46 -0
  28. package/lib/base/transaction.d.ts.map +1 -0
  29. package/lib/base/transaction.js +186 -0
  30. package/lib/base/transaction.js.map +1 -0
  31. package/lib/dialects/memory.d.ts +13 -8
  32. package/lib/dialects/memory.d.ts.map +1 -1
  33. package/lib/dialects/memory.js +10 -7
  34. package/lib/dialects/memory.js.map +1 -1
  35. package/lib/dialects/mongodb.d.ts +12 -8
  36. package/lib/dialects/mongodb.d.ts.map +1 -1
  37. package/lib/dialects/mongodb.js +21 -18
  38. package/lib/dialects/mongodb.js.map +1 -1
  39. package/lib/dialects/mysql.d.ts +36 -7
  40. package/lib/dialects/mysql.d.ts.map +1 -1
  41. package/lib/dialects/mysql.js +140 -21
  42. package/lib/dialects/mysql.js.map +1 -1
  43. package/lib/dialects/pg.d.ts +36 -7
  44. package/lib/dialects/pg.d.ts.map +1 -1
  45. package/lib/dialects/pg.js +140 -21
  46. package/lib/dialects/pg.js.map +1 -1
  47. package/lib/dialects/redis.d.ts +13 -8
  48. package/lib/dialects/redis.d.ts.map +1 -1
  49. package/lib/dialects/redis.js +14 -11
  50. package/lib/dialects/redis.js.map +1 -1
  51. package/lib/dialects/sqlite.d.ts +20 -7
  52. package/lib/dialects/sqlite.d.ts.map +1 -1
  53. package/lib/dialects/sqlite.js +66 -13
  54. package/lib/dialects/sqlite.js.map +1 -1
  55. package/lib/index.d.ts +1 -0
  56. package/lib/index.d.ts.map +1 -1
  57. package/lib/index.js +1 -0
  58. package/lib/index.js.map +1 -1
  59. package/lib/migration.d.ts +132 -0
  60. package/lib/migration.d.ts.map +1 -0
  61. package/lib/migration.js +475 -0
  62. package/lib/migration.js.map +1 -0
  63. package/lib/registry.d.ts +26 -23
  64. package/lib/registry.d.ts.map +1 -1
  65. package/lib/registry.js +1 -5
  66. package/lib/registry.js.map +1 -1
  67. package/lib/type/document/database.d.ts +12 -12
  68. package/lib/type/document/database.d.ts.map +1 -1
  69. package/lib/type/document/database.js +1 -1
  70. package/lib/type/document/database.js.map +1 -1
  71. package/lib/type/document/model.d.ts +8 -8
  72. package/lib/type/document/model.d.ts.map +1 -1
  73. package/lib/type/document/model.js +1 -1
  74. package/lib/type/document/model.js.map +1 -1
  75. package/lib/type/keyvalue/database.d.ts +12 -12
  76. package/lib/type/keyvalue/database.d.ts.map +1 -1
  77. package/lib/type/keyvalue/database.js +1 -1
  78. package/lib/type/keyvalue/database.js.map +1 -1
  79. package/lib/type/keyvalue/model.d.ts +3 -3
  80. package/lib/type/keyvalue/model.d.ts.map +1 -1
  81. package/lib/type/keyvalue/model.js +1 -1
  82. package/lib/type/keyvalue/model.js.map +1 -1
  83. package/lib/type/related/database.d.ts +49 -14
  84. package/lib/type/related/database.d.ts.map +1 -1
  85. package/lib/type/related/database.js +259 -28
  86. package/lib/type/related/database.js.map +1 -1
  87. package/lib/type/related/model.d.ts +252 -16
  88. package/lib/type/related/model.d.ts.map +1 -1
  89. package/lib/type/related/model.js +648 -23
  90. package/lib/type/related/model.js.map +1 -1
  91. package/lib/types.d.ts +475 -37
  92. package/lib/types.d.ts.map +1 -1
  93. package/lib/types.js +6 -0
  94. package/lib/types.js.map +1 -1
  95. package/package.json +10 -5
  96. package/src/base/database.ts +168 -24
  97. package/src/base/dialect.ts +49 -10
  98. package/src/base/index.ts +2 -1
  99. package/src/base/model.ts +258 -18
  100. package/src/base/query-classes.ts +471 -63
  101. package/src/base/thenable.ts +12 -11
  102. package/src/base/transaction.ts +213 -0
  103. package/src/dialects/memory.ts +17 -16
  104. package/src/dialects/mongodb.ts +44 -42
  105. package/src/dialects/mysql.ts +155 -26
  106. package/src/dialects/pg.ts +152 -25
  107. package/src/dialects/redis.ts +45 -43
  108. package/src/dialects/sqlite.ts +77 -19
  109. package/src/index.ts +1 -2
  110. package/src/migration.ts +544 -0
  111. package/src/registry.ts +33 -33
  112. package/src/type/document/database.ts +33 -33
  113. package/src/type/document/model.ts +15 -15
  114. package/src/type/keyvalue/database.ts +33 -33
  115. package/src/type/keyvalue/model.ts +19 -19
  116. package/src/type/related/database.ts +309 -34
  117. package/src/type/related/model.ts +801 -34
  118. package/src/types.ts +559 -44
  119. 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 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 { 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: RelatedDatabase<MemoryConfig,S>;
18
- mongodb: DocumentDatabase<MongoDBDialectConfig,S>;
19
- mysql: RelatedDatabase<MySQLDialectConfig,S>;
20
- pg: RelatedDatabase<PostgreSQLDialectConfig,S>;
21
- redis: KeyValueDatabase<RedisDialectConfig,S>;
22
- sqlite: RelatedDatabase<SQLiteDialectConfig>;
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 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
- export type Factory<D, S extends Record<string, object>> = Creator<any, S> | Constructor<any, S>;
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 type Config<T extends Database<any, any, any>> = T extends Database<infer D, any, any> ? D: any;
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<D extends string, S extends Record<string, object>>(
41
+ export function register<S extends Record<string, object>,D extends keyof Databases<S>>(
38
42
  dialect: D,
39
- factory: Factory<any, S>
43
+ factory: Factory<Config[D], S,any>
40
44
  ): void {
41
- factories.set(dialect, factory as Factory<any, S>);
45
+ factories.set(dialect, factory as Factory<Config[D], S,any>);
42
46
  }
43
47
 
44
- export function create<D extends string, S extends Record<string, object>>(
48
+ export function create<S extends Record<string, object>, D extends keyof Databases<S>>(
45
49
  dialect: D,
46
- config: any,
47
- definitions?: Database.Definitions<S>
48
- ): any {
49
- const factory = factories.get(dialect) as Factory<any, S> | undefined;
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 (isConstructor(factory) ? new factory(config, definitions) : factory(config, definitions)) as any;
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
  }
@@ -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,
@@ -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.Definitions<S>,
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<U extends object = any>(params: QueryParams<U>): BuildQueryResult<DocumentQueryResult> {
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<U>);
44
+ return this.buildCreateQuery(params as CreateQueryParams<S, T>);
45
45
  case 'select':
46
- return this.buildSelectQuery(params as SelectQueryParams<U>);
46
+ return this.buildSelectQuery(params as SelectQueryParams<S, T>);
47
47
  case 'insert':
48
- return this.buildInsertQuery(params as InsertQueryParams<U>);
48
+ return this.buildInsertQuery(params as InsertQueryParams<S, T>);
49
49
  case 'update':
50
- return this.buildUpdateQuery(params as UpdateQueryParams<U>);
50
+ return this.buildUpdateQuery(params as UpdateQueryParams<S, T>);
51
51
  case 'delete':
52
- return this.buildDeleteQuery(params as DeleteQueryParams<U>);
52
+ return this.buildDeleteQuery(params as DeleteQueryParams<S, T>);
53
53
  case 'alter':
54
- return this.buildAlterQuery(params as AlterQueryParams<U>);
54
+ return this.buildAlterQuery(params as AlterQueryParams<S, T>);
55
55
  case 'drop_table':
56
- return this.buildDropTableQuery(params as DropTableQueryParams<U>);
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 object>(params: CreateQueryParams<T>): BuildQueryResult<DocumentQueryResult> {
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 object>(params: SelectQueryParams<T>): BuildQueryResult<DocumentQueryResult> {
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 object>(params: InsertQueryParams<T>): BuildQueryResult<DocumentQueryResult> {
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 object>(params: UpdateQueryParams<T>): BuildQueryResult<DocumentQueryResult> {
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 object>(params: DeleteQueryParams<T>): BuildQueryResult<DocumentQueryResult> {
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 object>(params: AlterQueryParams<T>): BuildQueryResult<DocumentQueryResult> {
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 object>(params: DropTableQueryParams<T>): BuildQueryResult<DocumentQueryResult> {
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[T], D> {
268
- let model = this.models.get(name as string);
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(this as unknown as DocumentDatabase<D>, name as string);
271
- this.models.set(name as string, model);
270
+ model = new DocumentModel<D, S, T>(this, name);
271
+ this.models.set(name, model as any);
272
272
  }
273
- return model as unknown as DocumentModel<S[T], D>;
273
+ return model as DocumentModel<D, S, T>;
274
274
  }
275
275
 
276
276
  /**
@@ -1,4 +1,4 @@
1
- import { Model} from '../../base';
1
+ import { Model} from '../../base/index.js';
2
2
  import { DocumentDatabase } from './database.js';
3
3
  import { DocumentQueryResult, Condition } from '../../types.js';
4
4
 
@@ -6,10 +6,10 @@ import { DocumentQueryResult, Condition } from '../../types.js';
6
6
  * 文档型模型类
7
7
  * 继承自 Model,提供文档型数据库特有的操作
8
8
  */
9
- export class DocumentModel<T extends object = object, D =any> extends Model<D, T, DocumentQueryResult> {
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: string
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
  /**
@@ -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,
@@ -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.Definitions<S>,
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<U extends object = any>(params: QueryParams<U>): BuildQueryResult<KeyValueQueryResult> {
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<U>);
45
+ return this.buildCreateQuery(params as CreateQueryParams<S, T>);
46
46
  case 'select':
47
- return this.buildSelectQuery(params as SelectQueryParams<U>);
47
+ return this.buildSelectQuery(params as SelectQueryParams<S, T>);
48
48
  case 'insert':
49
- return this.buildInsertQuery(params as InsertQueryParams<U>);
49
+ return this.buildInsertQuery(params as InsertQueryParams<S, T>);
50
50
  case 'update':
51
- return this.buildUpdateQuery(params as UpdateQueryParams<U>);
51
+ return this.buildUpdateQuery(params as UpdateQueryParams<S, T>);
52
52
  case 'delete':
53
- return this.buildDeleteQuery(params as DeleteQueryParams<U>);
53
+ return this.buildDeleteQuery(params as DeleteQueryParams<S, T>);
54
54
  case 'alter':
55
- return this.buildAlterQuery(params as AlterQueryParams<U>);
55
+ return this.buildAlterQuery(params as AlterQueryParams<S, T>);
56
56
  case 'drop_table':
57
- return this.buildDropTableQuery(params as DropTableQueryParams<U>);
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 object>(params: CreateQueryParams<T>): BuildQueryResult<KeyValueQueryResult> {
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 object>(params: SelectQueryParams<T>): BuildQueryResult<KeyValueQueryResult> {
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 object>(params: InsertQueryParams<T>): BuildQueryResult<KeyValueQueryResult> {
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 object>(params: UpdateQueryParams<T>): BuildQueryResult<KeyValueQueryResult> {
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 object>(params: DeleteQueryParams<T>): BuildQueryResult<KeyValueQueryResult> {
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 object>(params: AlterQueryParams<T>): BuildQueryResult<KeyValueQueryResult> {
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 object>(params: DropTableQueryParams<T>): BuildQueryResult<KeyValueQueryResult> {
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[T], D> {
246
- let model = this.models.get(name as string);
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(this as unknown as KeyValueDatabase<D>, name as string);
249
- this.models.set(name as string, model);
248
+ model = new KeyValueModel<D, S, T>(this, name);
249
+ this.models.set(name, model as any);
250
250
  }
251
- return model as unknown as KeyValueModel<S[T], D>;
251
+ return model as KeyValueModel<D, S, T>;
252
252
  }
253
253
 
254
254
  /**