@forklaunch/core 4.3.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"}
@@ -525,6 +525,10 @@ var complianceRegistry = sharedRegistry(
525
525
  function getEntityComplianceFields(entityName) {
526
526
  return complianceRegistry.get(entityName);
527
527
  }
528
+ var DEFAULT_RETENTION_FROM = "createdAt";
529
+ function retentionFrom(policy) {
530
+ return policy.from ?? DEFAULT_RETENTION_FROM;
531
+ }
528
532
  var DURATION_REGEX = /^P(?:(\d+)Y)?(?:(\d+)M)?(?:(\d+)D)?$/;
529
533
  function parseDuration(iso) {
530
534
  const match = DURATION_REGEX.exec(iso);
@@ -704,8 +708,9 @@ var RetentionService = class {
704
708
  }
705
709
  }
706
710
  }
711
+ const from = retentionFrom(policy);
707
712
  const filter = {
708
- createdAt: { $lt: cutoff }
713
+ [from]: { $ne: null, $lt: cutoff }
709
714
  };
710
715
  if (policy.action === "anonymize") {
711
716
  filter["retentionAnonymizedAt"] = null;
@@ -717,6 +722,7 @@ var RetentionService = class {
717
722
  this.otel.info("[RetentionService] Dry run \u2014 would process", {
718
723
  entityName,
719
724
  action: policy.action,
725
+ from,
720
726
  count
721
727
  });
722
728
  if (policy.action === "delete") stats.deleted += count;
@@ -737,7 +743,7 @@ var RetentionService = class {
737
743
  try {
738
744
  const records = await em.find(entityClass, filter, {
739
745
  limit: batchSize,
740
- orderBy: { createdAt: "ASC" }
746
+ orderBy: { [from]: "ASC" }
741
747
  });
742
748
  if (records.length === 0) break;
743
749
  if (policy.action === "delete") {