@cheetah.js/orm 0.1.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.
- package/README.md +228 -0
- package/build.ts +14 -0
- package/cheetah.config.ts +14 -0
- package/dist/SqlBuilder.d.ts +57 -0
- package/dist/SqlBuilder.js +436 -0
- package/dist/SqlBuilder.js.map +1 -0
- package/dist/bun/index.d.ts +13 -0
- package/dist/bun/index.js +224319 -0
- package/dist/bun/index.js.map +306 -0
- package/dist/cheetah.d.ts +1 -0
- package/dist/cheetah.js +24 -0
- package/dist/cheetah.js.map +1 -0
- package/dist/constants.d.ts +4 -0
- package/dist/constants.js +5 -0
- package/dist/constants.js.map +1 -0
- package/dist/decorators/entity.decorator.d.ts +3 -0
- package/dist/decorators/entity.decorator.js +10 -0
- package/dist/decorators/entity.decorator.js.map +1 -0
- package/dist/decorators/index.decorator.d.ts +3 -0
- package/dist/decorators/index.decorator.js +17 -0
- package/dist/decorators/index.decorator.js.map +1 -0
- package/dist/decorators/one-many.decorator.d.ts +3 -0
- package/dist/decorators/one-many.decorator.js +17 -0
- package/dist/decorators/one-many.decorator.js.map +1 -0
- package/dist/decorators/primary-key.decorator.d.ts +2 -0
- package/dist/decorators/primary-key.decorator.js +6 -0
- package/dist/decorators/primary-key.decorator.js.map +1 -0
- package/dist/decorators/property.decorator.d.ts +15 -0
- package/dist/decorators/property.decorator.js +25 -0
- package/dist/decorators/property.decorator.js.map +1 -0
- package/dist/domain/base-entity.d.ts +42 -0
- package/dist/domain/base-entity.js +106 -0
- package/dist/domain/base-entity.js.map +1 -0
- package/dist/domain/collection.d.ts +6 -0
- package/dist/domain/collection.js +11 -0
- package/dist/domain/collection.js.map +1 -0
- package/dist/domain/entities.d.ts +40 -0
- package/dist/domain/entities.js +137 -0
- package/dist/domain/entities.js.map +1 -0
- package/dist/domain/reference.d.ts +4 -0
- package/dist/domain/reference.js +7 -0
- package/dist/domain/reference.js.map +1 -0
- package/dist/driver/driver.interface.d.ts +270 -0
- package/dist/driver/driver.interface.js +2 -0
- package/dist/driver/driver.interface.js.map +1 -0
- package/dist/driver/pg-driver.d.ts +43 -0
- package/dist/driver/pg-driver.js +255 -0
- package/dist/driver/pg-driver.js.map +1 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.js +16 -0
- package/dist/index.js.map +1 -0
- package/dist/migration/diff-calculator.d.ts +17 -0
- package/dist/migration/diff-calculator.js +230 -0
- package/dist/migration/diff-calculator.js.map +1 -0
- package/dist/migration/migrator.d.ts +18 -0
- package/dist/migration/migrator.js +233 -0
- package/dist/migration/migrator.js.map +1 -0
- package/dist/orm.d.ts +14 -0
- package/dist/orm.js +23 -0
- package/dist/orm.js.map +1 -0
- package/dist/orm.service.d.ts +8 -0
- package/dist/orm.service.js +115 -0
- package/dist/orm.service.js.map +1 -0
- package/dist/utils.d.ts +1 -0
- package/dist/utils.js +16 -0
- package/dist/utils.js.map +1 -0
- package/package.json +50 -0
- package/src/SqlBuilder.ts +542 -0
- package/src/cheetah.ts +28 -0
- package/src/constants.ts +4 -0
- package/src/decorators/entity.decorator.ts +10 -0
- package/src/decorators/index.decorator.ts +18 -0
- package/src/decorators/one-many.decorator.ts +19 -0
- package/src/decorators/primary-key.decorator.ts +6 -0
- package/src/decorators/property.decorator.ts +41 -0
- package/src/domain/base-entity.ts +149 -0
- package/src/domain/collection.ts +10 -0
- package/src/domain/entities.ts +159 -0
- package/src/domain/reference.ts +5 -0
- package/src/driver/driver.interface.ts +331 -0
- package/src/driver/pg-driver.ts +308 -0
- package/src/index.ts +17 -0
- package/src/migration/diff-calculator.ts +258 -0
- package/src/migration/migrator.ts +278 -0
- package/src/orm.service.ts +115 -0
- package/src/orm.ts +30 -0
- package/src/utils.ts +18 -0
- package/test/domain/base-entity.spec.ts +463 -0
- package/test/migration/.sql +5 -0
- package/test/migration/cheetah.config.ts +13 -0
- package/test/migration/migator.spec.ts +251 -0
- package/test/migration/test.sql +5 -0
- package/test/node-database.ts +32 -0
|
@@ -0,0 +1,270 @@
|
|
|
1
|
+
import { PropertyOptions } from '../decorators/property.decorator';
|
|
2
|
+
import { Collection } from '../domain/collection';
|
|
3
|
+
import { Reference } from '../domain/reference';
|
|
4
|
+
export interface DriverInterface {
|
|
5
|
+
connectionString: string;
|
|
6
|
+
executeStatement(statement: Statement<any>): Promise<{
|
|
7
|
+
query: any;
|
|
8
|
+
startTime: number;
|
|
9
|
+
sql: string;
|
|
10
|
+
}>;
|
|
11
|
+
connect(): Promise<void>;
|
|
12
|
+
disconnect(): Promise<void>;
|
|
13
|
+
executeSql(s: string): Promise<any>;
|
|
14
|
+
snapshot(tableName: string, options: any): Promise<SnapshotTable | undefined>;
|
|
15
|
+
getCreateTableInstruction(schema: string | undefined, tableName: string, creates: ColDiff[]): string;
|
|
16
|
+
getAlterTableFkInstruction(schema: string | undefined, tableName: string, colDiff: ColDiff, fk: ForeignKeyInfo): string;
|
|
17
|
+
getCreateIndex(index: {
|
|
18
|
+
name: string;
|
|
19
|
+
properties?: string[];
|
|
20
|
+
}, schema: string | undefined, tableName: string): string;
|
|
21
|
+
getAddColumn(schema: string | undefined, tableName: string, colName: string, colDiff: ColDiff, colDiffInstructions: string[]): void;
|
|
22
|
+
getDropColumn(colDiffInstructions: string[], schema: string | undefined, tableName: string, colName: string): void;
|
|
23
|
+
getDropIndex(index: {
|
|
24
|
+
name: string;
|
|
25
|
+
properties?: string[];
|
|
26
|
+
}, schema: string | undefined, tableName: string): string;
|
|
27
|
+
getAlterTableType(schema: string | undefined, tableName: string, colName: string, colDiff: ColDiff): string;
|
|
28
|
+
getAlterTableDefaultInstruction(schema: string | undefined, tableName: string, colName: string, colDiff: ColDiff): string;
|
|
29
|
+
getAlterTablePrimaryKeyInstruction(schema: string | undefined, tableName: string, colName: string, colDiff: ColDiff): string;
|
|
30
|
+
getDropConstraint(param: {
|
|
31
|
+
name: string;
|
|
32
|
+
}, schema: string | undefined, tableName: string): string;
|
|
33
|
+
getAddUniqueConstraint(schema: string | undefined, tableName: string, colName: string): string;
|
|
34
|
+
getAlterTableDropNullInstruction(schema: string | undefined, tableName: string, colName: string, colDiff: ColDiff): string;
|
|
35
|
+
getAlterTableDropNotNullInstruction(schema: string | undefined, tableName: string, colName: string, colDiff: ColDiff): string;
|
|
36
|
+
startTransaction(): Promise<void>;
|
|
37
|
+
commitTransaction(): Promise<void>;
|
|
38
|
+
rollbackTransaction(): Promise<void>;
|
|
39
|
+
}
|
|
40
|
+
export type SnapshotConstraintInfo = {
|
|
41
|
+
indexName: string;
|
|
42
|
+
consDef: string;
|
|
43
|
+
type: string;
|
|
44
|
+
};
|
|
45
|
+
export interface ConnectionSettings<T extends DriverInterface = DriverInterface> {
|
|
46
|
+
host?: string;
|
|
47
|
+
port?: number;
|
|
48
|
+
username?: string;
|
|
49
|
+
password?: string;
|
|
50
|
+
database?: string;
|
|
51
|
+
connectionString?: string;
|
|
52
|
+
ssl?: boolean;
|
|
53
|
+
driver: T;
|
|
54
|
+
entities?: Function[] | string;
|
|
55
|
+
migrationPath?: string;
|
|
56
|
+
}
|
|
57
|
+
export type ConditionOperators<T, C> = {
|
|
58
|
+
$ne?: T;
|
|
59
|
+
$in?: T[];
|
|
60
|
+
$nin?: T[];
|
|
61
|
+
$like?: string;
|
|
62
|
+
$gt?: T;
|
|
63
|
+
$gte?: T;
|
|
64
|
+
$lt?: T;
|
|
65
|
+
$lte?: T;
|
|
66
|
+
$and?: Condition<C>[];
|
|
67
|
+
$or?: Condition<C>[];
|
|
68
|
+
};
|
|
69
|
+
export type Condition<T> = {
|
|
70
|
+
[P in keyof T]?: T[P] | ConditionOperators<T[P], T>;
|
|
71
|
+
};
|
|
72
|
+
export type InstanceOf<T> = {
|
|
73
|
+
[key in keyof T]: T[key];
|
|
74
|
+
};
|
|
75
|
+
export type JoinStatement<T> = {
|
|
76
|
+
type: 'INNER' | 'LEFT' | 'RIGHT';
|
|
77
|
+
originalEntity?: Function;
|
|
78
|
+
originTable: string;
|
|
79
|
+
originSchema: string;
|
|
80
|
+
originAlias: string;
|
|
81
|
+
joinTable: string;
|
|
82
|
+
joinSchema: string;
|
|
83
|
+
joinAlias: string;
|
|
84
|
+
joinEntity?: Function;
|
|
85
|
+
joinWhere?: string;
|
|
86
|
+
joinProperty: string;
|
|
87
|
+
on: string;
|
|
88
|
+
propertyKey: string | symbol;
|
|
89
|
+
};
|
|
90
|
+
export type Statement<T> = {
|
|
91
|
+
statement?: 'select' | 'insert' | 'update' | 'delete';
|
|
92
|
+
table?: string;
|
|
93
|
+
alias?: string;
|
|
94
|
+
columns?: Array<keyof T>;
|
|
95
|
+
join?: JoinStatement<T>[];
|
|
96
|
+
where?: string;
|
|
97
|
+
values?: any;
|
|
98
|
+
groupBy?: string[];
|
|
99
|
+
orderBy?: string[];
|
|
100
|
+
limit?: number;
|
|
101
|
+
offset?: number;
|
|
102
|
+
};
|
|
103
|
+
export type SnapshotTable = {
|
|
104
|
+
tableName: string;
|
|
105
|
+
schema?: string;
|
|
106
|
+
columns: ColumnsInfo[];
|
|
107
|
+
indexes: SnapshotIndexInfo[];
|
|
108
|
+
foreignKeys?: ForeignKeyInfo[];
|
|
109
|
+
};
|
|
110
|
+
export type SnapshotIndexInfo = {
|
|
111
|
+
table: string;
|
|
112
|
+
indexName: string;
|
|
113
|
+
columnName: string;
|
|
114
|
+
};
|
|
115
|
+
export type ForeignKeyInfo = {
|
|
116
|
+
referencedTableName: string;
|
|
117
|
+
referencedColumnName: string;
|
|
118
|
+
};
|
|
119
|
+
export type ColumnsInfo = {
|
|
120
|
+
name: string;
|
|
121
|
+
type: string;
|
|
122
|
+
nullable?: boolean;
|
|
123
|
+
default?: string | null;
|
|
124
|
+
primary?: boolean;
|
|
125
|
+
unique?: boolean;
|
|
126
|
+
length?: number;
|
|
127
|
+
foreignKeys?: ForeignKeyInfo[];
|
|
128
|
+
};
|
|
129
|
+
export type SqlActionType = 'CREATE' | 'DELETE' | 'ALTER' | 'INDEX';
|
|
130
|
+
export type ColDiff = {
|
|
131
|
+
actionType: SqlActionType;
|
|
132
|
+
colName: string;
|
|
133
|
+
colType?: string;
|
|
134
|
+
colLength?: number;
|
|
135
|
+
indexTables?: {
|
|
136
|
+
name: string;
|
|
137
|
+
properties?: string[];
|
|
138
|
+
}[];
|
|
139
|
+
colChanges?: {
|
|
140
|
+
default?: string | null;
|
|
141
|
+
primary?: boolean;
|
|
142
|
+
unique?: boolean;
|
|
143
|
+
nullable?: boolean;
|
|
144
|
+
foreignKeys?: ForeignKeyInfo[];
|
|
145
|
+
};
|
|
146
|
+
};
|
|
147
|
+
export type TableDiff = {
|
|
148
|
+
tableName: string;
|
|
149
|
+
schema?: string;
|
|
150
|
+
newTable?: boolean;
|
|
151
|
+
colDiffs: ColDiff[];
|
|
152
|
+
};
|
|
153
|
+
export declare const PrimaryKeyType: unique symbol;
|
|
154
|
+
export declare const PrimaryKeyProp: unique symbol;
|
|
155
|
+
type ReadonlyPrimary<T> = T extends any[] ? Readonly<T> : T;
|
|
156
|
+
export type Primary<T> = T extends {
|
|
157
|
+
[PrimaryKeyType]?: infer PK;
|
|
158
|
+
} ? ReadonlyPrimary<PK> : T extends {
|
|
159
|
+
_id?: infer PK;
|
|
160
|
+
} ? ReadonlyPrimary<PK> | string : T extends {
|
|
161
|
+
uuid?: infer PK;
|
|
162
|
+
} ? ReadonlyPrimary<PK> : T extends {
|
|
163
|
+
id?: infer PK;
|
|
164
|
+
} ? ReadonlyPrimary<PK> : never;
|
|
165
|
+
export type PrimaryProperty<T> = T extends {
|
|
166
|
+
[PrimaryKeyProp]?: infer PK;
|
|
167
|
+
} ? PK : T extends {
|
|
168
|
+
_id?: any;
|
|
169
|
+
} ? '_id' | string : T extends {
|
|
170
|
+
uuid?: any;
|
|
171
|
+
} ? 'uuid' : T extends {
|
|
172
|
+
id?: any;
|
|
173
|
+
} ? 'id' : never;
|
|
174
|
+
export type IPrimaryKeyValue = number | string | bigint | Date | {
|
|
175
|
+
toHexString(): string;
|
|
176
|
+
};
|
|
177
|
+
export type IPrimaryKey<T extends IPrimaryKeyValue = IPrimaryKeyValue> = T;
|
|
178
|
+
export type OperatorMap<T> = {
|
|
179
|
+
$and?: Query<T>[];
|
|
180
|
+
$or?: Query<T>[];
|
|
181
|
+
$eq?: ExpandScalar<T> | ExpandScalar<T>[];
|
|
182
|
+
$ne?: ExpandScalar<T>;
|
|
183
|
+
$in?: ExpandScalar<T>[];
|
|
184
|
+
$nin?: ExpandScalar<T>[];
|
|
185
|
+
$not?: Query<T>;
|
|
186
|
+
$gt?: ExpandScalar<T>;
|
|
187
|
+
$gte?: ExpandScalar<T>;
|
|
188
|
+
$lt?: ExpandScalar<T>;
|
|
189
|
+
$lte?: ExpandScalar<T>;
|
|
190
|
+
$like?: string;
|
|
191
|
+
};
|
|
192
|
+
export type ExcludeFunctions<T, K extends keyof T> = T[K] extends Function ? never : (K extends symbol ? never : K);
|
|
193
|
+
export type Scalar = boolean | number | string | bigint | symbol | Date | RegExp | Uint8Array | {
|
|
194
|
+
toHexString(): string;
|
|
195
|
+
};
|
|
196
|
+
export type ExpandProperty<T> = T extends (infer U)[] ? NonNullable<U> : NonNullable<T>;
|
|
197
|
+
export type ExpandScalar<T> = null | (T extends string ? T | RegExp : T extends Date ? Date | string : T);
|
|
198
|
+
type ExpandObject<T> = T extends object ? T extends Scalar ? never : {
|
|
199
|
+
-readonly [K in keyof T as ExcludeFunctions<T, K>]?: Query<ExpandProperty<T[K]>> | FilterValue<ExpandProperty<T[K]>> | null;
|
|
200
|
+
} : never;
|
|
201
|
+
export type EntityProps<T> = {
|
|
202
|
+
-readonly [K in keyof T as ExcludeFunctions<T, K>]?: T[K];
|
|
203
|
+
};
|
|
204
|
+
export type Query<T> = T extends object ? T extends Scalar ? never : FilterQuery<T> : FilterValue<T>;
|
|
205
|
+
export type FilterValue2<T> = T | ExpandScalar<T> | Primary<T>;
|
|
206
|
+
export type FilterValue<T> = OperatorMap<FilterValue2<T>> | FilterValue2<T> | FilterValue2<T>[] | null;
|
|
207
|
+
export type EntityClass<T> = Function & {
|
|
208
|
+
prototype: T;
|
|
209
|
+
};
|
|
210
|
+
export type EntityName<T> = string | EntityClass<T> | {
|
|
211
|
+
name: string;
|
|
212
|
+
};
|
|
213
|
+
export type ObjectQuery<T> = ExpandObject<T> & OperatorMap<T>;
|
|
214
|
+
export type FilterQuery<T> = ObjectQuery<T> | NonNullable<ExpandScalar<Primary<T>>> | NonNullable<EntityProps<T> & OperatorMap<T>> | FilterQuery<T>[];
|
|
215
|
+
export type Relationship<T> = {
|
|
216
|
+
isRelation?: boolean;
|
|
217
|
+
relation: 'one-to-many' | 'many-to-one';
|
|
218
|
+
type: Function;
|
|
219
|
+
fkKey?: (string & keyof T) | ((e: T) => any);
|
|
220
|
+
entity: () => EntityName<T>;
|
|
221
|
+
originalEntity?: EntityName<T>;
|
|
222
|
+
propertyKey: string | symbol;
|
|
223
|
+
} & Partial<PropertyOptions>;
|
|
224
|
+
export type Cast<T, R> = T extends R ? T : R;
|
|
225
|
+
export type IsUnknown<T> = T extends unknown ? unknown extends T ? true : never : never;
|
|
226
|
+
export type IdentifiedReference<T, PK extends keyof T | unknown = PrimaryProperty<T>> = true extends IsUnknown<PK> ? Reference<T> : ({
|
|
227
|
+
[K in Cast<PK, keyof T>]: T[K];
|
|
228
|
+
} & Reference<T>);
|
|
229
|
+
export type Ref<T, PK extends keyof T | unknown = PrimaryProperty<T>> = IdentifiedReference<T, PK>;
|
|
230
|
+
type Loadable<T extends object> = Collection<T, any> | Reference<T> | readonly T[];
|
|
231
|
+
type ExtractType<T> = T extends Loadable<infer U> ? U : T;
|
|
232
|
+
type StringKeys<T, E extends string = never> = T extends Collection<any, any> ? `${Exclude<keyof ExtractType<T> | E, symbol>}` : T extends Ref<any> ? `${Exclude<keyof ExtractType<T> | E, symbol>}` : T extends object ? `${Exclude<keyof ExtractType<T> | E, symbol>}` : never;
|
|
233
|
+
type Defined<T> = Exclude<T, null | undefined>;
|
|
234
|
+
type GetStringKey<T, K extends StringKeys<T, string>, E extends string> = K extends keyof T ? ExtractType<T[K]> : (K extends E ? keyof T : never);
|
|
235
|
+
type Prev = [never, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
|
|
236
|
+
export type AutoPath<O, P extends string, E extends string = never, D extends Prev[number] = 5> = [D] extends [never] ? string : P extends any ? (P & `${string}.` extends never ? P : P & `${string}.`) extends infer Q ? Q extends `${infer A}.${infer B}` ? A extends StringKeys<O, E> ? `${A}.${AutoPath<Defined<GetStringKey<O, A, E>>, B, E, Prev[D]>}` : never : Q extends StringKeys<O, E> ? (Defined<GetStringKey<O, Q, E>> extends unknown ? Exclude<P, `${string}.`> : never) | (StringKeys<Defined<GetStringKey<O, Q, E>>, E> extends never ? never : `${Q}.`) : keyof ExtractType<O> : never : never;
|
|
237
|
+
export declare enum QueryOrder {
|
|
238
|
+
ASC = "ASC",
|
|
239
|
+
ASC_NULLS_LAST = "ASC NULLS LAST",
|
|
240
|
+
ASC_NULLS_FIRST = "ASC NULLS FIRST",
|
|
241
|
+
DESC = "DESC",
|
|
242
|
+
DESC_NULLS_LAST = "DESC NULLS LAST",
|
|
243
|
+
DESC_NULLS_FIRST = "DESC NULLS FIRST",
|
|
244
|
+
asc = "asc",
|
|
245
|
+
asc_nulls_last = "asc nulls last",
|
|
246
|
+
asc_nulls_first = "asc nulls first",
|
|
247
|
+
desc = "desc",
|
|
248
|
+
desc_nulls_last = "desc nulls last",
|
|
249
|
+
desc_nulls_first = "desc nulls first"
|
|
250
|
+
}
|
|
251
|
+
export declare enum QueryOrderNumeric {
|
|
252
|
+
ASC = 1,
|
|
253
|
+
DESC = -1
|
|
254
|
+
}
|
|
255
|
+
export type QueryOrderKeysFlat = QueryOrder | QueryOrderNumeric | keyof typeof QueryOrder;
|
|
256
|
+
export type QueryOrderKeys<T> = QueryOrderKeysFlat | QueryOrderMap<T>;
|
|
257
|
+
export type QueryOrderMap<T> = {
|
|
258
|
+
[K in keyof T as ExcludeFunctions<T, K>]?: QueryOrderKeys<ExpandProperty<T[K]>>;
|
|
259
|
+
};
|
|
260
|
+
export type EntityField<T, P extends string = never> = AutoPath<T, P, '*'>;
|
|
261
|
+
export interface FindOptions<T, P extends string = never> {
|
|
262
|
+
orderBy?: (QueryOrderMap<T> & {
|
|
263
|
+
0?: never;
|
|
264
|
+
}) | QueryOrderMap<T>[];
|
|
265
|
+
limit?: number;
|
|
266
|
+
offset?: number;
|
|
267
|
+
fields?: readonly EntityField<T, P>[];
|
|
268
|
+
}
|
|
269
|
+
export type FindOneOption<T, P extends string = never> = Omit<FindOptions<T, P>, 'limit' | 'offset'>;
|
|
270
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"driver.interface.js","sourceRoot":"","sources":["../../src/driver/driver.interface.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { ColDiff, ConnectionSettings, DriverInterface, ForeignKeyInfo, SnapshotConstraintInfo, SnapshotIndexInfo, SnapshotTable, Statement } from './driver.interface';
|
|
2
|
+
export declare class PgDriver implements DriverInterface {
|
|
3
|
+
connectionString: string;
|
|
4
|
+
private client;
|
|
5
|
+
constructor(options: ConnectionSettings);
|
|
6
|
+
getCreateTableInstruction(schema: string | undefined, tableName: string, creates: ColDiff[]): string;
|
|
7
|
+
getAlterTableFkInstruction(schema: string | undefined, tableName: string, colDiff: ColDiff, fk: ForeignKeyInfo): string;
|
|
8
|
+
getCreateIndex(index: {
|
|
9
|
+
name: string;
|
|
10
|
+
properties: string[];
|
|
11
|
+
}, schema: string | undefined, tableName: string): string;
|
|
12
|
+
getAddColumn(schema: string | undefined, tableName: string, colName: string, colDiff: ColDiff, colDiffInstructions: string[]): void;
|
|
13
|
+
getDropColumn(colDiffInstructions: string[], schema: string | undefined, tableName: string, colName: string): void;
|
|
14
|
+
getDropIndex(index: {
|
|
15
|
+
name: string;
|
|
16
|
+
properties?: string[];
|
|
17
|
+
}, schema: string | undefined, tableName: string): string;
|
|
18
|
+
getAlterTableType(schema: string | undefined, tableName: string, colName: string, colDiff: ColDiff): string;
|
|
19
|
+
getAlterTableDefaultInstruction(schema: string | undefined, tableName: string, colName: string, colDiff: ColDiff): string;
|
|
20
|
+
getAlterTablePrimaryKeyInstruction(schema: string | undefined, tableName: string, colName: string, colDiff: ColDiff): string;
|
|
21
|
+
getDropConstraint(param: {
|
|
22
|
+
name: string;
|
|
23
|
+
}, schema: string | undefined, tableName: string): string;
|
|
24
|
+
getAddUniqueConstraint(schema: string | undefined, tableName: string, colName: string): string;
|
|
25
|
+
getAlterTableDropNullInstruction(schema: string | undefined, tableName: string, colName: string, colDiff: ColDiff): string;
|
|
26
|
+
getAlterTableDropNotNullInstruction(schema: string | undefined, tableName: string, colName: string, colDiff: ColDiff): string;
|
|
27
|
+
startTransaction(): Promise<void>;
|
|
28
|
+
commitTransaction(): Promise<void>;
|
|
29
|
+
rollbackTransaction(): Promise<void>;
|
|
30
|
+
executeStatement(statement: Statement<any>): Promise<{
|
|
31
|
+
query: any;
|
|
32
|
+
startTime: number;
|
|
33
|
+
sql: string;
|
|
34
|
+
}>;
|
|
35
|
+
connect(): Promise<void>;
|
|
36
|
+
disconnect(): Promise<void>;
|
|
37
|
+
executeSql(s: string): Promise<import("pg").QueryResult<any>>;
|
|
38
|
+
snapshot(tableName: string, options: any): Promise<SnapshotTable | undefined>;
|
|
39
|
+
private getForeignKeys;
|
|
40
|
+
index(tableName: string, options: any): Promise<SnapshotIndexInfo[] | undefined>;
|
|
41
|
+
constraints(tableName: string, options: any): Promise<SnapshotConstraintInfo[] | undefined>;
|
|
42
|
+
private toDatabaseValue;
|
|
43
|
+
}
|
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
import { Client } from 'pg';
|
|
2
|
+
export class PgDriver {
|
|
3
|
+
constructor(options) {
|
|
4
|
+
if (options.connectionString) {
|
|
5
|
+
this.connectionString = options.connectionString;
|
|
6
|
+
}
|
|
7
|
+
else {
|
|
8
|
+
const { host, port, username, password, database } = options;
|
|
9
|
+
this.connectionString = `postgres://${username}:${password}@${host}:${port}/${database}`;
|
|
10
|
+
}
|
|
11
|
+
this.client = new Client({
|
|
12
|
+
connectionString: this.connectionString,
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
getCreateTableInstruction(schema, tableName, creates) {
|
|
16
|
+
return `CREATE TABLE "${schema}"."${tableName}" (${creates.map(colDiff => {
|
|
17
|
+
let sql = `"${colDiff.colName}" ${colDiff.colType}${(colDiff.colLength ? `(${colDiff.colLength})` : '')}`;
|
|
18
|
+
if (!colDiff.colChanges?.nullable) {
|
|
19
|
+
sql += ' NOT NULL';
|
|
20
|
+
}
|
|
21
|
+
if (colDiff.colChanges?.primary) {
|
|
22
|
+
sql += ' PRIMARY KEY';
|
|
23
|
+
}
|
|
24
|
+
if (colDiff.colChanges?.unique) {
|
|
25
|
+
sql += ' UNIQUE';
|
|
26
|
+
}
|
|
27
|
+
if (colDiff.colChanges?.default) {
|
|
28
|
+
sql += ` DEFAULT ${colDiff.colChanges.default}`;
|
|
29
|
+
}
|
|
30
|
+
return sql;
|
|
31
|
+
})});`;
|
|
32
|
+
}
|
|
33
|
+
getAlterTableFkInstruction(schema, tableName, colDiff, fk) {
|
|
34
|
+
return `ALTER TABLE "${schema}"."${tableName}" ADD CONSTRAINT "${tableName}_${colDiff.colName}_fk" FOREIGN KEY ("${colDiff.colName}") REFERENCES "${fk.referencedTableName}" ("${fk.referencedColumnName}");`;
|
|
35
|
+
}
|
|
36
|
+
getCreateIndex(index, schema, tableName) {
|
|
37
|
+
return `CREATE INDEX "${index.name}" ON "${schema}"."${tableName}" (${index.properties.map(prop => `"${prop}"`).join(', ')});`;
|
|
38
|
+
}
|
|
39
|
+
getAddColumn(schema, tableName, colName, colDiff, colDiffInstructions) {
|
|
40
|
+
let sql = `ALTER TABLE "${schema}"."${tableName}" ADD COLUMN "${colName}" ${colDiff.colType}${(colDiff.colLength ? `(${colDiff.colLength})` : '')}`;
|
|
41
|
+
if (!colDiff.colChanges?.nullable) {
|
|
42
|
+
sql += ' NOT NULL';
|
|
43
|
+
}
|
|
44
|
+
if (colDiff.colChanges?.primary) {
|
|
45
|
+
sql += ' PRIMARY KEY';
|
|
46
|
+
}
|
|
47
|
+
if (colDiff.colChanges?.unique) {
|
|
48
|
+
sql += ' UNIQUE';
|
|
49
|
+
}
|
|
50
|
+
if (colDiff.colChanges?.default) {
|
|
51
|
+
sql += ` DEFAULT ${colDiff.colChanges.default}`;
|
|
52
|
+
}
|
|
53
|
+
colDiffInstructions.push(sql.concat(';'));
|
|
54
|
+
if (colDiff.colChanges?.foreignKeys) {
|
|
55
|
+
colDiff.colChanges.foreignKeys.forEach(fk => {
|
|
56
|
+
colDiffInstructions.push(`ALTER TABLE "${schema}"."${tableName}" ADD CONSTRAINT "${tableName}_${colName}_fk" FOREIGN KEY ("${colName}") REFERENCES "${fk.referencedTableName}" ("${fk.referencedColumnName}");`);
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
getDropColumn(colDiffInstructions, schema, tableName, colName) {
|
|
61
|
+
colDiffInstructions.push(`ALTER TABLE "${schema}"."${tableName}" DROP COLUMN IF EXISTS "${colName}";`);
|
|
62
|
+
}
|
|
63
|
+
getDropIndex(index, schema, tableName) {
|
|
64
|
+
return `DROP INDEX "${index.name}" ON "${schema}"."${tableName}";`;
|
|
65
|
+
}
|
|
66
|
+
getAlterTableType(schema, tableName, colName, colDiff) {
|
|
67
|
+
return `ALTER TABLE "${schema}"."${tableName}" ALTER COLUMN "${colName}" TYPE ${colDiff.colType}${(colDiff.colLength ? `(${colDiff.colLength})` : '')};`;
|
|
68
|
+
}
|
|
69
|
+
getAlterTableDefaultInstruction(schema, tableName, colName, colDiff) {
|
|
70
|
+
return `ALTER TABLE "${schema}"."${tableName}" ALTER COLUMN "${colName}" SET DEFAULT ${colDiff.colChanges.default};`;
|
|
71
|
+
}
|
|
72
|
+
getAlterTablePrimaryKeyInstruction(schema, tableName, colName, colDiff) {
|
|
73
|
+
return `ALTER TABLE "${schema}"."${tableName}" ADD PRIMARY KEY ("${colName}");`;
|
|
74
|
+
}
|
|
75
|
+
getDropConstraint(param, schema, tableName) {
|
|
76
|
+
return `ALTER TABLE "${schema}"."${tableName}" DROP CONSTRAINT "${param.name}";`;
|
|
77
|
+
}
|
|
78
|
+
getAddUniqueConstraint(schema, tableName, colName) {
|
|
79
|
+
return `ALTER TABLE "${schema}"."${tableName}" ADD UNIQUE ("${colName}");`;
|
|
80
|
+
}
|
|
81
|
+
getAlterTableDropNullInstruction(schema, tableName, colName, colDiff) {
|
|
82
|
+
return `ALTER TABLE "${schema}"."${tableName}" ALTER COLUMN "${colName}" DROP NOT NULL;`;
|
|
83
|
+
}
|
|
84
|
+
getAlterTableDropNotNullInstruction(schema, tableName, colName, colDiff) {
|
|
85
|
+
return `ALTER TABLE "${schema}"."${tableName}" ALTER COLUMN "${colName}" SET NOT NULL;`;
|
|
86
|
+
}
|
|
87
|
+
async startTransaction() {
|
|
88
|
+
await this.client.query('BEGIN;');
|
|
89
|
+
}
|
|
90
|
+
async commitTransaction() {
|
|
91
|
+
await this.client.query('COMMIT;');
|
|
92
|
+
}
|
|
93
|
+
async rollbackTransaction() {
|
|
94
|
+
await this.client.query('ROLLBACK;');
|
|
95
|
+
}
|
|
96
|
+
async executeStatement(statement) {
|
|
97
|
+
let { statement: statementType, table, columns, where, limit, alias } = statement;
|
|
98
|
+
let sql = '';
|
|
99
|
+
switch (statementType) {
|
|
100
|
+
case 'select':
|
|
101
|
+
sql = `SELECT ${columns ? columns.join(', ') : '*'} FROM ${table} ${alias}`;
|
|
102
|
+
break;
|
|
103
|
+
case 'insert': // TODO: Tratar corretamente os valores string, number
|
|
104
|
+
const fields = Object.keys(statement.values).map(v => `"${v}"`).join(', ');
|
|
105
|
+
const values = Object.values(statement.values).map(value => this.toDatabaseValue(value)).join(', ');
|
|
106
|
+
sql = `INSERT INTO ${table} (${fields}) VALUES (${values}) RETURNING ${statement.columns.join(', ').replaceAll(`${alias}.`, '')}`;
|
|
107
|
+
break;
|
|
108
|
+
case 'update':
|
|
109
|
+
sql = `UPDATE ${table} as ${alias} SET ${Object.entries(statement.values).map(([key, value]) => `${key} = '${value}'`).join(', ')}`;
|
|
110
|
+
break;
|
|
111
|
+
case 'delete':
|
|
112
|
+
break;
|
|
113
|
+
}
|
|
114
|
+
if (statement.join) {
|
|
115
|
+
statement.join.forEach(join => {
|
|
116
|
+
sql += ` ${join.type} JOIN ${join.joinSchema}.${join.joinTable} ${join.joinAlias} ON ${join.on}`;
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
if (statementType !== 'insert') {
|
|
120
|
+
if (where) {
|
|
121
|
+
sql += ` WHERE ${where}`;
|
|
122
|
+
}
|
|
123
|
+
if (statement.orderBy) {
|
|
124
|
+
sql += ` ORDER BY ${statement.orderBy.join(', ')}`;
|
|
125
|
+
}
|
|
126
|
+
if (statement.offset) {
|
|
127
|
+
sql += ` OFFSET ${statement.offset}`;
|
|
128
|
+
}
|
|
129
|
+
if (limit) {
|
|
130
|
+
sql += ` LIMIT ${statement.limit}`;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
const startTime = Date.now();
|
|
134
|
+
return {
|
|
135
|
+
query: await this.client.query(sql),
|
|
136
|
+
startTime,
|
|
137
|
+
sql
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
connect() {
|
|
141
|
+
return this.client.connect();
|
|
142
|
+
}
|
|
143
|
+
disconnect() {
|
|
144
|
+
return this.client.end();
|
|
145
|
+
}
|
|
146
|
+
executeSql(s) {
|
|
147
|
+
return this.client.query(s);
|
|
148
|
+
}
|
|
149
|
+
async snapshot(tableName, options) {
|
|
150
|
+
const schema = (options && options.schema) || 'public';
|
|
151
|
+
const sql = `SELECT * FROM information_schema.columns WHERE table_name = '${tableName}' AND table_schema = '${schema}'`;
|
|
152
|
+
const result = await this.client.query(sql);
|
|
153
|
+
if (!result.rows || result.rows.length === 0) {
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
const indexes = await this.index(tableName, options) || [];
|
|
157
|
+
const constraints = await this.constraints(tableName, options) || [];
|
|
158
|
+
return {
|
|
159
|
+
tableName,
|
|
160
|
+
schema,
|
|
161
|
+
indexes,
|
|
162
|
+
columns: result.rows.map(row => {
|
|
163
|
+
// console.log(this.getForeignKeys(constraints, row), row.column_name)
|
|
164
|
+
return {
|
|
165
|
+
default: row.column_default,
|
|
166
|
+
length: row.character_maximum_length || row.numeric_precision || row.datetime_precision,
|
|
167
|
+
name: row.column_name,
|
|
168
|
+
nullable: row.is_nullable === 'YES',
|
|
169
|
+
primary: constraints.some(c => c.type === 'PRIMARY KEY' && c.consDef.includes(row.column_name)),
|
|
170
|
+
unique: constraints.some(c => (c.type === 'UNIQUE' || c.type === 'PRIMARY KEY') && c.consDef.includes(row.column_name)),
|
|
171
|
+
type: row.data_type,
|
|
172
|
+
foreignKeys: this.getForeignKeys(constraints, row)
|
|
173
|
+
};
|
|
174
|
+
})
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
getForeignKeys(constraints, row) {
|
|
178
|
+
const name = row.column_name;
|
|
179
|
+
return constraints.filter(c => c.type === 'FOREIGN KEY' && c.consDef.match(new RegExp(`FOREIGN KEY \\("${row.column_name}"\\)`))).map(c => {
|
|
180
|
+
const filter = c.consDef.match(/REFERENCES\s+"([^"]+)"\s*\(([^)]+)\)/);
|
|
181
|
+
if (!filter)
|
|
182
|
+
throw new Error('Invalid constraint definition');
|
|
183
|
+
return {
|
|
184
|
+
referencedColumnName: filter[2].split(',')[0].trim(),
|
|
185
|
+
referencedTableName: filter[1],
|
|
186
|
+
};
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
async index(tableName, options) {
|
|
190
|
+
const schema = (options && options.schema) || 'public';
|
|
191
|
+
const result = await this.client.query(`SELECT indexname AS index_name, indexdef AS column_name, tablename AS table_name
|
|
192
|
+
FROM pg_indexes
|
|
193
|
+
WHERE tablename = '${tableName}' AND schemaname = '${schema}'`);
|
|
194
|
+
return result.rows.map(row => {
|
|
195
|
+
return {
|
|
196
|
+
table: tableName,
|
|
197
|
+
indexName: row.index_name,
|
|
198
|
+
columnName: row.column_name
|
|
199
|
+
};
|
|
200
|
+
});
|
|
201
|
+
}
|
|
202
|
+
async constraints(tableName, options) {
|
|
203
|
+
const schema = (options && options.schema) || 'public';
|
|
204
|
+
const result = await this.client.query(`SELECT
|
|
205
|
+
conname AS index_name,
|
|
206
|
+
pg_get_constraintdef(pg_constraint.oid) as consdef,
|
|
207
|
+
CASE contype
|
|
208
|
+
WHEN 'c' THEN 'CHECK'
|
|
209
|
+
WHEN 'f' THEN 'FOREIGN KEY'
|
|
210
|
+
WHEN 'p' THEN 'PRIMARY KEY'
|
|
211
|
+
WHEN 'u' THEN 'UNIQUE'
|
|
212
|
+
WHEN 't' THEN 'TRIGGER'
|
|
213
|
+
WHEN 'x' THEN 'EXCLUSION'
|
|
214
|
+
ELSE 'UNKNOWN'
|
|
215
|
+
END AS constraint_type
|
|
216
|
+
FROM pg_constraint
|
|
217
|
+
where conrelid = (
|
|
218
|
+
SELECT oid
|
|
219
|
+
FROM pg_class
|
|
220
|
+
WHERE relname = '${tableName}'
|
|
221
|
+
AND relnamespace = (SELECT oid FROM pg_namespace WHERE nspname = '${schema}')
|
|
222
|
+
)
|
|
223
|
+
AND conkey @> ARRAY(
|
|
224
|
+
SELECT attnum
|
|
225
|
+
FROM pg_attribute
|
|
226
|
+
WHERE attrelid = conrelid
|
|
227
|
+
AND attname = '${schema}'
|
|
228
|
+
)`);
|
|
229
|
+
return result.rows.map(row => {
|
|
230
|
+
return {
|
|
231
|
+
indexName: row.index_name,
|
|
232
|
+
type: row.constraint_type,
|
|
233
|
+
consDef: row.consdef
|
|
234
|
+
};
|
|
235
|
+
});
|
|
236
|
+
}
|
|
237
|
+
toDatabaseValue(value) {
|
|
238
|
+
if (value instanceof Date) {
|
|
239
|
+
return `'${value.toISOString()}'`;
|
|
240
|
+
}
|
|
241
|
+
switch (typeof value) {
|
|
242
|
+
case 'string':
|
|
243
|
+
return `'${value}'`;
|
|
244
|
+
case 'number':
|
|
245
|
+
return value;
|
|
246
|
+
case 'boolean':
|
|
247
|
+
return value;
|
|
248
|
+
case 'object':
|
|
249
|
+
return `'${JSON.stringify(value)}'`;
|
|
250
|
+
default:
|
|
251
|
+
return `'${value}'`;
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
//# sourceMappingURL=pg-driver.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pg-driver.js","sourceRoot":"","sources":["../../src/driver/pg-driver.ts"],"names":[],"mappings":"AASA,OAAO,EAAE,MAAM,EAAE,MAAM,IAAI,CAAA;AAE3B,MAAM,OAAO,QAAQ;IAKnB,YAAY,OAA2B;QACrC,IAAI,OAAO,CAAC,gBAAgB,EAAE;YAC5B,IAAI,CAAC,gBAAgB,GAAG,OAAO,CAAC,gBAAgB,CAAC;SAClD;aAAM;YACL,MAAM,EAAC,IAAI,EAAE,IAAI,EAAE,QAAQ,EAAE,QAAQ,EAAE,QAAQ,EAAC,GAAG,OAAO,CAAC;YAC3D,IAAI,CAAC,gBAAgB,GAAG,cAAc,QAAQ,IAAI,QAAQ,IAAI,IAAI,IAAI,IAAI,IAAI,QAAQ,EAAE,CAAC;SAC1F;QAED,IAAI,CAAC,MAAM,GAAG,IAAI,MAAM,CAAC;YACvB,gBAAgB,EAAE,IAAI,CAAC,gBAAgB;SACxC,CAAC,CAAA;IACJ,CAAC;IAED,yBAAyB,CAAC,MAA0B,EAAE,SAAiB,EAAE,OAAkB;QACzF,OAAO,iBAAiB,MAAM,MAAM,SAAS,MAAM,OAAO,CAAC,GAAG,CAAC,OAAO,CAAC,EAAE;YACvE,IAAI,GAAG,GAAG,IAAI,OAAO,CAAC,OAAO,KAAK,OAAO,CAAC,OAAO,GAAG,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC,CAAC,IAAI,OAAO,CAAC,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SAAS,CAAC,KAAK,CAAC,GAAG,CAAC;YACtC;gBACE,OAAO,IAAI,KAAK,GAAG,CAAC;SACvB;IACH,CAAC;CACF"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Cheetah } from '@cheetah.js/core';
|
|
2
|
+
export * from './decorators/entity.decorator';
|
|
3
|
+
export * from './decorators/property.decorator';
|
|
4
|
+
export * from './decorators/primary-key.decorator';
|
|
5
|
+
export * from './decorators/one-many.decorator';
|
|
6
|
+
export * from './decorators/index.decorator';
|
|
7
|
+
export * from './orm';
|
|
8
|
+
export * from './orm.service';
|
|
9
|
+
export * from './domain/base-entity';
|
|
10
|
+
export * from './driver/pg-driver';
|
|
11
|
+
export * from './utils';
|
|
12
|
+
export * from './driver/driver.interface';
|
|
13
|
+
export declare const CheetahOrm: Cheetah;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { Cheetah } from '@cheetah.js/core';
|
|
2
|
+
import { OrmService } from '@cheetah.js/orm/orm.service';
|
|
3
|
+
import { EntityStorage } from '@cheetah.js/orm/domain/entities';
|
|
4
|
+
export * from './decorators/entity.decorator';
|
|
5
|
+
export * from './decorators/property.decorator';
|
|
6
|
+
export * from './decorators/primary-key.decorator';
|
|
7
|
+
export * from './decorators/one-many.decorator';
|
|
8
|
+
export * from './decorators/index.decorator';
|
|
9
|
+
export * from './orm';
|
|
10
|
+
export * from './orm.service';
|
|
11
|
+
export * from './domain/base-entity';
|
|
12
|
+
export * from './driver/pg-driver';
|
|
13
|
+
export * from './utils';
|
|
14
|
+
export * from './driver/driver.interface';
|
|
15
|
+
export const CheetahOrm = new Cheetah({ exports: [OrmService, EntityStorage] });
|
|
16
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,kBAAkB,CAAC;AAC3C,OAAO,EAAE,UAAU,EAAE,MAAM,6BAA6B,CAAC;AACzD,OAAO,EAAE,aAAa,EAAE,MAAM,iCAAiC,CAAC;AAEhE,cAAc,+BAA+B,CAAC;AAC9C,cAAc,iCAAiC,CAAC;AAChD,cAAc,oCAAoC,CAAC;AACnD,cAAc,iCAAiC,CAAC;AAChD,cAAc,8BAA8B,CAAC;AAC7C,cAAc,OAAO,CAAA;AACrB,cAAc,eAAe,CAAA;AAC7B,cAAc,sBAAsB,CAAA;AACpC,cAAc,oBAAoB,CAAA;AAClC,cAAc,SAAS,CAAA;AACvB,cAAc,2BAA2B,CAAA;AAEzC,MAAM,CAAC,MAAM,UAAU,GAAG,IAAI,OAAO,CAAC,EAAC,OAAO,EAAE,CAAC,UAAU,EAAE,aAAa,CAAC,EAAC,CAAC,CAAA"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { EntityStorage } from '../domain/entities';
|
|
2
|
+
import { SnapshotTable, TableDiff } from '@cheetah.js/orm/driver/driver.interface';
|
|
3
|
+
export declare class DiffCalculator {
|
|
4
|
+
private entities;
|
|
5
|
+
constructor(entities: EntityStorage);
|
|
6
|
+
diff(snapshotBd: SnapshotTable[], snapshotEntities: SnapshotTable[]): TableDiff[];
|
|
7
|
+
private checkIndexes;
|
|
8
|
+
private createNewColumn;
|
|
9
|
+
private diffColumnType;
|
|
10
|
+
private diffColumnDefault;
|
|
11
|
+
private diffColumnPrimary;
|
|
12
|
+
private diffColumnUnique;
|
|
13
|
+
private diffForeignKey;
|
|
14
|
+
private diffColumnSql;
|
|
15
|
+
private diffColumnNullable;
|
|
16
|
+
private convertEntityTypeToSqlType;
|
|
17
|
+
}
|