@forklaunch/core 4.4.0 → 4.6.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.
@@ -0,0 +1,107 @@
1
+ /**
2
+ * Bring a component's database up to date when it starts, including the very
3
+ * first start of a service that has no migrations yet.
4
+ *
5
+ * A freshly scaffolded service has entities but no `migrations-<db>/` folder:
6
+ * MikroORM's `migration:create --initial` needs a live database, so the
7
+ * initial migration only exists once someone runs `migrate:init` locally. A
8
+ * service deployed before that used to start against an empty database and
9
+ * fail every request with `relation "<service>_<entity>" does not exist`.
10
+ *
11
+ * `migrateOrCreateSchema` runs under the same lock as `runMigrationsWithLock`
12
+ * and decides what to do from what the database already holds:
13
+ *
14
+ * - Migrations exist: apply them. If none has run yet and the leading ones
15
+ * only create tables that already exist (the schema was created from the
16
+ * entities by an earlier deploy), they are recorded as executed instead of
17
+ * run, exactly as MikroORM's own `migration:create --initial` does for a
18
+ * database whose tables already exist. Later migrations then run normally.
19
+ * - No migrations and none of the service's tables exist: create the schema
20
+ * from the entities (MikroORM SchemaGenerator) and say so loudly.
21
+ * - No migrations and some or all of the tables exist: never touch the
22
+ * schema; warn about anything missing.
23
+ *
24
+ * After migrating it compares the database with the entities and warns, with
25
+ * the SQL, when they differ. It never applies that difference.
26
+ *
27
+ * MongoDB has no tables to create (collections appear on first write), so it
28
+ * only runs its migrations.
29
+ */
30
+ import { type MigrationLockOrm, type RunMigrationsWithLockOptions } from './migrationLock';
31
+ type Execute = (sql: string, params?: unknown[], method?: 'all' | 'get' | 'run', ctx?: unknown) => Promise<unknown>;
32
+ interface MigratorLike {
33
+ up(): Promise<unknown>;
34
+ getExecuted(): Promise<unknown[]>;
35
+ getPending(): Promise<{
36
+ name: string;
37
+ }[]>;
38
+ getStorage?(): {
39
+ logMigration(params: {
40
+ name: string;
41
+ }): Promise<void>;
42
+ };
43
+ }
44
+ type MigrationConstructor = new (...args: any[]) => unknown;
45
+ type MigrationEntry = {
46
+ name?: string;
47
+ class: MigrationConstructor;
48
+ } | MigrationConstructor;
49
+ /** The slice of a MikroORM instance this needs. */
50
+ export interface MigrateOrCreateSchemaOrm extends MigrationLockOrm {
51
+ em: MigrationLockOrm['em'] & {
52
+ getConnection(): {
53
+ execute: Execute;
54
+ transactional<T>(cb: (trx: unknown) => Promise<T>): Promise<T>;
55
+ };
56
+ getPlatform(): {
57
+ getDefaultSchemaName?(): string | undefined;
58
+ getSchemaHelper?(): unknown;
59
+ };
60
+ getDriver(): unknown;
61
+ };
62
+ config: {
63
+ get(key: string): unknown;
64
+ };
65
+ schema?: unknown;
66
+ migrator?: MigratorLike;
67
+ getMigrator?: () => MigratorLike;
68
+ }
69
+ export interface MigrateOrCreateSchemaOptions extends Pick<RunMigrationsWithLockOptions, 'lockName'> {
70
+ log?: (message: string) => void;
71
+ warn?: (message: string) => void;
72
+ /**
73
+ * Compare the database with the entities after migrating and warn when they
74
+ * differ. Defaults to true.
75
+ */
76
+ checkDrift?: boolean;
77
+ }
78
+ export type MigrateOrCreateSchemaAction = 'created-schema' | 'migrated' | 'nothing-to-do';
79
+ export interface MigrateOrCreateSchemaResult {
80
+ action: MigrateOrCreateSchemaAction;
81
+ /** Migrations recorded as executed because their tables already existed. */
82
+ adopted: string[];
83
+ /** Service tables the database is missing when nothing could create them. */
84
+ missingTables: string[];
85
+ /** SQL that would bring the database in line with the entities, if any. */
86
+ drift?: string;
87
+ }
88
+ /**
89
+ * The SQL a migration's `up()` would run, captured without running it, or
90
+ * undefined when it does anything other than `addSql` with plain SQL.
91
+ */
92
+ export declare function captureMigrationSql(entry: MigrationEntry, driver: unknown, config: unknown): Promise<string[] | undefined>;
93
+ /**
94
+ * The tables a migration creates when it only creates schema (an initial
95
+ * migration), or undefined when it does anything else.
96
+ */
97
+ export declare function tablesCreatedByInitialMigration(statements: string[]): {
98
+ schema?: string;
99
+ name: string;
100
+ }[] | undefined;
101
+ /**
102
+ * Apply migrations, or create the schema from the entities when the service
103
+ * has no migrations and none of its tables exist. See the module comment.
104
+ */
105
+ export declare function migrateOrCreateSchema(orm: MigrateOrCreateSchemaOrm, options?: MigrateOrCreateSchemaOptions): Promise<MigrateOrCreateSchemaResult>;
106
+ export {};
107
+ //# sourceMappingURL=migrateOrCreateSchema.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"migrateOrCreateSchema.d.ts","sourceRoot":"","sources":["../../src/persistence/migrateOrCreateSchema.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,OAAO,EAEL,KAAK,gBAAgB,EACrB,KAAK,4BAA4B,EAClC,MAAM,iBAAiB,CAAC;AAEzB,KAAK,OAAO,GAAG,CACb,GAAG,EAAE,MAAM,EACX,MAAM,CAAC,EAAE,OAAO,EAAE,EAClB,MAAM,CAAC,EAAE,KAAK,GAAG,KAAK,GAAG,KAAK,EAC9B,GAAG,CAAC,EAAE,OAAO,KACV,OAAO,CAAC,OAAO,CAAC,CAAC;AAEtB,UAAU,YAAY;IACpB,EAAE,IAAI,OAAO,CAAC,OAAO,CAAC,CAAC;IACvB,WAAW,IAAI,OAAO,CAAC,OAAO,EAAE,CAAC,CAAC;IAClC,UAAU,IAAI,OAAO,CAAC;QAAE,IAAI,EAAE,MAAM,CAAA;KAAE,EAAE,CAAC,CAAC;IAC1C,UAAU,CAAC,IAAI;QAAE,YAAY,CAAC,MAAM,EAAE;YAAE,IAAI,EAAE,MAAM,CAAA;SAAE,GAAG,OAAO,CAAC,IAAI,CAAC,CAAA;KAAE,CAAC;CAC1E;AAcD,KAAK,oBAAoB,GAAG,KAAK,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,OAAO,CAAC;AAC5D,KAAK,cAAc,GACjB;IAAE,IAAI,CAAC,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,oBAAoB,CAAA;CAAE,GAAG,oBAAoB,CAAC;AAExE,mDAAmD;AACnD,MAAM,WAAW,wBAAyB,SAAQ,gBAAgB;IAChE,EAA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f,MAAM,EAAE,OAAO,GACd,OAAO,CAAC,MAAM,EAAE,GAAG,SAAS,CAAC,CAyB/B;AAED;;;GAGG;AACH,wBAAgB,+BAA+B,CAC7C,UAAU,EAAE,MAAM,EAAE,GACnB;IAAE,MAAM,CAAC,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAA;CAAE,EAAE,GAAG,SAAS,CAkBjD;AAQD;;;GAGG;AACH,wBAAsB,qBAAqB,CACzC,GAAG,EAAE,wBAAwB,EAC7B,OAAO,GAAE,4BAAiC,GACzC,OAAO,CAAC,2BAA2B,CAAC,CAkKtC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@forklaunch/core",
3
- "version": "4.4.0",
3
+ "version": "4.6.0",
4
4
  "description": "forklaunch-js core package. Contains useful building blocks.",
5
5
  "homepage": "https://github.com/forklaunch/forklaunch-js#readme",
6
6
  "bugs": {
@@ -120,6 +120,7 @@
120
120
  "pino-http": "^11.0.0",
121
121
  "pino-pretty": "^13.2.0",
122
122
  "redis": "^6.4.0",
123
+ "undici": "^8.14.0",
123
124
  "uuid": "^14.0.3",
124
125
  "zod": "^4.6.7"
125
126
  },