@lobb-js/core 0.25.0 → 0.26.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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@lobb-js/core",
3
3
  "license": "UNLICENSED",
4
- "version": "0.25.0",
4
+ "version": "0.26.1",
5
5
  "type": "module",
6
6
  "publishConfig": {
7
7
  "access": "public"
@@ -76,6 +76,7 @@ export class MetaService {
76
76
 
77
77
  // filling the collection ui
78
78
  collections[collectionName].ui = collection.ui;
79
+ collections[collectionName].children = Lobb.instance.configManager.getCollectionChildren(collectionName);
79
80
  }
80
81
 
81
82
  return collections;
@@ -425,6 +425,45 @@ export class ConfigManager {
425
425
  return [];
426
426
  }
427
427
 
428
+ public getCollectionChildren(collectionName: string): { type: string; collection: string; field?: string }[] {
429
+ const allRelations = this.config.relations ?? [];
430
+ const allCollections = this.config.collections;
431
+ const children: { type: string; collection: string; field?: string }[] = [];
432
+ const seenJunctions = new Set<string>();
433
+
434
+ for (const relation of allRelations) {
435
+ if ((relation as any).type === "polymorphic") continue;
436
+ const reg = relation as RegularRelation;
437
+ if (reg.to.collection !== collectionName) continue;
438
+
439
+ const childCollection = reg.from.collection;
440
+ if ((allCollections[childCollection] as any)?.junction) {
441
+ if (seenJunctions.has(childCollection)) continue;
442
+ seenJunctions.add(childCollection);
443
+ const otherSide = allRelations.find((r) =>
444
+ !("type" in r && r.type === "polymorphic") &&
445
+ (r as RegularRelation).from.collection === childCollection &&
446
+ (r as RegularRelation).from.field !== reg.from.field,
447
+ ) as RegularRelation | undefined;
448
+ if (otherSide) {
449
+ children.push({ type: "m2m", collection: otherSide.to.collection });
450
+ }
451
+ } else {
452
+ children.push({ type: "fk", collection: childCollection, field: reg.from.field });
453
+ }
454
+ }
455
+
456
+ for (const relation of allRelations) {
457
+ if (!("type" in relation) || relation.type !== "polymorphic") continue;
458
+ const poly = relation as any;
459
+ if (Array.isArray(poly.to) && poly.to.includes(collectionName)) {
460
+ children.push({ type: "polymorphic", collection: poly.from.collection });
461
+ }
462
+ }
463
+
464
+ return children;
465
+ }
466
+
428
467
  public isCollectionSingleton(collectionName: string): boolean {
429
468
  const collection = this.config.collections[collectionName];
430
469
  if (!collection || collection.virtual) return false;
@@ -35,11 +35,27 @@ export class DatabaseSyncManager {
35
35
  // Startup tasks run first — applies any idempotent DB fixes before schema reading begins.
36
36
  await runCoreDbSetup();
37
37
 
38
+ const dbSchemaDiff = await this.getDbDifferences(specificColleciton);
39
+
40
+ // Pass 1: additive ops only (add-collection, add-field) — always safe, no data loss possible.
41
+ // Running these before migrations means any new collection defined in config already exists
42
+ // when migration code tries to seed it.
43
+ const additiveOps = dbSchemaDiff.filter(
44
+ (op) => op.op === "add-collection" || op.op === "add-field",
45
+ );
46
+ await this.applyDbSchemaDiff(additiveOps, forceSync);
47
+
48
+ // Migrations run after additive schema changes so they can safely insert into new collections.
38
49
  const migrationsManager = new MigrationsManager();
39
50
  await migrationsManager.init();
40
51
 
41
- const dbSchemaDiff = await this.getDbDifferences(specificColleciton);
42
- await this.applyDbSchemaDiff(dbSchemaDiff, forceSync);
52
+ // Pass 2: everything else (remove-collection, remove-field, alter-field, indexes).
53
+ // Destructive ops run after migrations so a migration can clear data before applyDbSchemaDiff
54
+ // would otherwise refuse to drop a column/table that still has rows.
55
+ const remainingOps = dbSchemaDiff.filter(
56
+ (op) => op.op !== "add-collection" && op.op !== "add-field",
57
+ );
58
+ await this.applyDbSchemaDiff(remainingOps, forceSync);
43
59
  }
44
60
 
45
61
  private async applyDbSchemaDiff(ops: SchemaDiffOp[], forceSync: boolean) {