@dbml/core 7.1.0-alpha.0 → 7.1.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/lib/index.mjs CHANGED
@@ -9839,8 +9839,14 @@ function processColumnInDb(_) {
9839
9839
  }
9840
9840
  function expandDiagramViewWildcards(_) {
9841
9841
  for (let HY of _.diagramViews.values()) {
9842
- let P = HY.visibleEntities, UY = _.diagramViewWildcards.get(HY), WY = _.diagramViewExplicitlySet.get(HY);
9843
- !UY || !WY || UY.has("tableGroups") && P.tableGroups && P.tableGroups.length === 0 && (WY.has("tables") || WY.has("schemas") || (P.tableGroups = Array.from(_.tableGroups.values()).map((_) => ({ name: _.name }))));
9842
+ let P = HY.visibleEntities, UY = _.diagramViewWildcards.get(HY);
9843
+ if (UY) {
9844
+ if (UY.has("tables") || UY.has("schemas")) {
9845
+ P.tables = [], P.tableGroups = [], P.schemas = [];
9846
+ continue;
9847
+ }
9848
+ UY.has("tableGroups") && P.tableGroups && P.tableGroups.length === 0 && (P.tableGroups = Array.from(_.tableGroups.values()).map((_) => ({ name: _.name })));
9849
+ }
9844
9850
  }
9845
9851
  }
9846
9852
  function convertEnvToDb(_, HY) {
@@ -12118,14 +12124,46 @@ function findDiagramViewBlocks$1(_) {
12118
12124
  }
12119
12125
  return HY;
12120
12126
  }
12127
+ function emitTablesBlock(_, HY) {
12128
+ let P = HY.map((_) => _.schemaName === "public" ? _.name : `${_.schemaName}.${_.name}`);
12129
+ _.push(" Tables {"), P.forEach((HY) => _.push(` ${HY}`)), _.push(" }");
12130
+ }
12131
+ function emitTableGroupsBlock(_, HY) {
12132
+ _.push(" TableGroups {"), HY.forEach((HY) => _.push(` ${HY.name}`)), _.push(" }");
12133
+ }
12134
+ function emitSchemasBlock(_, HY) {
12135
+ _.push(" Schemas {"), HY.forEach((HY) => _.push(` ${HY.name}`)), _.push(" }");
12136
+ }
12137
+ function emitNotesBlock(_, HY) {
12138
+ _.push(" Notes {"), HY.forEach((HY) => _.push(` ${HY.name}`)), _.push(" }");
12139
+ }
12121
12140
  function generateDiagramViewBlock(_, HY) {
12122
- let P = [`DiagramView ${addDoubleQuoteIfNeeded(_)} {`];
12123
- if (HY?.tables !== void 0 && HY.tables !== null) if (HY.tables.length === 0) P.push(" Tables { * }");
12124
- else {
12125
- let _ = HY.tables.map((_) => _.schemaName === "public" ? _.name : `${_.schemaName}.${_.name}`);
12126
- P.push(" Tables {"), _.forEach((_) => P.push(` ${_}`)), P.push(" }");
12127
- }
12128
- return HY?.stickyNotes !== void 0 && (HY.stickyNotes === null || (HY.stickyNotes.length === 0 ? P.push(" Notes { * }") : (P.push(" Notes {"), HY.stickyNotes.forEach((_) => P.push(` ${_.name}`)), P.push(" }")))), HY?.tableGroups !== void 0 && (HY.tableGroups === null || (HY.tableGroups.length === 0 ? P.push(" TableGroups { * }") : (P.push(" TableGroups {"), HY.tableGroups.forEach((_) => P.push(` ${_.name}`)), P.push(" }")))), HY?.schemas !== void 0 && (HY.schemas === null || (HY.schemas.length === 0 ? P.push(" Schemas { * }") : (P.push(" Schemas {"), HY.schemas.forEach((_) => P.push(` ${_.name}`)), P.push(" }")))), P.push("}"), P.join("\n");
12141
+ let P = `DiagramView ${addDoubleQuoteIfNeeded(_)} {`;
12142
+ if (!HY) return `${P}\n}`;
12143
+ let { tables: UY, tableGroups: WY, schemas: GY, stickyNotes: KY } = HY, qY = UY === null, JY = WY === null, YY = GY === null;
12144
+ if (qY && JY && YY && KY === null) return `${P}\n}`;
12145
+ if (qY || JY || YY) {
12146
+ let _ = !qY && UY.length > 0, HY = !JY && WY.length > 0, XY = !YY && GY.length > 0;
12147
+ if (!(_ || HY || XY)) {
12148
+ if (KY && KY.length > 0) {
12149
+ let _ = [P];
12150
+ return emitNotesBlock(_, KY), _.push("}"), _.join("\n");
12151
+ }
12152
+ return `${P}\n Notes { * }\n}`;
12153
+ }
12154
+ let ZY = [P];
12155
+ return _ && emitTablesBlock(ZY, UY), HY && emitTableGroupsBlock(ZY, WY), XY && emitSchemasBlock(ZY, GY), KY && KY.length > 0 && emitNotesBlock(ZY, KY), ZY.push("}"), ZY.join("\n");
12156
+ }
12157
+ let XY = UY, ZY = WY, QY = GY, $Y = XY.length === 0 && ZY.length === 0 && QY.length === 0, eX = KY != null && KY.length > 0;
12158
+ if ($Y) {
12159
+ if (eX) {
12160
+ let _ = [P, " Tables { * }"];
12161
+ return emitNotesBlock(_, KY), _.push("}"), _.join("\n");
12162
+ }
12163
+ return `${P}\n *\n}`;
12164
+ }
12165
+ let tX = [P];
12166
+ return XY.length > 0 && emitTablesBlock(tX, XY), ZY.length > 0 && emitTableGroupsBlock(tX, ZY), QY.length > 0 && emitSchemasBlock(tX, QY), eX && emitNotesBlock(tX, KY), tX.push("}"), tX.join("\n");
12129
12167
  }
