@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.
Files changed (87) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/lib/base/database.d.ts +7 -7
  3. package/lib/base/database.d.ts.map +1 -1
  4. package/lib/base/database.js +5 -5
  5. package/lib/base/database.js.map +1 -1
  6. package/lib/base/dialect.d.ts.map +1 -1
  7. package/lib/base/dialect.js.map +1 -1
  8. package/lib/base/model.d.ts +2 -2
  9. package/lib/base/model.d.ts.map +1 -1
  10. package/lib/base/model.js.map +1 -1
  11. package/lib/base/query-classes.d.ts +4 -4
  12. package/lib/base/query-classes.d.ts.map +1 -1
  13. package/lib/base/query-classes.js +4 -4
  14. package/lib/base/query-classes.js.map +1 -1
  15. package/lib/dialects/memory.d.ts +2 -2
  16. package/lib/dialects/memory.d.ts.map +1 -1
  17. package/lib/dialects/memory.js +5 -5
  18. package/lib/dialects/memory.js.map +1 -1
  19. package/lib/dialects/mongodb.d.ts +2 -2
  20. package/lib/dialects/mongodb.d.ts.map +1 -1
  21. package/lib/dialects/mongodb.js +5 -5
  22. package/lib/dialects/mongodb.js.map +1 -1
  23. package/lib/dialects/mysql.d.ts +2 -2
  24. package/lib/dialects/mysql.d.ts.map +1 -1
  25. package/lib/dialects/mysql.js +5 -5
  26. package/lib/dialects/mysql.js.map +1 -1
  27. package/lib/dialects/pg.d.ts +2 -2
  28. package/lib/dialects/pg.d.ts.map +1 -1
  29. package/lib/dialects/pg.js +5 -5
  30. package/lib/dialects/pg.js.map +1 -1
  31. package/lib/dialects/redis.d.ts +2 -2
  32. package/lib/dialects/redis.d.ts.map +1 -1
  33. package/lib/dialects/redis.js +5 -5
  34. package/lib/dialects/redis.js.map +1 -1
  35. package/lib/dialects/sqlite.d.ts +2 -2
  36. package/lib/dialects/sqlite.d.ts.map +1 -1
  37. package/lib/dialects/sqlite.js +5 -5
  38. package/lib/dialects/sqlite.js.map +1 -1
  39. package/lib/registry.d.ts +12 -12
  40. package/lib/registry.d.ts.map +1 -1
  41. package/lib/registry.js +2 -2
  42. package/lib/registry.js.map +1 -1
  43. package/lib/type/document/database.d.ts +2 -2
  44. package/lib/type/document/database.d.ts.map +1 -1
  45. package/lib/type/document/database.js +4 -4
  46. package/lib/type/document/database.js.map +1 -1
  47. package/lib/type/document/model.d.ts +1 -1
  48. package/lib/type/document/model.d.ts.map +1 -1
  49. package/lib/type/document/model.js +1 -1
  50. package/lib/type/document/model.js.map +1 -1
  51. package/lib/type/keyvalue/database.d.ts +2 -2
  52. package/lib/type/keyvalue/database.d.ts.map +1 -1
  53. package/lib/type/keyvalue/database.js +4 -4
  54. package/lib/type/keyvalue/database.js.map +1 -1
  55. package/lib/type/keyvalue/model.d.ts +1 -1
  56. package/lib/type/keyvalue/model.d.ts.map +1 -1
  57. package/lib/type/keyvalue/model.js +1 -1
  58. package/lib/type/keyvalue/model.js.map +1 -1
  59. package/lib/type/related/database.d.ts +4 -4
  60. package/lib/type/related/database.d.ts.map +1 -1
  61. package/lib/type/related/database.js +7 -7
  62. package/lib/type/related/database.js.map +1 -1
  63. package/lib/type/related/model.d.ts +1 -1
  64. package/lib/type/related/model.d.ts.map +1 -1
  65. package/lib/type/related/model.js +1 -1
  66. package/lib/type/related/model.js.map +1 -1
  67. package/lib/types.d.ts +8 -8
  68. package/lib/types.d.ts.map +1 -1
  69. package/package.json +1 -1
  70. package/src/base/database.ts +7 -7
  71. package/src/base/dialect.ts +0 -26
  72. package/src/base/model.ts +2 -2
  73. package/src/base/query-classes.ts +4 -4
  74. package/src/dialects/memory.ts +7 -6
  75. package/src/dialects/mongodb.ts +6 -6
  76. package/src/dialects/mysql.ts +7 -7
  77. package/src/dialects/pg.ts +7 -7
  78. package/src/dialects/redis.ts +7 -7
  79. package/src/dialects/sqlite.ts +7 -7
  80. package/src/registry.ts +13 -13
  81. package/src/type/document/database.ts +4 -4
  82. package/src/type/document/model.ts +1 -1
  83. package/src/type/keyvalue/database.ts +4 -4
  84. package/src/type/keyvalue/model.ts +2 -2
  85. package/src/type/related/database.ts +13 -13
  86. package/src/type/related/model.ts +1 -1
  87. 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, schemas?: Database.Schemas<S>) => Database<any, S, any>;
