@zhin.js/database 1.0.0

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 (109) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +93 -0
  3. package/lib/base/database.d.ts +71 -0
  4. package/lib/base/database.d.ts.map +1 -0
  5. package/lib/base/database.js +101 -0
  6. package/lib/base/database.js.map +1 -0
  7. package/lib/base/dialect.d.ts +34 -0
  8. package/lib/base/dialect.d.ts.map +1 -0
  9. package/lib/base/dialect.js +21 -0
  10. package/lib/base/dialect.js.map +1 -0
  11. package/lib/base/index.d.ts +6 -0
  12. package/lib/base/index.d.ts.map +1 -0
  13. package/lib/base/index.js +6 -0
  14. package/lib/base/index.js.map +1 -0
  15. package/lib/base/model.d.ts +53 -0
  16. package/lib/base/model.d.ts.map +1 -0
  17. package/lib/base/model.js +65 -0
  18. package/lib/base/model.js.map +1 -0
  19. package/lib/base/query-classes.d.ts +68 -0
  20. package/lib/base/query-classes.d.ts.map +1 -0
  21. package/lib/base/query-classes.js +178 -0
  22. package/lib/base/query-classes.js.map +1 -0
  23. package/lib/base/thenable.d.ts +15 -0
  24. package/lib/base/thenable.d.ts.map +1 -0
  25. package/lib/base/thenable.js +33 -0
  26. package/lib/base/thenable.js.map +1 -0
  27. package/lib/dialects/memory.d.ts +52 -0
  28. package/lib/dialects/memory.d.ts.map +1 -0
  29. package/lib/dialects/memory.js +772 -0
  30. package/lib/dialects/memory.js.map +1 -0
  31. package/lib/dialects/mongodb.d.ts +85 -0
  32. package/lib/dialects/mongodb.d.ts.map +1 -0
  33. package/lib/dialects/mongodb.js +461 -0
  34. package/lib/dialects/mongodb.js.map +1 -0
  35. package/lib/dialects/mysql.d.ts +33 -0
  36. package/lib/dialects/mysql.d.ts.map +1 -0
  37. package/lib/dialects/mysql.js +132 -0
  38. package/lib/dialects/mysql.js.map +1 -0
  39. package/lib/dialects/pg.d.ts +33 -0
  40. package/lib/dialects/pg.d.ts.map +1 -0
  41. package/lib/dialects/pg.js +132 -0
  42. package/lib/dialects/pg.js.map +1 -0
  43. package/lib/dialects/redis.d.ts +87 -0
  44. package/lib/dialects/redis.d.ts.map +1 -0
  45. package/lib/dialects/redis.js +500 -0
  46. package/lib/dialects/redis.js.map +1 -0
  47. package/lib/dialects/sqlite.d.ts +46 -0
  48. package/lib/dialects/sqlite.d.ts.map +1 -0
  49. package/lib/dialects/sqlite.js +201 -0
  50. package/lib/dialects/sqlite.js.map +1 -0
  51. package/lib/index.d.ts +18 -0
  52. package/lib/index.d.ts.map +1 -0
  53. package/lib/index.js +18 -0
  54. package/lib/index.js.map +1 -0
  55. package/lib/registry.d.ts +37 -0
  56. package/lib/registry.d.ts.map +1 -0
  57. package/lib/registry.js +21 -0
  58. package/lib/registry.js.map +1 -0
  59. package/lib/type/document/database.d.ts +60 -0
  60. package/lib/type/document/database.d.ts.map +1 -0
  61. package/lib/type/document/database.js +242 -0
  62. package/lib/type/document/database.js.map +1 -0
  63. package/lib/type/document/model.d.ts +42 -0
  64. package/lib/type/document/model.d.ts.map +1 -0
  65. package/lib/type/document/model.js +65 -0
  66. package/lib/type/document/model.js.map +1 -0
  67. package/lib/type/keyvalue/database.d.ts +64 -0
  68. package/lib/type/keyvalue/database.d.ts.map +1 -0
  69. package/lib/type/keyvalue/database.js +214 -0
  70. package/lib/type/keyvalue/database.js.map +1 -0
  71. package/lib/type/keyvalue/model.d.ts +107 -0
  72. package/lib/type/keyvalue/model.d.ts.map +1 -0
  73. package/lib/type/keyvalue/model.js +261 -0
  74. package/lib/type/keyvalue/model.js.map +1 -0
  75. package/lib/type/related/database.d.ts +32 -0
  76. package/lib/type/related/database.d.ts.map +1 -0
  77. package/lib/type/related/database.js +334 -0
  78. package/lib/type/related/database.js.map +1 -0
  79. package/lib/type/related/model.d.ts +43 -0
  80. package/lib/type/related/model.d.ts.map +1 -0
  81. package/lib/type/related/model.js +108 -0
  82. package/lib/type/related/model.js.map +1 -0
  83. package/lib/types.d.ts +251 -0
  84. package/lib/types.d.ts.map +1 -0
  85. package/lib/types.js +28 -0
  86. package/lib/types.js.map +1 -0
  87. package/package.json +54 -0
  88. package/src/base/database.ts +128 -0
  89. package/src/base/dialect.ts +76 -0
  90. package/src/base/index.ts +5 -0
  91. package/src/base/model.ts +89 -0
  92. package/src/base/query-classes.ts +217 -0
  93. package/src/base/thenable.ts +54 -0
  94. package/src/dialects/memory.ts +880 -0
  95. package/src/dialects/mongodb.ts +533 -0
  96. package/src/dialects/mysql.ts +157 -0
  97. package/src/dialects/pg.ts +157 -0
  98. package/src/dialects/redis.ts +598 -0
  99. package/src/dialects/sqlite.ts +233 -0
  100. package/src/index.ts +20 -0
  101. package/src/registry.ts +59 -0
  102. package/src/type/document/database.ts +283 -0
  103. package/src/type/document/model.ts +86 -0
  104. package/src/type/keyvalue/database.ts +261 -0
  105. package/src/type/keyvalue/model.ts +339 -0
  106. package/src/type/related/database.ts +392 -0
  107. package/src/type/related/model.ts +117 -0
  108. package/src/types.ts +403 -0
  109. package/tsconfig.json +24 -0
