@dbml/core 3.1.4 → 3.1.5-alpha.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.
@@ -3,11 +3,13 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.TABLE_GROUP = exports.TABLE = exports.REF = exports.ENUM = exports.DEFAULT_SCHEMA_NAME = void 0;
6
+ exports.TABLE_GROUP = exports.TABLE = exports.REF = exports.NOTE = exports.ENUM = exports.DEFAULT_SCHEMA_NAME = void 0;
7
7
  var DEFAULT_SCHEMA_NAME = 'public';
8
8
  exports.DEFAULT_SCHEMA_NAME = DEFAULT_SCHEMA_NAME;
9
9
  var TABLE = 'table';
10
10
  exports.TABLE = TABLE;
11
+ var NOTE = 'note';
12
+ exports.NOTE = NOTE;
11
13
  var ENUM = 'enum';
12
14
  exports.ENUM = ENUM;
13
15
  var REF = 'ref';
@@ -11,6 +11,7 @@ var _ref2 = _interopRequireDefault(require("./ref"));
11
11
  var _enum2 = _interopRequireDefault(require("./enum"));
12
12
  var _tableGroup = _interopRequireDefault(require("./tableGroup"));
13
13
  var _table = _interopRequireDefault(require("./table"));
14
+ var _note = _interopRequireDefault(require("./note"));
14
15
  var _element = _interopRequireDefault(require("./element"));
15
16
  var _config = require("./config");
16
17
  var _dbState = _interopRequireDefault(require("./dbState"));