29
- export type Constructor<D, S extends Record<string, object>> = new (config: D, schemas?: Database.Schemas<S>) => Database<any, S, any>;
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
- schemas?: Database.Schemas<S>
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, schemas) : factory(config, schemas)) as any;
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
- schemas?: Database.Schemas<S>,
28
+ definitions?: Database.Definitions<S>,
29
29
  ) {
30
- super(dialect, schemas);
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.schemas || {});
280
+ return Object.keys(this.definitions || {});
281
281
  }
282
282
  }
283
283
 
@@ -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
 
@@ -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
- schemas?: Database.Schemas<S>,
29
+ definitions?: Database.Definitions<S>,
30
30
  ) {
31
- super(dialect, schemas);
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.schemas || {});
258
+ return Object.keys(this.definitions || {});
259
259
  }
260
260
  }
261
261
 
@@ -1,6 +1,6 @@
1
- import { Model, Dialect, Database } from '../../base';
1
+ import { Model, } from '../../base/index.js';
2
2
  import { KeyValueDatabase } from './database.js';
3
- import { Condition, KeyValueQueryResult } from '../../types.js';
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
- AddSchema,
17
- ModifySchema,
18
- DropSchema,
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
- schemas?: Database.Schemas<S>,
40
+ definitions?: Database.Definitions<S>,
41
41
  ) {
42
- super(dialect,schemas);
42
+ super(dialect,definitions);
43
43
  }
44
44
 
45
45
  protected async initialize(): Promise<void> {
46
46
  // 自动创建表
47
- for (const [tableName, schema] of Object.entries(this.schemas || {})) {
48
- await this.create(tableName, schema);
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.schema).map(([field, column]) => this.formatColumnDefinition(field,column as Column));
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 AddSchema<T> | ModifySchema<T> | DropSchema));
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: AddSchema<T> | ModifySchema<T> | DropSchema): string {
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.schemas || {});
390
+ return Object.keys(this.definitions || {});
391
391
  }
392
392
  }
@@ -1,4 +1,4 @@
1
- import { Model} from '../../base';
1
+ import { Model} from '../../base/index.js';
2
2
  import { RelatedDatabase } from './database.js';
3
3
  import { Condition } from '../../types.js';
4
4
 
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 Schema<T extends object=object> = {
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 AddSchema<T = any> {
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 ModifySchema<T = any> {
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 DropSchema {
59
+ export interface DropDefinition {
60
60
  action: "drop";
61
61
  }
62
62
 
63
- export type AlterSchema<T extends object> = {
64
- [P in keyof T]?:AddSchema<T[P]> | ModifySchema<T[P]> | DropSchema
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
- schema: Schema<T>;
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: AlterSchema<T>;
120
+ alterations: AlterDefinition<T>;
121
121
  }
122
122
 
123
123
  export interface DropTableQueryParams<T extends object = any> extends BaseQueryParams {