package/src/types.ts ADDED
@@ -0,0 +1,403 @@
1
+
2
+ export type QueryType =
3
+ | 'create'
4
+ | 'alter'
5
+ | 'drop_table'
6
+ | 'drop_index'
7
+ | 'select'
8
+ | 'insert'
9
+ | 'update'
10
+ | 'delete';
11
+
12
+ // ============================================================================
13
+ // Column Type Definitions
14
+ // ============================================================================
15
+
16
+ export type ColumnType =
17
+ | "text"
18
+ | "integer"
19
+ | "float"
20
+ | "boolean"
21
+ | "date"
22
+ | "json";
23
+
24
+ export interface Column<T = any> {
25
+ type: ColumnType;
26
+ nullable?: boolean;
27
+ default?: T;
28
+ autoIncrement?: boolean;
29
+ primary?: boolean;
30
+ unique?: boolean;
31
+ length?: number;
32
+ }
33
+
34
+ export type Schema<T extends object=object> = {
35
+ [P in keyof T]: Column<T[P]>;
36
+ }
37
+
38
+ // ============================================================================
39
+ // Column Alteration Types
40
+ // ============================================================================
41
+
42
+ export interface AddSchema<T = any> {
43
+ action: "add";
44
+ type: ColumnType;
45
+ nullable?: boolean;
46
+ default?: T;
47
+ primary?: boolean;
48
+ length?: number;
49
+ }
50
+
51
+ export interface ModifySchema<T = any> {
52
+ action: "modify";
53
+ type?: ColumnType;
54
+ nullable?: boolean;
55
+ default?: T;
56
+ length?: number;
57
+ }
58
+
59
+ export interface DropSchema {
60
+ action: "drop";
61
+ }
62
+
63
+ export type AlterSchema<T extends object> = {
64
+ [P in keyof T]?:AddSchema<T[P]> | ModifySchema<T[P]> | DropSchema
65
+ };
66
+
67
+ // ============================================================================
68
+ // Condition Types
69
+ // ============================================================================
70
+
71
+ export interface ComparisonOperators<T> {
72
+ $eq?: T;
73
+ $ne?: T;
74
+ $gt?: T;
75
+ $gte?: T;
76
+ $lt?: T;
77
+ $lte?: T;
78
+ $in?: T[];
79
+ $nin?: T[];
80
+ $like?: string;
81
+ $nlike?: string;
82
+ }
83
+
84
+ export interface LogicOperators<T = any> {
85
+ $and?: Condition<T>[];
86
+ $or?: Condition<T>[];
87
+ $not?: Condition<T>;
88
+ }
89
+
90
+ export type Condition<T = object> ={
91
+ [P in keyof T]?: T[P] | ComparisonOperators<T[P]> | LogicOperators<T[P]>;
92
+ }|LogicOperators<T>;
93
+
94
+ // ============================================================================
95
+ // Ordering Types
96
+ // ============================================================================
97
+
98
+ export type SortDirection = "ASC" | "DESC";
99
+
100
+ export interface Ordering<T extends object> {
101
+ field: keyof T;
102
+ direction: SortDirection;
103
+ }
104
+
105
+ // ============================================================================
106
+ // Query Parameter Types (Discriminated Union)
107
+ // ============================================================================
108
+
109
+ export interface BaseQueryParams {
110
+ tableName: string;
111
+ }
112
+
113
+ export interface CreateQueryParams<T extends object = any> extends BaseQueryParams {
114
+ type: 'create';
115
+ schema: Schema<T>;
116
+ }
117
+
118
+ export interface AlterQueryParams<T extends object = any> extends BaseQueryParams {
119
+ type: 'alter';
120
+ alterations: AlterSchema<T>;
121
+ }
122
+
123
+ export interface DropTableQueryParams<T extends object = any> extends BaseQueryParams {
124
+ type: 'drop_table';
125
+ conditions?: Condition<T>;
126
+ }
127
+
128
+ export interface DropIndexQueryParams extends BaseQueryParams {
129
+ type: 'drop_index';
130
+ indexName: string;
131
+ conditions?: Condition<any>;
132
+ }
133
+
134
+ export interface SelectQueryParams<T extends object = any> extends BaseQueryParams {
135
+ type: 'select';
136
+ fields?: (keyof T)[];
137
+ conditions?: Condition<T>;
138
+ groupings?: (keyof T)[];
139
+ orderings?: Ordering<T>[];
140
+ limitCount?: number;
141
+ offsetCount?: number;
142
+ }
143
+
144
+ export interface InsertQueryParams<T extends object = any> extends BaseQueryParams {
145
+ type: 'insert';
146
+ data: T;
147
+ }
148
+
149
+ export interface UpdateQueryParams<T extends object = any> extends BaseQueryParams {
150
+ type: 'update';
151
+ update: Partial<T>;
152
+ conditions?: Condition<T>;
153
+ }
154
+
155
+ export interface DeleteQueryParams<T extends object = any> extends BaseQueryParams {
156
+ type: 'delete';
157
+ conditions?: Condition<T>;
158
+ }
159
+
160
+ export type QueryParams<T extends object = any> =
161
+ | CreateQueryParams<T>
162
+ | AlterQueryParams<T>
163
+ | DropTableQueryParams<T>
164
+ | DropIndexQueryParams
165
+ | SelectQueryParams<T>
166
+ | InsertQueryParams<T>
167
+ | UpdateQueryParams<T>
168
+ | DeleteQueryParams<T>;
169
+
170
+ // ============================================================================
171
+ // Query Result Types
172
+ // ============================================================================
173
+
174
+ export interface BuildQueryResult<R> {
175
+ query: R;
176
+ params: any[];
177
+ }
178
+
179
+ // ============================================================================
180
+ // NoSQL Query Result Types
181
+ // ============================================================================
182
+
183
+ /**
184
+ * 文档查询结果类型
185
+ */
186
+ export interface DocumentQueryResult {
187
+ collection: string;
188
+ filter: Record<string, any>;
189
+ sort?: Record<string, 1 | -1>;
190
+ limit?: number;
191
+ skip?: number;
192
+ projection?: Record<string, 1 | 0>;
193
+ operation?: 'find' | 'insertOne' | 'insertMany' | 'updateOne' | 'updateMany' | 'deleteOne' | 'deleteMany' | 'createCollection' | 'createIndex' | 'dropIndex' | 'dropCollection';
194
+ }
195
+
196
+ /**
197
+ * 键值查询结果类型
198
+ */
199
+ export interface KeyValueQueryResult {
200
+ bucket: string;
201
+ operation: 'get' | 'set' | 'delete' | 'has' | 'keys' | 'values' | 'entries' | 'clear' | 'size' | 'expire' | 'ttl' | 'persist' | 'cleanup' | 'keysByPattern';
202
+ key?: string;
203
+ value?: any;
204
+ ttl?: number;
205
+ pattern?: string;
206
+ keys?: string[];
207
+ }
208
+
209
+ /**
210
+ * 文档操作结果类型
211
+ */
212
+ export interface DocumentOperationResult<T = any> {
213
+ success: boolean;
214
+ data?: T | T[];
215
+ count?: number;
216
+ error?: string;
217
+ }
218
+
219
+ /**
220
+ * 键值操作结果类型
221
+ */
222
+ export interface KeyValueOperationResult<T = any> {
223
+ success: boolean;
224
+ data?: T | T[] | number | boolean;
225
+ error?: string;
226
+ }
227
+
228
+ /**
229
+ * 数据库方言信息接口
230
+ */
231
+ export interface DatabaseDialect {
232
+ name: string;
233
+ version: string;
234
+ features: string[];
235
+ dataTypes: Record<string, string>;
236
+ identifierQuote: string;
237
+ parameterPlaceholder: string;
238
+ supportsTransactions: boolean;
239
+ supportsIndexes: boolean;
240
+ supportsForeignKeys: boolean;
241
+ supportsViews: boolean;
242
+ supportsStoredProcedures: boolean;
243
+ }
244
+
245
+ export interface SelectResult<T> {
246
+ rows: T[];
247
+ count?: number;
248
+ }
249
+
250
+ export interface InsertResult<T = any> {
251
+ insertId?: number | string;
252
+ affectedRows?: number;
253
+ data?: T;
254
+ }
255
+
256
+ export interface UpdateResult {
257
+ affectedRows: number;
258
+ }
259
+
260
+ export interface DeleteResult {
261
+ affectedRows: number;
262
+ }
263
+
264
+ // ============================================================================
265
+ // Driver Configuration Types
266
+ // ============================================================================
267
+
268
+ export interface BaseDriverConfig {
269
+ timeout?: number;
270
+ retries?: number;
271
+ }
272
+
273
+
274
+ export interface MemoryConfig{
275
+ }
276
+
277
+
278
+ // ============================================================================
279
+ // Driver Interface Types
280
+ // ============================================================================
281
+
282
+ export interface DriverConnection {
283
+ isConnected(): boolean;
284
+ connect(): Promise<void>;
285
+ disconnect(): Promise<void>;
286
+ healthCheck(): Promise<boolean>;
287
+ }
288
+
289
+ export interface DriverQuery {
290
+ query<T = any>(sql: string, params?: any[]): Promise<T>;
291
+ }
292
+
293
+ export interface DriverSchema {
294
+ getTables(): Promise<string[]>;
295
+ getTableInfo(tableName: string): Promise<TableInfo[]>;
296
+ }
297
+
298
+ export interface DriverQueryBuilder<R> {
299
+ buildQuery<T extends object = any>(params: QueryParams<T>): BuildQueryResult<R>;
300
+ }
301
+
302
+ export interface DriverLifecycle {
303
+ dispose(): Promise<void>;
304
+ }
305
+
306
+ export interface TableInfo {
307
+ name: string;
308
+ type: string;
309
+ nullable: boolean;
310
+ defaultValue: any;
311
+ primaryKey: boolean;
312
+ unique: boolean;
313
+ length?: number;
314
+ }
315
+
316
+ // ============================================================================
317
+ // Model Types
318
+ // ============================================================================
319
+
320
+ export interface ModelField {
321
+ type:
322
+ | "string"
323
+ | "integer"
324
+ | "number"
325
+ | "float"
326
+ | "json"
327
+ | "boolean"
328
+ | "date"
329
+ | "object"
330
+ | "array";
331
+ initial?: any;
332
+ length?: number;
333
+ primary?: boolean;
334
+ autoIncrement?: boolean;
335
+ unique?: boolean;
336
+ notNull?: boolean;
337
+ }
338
+
339
+ export interface ModelConfig {
340
+ driver?: string;
341
+ timestamps?: boolean;
342
+ softDelete?: boolean;
343
+ relations?: ModelRelation[];
344
+ }
345
+
346
+ export interface ModelRelation {
347
+ model: string;
348
+ type: "belongs-to" | "has-one" | "has-many" | "many-to-many";
349
+ foreignKey?: string;
350
+ through?: string;
351
+ }
352
+
353
+ // ============================================================================
354
+ // Utility Types
355
+ // ========================================
356
+
357
+ export type RequiredKeys<T, K extends keyof T> = T & Required<Pick<T, K>>;
358
+
359
+ export type OptionalKeys<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
360
+
361
+ export type DeepPartial<T> = {
362
+ [P in keyof T]?: T[P] extends object ? DeepPartial<T[P]> : T[P];
363
+ };
364
+
365
+ export type DeepRequired<T> = {
366
+ [P in keyof T]-?: T[P] extends object ? DeepRequired<T[P]> : T[P];
367
+ };
368
+
369
+ // ============================================================================
370
+ // Type Guards
371
+ // ============================================================================
372
+
373
+ export function isCreateQuery<T extends object>(params: QueryParams<T>): params is CreateQueryParams<T> {
374
+ return params.type === 'create';
375
+ }
376
+
377
+ export function isAlterQuery<T extends object>(params: QueryParams<T>): params is AlterQueryParams<T> {
378
+ return params.type === 'alter';
379
+ }
380
+
381
+ export function isSelectQuery<T extends object>(params: QueryParams<T>): params is SelectQueryParams<T> {
382
+ return params.type === 'select';
383
+ }
384
+
385
+ export function isInsertQuery<T extends object>(params: QueryParams<T>): params is InsertQueryParams<T> {
386
+ return params.type === 'insert';
387
+ }
388
+
389
+ export function isUpdateQuery<T extends object>(params: QueryParams<T>): params is UpdateQueryParams<T> {
390
+ return params.type === 'update';
391
+ }
392
+
393
+ export function isDeleteQuery<T extends object>(params: QueryParams<T>): params is DeleteQueryParams<T> {
394
+ return params.type === 'delete';
395
+ }
396
+
397
+ export function isDropTableQuery<T extends object>(params: QueryParams<T>): params is DropTableQueryParams<T> {
398
+ return params.type === 'drop_table';
399
+ }
400
+
401
+ export function isDropIndexQuery(params: QueryParams): params is DropIndexQueryParams {
402
+ return params.type === 'drop_index';
403
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,24 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ES2022",
4
+ "module": "ESNext",
5
+ "moduleResolution": "bundler",
6
+ "outDir": "./lib",
7
+ "rootDir": "./src",
8
+ "strict": true,
9
+ "esModuleInterop": true,
10
+ "skipLibCheck": true,
11
+ "forceConsistentCasingInFileNames": true,
12
+ "resolveJsonModule": true,
13
+ "isolatedModules": true,
14
+ "allowSyntheticDefaultImports": true,
15
+ "experimentalDecorators": true,
16
+ "emitDecoratorMetadata": true,
17
+ "declaration": true,
18
+ "declarationMap": true,
19
+ "sourceMap": true,
20
+ "verbatimModuleSyntax": false
21
+ },
22
+ "include": ["src/**/*"],
23
+ "exclude": ["lib", "node_modules"]
24
+ }