@duraxis/orm 0.2.2 → 0.3.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/dist/index.js CHANGED
@@ -1223,6 +1223,17 @@ function mapJunctionSchemas(metadata) {
1223
1223
  }
1224
1224
  return [...schemas.values()];
1225
1225
  }
1226
+ // src/schema/schema-exporter.ts
1227
+ function exportRegistrySchema(registry) {
1228
+ return [...registry.all().map(mapEntitySchema), ...mapJunctionSchemas(registry.all())].map(normalizeCollection);
1229
+ }
1230
+ function exportSchema(entities) {
1231
+ return exportRegistrySchema(new MetadataRegistry(entities).finalize()).map((schema) => ({
1232
+ ...schema,
1233
+ indexes: [...schema.indexes].sort((a, b) => a.name < b.name ? -1 : a.name > b.name ? 1 : 0),
1234
+ ...schema.foreignKeys ? { foreignKeys: [...schema.foreignKeys].sort((a, b) => a.name < b.name ? -1 : a.name > b.name ? 1 : 0) } : {}
1235
+ })).sort((a, b) => a.name < b.name ? -1 : a.name > b.name ? 1 : 0);
1236
+ }
1226
1237
  // src/schema/schema-manager.ts
1227
1238
  function same(left, right) {
1228
1239
  return JSON.stringify(left) == JSON.stringify(right);
@@ -1241,9 +1252,9 @@ var SchemaManager = class {
1241
1252
  }
1242
1253
  async initialize() {
1243
1254
  const persisted = new Map((await this.database.getCollectionDefinitions()).map((definition) => [definition.name, normalizeCollection(definition)]));
1244
- const expectedSchemas = [...this.registry.all().map((metadata) => mapEntitySchema(metadata)), ...mapJunctionSchemas(this.registry.all())].map((schema) => normalizeCollection(schema));
1255
+ const expectedSchemas = exportRegistrySchema(this.registry);
1245
1256
  const missing = expectedSchemas.filter((schema) => !persisted.has(schema.name));
1246
- if (missing.length && this.mode == "validate") throw new SchemaViolationError(`Missing collection: ${missing[0].name}`);
1257
+ if (missing.length && this.mode != "create") throw new SchemaViolationError(`Missing collection: ${missing[0].name}`);
1247
1258
  if (missing.length) {
1248
1259
  await this.mutate(() => this.database.defineCollections(missing), `Could not create collections: ${missing.map((schema) => schema.name).join(", ")}`);
1249
1260
  for (const schema of missing) persisted.set(schema.name, schema);
@@ -1253,11 +1264,12 @@ var SchemaManager = class {
1253
1264
  if (!current) throw new SchemaViolationError(`Missing collection: ${expected.name}`);
1254
1265
  this.validateFields(expected, current);
1255
1266
  if (!same(expected.foreignKeys ?? [], current.foreignKeys ?? [])) throw new SchemaViolationError(`Incompatible foreign keys: ${expected.name}`);
1267
+ if (this.mode == "validate-managed" && current.indexes.length != expected.indexes.length) throw new SchemaViolationError(`Unexpected managed index: ${expected.name}`);
1256
1268
  const actualIndexes = indexMap(current);
1257
1269
  for (const expectedIndex of expected.indexes) {
1258
1270
  const actual = actualIndexes.get(expectedIndex.name);
1259
1271
  if (!actual) {
1260
- if (this.mode == "validate") throw new SchemaViolationError(`Missing index: ${expected.name}/${expectedIndex.name}`);
1272
+ if (this.mode != "create") throw new SchemaViolationError(`Missing index: ${expected.name}/${expectedIndex.name}`);
1261
1273
  await this.mutate(() => this.database.addIndex(expected.name, expectedIndex), `Could not create index: ${expected.name}/${expectedIndex.name}`);
1262
1274
  continue;
1263
1275
  }
@@ -1383,6 +1395,6 @@ var DuraxisORM = class {
1383
1395
  if (this.#lifecycle != "initialized") throw new OrmNotInitializedError();
1384
1396
  }
1385
1397
  };
1386
- export { Column, CommitOutcomeUnknownError, DuplicateRecordError, DuraxisORM, DuraxisOrmError, Entity, EntityHydrator, EntityMapper, EntityMetadata, EntityNotRegisteredError, Index, InvalidColumnError, InvalidMetadataError, InvalidQueryError, JoinTable, ManyToMany, ManyToOne, MetadataRegistry, MissingPrimaryKeyError, OneToMany, OneToOne, OrmClosedError, OrmNotInitializedError, OrmTransactionManager, PrimaryKey, QueryBuilder, RecordNotFoundError, RelationLoader, RelationOperations, Repository, SchemaManager, SchemaViolationError, TransactionConflictError, TransactionManagerClosedError, UniqueIndexViolationOrmError, ValueTransformer };
1398
+ export { Column, CommitOutcomeUnknownError, DuplicateRecordError, DuraxisORM, DuraxisOrmError, Entity, EntityHydrator, EntityMapper, EntityMetadata, EntityNotRegisteredError, Index, InvalidColumnError, InvalidMetadataError, InvalidQueryError, JoinTable, ManyToMany, ManyToOne, MetadataRegistry, MissingPrimaryKeyError, OneToMany, OneToOne, OrmClosedError, OrmNotInitializedError, OrmTransactionManager, PrimaryKey, QueryBuilder, RecordNotFoundError, RelationLoader, RelationOperations, Repository, SchemaManager, SchemaViolationError, TransactionConflictError, TransactionManagerClosedError, UniqueIndexViolationOrmError, ValueTransformer, exportSchema };
1387
1399
 
1388
1400
  //# sourceMappingURL=index.js.map