@dbml/core 10.3.1 → 10.4.0-alpha.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.
@@ -4,7 +4,7 @@ require("../_virtual/_rolldown/runtime.cjs");const e=require("../node_modules/lo
4
4
  `)}\n}\n`});return n.length?n.join(`
5
5
  `):``}static getFieldLines(e,t){return t.tables[e].fieldIds.map(e=>{let n=t.fields[e],i=``;n.type.schemaName&&n.type.schemaName!==`public`&&(i=r.hasWhiteSpace(n.type.schemaName)?`"${n.type.schemaName}".`:`${n.type.schemaName}.`);let a=`"${n.name}" ${i}${r.hasWhiteSpace(n.type.type_name)||r.hasSquareBracket(n.type.type_name)?`"${n.type.type_name}"`:n.type.type_name}`,o=[];if(n.unique&&o.push(`unique`),n.pk&&o.push(`pk`),n.not_null&&o.push(`not null`),n.increment&&o.push(`increment`),n.checkIds&&o.push(...n.checkIds.map(e=>`check: \`${t.checks[e].expression}\``)),n.dbdefault&&!(n.dbdefault.type===`boolean`&&(n.dbdefault.value===null||typeof n.dbdefault.value==`string`&&n.dbdefault.value.toLowerCase()===`null`))){let e=`default: `;switch(n.dbdefault.type){case`boolean`:case`number`:e+=`${n.dbdefault.value}`;break;case`string`:{let t=String(n.dbdefault.value),r=t.includes(`
6
6
  `)?`'''`:`'`;e+=`${r}${t}${r}`;break}case`expression`:e+=`\`${n.dbdefault.value}\``;break;default:e+=`\`${n.dbdefault.value}\``;break}o.push(e)}return n.note&&o.push(`note: ${r.escapeNote(n.note)}`),o.length>0&&(a+=` [${o.join(`, `)}]`),a})}static getIndexLines(e,t){return t.tables[e].indexIds.map(e=>{let n=``,r=t.indexes[e];if(r.columnIds.length>1)n=`(${r.columnIds.map(e=>{let n=t.indexColumns[e];return n.type===`expression`?`\`${n.value}\``:n.value}).join(`, `)})`;else if(r.columnIds.length===1){let e=t.indexColumns[r.columnIds[0]];n=e.type===`expression`?`\`${e.value}\``:e.value}let i=[];return r.pk&&i.push(`pk`),r.type&&i.push(`type: ${r.type.toLowerCase()}`),r.unique&&i.push(`unique`),r.name&&i.push(`name: "${r.name}"`),i.length>1?n+=` [${i.join(`, `)}]`:i.length===1&&(n+=` [${i[0]}]`),n})}static getCheckLines(e,t){return t.tables[e].checkIds.map(e=>{let n=``,{expression:r,name:i}=t.checks[e];return n+=`\`${r}\``,i&&(n+=` [name: '${i}']`),n})}static getTableContentArr(e,t){return e.map(e=>({tableId:e,fieldContents:r.getFieldLines(e,t),checkContents:r.getCheckLines(e,t),indexContents:r.getIndexLines(e,t)}))}static getTableSettings(e){let t=``;return e.headerColor&&(t+=`headerColor: ${e.headerColor}, `),t.endsWith(`, `)&&(t=t.replace(/,\s$/,``)),t?` [${t}]`:``}static exportTables(e,n){let o=r.getTableContentArr(e,n).map(e=>{let o=n.tables[e.tableId],s=n.schemas[o.schemaId],c=r.getTableSettings(o),l=`"${o.name}"`;i.shouldPrintSchema(s,n)&&(l=`"${s.name}"."${o.name}"`);let u=o.alias?` as ${(0,a.addDoubleQuoteIfNeeded)(o.alias)}`:``,d=e.fieldContents.map(e=>` ${e}\n`).join(``),f=``;t.default(e.checkContents)||(f=`\n Checks {\n${e.checkContents.map(e=>` ${e}\n`).join(``)} }\n`);let p=``;t.default(e.indexContents)||(p=`\n Indexes {\n${e.indexContents.map(e=>` ${e}\n`).join(``)} }\n`);let m=o.note?` Note: ${r.escapeNote(o.note)}\n`:``;return`Table ${l}${u}${c} {\n${d}${f}${p}${m}}\n`});return o.length?o.join(`
7
- `):``}static buildFieldName(e,t){let n=e.map(e=>`"${t.fields[e].name}"`).join(`, `);return e.length===1?n:`(${n})`}static exportRefs(e,t){let n=e.map(e=>{let n=t.refs[e],o=n.endpointIds.findIndex(e=>(0,a.parseCardinality)(t.endpoints[e].relation).max===1),s=o===-1?0:o,c=t.endpoints[n.endpointIds[s]],l=t.endpoints[n.endpointIds[1-s]],u=(0,a.getRelationshipOp)(c.relation,l.relation),d=t.fields[c.fieldIds[0]],f=t.tables[d.tableId],p=t.schemas[f.schemaId],m=r.buildFieldName(c.fieldIds,t),h=`Ref`;n.name&&(h+=` ${i.shouldPrintSchema(t.schemas[n.schemaId],t)?`"${t.schemas[n.schemaId].name}".`:``}"${n.name}"`),h+=`:`,h+=`${i.shouldPrintSchema(p,t)?`"${p.name}".`:``}"${f.name}".${m} `;let g=t.fields[l.fieldIds[0]],_=t.tables[g.tableId],v=t.schemas[_.schemaId],y=r.buildFieldName(l.fieldIds,t);h+=`${u} `,h+=`${i.shouldPrintSchema(v,t)?`"${v.name}".`:``}"${_.name}".${y}`;let b=[];return n.onUpdate&&b.push(`update: ${n.onUpdate.toLowerCase()}`),n.onDelete&&b.push(`delete: ${n.onDelete.toLowerCase()}`),n.inactive&&b.push(`inactive`),b.length>0&&(h+=` [${b.join(`, `)}]`),h+=`
7
+ `):``}static buildFieldName(e,t){let n=e.map(e=>`"${t.fields[e].name}"`).join(`, `);return e.length===1?n:`(${n})`}static exportRefs(e,t){let n=e.map(e=>{let n=t.refs[e],o=n.endpointIds.findIndex(e=>(0,a.parseCardinality)(t.endpoints[e].relation).max===1),s=o===-1?0:o,c=t.endpoints[n.endpointIds[s]],l=t.endpoints[n.endpointIds[1-s]],u=(0,a.getRelationshipOp)(c.relation,l.relation),d=t.fields[c.fieldIds[0]],f=t.tables[d.tableId],p=t.schemas[f.schemaId],m=r.buildFieldName(c.fieldIds,t),h=`Ref`;n.name&&(h+=` ${i.shouldPrintSchema(t.schemas[n.schemaId],t)?`"${t.schemas[n.schemaId].name}".`:``}"${n.name}"`),h+=`:`,h+=`${i.shouldPrintSchema(p,t)?`"${p.name}".`:``}"${f.name}".${m} `;let g=t.fields[l.fieldIds[0]],_=t.tables[g.tableId],v=t.schemas[_.schemaId],y=r.buildFieldName(l.fieldIds,t);h+=`${u} `,h+=`${i.shouldPrintSchema(v,t)?`"${v.name}".`:``}"${_.name}".${y}`;let b=[];return n.onUpdate&&b.push(`update: ${n.onUpdate.toLowerCase()}`),n.onDelete&&b.push(`delete: ${n.onDelete.toLowerCase()}`),n.color&&b.push(`color: ${n.color}`),n.inactive&&b.push(`inactive`),Object.entries(n.metadata??{}).forEach(([e,t])=>{b.push(`${e}: ${r.formatDepCustomValue(t)}`)}),b.length>0&&(h+=` [${b.join(`, `)}]`),h+=`
8
8
  `,h});return n.length?n.join(`
9
9
  `):``}static getTableGroupSettings(e){let t=[];return e.color&&t.push(`color: ${e.color}`),t.length?` [${t.join(`, `)}]`:``}static formatDepEndpoint(e){let t=e.schemaName?`"${e.schemaName}".`:``,n=`"${e.tableName}"`;return!e.fieldNames||e.fieldNames.length===0?`${t}${n}`:`${t}${n}.${e.fieldNames.length===1?`"${e.fieldNames[0]}"`:`(${e.fieldNames.map(e=>`"${e}"`).join(`, `)})`}`}static formatDepCustomValue(e){return typeof e==`string`?`'${(0,a.escapeString)(e)}'`:e==null?`null`:String(e)}static exportDeps(e,t){let n=e.map(e=>{let n=t.deps[e],i=n.edgeIds.map(e=>t.depEdges[e]),a=!!n.note,o=n.metadata?Object.entries(n.metadata):[],s=a||o.length>0;if(i.length===1&&!s)return`Dep: ${r.formatDepEndpoint(i[0].upstream)} -> ${r.formatDepEndpoint(i[0].downstream)}\n`;let c=`Dep {
10
10
  `;return i.forEach(e=>{c+=` ${r.formatDepEndpoint(e.upstream)} -> ${r.formatDepEndpoint(e.downstream)}\n`}),s&&(c+=`
@@ -115,7 +115,9 @@ var d = class r {
115
115
  let _ = t.fields[l.fieldIds[0]], v = t.tables[_.tableId], y = t.schemas[v.schemaId], b = r.buildFieldName(l.fieldIds, t);
116
116
  g += `${d} `, g += `${i(y, t) ? `"${y.name}".` : ""}"${v.name}".${b}`;
117
117
  let x = [];
118
- return n.onUpdate && x.push(`update: ${n.onUpdate.toLowerCase()}`), n.onDelete && x.push(`delete: ${n.onDelete.toLowerCase()}`), n.inactive && x.push("inactive"), x.length > 0 && (g += ` [${x.join(", ")}]`), g += "\n", g;
118
+ return n.onUpdate && x.push(`update: ${n.onUpdate.toLowerCase()}`), n.onDelete && x.push(`delete: ${n.onDelete.toLowerCase()}`), n.color && x.push(`color: ${n.color}`), n.inactive && x.push("inactive"), Object.entries(n.metadata ?? {}).forEach(([e, t]) => {
119
+ x.push(`${e}: ${r.formatDepCustomValue(t)}`);
120
+ }), x.length > 0 && (g += ` [${x.join(", ")}]`), g += "\n", g;
119
121
  });
120
122
  return n.length ? n.join("\n") : "";
121
123
  }
@@ -1 +1 @@
1
- require("../_virtual/_rolldown/runtime.cjs"),require("./config.cjs");const e=require("./element.cjs"),t=require("./endpoint.cjs");function n(e,t){return e[0].equals(t[0])&&e[1].equals(t[1])}var r=class extends e.default{constructor({name:e,color:t,endpoints:n,onDelete:r,onUpdate:i,inactive:a,token:o,schema:s={},injectedPartial:c=null}={}){super(o),this.name=e,this.color=t,this.onDelete=r,this.onUpdate=i,this.inactive=a,this.endpoints=[],this.schema=s,this.injectedPartial=c,this.dbState=this.schema.dbState,this.generateId(),this.processEndpoints(n)}generateId(){this.id=this.dbState.generateId(`refId`)}processEndpoints(e){e.forEach(e=>{this.endpoints.push(new t.default({...e,ref:this})),e.schemaName}),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`)}equals(e){return n(this.endpoints,e.endpoints)||n(this.endpoints,e.endpoints.slice().reverse())}export(){return{...this.shallowExport(),...this.exportChild()}}shallowExport(){return{name:this.name,color:this.color,onDelete:this.onDelete,onUpdate:this.onUpdate,inactive:this.inactive,injectedPartialId:this.injectedPartial?.id}}exportChild(){return{endpoints:this.endpoints.map(e=>e.export())}}exportChildIds(){return{endpointIds:this.endpoints.map(e=>e.id)}}exportParentIds(){return{schemaId:this.schema.id}}normalize(e){e.refs[this.id]={id:this.id,...this.shallowExport(),...this.exportChildIds(),...this.exportParentIds()},this.endpoints.forEach(t=>t.normalize(e))}};exports.default=r;
1
+ require("../_virtual/_rolldown/runtime.cjs"),require("./config.cjs");const e=require("./element.cjs"),t=require("../packages/dbml-core/node_modules/lodash/lodash.cjs"),n=require("./endpoint.cjs");var r=t.default;function i(e,t){return e[0].equals(t[0])&&e[1].equals(t[1])}var a=class extends e.default{constructor({name:e,color:t,note:n,noteToken:i=null,endpoints:a,onDelete:o,onUpdate:s,inactive:c,token:l,schema:u={},injectedPartial:d=null,metadata:f={}}={}){super(l),this.name=e,this.color=t,this.note=n?(0,r.get)(n,`value`,n):null,this.noteToken=n?(0,r.get)(n,`token`,i):null,this.onDelete=o,this.onUpdate=s,this.inactive=c,this.metadata=f,this.endpoints=[],this.schema=u,this.injectedPartial=d,this.dbState=this.schema.dbState,this.generateId(),this.processEndpoints(a)}generateId(){this.id=this.dbState.generateId(`refId`)}processEndpoints(e){e.forEach(e=>{this.endpoints.push(new n.default({...e,ref:this})),e.schemaName}),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`)}equals(e){return i(this.endpoints,e.endpoints)||i(this.endpoints,e.endpoints.slice().reverse())}export(){return{...this.shallowExport(),...this.exportChild()}}shallowExport(){return{name:this.name,color:this.color,note:this.note,onDelete:this.onDelete,onUpdate:this.onUpdate,inactive:this.inactive,metadata:this.metadata,injectedPartialId:this.injectedPartial?.id}}exportChild(){return{endpoints:this.endpoints.map(e=>e.export())}}exportChildIds(){return{endpointIds:this.endpoints.map(e=>e.id)}}exportParentIds(){return{schemaId:this.schema.id}}normalize(e){e.refs[this.id]={id:this.id,...this.shallowExport(),...this.exportChildIds(),...this.exportParentIds()},this.endpoints.forEach(t=>t.normalize(e))}};exports.default=a;
@@ -1,27 +1,29 @@
1
1
  import "./config.mjs";
2
2
  import e from "./element.mjs";
3
- import t from "./endpoint.mjs";
3
+ import { require_lodash as t } from "../packages/dbml-core/node_modules/lodash/lodash.mjs";
4
+ import n from "./endpoint.mjs";
4
5
  //#region src/model_structure/ref.js
5
- function n(e, t) {
6
+ var r = t();
7
+ function i(e, t) {
6
8
  return e[0].equals(t[0]) && e[1].equals(t[1]);
7
9
  }
8
- var r = class extends e {
9
- constructor({ name: e, color: t, endpoints: n, onDelete: r, onUpdate: i, inactive: a, token: o, schema: s = {}, injectedPartial: c = null } = {}) {
10
- super(o), this.name = e, this.color = t, this.onDelete = r, this.onUpdate = i, this.inactive = a, this.endpoints = [], this.schema = s, this.injectedPartial = c, this.dbState = this.schema.dbState, this.generateId(), this.processEndpoints(n);
10
+ var a = class extends e {
11
+ constructor({ name: e, color: t, note: n, noteToken: i = null, endpoints: a, onDelete: o, onUpdate: s, inactive: c, token: l, schema: u = {}, injectedPartial: d = null, metadata: f = {} } = {}) {
12
+ super(l), this.name = e, this.color = t, this.note = n ? (0, r.get)(n, "value", n) : null, this.noteToken = n ? (0, r.get)(n, "token", i) : null, this.onDelete = o, this.onUpdate = s, this.inactive = c, this.metadata = f, this.endpoints = [], this.schema = u, this.injectedPartial = d, this.dbState = this.schema.dbState, this.generateId(), this.processEndpoints(a);
11
13
  }
12
14
  generateId() {
13
15
  this.id = this.dbState.generateId("refId");
14
16
  }
15
17
  processEndpoints(e) {
16
18
  e.forEach((e) => {
17
- this.endpoints.push(new t({
19
+ this.endpoints.push(new n({
18
20
  ...e,
19
21
  ref: this
20
22
  })), e.schemaName;
21
23
  }), 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");
22
24
  }
23
25
  equals(e) {
24
- return n(this.endpoints, e.endpoints) || n(this.endpoints, e.endpoints.slice().reverse());
26
+ return i(this.endpoints, e.endpoints) || i(this.endpoints, e.endpoints.slice().reverse());
25
27
  }
26
28
  export() {
27
29
  return {
@@ -33,9 +35,11 @@ var r = class extends e {
33
35
  return {
34
36
  name: this.name,
35
37
  color: this.color,
38
+ note: this.note,
36
39
  onDelete: this.onDelete,
37
40
  onUpdate: this.onUpdate,
38
41
  inactive: this.inactive,
42
+ metadata: this.metadata,
39
43
  injectedPartialId: this.injectedPartial?.id
40
44
  };
41
45
  }
@@ -58,4 +62,4 @@ var r = class extends e {
58
62
  }
59
63
  };
60
64
  //#endregion
61
- export { r as default };
65
+ export { a as default };
@@ -1 +1 @@
1
- require("../../_virtual/_rolldown/runtime.cjs");var e=`10.3.1`;exports.version=e;
1
+ require("../../_virtual/_rolldown/runtime.cjs");var e=`10.4.0-alpha.1`;exports.version=e;
@@ -1,4 +1,4 @@
1
1
  //#region package.json
2
- var e = "10.3.1";
2
+ var e = "10.4.0-alpha.1";
3
3
  //#endregion
4
4
  export { e as version };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "$schema": "https://json.schemastore.org/package",
3
3
  "name": "@dbml/core",
4
- "version": "10.3.1",
4
+ "version": "10.4.0-alpha.1",
5
5
  "description": "> TODO: description",
6
6
  "author": "Holistics <dev@holistics.io>",
7
7
  "license": "Apache-2.0",
@@ -48,7 +48,7 @@
48
48
  "lint:fix": "eslint --fix ."
49
49
  },
50
50
  "dependencies": {
51
- "@dbml/parse": "^10.3.1",
51
+ "@dbml/parse": "^10.4.0-alpha.1",
52
52
  "antlr4": "^4.13.1",
53
53
  "lodash": "^4.18.1",
54
54
  "lodash-es": "^4.18.1",
@@ -59,7 +59,7 @@
59
59
  "devDependencies": {
60
60
  "bluebird": "^3.5.5"
61
61
  },
62
- "gitHead": "34108bb9346df5f22d5224549c106ff47ca931c8",
62
+ "gitHead": "2abacb40597047ca589b2c18117f57f8fe3978e6",
63
63
  "engines": {
64
64
  "node": ">=16"
65
65
  }
@@ -1,4 +1,5 @@
1
- import Element, { Token, Color } from './element';
1
+ import type { CustomMetadata } from '@dbml/parse';
2
+ import Element, { Token, Color, RawNote } from './element';
2
3
  import Endpoint from './endpoint';
3
4
  import Schema from './schema';
4
5
  import DbState from './dbState';
@@ -7,26 +8,31 @@ import TablePartial from './tablePartial';
7
8
  export interface RawRef {
8
9
  name: string;
9
10
  color?: Color;
11
+ note?: RawNote;
10
12
  endpoints: Endpoint[];
11
13
  onDelete: any;
12
14
  onUpdate: any;
13
15
  inactive?: boolean;
16
+ metadata?: CustomMetadata;
14
17
  token: Token;
15
18
  schema: Schema;
16
19
  }
17
20
  declare class Ref extends Element {
18
21
  name: string;
19
22
  color?: Color;
23
+ note: string;
24
+ noteToken: Token;
20
25
  endpoints: Endpoint[];
21
26
  onDelete: any;
22
27
  onUpdate: any;
23
28
  inactive?: boolean;
29
+ metadata: CustomMetadata;
24
30
  schema: Schema;
25
31
  dbState: DbState;
26
32
  id: number;
27
33
  database: Database;
28
34
  injectedPartial?: TablePartial;
29
- constructor({ name, endpoints, onDelete, onUpdate, token, schema }: RawRef);
35
+ constructor({ name, endpoints, onDelete, onUpdate, metadata, token, schema }: RawRef);
30
36
  generateId(): void;
31
37
  processEndpoints(rawEndpoints: any): void;
32
38
  equals(ref: any): any;
@@ -40,13 +46,16 @@ declare class Ref extends Element {
40
46
  name: string;
41
47
  onDelete: any;
42
48
  onUpdate: any;
49
+ metadata: CustomMetadata;
43
50
  injectedPartialId?: number;
44
51
  };
45
52
  shallowExport(): {
46
53
  name: string;
47
54
  onDelete: any;
48
55
  onUpdate: any;
56
+ metadata: CustomMetadata;
49
57
  injectedPartialId?: number;
58
+ note: string;
50
59
  };
51
60
  exportChild(): {
52
61
  endpoints: {
@@ -71,9 +80,11 @@ export interface NormalizedRef {
71
80
  onUpdate?: string;
72
81
  onDelete?: string;
73
82
  inactive?: boolean;
83
+ metadata: CustomMetadata;
74
84
  schemaId: number;
75
85
  endpointIds: number[];
76
86
  injectedPartialId?: number;
87
+ note: string;
77
88
  }
78
89
 
79
90
  export interface NormalizedRefIdMap {