12130
12168
  function syncDiagramView$1(_, HY, P) {
12131
12169
  let UY = P ?? findDiagramViewBlocks$1(_), WY = [];
@@ -16203,19 +16241,19 @@ var POSTGRES_BUILTIN_TYPES = /* @__PURE__ */ "SMALLINT.INTEGER.INT.BIGINT.DECIMA
16203
16241
  ];
16204
16242
  return concat_default(KY.schemas, KY.enums, KY.tables, KY.indexes, KY.comments, KY.refs, JY).join("\n");
16205
16243
  }
16244
+ }, DbState = class {
16245
+ constructor() {
16246
+ this.dbId = 1, this.schemaId = 1, this.enumId = 1, this.tableGroupId = 1, this.refId = 1, this.tableId = 1, this.noteId = 1, this.enumValueId = 1, this.endpointId = 1, this.indexId = 1, this.checkId = 1, this.fieldId = 1, this.indexColumnId = 1, this.recordId = 1, this.tablePartialId = 1;
16247
+ }
16248
+ generateId(_) {
16249
+ let HY = this[_];
16250
+ return this[_] += 1, HY;
16251
+ }
16206
16252
  }, ElementError = class extends Error {
16207
- constructor(_, HY = {
16208
- start: {
16209
- line: 1,
16210
- column: 1,
16211
- offset: 0
16212
- },
16213
- end: {
16214
- line: 1,
16215
- column: 1,
16216
- offset: 0
16217
- }
16218
- }) {
16253
+ constructor(_, HY = { start: {
16254
+ line: 1,
16255
+ column: 1
16256
+ } }) {
16219
16257
  super(_), this.location = HY, this.error = "error";
16220
16258
  }
16221
16259
  }, element_default = class {
@@ -16228,8 +16266,207 @@ var POSTGRES_BUILTIN_TYPES = /* @__PURE__ */ "SMALLINT.INTEGER.INT.BIGINT.DECIMA
16228
16266
  error(_) {
16229
16267
  throw new ElementError(_, this.token);
16230
16268
  }
16269
+ };
16270
+ init_lodash();
16271
+ var enumValue_default = class extends element_default {
16272
+ constructor({ name: _, token: HY, note: P, _enum: UY, noteToken: WY = null } = {}) {
16273
+ super(HY), _ || this.error("Enum value must have a name"), this.name = _, this.note = P ? get_default(P, "value", P) : null, this.noteToken = P ? get_default(P, "token", WY) : null, this._enum = UY, this.dbState = this._enum.dbState, this.generateId();
16274
+ }
16275
+ generateId() {
16276
+ this.id = this.dbState.generateId("enumValueId");
16277
+ }
16278
+ export() {
16279
+ return { ...this.shallowExport() };
16280
+ }
16281
+ exportParentIds() {
16282
+ return { enumId: this._enum.id };
16283
+ }
16284
+ shallowExport() {
16285
+ return {
16286
+ name: this.name,
16287
+ note: this.note
16288
+ };
16289
+ }
16290
+ normalize(_) {
16291
+ _.enumValues[this.id] = {
16292
+ id: this.id,
16293
+ ...this.shallowExport(),
16294
+ ...this.exportParentIds()
16295
+ };
16296
+ }
16297
+ };
16298
+ init_lodash();
16299
+ var enum_default = class extends element_default {
16300
+ constructor({ name: _, token: HY, values: P, note: UY, schema: WY, noteToken: GY = null } = {}) {
16301
+ super(HY), _ || this.error("Enum must have a name"), this.name = _, this.note = UY ? get_default(UY, "value", UY) : null, this.noteToken = UY ? get_default(UY, "token", GY) : null, this.values = [], this.fields = [], this.schema = WY, this.dbState = this.schema.dbState, this.generateId(), this.processValues(P);
16302
+ }
16303
+ generateId() {
16304
+ this.id = this.dbState.generateId("enumId");
16305
+ }
16306
+ processValues(_) {
16307
+ _.forEach((_) => {
16308
+ this.pushValue(new enumValue_default({
16309
+ ..._,
16310
+ _enum: this
16311
+ }));
16312
+ });
16313
+ }
16314
+ pushValue(_) {
16315
+ this.checkValue(_), this.values.push(_);
16316
+ }
16317
+ checkValue(_) {
16318
+ this.values.some((HY) => HY.name === _.name) && _.error(`Enum value "${_.name}" existed in enum ${shouldPrintSchema$1(this.schema) ? `"${this.schema.name}".` : ""}"${this.name}"`);
16319
+ }
16320
+ pushField(_) {
16321
+ this.checkField(_), this.fields.push(_);
16322
+ }
16323
+ checkField(_) {
16324
+ this.fields.some((HY) => HY.id === _.id) && this.error(`Field ${shouldPrintSchema$1(_.table.schema) ? `"${_.table.schema.name}".` : ""}"${_.table.name}"."${_.name}" already associated with enum ${shouldPrintSchema$1(this.schema) ? `"${this.schema.name}".` : ""}${this.name}"`);
16325
+ }
16326
+ export() {
16327
+ return {
16328
+ ...this.shallowExport(),
16329
+ ...this.exportChild()
16330
+ };
16331
+ }
16332
+ exportChild() {
16333
+ return { values: this.values.map((_) => _.export()) };
16334
+ }
16335
+ exportChildIds() {
16336
+ return {
16337
+ valueIds: this.values.map((_) => _.id),
16338
+ fieldIds: this.fields.map((_) => _.id)
16339
+ };
16340
+ }
16341
+ exportParentIds() {
16342
+ return { schemaId: this.schema.id };
16343
+ }
16344
+ shallowExport() {
16345
+ return {
16346
+ name: this.name,
16347
+ note: this.note
16348
+ };
16349
+ }
16350
+ normalize(_) {
16351
+ _.enums[this.id] = {
16352
+ id: this.id,
16353
+ ...this.shallowExport(),
16354
+ ...this.exportChildIds(),
16355
+ ...this.exportParentIds()
16356
+ }, this.values.forEach((HY) => HY.normalize(_));
16357
+ }
16358
+ }, endpoint_default = class extends element_default {
16359
+ constructor({ tableName: _, schemaName: HY, fieldNames: P, relation: UY, token: WY, ref: GY }) {
16360
+ super(WY), this.relation = UY, this.schemaName = HY, this.tableName = _, this.fieldNames = P, this.fields = [], this.ref = GY, this.dbState = this.ref.dbState, this.generateId();
16361
+ let KY = GY.schema.database.findOrCreateSchema(HY || "public").database.findTable(HY, _);
16362
+ KY || this.error(`Can't find table ${shouldPrintSchemaName(HY) ? `"${HY}".` : ""}"${_}"`), this.setFields(P, KY);
16363
+ }
16364
+ generateId() {
16365
+ this.id = this.dbState.generateId("endpointId");
16366
+ }
16367
+ equals(_) {
16368
+ return this.fields.length === _.fields.length ? this.compareFields(_) : !1;
16369
+ }
16370
+ compareFields(_) {
16371
+ let HY = this.fields.map((_) => _.id).sort(), P = _.fields.map((_) => _.id).sort();
16372
+ for (let _ = 0; _ < HY.length; _ += 1) if (HY[_] !== P[_]) return !1;
16373
+ return !0;
16374
+ }
16375
+ export() {
16376
+ return { ...this.shallowExport() };
16377
+ }
16378
+ exportParentIds() {
16379
+ return {
16380
+ refId: this.ref.id,
16381
+ fieldIds: this.fields.map((_) => _.id)
16382
+ };
16383
+ }
16384
+ shallowExport() {
16385
+ return {
16386
+ schemaName: this.schemaName,
16387
+ tableName: this.tableName,
16388
+ fieldNames: this.fieldNames,
16389
+ relation: this.relation
16390
+ };
16391
+ }
16392
+ setFields(_, HY) {
16393
+ let P = _ && _.length ? [..._] : [];
16394
+ if (!P.length) {
16395
+ let _ = HY.fields.find((_) => _.pk);
16396
+ if (_) P.push(_.name);
16397
+ else {
16398
+ let _ = HY.indexes.find((_) => _.pk);
16399
+ _ ? P = _.columns.map((_) => _.value) : this.error(`Can't find primary or composite key in table ${shouldPrintSchema$1(HY.schema) ? `"${HY.schema.name}".` : ""}"${HY.name}"`);
16400
+ }
16401
+ }
16402
+ P.forEach((_) => {
16403
+ let P = HY.findField(_);
16404
+ P || this.error(`Can't find field ${shouldPrintSchema$1(HY.schema) ? `"${HY.schema.name}".` : ""}"${_}" in table "${HY.name}"`), this.fields.push(P), P.pushEndpoint(this);
16405
+ });
16406
+ }
16407
+ normalize(_) {
16408
+ _.endpoints[this.id] = {
16409
+ id: this.id,
16410
+ ...this.shallowExport(),
16411
+ ...this.exportParentIds()
16412
+ };
16413
+ }
16414
+ };
16415
+ function isEqualPair(_, HY) {
16416
+ return _[0].equals(HY[0]) && _[1].equals(HY[1]);
16417
+ }
16418
+ var ref_default = class extends element_default {
16419
+ constructor({ name: _, color: HY, endpoints: P, onDelete: UY, onUpdate: WY, token: GY, schema: KY = {}, injectedPartial: qY = null } = {}) {
16420
+ super(GY), this.name = _, this.color = HY, this.onDelete = UY, this.onUpdate = WY, this.endpoints = [], this.schema = KY, this.injectedPartial = qY, this.dbState = this.schema.dbState, this.generateId(), this.processEndpoints(P);
16421
+ }
16422
+ generateId() {
16423
+ this.id = this.dbState.generateId("refId");
16424
+ }
16425
+ processEndpoints(_) {
16426
+ _.forEach((_) => {
16427
+ this.endpoints.push(new endpoint_default({
16428
+ ..._,
16429
+ ref: this
16430
+ })), _.schemaName;
16431
+ }), this.endpoints[0].equals(this.endpoints[1]) && this.error("Two endpoints are the same"), this.endpoints[0].fields.length !== this.endpoints[1].fields.length && this.error("Two endpoints have unequal number of fields");
16432
+ }
16433
+ equals(_) {
16434
+ return isEqualPair(this.endpoints, _.endpoints) || isEqualPair(this.endpoints, _.endpoints.slice().reverse());
16435
+ }
16436
+ export() {
16437
+ return {
16438
+ ...this.shallowExport(),
16439
+ ...this.exportChild()
16440
+ };
16441
+ }
16442
+ shallowExport() {
16443
+ return {
16444
+ name: this.name,
16445
+ color: this.color,
16446
+ onDelete: this.onDelete,
16447
+ onUpdate: this.onUpdate,
16448
+ injectedPartialId: this.injectedPartial?.id
16449
+ };
16450
+ }
16451
+ exportChild() {
16452
+ return { endpoints: this.endpoints.map((_) => _.export()) };
16453
+ }
16454
+ exportChildIds() {
16455
+ return { endpointIds: this.endpoints.map((_) => _.id) };
16456
+ }
16457
+ exportParentIds() {
16458
+ return { schemaId: this.schema.id };
16459
+ }
16460
+ normalize(_) {
16461
+ _.refs[this.id] = {
16462
+ id: this.id,
16463
+ ...this.shallowExport(),
16464
+ ...this.exportChildIds(),
16465
+ ...this.exportParentIds()
16466
+ }, this.endpoints.forEach((HY) => HY.normalize(_));
16467
+ }
16231
16468
  }, check_default = class extends element_default {
16232
- constructor({ token: _, name: HY, expression: P, table: UY, column: WY = null, injectedPartial: GY = null }) {
16469
+ constructor({ token: _, name: HY, expression: P, table: UY, column: WY = null, injectedPartial: GY = null } = {}) {
16233
16470
  super(_), this.name = HY, this.expression = P, this.table = UY, this.column = WY, this.injectedPartial = GY, this.dbState = this.table.dbState, this.generateId();
16234
16471
  }
16235
16472
  generateId() {
@@ -16241,8 +16478,8 @@ var POSTGRES_BUILTIN_TYPES = /* @__PURE__ */ "SMALLINT.INTEGER.INT.BIGINT.DECIMA
16241
16478
  exportParentIds() {
16242
16479
  return {
16243
16480
  tableId: this.table.id,
16244
- columnId: this.column?.id ?? null,
16245
- injectedPartialId: this.injectedPartial?.id ?? null
16481
+ columnId: this.column?.id,
16482
+ injectedPartialId: this.injectedPartial?.id
16246
16483
  };
16247
16484
  }
16248
16485
  shallowExport() {
@@ -16252,9 +16489,8 @@ var POSTGRES_BUILTIN_TYPES = /* @__PURE__ */ "SMALLINT.INTEGER.INT.BIGINT.DECIMA
16252
16489
  };
16253
16490
  }
16254
16491
  normalize(_) {
16255
- let HY = this.id;
16256
- _.checks[HY] = {
16257
- id: HY,
16492
+ _.checks[this.id] = {
16493
+ id: this.id,
16258
16494
  ...this.shallowExport(),
16259
16495
  ...this.exportParentIds()
16260
16496
  };
@@ -16262,7 +16498,7 @@ var POSTGRES_BUILTIN_TYPES = /* @__PURE__ */ "SMALLINT.INTEGER.INT.BIGINT.DECIMA
16262
16498
  };
16263
16499
  init_lodash();
16264
16500
  var field_default = class extends element_default {
16265
- constructor({ name: _, type: HY, unique: P, pk: UY, token: WY, not_null: GY, note: KY, dbdefault: qY, increment: JY, checks: YY = [], table: XY = {}, noteToken: ZY = null, injectedPartial: QY = null, injectedToken: $Y = null }) {
16501
+ constructor({ name: _, type: HY, unique: P, pk: UY, token: WY, not_null: GY, note: KY, dbdefault: qY, increment: JY, checks: YY = [], table: XY = {}, noteToken: ZY = null, injectedPartial: QY = null, injectedToken: $Y = null } = {}) {
16266
16502
  super(WY), _ || this.error("Field must have a name"), HY || this.error("Field must have a type"), this.name = _, this.type = HY, this.unique = P, this.pk = UY, this.not_null = GY, this.note = KY ? get_default(KY, "value", KY) : null, this.noteToken = KY ? get_default(KY, "token", ZY) : null, this.dbdefault = qY, this.increment = JY, this.checks = [], this.endpoints = [], this.table = XY, this.injectedPartial = QY, this.injectedToken = $Y, this.dbState = this.table.dbState, this.generateId(), this.bindType(), this.processChecks(YY);
16267
16503
  }
16268
16504
  generateId() {
@@ -16331,7 +16567,7 @@ var field_default = class extends element_default {
16331
16567
  }
16332
16568
  }, indexColumn_default = class extends element_default {
16333
16569
  constructor({ type: _, value: HY, index: P, token: UY }) {
16334
- super(UY), this.type = _, this.value = HY, this.index = P, this.token = UY, this.dbState = this.index.dbState, this.generateId();
16570
+ super(), this.type = _, this.value = HY, this.index = P, this.token = UY, this.dbState = this.index.dbState, this.generateId();
16335
16571
  }
16336
16572
  generateId() {
16337
16573
  this.id = this.dbState.generateId("indexColumnId");
@@ -16356,7 +16592,7 @@ var field_default = class extends element_default {
16356
16592
  };
16357
16593
  }
16358
16594
  }, indexes_default = class extends element_default {
16359
- constructor({ columns: _, type: HY, unique: P, pk: UY, token: WY, name: GY, note: KY, table: qY = {}, injectedPartial: JY = null }) {
16595
+ constructor({ columns: _, type: HY, unique: P, pk: UY, token: WY, name: GY, note: KY, table: qY = {}, injectedPartial: JY = null } = {}) {
16360
16596
  super(WY), this.name = GY, this.type = HY, this.unique = P, this.note = KY ? KY.value : null, this.noteToken = KY ? KY.token : null, this.pk = UY, this.columns = [], this.table = qY, this.injectedPartial = JY, this.dbState = this.table.dbState, this.generateId(), this.processIndexColumns(_);
16361
16597
  }
16362
16598
  generateId() {
@@ -16412,7 +16648,7 @@ var field_default = class extends element_default {
16412
16648
  };
16413
16649
  init_lodash();
16414
16650
  var table_default = class extends element_default {
16415
- constructor({ name: _, alias: HY, note: P, fields: UY = [], indexes: WY = [], checks: GY = [], schema: KY = {}, token: qY, headerColor: JY, noteToken: YY = null, partials: XY = [] }) {
16651
+ constructor({ name: _, alias: HY, note: P, fields: UY = [], indexes: WY = [], checks: GY = [], schema: KY = {}, token: qY, headerColor: JY, noteToken: YY = null, partials: XY = [] } = {}) {
16416
16652
  super(qY), this.name = _, this.alias = HY, this.note = P ? get_default(P, "value", P) : null, this.noteToken = P ? get_default(P, "token", YY) : null, this.headerColor = JY, this.fields = [], this.indexes = [], this.checks = [], this.schema = KY, this.partials = XY, this.records = [], this.dbState = this.schema.dbState, this.generateId(), this.processFields(UY), this.processPartials(), this.checkFieldCount(), this.processIndexes(WY), this.processChecks(GY);
16417
16653
  }
16418
16654
  generateId() {
@@ -16466,18 +16702,18 @@ var table_default = class extends element_default {
16466
16702
  return this.fields.find((HY) => HY.name === _);
16467
16703
  }
16468
16704
  checkSameId(_) {
16469
- return this.schema.checkSameId(_.schemaName || "public") && (this.name === _.name || this.alias === _.name || this.name === _.alias || !!(this.alias && this.alias === _.alias));
16705
+ return this.schema.checkSameId(_.schemaName || "public") && (this.name === _.name || this.alias === _.name || this.name === _.alias || this.alias && this.alias === _.alias);
16470
16706
  }
16471
16707
  processPartials() {
16472
16708
  if (!this.partials?.length) return;
16473
16709
  let _ = new Set(this.fields.map((_) => _.name)), HY = /* @__PURE__ */ new Set();
16474
16710
  isNil_default(this.note) || HY.add("note"), isNil_default(this.headerColor) || HY.add("headerColor");
16475
16711
  let P = this.partials.sort((_, HY) => HY.order - _.order);
16476
- [...P].reverse().forEach((_) => {
16712
+ P.toReversed().forEach((_) => {
16477
16713
  this.fields.splice(_.order, 0, "dummy");
16478
16714
  }), P.forEach((P) => {
16479
16715
  let UY = this.schema.database.findTablePartial(P.name);
16480
- if (UY || this.error(`Table partial ${P.name} not found`), P.id = UY.id, UY.fields) {
16716
+ if (UY || this.error(`Table partial ${P.name} not found`, P.token), P.id = UY.id, UY.fields) {
16481
16717
  let HY = UY.fields.filter((HY) => !_.has(HY.name)).map((HY) => (_.add(HY.name), HY.inline_refs && HY.inline_refs.forEach((_) => {
16482
16718
  let P = {
16483
16719
  token: HY.token,
@@ -16567,95 +16803,6 @@ var table_default = class extends element_default {
16567
16803
  }
16568
16804
  };
16569
16805
  init_lodash();
16570
- var enumValue_default = class extends element_default {
16571
- constructor({ name: _, token: HY, note: P, _enum: UY, noteToken: WY = null }) {
16572
- super(HY), _ || this.error("Enum value must have a name"), this.name = _, this.note = P ? get_default(P, "value", P) : null, this.noteToken = P ? get_default(P, "token", WY) : null, this._enum = UY, this.dbState = this._enum.dbState, this.generateId();
16573
- }
16574
- generateId() {
16575
- this.id = this.dbState.generateId("enumValueId");
16576
- }
16577
- export() {
16578
- return { ...this.shallowExport() };
16579
- }
16580
- exportParentIds() {
16581
- return { enumId: this._enum.id };
16582
- }
16583
- shallowExport() {
16584
- return {
16585
- name: this.name,
16586
- note: this.note
16587
- };
16588
- }
16589
- normalize(_) {
16590
- _.enumValues[this.id] = {
16591
- id: this.id,
16592
- ...this.shallowExport(),
16593
- ...this.exportParentIds()
16594
- };
16595
- }
16596
- };
16597
- init_lodash();
16598
- var enum_default = class extends element_default {
16599
- constructor({ name: _, token: HY, values: P, note: UY, schema: WY, noteToken: GY = null }) {
16600
- super(HY), _ || this.error("Enum must have a name"), this.name = _, this.note = UY ? get_default(UY, "value", UY) : null, this.noteToken = UY ? get_default(UY, "token", GY) : null, this.values = [], this.fields = [], this.schema = WY, this.dbState = this.schema.dbState, this.generateId(), this.processValues(P);
16601
- }
16602
- generateId() {
16603
- this.id = this.dbState.generateId("enumId");
16604
- }
16605
- processValues(_) {
16606
- _.forEach((_) => {
16607
- this.pushValue(new enumValue_default({
16608
- ..._,
16609
- _enum: this
16610
- }));
16611
- });
16612
- }
16613
- pushValue(_) {
16614
- this.checkValue(_), this.values.push(_);
16615
- }
16616
- checkValue(_) {
16617
- this.values.some((HY) => HY.name === _.name) && _.error(`Enum value "${_.name}" existed in enum ${shouldPrintSchema$1(this.schema) ? `"${this.schema.name}".` : ""}"${this.name}"`);
16618
- }
16619
- pushField(_) {
16620
- this.checkField(_), this.fields.push(_);
16621
- }
16622
- checkField(_) {
16623
- this.fields.some((HY) => HY.id === _.id) && this.error(`Field ${shouldPrintSchema$1(_.table.schema) ? `"${_.table.schema.name}".` : ""}"${_.table.name}"."${_.name}" already associated with enum ${shouldPrintSchema$1(this.schema) ? `"${this.schema.name}".` : ""}${this.name}"`);
16624
- }
16625
- export() {
16626
- return {
16627
- ...this.shallowExport(),
16628
- ...this.exportChild()
16629
- };
16630
- }
16631
- exportChild() {
16632
- return { values: this.values.map((_) => _.export()) };
16633
- }
16634
- exportChildIds() {
16635
- return {
16636
- valueIds: this.values.map((_) => _.id),
16637
- fieldIds: this.fields.map((_) => _.id)
16638
- };
16639
- }
16640
- exportParentIds() {
16641
- return { schemaId: this.schema.id };
16642
- }
16643
- shallowExport() {
16644
- return {
16645
- name: this.name,
16646
- note: this.note
16647
- };
16648
- }
16649
- normalize(_) {
16650
- _.enums[this.id] = {
16651
- id: this.id,
16652
- ...this.shallowExport(),
16653
- ...this.exportChildIds(),
16654
- ...this.exportParentIds()
16655
- }, this.values.forEach((HY) => HY.normalize(_));
16656
- }
16657
- };
16658
- init_lodash();
16659
16806
  var tableGroup_default = class extends element_default {
16660
16807
  constructor({ name: _, token: HY, tables: P = [], schema: UY = {}, note: WY, color: GY, noteToken: KY = null }) {
16661
16808
  super(HY), this.name = _, this.tables = [], this.schema = UY, this.dbState = this.schema.dbState, this.note = WY ? get_default(WY, "value", WY) : null, this.noteToken = WY ? get_default(WY, "token", KY) : null, this.color = GY, this.generateId(), this.processTables(P);
@@ -16708,120 +16855,10 @@ var tableGroup_default = class extends element_default {
16708
16855
  ...this.exportParentIds()
16709
16856
  };
16710
16857
  }
16711
- }, endpoint_default = class extends element_default {
16712
- constructor({ tableName: _, schemaName: HY, fieldNames: P, relation: UY, token: WY, ref: GY }) {
16713
- super(WY), this.relation = UY, this.schemaName = HY, this.tableName = _, this.fieldNames = P, this.fields = [], this.ref = GY, this.dbState = this.ref.dbState, this.generateId();
16714
- let KY = GY.schema.database.findOrCreateSchema(HY || "public").database.findTable(HY, _);
16715
- KY || this.error(`Can't find table ${shouldPrintSchemaName(HY) ? `"${HY}".` : ""}"${_}"`), this.setFields(P, KY);
16716
- }
16717
- generateId() {
16718
- this.id = this.dbState.generateId("endpointId");
16719
- }
16720
- equals(_) {
16721
- return this.fields.length === _.fields.length ? this.compareFields(_) : !1;
16722
- }
16723
- compareFields(_) {
16724
- let HY = this.fields.map((_) => _.id).sort(), P = _.fields.map((_) => _.id).sort();
16725
- for (let _ = 0; _ < HY.length; _ += 1) if (HY[_] !== P[_]) return !1;
16726
- return !0;
16727
- }
16728
- export() {
16729
- return { ...this.shallowExport() };
16730
- }
16731
- exportParentIds() {
16732
- return {
16733
- refId: this.ref.id,
16734
- fieldIds: this.fields.map((_) => _.id)
16735
- };
16736
- }
16737
- shallowExport() {
16738
- return {
16739
- schemaName: this.schemaName,
16740
- tableName: this.tableName,
16741
- fieldNames: this.fieldNames,
16742
- relation: this.relation
16743
- };
16744
- }
16745
- setFields(_, HY) {
16746
- let P = _ && _.length ? [..._] : [];
16747
- if (!P.length) {
16748
- let _ = HY.fields.find((_) => _.pk);
16749
- if (_) P.push(_.name);
16750
- else {
16751
- let _ = HY.indexes.find((_) => _.pk);
16752
- _ ? P = _.columns.map((_) => _.value) : this.error(`Can't find primary or composite key in table ${shouldPrintSchema$1(HY.schema) ? `"${HY.schema.name}".` : ""}"${HY.name}"`);
16753
- }
16754
- }
16755
- P.forEach((_) => {
16756
- let P = HY.findField(_);
16757
- P || this.error(`Can't find field ${shouldPrintSchema$1(HY.schema) ? `"${HY.schema.name}".` : ""}"${_}" in table "${HY.name}"`), this.fields.push(P), P.pushEndpoint(this);
16758
- });
16759
- }
16760
- normalize(_) {
16761
- _.endpoints[this.id] = {
16762
- id: this.id,
16763
- ...this.shallowExport(),
16764
- ...this.exportParentIds()
16765
- };
16766
- }
16767
- };
16768
- function isEqualPair(_, HY) {
16769
- return _[0].equals(HY[0]) && _[1].equals(HY[1]);
16770
- }
16771
- var ref_default = class extends element_default {
16772
- constructor({ name: _, color: HY, endpoints: P, onDelete: UY, onUpdate: WY, token: GY, schema: KY = {}, injectedPartial: qY = void 0 }) {
16773
- super(GY), this.name = _, this.color = HY, this.onDelete = UY, this.onUpdate = WY, this.endpoints = [], this.schema = KY, this.injectedPartial = qY, this.dbState = this.schema.dbState, this.generateId(), this.processEndpoints(P);
16774
- }
16775
- generateId() {
16776
- this.id = this.dbState.generateId("refId");
16777
- }
16778
- processEndpoints(_) {
16779
- _.forEach((_) => {
16780
- this.endpoints.push(new endpoint_default({
16781
- ..._,
16782
- ref: this
16783
- })), _.schemaName;
16784
- }), this.endpoints[0].equals(this.endpoints[1]) && this.error("Two endpoints are the same"), this.endpoints[0].fields.length !== this.endpoints[1].fields.length && this.error("Two endpoints have unequal number of fields");
16785
- }
16786
- equals(_) {
16787
- return isEqualPair(this.endpoints, _.endpoints) || isEqualPair(this.endpoints, _.endpoints.slice().reverse());
16788
- }
16789
- export() {
16790
- return {
16791
- ...this.shallowExport(),
16792
- ...this.exportChild()
16793
- };
16794
- }
16795
- shallowExport() {
16796
- return {
16797
- name: this.name,
16798
- color: this.color,
16799
- onDelete: this.onDelete,
16800
- onUpdate: this.onUpdate,
16801
- injectedPartialId: this.injectedPartial?.id
16802
- };
16803
- }
16804
- exportChild() {
16805
- return { endpoints: this.endpoints.map((_) => _.export()) };
16806
- }
16807
- exportChildIds() {
16808
- return { endpointIds: this.endpoints.map((_) => _.id) };
16809
- }
16810
- exportParentIds() {
16811
- return { schemaId: this.schema.id };
16812
- }
16813
- normalize(_) {
16814
- _.refs[this.id] = {
16815
- id: this.id,
16816
- ...this.shallowExport(),
16817
- ...this.exportChildIds(),
16818
- ...this.exportParentIds()
16819
- }, this.endpoints.forEach((HY) => HY.normalize(_));
16820
- }
16821
16858
  };
16822
16859
  init_lodash();
16823
16860
  var schema_default = class extends element_default {
16824
- constructor({ name: _, alias: HY, note: P, tables: UY = [], refs: WY = [], enums: GY = [], tableGroups: KY = [], token: qY, database: JY = {}, noteToken: YY = null }) {
16861
+ constructor({ name: _, alias: HY, note: P, tables: UY = [], refs: WY = [], enums: GY = [], tableGroups: KY = [], token: qY, database: JY = {}, noteToken: YY = null } = {}) {
16825
16862
  super(qY), this.tables = [], this.enums = [], this.tableGroups = [], this.refs = [], this.name = _, this.note = P ? get_default(P, "value", P) : null, this.noteToken = P ? get_default(P, "token", YY) : null, this.alias = HY, this.database = JY, this.dbState = this.database.dbState, this.generateId(), this.processEnums(GY), this.processTables(UY), this.processRefs(WY), this.processTableGroups(KY);
16826
16863
  }
16827
16864
  generateId() {
@@ -16871,7 +16908,7 @@ var schema_default = class extends element_default {
16871
16908
  }
16872
16909
  checkRef(_) {
16873
16910
  if (this.refs.some((HY) => HY.equals(_))) {
16874
- let HY = _.endpoints[0], P = HY.fieldNames.map((_) => JSON.stringify(_)).join(", "), UY = _.endpoints[1], WY = UY.fieldNames.map((_) => JSON.stringify(_)).join(", "), GY = `"${HY.schemaName ? `${HY.schemaName}"."` : ""}${HY.tableName}"(${P})`, KY = `"${UY.schemaName ? `${UY.schemaName}"."` : ""}${UY.tableName}"(${WY})`;
16911
+ let HY = _.endpoints[0], P = HY.fieldNames.map(JSON.stringify).join(", "), UY = _.endpoints[1], WY = UY.fieldNames.map(JSON.stringify).join(", "), GY = `"${HY.schemaName ? `${HY.schemaName}"."` : ""}${HY.tableName}"(${P})`, KY = `"${UY.schemaName ? `${UY.schemaName}"."` : ""}${UY.tableName}"(${WY})`;
16875
16912
  _.error(`Reference with the same endpoints already exists: ${GY} references ${KY}`);
16876
16913
  }
16877
16914
  }
@@ -16890,7 +16927,7 @@ var schema_default = class extends element_default {
16890
16927
  this.tableGroups.some((HY) => HY.name === _.name) && _.error(`Table Group ${shouldPrintSchema$1(this) ? `"${this.name}".` : ""}"${_.name}" existed`);
16891
16928
  }
16892
16929
  checkSameId(_) {
16893
- return this.name === _.name || this.alias === _.name || this.name === _.alias || !!(this.alias && this.alias === _.alias);
16930
+ return this.name === _.name || this.alias === _.name || this.name === _.alias || this.alias && this.alias === _.alias;
16894
16931
  }
16895
16932
  export() {
16896
16933
  return {
@@ -16933,7 +16970,7 @@ var schema_default = class extends element_default {
16933
16970
  }, this.tables.forEach((HY) => HY.normalize(_)), this.enums.forEach((HY) => HY.normalize(_)), this.tableGroups.forEach((HY) => HY.normalize(_)), this.refs.forEach((HY) => HY.normalize(_));
16934
16971
  }
16935
16972
  }, stickyNote_default = class extends element_default {
16936
- constructor({ name: _, content: HY, headerColor: P, token: UY, database: WY = {} }) {
16973
+ constructor({ name: _, content: HY, headerColor: P, token: UY, database: WY = {} } = {}) {
16937
16974
  super(UY), this.name = _, this.content = HY, this.headerColor = P, this.database = WY, this.dbState = this.database.dbState, this.generateId();
16938
16975
  }
16939
16976
  generateId() {
@@ -16952,18 +16989,10 @@ var schema_default = class extends element_default {
16952
16989
  ...this.export()
16953
16990
  };
16954
16991
  }
16955
- }, DbState = class {
16956
- constructor() {
16957
- this.dbId = 1, this.schemaId = 1, this.enumId = 1, this.tableGroupId = 1, this.refId = 1, this.tableId = 1, this.noteId = 1, this.enumValueId = 1, this.endpointId = 1, this.indexId = 1, this.checkId = 1, this.fieldId = 1, this.indexColumnId = 1, this.recordId = 1, this.tablePartialId = 1;
16958
- }
16959
- generateId(_) {
16960
- let HY = this[_];
16961
- return this[_] += 1, HY;
16962
- }
16963
16992
  };
16964
16993
  init_lodash();
16965
16994
  var tablePartial_default = class extends element_default {
16966
- constructor({ name: _, note: HY, fields: P = [], indexes: UY = [], checks: WY = [], token: GY, headerColor: KY, noteToken: qY = null, dbState: JY }) {
16995
+ constructor({ name: _, note: HY, fields: P = [], indexes: UY = [], checks: WY = [], token: GY, headerColor: KY, noteToken: qY = null, dbState: JY } = {}) {
16967
16996
  super(GY), this.name = _, this.note = HY ? get_default(HY, "value", HY) : null, this.noteToken = HY ? get_default(HY, "token", qY) : null, this.headerColor = KY, this.fields = P, this.indexes = UY, this.checks = WY, this.dbState = JY, this.generateId();
16968
16997
  }
16969
16998
  generateId() {
@@ -16991,7 +17020,7 @@ var tablePartial_default = class extends element_default {
16991
17020
  init_lodash();
16992
17021
  var database_default = class extends element_default {
16993
17022
  constructor({ schemas: _ = [], tables: HY = [], notes: P = [], enums: UY = [], refs: WY = [], tableGroups: GY = [], project: KY = {}, aliases: qY = [], records: JY = [], tablePartials: YY = [] }) {
16994
- super(void 0), this.dbState = new DbState(), this.generateId(), this.hasDefaultSchema = !1, this.schemas = [], this.notes = [], this.note = KY.note ? get_default(KY, "note.value", KY.note) : null, this.noteToken = KY.note ? get_default(KY, "note.token", KY.noteToken) : null, this.databaseType = KY.database_type, this.name = KY.name, this.token = KY.token, this.aliases = qY, this.records = [], this.tablePartials = [], this.injectedRawRefs = [], this.processNotes(P), this.processRecords(JY), this.processTablePartials(YY), this.processSchemas(_), this.processSchemaElements(UY, ENUM), this.processSchemaElements(HY, TABLE), this.linkRecordsToTables(), this.processSchemaElements(P, "note"), this.processSchemaElements(WY, "ref"), this.processSchemaElements(GY, TABLE_GROUP), this.injectedRawRefs.forEach((_) => {
17023
+ super(), this.dbState = new DbState(), this.generateId(), this.hasDefaultSchema = !1, this.schemas = [], this.notes = [], this.note = KY.note ? get_default(KY, "note.value", KY.note) : null, this.noteToken = KY.note ? get_default(KY, "note.token", KY.noteToken) : null, this.databaseType = KY.database_type, this.name = KY.name, this.token = KY.token, this.aliases = qY, this.records = [], this.tablePartials = [], this.injectedRawRefs = [], this.processNotes(P), this.processRecords(JY), this.processTablePartials(YY), this.processSchemas(_), this.processSchemaElements(UY, ENUM), this.processSchemaElements(HY, TABLE), this.linkRecordsToTables(), this.processSchemaElements(P, "note"), this.processSchemaElements(WY, "ref"), this.processSchemaElements(GY, TABLE_GROUP), this.injectedRawRefs.forEach((_) => {
16995
17024
  let HY = this.findOrCreateSchema(DEFAULT_SCHEMA_NAME), P = new ref_default({
16996
17025
  ..._,
16997
17026
  schema: HY
@@ -17097,30 +17126,27 @@ var database_default = class extends element_default {
17097
17126
  let HY = this.schemas.find((HY) => HY.name === _ || HY.alias === _);
17098
17127
  return HY || (HY = new schema_default({
17099
17128
  name: _,
17100
- note: {
17101
- value: _ === "public" ? `Default ${capitalize_default(DEFAULT_SCHEMA_NAME)} Schema` : null,
17102
- token: void 0
17103
- },
17129
+ note: { value: _ === "public" ? `Default ${capitalize_default(DEFAULT_SCHEMA_NAME)} Schema` : null },
17104
17130
  database: this
17105
17131
  }), this.pushSchema(HY)), HY;
17106
17132
  }
17107
17133
  findTableAlias(_) {
17108
17134
  let HY = this.aliases.find((HY) => HY.name === _);
17109
- if (!HY || HY.kind !== "table") return;
17135
+ if (!HY || HY.kind !== "table") return null;
17110
17136
  let P = HY.value.schemaName || "public", UY = this.schemas.find((_) => _.name === P);
17111
- if (!UY) return;
17137
+ if (!UY) return null;
17112
17138
  let { tableName: WY } = HY.value;
17113
17139
  return UY.tables.find((_) => _.name === WY);
17114
17140
  }
17115
17141
  findTable(_, HY) {
17116
- let P;
17142
+ let P = null;
17117
17143
  if (!_ && (P = this.findTableAlias(HY), P)) return P;
17118
17144
  let UY = this.findOrCreateSchema(_ || "public");
17119
17145
  return UY || this.error(`Schema ${_ || "public"} don't exist`), UY.findTable(HY);
17120
17146
  }
17121
17147
  findEnum(_, HY) {
17122
17148
  let P = this.schemas.find((HY) => HY.name === _ || HY.alias === _);
17123
- if (P) return P.enums.find((_) => _.name === HY);
17149
+ return P ? P.enums.find((_) => _.name === HY) : null;
17124
17150
  }
17125
17151
  findTablePartial(_) {
17126
17152
  return this.tablePartials.find((HY) => HY.name === _);
@@ -1822011,5 +1822037,5 @@ function findDiagramViewBlocks(_) {
1822011
1822037
  let HY = new Compiler();
1822012
1822038
  return HY.setSource(_), HY.findDiagramViewBlocks();
1822013
1822039
  }
1822014
- const VERSION = "7.1.0-alpha.0";
1822040
+ const VERSION = "7.1.0";
1822015
1822041
  export { CompilerError, ModelExporter_default as ModelExporter, Parser_default as Parser, VERSION, addDoubleQuoteIfNeeded, dbmlMonarchTokensProvider, export_default as exporter, findDiagramViewBlocks, formatRecordValue, import_default as importer, isBinaryType, isBooleanType, isDateTimeType, isFloatType, isIntegerType, isNumericType, isSerialType, isStringType, renameTable, syncDiagramView, tryExtractBoolean, tryExtractDateTime, tryExtractEnum, tryExtractInteger, tryExtractNumeric, tryExtractString };