@dbml/core 3.13.0-alpha.0 → 3.13.0-alpha.2

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.
@@ -75,21 +75,26 @@ var Database = /*#__PURE__*/function (_Element) {
75
75
  _this.records = [];
76
76
  _this.tablePartials = [];
77
77
  _this.injectedRawRefs = [];
78
+
79
+ // The process order is important. Do not change !
78
80
  _this.processNotes(notes);
79
81
  _this.processRecords(records);
80
82
  _this.processTablePartials(tablePartials);
81
- // The process order is important. Do not change !
82
83
  _this.processSchemas(schemas);
83
84
  _this.processSchemaElements(enums, _config.ENUM);
84
85
  _this.processSchemaElements(tables, _config.TABLE);
85
86
  _this.processSchemaElements(notes, _config.NOTE);
86
87
  _this.processSchemaElements(refs, _config.REF);
87
88
  _this.processSchemaElements(tableGroups, _config.TABLE_GROUP);
88
- _this.injectedRawRefs.forEach(function (ref) {
89
+ _this.injectedRawRefs.forEach(function (rawRef) {
89
90
  var schema = _this.findOrCreateSchema(_config.DEFAULT_SCHEMA_NAME);
90
- schema.pushRef(new _ref3["default"](_objectSpread(_objectSpread({}, ref), {}, {
91
+ var ref = new _ref3["default"](_objectSpread(_objectSpread({}, rawRef), {}, {
91
92
  schema: schema
92
- })));
93
+ }));
94
+ if (schema.refs.some(function (r) {
95
+ return r.equals(ref);
96
+ })) return;
97
+ schema.pushRef(ref);
93
98
  });
94
99
  return _this;
95
100
  }
@@ -346,7 +351,8 @@ var Database = /*#__PURE__*/function (_Element) {
346
351
  indexes: {},
347
352
  indexColumns: {},
348
353
  fields: {},
349
- records: {}
354
+ records: {},
355
+ tablePartials: {}
350
356
  };
351
357
  this.schemas.forEach(function (schema) {
352
358
  return schema.normalize(normalizedModel);
@@ -355,7 +361,10 @@ var Database = /*#__PURE__*/function (_Element) {
355
361
  return note.normalize(normalizedModel);
356
362
  });
357
363
  this.records.forEach(function (record) {
358
- return normalizedModel.records[record.id] = _objectSpread({}, record);
364
+ normalizedModel.records[record.id] = _objectSpread({}, record);
365
+ });
366
+ this.tablePartials.forEach(function (tablePartial) {
367
+ return tablePartial.normalize(normalizedModel);
359
368
  });
360
369
  return normalizedModel;
361
370
  }
@@ -26,6 +26,7 @@ var DbState = /*#__PURE__*/function () {
26
26
  this.fieldId = 1;
27
27
  this.indexColumnId = 1;
28
28
  this.recordId = 1;
29
+ this.tablePartialId = 1;
29
30
  }
30
31
  _createClass(DbState, [{
31
32
  key: "generateId",
@@ -104,11 +104,13 @@ var Endpoint = /*#__PURE__*/function (_Element) {
104
104
  }, {
105
105
  key: "shallowExport",
106
106
  value: function shallowExport() {
107
+ var _this$injectedPartial;
107
108
  return {
108
109
  schemaName: this.schemaName,
109
110
  tableName: this.tableName,
110
111
  fieldNames: this.fieldNames,
111
- relation: this.relation
112
+ relation: this.relation,
113
+ injectedPartialId: (_this$injectedPartial = this.injectedPartial) === null || _this$injectedPartial === void 0 ? void 0 : _this$injectedPartial.id
112
114
  };
113
115
  }
114
116
  }, {
@@ -44,7 +44,9 @@ var Field = /*#__PURE__*/function (_Element) {
44
44
  _ref$noteToken = _ref.noteToken,
45
45
  noteToken = _ref$noteToken === void 0 ? null : _ref$noteToken,
46
46
  _ref$injectedPartial = _ref.injectedPartial,
47
- injectedPartial = _ref$injectedPartial === void 0 ? null : _ref$injectedPartial;
47
+ injectedPartial = _ref$injectedPartial === void 0 ? null : _ref$injectedPartial,
48
+ _ref$injectedToken = _ref.injectedToken,
49
+ injectedToken = _ref$injectedToken === void 0 ? null : _ref$injectedToken;
48
50
  _classCallCheck(this, Field);
49
51
  _this = _super.call(this, token);
50
52
  if (!name) {
@@ -65,7 +67,9 @@ var Field = /*#__PURE__*/function (_Element) {
65
67
  _this.increment = increment;
66
68
  _this.endpoints = [];
67
69
  _this.table = table;
68
- _this.injectedPartial = injectedPartial, _this.dbState = _this.table.dbState;
70
+ _this.injectedPartial = injectedPartial;
71
+ _this.injectedToken = injectedToken;
72
+ _this.dbState = _this.table.dbState;
69
73
  _this.generateId();
70
74
  _this.bindType();
71
75
  return _this;
@@ -127,6 +131,7 @@ var Field = /*#__PURE__*/function (_Element) {
127
131
  }, {
128
132
  key: "shallowExport",
129
133
  value: function shallowExport() {
134
+ var _this$injectedPartial;
130
135
  return {
131
136
  name: this.name,
132
137
  type: this.type,
@@ -135,7 +140,8 @@ var Field = /*#__PURE__*/function (_Element) {
135
140
  not_null: this.not_null,
136
141
  note: this.note,
137
142
  dbdefault: this.dbdefault,
138
- increment: this.increment
143
+ increment: this.increment,
144
+ injectedPartialId: (_this$injectedPartial = this.injectedPartial) === null || _this$injectedPartial === void 0 ? void 0 : _this$injectedPartial.id
139
145
  };
140
146
  }
141
147
  }, {
@@ -101,11 +101,13 @@ var Ref = /*#__PURE__*/function (_Element) {
101
101
  }, {
102
102
  key: "shallowExport",
103
103
  value: function shallowExport() {
104
+ var _this$injectedPartial;
104
105
  return {
105
106
  name: this.name,
106
107
  color: this.color,
107
108
  onDelete: this.onDelete,
108
- onUpdate: this.onUpdate
109
+ onUpdate: this.onUpdate,
110
+ injectedPartialId: (_this$injectedPartial = this.injectedPartial) === null || _this$injectedPartial === void 0 ? void 0 : _this$injectedPartial.id
109
111
  };
110
112
  }
111
113
  }, {
@@ -68,8 +68,10 @@ var Table = /*#__PURE__*/function (_Element) {
68
68
  _this.dbState = _this.schema.dbState;
69
69
  _this.generateId();
70
70
  _this.processFields(fields);
71
- _this.processIndexes(indexes);
71
+ // Process partials after fields to get injected fields' orders
72
+ // Process partials before indexes for indexes to properly check all owned and injected columns
72
73
  _this.processPartials();
74
+ _this.processIndexes(indexes);
73
75
  return _this;
74
76
  }
75
77
  _createClass(Table, [{
@@ -149,16 +151,19 @@ var Table = /*#__PURE__*/function (_Element) {
149
151
  var _this$partials,
150
152
  _this5 = this;
151
153
  if (!((_this$partials = this.partials) !== null && _this$partials !== void 0 && _this$partials.length)) return;
152
-
153
154
  /**
154
155
  * When encountering conflicting columns or settings with identical names, the following resolution order is applied:
155
156
  * 1. Local Table Definition: If a definition exists within the local table, it takes precedence.
156
157
  * 2. Last Partial Definition: If no local definition is found,
157
- * the definition from the most recently processed partial containing the conflicting name is used.
158
+ * the definition from the last partial (in dbml source) containing the conflicting name is used.
158
159
  *
159
- * Indexes with identical column tuples and attributes will be merged into a single index.
160
+ * each partial has the following structure:
161
+ * {
162
+ * name: string,
163
+ * order: number, // determine where the partials fields will be injected in comparison with the table fields and other partials
164
+ * token, // token of the partial definition
165
+ * }
160
166
  */
161
-
162
167
  var existingFieldNames = new Set(this.fields.map(function (f) {
163
168
  return f.name;
164
169
  }));
@@ -170,9 +175,15 @@ var Table = /*#__PURE__*/function (_Element) {
170
175
  var sortedPartials = this.partials.sort(function (a, b) {
171
176
  return b.order - a.order;
172
177
  });
178
+
179
+ // insert placeholder into table.fields
180
+ sortedPartials.toReversed().forEach(function (partial) {
181
+ _this5.fields.splice(partial.order, 0, 'dummy');
182
+ });
173
183
  sortedPartials.forEach(function (partial) {
174
184
  var tablePartial = _this5.schema.database.findTablePartial(partial.name);
175
185
  if (!tablePartial) _this5.error("Table partial ".concat(partial.name, " not found"), partial.token);
186
+ partial.id = tablePartial.id;
176
187
  if (tablePartial.fields) {
177
188
  var _this5$fields;
178
189
  // ignore fields that already exist in the table, or have been added by a later partial
@@ -185,17 +196,21 @@ var Table = /*#__PURE__*/function (_Element) {
185
196
  // convert inline_refs from injected fields to refs
186
197
  if (rawField.inline_refs) {
187
198
  rawField.inline_refs.forEach(function (iref) {
199
+ var _this5$schema;
188
200
  var ref = {
201
+ token: rawField.token,
189
202
  endpoints: [{
190
203
  tableName: _this5.name,
191
- schemaName: _this5.schema.name,
204
+ schemaName: (_this5$schema = _this5.schema) === null || _this5$schema === void 0 ? void 0 : _this5$schema.name,
192
205
  fieldNames: [rawField.name],
193
- relation: ['-', '<'].includes(iref.relation) ? '1' : '*'
206
+ relation: ['-', '<'].includes(iref.relation) ? '1' : '*',
207
+ token: rawField.token
194
208
  }, {
195
209
  tableName: iref.tableName,
196
210
  schemaName: iref.schemaName,
197
211
  fieldNames: iref.fieldNames,
198
- relation: ['-', '>'].includes(iref.relation) ? '1' : '*'
212
+ relation: ['-', '>'].includes(iref.relation) ? '1' : '*',
213
+ token: iref.token
199
214
  }],
200
215
  injectedPartial: tablePartial
201
216
  };
@@ -203,11 +218,15 @@ var Table = /*#__PURE__*/function (_Element) {
203
218
  });
204
219
  }
205
220
  return new _field["default"](_objectSpread(_objectSpread({}, rawField), {}, {
221
+ noteToken: null,
206
222
  table: _this5,
207
- injectedPartial: tablePartial
223
+ injectedPartial: tablePartial,
224
+ injectedToken: partial.token
208
225
  }));
209
226
  });
210
- (_this5$fields = _this5.fields).splice.apply(_this5$fields, [partial.order, 0].concat(_toConsumableArray(fields)));
227
+ (_this5$fields = _this5.fields).splice.apply(_this5$fields, [partial.order, 1].concat(_toConsumableArray(fields)));
228
+ } else {
229
+ _this5.fields.splice(partial.order, 1); // still need to remove the dummy element, even when there's no field in the partial
211
230
  }
212
231
 
213
232
  // merge settings
@@ -228,14 +247,6 @@ var Table = /*#__PURE__*/function (_Element) {
228
247
  injectedPartial: tablePartial
229
248
  })));
230
249
  });
231
-
232
- // uniqWith(this.indexes, (a, b) => {
233
- // return
234
- // isEqual(a.columns, b.columns) &&
235
- // a.unique === b.unique &&
236
- // a.pk === b.pk &&
237
- // a.type === b.type
238
- // });
239
250
  });
240
251
  }
241
252
  }, {
@@ -282,7 +293,8 @@ var Table = /*#__PURE__*/function (_Element) {
282
293
  name: this.name,
283
294
  alias: this.alias,
284
295
  note: this.note,
285
- headerColor: this.headerColor
296
+ headerColor: this.headerColor,
297
+ partials: this.partials
286
298
  };
287
299
  }
288
300
  }, {
@@ -7,10 +7,6 @@ Object.defineProperty(exports, "__esModule", {
7
7
  exports["default"] = void 0;
8
8
  var _lodash = require("lodash");
9
9
  var _element = _interopRequireDefault(require("./element"));
10
- var _field = _interopRequireDefault(require("./field"));
11
- var _indexes = _interopRequireDefault(require("./indexes"));
12
- var _config = require("./config");
13
- var _utils = require("./utils");
14
10
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
15
11
  function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
16
12
  function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
@@ -54,9 +50,6 @@ var TablePartial = /*#__PURE__*/function (_Element) {
54
50
  _this.indexes = indexes;
55
51
  _this.dbState = dbState;
56
52
  _this.generateId();
57
-
58
- // this.processFields(fields);
59
- // this.processIndexes(indexes);
60
53
  return _this;
61
54
  }
62
55
  _createClass(TablePartial, [{
@@ -64,74 +57,10 @@ var TablePartial = /*#__PURE__*/function (_Element) {
64
57
  value: function generateId() {
65
58
  this.id = this.dbState.generateId('tablePartialId');
66
59
  }
67
-
68
- // processFields (rawFields) {
69
- // rawFields.forEach((field) => {
70
- // this.pushField(new Field({ ...field, table: this }));
71
- // });
72
- // }
73
-
74
- // pushField (field) {
75
- // this.checkField(field);
76
- // this.fields.push(field);
77
- // }
78
-
79
- // checkField (field) {
80
- // if (this.fields.some(f => f.name === field.name)) {
81
- // field.error(`Field "${field.name}" existed in table partial "${this.name}"`);
82
- // }
83
- // }
84
-
85
- // processIndexes (rawIndexes) {
86
- // rawIndexes.forEach((index) => {
87
- // this.pushIndex(new Index({ ...index, table: this }));
88
- // });
89
- // }
90
-
91
- // pushIndex (index) {
92
- // this.checkIndex(index);
93
- // this.indexes.push(index);
94
- // }
95
-
96
- // checkIndex (index) {
97
- // index.columns.forEach((column) => {
98
- // if (column.type === 'column' && !(this.findField(column.value))) {
99
- // index.error(`Column "${column.value}" do not exist in table partial "${this.name}"`);
100
- // }
101
- // });
102
- // }
103
-
104
- // findField (fieldName) {
105
- // return this.fields.find(f => f.name === fieldName);
106
- // }
107
60
  }, {
108
61
  key: "export",
109
62
  value: function _export() {
110
- return _objectSpread(_objectSpread({}, this.shallowExport()), this.exportChild());
111
- }
112
- }, {
113
- key: "exportChild",
114
- value: function exportChild() {
115
- return {
116
- fields: this.fields.map(function (f) {
117
- return f["export"]();
118
- }),
119
- indexes: this.indexes.map(function (i) {
120
- return i["export"]();
121
- })
122
- };
123
- }
124
- }, {
125
- key: "exportChildIds",
126
- value: function exportChildIds() {
127
- return {
128
- fieldIds: this.fields.map(function (f) {
129
- return f.id;
130
- }),
131
- indexIds: this.indexes.map(function (i) {
132
- return i.id;
133
- })
134
- };
63
+ return _objectSpread({}, this.shallowExport());
135
64
  }
136
65
  }, {
137
66
  key: "shallowExport",
@@ -139,21 +68,17 @@ var TablePartial = /*#__PURE__*/function (_Element) {
139
68
  return {
140
69
  name: this.name,
141
70
  note: this.note,
142
- headerColor: this.headerColor
71
+ headerColor: this.headerColor,
72
+ fields: this.fields,
73
+ indexes: this.indexes
143
74
  };
144
75
  }
145
76
  }, {
146
77
  key: "normalize",
147
78
  value: function normalize(model) {
148
- model.tables[this.id] = _objectSpread(_objectSpread({
79
+ model.tablePartials[this.id] = _objectSpread({
149
80
  id: this.id
150
- }, this.shallowExport()), this.exportChildIds());
151
- this.fields.forEach(function (field) {
152
- return field.normalize(model);
153
- });
154
- this.indexes.forEach(function (index) {
155
- return index.normalize(model);
156
- });
81
+ }, this.shallowExport());
157
82
  }
158
83
  }]);
159
84
  return TablePartial;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dbml/core",
3
- "version": "3.13.0-alpha.0",
3
+ "version": "3.13.0-alpha.2",
4
4
  "description": "> TODO: description",
5
5
  "author": "Holistics <dev@holistics.io>",
6
6
  "license": "Apache-2.0",
@@ -32,7 +32,7 @@
32
32
  "prepublish": "npm run build"
33
33
  },
34
34
  "dependencies": {
35
- "@dbml/parse": "^3.13.0-alpha.0",
35
+ "@dbml/parse": "^3.13.0-alpha.2",
36
36
  "antlr4": "^4.13.1",
37
37
  "lodash": "^4.17.15",
38
38
  "parsimmon": "^1.13.0",
@@ -59,7 +59,7 @@
59
59
  "\\.(?!json$)[^.]*$": "@glen/jest-raw-loader"
60
60
  }
61
61
  },
62
- "gitHead": "6b6edb8bc9804cd65849451820f1d43072d8c01c",
62
+ "gitHead": "2a00c9bde5b128f5463344b6567248c3a1d7f7a1",
63
63
  "engines": {
64
64
  "node": ">=16"
65
65
  }