@@ -41,6 +42,8 @@ var Database = /*#__PURE__*/function (_Element) {
41
42
  schemas = _ref$schemas === void 0 ? [] : _ref$schemas,
42
43
  _ref$tables = _ref.tables,
43
44
  tables = _ref$tables === void 0 ? [] : _ref$tables,
45
+ _ref$notes = _ref.notes,
46
+ notes = _ref$notes === void 0 ? [] : _ref$notes,
44
47
  _ref$enums = _ref.enums,
45
48
  enums = _ref$enums === void 0 ? [] : _ref$enums,
46
49
  _ref$refs = _ref.refs,
@@ -57,16 +60,18 @@ var Database = /*#__PURE__*/function (_Element) {
57
60
  _this.generateId();
58
61
  _this.hasDefaultSchema = false;
59
62
  _this.schemas = [];
63
+ _this.notes = [];
60
64
  _this.note = project.note ? (0, _lodash.get)(project, 'note.value', project.note) : null;
61
65
  _this.noteToken = project.note ? (0, _lodash.get)(project, 'note.token', project.noteToken) : null;
62
66
  _this.databaseType = project.database_type;
63
67
  _this.name = project.name;
64
68
  _this.aliases = aliases;
65
-
69
+ _this.processNotes(notes);
66
70
  // The process order is important. Do not change !
67
71
  _this.processSchemas(schemas);
68
72
  _this.processSchemaElements(enums, _config.ENUM);
69
73
  _this.processSchemaElements(tables, _config.TABLE);
74
+ _this.processSchemaElements(notes, _config.NOTE);
70
75
  _this.processSchemaElements(refs, _config.REF);
71
76
  _this.processSchemaElements(tableGroups, _config.TABLE_GROUP);
72
77
  return _this;
@@ -76,13 +81,38 @@ var Database = /*#__PURE__*/function (_Element) {
76
81
  value: function generateId() {
77
82
  this.id = this.dbState.generateId('dbId');
78
83
  }
84
+ }, {
85
+ key: "processNotes",
86
+ value: function processNotes(rawNotes) {
87
+ var _this2 = this;
88
+ rawNotes.forEach(function (note) {
89
+ _this2.pushNote(new _note["default"](_objectSpread(_objectSpread({}, note), {}, {
90
+ database: _this2
91
+ })));
92
+ });
93
+ }
94
+ }, {
95
+ key: "pushNote",
96
+ value: function pushNote(note) {
97
+ this.checkNote(note);
98
+ this.notes.push(note);
99
+ }
100
+ }, {
101
+ key: "checkNote",
102
+ value: function checkNote(note) {
103
+ if (this.notes.some(function (n) {
104
+ return n.name === note.name;
105
+ })) {
106
+ note.error("Notes ".concat(note.name, " existed"));
107
+ }
108
+ }
79
109
  }, {
80
110
  key: "processSchemas",
81
111
  value: function processSchemas(rawSchemas) {
82
- var _this2 = this;
112
+ var _this3 = this;
83
113
  rawSchemas.forEach(function (schema) {
84
- _this2.pushSchema(new _schema["default"](_objectSpread(_objectSpread({}, schema), {}, {
85
- database: _this2
114
+ _this3.pushSchema(new _schema["default"](_objectSpread(_objectSpread({}, schema), {}, {
115
+ database: _this3
86
116
  })));
87
117
  });
88
118
  }
@@ -104,16 +134,16 @@ var Database = /*#__PURE__*/function (_Element) {
104
134
  }, {
105
135
  key: "processSchemaElements",
106
136
  value: function processSchemaElements(elements, elementType) {
107
- var _this3 = this;
137
+ var _this4 = this;
108
138
  var schema;
109
139
  elements.forEach(function (element) {
110
140
  if (element.schemaName) {
111
- schema = _this3.findOrCreateSchema(element.schemaName);
141
+ schema = _this4.findOrCreateSchema(element.schemaName);
112
142
  if (element.schemaName === _config.DEFAULT_SCHEMA_NAME) {
113
143
  // this.hasDefaultSchema = true;
114
144
  }
115
145
  } else {
116
- schema = _this3.findOrCreateSchema(_config.DEFAULT_SCHEMA_NAME);
146
+ schema = _this4.findOrCreateSchema(_config.DEFAULT_SCHEMA_NAME);
117
147
  }
118
148
  switch (elementType) {
119
149
  case _config.TABLE:
@@ -225,6 +255,9 @@ var Database = /*#__PURE__*/function (_Element) {
225
255
  return {
226
256
  schemas: this.schemas.map(function (s) {
227
257
  return s["export"]();
258
+ }),
259
+ notes: this.notes.map(function (n) {
260
+ return n["export"]();
228
261
  })
229
262
  };
230
263
  }
@@ -234,6 +267,9 @@ var Database = /*#__PURE__*/function (_Element) {
234
267
  return {
235
268
  schemaIds: this.schemas.map(function (s) {
236
269
  return s.id;
270
+ }),
271
+ noteIds: this.notes.map(function (n) {
272
+ return n.id;
237
273
  })
238
274
  };
239
275
  }
@@ -245,6 +281,7 @@ var Database = /*#__PURE__*/function (_Element) {
245
281
  id: this.id
246
282
  }, this.shallowExport()), this.exportChildIds())),
247
283
  schemas: {},
284
+ notes: {},
248
285
  refs: {},
249
286
  enums: {},
250
287
  tableGroups: {},
@@ -258,6 +295,9 @@ var Database = /*#__PURE__*/function (_Element) {
258
295
  this.schemas.forEach(function (schema) {
259
296
  return schema.normalize(normalizedModel);
260
297
  });
298
+ this.notes.forEach(function (note) {
299
+ return note.normalize(normalizedModel);
300
+ });
261
301
  return normalizedModel;
262
302
  }
263
303
  }]);
@@ -19,6 +19,7 @@ var DbState = /*#__PURE__*/function () {
19
19
  this.tableGroupId = 1;
20
20
  this.refId = 1;
21
21
  this.tableId = 1;
22
+ this.noteId = 1;
22
23
  this.enumValueId = 1;
23
24
  this.endpointId = 1;
24
25
  this.indexId = 1;
@@ -129,6 +129,11 @@ var Parser = /*#__PURE__*/function () {
129
129
  value: function parseMSSQLToJSON(str) {
130
130
  return _mssqlParser["default"].parseWithPegError(str);
131
131
  }
132
+ }, {
133
+ key: "parse",
134
+ value: function parse(str, format) {
135
+ return new Parser().parse(str, format);
136
+ }
132
137
  }]);
133
138
  return Parser;
134
139
  }();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dbml/core",
3
- "version": "3.1.4",
3
+ "version": "3.1.5-alpha.0",
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.1.4",
35
+ "@dbml/parse": "^3.1.5-alpha.0",
36
36
  "antlr4": "^4.13.1",
37
37
  "lodash": "^4.17.15",
38
38
  "parsimmon": "^1.13.0",
@@ -59,5 +59,5 @@
59
59
  "\\.(?!json$)[^.]*$": "@glen/jest-raw-loader"
60
60
  }
61
61
  },
62
- "gitHead": "5d1db8fcffda24f8bae4476a72025215fb6335b4"
62
+ "gitHead": "21f760b3d70c53ca78c40720f07f99933862349e"
63
63
  }
@@ -3,6 +3,7 @@ import Ref, { NormalizedRef } from './ref';
3
3
  import Enum, { NormalizedEnum } from './enum';
4
4
  import TableGroup, { NormalizedTableGroup } from './tableGroup';
5
5
  import Table, { NormalizedTable } from './table';
6
+ import Note, { NormalizedNote } from './note';
6
7
  import Element, { RawNote, Token } from './element';
7
8
  import DbState from './dbState';
8
9
  import { NormalizedEndpoint } from './endpoint';
@@ -18,6 +19,7 @@ export interface Project {
18
19
  export interface RawDatabase {
19
20
  schemas: Schema[];
20
21
  tables: Table[];
22
+ notes: Note[];
21
23
  enums: Enum[];
22
24
  refs: Ref[];
23
25
  tableGroups: TableGroup[];
@@ -27,6 +29,7 @@ declare class Database extends Element {
27
29
  dbState: DbState;
28
30
  hasDefaultSchema: boolean;
29
31
  schemas: Schema[];
32
+ notes: Note[];
30
33
  note: string;
31
34
  noteToken: Token;
32
35
  databaseType: string;
@@ -99,6 +102,12 @@ declare class Database extends Element {
99
102
  note: string;
100
103
  alias: string;
101
104
  }[];
105
+ notes: {
106
+ id: number;
107
+ name: string;
108
+ content: string;
109
+ headerColor: string;
110
+ }[];
102
111
  };
103
112
  shallowExport(): {
104
113
  hasDefaultSchema: boolean;
@@ -165,9 +174,16 @@ declare class Database extends Element {
165
174
  note: string;
166
175
  alias: string;
167
176
  }[];
177
+ notes: {
178
+ id: number;
179
+ name: string;
180
+ content: string;
181
+ headerColor: string;
182
+ }[];
168
183
  };
169
184
  exportChildIds(): {
170
185
  schemaIds: number[];
186
+ noteIds: number[];
171
187
  };
172
188
  normalize(): NormalizedDatabase;
173
189
  }
@@ -180,9 +196,11 @@ export interface NormalizedDatabase {
180
196
  databaseType: string;
181
197
  name: string;
182
198
  schemaIds: number[];
199
+ noteIds: number[];
183
200
  };
184
201
  };
185
202
  schemas: NormalizedSchema;
203
+ notes: NormalizedNote;
186
204
  refs: NormalizedRef;
187
205
  enums: NormalizedEnum;
188
206
  tableGroups: NormalizedTableGroup;
@@ -5,6 +5,7 @@ export default class DbState {
5
5
  tableGroupId: number;
6
6
  refId: number;
7
7
  tableId: number;
8
+ noteId: number;
8
9
  enumValueId: number;
9
10
  endpointId: number;
10
11
  indexId: number;
@@ -0,0 +1,37 @@
1
+ import Element, { Token } from './element';
2
+ import Database from './database';
3
+ import DbState from './dbState';
4
+ import { NormalizedDatabase } from './database';
5
+ interface RawStickyNote {
6
+ name: string;
7
+ content: string;
8
+ database: Database;
9
+ token: Token;
10
+ headerColor: string;
11
+ }
12
+ declare class Note extends Element {
13
+ name: string;
14
+ content: string;
15
+ noteToken: Token;
16
+ headerColor: string;
17
+ database: Database;
18
+ dbState: DbState;
19
+ id: number;
20
+ constructor({ name, content, token, headerColor, database }: RawStickyNote);
21
+ generateId(): void;
22
+ export(): {
23
+ name: string;
24
+ content: string;
25
+ headerColor: string;
26
+ };
27
+ normalize(model: NormalizedDatabase): void;
28
+ }
29
+ export interface NormalizedNote {
30
+ [id: number]: {
31
+ id: number;
32
+ name: string;
33
+ content: string;
34
+ headerColor: string;
35
+ };
36
+ }
37
+ export default Note;
@@ -159,6 +159,7 @@ declare class Schema extends Element {
159
159
  };
160
160
  exportChildIds(): {
161
161
  tableIds: number[];
162
+ noteIds: number[];
162
163
  enumIds: number[];
163
164
  tableGroupIds: number[];
164
165
  refIds: number[];
@@ -180,6 +181,7 @@ export interface NormalizedSchema {
180
181
  note: string;
181
182
  alias: string;
182
183
  tableIds: number[];
184
+ noteIds: number[];
183
185
  enumIds: number[];
184
186
  tableGroupIds: number[];
185
187
  refIds: number[];
@@ -1,7 +1,7 @@
1
1
  import { Compiler } from '@dbml/parse';
2
2
  import Database, { RawDatabase } from '../model_structure/database';
3
3
  declare class Parser {
4
- constructor(DBMLCompiler: Compiler?);
4
+ constructor(DBMLCompiler?: Compiler);
5
5
  static parseJSONToDatabase(rawDatabase: RawDatabase): Database;
6
6
  static parseMySQLToJSON(str: string): RawDatabase;
7
7
  static parsePostgresToJSON(str: string): RawDatabase;
@@ -9,6 +9,10 @@ declare class Parser {
9
9
  static parseDBMLToJSON(str: string): RawDatabase;
10
10
  static parseSchemaRbToJSON(str: string): RawDatabase;
11
11
  static parseMSSQLToJSON(str: string): RawDatabase;
12
+ /**
13
+ * Should use parse() instance method instead of this static method whenever possible
14
+ */
15
+ static parse(str: string, format: 'mysql' | 'postgres' | 'dbml' | 'dbmlv2' | 'schemarb' | 'mssql' | 'json'): Database;
12
16
  parse(str: string, format: 'mysql' | 'postgres' | 'dbml' | 'dbmlv2' | 'schemarb' | 'mssql' | 'json'): Database;
13
17
  }
14
18
  export default Parser;