peta-migrate 0.1.1 → 0.2.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 +6 -5
- package/bin/peta +3 -0
- package/dist/cli.mjs +117 -9
- package/dist/index.d.mts +88 -379
- package/dist/index.mjs +3 -2
- package/dist/pusher-DJvKHlOT.mjs +88 -0
- package/dist/snapshot-DopEB8mx.mjs +550 -0
- package/package.json +10 -3
- package/dist/runner-DOQsuaSQ.mjs +0 -180
package/dist/index.d.mts
CHANGED
|
@@ -1,5 +1,28 @@
|
|
|
1
1
|
import { Kysely } from "kysely";
|
|
2
|
+
import { ModelDefinition } from "peta-orm";
|
|
2
3
|
|
|
4
|
+
//#region src/checksum.d.ts
|
|
5
|
+
interface ChecksumStore {
|
|
6
|
+
[name: string]: string;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Compute SHA-256 hex digest of a file's content.
|
|
10
|
+
*/
|
|
11
|
+
declare function computeChecksum(filePath: string): string;
|
|
12
|
+
/**
|
|
13
|
+
* Load the checksum store from the migrations directory.
|
|
14
|
+
*/
|
|
15
|
+
declare function loadChecksums(migrationsDir: string): ChecksumStore;
|
|
16
|
+
/**
|
|
17
|
+
* Save checksums to the migrations directory.
|
|
18
|
+
*/
|
|
19
|
+
declare function saveChecksums(migrationsDir: string, store: ChecksumStore): void;
|
|
20
|
+
/**
|
|
21
|
+
* Verify a migration file's checksum against the stored value.
|
|
22
|
+
* Returns true if match or no stored checksum exists.
|
|
23
|
+
*/
|
|
24
|
+
declare function verifyChecksum(migrationsDir: string, name: string, filePath: string): boolean;
|
|
25
|
+
//#endregion
|
|
3
26
|
//#region src/types.d.ts
|
|
4
27
|
interface MigrationFile {
|
|
5
28
|
name: string;
|
|
@@ -24,393 +47,56 @@ interface ResolvedConfig {
|
|
|
24
47
|
models: string[] | string;
|
|
25
48
|
getKysely: () => Kysely<unknown>;
|
|
26
49
|
}
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
readonly data: Collection;
|
|
39
|
-
readonly total: number;
|
|
40
|
-
readonly perPage: number;
|
|
41
|
-
readonly currentPage: number;
|
|
42
|
-
readonly lastPage: number;
|
|
43
|
-
readonly hasMorePages: boolean;
|
|
44
|
-
readonly hasPages: boolean;
|
|
45
|
-
readonly firstItem: number;
|
|
46
|
-
readonly lastItem: number;
|
|
47
|
-
readonly onFirstPage: boolean;
|
|
48
|
-
readonly onLastPage: boolean;
|
|
49
|
-
readonly count: number;
|
|
50
|
-
map<T>(fn: (item: ModelInstance) => T): T[];
|
|
51
|
-
toJSON(): PaginatorJson;
|
|
52
|
-
}
|
|
53
|
-
interface PaginatorJson {
|
|
54
|
-
data: Record<string, unknown>[];
|
|
55
|
-
total: number;
|
|
56
|
-
perPage: number;
|
|
57
|
-
currentPage: number;
|
|
58
|
-
lastPage: number;
|
|
59
|
-
hasMorePages: boolean;
|
|
60
|
-
hasPages: boolean;
|
|
61
|
-
firstItem: number | null;
|
|
62
|
-
lastItem: number | null;
|
|
63
|
-
onFirstPage: boolean;
|
|
64
|
-
onLastPage: boolean;
|
|
65
|
-
}
|
|
66
|
-
//#endregion
|
|
67
|
-
//#region ../orm/src/relations/base.d.ts
|
|
68
|
-
type RelationType = "hasMany" | "belongsTo" | "hasOne" | "manyToMany" | "hasManyThrough";
|
|
69
|
-
interface Relation {
|
|
70
|
-
readonly type: RelationType;
|
|
71
|
-
readonly relatedModelClass: ModelDefinition;
|
|
72
|
-
readonly foreignKey: string;
|
|
73
|
-
readonly localKey: string;
|
|
74
|
-
readonly throughTable?: string;
|
|
75
|
-
readonly foreignPivotKey?: string;
|
|
76
|
-
readonly relatedPivotKey?: string;
|
|
77
|
-
readonly throughForeignKey?: string;
|
|
78
|
-
readonly throughLocalKey?: string;
|
|
79
|
-
_morphMap?: Record<string, () => ModelDefinition>;
|
|
80
|
-
_morphType?: string;
|
|
81
|
-
_morphId?: string;
|
|
82
|
-
_morphTypeValue?: string;
|
|
83
|
-
query(parent: ModelInstance): QueryBuilder;
|
|
84
|
-
addEagerConstraints(query: QueryBuilder, models: ModelInstance[]): void;
|
|
85
|
-
match(models: ModelInstance[], results: ModelInstance[], relationName: string): void;
|
|
86
|
-
getResults(parent: ModelInstance): Promise<ModelInstance | ModelInstance[] | null>;
|
|
87
|
-
loadEager(models: ModelInstance[], relationName: string, constraints?: ((qb: QueryBuilder) => void) | null): Promise<void>;
|
|
50
|
+
interface SchemaColumn {
|
|
51
|
+
name: string;
|
|
52
|
+
type: string;
|
|
53
|
+
isNullable: boolean;
|
|
54
|
+
isPrimaryKey: boolean;
|
|
55
|
+
isUnique: boolean;
|
|
56
|
+
defaultValue: unknown;
|
|
57
|
+
references?: {
|
|
58
|
+
table: string;
|
|
59
|
+
column: string;
|
|
60
|
+
};
|
|
88
61
|
}
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
allowRefs?: boolean;
|
|
94
|
-
/**
|
|
95
|
-
* If `true`, objects with an `id` property get related (pivot row / FK set)
|
|
96
|
-
* instead of inserted. Can be an array of relation names to scope.
|
|
97
|
-
*/
|
|
98
|
-
relate?: boolean | string[];
|
|
99
|
-
/**
|
|
100
|
-
* Whitelist of relation paths allowed for this graph operation.
|
|
101
|
-
* Accepts an array of dotted paths or a Set. If not set, all relations are allowed.
|
|
102
|
-
* When used via the query builder, the QB's `allowGraph()` set is forwarded automatically.
|
|
103
|
-
*/
|
|
104
|
-
allowGraph?: string[] | Set<string>;
|
|
62
|
+
interface SchemaIndex {
|
|
63
|
+
name: string;
|
|
64
|
+
columns: string[];
|
|
65
|
+
unique?: boolean;
|
|
105
66
|
}
|
|
106
|
-
interface
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
noDelete?: boolean | string[];
|
|
111
|
-
/** Prevent insertion for all or specific relation paths */
|
|
112
|
-
noInsert?: boolean | string[];
|
|
113
|
-
/** Prevent update for all or specific relation paths */
|
|
114
|
-
noUpdate?: boolean | string[];
|
|
67
|
+
interface SchemaTable {
|
|
68
|
+
name: string;
|
|
69
|
+
columns: SchemaColumn[];
|
|
70
|
+
indexes: SchemaIndex[];
|
|
115
71
|
}
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
execute(): Promise<ModelInstance[]>;
|
|
120
|
-
collect(): Promise<Collection>;
|
|
121
|
-
executeTakeFirst(): Promise<ModelInstance | undefined>;
|
|
122
|
-
executeTakeFirstOrThrow(): Promise<ModelInstance>;
|
|
123
|
-
find(id: number | string): Promise<ModelInstance | undefined>;
|
|
124
|
-
findOrFail(id: number | string): Promise<ModelInstance>;
|
|
125
|
-
first(): Promise<ModelInstance | undefined>;
|
|
126
|
-
toSQL(): {
|
|
127
|
-
sql: string;
|
|
128
|
-
parameters: readonly unknown[];
|
|
129
|
-
};
|
|
130
|
-
count(): Promise<number>;
|
|
131
|
-
sum(column: string): Promise<number>;
|
|
132
|
-
avg(column: string): Promise<number>;
|
|
133
|
-
min(column: string): Promise<number>;
|
|
134
|
-
max(column: string): Promise<number>;
|
|
135
|
-
withCount(relation: string): QueryBuilder;
|
|
136
|
-
withSum(relation: string, column: string): QueryBuilder;
|
|
137
|
-
withAvg(relation: string, column: string): QueryBuilder;
|
|
138
|
-
withMin(relation: string, column: string): QueryBuilder;
|
|
139
|
-
withMax(relation: string, column: string): QueryBuilder;
|
|
140
|
-
withExists(relation: string): QueryBuilder;
|
|
141
|
-
chunk(size: number, callback: (chunk: ModelInstance[]) => Promise<void>): Promise<void>;
|
|
142
|
-
paginate(page: number, perPage?: number): Promise<Paginator>;
|
|
143
|
-
insertGraph(data: Record<string, unknown> | Record<string, unknown>[], options?: InsertGraphOptions): Promise<any>;
|
|
144
|
-
upsertGraph(data: Record<string, unknown> | Record<string, unknown>[], options?: UpsertGraphOptions): Promise<any>;
|
|
145
|
-
with(...relations: (string | Record<string, (qb: QueryBuilder) => void>)[]): QueryBuilder;
|
|
146
|
-
/**
|
|
147
|
-
* Whitelist allowed relations (and nested paths) for eager loading.
|
|
148
|
-
* Throws if a relation path is not in the allow list.
|
|
149
|
-
*
|
|
150
|
-
* Supports dotted paths for granular control:
|
|
151
|
-
* - `allowGraph("posts")` allows `posts`, `posts.author`, `posts.author.profile`, etc.
|
|
152
|
-
* - `allowGraph("posts.author")` allows `posts.author` and `posts.author.profile`,
|
|
153
|
-
* but NOT bare `posts` or `posts.comments`.
|
|
154
|
-
*
|
|
155
|
-
* Multiple arguments are merged: `allowGraph("posts", "profile")`
|
|
156
|
-
*/
|
|
157
|
-
allowGraph(...expressions: string[]): QueryBuilder;
|
|
158
|
-
updateMany(data: Record<string, unknown>): Promise<number>;
|
|
159
|
-
deleteMany(): Promise<number>;
|
|
160
|
-
withTrashed(): QueryBuilder;
|
|
161
|
-
onlyTrashed(): QueryBuilder;
|
|
162
|
-
whereIn(column: string, values: unknown[]): QueryBuilder;
|
|
163
|
-
whereInPivot(column: string, values: unknown[]): QueryBuilder;
|
|
164
|
-
has(relationName: string): QueryBuilder;
|
|
165
|
-
whereHas(relationName: string, callback?: (qb: QueryBuilder) => void): QueryBuilder;
|
|
166
|
-
whereDoesntHave(relationName: string, callback?: (qb: QueryBuilder) => void): QueryBuilder;
|
|
167
|
-
where(column: string, operator: unknown, value?: unknown): QueryBuilder;
|
|
168
|
-
whereRef(col1: string, operator: string, col2: string): QueryBuilder;
|
|
169
|
-
orWhere(column: string, operator: unknown, value?: unknown): QueryBuilder;
|
|
170
|
-
orderBy(column: string, direction?: "asc" | "desc"): QueryBuilder;
|
|
171
|
-
limit(n: number): QueryBuilder;
|
|
172
|
-
offset(n: number): QueryBuilder;
|
|
173
|
-
select(...columns: string[]): QueryBuilder;
|
|
174
|
-
selectAll(table?: string): QueryBuilder;
|
|
175
|
-
innerJoin(table: string, lhs: string, rhs: string): QueryBuilder;
|
|
176
|
-
leftJoin(table: string, lhs: string, rhs: string): QueryBuilder;
|
|
177
|
-
groupBy(...columns: string[]): QueryBuilder;
|
|
178
|
-
having(column: string, operator: string, value: unknown): QueryBuilder;
|
|
179
|
-
withoutGlobalScope(name: string): QueryBuilder;
|
|
180
|
-
all(): QueryBuilder;
|
|
181
|
-
/** @internal Access underlying Kysely builder for raw SQL operations */
|
|
182
|
-
_getKyselyQb(): any;
|
|
183
|
-
/** @internal Replace the underlying Kysely builder */
|
|
184
|
-
_replaceKyselyQb(newQb: any): void;
|
|
185
|
-
when(condition: unknown, callback: (q: QueryBuilder) => QueryBuilder): QueryBuilder;
|
|
186
|
-
unless(condition: unknown, callback: (q: QueryBuilder) => QueryBuilder): QueryBuilder;
|
|
72
|
+
interface SchemaSnapshot {
|
|
73
|
+
version: 1;
|
|
74
|
+
tables: SchemaTable[];
|
|
187
75
|
}
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
/** Detach related model(s) for many-to-many relations. */
|
|
194
|
-
detach(ids?: number | number[] | string | string[]): Promise<void>;
|
|
195
|
-
/** Sync related models: attaches new IDs, detaches missing ones. */
|
|
196
|
-
sync(ids: (number | string)[] | Record<number | string, Record<string, unknown>>): Promise<void>;
|
|
197
|
-
/** Sync without detaching existing IDs. */
|
|
198
|
-
syncWithoutDetaching(ids: (number | string)[]): Promise<void>;
|
|
199
|
-
/** Update pivot data for a specific related model. */
|
|
200
|
-
updateExistingPivot(id: number | string, data: Record<string, unknown>): Promise<void>;
|
|
76
|
+
interface SchemaDiff {
|
|
77
|
+
type: "createTable" | "dropTable" | "addColumn" | "dropColumn" | "alterColumn" | "addIndex" | "dropIndex";
|
|
78
|
+
table: string;
|
|
79
|
+
column?: string;
|
|
80
|
+
details?: Record<string, unknown>;
|
|
201
81
|
}
|
|
202
82
|
//#endregion
|
|
203
|
-
//#region
|
|
83
|
+
//#region src/config.d.ts
|
|
84
|
+
declare function defineConfig(config: PetaMigrateConfig): PetaMigrateConfig;
|
|
85
|
+
declare function loadConfig(): Promise<ResolvedConfig>;
|
|
86
|
+
declare function loadMigrationFiles(dir: string): Promise<MigrationFile[]>;
|
|
204
87
|
/**
|
|
205
|
-
*
|
|
206
|
-
*
|
|
207
|
-
*
|
|
208
|
-
* ```ts
|
|
209
|
-
* const myPlugin = (options?: MyOptions): Plugin =>
|
|
210
|
-
* (def) => {
|
|
211
|
-
* def.addGlobalScope?.('active', (q) => q.where('active', true))
|
|
212
|
-
* return def
|
|
213
|
-
* }
|
|
214
|
-
* ```
|
|
88
|
+
* Load model definitions from glob patterns.
|
|
89
|
+
* Dynamically imports each matched file and collects ModelDefinition exports.
|
|
90
|
+
* Models are keyed by their `.name` property.
|
|
215
91
|
*/
|
|
216
|
-
|
|
217
|
-
//#endregion
|
|
218
|
-
//#region ../orm/src/columns/schema.d.ts
|
|
219
|
-
interface Constraint {
|
|
220
|
-
type: string;
|
|
221
|
-
args: unknown[];
|
|
222
|
-
}
|
|
223
|
-
//#endregion
|
|
224
|
-
//#region ../orm/src/columns/column.d.ts
|
|
225
|
-
interface Column<out T = unknown> {
|
|
226
|
-
readonly arkType: unknown;
|
|
227
|
-
readonly dataType: string;
|
|
228
|
-
readonly args: readonly unknown[];
|
|
229
|
-
readonly constraints: readonly Constraint[];
|
|
230
|
-
readonly isNullable: boolean;
|
|
231
|
-
readonly isPrimaryKey: boolean;
|
|
232
|
-
readonly isUnique: boolean;
|
|
233
|
-
readonly defaultValue: unknown;
|
|
234
|
-
hasConstraint(type: string): boolean;
|
|
235
|
-
parse(value: unknown): T;
|
|
236
|
-
assert(value: unknown): T;
|
|
237
|
-
primaryKey(): Column<T>;
|
|
238
|
-
nullable(): Column<T | null>;
|
|
239
|
-
default<V>(value: V): Column<T>;
|
|
240
|
-
unique(): Column<T>;
|
|
241
|
-
index(): Column<T>;
|
|
242
|
-
min(n: number): Column<T>;
|
|
243
|
-
max(n: number): Column<T>;
|
|
244
|
-
email(): Column<T>;
|
|
245
|
-
url(): Column<T>;
|
|
246
|
-
pattern(regex: RegExp | string): Column<T>;
|
|
247
|
-
references(table: () => unknown, columns: string[]): Column<T>;
|
|
248
|
-
}
|
|
249
|
-
type ColumnShape = Record<string, Column>;
|
|
250
|
-
//#endregion
|
|
251
|
-
//#region ../orm/src/types.d.ts
|
|
252
|
-
interface ModelLike {
|
|
253
|
-
get<T = unknown>(key: string): T;
|
|
254
|
-
set(key: string, value: unknown): void;
|
|
255
|
-
}
|
|
256
|
-
interface ORMLike {
|
|
257
|
-
readonly kysely: Database;
|
|
258
|
-
register(model: ModelDefinition$1<any>): void;
|
|
259
|
-
registerAll(...models: (ModelDefinition$1<any> | ModelDefinition$1<any>[])[]): void;
|
|
260
|
-
destroy(): Promise<void>;
|
|
261
|
-
transaction<T>(fn: (trx: import("kysely").Kysely<Record<string, never>>) => Promise<T>): Promise<T>;
|
|
262
|
-
readonly models: ReadonlyMap<string, ModelDefinition$1<any>>;
|
|
263
|
-
getModel<T extends ColumnShape = ColumnShape>(name: string): ModelDefinition$1<T> | undefined;
|
|
264
|
-
}
|
|
265
|
-
interface ModelDefinition$1<TColumns extends ColumnShape = ColumnShape> {
|
|
266
|
-
readonly table: string;
|
|
267
|
-
readonly columns: TColumns;
|
|
268
|
-
readonly relations: Record<string, Relation>;
|
|
269
|
-
readonly name: string;
|
|
270
|
-
_orm: ORMLike | null;
|
|
271
|
-
query(): QueryBuilder;
|
|
272
|
-
find(id: number | string): Promise<ModelInstance | undefined>;
|
|
273
|
-
findOrFail(id: number | string): Promise<ModelInstance>;
|
|
274
|
-
first(): Promise<ModelInstance | undefined>;
|
|
275
|
-
create(data: Record<string, unknown>): Promise<ModelInstance>;
|
|
276
|
-
insert(data: Record<string, unknown>): Promise<ModelInstance>;
|
|
277
|
-
insertMany(dataArray: Record<string, unknown>[]): Promise<ModelInstance[]>;
|
|
278
|
-
update(id: number | string, data: Record<string, unknown>): Promise<ModelInstance>;
|
|
279
|
-
delete(id: number | string): Promise<void>;
|
|
280
|
-
hydrate(row: Record<string, unknown>): ModelInstance;
|
|
281
|
-
on(event: string, callback: (model: ModelInstance) => void | Promise<void>): () => void;
|
|
282
|
-
getHooks(): HookManager;
|
|
283
|
-
addGlobalScope(name: string, callback: (qb: QueryBuilder) => void): void;
|
|
284
|
-
removeGlobalScope(name: string): void;
|
|
285
|
-
getGlobalScopes(): Map<string, (qb: QueryBuilder) => void> | undefined;
|
|
286
|
-
_init(orm: ORMLike): void;
|
|
287
|
-
}
|
|
288
|
-
//#endregion
|
|
289
|
-
//#region ../orm/src/hooks/index.d.ts
|
|
290
|
-
type LifecycleEvent = "beforeCreate" | "afterCreate" | "beforeUpdate" | "afterUpdate" | "beforeSave" | "afterSave" | "beforeDelete" | "afterDelete" | "beforeRestore" | "afterRestore" | "beforeForceDelete" | "afterForceDelete";
|
|
291
|
-
type HookCallback = (model: ModelLike) => void | Promise<void>;
|
|
292
|
-
interface HookManager {
|
|
293
|
-
on(event: LifecycleEvent, callback: HookCallback): () => void;
|
|
294
|
-
off(event: LifecycleEvent, callback: HookCallback): void;
|
|
295
|
-
trigger(event: LifecycleEvent, model: ModelLike): Promise<void>;
|
|
296
|
-
clone(): HookManager;
|
|
297
|
-
}
|
|
298
|
-
//#endregion
|
|
299
|
-
//#region ../orm/src/hooks/static.d.ts
|
|
300
|
-
interface StaticHookArgs {
|
|
301
|
-
/** Transform the mutating query into a SELECT to preview affected rows */
|
|
302
|
-
asFindQuery(): QueryBuilder;
|
|
303
|
-
/** Cancel the mutation and return a custom result */
|
|
304
|
-
cancelQuery(result: unknown): void;
|
|
305
|
-
/** The column data being inserted/updated (for create/update hooks) */
|
|
306
|
-
inputItems?: Record<string, unknown>[];
|
|
307
|
-
}
|
|
308
|
-
type StaticHookCallback = (args: StaticHookArgs) => void | Promise<void>;
|
|
92
|
+
declare function loadModels(patterns: string | string[]): Promise<Map<string, ModelDefinition>>;
|
|
309
93
|
//#endregion
|
|
310
|
-
//#region
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
get<T = unknown>(key: string): T;
|
|
317
|
-
set(key: string, value: unknown): void;
|
|
318
|
-
fill(data: Record<string, unknown>): void;
|
|
319
|
-
reset(): void;
|
|
320
|
-
$getRelation<T = unknown>(name: string): T;
|
|
321
|
-
$setRelation(name: string, value: unknown): void;
|
|
322
|
-
$hasRelation(name: string): boolean;
|
|
323
|
-
$relationData(): Record<string, unknown>;
|
|
324
|
-
$load(...relations: string[]): Promise<void>;
|
|
325
|
-
$related(name: string): RelationQuery;
|
|
326
|
-
$save(): Promise<this>;
|
|
327
|
-
$delete(): Promise<void>;
|
|
328
|
-
$forceDelete(): Promise<void>;
|
|
329
|
-
$restore(): Promise<void>;
|
|
330
|
-
$trashed(): boolean;
|
|
331
|
-
$reload(): Promise<void>;
|
|
332
|
-
$toJSON(): Record<string, unknown>;
|
|
333
|
-
toJSON(): Record<string, unknown>;
|
|
334
|
-
}
|
|
335
|
-
interface ModelDefinition<TColumns extends ColumnShape = ColumnShape> {
|
|
336
|
-
readonly table: string;
|
|
337
|
-
readonly columns: TColumns;
|
|
338
|
-
readonly relations: Record<string, Relation>;
|
|
339
|
-
readonly name: string;
|
|
340
|
-
_orm: ORMLike | null;
|
|
341
|
-
query(): QueryBuilder;
|
|
342
|
-
find(id: number | string): Promise<ModelInstance | undefined>;
|
|
343
|
-
findOrFail(id: number | string): Promise<ModelInstance>;
|
|
344
|
-
first(): Promise<ModelInstance | undefined>;
|
|
345
|
-
create(data: Record<string, unknown>): Promise<ModelInstance>;
|
|
346
|
-
insert(data: Record<string, unknown>): Promise<ModelInstance>;
|
|
347
|
-
insertMany(dataArray: Record<string, unknown>[]): Promise<ModelInstance[]>;
|
|
348
|
-
update(id: number | string, data: Record<string, unknown>): Promise<ModelInstance>;
|
|
349
|
-
delete(id: number | string): Promise<void>;
|
|
350
|
-
insertGraph(data: Record<string, unknown> | Record<string, unknown>[], options?: InsertGraphOptions): Promise<any>;
|
|
351
|
-
upsertGraph(data: Record<string, unknown> | Record<string, unknown>[], options?: UpsertGraphOptions): Promise<any>;
|
|
352
|
-
hydrate(row: Record<string, unknown>): ModelInstance;
|
|
353
|
-
use(plugin: Plugin): ModelDefinition;
|
|
354
|
-
makeHelper<A extends any[], R>(fn: (qb: QueryBuilder, ...args: A) => R): (...args: A) => R;
|
|
355
|
-
on(event: string, callback: (model: ModelInstance) => void | Promise<void>): () => void;
|
|
356
|
-
getHooks(): HookManager;
|
|
357
|
-
beforeDelete(callback: StaticHookCallback): () => void;
|
|
358
|
-
afterDelete(callback: StaticHookCallback): () => void;
|
|
359
|
-
beforeUpdate(callback: StaticHookCallback): () => void;
|
|
360
|
-
afterUpdate(callback: StaticHookCallback): () => void;
|
|
361
|
-
beforeCreate(callback: StaticHookCallback): () => void;
|
|
362
|
-
afterCreate(callback: StaticHookCallback): () => void;
|
|
363
|
-
beforeFind(callback: StaticHookCallback): () => void;
|
|
364
|
-
afterFind(callback: StaticHookCallback): () => void;
|
|
365
|
-
addGlobalScope(name: string, callback: (qb: QueryBuilder) => void): void;
|
|
366
|
-
removeGlobalScope(name: string): void;
|
|
367
|
-
getGlobalScopes(): Map<string, (qb: QueryBuilder) => void> | undefined;
|
|
368
|
-
registerTimestamps?(createdAtCol?: string, updatedAtCol?: string): void;
|
|
369
|
-
registerSoftDeletes?(deletedAtCol?: string): void;
|
|
370
|
-
discover?(): Promise<never>;
|
|
371
|
-
_init(orm: ORMLike): void;
|
|
372
|
-
}
|
|
373
|
-
//#endregion
|
|
374
|
-
//#region ../orm/src/collection/index.d.ts
|
|
375
|
-
interface Collection {
|
|
376
|
-
readonly length: number;
|
|
377
|
-
[Symbol.iterator](): Iterator<ModelInstance>;
|
|
378
|
-
at(index: number): ModelInstance | undefined;
|
|
379
|
-
first(): ModelInstance | undefined;
|
|
380
|
-
last(): ModelInstance | undefined;
|
|
381
|
-
all(): ModelInstance[];
|
|
382
|
-
findBy(id: number | string): ModelInstance | undefined;
|
|
383
|
-
find(callback: (item: ModelInstance, index: number) => boolean): ModelInstance | undefined;
|
|
384
|
-
some(callback: (item: ModelInstance, index: number) => boolean): boolean;
|
|
385
|
-
includes(item: ModelInstance): boolean;
|
|
386
|
-
isEmpty(): boolean;
|
|
387
|
-
isNotEmpty(): boolean;
|
|
388
|
-
get(key: string): unknown[];
|
|
389
|
-
pluck(key: string): unknown[];
|
|
390
|
-
groupBy(key: string): Record<string, ModelInstance[]>;
|
|
391
|
-
keyBy(key: string): Record<string, ModelInstance>;
|
|
392
|
-
map<T>(fn: (item: ModelInstance, index: number) => T): T[];
|
|
393
|
-
filter(fn: (item: ModelInstance, index: number) => boolean): Collection;
|
|
394
|
-
reduce<T>(fn: (acc: T, item: ModelInstance, index: number) => T, initial: T): T;
|
|
395
|
-
forEach(fn: (item: ModelInstance, index: number) => void): void;
|
|
396
|
-
each(fn: (item: ModelInstance, index: number) => void): Collection;
|
|
397
|
-
unique(key?: string): Collection;
|
|
398
|
-
sortBy(key: string, direction?: "asc" | "desc"): Collection;
|
|
399
|
-
shuffle(): Collection;
|
|
400
|
-
take(n: number): Collection;
|
|
401
|
-
skip(n: number): Collection;
|
|
402
|
-
chunk(size: number): Collection[];
|
|
403
|
-
sum(key: string): number;
|
|
404
|
-
avg(key: string): number;
|
|
405
|
-
min(key: string): number;
|
|
406
|
-
max(key: string): number;
|
|
407
|
-
diff(other: Collection): Collection;
|
|
408
|
-
intersect(other: Collection): Collection;
|
|
409
|
-
concat(other: Collection): Collection;
|
|
410
|
-
push(...items: ModelInstance[]): void;
|
|
411
|
-
load(...relations: string[]): Promise<Collection>;
|
|
412
|
-
toJSON(): Record<string, unknown>[];
|
|
413
|
-
}
|
|
94
|
+
//#region src/differ.d.ts
|
|
95
|
+
/**
|
|
96
|
+
* Compare two SchemaSnapshots and produce a list of SchemaDiff operations.
|
|
97
|
+
* Compares `prev` (old) vs `next` (new) to generate a migration plan.
|
|
98
|
+
*/
|
|
99
|
+
declare function diffSnapshots(prev: SchemaSnapshot, next: SchemaSnapshot): SchemaDiff[];
|
|
414
100
|
//#endregion
|
|
415
101
|
//#region src/generator.d.ts
|
|
416
102
|
interface GeneratorOptions {
|
|
@@ -418,9 +104,18 @@ interface GeneratorOptions {
|
|
|
418
104
|
}
|
|
419
105
|
interface MigrationGenerator {
|
|
420
106
|
generateInitialMigration(models: Map<string, ModelDefinition>, options?: GeneratorOptions): string;
|
|
107
|
+
generateMigrationFromDiff(diffs: SchemaDiff[], options?: GeneratorOptions): string;
|
|
421
108
|
}
|
|
422
109
|
declare function createMigrationGenerator(): MigrationGenerator;
|
|
423
110
|
//#endregion
|
|
111
|
+
//#region src/pusher.d.ts
|
|
112
|
+
/**
|
|
113
|
+
* Push current model schema directly to the database.
|
|
114
|
+
* Creates tables and columns that don't exist yet (no destructive changes).
|
|
115
|
+
* Returns list of tables that were created.
|
|
116
|
+
*/
|
|
117
|
+
declare function pushSchema(db: Kysely<unknown>, models: Map<string, ModelDefinition>): Promise<string[]>;
|
|
118
|
+
//#endregion
|
|
424
119
|
//#region src/runner.d.ts
|
|
425
120
|
interface MigrationRunner {
|
|
426
121
|
ensureTable(): Promise<void>;
|
|
@@ -431,4 +126,18 @@ interface MigrationRunner {
|
|
|
431
126
|
}
|
|
432
127
|
declare function createMigrationRunner(db: Kysely<Record<string, never>>, table?: string): MigrationRunner;
|
|
433
128
|
//#endregion
|
|
434
|
-
|
|
129
|
+
//#region src/snapshot.d.ts
|
|
130
|
+
/**
|
|
131
|
+
* Extract a SchemaSnapshot from a map of model definitions.
|
|
132
|
+
*/
|
|
133
|
+
declare function createSnapshot(models: Map<string, ModelDefinition>): SchemaSnapshot;
|
|
134
|
+
/**
|
|
135
|
+
* Load snapshot from a JSON file path.
|
|
136
|
+
*/
|
|
137
|
+
declare function loadSnapshot(filePath: string): Promise<SchemaSnapshot | null>;
|
|
138
|
+
/**
|
|
139
|
+
* Save snapshot to a JSON file path.
|
|
140
|
+
*/
|
|
141
|
+
declare function saveSnapshot(filePath: string, snapshot: SchemaSnapshot): Promise<void>;
|
|
142
|
+
//#endregion
|
|
143
|
+
export { type GeneratorOptions, type MigrationFile, type MigrationGenerator, type MigrationRecord, type MigrationRunner, type MigrationStatus, type PetaMigrateConfig, type ResolvedConfig, type SchemaColumn, type SchemaDiff, type SchemaIndex, type SchemaSnapshot, type SchemaTable, computeChecksum, createMigrationGenerator, createMigrationRunner, createSnapshot, defineConfig, diffSnapshots, loadChecksums, loadConfig, loadMigrationFiles, loadModels, loadSnapshot, pushSchema, saveChecksums, saveSnapshot, verifyChecksum };
|
package/dist/index.mjs
CHANGED
|
@@ -1,2 +1,3 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
1
|
+
import { t as pushSchema } from "./pusher-DJvKHlOT.mjs";
|
|
2
|
+
import { a as createMigrationGenerator, c as loadConfig, d as computeChecksum, f as loadChecksums, i as createMigrationRunner, l as loadMigrationFiles, m as verifyChecksum, n as loadSnapshot, o as diffSnapshots, p as saveChecksums, r as saveSnapshot, s as defineConfig, t as createSnapshot, u as loadModels } from "./snapshot-DopEB8mx.mjs";
|
|
3
|
+
export { computeChecksum, createMigrationGenerator, createMigrationRunner, createSnapshot, defineConfig, diffSnapshots, loadChecksums, loadConfig, loadMigrationFiles, loadModels, loadSnapshot, pushSchema, saveChecksums, saveSnapshot, verifyChecksum };
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
//#region \0rolldown/runtime.js
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __exportAll = (all, no_symbols) => {
|
|
4
|
+
let target = {};
|
|
5
|
+
for (var name in all) __defProp(target, name, {
|
|
6
|
+
get: all[name],
|
|
7
|
+
enumerable: true
|
|
8
|
+
});
|
|
9
|
+
if (!no_symbols) __defProp(target, Symbol.toStringTag, { value: "Module" });
|
|
10
|
+
return target;
|
|
11
|
+
};
|
|
12
|
+
//#endregion
|
|
13
|
+
//#region src/pusher.ts
|
|
14
|
+
var pusher_exports = /* @__PURE__ */ __exportAll({ pushSchema: () => pushSchema });
|
|
15
|
+
/**
|
|
16
|
+
* Push current model schema directly to the database.
|
|
17
|
+
* Creates tables and columns that don't exist yet (no destructive changes).
|
|
18
|
+
* Returns list of tables that were created.
|
|
19
|
+
*/
|
|
20
|
+
async function pushSchema(db, models) {
|
|
21
|
+
const createdTables = [];
|
|
22
|
+
for (const [, model] of models) {
|
|
23
|
+
if (!model.table) continue;
|
|
24
|
+
if (await tableExists(db, model.table)) continue;
|
|
25
|
+
const qb = db.schema.createTable(model.table).ifNotExists();
|
|
26
|
+
const columns = model.columns;
|
|
27
|
+
for (const [name, col] of Object.entries(columns)) qb.addColumn(name, mapPushType(col), (cb) => buildColumn(col, cb));
|
|
28
|
+
await qb.execute();
|
|
29
|
+
createdTables.push(model.table);
|
|
30
|
+
for (const [colName, col] of Object.entries(columns)) if (col.hasConstraint("index") && !col.isPrimaryKey && !col.isUnique) await db.schema.createIndex(`${model.table}_${colName}_index`).on(model.table).column(colName).execute();
|
|
31
|
+
}
|
|
32
|
+
return createdTables;
|
|
33
|
+
}
|
|
34
|
+
async function tableExists(db, name) {
|
|
35
|
+
try {
|
|
36
|
+
return (await db.introspection.getTables({ withInternalKyselyTables: true })).some((t) => t.name === name);
|
|
37
|
+
} catch {
|
|
38
|
+
return false;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
function buildColumn(col, cb) {
|
|
42
|
+
if (col.isPrimaryKey) {
|
|
43
|
+
if (col.dataType === "integer") cb = cb.autoIncrement();
|
|
44
|
+
cb = cb.primaryKey();
|
|
45
|
+
}
|
|
46
|
+
if (!col.isNullable && !col.isPrimaryKey) cb = cb.notNull();
|
|
47
|
+
if (col.defaultValue !== void 0 && typeof col.defaultValue !== "function") cb = cb.defaultTo(col.defaultValue);
|
|
48
|
+
if (col.isUnique && !col.isPrimaryKey) cb = cb.unique();
|
|
49
|
+
const refConstraint = col.constraints.find((c) => c.type === "references");
|
|
50
|
+
if (refConstraint?.args[0]) {
|
|
51
|
+
const targetTable = (typeof refConstraint.args[0] === "function" ? refConstraint.args[0]() : refConstraint.args[0])?.table;
|
|
52
|
+
const targetColumns = refConstraint.args[1];
|
|
53
|
+
if (typeof targetTable === "string" && targetTable && targetColumns?.length) {
|
|
54
|
+
const first = targetColumns[0];
|
|
55
|
+
if (first) cb = cb.references(`${targetTable}.${first}`);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
return cb;
|
|
59
|
+
}
|
|
60
|
+
function mapPushType(col) {
|
|
61
|
+
switch (col.dataType) {
|
|
62
|
+
case "integer":
|
|
63
|
+
case "smallint":
|
|
64
|
+
case "bigint":
|
|
65
|
+
case "text":
|
|
66
|
+
case "boolean":
|
|
67
|
+
case "timestamp":
|
|
68
|
+
case "date":
|
|
69
|
+
case "float":
|
|
70
|
+
case "double":
|
|
71
|
+
case "uuid": return col.dataType;
|
|
72
|
+
case "string": {
|
|
73
|
+
const max = col.args[0];
|
|
74
|
+
return max != null ? `varchar(${max})` : "varchar";
|
|
75
|
+
}
|
|
76
|
+
case "json":
|
|
77
|
+
case "jsonb": return "json";
|
|
78
|
+
case "decimal": {
|
|
79
|
+
const p = col.args[0];
|
|
80
|
+
const s = col.args[1];
|
|
81
|
+
return p != null ? `decimal(${p}, ${s ?? 0})` : "decimal";
|
|
82
|
+
}
|
|
83
|
+
case "enum": return "text";
|
|
84
|
+
default: return col.dataType;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
//#endregion
|
|
88
|
+
export { pusher_exports as n, pushSchema as t };
|