rake-db 2.6.0 → 2.7.1
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/dist/index.d.ts +166 -36
- package/dist/index.js +50 -23
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +53 -25
- package/dist/index.mjs.map +1 -1
- package/package.json +4 -4
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
1
|
+
import * as pqb from 'pqb';
|
|
2
|
+
import { ColumnsShape, Db as Db$1, ColumnType, EnumColumn, Adapter, DefaultColumnTypes, DbResult, TransactionAdapter, QueryLogObject, IndexColumnOptions, IndexOptions, ForeignKeyOptions, TextColumn, NoPrimaryKeyOption, TableData, SingleColumnIndexOptions, AdapterOptions, QueryLogOptions } from 'pqb';
|
|
3
|
+
import * as orchid_core from 'orchid-core';
|
|
4
|
+
import { ColumnTypesBase, EmptyObject, RawExpression, raw, MaybeArray } from 'orchid-core';
|
|
3
5
|
|
|
4
6
|
type CreateTableResult<Table extends string, Shape extends ColumnsShape> = {
|
|
5
7
|
table: Db$1<Table, Shape>;
|
|
@@ -29,7 +31,7 @@ declare const tableChangeMethods: {
|
|
|
29
31
|
rename(name: string): RakeDbAst.ChangeTableItem.Rename;
|
|
30
32
|
enum(this: ColumnTypesBase, name: string): EnumColumn<string, [string, ...string[]]>;
|
|
31
33
|
};
|
|
32
|
-
type TableChanger = MigrationColumnTypes & TableChangeMethods;
|
|
34
|
+
type TableChanger<CT extends ColumnTypesBase> = MigrationColumnTypes<CT> & TableChangeMethods;
|
|
33
35
|
type TableChangeData = Record<string, RakeDbAst.ChangeTableItem.Column | RakeDbAst.ChangeTableItem.Rename | Change | EmptyObject>;
|
|
34
36
|
|
|
35
37
|
type DropMode = 'CASCADE' | 'RESTRICT';
|
|
@@ -40,20 +42,20 @@ type TableOptions = {
|
|
|
40
42
|
snakeCase?: boolean;
|
|
41
43
|
};
|
|
42
44
|
type TextColumnCreator = () => TextColumn;
|
|
43
|
-
type MigrationColumnTypes = Omit<
|
|
45
|
+
type MigrationColumnTypes<CT extends ColumnTypesBase> = Omit<CT, 'text' | 'string' | 'enum'> & {
|
|
44
46
|
text: TextColumnCreator;
|
|
45
47
|
string: TextColumnCreator;
|
|
46
48
|
citext: TextColumnCreator;
|
|
47
49
|
enum: (name: string) => EnumColumn;
|
|
48
50
|
};
|
|
49
|
-
type ColumnsShapeCallback<Shape extends ColumnsShape = ColumnsShape> = (t: MigrationColumnTypes & {
|
|
51
|
+
type ColumnsShapeCallback<CT extends ColumnTypesBase, Shape extends ColumnsShape = ColumnsShape> = (t: MigrationColumnTypes<CT> & {
|
|
50
52
|
raw: typeof raw;
|
|
51
53
|
}) => Shape;
|
|
52
54
|
type ChangeTableOptions = {
|
|
53
55
|
snakeCase?: boolean;
|
|
54
56
|
comment?: string | [string, string] | null;
|
|
55
57
|
};
|
|
56
|
-
type ChangeTableCallback = (t: TableChanger) => TableChangeData;
|
|
58
|
+
type ChangeTableCallback<CT extends ColumnTypesBase> = (t: TableChanger<CT>) => TableChangeData;
|
|
57
59
|
type ColumnComment = {
|
|
58
60
|
column: string;
|
|
59
61
|
comment: string | null;
|
|
@@ -62,7 +64,7 @@ type SilentQueries = {
|
|
|
62
64
|
silentQuery: Adapter['query'];
|
|
63
65
|
silentArrays: Adapter['arrays'];
|
|
64
66
|
};
|
|
65
|
-
type Migration = DbResult<
|
|
67
|
+
type Migration<CT extends ColumnTypesBase = DefaultColumnTypes> = DbResult<CT> & MigrationBase<CT> & {
|
|
66
68
|
adapter: SilentQueries;
|
|
67
69
|
};
|
|
68
70
|
type ConstraintArg = {
|
|
@@ -76,22 +78,23 @@ type ConstraintArg = {
|
|
|
76
78
|
check?: RawExpression;
|
|
77
79
|
dropMode?: DropMode;
|
|
78
80
|
};
|
|
79
|
-
declare const createMigrationInterface: (tx: TransactionAdapter, up: boolean,
|
|
80
|
-
declare class MigrationBase {
|
|
81
|
+
declare const createMigrationInterface: <CT extends ColumnTypesBase>(tx: TransactionAdapter, up: boolean, config: RakeDbConfig<CT>) => Migration;
|
|
82
|
+
declare class MigrationBase<CT extends ColumnTypesBase> {
|
|
81
83
|
adapter: TransactionAdapter;
|
|
82
84
|
log?: QueryLogObject;
|
|
83
85
|
up: boolean;
|
|
84
86
|
options: RakeDbConfig;
|
|
85
87
|
migratedAsts: RakeDbAst[];
|
|
86
|
-
|
|
87
|
-
createTable<Table extends string, Shape extends ColumnsShape>(tableName: Table, fn: ColumnsShapeCallback<Shape>): Promise<CreateTableResult<Table, Shape>>;
|
|
88
|
-
|
|
89
|
-
dropTable<Table extends string, Shape extends ColumnsShape>(tableName: Table, fn: ColumnsShapeCallback<Shape>): Promise<CreateTableResult<Table, Shape>>;
|
|
90
|
-
|
|
91
|
-
changeTable(tableName: string,
|
|
88
|
+
columnTypes: CT;
|
|
89
|
+
createTable<Table extends string, Shape extends ColumnsShape>(tableName: Table, options: TableOptions, fn: ColumnsShapeCallback<CT, Shape>): Promise<CreateTableResult<Table, Shape>>;
|
|
90
|
+
createTable<Table extends string, Shape extends ColumnsShape>(tableName: Table, fn: ColumnsShapeCallback<CT, Shape>): Promise<CreateTableResult<Table, Shape>>;
|
|
91
|
+
dropTable<Table extends string, Shape extends ColumnsShape>(tableName: Table, options: TableOptions, fn: ColumnsShapeCallback<CT, Shape>): Promise<CreateTableResult<Table, Shape>>;
|
|
92
|
+
dropTable<Table extends string, Shape extends ColumnsShape>(tableName: Table, fn: ColumnsShapeCallback<CT, Shape>): Promise<CreateTableResult<Table, Shape>>;
|
|
93
|
+
changeTable(tableName: string, options: ChangeTableOptions, fn?: ChangeTableCallback<CT>): Promise<void>;
|
|
94
|
+
changeTable(tableName: string, fn: ChangeTableCallback<CT>): Promise<void>;
|
|
92
95
|
renameTable(from: string, to: string): Promise<void>;
|
|
93
|
-
addColumn(tableName: string, columnName: string, fn: (t: MigrationColumnTypes) => ColumnType): Promise<void>;
|
|
94
|
-
dropColumn(tableName: string, columnName: string, fn: (t: MigrationColumnTypes) => ColumnType): Promise<void>;
|
|
96
|
+
addColumn(tableName: string, columnName: string, fn: (t: MigrationColumnTypes<CT>) => ColumnType): Promise<void>;
|
|
97
|
+
dropColumn(tableName: string, columnName: string, fn: (t: MigrationColumnTypes<CT>) => ColumnType): Promise<void>;
|
|
95
98
|
addIndex(tableName: string, columns: MaybeArray<string | IndexColumnOptions>, options?: IndexOptions): Promise<void>;
|
|
96
99
|
dropIndex(tableName: string, columns: MaybeArray<string | IndexColumnOptions>, options?: IndexOptions): Promise<void>;
|
|
97
100
|
addForeignKey(tableName: string, columns: [string, ...string[]], foreignTable: string, foreignColumns: [string, ...string[]], options?: ForeignKeyOptions): Promise<void>;
|
|
@@ -113,8 +116,8 @@ declare class MigrationBase {
|
|
|
113
116
|
dropExtension(name: string, options?: Omit<RakeDbAst.Extension, 'type' | 'action' | 'name' | 'values'>): Promise<void>;
|
|
114
117
|
createEnum(name: string, values: [string, ...string[]], options?: Omit<RakeDbAst.Enum, 'type' | 'action' | 'name' | 'values' | 'schema'>): Promise<void>;
|
|
115
118
|
dropEnum(name: string, values: [string, ...string[]], options?: Omit<RakeDbAst.Enum, 'type' | 'action' | 'name' | 'values' | 'schema'>): Promise<void>;
|
|
116
|
-
createDomain(name: string, fn: (t:
|
|
117
|
-
dropDomain(name: string, fn: (t:
|
|
119
|
+
createDomain(name: string, fn: (t: CT) => ColumnType, options?: Omit<RakeDbAst.Domain, 'type' | 'action' | 'schema' | 'name' | 'baseType'>): Promise<void>;
|
|
120
|
+
dropDomain(name: string, fn: (t: CT) => ColumnType, options?: Omit<RakeDbAst.Domain, 'type' | 'action' | 'schema' | 'name' | 'baseType'>): Promise<void>;
|
|
118
121
|
createView(name: string, options: RakeDbAst.ViewOptions, sql: string | RawExpression): Promise<void>;
|
|
119
122
|
createView(name: string, sql: string | RawExpression): Promise<void>;
|
|
120
123
|
dropView(name: string, options: RakeDbAst.ViewOptions, sql: string | RawExpression): Promise<void>;
|
|
@@ -259,12 +262,22 @@ declare namespace RakeDbAst {
|
|
|
259
262
|
}
|
|
260
263
|
|
|
261
264
|
type Db = DbResult<DefaultColumnTypes>;
|
|
262
|
-
type
|
|
265
|
+
type InputRakeDbConfig<CT extends ColumnTypesBase> = Partial<Omit<RakeDbConfig<CT>, 'columnTypes'>> & ({
|
|
266
|
+
columnTypes?: CT | ((t: DefaultColumnTypes) => CT);
|
|
267
|
+
} | {
|
|
268
|
+
baseTable?: new () => {
|
|
269
|
+
columnTypes: CT;
|
|
270
|
+
snakeCase?: boolean;
|
|
271
|
+
};
|
|
272
|
+
});
|
|
273
|
+
type RakeDbConfig<CT extends ColumnTypesBase = DefaultColumnTypes> = {
|
|
274
|
+
columnTypes: CT;
|
|
263
275
|
basePath: string;
|
|
276
|
+
dbScript: string;
|
|
264
277
|
migrationsPath: string;
|
|
265
278
|
migrationsTable: string;
|
|
266
279
|
snakeCase: boolean;
|
|
267
|
-
commands: Record<string, (options: AdapterOptions[], config: RakeDbConfig
|
|
280
|
+
commands: Record<string, (options: AdapterOptions[], config: RakeDbConfig<CT>, args: string[]) => Promise<void>>;
|
|
268
281
|
import(path: string): Promise<void>;
|
|
269
282
|
noPrimaryKey?: NoPrimaryKeyOption;
|
|
270
283
|
appCodeUpdater?: AppCodeUpdater;
|
|
@@ -282,27 +295,144 @@ type AppCodeUpdater = (params: {
|
|
|
282
295
|
logger: QueryLogOptions['logger'];
|
|
283
296
|
}) => Promise<void>;
|
|
284
297
|
|
|
285
|
-
declare const createDb: (arg: MaybeArray<AdapterOptions>, config: RakeDbConfig) => Promise<void>;
|
|
298
|
+
declare const createDb: <CT extends ColumnTypesBase>(arg: MaybeArray<AdapterOptions>, config: RakeDbConfig<CT>) => Promise<void>;
|
|
286
299
|
declare const dropDb: (arg: MaybeArray<AdapterOptions>) => Promise<void>;
|
|
287
|
-
declare const resetDb: (arg: MaybeArray<AdapterOptions>, config: RakeDbConfig) => Promise<void>;
|
|
300
|
+
declare const resetDb: <CT extends ColumnTypesBase>(arg: MaybeArray<AdapterOptions>, config: RakeDbConfig<CT>) => Promise<void>;
|
|
288
301
|
|
|
289
|
-
declare const writeMigrationFile: (config: RakeDbConfig
|
|
290
|
-
declare const generate: (config: RakeDbConfig
|
|
302
|
+
declare const writeMigrationFile: <CT extends ColumnTypesBase>(config: RakeDbConfig<CT>, version: string, name: string, content: (importPath: string, name: string) => string) => Promise<void>;
|
|
303
|
+
declare const generate: <CT extends ColumnTypesBase>(config: RakeDbConfig<CT>, [name]: string[]) => Promise<void>;
|
|
291
304
|
declare const makeFileTimeStamp: () => string;
|
|
292
305
|
|
|
293
|
-
type ChangeCallback = (db: Migration
|
|
294
|
-
declare const change: (fn: ChangeCallback) => void;
|
|
306
|
+
type ChangeCallback<CT extends ColumnTypesBase = ColumnTypesBase> = (db: Migration<CT>, up: boolean) => Promise<void>;
|
|
295
307
|
|
|
296
|
-
declare const migrateOrRollback: (options: MaybeArray<AdapterOptions>, config: RakeDbConfig
|
|
308
|
+
declare const migrateOrRollback: <CT extends ColumnTypesBase>(options: MaybeArray<AdapterOptions>, config: RakeDbConfig<CT>, args: string[], up: boolean) => Promise<void>;
|
|
297
309
|
declare const changeCache: Record<string, ChangeCallback[] | undefined>;
|
|
298
|
-
declare const migrate: (options: MaybeArray<AdapterOptions>, config: RakeDbConfig
|
|
299
|
-
declare const rollback: (options: MaybeArray<AdapterOptions>, config: RakeDbConfig
|
|
300
|
-
declare const redo: (options: MaybeArray<AdapterOptions>, config: RakeDbConfig
|
|
310
|
+
declare const migrate: <CT extends ColumnTypesBase>(options: MaybeArray<AdapterOptions>, config: RakeDbConfig<CT>, args?: string[]) => Promise<void>;
|
|
311
|
+
declare const rollback: <CT extends ColumnTypesBase>(options: MaybeArray<AdapterOptions>, config: RakeDbConfig<CT>, args?: string[]) => Promise<void>;
|
|
312
|
+
declare const redo: <CT extends ColumnTypesBase>(options: MaybeArray<AdapterOptions>, config: RakeDbConfig<CT>, args?: string[]) => Promise<void>;
|
|
301
313
|
|
|
302
|
-
declare const rakeDb:
|
|
314
|
+
declare const rakeDb: <CT extends ColumnTypesBase = {
|
|
315
|
+
timestamps: <T extends orchid_core.ColumnTypeBase<unknown, orchid_core.BaseOperators, unknown, orchid_core.ColumnDataBase>>(this: {
|
|
316
|
+
name(name: string): {
|
|
317
|
+
timestamp(): T;
|
|
318
|
+
};
|
|
319
|
+
timestamp(): T;
|
|
320
|
+
}) => {
|
|
321
|
+
createdAt: orchid_core.ColumnWithDefault<T, orchid_core.RawExpression>;
|
|
322
|
+
updatedAt: orchid_core.ColumnWithDefault<T, orchid_core.RawExpression>;
|
|
323
|
+
};
|
|
324
|
+
timestampsSnakeCase: <T_1 extends orchid_core.ColumnTypeBase<unknown, orchid_core.BaseOperators, unknown, orchid_core.ColumnDataBase>>(this: {
|
|
325
|
+
name(name: string): {
|
|
326
|
+
timestamp(): T_1;
|
|
327
|
+
};
|
|
328
|
+
timestamp(): T_1;
|
|
329
|
+
}) => {
|
|
330
|
+
createdAt: orchid_core.ColumnWithDefault<T_1, orchid_core.RawExpression>;
|
|
331
|
+
updatedAt: orchid_core.ColumnWithDefault<T_1, orchid_core.RawExpression>;
|
|
332
|
+
};
|
|
333
|
+
name: typeof orchid_core.name;
|
|
334
|
+
raw: (sql: string, values?: false | Record<string, unknown> | undefined) => orchid_core.RawExpression<orchid_core.ColumnTypeBase<unknown, orchid_core.BaseOperators, unknown, orchid_core.ColumnDataBase>>;
|
|
335
|
+
smallint(this: ColumnTypesBase): pqb.SmallIntColumn;
|
|
336
|
+
integer(this: ColumnTypesBase): pqb.IntegerColumn;
|
|
337
|
+
bigint(this: ColumnTypesBase): pqb.BigIntColumn;
|
|
338
|
+
numeric<Precision extends number | undefined = undefined, Scale extends number | undefined = undefined>(this: ColumnTypesBase, precision?: Precision | undefined, scale?: Scale | undefined): pqb.DecimalColumn<Precision, Scale>;
|
|
339
|
+
decimal<Precision_1 extends number | undefined = undefined, Scale_1 extends number | undefined = undefined>(this: ColumnTypesBase, precision?: Precision_1 | undefined, scale?: Scale_1 | undefined): pqb.DecimalColumn<Precision_1, Scale_1>;
|
|
340
|
+
real(this: ColumnTypesBase): pqb.RealColumn;
|
|
341
|
+
doublePrecision(this: ColumnTypesBase): pqb.DoublePrecisionColumn;
|
|
342
|
+
identity(this: ColumnTypesBase, options?: pqb.TableData.Identity | undefined): pqb.IntegerColumn;
|
|
343
|
+
smallSerial(this: ColumnTypesBase): pqb.SmallSerialColumn;
|
|
344
|
+
serial(this: ColumnTypesBase): pqb.SerialColumn;
|
|
345
|
+
bigSerial(this: ColumnTypesBase): pqb.BigSerialColumn;
|
|
346
|
+
money(this: ColumnTypesBase): pqb.MoneyColumn;
|
|
347
|
+
varchar<Limit extends number | undefined = undefined>(this: ColumnTypesBase, limit?: Limit | undefined): pqb.VarCharColumn<Limit>;
|
|
348
|
+
char<Limit_1 extends number | undefined = undefined>(this: ColumnTypesBase, limit?: Limit_1 | undefined): pqb.CharColumn<Limit_1>;
|
|
349
|
+
text: (this: ColumnTypesBase, min: number, max: number) => pqb.TextColumn;
|
|
350
|
+
string: (this: ColumnTypesBase, min: number, max: number) => pqb.TextColumn;
|
|
351
|
+
citext(this: ColumnTypesBase, min: number, max: number): pqb.CitextColumn;
|
|
352
|
+
bytea(this: ColumnTypesBase): pqb.ByteaColumn;
|
|
353
|
+
date(this: ColumnTypesBase): pqb.DateColumn;
|
|
354
|
+
timestampWithoutTimeZone<Precision_2 extends number>(this: ColumnTypesBase, precision?: Precision_2 | undefined): pqb.TimestampColumn<Precision_2>;
|
|
355
|
+
timestamp<Precision_3 extends number | undefined = undefined>(this: ColumnTypesBase, precision?: Precision_3 | undefined): pqb.TimestampTzColumn<number>;
|
|
356
|
+
time<Precision_4 extends number | undefined = undefined>(this: ColumnTypesBase, precision?: Precision_4 | undefined): pqb.TimeColumn<Precision_4>;
|
|
357
|
+
interval<Fields extends string | undefined = undefined, Precision_5 extends number | undefined = undefined>(this: ColumnTypesBase, fields?: Fields | undefined, precision?: Precision_5 | undefined): pqb.IntervalColumn<Fields, Precision_5>;
|
|
358
|
+
boolean(this: ColumnTypesBase): pqb.BooleanColumn;
|
|
359
|
+
enum<U extends string, T_2 extends [U, ...U[]]>(this: ColumnTypesBase, dataType: string, type: T_2): pqb.EnumColumn<U, T_2>;
|
|
360
|
+
point(this: ColumnTypesBase): pqb.PointColumn;
|
|
361
|
+
line(this: ColumnTypesBase): pqb.LineColumn;
|
|
362
|
+
lseg(this: ColumnTypesBase): pqb.LsegColumn;
|
|
363
|
+
box(this: ColumnTypesBase): pqb.BoxColumn;
|
|
364
|
+
path(this: ColumnTypesBase): pqb.PathColumn;
|
|
365
|
+
polygon(this: ColumnTypesBase): pqb.PolygonColumn;
|
|
366
|
+
circle(this: ColumnTypesBase): pqb.CircleColumn;
|
|
367
|
+
cidr(this: ColumnTypesBase): pqb.CidrColumn;
|
|
368
|
+
inet(this: ColumnTypesBase): pqb.InetColumn;
|
|
369
|
+
macaddr(this: ColumnTypesBase): pqb.MacAddrColumn;
|
|
370
|
+
macaddr8(this: ColumnTypesBase): pqb.MacAddr8Column;
|
|
371
|
+
bit<Length extends number>(this: ColumnTypesBase, length: Length): pqb.BitColumn<Length>;
|
|
372
|
+
bitVarying<Length_1 extends number | undefined = undefined>(this: ColumnTypesBase, length?: Length_1 | undefined): pqb.BitVaryingColumn<Length_1>;
|
|
373
|
+
tsvector(this: ColumnTypesBase): pqb.TsVectorColumn;
|
|
374
|
+
tsquery(this: ColumnTypesBase): pqb.TsQueryColumn;
|
|
375
|
+
uuid(this: ColumnTypesBase): pqb.UUIDColumn;
|
|
376
|
+
xml(this: ColumnTypesBase): pqb.XMLColumn;
|
|
377
|
+
json<Type extends orchid_core.JSONTypeAny>(this: ColumnTypesBase, schemaOrFn: Type | ((j: {
|
|
378
|
+
set: <Value extends orchid_core.JSONTypeAny>(valueType: Value) => orchid_core.JSONSet<Value>;
|
|
379
|
+
tuple: <T_3 extends [] | orchid_core.JSONTupleItems, Rest extends orchid_core.JSONTypeAny | null = null>(items: T_3, rest?: Rest) => orchid_core.JSONTuple<T_3, Rest>;
|
|
380
|
+
union: <T_4 extends [orchid_core.JSONTypeAny, orchid_core.JSONTypeAny, ...orchid_core.JSONTypeAny[]]>(types: T_4) => orchid_core.JSONUnion<T_4>;
|
|
381
|
+
any: () => orchid_core.JSONAny;
|
|
382
|
+
bigint: () => orchid_core.JSONBigInt;
|
|
383
|
+
boolean: () => orchid_core.JSONBoolean;
|
|
384
|
+
date: () => orchid_core.JSONDate;
|
|
385
|
+
nan: () => orchid_core.JSONNaN;
|
|
386
|
+
never: () => orchid_core.JSONNever;
|
|
387
|
+
null: () => orchid_core.JSONNull;
|
|
388
|
+
number: () => orchid_core.JSONNumber;
|
|
389
|
+
string: () => orchid_core.JSONString;
|
|
390
|
+
unknown: () => orchid_core.JSONUnknown;
|
|
391
|
+
array: <Type_1 extends orchid_core.JSONTypeAny>(element: Type_1) => orchid_core.JSONArray<Type_1, "many">;
|
|
392
|
+
discriminatedUnion: <Discriminator extends string, DiscriminatorValue extends orchid_core.Primitive, Types extends [orchid_core.JSONDiscriminatedObject<Discriminator, DiscriminatorValue>, orchid_core.JSONDiscriminatedObject<Discriminator, DiscriminatorValue>, ...orchid_core.JSONDiscriminatedObject<Discriminator, DiscriminatorValue>[]]>(discriminator: Discriminator, options: Types) => orchid_core.JSONDiscriminatedUnion<Discriminator, DiscriminatorValue, Types>;
|
|
393
|
+
enum: <U_1 extends string, T_5 extends [U_1, ...U_1[]]>(options: T_5) => orchid_core.JSONEnum<U_1, T_5>;
|
|
394
|
+
instanceOf: <T_6 extends new (...args: any[]) => any>(cls: T_6) => orchid_core.JSONInstanceOf<T_6>;
|
|
395
|
+
intersection: <Left extends orchid_core.JSONTypeAny, Right extends orchid_core.JSONTypeAny>(left: Left, right: Right) => orchid_core.JSONIntersection<Left, Right>;
|
|
396
|
+
lazy: <T_7 extends orchid_core.JSONTypeAny>(fn: () => T_7) => orchid_core.JSONLazy<T_7>;
|
|
397
|
+
literal: <T_8 extends orchid_core.Primitive>(value: T_8) => orchid_core.JSONLiteral<T_8>;
|
|
398
|
+
map: <Key extends orchid_core.JSONTypeAny, Value_1 extends orchid_core.JSONTypeAny>(keyType: Key, valueType: Value_1) => orchid_core.JSONMap<Key, Value_1>;
|
|
399
|
+
nativeEnum: <T_9 extends orchid_core.EnumLike>(givenEnum: T_9) => orchid_core.JSONNativeEnum<T_9>;
|
|
400
|
+
nullable: <T_10 extends orchid_core.JSONTypeAny>(type: T_10) => orchid_core.JSONNullable<T_10>;
|
|
401
|
+
nullish: <T_11 extends orchid_core.JSONTypeAny>(type: T_11) => orchid_core.JSONNullish<T_11>;
|
|
402
|
+
object: <T_12 extends orchid_core.JSONObjectShape, UnknownKeys extends orchid_core.UnknownKeysParam = "strip", Catchall extends orchid_core.JSONTypeAny = orchid_core.JSONTypeAny>(shape: T_12) => orchid_core.JSONObject<T_12, UnknownKeys, Catchall, orchid_core.JSONTypeAny extends Catchall ? orchid_core.addQuestionMarks<{ [k_1 in keyof T_12]: T_12[k_1]["type"]; }> extends infer T_13 extends object ? { [k in keyof T_13]: orchid_core.addQuestionMarks<{ [k_1 in keyof T_12]: T_12[k_1]["type"]; }>[k]; } : never : (orchid_core.addQuestionMarks<{ [k_1 in keyof T_12]: T_12[k_1]["type"]; }> extends infer T_16 extends object ? { [k in keyof T_16]: orchid_core.addQuestionMarks<{ [k_1 in keyof T_12]: T_12[k_1]["type"]; }>[k]; } : never) & {
|
|
403
|
+
[k: string]: Catchall["type"];
|
|
404
|
+
} extends infer T_14 extends object ? { [k_2 in keyof T_14]: ((orchid_core.addQuestionMarks<{ [k_1 in keyof T_12]: T_12[k_1]["type"]; }> extends infer T_15 extends object ? { [k in keyof T_15]: orchid_core.addQuestionMarks<{ [k_1 in keyof T_12]: T_12[k_1]["type"]; }>[k]; } : never) & {
|
|
405
|
+
[k: string]: Catchall["type"];
|
|
406
|
+
})[k_2]; } : never>;
|
|
407
|
+
optional: <T_17 extends orchid_core.JSONTypeAny>(type: T_17) => orchid_core.JSONOptional<T_17>;
|
|
408
|
+
record: typeof orchid_core.record;
|
|
409
|
+
}) => Type)): pqb.JSONColumn<Type>;
|
|
410
|
+
jsonText(this: ColumnTypesBase): pqb.JSONTextColumn;
|
|
411
|
+
array<Item extends pqb.ColumnType<unknown, orchid_core.BaseOperators, unknown>>(this: ColumnTypesBase, item: Item): pqb.ArrayColumn<Item>;
|
|
412
|
+
type(this: ColumnTypesBase, dataType: string): pqb.CustomTypeColumn;
|
|
413
|
+
domain(this: ColumnTypesBase, dataType: string): pqb.DomainColumn;
|
|
414
|
+
primaryKey(columns: string[], options?: {
|
|
415
|
+
name?: string | undefined;
|
|
416
|
+
} | undefined): {};
|
|
417
|
+
index(columns: MaybeArray<string | pqb.IndexColumnOptions>, options?: pqb.IndexOptions): {};
|
|
418
|
+
unique(columns: MaybeArray<string | pqb.IndexColumnOptions>, options?: pqb.IndexOptions): {};
|
|
419
|
+
constraint<Table extends string | (() => pqb.ForeignKeyTable), Columns extends Table extends () => pqb.ForeignKeyTable ? [pqb.ColumnNameOfTable<ReturnType<Table>>, ...pqb.ColumnNameOfTable<ReturnType<Table>>[]] : [string, ...string[]]>({ name, references, check, dropMode, }: {
|
|
420
|
+
name?: string | undefined;
|
|
421
|
+
references?: [columns: string[], fnOrTable: Table, foreignColumns: Columns, options?: pqb.ForeignKeyOptions | undefined] | undefined;
|
|
422
|
+
check?: orchid_core.RawExpression | undefined;
|
|
423
|
+
dropMode?: pqb.DropMode | undefined;
|
|
424
|
+
}): {};
|
|
425
|
+
foreignKey<Table_1 extends string | (() => pqb.ForeignKeyTable), Columns_1 extends Table_1 extends () => pqb.ForeignKeyTable ? [pqb.ColumnNameOfTable<ReturnType<Table_1>>, ...pqb.ColumnNameOfTable<ReturnType<Table_1>>[]] : [string, ...string[]]>(columns: string[], fnOrTable: Table_1, foreignColumns: Columns_1, options?: (pqb.ForeignKeyOptions & {
|
|
426
|
+
name?: string | undefined;
|
|
427
|
+
dropMode?: pqb.DropMode | undefined;
|
|
428
|
+
}) | undefined): {};
|
|
429
|
+
check(check: orchid_core.RawExpression): {};
|
|
430
|
+
}>(options: MaybeArray<AdapterOptions>, partialConfig?: InputRakeDbConfig<CT>, args?: string[]) => ((fn: ChangeCallback<CT>) => void) & {
|
|
431
|
+
promise: Promise<void>;
|
|
432
|
+
};
|
|
303
433
|
|
|
304
|
-
declare const saveMigratedVersion: (db: SilentQueries, version: string, config: RakeDbConfig) => Promise<void>;
|
|
305
|
-
declare const removeMigratedVersion: (db: SilentQueries, version: string, config: RakeDbConfig) => Promise<void>;
|
|
306
|
-
declare const getMigratedVersionsMap: (db: Adapter, config: RakeDbConfig) => Promise<Record<string, boolean>>;
|
|
434
|
+
declare const saveMigratedVersion: <CT extends ColumnTypesBase>(db: SilentQueries, version: string, config: RakeDbConfig<CT>) => Promise<void>;
|
|
435
|
+
declare const removeMigratedVersion: <CT extends ColumnTypesBase>(db: SilentQueries, version: string, config: RakeDbConfig<CT>) => Promise<void>;
|
|
436
|
+
declare const getMigratedVersionsMap: <CT extends ColumnTypesBase>(db: Adapter, config: RakeDbConfig<CT>) => Promise<Record<string, boolean>>;
|
|
307
437
|
|
|
308
|
-
export { AppCodeUpdater, ChangeTableCallback, ChangeTableOptions, ColumnComment, ColumnsShapeCallback, DropMode, Migration, MigrationBase, MigrationColumnTypes, RakeDbAst, RakeDbConfig, SilentQueries, TableOptions,
|
|
438
|
+
export { AppCodeUpdater, ChangeTableCallback, ChangeTableOptions, ColumnComment, ColumnsShapeCallback, DropMode, Migration, MigrationBase, MigrationColumnTypes, RakeDbAst, RakeDbConfig, SilentQueries, TableOptions, changeCache, createDb, createMigrationInterface, dropDb, generate, getMigratedVersionsMap, makeFileTimeStamp, migrate, migrateOrRollback, rakeDb, redo, removeMigratedVersion, resetDb, rollback, saveMigratedVersion, writeMigrationFile };
|
package/dist/index.js
CHANGED
|
@@ -56,12 +56,12 @@ const migrationConfigDefaults = {
|
|
|
56
56
|
useCodeUpdater: true
|
|
57
57
|
};
|
|
58
58
|
const processRakeDbConfig = (config) => {
|
|
59
|
-
var _a;
|
|
59
|
+
var _a, _b;
|
|
60
60
|
const result = __spreadValues$6(__spreadValues$6({}, migrationConfigDefaults), config);
|
|
61
61
|
if (!result.log) {
|
|
62
62
|
delete result.logger;
|
|
63
63
|
}
|
|
64
|
-
if (!result.basePath) {
|
|
64
|
+
if (!result.basePath || !result.dbScript) {
|
|
65
65
|
let stack;
|
|
66
66
|
const original = Error.prepareStackTrace;
|
|
67
67
|
Error.prepareStackTrace = (_, s) => stack = s;
|
|
@@ -84,6 +84,7 @@ const processRakeDbConfig = (config) => {
|
|
|
84
84
|
}
|
|
85
85
|
}
|
|
86
86
|
result.basePath = path.dirname(file);
|
|
87
|
+
result.dbScript = path.basename(file);
|
|
87
88
|
break;
|
|
88
89
|
}
|
|
89
90
|
}
|
|
@@ -99,6 +100,14 @@ const processRakeDbConfig = (config) => {
|
|
|
99
100
|
result.migrationsPath
|
|
100
101
|
);
|
|
101
102
|
}
|
|
103
|
+
if ("baseTable" in config) {
|
|
104
|
+
const proto = (_b = config.baseTable) == null ? void 0 : _b.prototype;
|
|
105
|
+
result.columnTypes = proto.columnTypes || pqb.columnTypes;
|
|
106
|
+
if (proto.snakeCase)
|
|
107
|
+
result.snakeCase = true;
|
|
108
|
+
} else {
|
|
109
|
+
result.columnTypes = "columnTypes" in config && (typeof config.columnTypes === "function" ? config.columnTypes(pqb.columnTypes) : config.columnTypes) || pqb.columnTypes;
|
|
110
|
+
}
|
|
102
111
|
return result;
|
|
103
112
|
};
|
|
104
113
|
const getDatabaseAndUserFromOptions = (options) => {
|
|
@@ -268,13 +277,11 @@ const makePopulateEnumQuery = (item) => {
|
|
|
268
277
|
};
|
|
269
278
|
|
|
270
279
|
let currentChanges = [];
|
|
271
|
-
const change = (fn) => {
|
|
272
|
-
currentChanges.push(fn);
|
|
273
|
-
};
|
|
274
280
|
const clearChanges = () => {
|
|
275
281
|
currentChanges = [];
|
|
276
282
|
};
|
|
277
283
|
const getCurrentChanges = () => currentChanges;
|
|
284
|
+
const pushChange = (fn) => currentChanges.push(fn);
|
|
278
285
|
|
|
279
286
|
var __defProp$5 = Object.defineProperty;
|
|
280
287
|
var __defProps$4 = Object.defineProperties;
|
|
@@ -569,9 +576,12 @@ var __objRest$1 = (source, exclude) => {
|
|
|
569
576
|
}
|
|
570
577
|
return target;
|
|
571
578
|
};
|
|
572
|
-
const types = Object.assign(Object.create(pqb.columnTypes), tableMethods);
|
|
573
579
|
const createTable$1 = async (migration, up, tableName, options, fn) => {
|
|
574
580
|
const snakeCase = "snakeCase" in options ? options.snakeCase : migration.options.snakeCase;
|
|
581
|
+
const types = Object.assign(
|
|
582
|
+
Object.create(migration.columnTypes),
|
|
583
|
+
tableMethods
|
|
584
|
+
);
|
|
575
585
|
types[orchidCore.snakeCaseKey] = snakeCase;
|
|
576
586
|
const shape = pqb.getColumnTypes(types, fn);
|
|
577
587
|
const tableData = pqb.getTableData();
|
|
@@ -895,7 +905,7 @@ const changeTable = async (migration, up, tableName, options, fn) => {
|
|
|
895
905
|
var _a;
|
|
896
906
|
pqb.resetTableData();
|
|
897
907
|
resetChangeTableData();
|
|
898
|
-
const tableChanger = Object.create(
|
|
908
|
+
const tableChanger = Object.create(migration.columnTypes);
|
|
899
909
|
Object.assign(tableChanger, tableChangeMethods);
|
|
900
910
|
const snakeCase = "snakeCase" in options ? options.snakeCase : migration.options.snakeCase;
|
|
901
911
|
tableChanger[orchidCore.snakeCaseKey] = snakeCase;
|
|
@@ -1285,10 +1295,10 @@ var __spreadValues$2 = (a, b) => {
|
|
|
1285
1295
|
return a;
|
|
1286
1296
|
};
|
|
1287
1297
|
var __spreadProps$1 = (a, b) => __defProps$1(a, __getOwnPropDescs$1(b));
|
|
1288
|
-
const createMigrationInterface = (tx, up,
|
|
1298
|
+
const createMigrationInterface = (tx, up, config) => {
|
|
1289
1299
|
const adapter = new pqb.TransactionAdapter(tx, tx.client, tx.types);
|
|
1290
1300
|
const { query, arrays } = adapter;
|
|
1291
|
-
const log = pqb.logParamToLogObject(
|
|
1301
|
+
const log = pqb.logParamToLogObject(config.logger || console, config.log);
|
|
1292
1302
|
adapter.query = (q, types) => {
|
|
1293
1303
|
return wrapWithLog(log, q, () => query.call(adapter, q, types));
|
|
1294
1304
|
};
|
|
@@ -1296,7 +1306,10 @@ const createMigrationInterface = (tx, up, options) => {
|
|
|
1296
1306
|
return wrapWithLog(log, q, () => arrays.call(adapter, q, types));
|
|
1297
1307
|
};
|
|
1298
1308
|
Object.assign(adapter, { silentQuery: query, silentArrays: arrays });
|
|
1299
|
-
const db = pqb.createDb({
|
|
1309
|
+
const db = pqb.createDb({
|
|
1310
|
+
adapter,
|
|
1311
|
+
columnTypes: config.columnTypes
|
|
1312
|
+
});
|
|
1300
1313
|
const { prototype: proto } = MigrationBase;
|
|
1301
1314
|
for (const key of Object.getOwnPropertyNames(proto)) {
|
|
1302
1315
|
db[key] = proto[key];
|
|
@@ -1306,7 +1319,7 @@ const createMigrationInterface = (tx, up, options) => {
|
|
|
1306
1319
|
adapter,
|
|
1307
1320
|
log,
|
|
1308
1321
|
up,
|
|
1309
|
-
options
|
|
1322
|
+
options: config
|
|
1310
1323
|
});
|
|
1311
1324
|
};
|
|
1312
1325
|
class MigrationBase {
|
|
@@ -1553,7 +1566,7 @@ const createDomain$1 = async (migration, up, name, fn, options) => {
|
|
|
1553
1566
|
action: up ? "create" : "drop",
|
|
1554
1567
|
schema,
|
|
1555
1568
|
name: domainName,
|
|
1556
|
-
baseType: fn(
|
|
1569
|
+
baseType: fn(migration.columnTypes)
|
|
1557
1570
|
}, options);
|
|
1558
1571
|
let query;
|
|
1559
1572
|
const values = [];
|
|
@@ -1690,7 +1703,6 @@ const begin = {
|
|
|
1690
1703
|
const processMigration = async (db, up, file, config, options, appCodeUpdaterCache) => {
|
|
1691
1704
|
var _a;
|
|
1692
1705
|
const asts = await db.transaction(begin, async (tx) => {
|
|
1693
|
-
const db2 = createMigrationInterface(tx, up, config);
|
|
1694
1706
|
clearChanges();
|
|
1695
1707
|
let changes = changeCache[file.path];
|
|
1696
1708
|
if (!changes) {
|
|
@@ -1704,6 +1716,7 @@ const processMigration = async (db, up, file, config, options, appCodeUpdaterCac
|
|
|
1704
1716
|
changes = getCurrentChanges();
|
|
1705
1717
|
changeCache[file.path] = changes;
|
|
1706
1718
|
}
|
|
1719
|
+
const db2 = createMigrationInterface(tx, up, config);
|
|
1707
1720
|
for (const fn of up ? changes : changes.reverse()) {
|
|
1708
1721
|
await fn(db2, up);
|
|
1709
1722
|
}
|
|
@@ -1850,14 +1863,18 @@ const writeMigrationFile = async (config, version, name, content) => {
|
|
|
1850
1863
|
var _a;
|
|
1851
1864
|
await promises.mkdir(config.migrationsPath, { recursive: true });
|
|
1852
1865
|
const filePath = path.resolve(config.migrationsPath, `${version}_${name}.ts`);
|
|
1853
|
-
|
|
1866
|
+
const importPath = orchidCore.getImportPath(
|
|
1867
|
+
filePath,
|
|
1868
|
+
path.join(config.basePath, config.dbScript)
|
|
1869
|
+
);
|
|
1870
|
+
await promises.writeFile(filePath, content(importPath, name));
|
|
1854
1871
|
(_a = config.logger) == null ? void 0 : _a.log(`Created ${orchidCore.pathToLog(filePath)}`);
|
|
1855
1872
|
};
|
|
1856
1873
|
const generate = async (config, [name]) => {
|
|
1857
1874
|
if (!name)
|
|
1858
1875
|
throw new Error("Migration name is missing");
|
|
1859
1876
|
const version = makeFileTimeStamp();
|
|
1860
|
-
await writeMigrationFile(config, version, name, makeContent
|
|
1877
|
+
await writeMigrationFile(config, version, name, makeContent);
|
|
1861
1878
|
};
|
|
1862
1879
|
const makeFileTimeStamp = () => {
|
|
1863
1880
|
const now = /* @__PURE__ */ new Date();
|
|
@@ -1870,8 +1887,8 @@ const makeFileTimeStamp = () => {
|
|
|
1870
1887
|
now.getUTCSeconds()
|
|
1871
1888
|
].map((value) => value < 10 ? `0${value}` : value).join("");
|
|
1872
1889
|
};
|
|
1873
|
-
const makeContent = (name) => {
|
|
1874
|
-
let content = `import { change } from '
|
|
1890
|
+
const makeContent = (importPath, name) => {
|
|
1891
|
+
let content = `import { change } from '${importPath}';
|
|
1875
1892
|
|
|
1876
1893
|
change(async (db) => {`;
|
|
1877
1894
|
const [first, rest] = getFirstWordAndRest(name);
|
|
@@ -2795,8 +2812,7 @@ const astToMigration = (config, ast) => {
|
|
|
2795
2812
|
}
|
|
2796
2813
|
if (!first.length && !tablesAndViews.length && !constraints.length)
|
|
2797
2814
|
return;
|
|
2798
|
-
let code =
|
|
2799
|
-
`;
|
|
2815
|
+
let code = "";
|
|
2800
2816
|
if (first.length) {
|
|
2801
2817
|
code += `
|
|
2802
2818
|
change(async (db) => {
|
|
@@ -2820,7 +2836,8 @@ ${orchidCore.codeToString(constraints, " ", " ")}
|
|
|
2820
2836
|
});
|
|
2821
2837
|
`;
|
|
2822
2838
|
}
|
|
2823
|
-
return
|
|
2839
|
+
return (importPath) => `import { change } from '${importPath}';
|
|
2840
|
+
${code}`;
|
|
2824
2841
|
};
|
|
2825
2842
|
const createSchema = (ast) => {
|
|
2826
2843
|
return `await db.createSchema(${orchidCore.singleQuote(ast.name)});`;
|
|
@@ -3010,9 +3027,20 @@ Append \`as\` method manually to ${count > 1 ? "these" : "this"} column${count >
|
|
|
3010
3027
|
adapter.close();
|
|
3011
3028
|
};
|
|
3012
3029
|
|
|
3013
|
-
const rakeDb =
|
|
3014
|
-
var _a, _b, _c;
|
|
3030
|
+
const rakeDb = (options, partialConfig = {}, args = process.argv.slice(2)) => {
|
|
3015
3031
|
const config = processRakeDbConfig(partialConfig);
|
|
3032
|
+
const promise = runCommand(options, config, args);
|
|
3033
|
+
return Object.assign(
|
|
3034
|
+
(fn) => {
|
|
3035
|
+
pushChange(fn);
|
|
3036
|
+
},
|
|
3037
|
+
{
|
|
3038
|
+
promise
|
|
3039
|
+
}
|
|
3040
|
+
);
|
|
3041
|
+
};
|
|
3042
|
+
const runCommand = async (options, config, args = process.argv.slice(2)) => {
|
|
3043
|
+
var _a, _b, _c;
|
|
3016
3044
|
const command = (_a = args[0]) == null ? void 0 : _a.split(":")[0];
|
|
3017
3045
|
try {
|
|
3018
3046
|
if (command === "create") {
|
|
@@ -3074,7 +3102,6 @@ Migrate and rollback common arguments:
|
|
|
3074
3102
|
`;
|
|
3075
3103
|
|
|
3076
3104
|
exports.MigrationBase = MigrationBase;
|
|
3077
|
-
exports.change = change;
|
|
3078
3105
|
exports.changeCache = changeCache;
|
|
3079
3106
|
exports.createDb = createDb;
|
|
3080
3107
|
exports.createMigrationInterface = createMigrationInterface;
|