@dbml/core 8.0.0-prerelease.1 → 8.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.cjs CHANGED
@@ -27,7 +27,7 @@ Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});var e=Object.
27
27
  `;return r})}static exportIndexes(e,t){return e.filter(e=>!t.indexes[e].pk).map(e=>{let n=t.indexes[e],r=t.tables[n.tableId],i=t.schemas[r.schemaId],a=`CREATE`;n.unique&&(a+=` UNIQUE`);let o=n.name?`"${n.name}"`:``;a+=` INDEX`,o&&(a+=` ${o}`),a+=` ON ${mm(i,t)?`"${i.name}".`:``}"${r.name}"`,n.type&&(a+=` USING ${n.type.toUpperCase()}`);let s=[];return n.columnIds.forEach(e=>{let n=t.indexColumns[e],r=``;r=n.type===`expression`?`(${n.value})`:`"${n.value}"`,s.push(r)}),a+=` (${s.join(`, `)})`,a+=`;
28
28
  `,a})}static exportComments(e,t){return e.map(e=>{let n=`COMMENT ON`,r=t.tables[e.tableId],i=t.schemas[r.schemaId];switch(e.type){case`table`:n+=` TABLE ${mm(i,t)?`"${i.name}".`:``}"${r.name}" IS '${r.note.replace(/'/g,`''`)}'`;break;case`column`:{let a=t.fields[e.fieldId];n+=` COLUMN ${mm(i,t)?`"${i.name}".`:``}"${r.name}"."${a.name}" IS '${a.note.replace(/'/g,`''`)}'`;break}default:break}return n+=`;
29
29
  `,n})}static export(t){let n=t.database[1],r=new Set(Object.values(t.tables).map(e=>e.name)),i=new Set,a=n.schemaIds.reduce((n,r)=>{let a=t.schemas[r],{enumIds:o}=a;return mm(a,t)&&n.schemas.push(`CREATE SCHEMA "${a.name}";\n`),qu(o)||e.exportEnums(o,t).forEach(e=>{let[t,r]=e;n.enums.push(r),i.add(t)}),n},{schemas:[],enums:[],tables:[],indexes:[],comments:[],refs:[]}),o=n.schemaIds.reduce((n,a)=>{let{tableIds:o,refIds:s}=t.schemas[a];qu(o)||n.tables.push(...e.exportTables(o,t,i));let c=io(o.map(e=>t.tables[e].indexIds));qu(c)||n.indexes.push(...e.exportIndexes(c,t));let l=io(o.map(e=>{let{fieldIds:n,note:r}=t.tables[e],i=n.filter(e=>t.fields[e].note).map(t=>({type:`column`,fieldId:t,tableId:e}));return r?[{type:`table`,tableId:e}].concat(i):i}));return qu(l)||n.comments.push(...e.exportComments(l,t)),qu(s)||n.refs.push(...e.exportRefs(s,t,r)),n},a),s=e.exportRecords(t),c=qu(s)?[]:[`-- Defer constraint checking for INSERT`,`BEGIN;`,`SET CONSTRAINTS ALL DEFERRED;`,``,...s,``,`SET CONSTRAINTS ALL IMMEDIATE;`,`COMMIT;`];return ac(o.schemas,o.enums,o.tables,o.indexes,o.comments,o.refs,c).join(`
30
- `)}},Vne=Bne,Hne=class{constructor(){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}generateId(e){let t=this[e];return this[e]+=1,t}},Une=class extends Error{constructor(e,t={start:{line:1,column:1}}){super(e),this.location=t,this.error=`error`}},Wne=class{constructor(e){this.token=e}bind(e){this.selection=e}error(e){throw new Une(e,this.token)}},bm=Wne;ud();var Gne=class extends bm{constructor({name:e,token:t,note:n,_enum:r,noteToken:i=null}={}){super(t),e||this.error(`Enum value must have a name`),this.name=e,this.note=n?Ga(n,`value`,n):null,this.noteToken=n?Ga(n,`token`,i):null,this._enum=r,this.dbState=this._enum.dbState,this.generateId()}generateId(){this.id=this.dbState.generateId(`enumValueId`)}export(){return{...this.shallowExport()}}exportParentIds(){return{enumId:this._enum.id}}shallowExport(){return{name:this.name,note:this.note}}normalize(e){e.enumValues[this.id]={id:this.id,...this.shallowExport(),...this.exportParentIds()}}},Kne=Gne;ud();var qne=class extends bm{constructor({name:e,token:t,values:n,note:r,schema:i,noteToken:a=null}={}){super(t),e||this.error(`Enum must have a name`),this.name=e,this.note=r?Ga(r,`value`,r):null,this.noteToken=r?Ga(r,`token`,a):null,this.values=[],this.fields=[],this.schema=i,this.dbState=this.schema.dbState,this.generateId(),this.processValues(n)}generateId(){this.id=this.dbState.generateId(`enumId`)}processValues(e){e.forEach(e=>{this.pushValue(new Kne({...e,_enum:this}))})}pushValue(e){this.checkValue(e),this.values.push(e)}checkValue(e){this.values.some(t=>t.name===e.name)&&e.error(`Enum value "${e.name}" existed in enum ${ym(this.schema)?`"${this.schema.name}".`:``}"${this.name}"`)}pushField(e){this.checkField(e),this.fields.push(e)}checkField(e){this.fields.some(t=>t.id===e.id)&&this.error(`Field ${ym(e.table.schema)?`"${e.table.schema.name}".`:``}"${e.table.name}"."${e.name}" already associated with enum ${ym(this.schema)?`"${this.schema.name}".`:``}${this.name}"`)}export(){return{...this.shallowExport(),...this.exportChild()}}exportChild(){return{values:this.values.map(e=>e.export())}}exportChildIds(){return{valueIds:this.values.map(e=>e.id),fieldIds:this.fields.map(e=>e.id)}}exportParentIds(){return{schemaId:this.schema.id}}shallowExport(){return{name:this.name,note:this.note}}normalize(e){e.enums[this.id]={id:this.id,...this.shallowExport(),...this.exportChildIds(),...this.exportParentIds()},this.values.forEach(t=>t.normalize(e))}},Jne=qne,Yne=class extends bm{constructor({tableName:e,schemaName:t,fieldNames:n,relation:r,token:i,ref:a}){super(i),this.relation=r,this.schemaName=t,this.tableName=e,this.fieldNames=n,this.fields=[],this.ref=a,this.dbState=this.ref.dbState,this.generateId();let o=a.schema.database.findOrCreateSchema(t||`public`).database.findTable(t,e);o||this.error(`Can't find table ${Lne(t)?`"${t}".`:``}"${e}"`),this.setFields(n,o)}generateId(){this.id=this.dbState.generateId(`endpointId`)}equals(e){return this.fields.length===e.fields.length?this.compareFields(e):!1}compareFields(e){let t=this.fields.map(e=>e.id).sort(),n=e.fields.map(e=>e.id).sort();for(let e=0;e<t.length;e+=1)if(t[e]!==n[e])return!1;return!0}export(){return{...this.shallowExport()}}exportParentIds(){return{refId:this.ref.id,fieldIds:this.fields.map(e=>e.id)}}shallowExport(){return{schemaName:this.schemaName,tableName:this.tableName,fieldNames:this.fieldNames,relation:this.relation}}setFields(e,t){let n=e&&e.length?[...e]:[];if(!n.length){let e=t.fields.find(e=>e.pk);if(e)n.push(e.name);else{let e=t.indexes.find(e=>e.pk);e?n=e.columns.map(e=>e.value):this.error(`Can't find primary or composite key in table ${ym(t.schema)?`"${t.schema.name}".`:``}"${t.name}"`)}}n.forEach(e=>{let n=t.findField(e);n||this.error(`Can't find field ${ym(t.schema)?`"${t.schema.name}".`:``}"${e}" in table "${t.name}"`),this.fields.push(n),n.pushEndpoint(this)})}normalize(e){e.endpoints[this.id]={id:this.id,...this.shallowExport(),...this.exportParentIds()}}},Xne=Yne;function Zne(e,t){return e[0].equals(t[0])&&e[1].equals(t[1])}var Qne=class extends bm{constructor({name:e,color:t,endpoints:n,onDelete:r,onUpdate:i,token:a,schema:o={},injectedPartial:s=null}={}){super(a),this.name=e,this.color=t,this.onDelete=r,this.onUpdate=i,this.endpoints=[],this.schema=o,this.injectedPartial=s,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 Xne({...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 Zne(this.endpoints,e.endpoints)||Zne(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,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))}},$ne=Qne,ere=class extends bm{constructor({token:e,name:t,expression:n,table:r,column:i=null,injectedPartial:a=null}={}){super(e),this.name=t,this.expression=n,this.table=r,this.column=i,this.injectedPartial=a,this.dbState=this.table.dbState,this.generateId()}generateId(){this.id=this.dbState.generateId(`checkId`)}export(){return{...this.shallowExport()}}exportParentIds(){return{tableId:this.table.id,columnId:this.column?.id,injectedPartialId:this.injectedPartial?.id}}shallowExport(){return{name:this.name,expression:this.expression}}normalize(e){e.checks[this.id]={id:this.id,...this.shallowExport(),...this.exportParentIds()}}},tre=ere;ud();var nre=class extends bm{constructor({name:e,type:t,unique:n,pk:r,token:i,not_null:a,note:o,dbdefault:s,increment:c,checks:l=[],table:u={},noteToken:d=null,injectedPartial:f=null,injectedToken:p=null}={}){super(i),e||this.error(`Field must have a name`),t||this.error(`Field must have a type`),this.name=e,this.type=t,this.unique=n,this.pk=r,this.not_null=a,this.note=o?Ga(o,`value`,o):null,this.noteToken=o?Ga(o,`token`,d):null,this.dbdefault=s,this.increment=c,this.checks=[],this.endpoints=[],this.table=u,this.injectedPartial=f,this.injectedToken=p,this.dbState=this.table.dbState,this.generateId(),this.bindType(),this.processChecks(l)}generateId(){this.id=this.dbState.generateId(`fieldId`)}bindType(){let e=this.type.type_name,t=this.type.schemaName||`public`;if(this.type.schemaName){let n=this.table.schema.database.findEnum(t,e);if(!n){this.type.type_name=`${t}.${e}`,this.type.originalTypeName=e;return}this._enum=n,n.pushField(this)}else{let n=this.table.schema.database.findEnum(t,e);if(!n)return;this._enum=n,n.pushField(this)}}pushEndpoint(e){this.endpoints.push(e)}export(){return{...this.shallowExport()}}exportParentIds(){return{tableId:this.table.id,enumId:this._enum?this._enum.id:null}}exportChildIds(){return{endpointIds:this.endpoints.map(e=>e.id)}}shallowExport(){return{name:this.name,type:this.type,unique:this.unique,pk:this.pk,not_null:this.not_null,note:this.note,dbdefault:this.dbdefault,increment:this.increment,injectedPartialId:this.injectedPartial?.id??null,checkIds:this.checks.map(e=>e.id)}}normalize(e){e.fields[this.id]={id:this.id,...this.shallowExport(),...this.exportChildIds(),...this.exportParentIds()},this.checks.forEach(t=>t.normalize(e))}processChecks(e){e.forEach(e=>{this.checks.push(new tre({...e,table:this.table,column:this}))})}},rre=nre,ire=class extends bm{constructor({type:e,value:t,index:n,token:r}){super(),this.type=e,this.value=t,this.index=n,this.token=r,this.dbState=this.index.dbState,this.generateId()}generateId(){this.id=this.dbState.generateId(`indexColumnId`)}export(){return{...this.shallowExport()}}exportParentIds(){return{indexId:this.index.id}}shallowExport(){return{type:this.type,value:this.value}}normalize(e){e.indexColumns[this.id]={id:this.id,...this.shallowExport(),...this.exportParentIds()}}},are=ire,ore=class extends bm{constructor({columns:e,type:t,unique:n,pk:r,token:i,name:a,note:o,table:s={},injectedPartial:c=null}={}){super(i),this.name=a,this.type=t,this.unique=n,this.note=o?o.value:null,this.noteToken=o?o.token:null,this.pk=r,this.columns=[],this.table=s,this.injectedPartial=c,this.dbState=this.table.dbState,this.generateId(),this.processIndexColumns(e)}generateId(){this.id=this.dbState.generateId(`indexId`)}processIndexColumns(e){e.forEach(e=>{this.pushIndexColumn(new are({...e,index:this}))})}pushIndexColumn(e){this.checkIndexColumn(e),this.columns.push(e)}checkIndexColumn(e){this.columns.some(t=>t.type===e.type&&t.value===e.value)&&e.error(`Index column ${e.value} existed`)}export(){return{...this.shallowExport(),...this.exportChild()}}exportChild(){return{columns:this.columns.map(e=>e.export())}}exportChildIds(){return{columnIds:this.columns.map(e=>e.id)}}exportParentIds(){return{tableId:this.table.id}}shallowExport(){return{name:this.name,type:this.type,unique:this.unique,pk:this.pk,note:this.note,injectedPartialId:this.injectedPartial?.id}}normalize(e){e.indexes[this.id]={id:this.id,...this.shallowExport(),...this.exportChildIds(),...this.exportParentIds()},this.columns.forEach(t=>t.normalize(e))}},sre=ore;ud();var cre=class extends bm{constructor({name:e,alias:t,note:n,fields:r=[],indexes:i=[],checks:a=[],schema:o={},token:s,headerColor:c,noteToken:l=null,partials:u=[]}={}){super(s),this.name=e,this.alias=t,this.note=n?Ga(n,`value`,n):null,this.noteToken=n?Ga(n,`token`,l):null,this.headerColor=c,this.fields=[],this.indexes=[],this.checks=[],this.schema=o,this.partials=u,this.records=[],this.dbState=this.schema.dbState,this.generateId(),this.processFields(r),this.processPartials(),this.checkFieldCount(),this.processIndexes(i),this.processChecks(a)}generateId(){this.id=this.dbState.generateId(`tableId`)}checkFieldCount(){this.fields.length===0&&this.error(`Table must have at least one field`)}processFields(e){e.forEach(e=>{this.pushField(new rre({...e,table:this}))})}pushField(e){this.checkField(e),this.fields.push(e)}checkField(e){this.fields.some(t=>t.name===e.name)&&e.error(`Field "${e.name}" existed in table ${ym(this.schema)?`"${this.schema.name}".`:``}"${this.name}"`)}processIndexes(e){e.forEach(e=>{this.pushIndex(new sre({...e,table:this}))})}pushIndex(e){this.checkIndex(e),this.indexes.push(e)}checkIndex(e){e.columns.forEach(t=>{t.type===`column`&&!this.findField(t.value)&&e.error(`Column "${t.value}" do not exist in table ${ym(this.schema)?`"${this.schema.name}".`:``}"${this.name}"`)})}processChecks(e){e.forEach(e=>{this.pushCheck(new tre({...e,table:this}))})}pushCheck(e){this.checks.push(e)}findField(e){return this.fields.find(t=>t.name===e)}checkSameId(e){return this.schema.checkSameId(e.schemaName||`public`)&&(this.name===e.name||this.alias===e.name||this.name===e.alias||this.alias&&this.alias===e.alias)}processPartials(){if(!this.partials?.length)return;let e=new Set(this.fields.map(e=>e.name)),t=new Set;Xu(this.note)||t.add(`note`),Xu(this.headerColor)||t.add(`headerColor`);let n=this.partials.sort((e,t)=>t.order-e.order);n.toReversed().forEach(e=>{this.fields.splice(e.order,0,`dummy`)}),n.forEach(n=>{let r=this.schema.database.findTablePartial(n.name);if(r||this.error(`Table partial ${n.name} not found`,n.token),n.id=r.id,r.fields){let t=r.fields.filter(t=>!e.has(t.name)).map(t=>(e.add(t.name),t.inline_refs&&t.inline_refs.forEach(e=>{let n={token:t.token,endpoints:[{tableName:this.name,schemaName:this.schema?.name,fieldNames:[t.name],relation:[`-`,`<`].includes(e.relation)?`1`:`*`,token:t.token},{tableName:e.tableName,schemaName:e.schemaName,fieldNames:e.fieldNames,relation:[`-`,`>`].includes(e.relation)?`1`:`*`,token:e.token}],injectedPartial:r};this.schema.database.injectedRawRefs.push(n)}),new rre({...t,noteToken:null,table:this,injectedPartial:r,injectedToken:n.token})));this.fields.splice(n.order,1,...t)}else this.fields.splice(n.order,1);!t.has(`note`)&&!Xu(r.note)&&(this.noteToken=null,this.note=r.note,t.add(`note`)),!t.has(`headerColor`)&&!Xu(r.headerColor)&&(this.headerColor=r.headerColor,t.add(`headerColor`)),r.indexes.forEach(e=>{this.indexes.push(new sre({...e,table:this,injectedPartial:r}))}),r.checks.forEach(e=>{this.checks.push(new tre({...e,name:e.name&&`${this.name}.${e.name}`,table:this,injectedPartial:r}))})})}export(){return{...this.shallowExport(),...this.exportChild()}}exportChild(){return{fields:this.fields.map(e=>e.export()),indexes:this.indexes.map(e=>e.export())}}exportChildIds(){return{fieldIds:this.fields.map(e=>e.id),indexIds:this.indexes.map(e=>e.id),checkIds:this.checks.map(e=>e.id)}}exportParentIds(){return{schemaId:this.schema.id,groupId:this.group?this.group.id:null}}shallowExport(){return{name:this.name,alias:this.alias,note:this.note,headerColor:this.headerColor,partials:this.partials,recordIds:this.records.map(e=>e.id)}}normalize(e){e.tables[this.id]={id:this.id,...this.shallowExport(),...this.exportChildIds(),...this.exportParentIds()},this.fields.forEach(t=>t.normalize(e)),this.indexes.forEach(t=>t.normalize(e)),this.checks.forEach(t=>t.normalize(e))}},lre=cre;ud();var ure=class extends bm{constructor({name:e,token:t,tables:n=[],schema:r={},note:i,color:a,noteToken:o=null}){super(t),this.name=e,this.tables=[],this.schema=r,this.dbState=this.schema.dbState,this.note=i?Ga(i,`value`,i):null,this.noteToken=i?Ga(i,`token`,o):null,this.color=a,this.generateId(),this.processTables(n)}generateId(){this.id=this.dbState.generateId(`tableGroupId`)}processTables(e){e.forEach(e=>{let t=this.schema.database.findTable(e.schemaName,e.name);t||this.error(`Table ${e.schemaName?`"${e.schemaName}".`:``}${e.name} don't exist`),this.pushTable(t)})}pushTable(e){this.checkTable(e),this.tables.push(e),e.group=this}checkTable(e){this.tables.some(t=>t.id===e.id)&&this.error(`Table ${ym(e.schema)?`"${e.schema.name}".`:``}.${e.name} is already in the group`),e.group&&this.error(`Table ${ym(e.schema)?`"${e.schema.name}".`:``}.${e.name} is already in group ${ym(e.group.schema)?`"${e.group.schema.name}".`:``}${e.group.name}`)}export(){return{...this.shallowExport(),...this.exportChild()}}exportChild(){return{tables:this.tables.map(e=>({tableName:e.name,schemaName:e.schema.name}))}}exportChildIds(){return{tableIds:this.tables.map(e=>e.id)}}exportParentIds(){return{schemaId:this.schema.id}}shallowExport(){return{name:this.name,note:this.note,color:this.color}}normalize(e){e.tableGroups[this.id]={id:this.id,...this.shallowExport(),...this.exportChildIds(),...this.exportParentIds()}}},dre=ure;ud();var fre=class extends bm{constructor({name:e,alias:t,note:n,tables:r=[],refs:i=[],enums:a=[],tableGroups:o=[],token:s,database:c={},noteToken:l=null}={}){super(s),this.tables=[],this.enums=[],this.tableGroups=[],this.refs=[],this.name=e,this.note=n?Ga(n,`value`,n):null,this.noteToken=n?Ga(n,`token`,l):null,this.alias=t,this.database=c,this.dbState=this.database.dbState,this.generateId(),this.processEnums(a),this.processTables(r),this.processRefs(i),this.processTableGroups(o)}generateId(){this.id=this.dbState.generateId(`schemaId`)}processTables(e){e.forEach(e=>{this.pushTable(new lre({...e,schema:this}))})}pushTable(e){this.checkTable(e),this.tables.push(e)}checkTable(e){this.tables.some(t=>t.name===e.name)&&e.error(`Table ${ym(this)?`"${this.name}".`:``}"${e.name}" existed`)}findTable(e){return this.tables.find(t=>t.name===e)}processEnums(e){e.forEach(e=>{this.pushEnum(new Jne({...e,schema:this}))})}pushEnum(e){this.checkEnum(e),this.enums.push(e)}checkEnum(e){this.enums.some(t=>t.name===e.name)&&e.error(`Enum ${ym(this)?`"${this.name}".`:``}"${e.name}" existed`)}processRefs(e){e.forEach(e=>{this.pushRef(new $ne({...e,schema:this}))})}pushRef(e){this.checkRef(e),this.refs.push(e)}checkRef(e){if(this.refs.some(t=>t.equals(e))){let t=e.endpoints[0],n=t.fieldNames.map(JSON.stringify).join(`, `),r=e.endpoints[1],i=r.fieldNames.map(JSON.stringify).join(`, `),a=`"${t.schemaName?`${t.schemaName}"."`:``}${t.tableName}"(${n})`,o=`"${r.schemaName?`${r.schemaName}"."`:``}${r.tableName}"(${i})`;e.error(`Reference with the same endpoints already exists: ${a} references ${o}`)}}processTableGroups(e){e.forEach(e=>{this.pushTableGroup(new dre({...e,schema:this}))})}pushTableGroup(e){this.checkTableGroup(e),this.tableGroups.push(e)}checkTableGroup(e){this.tableGroups.some(t=>t.name===e.name)&&e.error(`Table Group ${ym(this)?`"${this.name}".`:``}"${e.name}" existed`)}checkSameId(e){return this.name===e.name||this.alias===e.name||this.name===e.alias||this.alias&&this.alias===e.alias}export(){return{...this.shallowExport(),...this.exportChild()}}exportChild(){return{tables:this.tables.map(e=>e.export()),enums:this.enums.map(e=>e.export()),tableGroups:this.tableGroups.map(e=>e.export()),refs:this.refs.map(e=>e.export())}}exportChildIds(){return{tableIds:this.tables.map(e=>e.id),enumIds:this.enums.map(e=>e.id),tableGroupIds:this.tableGroups.map(e=>e.id),refIds:this.refs.map(e=>e.id)}}exportParentIds(){return{databaseId:this.database.id}}shallowExport(){return{name:this.name,note:this.note,alias:this.alias}}normalize(e){e.schemas[this.id]={id:this.id,...this.shallowExport(),...this.exportChildIds(),...this.exportParentIds()},this.tables.forEach(t=>t.normalize(e)),this.enums.forEach(t=>t.normalize(e)),this.tableGroups.forEach(t=>t.normalize(e)),this.refs.forEach(t=>t.normalize(e))}},pre=fre,mre=class extends bm{constructor({name:e,content:t,headerColor:n,token:r,database:i={}}={}){super(r),this.name=e,this.content=t,this.headerColor=n,this.database=i,this.dbState=this.database.dbState,this.generateId()}generateId(){this.id=this.dbState.generateId(`noteId`)}export(){return{name:this.name,content:this.content,headerColor:this.headerColor}}normalize(e){e.notes[this.id]={id:this.id,...this.export()}}},hre=mre;ud();var gre=class extends bm{constructor({name:e,note:t,fields:n=[],indexes:r=[],checks:i=[],token:a,headerColor:o,noteToken:s=null,dbState:c}={}){super(a),this.name=e,this.note=t?Ga(t,`value`,t):null,this.noteToken=t?Ga(t,`token`,s):null,this.headerColor=o,this.fields=n,this.indexes=r,this.checks=i,this.dbState=c,this.generateId()}generateId(){this.id=this.dbState.generateId(`tablePartialId`)}export(){return{...this.shallowExport()}}shallowExport(){return{name:this.name,note:this.note,headerColor:this.headerColor,fields:this.fields,indexes:this.indexes}}normalize(e){e.tablePartials[this.id]={id:this.id,...this.shallowExport()}}},_re=gre;ud();var vre=class extends bm{constructor({schemas:e=[],tables:t=[],notes:n=[],enums:r=[],refs:i=[],tableGroups:a=[],project:o={},aliases:s=[],records:c=[],tablePartials:l=[]}){super(),this.dbState=new Hne,this.generateId(),this.hasDefaultSchema=!1,this.schemas=[],this.notes=[],this.note=o.note?Ga(o,`note.value`,o.note):null,this.noteToken=o.note?Ga(o,`note.token`,o.noteToken):null,this.databaseType=o.database_type,this.name=o.name,this.token=o.token,this.aliases=s,this.records=[],this.tablePartials=[],this.injectedRawRefs=[],this.processNotes(n),this.processRecords(c),this.processTablePartials(l),this.processSchemas(e),this.processSchemaElements(r,Ene),this.processSchemaElements(t,wne),this.linkRecordsToTables(),this.processSchemaElements(n,`note`),this.processSchemaElements(i,`ref`),this.processSchemaElements(a,One),this.injectedRawRefs.forEach(e=>{let t=this.findOrCreateSchema(fm),n=new $ne({...e,schema:t});t.refs.some(e=>e.equals(n))||t.pushRef(n)})}generateId(){this.id=this.dbState.generateId(`dbId`)}processNotes(e){e.forEach(e=>{this.pushNote(new hre({...e,database:this}))})}processRecords(e){e.forEach(({schemaName:e,tableName:t,columns:n,values:r,token:i})=>{this.records.push({id:this.dbState.generateId(`recordId`),schemaName:e,tableName:t,columns:n,values:r,token:i})})}processTablePartials(e){e.forEach(e=>{this.tablePartials.push(new _re({...e,dbState:this.dbState}))})}pushNote(e){this.checkNote(e),this.notes.push(e)}checkNote(e){this.notes.some(t=>t.name===e.name)&&e.error(`Notes ${e.name} existed`)}processSchemas(e){e.forEach(e=>{this.pushSchema(new pre({...e,database:this}))})}pushSchema(e){this.checkSchema(e),this.schemas.push(e)}checkSchema(e){this.schemas.some(t=>t.name===e.name)&&e.error(`Schemas ${e.name} existed`)}processSchemaElements(e,t){let n;e.forEach(e=>{switch(e.schemaName?(n=this.findOrCreateSchema(e.schemaName),e.schemaName):n=this.findOrCreateSchema(fm),t){case wne:n.pushTable(new lre({...e,schema:n}));break;case Ene:n.pushEnum(new Jne({...e,schema:n}));break;case One:n.pushTableGroup(new dre({...e,schema:n}));break;case`ref`:n.pushRef(new $ne({...e,schema:n}));break;default:break}})}linkRecordsToTables(){let e={};this.schemas.forEach(t=>{e[t.name]={},t.tables.forEach(n=>{e[t.name][n.name]=n})}),this.records.forEach(t=>{let n=e[t.schemaName??`public`]?.[t.tableName];n&&(t.tableId=n.id,n.records.push(t))})}findOrCreateSchema(e){let t=this.schemas.find(t=>t.name===e||t.alias===e);return t||(t=new pre({name:e,note:{value:e===`public`?`Default ${Xo(fm)} Schema`:null},database:this}),this.pushSchema(t)),t}findTableAlias(e){let t=this.aliases.find(t=>t.name===e);if(!t||t.kind!==`table`)return null;let n=t.value.schemaName||`public`,r=this.schemas.find(e=>e.name===n);if(!r)return null;let{tableName:i}=t.value;return r.tables.find(e=>e.name===i)}findTable(e,t){let n=null;if(!e&&(n=this.findTableAlias(t),n))return n;let r=this.findOrCreateSchema(e||`public`);return r||this.error(`Schema ${e||`public`} don't exist`),r.findTable(t)}findEnum(e,t){let n=this.schemas.find(t=>t.name===e||t.alias===e);return n?n.enums.find(e=>e.name===t):null}findTablePartial(e){return this.tablePartials.find(t=>t.name===e)}export(){return{...this.exportChild()}}shallowExport(){return{hasDefaultSchema:this.hasDefaultSchema,note:this.note,databaseType:this.databaseType,name:this.name}}exportChild(){return{schemas:this.schemas.map(e=>e.export()),notes:this.notes.map(e=>e.export()),records:this.records.map(e=>({...e}))}}exportChildIds(){return{schemaIds:this.schemas.map(e=>e.id),noteIds:this.notes.map(e=>e.id)}}normalize(){let e={database:{[this.id]:{id:this.id,...this.shallowExport(),...this.exportChildIds()}},schemas:{},notes:{},refs:{},enums:{},tableGroups:{},tables:{},endpoints:{},enumValues:{},indexes:{},indexColumns:{},checks:{},fields:{},records:{},tablePartials:{}};return this.schemas.forEach(t=>t.normalize(e)),this.notes.forEach(t=>t.normalize(e)),this.records.forEach(t=>{e.records[t.id]={...t}}),this.tablePartials.forEach(t=>t.normalize(e)),e}},xm=vre,yre=class{static export(e,t){let{isNormalized:n}=t;return!n&&e instanceof xm?JSON.stringify(e.export(),null,2):JSON.stringify(e,null,2)}},bre=yre;ud();var xre=class e{static exportRecords(e){let t=Object.values(e.records||{});return qu(t)?[]:t.map(e=>{let{schemaName:t,tableName:n,columns:r,values:i}=e,a=t?`[${t}].[${n}]`:`[${n}]`,o=r.length>0?`([${r.join(`], [`)}])`:``,s=e=>e.value===null?`NULL`:e.type===`expression`||(0,f.isNumericType)(e.type)?e.value:(0,f.isBooleanType)(e.type)?String(e.value).toUpperCase()===`TRUE`?`1`:`0`:(0,f.isStringType)(e.type)||(0,f.isDateTimeType)(e.type)?`'${e.value.replace(/'/g,`''`)}'`:(0,f.isBinaryType)(e.type)?`0x${e.value}`:`CAST('${e.value.replace(/'/g,`''`)}' AS ${e.type})`;return`INSERT INTO ${a} ${o}\nVALUES\n ${i.map(e=>`(${e.map(s).join(`, `)})`).join(`,
30
+ `)}},Vne=Bne,Hne=class{constructor(){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}generateId(e){let t=this[e];return this[e]+=1,t}},Une=class extends Error{constructor(e,t={start:{line:1,column:1}}){super(e),this.location=t,this.error=`error`}},Wne=class{constructor(e){this.token=e}bind(e){this.selection=e}error(e){throw new Une(e,this.token)}},bm=Wne;ud();var Gne=class extends bm{constructor({name:e,token:t,note:n,_enum:r,noteToken:i=null}={}){super(t),e||this.error(`Enum value must have a name`),this.name=e,this.note=n?Ga(n,`value`,n):null,this.noteToken=n?Ga(n,`token`,i):null,this._enum=r,this.dbState=this._enum.dbState,this.generateId()}generateId(){this.id=this.dbState.generateId(`enumValueId`)}export(){return{...this.shallowExport()}}exportParentIds(){return{enumId:this._enum.id}}shallowExport(){return{name:this.name,note:this.note}}normalize(e){e.enumValues[this.id]={id:this.id,...this.shallowExport(),...this.exportParentIds()}}},Kne=Gne;ud();var qne=class extends bm{constructor({name:e,token:t,values:n,note:r,schema:i,noteToken:a=null}={}){super(t),e||this.error(`Enum must have a name`),this.name=e,this.note=r?Ga(r,`value`,r):null,this.noteToken=r?Ga(r,`token`,a):null,this.values=[],this.fields=[],this.schema=i,this.dbState=this.schema.dbState,this.generateId(),this.processValues(n)}generateId(){this.id=this.dbState.generateId(`enumId`)}processValues(e){e.forEach(e=>{this.pushValue(new Kne({...e,_enum:this}))})}pushValue(e){this.checkValue(e),this.values.push(e)}checkValue(e){this.values.some(t=>t.name===e.name)&&e.error(`Enum value "${e.name}" existed in enum ${ym(this.schema)?`"${this.schema.name}".`:``}"${this.name}"`)}pushField(e){this.checkField(e),this.fields.push(e)}checkField(e){this.fields.some(t=>t.id===e.id)&&this.error(`Field ${ym(e.table.schema)?`"${e.table.schema.name}".`:``}"${e.table.name}"."${e.name}" already associated with enum ${ym(this.schema)?`"${this.schema.name}".`:``}${this.name}"`)}export(){return{...this.shallowExport(),...this.exportChild()}}exportChild(){return{values:this.values.map(e=>e.export())}}exportChildIds(){return{valueIds:this.values.map(e=>e.id),fieldIds:this.fields.map(e=>e.id)}}exportParentIds(){return{schemaId:this.schema.id}}shallowExport(){return{name:this.name,note:this.note}}normalize(e){e.enums[this.id]={id:this.id,...this.shallowExport(),...this.exportChildIds(),...this.exportParentIds()},this.values.forEach(t=>t.normalize(e))}},Jne=qne,Yne=class extends bm{constructor({tableName:e,schemaName:t,fieldNames:n,relation:r,token:i,ref:a}){super(i),this.relation=r,this.schemaName=t,this.tableName=e,this.fieldNames=n,this.fields=[],this.ref=a,this.dbState=this.ref.dbState,this.generateId();let o=a.schema.database.findOrCreateSchema(t||`public`).database.findTable(t,e);o||this.error(`Can't find table ${Lne(t)?`"${t}".`:``}"${e}"`),this.setFields(n,o)}generateId(){this.id=this.dbState.generateId(`endpointId`)}equals(e){return this.fields.length===e.fields.length?this.compareFields(e):!1}compareFields(e){let t=this.fields.map(e=>e.id).sort(),n=e.fields.map(e=>e.id).sort();for(let e=0;e<t.length;e+=1)if(t[e]!==n[e])return!1;return!0}export(){return{...this.shallowExport()}}exportParentIds(){return{refId:this.ref.id,fieldIds:this.fields.map(e=>e.id)}}shallowExport(){return{schemaName:this.schemaName,tableName:this.tableName,fieldNames:this.fieldNames,relation:this.relation}}setFields(e,t){let n=e&&e.length?[...e]:[];if(!n.length){let e=t.fields.find(e=>e.pk);if(e)n.push(e.name);else{let e=t.indexes.find(e=>e.pk);e?n=e.columns.map(e=>e.value):this.error(`Can't find primary or composite key in table ${ym(t.schema)?`"${t.schema.name}".`:``}"${t.name}"`)}}n.forEach(e=>{let n=t.findField(e);n||this.error(`Can't find field ${ym(t.schema)?`"${t.schema.name}".`:``}"${e}" in table "${t.name}"`),this.fields.push(n),n.pushEndpoint(this)})}normalize(e){e.endpoints[this.id]={id:this.id,...this.shallowExport(),...this.exportParentIds()}}},Xne=Yne;function Zne(e,t){return e[0].equals(t[0])&&e[1].equals(t[1])}var Qne=class extends bm{constructor({name:e,color:t,endpoints:n,onDelete:r,onUpdate:i,token:a,schema:o={},injectedPartial:s=null}={}){super(a),this.name=e,this.color=t,this.onDelete=r,this.onUpdate=i,this.endpoints=[],this.schema=o,this.injectedPartial=s,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 Xne({...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 Zne(this.endpoints,e.endpoints)||Zne(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,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))}},$ne=Qne,ere=class extends bm{constructor({token:e,name:t,expression:n,table:r,column:i=null,injectedPartial:a=null}={}){super(e),this.name=t,this.expression=n,this.table=r,this.column=i,this.injectedPartial=a,this.dbState=this.table.dbState,this.generateId()}generateId(){this.id=this.dbState.generateId(`checkId`)}export(){return{...this.shallowExport()}}exportParentIds(){return{tableId:this.table.id,columnId:this.column?.id,injectedPartialId:this.injectedPartial?.id}}shallowExport(){return{name:this.name,expression:this.expression}}normalize(e){e.checks[this.id]={id:this.id,...this.shallowExport(),...this.exportParentIds()}}},tre=ere;ud();var nre=class extends bm{constructor({name:e,type:t,unique:n,pk:r,token:i,not_null:a,note:o,dbdefault:s,increment:c,checks:l=[],table:u={},noteToken:d=null,injectedPartial:f=null,injectedToken:p=null}={}){super(i),e||this.error(`Field must have a name`),t||this.error(`Field must have a type`),this.name=e,this.type=t,this.unique=n,this.pk=r,this.not_null=a,this.note=o?Ga(o,`value`,o):null,this.noteToken=o?Ga(o,`token`,d):null,this.dbdefault=s,this.increment=c,this.checks=[],this.endpoints=[],this.table=u,this.injectedPartial=f,this.injectedToken=p,this.dbState=this.table.dbState,this.generateId(),this.bindType(),this.processChecks(l)}generateId(){this.id=this.dbState.generateId(`fieldId`)}bindType(){let e=this.type.type_name,t=this.type.schemaName||`public`;if(this.type.schemaName){let n=this.table.schema.database.findEnum(t,e);if(!n){this.type.type_name=`${t}.${e}`,this.type.originalTypeName=e;return}this._enum=n,n.pushField(this)}else{let n=this.table.schema.database.findEnum(t,e);if(!n)return;this._enum=n,n.pushField(this)}}pushEndpoint(e){this.endpoints.push(e)}export(){return{...this.shallowExport()}}exportParentIds(){return{tableId:this.table.id,enumId:this._enum?this._enum.id:null}}exportChildIds(){return{endpointIds:this.endpoints.map(e=>e.id)}}shallowExport(){return{name:this.name,type:this.type,unique:this.unique,pk:this.pk,not_null:this.not_null,note:this.note,dbdefault:this.dbdefault,increment:this.increment,injectedPartialId:this.injectedPartial?.id??null,checkIds:this.checks.map(e=>e.id)}}normalize(e){e.fields[this.id]={id:this.id,...this.shallowExport(),...this.exportChildIds(),...this.exportParentIds()},this.checks.forEach(t=>t.normalize(e))}processChecks(e){e.forEach(e=>{this.checks.push(new tre({...e,table:this.table,column:this}))})}},rre=nre,ire=class extends bm{constructor({type:e,value:t,index:n,token:r}){super(),this.type=e,this.value=t,this.index=n,this.token=r,this.dbState=this.index.dbState,this.generateId()}generateId(){this.id=this.dbState.generateId(`indexColumnId`)}export(){return{...this.shallowExport()}}exportParentIds(){return{indexId:this.index.id}}shallowExport(){return{type:this.type,value:this.value}}normalize(e){e.indexColumns[this.id]={id:this.id,...this.shallowExport(),...this.exportParentIds()}}},are=ire,ore=class extends bm{constructor({columns:e,type:t,unique:n,pk:r,token:i,name:a,note:o,table:s={},injectedPartial:c=null}={}){super(i),this.name=a,this.type=t,this.unique=n,this.note=o?o.value:null,this.noteToken=o?o.token:null,this.pk=r,this.columns=[],this.table=s,this.injectedPartial=c,this.dbState=this.table.dbState,this.generateId(),this.processIndexColumns(e)}generateId(){this.id=this.dbState.generateId(`indexId`)}processIndexColumns(e){e.forEach(e=>{this.pushIndexColumn(new are({...e,index:this}))})}pushIndexColumn(e){this.checkIndexColumn(e),this.columns.push(e)}checkIndexColumn(e){this.columns.some(t=>t.type===e.type&&t.value===e.value)&&e.error(`Index column ${e.value} existed`)}export(){return{...this.shallowExport(),...this.exportChild()}}exportChild(){return{columns:this.columns.map(e=>e.export())}}exportChildIds(){return{columnIds:this.columns.map(e=>e.id)}}exportParentIds(){return{tableId:this.table.id}}shallowExport(){return{name:this.name,type:this.type,unique:this.unique,pk:this.pk,note:this.note,injectedPartialId:this.injectedPartial?.id}}normalize(e){e.indexes[this.id]={id:this.id,...this.shallowExport(),...this.exportChildIds(),...this.exportParentIds()},this.columns.forEach(t=>t.normalize(e))}},sre=ore;ud();var cre=class extends bm{constructor({name:e,alias:t,note:n,fields:r=[],indexes:i=[],checks:a=[],schema:o={},token:s,headerColor:c,noteToken:l=null,partials:u=[]}={}){super(s),this.name=e,this.alias=t,this.note=n?Ga(n,`value`,n):null,this.noteToken=n?Ga(n,`token`,l):null,this.headerColor=c,this.fields=[],this.indexes=[],this.checks=[],this.schema=o,this.partials=u,this.records=[],this.dbState=this.schema.dbState,this.generateId(),this.processFields(r),this.processPartials(),this.checkFieldCount(),this.processIndexes(i),this.processChecks(a)}generateId(){this.id=this.dbState.generateId(`tableId`)}checkFieldCount(){this.fields.length===0&&this.error(`Table must have at least one field`)}processFields(e){e.forEach(e=>{this.pushField(new rre({...e,table:this}))})}pushField(e){this.checkField(e),this.fields.push(e)}checkField(e){this.fields.some(t=>t.name===e.name)&&e.error(`Field "${e.name}" existed in table ${ym(this.schema)?`"${this.schema.name}".`:``}"${this.name}"`)}processIndexes(e){e.forEach(e=>{this.pushIndex(new sre({...e,table:this}))})}pushIndex(e){this.checkIndex(e),this.indexes.push(e)}checkIndex(e){e.columns.forEach(t=>{t.type===`column`&&!this.findField(t.value)&&e.error(`Column "${t.value}" do not exist in table ${ym(this.schema)?`"${this.schema.name}".`:``}"${this.name}"`)})}processChecks(e){e.forEach(e=>{this.pushCheck(new tre({...e,table:this}))})}pushCheck(e){this.checks.push(e)}findField(e){return this.fields.find(t=>t.name===e)}checkSameId(e){return this.schema.checkSameId(e.schemaName||`public`)&&(this.name===e.name||this.alias===e.name||this.name===e.alias||this.alias&&this.alias===e.alias)}processPartials(){if(!this.partials?.length)return;let e=new Set(this.fields.map(e=>e.name)),t=new Set;Xu(this.note)||t.add(`note`),Xu(this.headerColor)||t.add(`headerColor`);let n=this.partials.sort((e,t)=>t.order-e.order);n.toReversed().forEach(e=>{this.fields.splice(e.order,0,`dummy`)}),n.forEach(n=>{let r=this.schema.database.findTablePartial(n.name);if(r||this.error(`Table partial ${n.name} not found`,n.token),n.id=r.id,r.fields){let t=r.fields.filter(t=>!e.has(t.name)).map(t=>(e.add(t.name),t.inline_refs&&t.inline_refs.forEach(e=>{let n={token:t.token,endpoints:[{tableName:this.name,schemaName:this.schema?.name,fieldNames:[t.name],relation:[`-`,`<`].includes(e.relation)?`1`:`*`,token:t.token},{tableName:e.tableName,schemaName:e.schemaName,fieldNames:e.fieldNames,relation:[`-`,`>`].includes(e.relation)?`1`:`*`,token:e.token}],injectedPartial:r};this.schema.database.injectedRawRefs.push(n)}),new rre({...t,noteToken:null,table:this,injectedPartial:r,injectedToken:n.token})));this.fields.splice(n.order,1,...t)}else this.fields.splice(n.order,1);!t.has(`note`)&&!Xu(r.note)&&(this.noteToken=null,this.note=r.note,t.add(`note`)),!t.has(`headerColor`)&&!Xu(r.headerColor)&&(this.headerColor=r.headerColor,t.add(`headerColor`)),r.indexes.forEach(e=>{this.indexes.push(new sre({...e,table:this,injectedPartial:r}))}),r.checks.forEach(e=>{this.checks.push(new tre({...e,name:e.name&&`${this.name}.${e.name}`,table:this,injectedPartial:r}))})})}export(){return{...this.shallowExport(),...this.exportChild()}}exportChild(){return{fields:this.fields.map(e=>e.export()),indexes:this.indexes.map(e=>e.export())}}exportChildIds(){return{fieldIds:this.fields.map(e=>e.id),indexIds:this.indexes.map(e=>e.id),checkIds:this.checks.map(e=>e.id)}}exportParentIds(){return{schemaId:this.schema.id,groupId:this.group?this.group.id:null}}shallowExport(){return{name:this.name,alias:this.alias,note:this.note,headerColor:this.headerColor,partials:this.partials,recordIds:this.records.map(e=>e.id)}}normalize(e){e.tables[this.id]={id:this.id,...this.shallowExport(),...this.exportChildIds(),...this.exportParentIds()},this.fields.forEach(t=>t.normalize(e)),this.indexes.forEach(t=>t.normalize(e)),this.checks.forEach(t=>t.normalize(e))}},lre=cre;ud();var ure=class extends bm{constructor({name:e,token:t,tables:n=[],schema:r={},note:i,color:a,noteToken:o=null}){super(t),this.name=e,this.tables=[],this.schema=r,this.dbState=this.schema.dbState,this.note=i?Ga(i,`value`,i):null,this.noteToken=i?Ga(i,`token`,o):null,this.color=a,this.generateId(),this.processTables(n)}generateId(){this.id=this.dbState.generateId(`tableGroupId`)}processTables(e){e.forEach(e=>{let t=this.schema.database.findTable(e.schemaName,e.name);t||this.error(`Table ${e.schemaName?`"${e.schemaName}".`:``}${e.name} don't exist`),this.pushTable(t)})}pushTable(e){this.checkTable(e),this.tables.push(e),e.group=this}checkTable(e){this.tables.some(t=>t.id===e.id)&&this.error(`Table ${ym(e.schema)?`"${e.schema.name}".`:``}.${e.name} is already in the group`),e.group&&this.error(`Table ${ym(e.schema)?`"${e.schema.name}".`:``}.${e.name} is already in group ${ym(e.group.schema)?`"${e.group.schema.name}".`:``}${e.group.name}`)}export(){return{...this.shallowExport(),...this.exportChild()}}exportChild(){return{tables:this.tables.map(e=>({tableName:e.name,schemaName:e.schema.name}))}}exportChildIds(){return{tableIds:this.tables.map(e=>e.id)}}exportParentIds(){return{schemaId:this.schema.id}}shallowExport(){return{name:this.name,note:this.note,color:this.color}}normalize(e){e.tableGroups[this.id]={id:this.id,...this.shallowExport(),...this.exportChildIds(),...this.exportParentIds()}}},dre=ure;ud();var fre=class extends bm{constructor({name:e,alias:t,note:n,tables:r=[],refs:i=[],enums:a=[],tableGroups:o=[],token:s,database:c={},noteToken:l=null}={}){super(s),this.tables=[],this.enums=[],this.tableGroups=[],this.refs=[],this.name=e,this.note=n?Ga(n,`value`,n):null,this.noteToken=n?Ga(n,`token`,l):null,this.alias=t,this.database=c,this.dbState=this.database.dbState,this.generateId(),this.processEnums(a),this.processTables(r),this.processRefs(i),this.processTableGroups(o)}generateId(){this.id=this.dbState.generateId(`schemaId`)}processTables(e){e.forEach(e=>{this.pushTable(new lre({...e,schema:this}))})}pushTable(e){this.checkTable(e),this.tables.push(e)}checkTable(e){this.tables.some(t=>t.name===e.name)&&e.error(`Table ${ym(this)?`"${this.name}".`:``}"${e.name}" existed`)}findTable(e){return this.tables.find(t=>t.name===e)}processEnums(e){e.forEach(e=>{this.pushEnum(new Jne({...e,schema:this}))})}pushEnum(e){this.checkEnum(e),this.enums.push(e)}checkEnum(e){this.enums.some(t=>t.name===e.name)&&e.error(`Enum ${ym(this)?`"${this.name}".`:``}"${e.name}" existed`)}processRefs(e){e.forEach(e=>{this.pushRef(new $ne({...e,schema:this}))})}pushRef(e){this.checkRef(e),this.refs.push(e)}checkRef(e){if(this.refs.some(t=>t.equals(e))){let t=e.endpoints[0],n=t.fieldNames.map(JSON.stringify).join(`, `),r=e.endpoints[1],i=r.fieldNames.map(JSON.stringify).join(`, `),a=`"${t.schemaName?`${t.schemaName}"."`:``}${t.tableName}"(${n})`,o=`"${r.schemaName?`${r.schemaName}"."`:``}${r.tableName}"(${i})`;e.error(`Reference with the same endpoints already exists: ${a} references ${o}`)}}processTableGroups(e){e.forEach(e=>{this.pushTableGroup(new dre({...e,schema:this}))})}pushTableGroup(e){this.checkTableGroup(e),this.tableGroups.push(e)}checkTableGroup(e){this.tableGroups.some(t=>t.name===e.name)&&e.error(`Table Group ${ym(this)?`"${this.name}".`:``}"${e.name}" existed`)}checkSameId(e){return this.name===e.name||this.alias===e.name||this.name===e.alias||this.alias&&this.alias===e.alias}export(){return{...this.shallowExport(),...this.exportChild()}}exportChild(){return{tables:this.tables.map(e=>e.export()),enums:this.enums.map(e=>e.export()),tableGroups:this.tableGroups.map(e=>e.export()),refs:this.refs.map(e=>e.export())}}exportChildIds(){return{tableIds:this.tables.map(e=>e.id),enumIds:this.enums.map(e=>e.id),tableGroupIds:this.tableGroups.map(e=>e.id),refIds:this.refs.map(e=>e.id)}}exportParentIds(){return{databaseId:this.database.id}}shallowExport(){return{name:this.name,note:this.note,alias:this.alias}}normalize(e){e.schemas[this.id]={id:this.id,...this.shallowExport(),...this.exportChildIds(),...this.exportParentIds()},this.tables.forEach(t=>t.normalize(e)),this.enums.forEach(t=>t.normalize(e)),this.tableGroups.forEach(t=>t.normalize(e)),this.refs.forEach(t=>t.normalize(e))}},pre=fre,mre=class extends bm{constructor({name:e,content:t,headerColor:n,token:r,database:i={}}={}){super(r),this.name=e,this.content=t,this.headerColor=n,this.database=i,this.dbState=this.database.dbState,this.generateId()}generateId(){this.id=this.dbState.generateId(`noteId`)}export(){return{name:this.name,content:this.content,headerColor:this.headerColor}}normalize(e){e.notes[this.id]={id:this.id,...this.export()}}},hre=mre;ud();var gre=class extends bm{constructor({name:e,note:t,fields:n=[],indexes:r=[],checks:i=[],token:a,headerColor:o,noteToken:s=null,dbState:c}={}){super(a),this.name=e,this.note=t?Ga(t,`value`,t):null,this.noteToken=t?Ga(t,`token`,s):null,this.headerColor=o,this.fields=n,this.indexes=r,this.checks=i,this.dbState=c,this.generateId()}generateId(){this.id=this.dbState.generateId(`tablePartialId`)}export(){return{...this.shallowExport()}}shallowExport(){return{name:this.name,note:this.note,headerColor:this.headerColor,fields:this.fields,indexes:this.indexes}}normalize(e){e.tablePartials[this.id]={id:this.id,...this.shallowExport()}}},_re=gre;ud();var vre=class extends bm{constructor({schemas:e=[],tables:t=[],notes:n=[],enums:r=[],refs:i=[],tableGroups:a=[],project:o={},aliases:s=[],records:c=[],tablePartials:l=[],diagramViews:u=[]}){super(),this.dbState=new Hne,this.generateId(),this.hasDefaultSchema=!1,this.schemas=[],this.notes=[],this.note=o.note?Ga(o,`note.value`,o.note):null,this.noteToken=o.note?Ga(o,`note.token`,o.noteToken):null,this.databaseType=o.database_type,this.name=o.name,this.token=o.token,this.aliases=s,this.records=[],this.tablePartials=[],this.diagramViews=u,this.injectedRawRefs=[],this.processNotes(n),this.processRecords(c),this.processTablePartials(l),this.processSchemas(e),this.processSchemaElements(r,Ene),this.processSchemaElements(t,wne),this.linkRecordsToTables(),this.processSchemaElements(n,`note`),this.processSchemaElements(i,`ref`),this.processSchemaElements(a,One),this.injectedRawRefs.forEach(e=>{let t=this.findOrCreateSchema(fm),n=new $ne({...e,schema:t});t.refs.some(e=>e.equals(n))||t.pushRef(n)})}generateId(){this.id=this.dbState.generateId(`dbId`)}processNotes(e){e.forEach(e=>{this.pushNote(new hre({...e,database:this}))})}processRecords(e){e.forEach(({schemaName:e,tableName:t,columns:n,values:r,token:i})=>{this.records.push({id:this.dbState.generateId(`recordId`),schemaName:e,tableName:t,columns:n,values:r,token:i})})}processTablePartials(e){e.forEach(e=>{this.tablePartials.push(new _re({...e,dbState:this.dbState}))})}pushNote(e){this.checkNote(e),this.notes.push(e)}checkNote(e){this.notes.some(t=>t.name===e.name)&&e.error(`Notes ${e.name} existed`)}processSchemas(e){e.forEach(e=>{this.pushSchema(new pre({...e,database:this}))})}pushSchema(e){this.checkSchema(e),this.schemas.push(e)}checkSchema(e){this.schemas.some(t=>t.name===e.name)&&e.error(`Schemas ${e.name} existed`)}processSchemaElements(e,t){let n;e.forEach(e=>{switch(e.schemaName?(n=this.findOrCreateSchema(e.schemaName),e.schemaName):n=this.findOrCreateSchema(fm),t){case wne:n.pushTable(new lre({...e,schema:n}));break;case Ene:n.pushEnum(new Jne({...e,schema:n}));break;case One:n.pushTableGroup(new dre({...e,schema:n}));break;case`ref`:n.pushRef(new $ne({...e,schema:n}));break;default:break}})}linkRecordsToTables(){let e={};this.schemas.forEach(t=>{e[t.name]={},t.tables.forEach(n=>{e[t.name][n.name]=n})}),this.records.forEach(t=>{let n=e[t.schemaName??`public`]?.[t.tableName];n&&(t.tableId=n.id,n.records.push(t))})}findOrCreateSchema(e){let t=this.schemas.find(t=>t.name===e||t.alias===e);return t||(t=new pre({name:e,note:{value:e===`public`?`Default ${Xo(fm)} Schema`:null},database:this}),this.pushSchema(t)),t}findTableAlias(e){let t=this.aliases.find(t=>t.name===e);if(!t||t.kind!==`table`)return null;let n=t.value.schemaName||`public`,r=this.schemas.find(e=>e.name===n);if(!r)return null;let{tableName:i}=t.value;return r.tables.find(e=>e.name===i)}findTable(e,t){let n=null;if(!e&&(n=this.findTableAlias(t),n))return n;let r=this.findOrCreateSchema(e||`public`);return r||this.error(`Schema ${e||`public`} don't exist`),r.findTable(t)}findEnum(e,t){let n=this.schemas.find(t=>t.name===e||t.alias===e);return n?n.enums.find(e=>e.name===t):null}findTablePartial(e){return this.tablePartials.find(t=>t.name===e)}export(){return{...this.exportChild()}}shallowExport(){return{hasDefaultSchema:this.hasDefaultSchema,note:this.note,databaseType:this.databaseType,name:this.name}}exportChild(){return{schemas:this.schemas.map(e=>e.export()),notes:this.notes.map(e=>e.export()),records:this.records.map(e=>({...e}))}}exportChildIds(){return{schemaIds:this.schemas.map(e=>e.id),noteIds:this.notes.map(e=>e.id)}}normalize(){let e={database:{[this.id]:{id:this.id,...this.shallowExport(),...this.exportChildIds()}},schemas:{},notes:{},refs:{},enums:{},tableGroups:{},tables:{},endpoints:{},enumValues:{},indexes:{},indexColumns:{},checks:{},fields:{},records:{},tablePartials:{}};return this.schemas.forEach(t=>t.normalize(e)),this.notes.forEach(t=>t.normalize(e)),this.records.forEach(t=>{e.records[t.id]={...t}}),this.tablePartials.forEach(t=>t.normalize(e)),e}},xm=vre,yre=class{static export(e,t){let{isNormalized:n}=t;return!n&&e instanceof xm?JSON.stringify(e.export(),null,2):JSON.stringify(e,null,2)}},bre=yre;ud();var xre=class e{static exportRecords(e){let t=Object.values(e.records||{});return qu(t)?[]:t.map(e=>{let{schemaName:t,tableName:n,columns:r,values:i}=e,a=t?`[${t}].[${n}]`:`[${n}]`,o=r.length>0?`([${r.join(`], [`)}])`:``,s=e=>e.value===null?`NULL`:e.type===`expression`||(0,f.isNumericType)(e.type)?e.value:(0,f.isBooleanType)(e.type)?String(e.value).toUpperCase()===`TRUE`?`1`:`0`:(0,f.isStringType)(e.type)||(0,f.isDateTimeType)(e.type)?`'${e.value.replace(/'/g,`''`)}'`:(0,f.isBinaryType)(e.type)?`0x${e.value}`:`CAST('${e.value.replace(/'/g,`''`)}' AS ${e.type})`;return`INSERT INTO ${a} ${o}\nVALUES\n ${i.map(e=>`(${e.map(s).join(`, `)})`).join(`,
31
31
  `)};\nGO`})}static getFieldLines(e,t){return t.tables[e].fieldIds.map(e=>{let n=t.fields[e],r=``;if(n.enumId){let e=t.enums[n.enumId];r=`[${n.name}] nvarchar(255) NOT NULL CHECK ([${n.name}] IN (`;let i=e.valueIds.map(e=>`'${t.enumValues[e].name}'`);r+=`${i.join(`, `)}))`}else r=`[${n.name}] ${n.type.type_name===`varchar`?`nvarchar(255)`:n.type.type_name}`;if(n.unique&&(r+=` UNIQUE`),n.pk&&(r+=` PRIMARY KEY`),n.not_null&&(r+=` NOT NULL`),n.increment&&(r+=` IDENTITY(1, 1)`),n.checkIds&&n.checkIds.length>0)if(n.checkIds.length===1){let e=t.checks[n.checkIds[0]];e.name&&(r+=` CONSTRAINT [${e.name}]`),r+=` CHECK (${e.expression})`}else{let e=n.checkIds.map(e=>`(${t.checks[e].expression})`);r+=` CHECK (${e.join(` AND `)})`}return n.dbdefault&&(n.dbdefault.type===`boolean`&&(n.dbdefault.value===null||typeof n.dbdefault.value==`string`&&n.dbdefault.value.toLowerCase()===`null`)||(n.dbdefault.type===`expression`?r+=` DEFAULT (${n.dbdefault.value})`:n.dbdefault.type===`string`?r+=` DEFAULT '${n.dbdefault.value}'`:r+=` DEFAULT (${n.dbdefault.value})`)),r})}static getCompositePKs(e,t){let n=t.tables[e];return(n.indexIds?n.indexIds.filter(e=>t.indexes[e].pk):[]).map(e=>{let n=t.indexes[e],r=`PRIMARY KEY`,i=[];return n.columnIds.forEach(e=>{let n=t.indexColumns[e],r=``;r=n.type===`expression`?`(${n.value})`:`[${n.value}]`,i.push(r)}),r+=` (${i.join(`, `)})`,r})}static getCheckLines(e,t){let n=t.tables[e];return!n.checkIds||n.checkIds.length===0?[]:n.checkIds.map(e=>{let n=t.checks[e],r=``;return n.name&&(r=`CONSTRAINT [${n.name}] `),r+=`CHECK (${n.expression})`,r})}static getTableContentArr(t,n){return t.map(t=>({tableId:t,fieldContents:e.getFieldLines(t,n),checkContents:e.getCheckLines(t,n),compositePKs:e.getCompositePKs(t,n)}))}static exportTables(t,n){return e.getTableContentArr(t,n).map(e=>{let t=[...e.fieldContents,...e.checkContents,...e.compositePKs],r=n.tables[e.tableId],i=n.schemas[r.schemaId];return`CREATE TABLE ${mm(i,n)?`[${i.name}].`:``}[${r.name}] (\n${t.map(e=>` ${e}`).join(`,
32
32
  `)}\n)\nGO\n`})}static buildTableManyToMany(e,t,n,r,i){let a=`CREATE TABLE ${mm(r,i)?`[${r.name}].`:``}[${n}] (\n`,o=[...e.keys()].join(`], [`),s=[...t.keys()].join(`], [`);return e.forEach((e,t)=>{a+=` [${t}] ${e},\n`}),t.forEach((e,t)=>{a+=` [${t}] ${e},\n`}),a+=` PRIMARY KEY ([${o}], [${s}])\n`,a+=`);
33
33
  GO
@@ -121,4 +121,4 @@ function print() { __p += __j.call(arguments, '') }
121
121
  `,!1),pt=`
122
122
  `,mt=Dt(`
123
123
  `,!1),ht=At(`whitespace`),gt=/^[ \t\r\n\r]/,_t=Ot([` `,` `,`\r`,`
124
- `,`\r`],!1,!1),vt=` `,yt=Dt(` `,!1),S=0,bt=0,xt=[{line:1,column:1}],St=0,Ct=[],wt=0,Tt;if(`startRule`in t){if(!(t.startRule in i))throw Error(`Can't start parsing from rule "`+t.startRule+`".`);a=i[t.startRule]}function Et(e,t){throw t=t===void 0?Mt(bt,S):t,Pt(e,t)}function Dt(e,t){return{type:`literal`,text:e,ignoreCase:t}}function Ot(e,t,n){return{type:`class`,parts:e,inverted:t,ignoreCase:n}}function kt(){return{type:`end`}}function At(e){return{type:`other`,description:e}}function jt(t){var n=xt[t],r;if(n)return n;for(r=t-1;!xt[r];)r--;for(n=xt[r],n={line:n.line,column:n.column};r<t;)e.charCodeAt(r)===10?(n.line++,n.column=1):n.column++,r++;return xt[t]=n,n}function Mt(e,t){var n=jt(e),r=jt(t);return{start:{offset:e,line:n.line,column:n.column},end:{offset:t,line:r.line,column:r.column}}}function Nt(e){S<St||(S>St&&(St=S,Ct=[]),Ct.push(e))}function Pt(e,t){return new r(e,null,null,t)}function Ft(e,t,n){return new r(r.buildMessage(e,t),e,t,n)}function It(){for(var e=S,t=[],r=Lt();r!==n;)t.push(r),r=Lt();return t!==n&&(bt=e,t=o()),e=t,e}function Lt(){for(var e=S,t=[],r=Dn();r!==n;)t.push(r),r=Dn();return t===n?(S=e,e=n):(r=Rt(),r===n?(S=e,e=n):(t=[t,r],e=t)),e===n&&(e=vn(),e===n&&(e=_n(),e===n&&(e=Cn()))),e}function Rt(){var e=S,t=Ht();return t!==n&&(bt=e,t=s(t)),e=t,e===n&&(e=S,t=Bt(),t!==n&&(bt=e,t=c(t)),e=t,e===n&&(e=zt(),e===n&&(e=ln()))),e}function zt(){for(var t=S,r=[],i=On(),a,o,s,c,f,p,m;i!==n;)r.push(i),i=On();if(r!==n)if(i=rn(),i!==n){for(a=[],o=On();o!==n;)a.push(o),o=On();if(a!==n)if(o=pn(),o===n&&(o=un()),o!==n)if(e.charCodeAt(S)===44?(s=l,S++):(s=n,wt===0&&Nt(u)),s!==n){for(c=[],f=On();f!==n;)c.push(f),f=On();if(c!==n)if(f=un(),f!==n){for(p=[],m=Zt();m!==n;)p.push(m),m=Zt();p===n?(S=t,t=n):(bt=t,r=d(o,f,p),t=r)}else S=t,t=n;else S=t,t=n}else S=t,t=n;else S=t,t=n;else S=t,t=n}else S=t,t=n;else S=t,t=n;return t}function Bt(){for(var t=S,r=[],i=On(),a,o,s,c,d,p,m;i!==n;)r.push(i),i=On();if(r!==n)if(i=an(),i!==n){for(a=[],o=On();o!==n;)a.push(o),o=On();if(a!==n)if(o=un(),o!==n)if(e.charCodeAt(S)===44?(s=l,S++):(s=n,wt===0&&Nt(u)),s!==n){for(c=[],d=On();d!==n;)c.push(d),d=On();if(c!==n)if(d=un(),d!==n){for(p=[],m=Vt();m!==n;)p.push(m),m=Vt();p===n?(S=t,t=n):(bt=t,r=f(o,d,p),t=r)}else S=t,t=n;else S=t,t=n}else S=t,t=n;else S=t,t=n;else S=t,t=n}else S=t,t=n;else S=t,t=n;return t}function Vt(){var t=S,r,i,a,o,s,c;if(e.charCodeAt(S)===44?(r=l,S++):(r=n,wt===0&&Nt(u)),r!==n){for(i=[],a=On();a!==n;)i.push(a),a=On();if(i!==n)if(a=on(),a!==n)if(e.charCodeAt(S)===58?(o=p,S++):(o=n,wt===0&&Nt(m)),o!==n){for(s=[],c=On();c!==n;)s.push(c),c=On();s===n?(S=t,t=n):(c=un(),c===n?(S=t,t=n):(bt=t,r=h(c),t=r))}else S=t,t=n;else S=t,t=n;else S=t,t=n}else S=t,t=n;if(t===n){if(t=S,e.charCodeAt(S)===44?(r=l,S++):(r=n,wt===0&&Nt(u)),r!==n){for(i=[],a=On();a!==n;)i.push(a),a=On();if(i!==n)if(a=sn(),a!==n)if(e.charCodeAt(S)===58?(o=p,S++):(o=n,wt===0&&Nt(m)),o!==n){for(s=[],c=On();c!==n;)s.push(c),c=On();s===n?(S=t,t=n):(c=un(),c===n?(S=t,t=n):(bt=t,r=g(c),t=r))}else S=t,t=n;else S=t,t=n;else S=t,t=n}else S=t,t=n;if(t===n)if(t=S,e.charCodeAt(S)===44?(r=l,S++):(r=n,wt===0&&Nt(u)),r!==n){for(i=[],a=On();a!==n;)i.push(a),a=On();if(i!==n)if(a=Qt(),a!==n)if(e.charCodeAt(S)===58?(o=p,S++):(o=n,wt===0&&Nt(m)),o!==n){for(s=[],c=On();c!==n;)s.push(c),c=On();s===n?(S=t,t=n):(c=pn(),c===n?(S=t,t=n):(bt=t,r=_(a,c),t=r))}else S=t,t=n;else S=t,t=n;else S=t,t=n}else S=t,t=n}return t}function Ht(){var e=S,t=$t(),r,i,a,o,s,c;if(t!==n){for(r=[],i=On();i!==n;)r.push(i),i=On();r===n?(S=e,e=n):(i=un(),i===n?(S=e,e=n):(a=yn(),a===n?(S=e,e=n):(o=wn(),o===n?(S=e,e=n):(s=Ut(),s===n?(S=e,e=n):(c=_n(),c===n?(S=e,e=n):(bt=e,t=v(i,s),e=t))))))}else S=e,e=n;return e}function Ut(){for(var e=S,t=[],r=Wt();r!==n;)t.push(r),r=Wt();return t!==n&&(bt=e,t=y(t)),e=t,e}function Wt(){for(var e=S,t=[],r=Dn(),i,a;r!==n;)t.push(r),r=Dn();if(t!==n)if(r=Gt(),r!==n){for(i=[],a=bn();a!==n;)i.push(a),a=bn();i===n?(S=e,e=n):(a=wn(),a===n?(S=e,e=n):(bt=e,t=b(r),e=t))}else S=e,e=n;else S=e,e=n;return e}function Gt(){var e=Kt(),t;return e===n&&(e=S,t=qt(),t!==n&&(bt=e,t=x(t)),e=t,e===n&&(e=S,t=Jt(),t!==n&&(bt=e,t=ee(t)),e=t,e===n&&(e=S,t=Yt(),t!==n&&(bt=e,t=te(t)),e=t))),e}function Kt(){var e=S,t=en(),r,i;if(t!==n){if(r=[],i=On(),i!==n)for(;i!==n;)r.push(i),i=On();else r=n;r===n?(S=e,e=n):(i=yn(),i===n?(S=e,e=n):(t=[t,r,i],e=t))}else S=e,e=n;return e}function qt(){var e=S,t=tn(),r,i;if(t!==n){if(r=[],i=On(),i!==n)for(;i!==n;)r.push(i),i=On();else r=n;r===n?(S=e,e=n):(i=Xt(),i===n?(S=e,e=n):(bt=e,t=ne(i),e=t))}else S=e,e=n;return e}function Jt(){var e=S,t=nn(),r,i,a,o;if(t!==n){if(r=[],i=On(),i!==n)for(;i!==n;)r.push(i),i=On();else r=n;if(r!==n)if(i=un(),i!==n){for(a=[],o=Zt();o!==n;)a.push(o),o=Zt();a===n?(S=e,e=n):(bt=e,t=re(i,a),e=t)}else S=e,e=n;else S=e,e=n}else S=e,e=n;return e}function Yt(){var e=S,t=hn(),r,i;if(t!==n){if(r=[],i=On(),i!==n)for(;i!==n;)r.push(i),i=On();else r=n;r===n?(S=e,e=n):(i=un(),i===n?(S=e,e=n):(bt=e,t=ie(t,i),e=t))}else S=e,e=n;return e}function Xt(){var t=S,r,i;return e.charCodeAt(S)===58?(r=p,S++):(r=n,wt===0&&Nt(m)),r===n?(S=t,t=n):(i=mn(),i===n?(S=t,t=n):(bt=t,r=ae(i),t=r)),t===n&&(t=S,r=un(),r!==n&&(bt=t,r=ae(r)),t=r),t}function Zt(){var t=S,r,i,a,o,s,c;if(e.charCodeAt(S)===44?(r=l,S++):(r=n,wt===0&&Nt(u)),r!==n){for(i=[],a=On();a!==n;)i.push(a),a=On();if(i!==n)if(e.substr(S,4).toLowerCase()===oe?(a=e.substr(S,4),S+=4):(a=n,wt===0&&Nt(se)),a!==n)if(e.charCodeAt(S)===58?(o=p,S++):(o=n,wt===0&&Nt(m)),o!==n){for(s=[],c=On();c!==n;)s.push(c),c=On();s===n?(S=t,t=n):(c=un(),c===n?(S=t,t=n):(bt=t,r=ce(c),t=r))}else S=t,t=n;else S=t,t=n;else S=t,t=n}else S=t,t=n;return t}function Qt(){var t;return e.substr(S,9).toLowerCase()===le?(t=e.substr(S,9),S+=9):(t=n,wt===0&&Nt(ue)),t===n&&(e.substr(S,9).toLowerCase()===de?(t=e.substr(S,9),S+=9):(t=n,wt===0&&Nt(fe))),t}function $t(){var t;return wt++,e.substr(S,12).toLowerCase()===me?(t=e.substr(S,12),S+=12):(t=n,wt===0&&Nt(he)),wt--,t===n&&wt===0&&Nt(pe),t}function en(){var t,r,i;return wt++,t=S,r=gn(),r===n?(S=t,t=n):(e.substr(S,6)===_e?(i=_e,S+=6):(i=n,wt===0&&Nt(ve)),i===n?(S=t,t=n):(r=[r,i],t=r)),wt--,t===n&&(r=n,wt===0&&Nt(ge)),t}function tn(){var t,r,i;return wt++,t=S,r=gn(),r===n?(S=t,t=n):(e.substr(S,11)===be?(i=be,S+=11):(i=n,wt===0&&Nt(xe)),i===n?(S=t,t=n):(r=[r,i],t=r)),wt--,t===n&&(r=n,wt===0&&Nt(ye)),t}function nn(){var t,r,i;return wt++,t=S,r=gn(),r===n?(S=t,t=n):(e.substr(S,17)===Ce?(i=Ce,S+=17):(i=n,wt===0&&Nt(we)),i===n?(S=t,t=n):(r=[r,i],t=r)),wt--,t===n&&(r=n,wt===0&&Nt(Se)),t}function rn(){var t;return wt++,e.substr(S,20).toLowerCase()===Ee?(t=e.substr(S,20),S+=20):(t=n,wt===0&&Nt(De)),wt--,t===n&&wt===0&&Nt(Te),t}function an(){var t;return wt++,e.substr(S,15).toLowerCase()===ke?(t=e.substr(S,15),S+=15):(t=n,wt===0&&Nt(Ae)),wt--,t===n&&wt===0&&Nt(Oe),t}function on(){var t;return wt++,e.substr(S,6)===Me?(t=Me,S+=6):(t=n,wt===0&&Nt(Ne)),wt--,t===n&&wt===0&&Nt(je),t}function sn(){var t;return wt++,e.substr(S,11)===Fe?(t=Fe,S+=11):(t=n,wt===0&&Nt(Ie)),wt--,t===n&&wt===0&&Nt(Pe),t}function cn(){var t;return e.substr(S,3)===Le?(t=Le,S+=3):(t=n,wt===0&&Nt(Re)),t}function ln(){var e=S,t=mn(),r,i;return t===n?(S=e,e=n):(r=yn(),r===n?(S=e,e=n):(i=wn(),i===n&&(i=null),i===n?(S=e,e=n):(t=[t,r,i],e=t))),e}function un(){var e=dn();return e===n&&(e=fn()),e}function dn(){var t=S,r=Sn(),i,a;if(r!==n){for(i=[],ze.test(e.charAt(S))?(a=e.charAt(S),S++):(a=n,wt===0&&Nt(Be));a!==n;)i.push(a),ze.test(e.charAt(S))?(a=e.charAt(S),S++):(a=n,wt===0&&Nt(Be));i===n?(S=t,t=n):(a=Sn(),a===n?(S=t,t=n):(bt=t,r=Ve(i),t=r))}else S=t,t=n;return t}function fn(){var t=S,r=xn(),i,a;if(r!==n){for(i=[],He.test(e.charAt(S))?(a=e.charAt(S),S++):(a=n,wt===0&&Nt(Ue));a!==n;)i.push(a),He.test(e.charAt(S))?(a=e.charAt(S),S++):(a=n,wt===0&&Nt(Ue));i===n?(S=t,t=n):(a=xn(),a===n?(S=t,t=n):(bt=t,r=Ve(i),t=r))}else S=t,t=n;return t}function pn(){var t=S,r,i,a;if(e.charCodeAt(S)===58?(r=p,S++):(r=n,wt===0&&Nt(m)),r!==n){for(i=[],a=gn();a!==n;)i.push(a),a=gn();i===n?(S=t,t=n):(bt=t,r=Ve(i),t=r)}else S=t,t=n;return t}function mn(){var e=S,t=[],r=gn();if(r!==n)for(;r!==n;)t.push(r),r=gn();else t=n;return t!==n&&(bt=e,t=Ve(t)),e=t,e}function hn(){var t=S,r=gn(),i,a,o;if(r!==n)if(e.charCodeAt(S)===46?(i=We,S++):(i=n,wt===0&&Nt(Ge)),i!==n){if(a=[],o=gn(),o!==n)for(;o!==n;)a.push(o),o=gn();else a=n;a===n?(S=t,t=n):(bt=t,r=Ve(a),t=r)}else S=t,t=n;else S=t,t=n;return t}function gn(){var t;return wt++,qe.test(e.charAt(S))?(t=e.charAt(S),S++):(t=n,wt===0&&Nt(Je)),wt--,t===n&&wt===0&&Nt(Ke),t}function _n(){for(var e=S,t=[],r=Dn(),i;r!==n;)t.push(r),r=Dn();return t===n?(S=e,e=n):(r=cn(),r===n?(S=e,e=n):(i=wn(),i===n&&(i=null),i===n?(S=e,e=n):(t=[t,r,i],e=t))),e}function vn(){var t,r,i,a,o;for(wt++,t=S,r=[],i=Dn();i!==n;)r.push(i),i=Dn();return r===n?(S=t,t=n):(e.charCodeAt(S)===35?(i=Xe,S++):(i=n,wt===0&&Nt(Ze)),i===n?(S=t,t=n):(a=yn(),a===n?(S=t,t=n):(o=wn(),o===n&&(o=null),o===n?(S=t,t=n):(r=[r,i,a,o],t=r)))),wt--,t===n&&(r=n,wt===0&&Nt(Ye)),t}function yn(){var t,r;for(wt++,t=[],$e.test(e.charAt(S))?(r=e.charAt(S),S++):(r=n,wt===0&&Nt(et));r!==n;)t.push(r),$e.test(e.charAt(S))?(r=e.charAt(S),S++):(r=n,wt===0&&Nt(et));return wt--,t===n&&(r=n,wt===0&&Nt(Qe)),t}function bn(){var t;return $e.test(e.charAt(S))?(t=e.charAt(S),S++):(t=n,wt===0&&Nt(et)),t}function xn(){var t;return e.charCodeAt(S)===39?(t=tt,S++):(t=n,wt===0&&Nt(nt)),t}function Sn(){var t;return e.charCodeAt(S)===34?(t=rt,S++):(t=n,wt===0&&Nt(it)),t}function Cn(){var e=[],t=Tn();if(t===n&&(t=Dn()),t!==n)for(;t!==n;)e.push(t),t=Tn(),t===n&&(t=Dn());else e=n;return e}function wn(){for(var e=S,t=[],r=On();r!==n;)t.push(r),r=On();return t===n?(S=e,e=n):(r=En(),r===n?(S=e,e=n):(t=[t,r],e=t)),e}function Tn(){var t,r,i;return wt++,t=S,e.substr(S,2)===ot?(r=ot,S+=2):(r=n,wt===0&&Nt(st)),r===n?(S=t,t=n):(ct.test(e.charAt(S))?(i=e.charAt(S),S++):(i=n,wt===0&&Nt(lt)),i===n&&(i=null),i===n?(S=t,t=n):(r=[r,i],t=r)),wt--,t===n&&(r=n,wt===0&&Nt(at)),t}function En(){var t;return wt++,e.substr(S,2)===dt?(t=dt,S+=2):(t=n,wt===0&&Nt(ft)),t===n&&(e.charCodeAt(S)===10?(t=pt,S++):(t=n,wt===0&&Nt(mt))),wt--,t===n&&wt===0&&Nt(ut),t}function Dn(){var t;return wt++,gt.test(e.charAt(S))?(t=e.charAt(S),S++):(t=n,wt===0&&Nt(_t)),wt--,t===n&&wt===0&&Nt(ht),t}function On(){var t;return e.charCodeAt(S)===32?(t=vt,S++):(t=n,wt===0&&Nt(yt)),t}var kn=$7(),An=Q7();let jn={tables:[],refs:[]};function Mn(e){jn.tables.find(t=>t.name==e.name)?Et(`Duplicated table name`):(e.fields.find(e=>e.name===`id`)||e.fields.unshift({name:`id`,type:{type_name:`varchar`}}),jn.tables.push(e))}function Nn(e,t,n){let r=jn.tables.find(t=>t.name===e);r||Et("Table ${tableName} not found"),r.checks||=[];let i={expression:t};n&&(i.name=n),r.checks.push(i)}function Pn(e=[],t=[]){let n=t.find(e=>e.name===`primary_key`);return n?e.find(e=>e.name===n.value)?e.map(({name:e,type:t})=>({name:e,type:t,PK:n.value===field.name})):[{name:n.value,type:{type_name:`varchar`},PK:!0}].concat(e):e}function Fn(e,t){return e.tableName==t.tableName&&An.isEqual(An.sortBy(e.fieldNames),An.sortBy(t.fieldNames))}function In(e,t){return Fn(e[0],t[0])&&Fn(e[1],t[1])}function Ln(e,t){return In(e.endpoints,t.endpoints)||In(e.endpoints,t.endpoints.slice().reverse())}function Rn(e){e&&(jn.refs.find(t=>Ln(t,e))&&Et(`Duplicated references`),jn.refs.push(e))}function zn(e=[]){if(!(!e||e.length===0))for(let t=0;t<e.length;t+=1)Rn(e[t])}function Bn(e){let{tables:t}=jn,{endpoints:n}=e,r=t.find(e=>e.name===n[0].tableName);if(!r||!t.find(e=>e.name===n[1].tableName))return null;if(!n[0].fieldNames){let e=`${kn.singular(n[1].tableName)}_id`;if(n[0].fieldNames=[e],!r.fields.find(t=>t.name===e))return null;n[0].fieldNames=[e]}return n[1].fieldNames||(n[1].fieldNames=[`id`]),e}function Vn(e,t,n){let r=[{tableName:e,relation:`1`},{tableName:t,relation:`1`}],i={};for(let e=0;e<n.length;e+=1){let t=n[e];t.columnName&&(r[0].fieldNames=[t.columnName]),t.primaryKey&&(r[1].fieldNames=[t.primaryKey]),t.onDelete&&(i={...i,onDelete:t.onDelete}),t.onUpdate&&(i={...i,onUpdate:t.onUpdate})}return{name:`fk_rails_${e}_${t}`,endpoints:r,...i}}function Hn(e,t){if(!t||t.length===0)return[];let n=[];for(let r=0;r<t.length;r+=1){let i=t[r],a=kn.plural(i),{tables:o}=jn,s=o.find(e=>e.name===a);if(!s)continue;let c=`${i}_id`,l=e.fields.find(e=>e.name===c),u=s.fields.find(e=>e.name===`id`);l||e.fields.push({name:c,type:{type_name:u?u.type.type_name:`varchar`}}),n.push({name:`fk_rails_${e.name}_${a}`,endpoints:[{tableName:e.name,fieldNames:[c],relation:`1`},{tableName:a,fieldNames:[`id`],relation:`1`}]})}return n}function Un(e){let{tables:t,refs:n}=e,r=t.map(e=>{let{name:t}=e;return{name:t,field:`${kn.singular(t)}_id`}});for(let e=0;e<t.length;e+=1){let i=t[e],{fields:a}=i;for(let e=0;e<a.length;e+=1){let t=a[e],o=r.find(e=>e.field===t.name);if(o){let e={name:`fk_rails_${i.name}_${o.name}`,endpoints:[{tableName:i.name,fieldNames:[t.name],relation:`1`},{tableName:o.name,fieldNames:[`id`],relation:`1`}]};n.find(t=>Ln(t,e))||n.push(e)}}}return e}if(Tt=a(),Tt!==n&&S===e.length)return Tt;throw Tt!==n&&S<e.length&&Nt(kt()),Ft(Ct,St<e.length?e.charAt(St):null,St<e.length?Mt(St,St+1):Mt(St,St))}t.exports={SyntaxError:r,parse:i}})),pRe=u(oPe()),mRe=u(lRe()),hRe=u(uRe()),gRe=u(dRe()),_Re=u(fRe()),vRe=class e{constructor(){this.layout=new f.MemoryProjectLayout,this.DBMLCompiler=new f.Compiler(this.layout)}static parseJSONToDatabase(e){return new xm(e)}static parseMySQLToJSONv2(e){return Z7(e,`mysql`)}static parseMySQLToJSON(e){return hRe.default.parse(e)}static parsePostgresToJSONv2(e){return Z7(e,`postgres`)}static parsePostgresToJSON(e){return gRe.default.parse(e)}static parseDBMLToJSONv2(e){let t=new f.MemoryProjectLayout;t.setSource(f.DEFAULT_ENTRY,e);let n=new f.Compiler(t),r=bRe(this.DBMLCompiler.parse.errors(f.DEFAULT_ENTRY));if(r.length>0)throw W7.create(r);return n.parse.rawDb(f.DEFAULT_ENTRY)}static parseDBMLToJSON(e){return pRe.default.parse(e)}static parseSchemaRbToJSON(e){return _Re.default.parse(e)}static parseMSSQLToJSON(e){return mRe.default.parseWithPegError(e)}static parseMSSQLToJSONv2(e){return Z7(e,`mssql`)}static parseSnowflakeToJSON(e){return Z7(e,`snowflake`)}static parseOracleToJSON(e){return Z7(e,`oracle`)}static parse(t,n){return new e().parse(t,n)}parse(t,n){try{let r={};switch(n){case`mysql`:r=e.parseMySQLToJSONv2(t);break;case`mysqlLegacy`:r=e.parseMySQLToJSON(t);break;case`postgres`:r=e.parsePostgresToJSONv2(t);break;case`snowflake`:r=e.parseSnowflakeToJSON(t);break;case`postgresLegacy`:r=e.parsePostgresToJSON(t);break;case`dbml`:r=e.parseDBMLToJSON(t);break;case`dbmlv2`:{this.layout.setSource(f.DEFAULT_ENTRY,t);let e=bRe(this.DBMLCompiler.parse.errors(f.DEFAULT_ENTRY));if(e.length>0)throw W7.create(e);r=this.DBMLCompiler.parse.rawDb(f.DEFAULT_ENTRY);break}case`schemarb`:r=e.parseSchemaRbToJSON(t);break;case`mssqlLegacy`:r=e.parseMSSQLToJSON(t);break;case`mssql`:r=e.parseMSSQLToJSONv2(t);break;case`oracle`:r=e.parseOracleToJSON(t);break;case`json`:r=typeof t==`object`?t:JSON.parse(t);break;default:break}return e.parseJSONToDatabase(r)}catch(e){throw W7.create(e)}}},yRe=vRe;function bRe(e){return e.map(e=>({message:e.diagnostic,location:{start:{line:e.nodeOrToken.startPos.line+1,column:e.nodeOrToken.startPos.column+1},end:{line:e.nodeOrToken.endPos.line+1,column:e.nodeOrToken.endPos.column+1}},code:e.code}))}function xRe(e,t,n={isNormalized:!0,includeRecords:!0}){let r=SRe(n),i=new yRe().parse(e,`dbmlv2`);return $9.export(i.normalize(),t,r)}function SRe(e={}){if(typeof e==`boolean`)return{isNormalized:e,includeRecords:!0};let{isNormalized:t=!0,includeRecords:n=!0}=e;return{isNormalized:t,includeRecords:n}}var CRe={export:xRe},wRe=class{static export(e,t,n={isNormalized:!0,includeRecords:!0}){let{isNormalized:r,includeRecords:i}=SRe(n),a=e instanceof xm?e.normalize():e,o=``;switch(t){case`dbml`:o=Pne.export(a,{includeRecords:i});break;case`mysql`:o=Ine.export(a);break;case`postgres`:o=Vne.export(a);break;case`json`:o=bre.export(e,{isNormalized:r});break;case`mssql`:o=Sre.export(a);break;case`oracle`:o=wre.export(a);break;default:break}return o}},$9=wRe,TRe=e=>new xm(e),ERe=e=>e.map(e=>{let{name:t,endpoints:n,onDelete:r,onUpdate:i}=e;return new RNe({name:t,endpoints:n.map(e=>new M7(e)),onDelete:r,onUpdate:i}).toJSON()}),DRe=e=>e.map(e=>{let{name:t,schemaName:n,values:r}=e;return new N7({name:t,schemaName:n,values:r})}),ORe=(e,t)=>e.map(e=>{let n=t[e.name]||{};return new A7({name:e.name,type:e.type,dbdefault:e.dbdefault,not_null:e.not_null,increment:e.increment,pk:n.pk||e.pk,unique:n.unique||e.unique,note:e.note,checks:n.checks})}),kRe=e=>e.map(e=>{let{name:t,unique:n,pk:r,type:i,columns:a}=e;return new k7({name:t,unique:n,pk:r,type:i,columns:a})}),ARe=(e,t,n,r,i)=>e.map(e=>{let{name:a,schemaName:o,note:s}=e,c=o?`${o}.${a}`:`${a}`,l=i[c]||{};return new j7({name:a,schemaName:o,fields:ORe(t[c],l),indexes:kRe(n[c]||[]),note:s,checks:r[c]})}),jRe=e=>{let{tables:t,fields:n,indexes:r,refs:i,enums:a,tableConstraints:o,checks:s}=e;try{let e=ARe(t,n,r,s,o),c=DRe(a);return TRe({schemas:[],tables:e,refs:ERe(i),enums:c,tableGroups:[],aliases:[],project:{}})}catch(e){throw Error(e)}};function MRe(e,t,n={includeRecords:!0}){let{includeRecords:r=!0}=n,i=new yRe().parse(e,t);return $9.export(i.normalize(),`dbml`,{includeRecords:r})}function NRe(e){let t=jRe(e);return $9.export(t.normalize(),`dbml`)}var PRe={import:MRe,generateDbml:NRe};function FRe(e,t,n){let r=new f.MemoryProjectLayout;return r.setSource(f.DEFAULT_ENTRY,n),new f.Compiler(r).renameTable(f.DEFAULT_ENTRY,e,t).get(f.DEFAULT_ENTRY.absolute)??n}function IRe(e,t,n){let r=new f.MemoryProjectLayout;return r.setSource(f.DEFAULT_ENTRY,e),new f.Compiler(r).syncDiagramView(f.DEFAULT_ENTRY,t,n)}function LRe(e){let t=new f.MemoryProjectLayout;return t.setSource(f.DEFAULT_ENTRY,e),new f.Compiler(t).findDiagramViewBlocks(f.DEFAULT_ENTRY)}const RRe=`8.0.0-prerelease.1`,zRe=`8.0.0-prerelease.1`;exports.CompilerError=W7,exports.ModelExporter=$9,exports.Parser=yRe,exports.VERSION=`8.0.0-prerelease.1`,Object.defineProperty(exports,`addDoubleQuoteIfNeeded`,{enumerable:!0,get:function(){return f.addDoubleQuoteIfNeeded}}),Object.defineProperty(exports,`dbmlMonarchTokensProvider`,{enumerable:!0,get:function(){return f.dbmlMonarchTokensProvider}}),exports.exporter=CRe,exports.findDiagramViewBlocks=LRe,Object.defineProperty(exports,`formatRecordValue`,{enumerable:!0,get:function(){return f.formatRecordValue}}),exports.importer=PRe,Object.defineProperty(exports,`isBinaryType`,{enumerable:!0,get:function(){return f.isBinaryType}}),Object.defineProperty(exports,`isBooleanType`,{enumerable:!0,get:function(){return f.isBooleanType}}),Object.defineProperty(exports,`isDateTimeType`,{enumerable:!0,get:function(){return f.isDateTimeType}}),Object.defineProperty(exports,`isFloatType`,{enumerable:!0,get:function(){return f.isFloatType}}),Object.defineProperty(exports,`isIntegerType`,{enumerable:!0,get:function(){return f.isIntegerType}}),Object.defineProperty(exports,`isNumericType`,{enumerable:!0,get:function(){return f.isNumericType}}),Object.defineProperty(exports,`isSerialType`,{enumerable:!0,get:function(){return f.isSerialType}}),Object.defineProperty(exports,`isStringType`,{enumerable:!0,get:function(){return f.isStringType}}),exports.renameTable=FRe,exports.syncDiagramView=IRe,Object.defineProperty(exports,`tryExtractBoolean`,{enumerable:!0,get:function(){return f.tryExtractBoolean}}),Object.defineProperty(exports,`tryExtractDateTime`,{enumerable:!0,get:function(){return f.tryExtractDateTime}}),Object.defineProperty(exports,`tryExtractEnum`,{enumerable:!0,get:function(){return f.tryExtractEnum}}),Object.defineProperty(exports,`tryExtractInteger`,{enumerable:!0,get:function(){return f.tryExtractInteger}}),Object.defineProperty(exports,`tryExtractNumeric`,{enumerable:!0,get:function(){return f.tryExtractNumeric}}),Object.defineProperty(exports,`tryExtractString`,{enumerable:!0,get:function(){return f.tryExtractString}});
124
+ `,`\r`],!1,!1),vt=` `,yt=Dt(` `,!1),S=0,bt=0,xt=[{line:1,column:1}],St=0,Ct=[],wt=0,Tt;if(`startRule`in t){if(!(t.startRule in i))throw Error(`Can't start parsing from rule "`+t.startRule+`".`);a=i[t.startRule]}function Et(e,t){throw t=t===void 0?Mt(bt,S):t,Pt(e,t)}function Dt(e,t){return{type:`literal`,text:e,ignoreCase:t}}function Ot(e,t,n){return{type:`class`,parts:e,inverted:t,ignoreCase:n}}function kt(){return{type:`end`}}function At(e){return{type:`other`,description:e}}function jt(t){var n=xt[t],r;if(n)return n;for(r=t-1;!xt[r];)r--;for(n=xt[r],n={line:n.line,column:n.column};r<t;)e.charCodeAt(r)===10?(n.line++,n.column=1):n.column++,r++;return xt[t]=n,n}function Mt(e,t){var n=jt(e),r=jt(t);return{start:{offset:e,line:n.line,column:n.column},end:{offset:t,line:r.line,column:r.column}}}function Nt(e){S<St||(S>St&&(St=S,Ct=[]),Ct.push(e))}function Pt(e,t){return new r(e,null,null,t)}function Ft(e,t,n){return new r(r.buildMessage(e,t),e,t,n)}function It(){for(var e=S,t=[],r=Lt();r!==n;)t.push(r),r=Lt();return t!==n&&(bt=e,t=o()),e=t,e}function Lt(){for(var e=S,t=[],r=Dn();r!==n;)t.push(r),r=Dn();return t===n?(S=e,e=n):(r=Rt(),r===n?(S=e,e=n):(t=[t,r],e=t)),e===n&&(e=vn(),e===n&&(e=_n(),e===n&&(e=Cn()))),e}function Rt(){var e=S,t=Ht();return t!==n&&(bt=e,t=s(t)),e=t,e===n&&(e=S,t=Bt(),t!==n&&(bt=e,t=c(t)),e=t,e===n&&(e=zt(),e===n&&(e=ln()))),e}function zt(){for(var t=S,r=[],i=On(),a,o,s,c,f,p,m;i!==n;)r.push(i),i=On();if(r!==n)if(i=rn(),i!==n){for(a=[],o=On();o!==n;)a.push(o),o=On();if(a!==n)if(o=pn(),o===n&&(o=un()),o!==n)if(e.charCodeAt(S)===44?(s=l,S++):(s=n,wt===0&&Nt(u)),s!==n){for(c=[],f=On();f!==n;)c.push(f),f=On();if(c!==n)if(f=un(),f!==n){for(p=[],m=Zt();m!==n;)p.push(m),m=Zt();p===n?(S=t,t=n):(bt=t,r=d(o,f,p),t=r)}else S=t,t=n;else S=t,t=n}else S=t,t=n;else S=t,t=n;else S=t,t=n}else S=t,t=n;else S=t,t=n;return t}function Bt(){for(var t=S,r=[],i=On(),a,o,s,c,d,p,m;i!==n;)r.push(i),i=On();if(r!==n)if(i=an(),i!==n){for(a=[],o=On();o!==n;)a.push(o),o=On();if(a!==n)if(o=un(),o!==n)if(e.charCodeAt(S)===44?(s=l,S++):(s=n,wt===0&&Nt(u)),s!==n){for(c=[],d=On();d!==n;)c.push(d),d=On();if(c!==n)if(d=un(),d!==n){for(p=[],m=Vt();m!==n;)p.push(m),m=Vt();p===n?(S=t,t=n):(bt=t,r=f(o,d,p),t=r)}else S=t,t=n;else S=t,t=n}else S=t,t=n;else S=t,t=n;else S=t,t=n}else S=t,t=n;else S=t,t=n;return t}function Vt(){var t=S,r,i,a,o,s,c;if(e.charCodeAt(S)===44?(r=l,S++):(r=n,wt===0&&Nt(u)),r!==n){for(i=[],a=On();a!==n;)i.push(a),a=On();if(i!==n)if(a=on(),a!==n)if(e.charCodeAt(S)===58?(o=p,S++):(o=n,wt===0&&Nt(m)),o!==n){for(s=[],c=On();c!==n;)s.push(c),c=On();s===n?(S=t,t=n):(c=un(),c===n?(S=t,t=n):(bt=t,r=h(c),t=r))}else S=t,t=n;else S=t,t=n;else S=t,t=n}else S=t,t=n;if(t===n){if(t=S,e.charCodeAt(S)===44?(r=l,S++):(r=n,wt===0&&Nt(u)),r!==n){for(i=[],a=On();a!==n;)i.push(a),a=On();if(i!==n)if(a=sn(),a!==n)if(e.charCodeAt(S)===58?(o=p,S++):(o=n,wt===0&&Nt(m)),o!==n){for(s=[],c=On();c!==n;)s.push(c),c=On();s===n?(S=t,t=n):(c=un(),c===n?(S=t,t=n):(bt=t,r=g(c),t=r))}else S=t,t=n;else S=t,t=n;else S=t,t=n}else S=t,t=n;if(t===n)if(t=S,e.charCodeAt(S)===44?(r=l,S++):(r=n,wt===0&&Nt(u)),r!==n){for(i=[],a=On();a!==n;)i.push(a),a=On();if(i!==n)if(a=Qt(),a!==n)if(e.charCodeAt(S)===58?(o=p,S++):(o=n,wt===0&&Nt(m)),o!==n){for(s=[],c=On();c!==n;)s.push(c),c=On();s===n?(S=t,t=n):(c=pn(),c===n?(S=t,t=n):(bt=t,r=_(a,c),t=r))}else S=t,t=n;else S=t,t=n;else S=t,t=n}else S=t,t=n}return t}function Ht(){var e=S,t=$t(),r,i,a,o,s,c;if(t!==n){for(r=[],i=On();i!==n;)r.push(i),i=On();r===n?(S=e,e=n):(i=un(),i===n?(S=e,e=n):(a=yn(),a===n?(S=e,e=n):(o=wn(),o===n?(S=e,e=n):(s=Ut(),s===n?(S=e,e=n):(c=_n(),c===n?(S=e,e=n):(bt=e,t=v(i,s),e=t))))))}else S=e,e=n;return e}function Ut(){for(var e=S,t=[],r=Wt();r!==n;)t.push(r),r=Wt();return t!==n&&(bt=e,t=y(t)),e=t,e}function Wt(){for(var e=S,t=[],r=Dn(),i,a;r!==n;)t.push(r),r=Dn();if(t!==n)if(r=Gt(),r!==n){for(i=[],a=bn();a!==n;)i.push(a),a=bn();i===n?(S=e,e=n):(a=wn(),a===n?(S=e,e=n):(bt=e,t=b(r),e=t))}else S=e,e=n;else S=e,e=n;return e}function Gt(){var e=Kt(),t;return e===n&&(e=S,t=qt(),t!==n&&(bt=e,t=x(t)),e=t,e===n&&(e=S,t=Jt(),t!==n&&(bt=e,t=ee(t)),e=t,e===n&&(e=S,t=Yt(),t!==n&&(bt=e,t=te(t)),e=t))),e}function Kt(){var e=S,t=en(),r,i;if(t!==n){if(r=[],i=On(),i!==n)for(;i!==n;)r.push(i),i=On();else r=n;r===n?(S=e,e=n):(i=yn(),i===n?(S=e,e=n):(t=[t,r,i],e=t))}else S=e,e=n;return e}function qt(){var e=S,t=tn(),r,i;if(t!==n){if(r=[],i=On(),i!==n)for(;i!==n;)r.push(i),i=On();else r=n;r===n?(S=e,e=n):(i=Xt(),i===n?(S=e,e=n):(bt=e,t=ne(i),e=t))}else S=e,e=n;return e}function Jt(){var e=S,t=nn(),r,i,a,o;if(t!==n){if(r=[],i=On(),i!==n)for(;i!==n;)r.push(i),i=On();else r=n;if(r!==n)if(i=un(),i!==n){for(a=[],o=Zt();o!==n;)a.push(o),o=Zt();a===n?(S=e,e=n):(bt=e,t=re(i,a),e=t)}else S=e,e=n;else S=e,e=n}else S=e,e=n;return e}function Yt(){var e=S,t=hn(),r,i;if(t!==n){if(r=[],i=On(),i!==n)for(;i!==n;)r.push(i),i=On();else r=n;r===n?(S=e,e=n):(i=un(),i===n?(S=e,e=n):(bt=e,t=ie(t,i),e=t))}else S=e,e=n;return e}function Xt(){var t=S,r,i;return e.charCodeAt(S)===58?(r=p,S++):(r=n,wt===0&&Nt(m)),r===n?(S=t,t=n):(i=mn(),i===n?(S=t,t=n):(bt=t,r=ae(i),t=r)),t===n&&(t=S,r=un(),r!==n&&(bt=t,r=ae(r)),t=r),t}function Zt(){var t=S,r,i,a,o,s,c;if(e.charCodeAt(S)===44?(r=l,S++):(r=n,wt===0&&Nt(u)),r!==n){for(i=[],a=On();a!==n;)i.push(a),a=On();if(i!==n)if(e.substr(S,4).toLowerCase()===oe?(a=e.substr(S,4),S+=4):(a=n,wt===0&&Nt(se)),a!==n)if(e.charCodeAt(S)===58?(o=p,S++):(o=n,wt===0&&Nt(m)),o!==n){for(s=[],c=On();c!==n;)s.push(c),c=On();s===n?(S=t,t=n):(c=un(),c===n?(S=t,t=n):(bt=t,r=ce(c),t=r))}else S=t,t=n;else S=t,t=n;else S=t,t=n}else S=t,t=n;return t}function Qt(){var t;return e.substr(S,9).toLowerCase()===le?(t=e.substr(S,9),S+=9):(t=n,wt===0&&Nt(ue)),t===n&&(e.substr(S,9).toLowerCase()===de?(t=e.substr(S,9),S+=9):(t=n,wt===0&&Nt(fe))),t}function $t(){var t;return wt++,e.substr(S,12).toLowerCase()===me?(t=e.substr(S,12),S+=12):(t=n,wt===0&&Nt(he)),wt--,t===n&&wt===0&&Nt(pe),t}function en(){var t,r,i;return wt++,t=S,r=gn(),r===n?(S=t,t=n):(e.substr(S,6)===_e?(i=_e,S+=6):(i=n,wt===0&&Nt(ve)),i===n?(S=t,t=n):(r=[r,i],t=r)),wt--,t===n&&(r=n,wt===0&&Nt(ge)),t}function tn(){var t,r,i;return wt++,t=S,r=gn(),r===n?(S=t,t=n):(e.substr(S,11)===be?(i=be,S+=11):(i=n,wt===0&&Nt(xe)),i===n?(S=t,t=n):(r=[r,i],t=r)),wt--,t===n&&(r=n,wt===0&&Nt(ye)),t}function nn(){var t,r,i;return wt++,t=S,r=gn(),r===n?(S=t,t=n):(e.substr(S,17)===Ce?(i=Ce,S+=17):(i=n,wt===0&&Nt(we)),i===n?(S=t,t=n):(r=[r,i],t=r)),wt--,t===n&&(r=n,wt===0&&Nt(Se)),t}function rn(){var t;return wt++,e.substr(S,20).toLowerCase()===Ee?(t=e.substr(S,20),S+=20):(t=n,wt===0&&Nt(De)),wt--,t===n&&wt===0&&Nt(Te),t}function an(){var t;return wt++,e.substr(S,15).toLowerCase()===ke?(t=e.substr(S,15),S+=15):(t=n,wt===0&&Nt(Ae)),wt--,t===n&&wt===0&&Nt(Oe),t}function on(){var t;return wt++,e.substr(S,6)===Me?(t=Me,S+=6):(t=n,wt===0&&Nt(Ne)),wt--,t===n&&wt===0&&Nt(je),t}function sn(){var t;return wt++,e.substr(S,11)===Fe?(t=Fe,S+=11):(t=n,wt===0&&Nt(Ie)),wt--,t===n&&wt===0&&Nt(Pe),t}function cn(){var t;return e.substr(S,3)===Le?(t=Le,S+=3):(t=n,wt===0&&Nt(Re)),t}function ln(){var e=S,t=mn(),r,i;return t===n?(S=e,e=n):(r=yn(),r===n?(S=e,e=n):(i=wn(),i===n&&(i=null),i===n?(S=e,e=n):(t=[t,r,i],e=t))),e}function un(){var e=dn();return e===n&&(e=fn()),e}function dn(){var t=S,r=Sn(),i,a;if(r!==n){for(i=[],ze.test(e.charAt(S))?(a=e.charAt(S),S++):(a=n,wt===0&&Nt(Be));a!==n;)i.push(a),ze.test(e.charAt(S))?(a=e.charAt(S),S++):(a=n,wt===0&&Nt(Be));i===n?(S=t,t=n):(a=Sn(),a===n?(S=t,t=n):(bt=t,r=Ve(i),t=r))}else S=t,t=n;return t}function fn(){var t=S,r=xn(),i,a;if(r!==n){for(i=[],He.test(e.charAt(S))?(a=e.charAt(S),S++):(a=n,wt===0&&Nt(Ue));a!==n;)i.push(a),He.test(e.charAt(S))?(a=e.charAt(S),S++):(a=n,wt===0&&Nt(Ue));i===n?(S=t,t=n):(a=xn(),a===n?(S=t,t=n):(bt=t,r=Ve(i),t=r))}else S=t,t=n;return t}function pn(){var t=S,r,i,a;if(e.charCodeAt(S)===58?(r=p,S++):(r=n,wt===0&&Nt(m)),r!==n){for(i=[],a=gn();a!==n;)i.push(a),a=gn();i===n?(S=t,t=n):(bt=t,r=Ve(i),t=r)}else S=t,t=n;return t}function mn(){var e=S,t=[],r=gn();if(r!==n)for(;r!==n;)t.push(r),r=gn();else t=n;return t!==n&&(bt=e,t=Ve(t)),e=t,e}function hn(){var t=S,r=gn(),i,a,o;if(r!==n)if(e.charCodeAt(S)===46?(i=We,S++):(i=n,wt===0&&Nt(Ge)),i!==n){if(a=[],o=gn(),o!==n)for(;o!==n;)a.push(o),o=gn();else a=n;a===n?(S=t,t=n):(bt=t,r=Ve(a),t=r)}else S=t,t=n;else S=t,t=n;return t}function gn(){var t;return wt++,qe.test(e.charAt(S))?(t=e.charAt(S),S++):(t=n,wt===0&&Nt(Je)),wt--,t===n&&wt===0&&Nt(Ke),t}function _n(){for(var e=S,t=[],r=Dn(),i;r!==n;)t.push(r),r=Dn();return t===n?(S=e,e=n):(r=cn(),r===n?(S=e,e=n):(i=wn(),i===n&&(i=null),i===n?(S=e,e=n):(t=[t,r,i],e=t))),e}function vn(){var t,r,i,a,o;for(wt++,t=S,r=[],i=Dn();i!==n;)r.push(i),i=Dn();return r===n?(S=t,t=n):(e.charCodeAt(S)===35?(i=Xe,S++):(i=n,wt===0&&Nt(Ze)),i===n?(S=t,t=n):(a=yn(),a===n?(S=t,t=n):(o=wn(),o===n&&(o=null),o===n?(S=t,t=n):(r=[r,i,a,o],t=r)))),wt--,t===n&&(r=n,wt===0&&Nt(Ye)),t}function yn(){var t,r;for(wt++,t=[],$e.test(e.charAt(S))?(r=e.charAt(S),S++):(r=n,wt===0&&Nt(et));r!==n;)t.push(r),$e.test(e.charAt(S))?(r=e.charAt(S),S++):(r=n,wt===0&&Nt(et));return wt--,t===n&&(r=n,wt===0&&Nt(Qe)),t}function bn(){var t;return $e.test(e.charAt(S))?(t=e.charAt(S),S++):(t=n,wt===0&&Nt(et)),t}function xn(){var t;return e.charCodeAt(S)===39?(t=tt,S++):(t=n,wt===0&&Nt(nt)),t}function Sn(){var t;return e.charCodeAt(S)===34?(t=rt,S++):(t=n,wt===0&&Nt(it)),t}function Cn(){var e=[],t=Tn();if(t===n&&(t=Dn()),t!==n)for(;t!==n;)e.push(t),t=Tn(),t===n&&(t=Dn());else e=n;return e}function wn(){for(var e=S,t=[],r=On();r!==n;)t.push(r),r=On();return t===n?(S=e,e=n):(r=En(),r===n?(S=e,e=n):(t=[t,r],e=t)),e}function Tn(){var t,r,i;return wt++,t=S,e.substr(S,2)===ot?(r=ot,S+=2):(r=n,wt===0&&Nt(st)),r===n?(S=t,t=n):(ct.test(e.charAt(S))?(i=e.charAt(S),S++):(i=n,wt===0&&Nt(lt)),i===n&&(i=null),i===n?(S=t,t=n):(r=[r,i],t=r)),wt--,t===n&&(r=n,wt===0&&Nt(at)),t}function En(){var t;return wt++,e.substr(S,2)===dt?(t=dt,S+=2):(t=n,wt===0&&Nt(ft)),t===n&&(e.charCodeAt(S)===10?(t=pt,S++):(t=n,wt===0&&Nt(mt))),wt--,t===n&&wt===0&&Nt(ut),t}function Dn(){var t;return wt++,gt.test(e.charAt(S))?(t=e.charAt(S),S++):(t=n,wt===0&&Nt(_t)),wt--,t===n&&wt===0&&Nt(ht),t}function On(){var t;return e.charCodeAt(S)===32?(t=vt,S++):(t=n,wt===0&&Nt(yt)),t}var kn=$7(),An=Q7();let jn={tables:[],refs:[]};function Mn(e){jn.tables.find(t=>t.name==e.name)?Et(`Duplicated table name`):(e.fields.find(e=>e.name===`id`)||e.fields.unshift({name:`id`,type:{type_name:`varchar`}}),jn.tables.push(e))}function Nn(e,t,n){let r=jn.tables.find(t=>t.name===e);r||Et("Table ${tableName} not found"),r.checks||=[];let i={expression:t};n&&(i.name=n),r.checks.push(i)}function Pn(e=[],t=[]){let n=t.find(e=>e.name===`primary_key`);return n?e.find(e=>e.name===n.value)?e.map(({name:e,type:t})=>({name:e,type:t,PK:n.value===field.name})):[{name:n.value,type:{type_name:`varchar`},PK:!0}].concat(e):e}function Fn(e,t){return e.tableName==t.tableName&&An.isEqual(An.sortBy(e.fieldNames),An.sortBy(t.fieldNames))}function In(e,t){return Fn(e[0],t[0])&&Fn(e[1],t[1])}function Ln(e,t){return In(e.endpoints,t.endpoints)||In(e.endpoints,t.endpoints.slice().reverse())}function Rn(e){e&&(jn.refs.find(t=>Ln(t,e))&&Et(`Duplicated references`),jn.refs.push(e))}function zn(e=[]){if(!(!e||e.length===0))for(let t=0;t<e.length;t+=1)Rn(e[t])}function Bn(e){let{tables:t}=jn,{endpoints:n}=e,r=t.find(e=>e.name===n[0].tableName);if(!r||!t.find(e=>e.name===n[1].tableName))return null;if(!n[0].fieldNames){let e=`${kn.singular(n[1].tableName)}_id`;if(n[0].fieldNames=[e],!r.fields.find(t=>t.name===e))return null;n[0].fieldNames=[e]}return n[1].fieldNames||(n[1].fieldNames=[`id`]),e}function Vn(e,t,n){let r=[{tableName:e,relation:`1`},{tableName:t,relation:`1`}],i={};for(let e=0;e<n.length;e+=1){let t=n[e];t.columnName&&(r[0].fieldNames=[t.columnName]),t.primaryKey&&(r[1].fieldNames=[t.primaryKey]),t.onDelete&&(i={...i,onDelete:t.onDelete}),t.onUpdate&&(i={...i,onUpdate:t.onUpdate})}return{name:`fk_rails_${e}_${t}`,endpoints:r,...i}}function Hn(e,t){if(!t||t.length===0)return[];let n=[];for(let r=0;r<t.length;r+=1){let i=t[r],a=kn.plural(i),{tables:o}=jn,s=o.find(e=>e.name===a);if(!s)continue;let c=`${i}_id`,l=e.fields.find(e=>e.name===c),u=s.fields.find(e=>e.name===`id`);l||e.fields.push({name:c,type:{type_name:u?u.type.type_name:`varchar`}}),n.push({name:`fk_rails_${e.name}_${a}`,endpoints:[{tableName:e.name,fieldNames:[c],relation:`1`},{tableName:a,fieldNames:[`id`],relation:`1`}]})}return n}function Un(e){let{tables:t,refs:n}=e,r=t.map(e=>{let{name:t}=e;return{name:t,field:`${kn.singular(t)}_id`}});for(let e=0;e<t.length;e+=1){let i=t[e],{fields:a}=i;for(let e=0;e<a.length;e+=1){let t=a[e],o=r.find(e=>e.field===t.name);if(o){let e={name:`fk_rails_${i.name}_${o.name}`,endpoints:[{tableName:i.name,fieldNames:[t.name],relation:`1`},{tableName:o.name,fieldNames:[`id`],relation:`1`}]};n.find(t=>Ln(t,e))||n.push(e)}}}return e}if(Tt=a(),Tt!==n&&S===e.length)return Tt;throw Tt!==n&&S<e.length&&Nt(kt()),Ft(Ct,St<e.length?e.charAt(St):null,St<e.length?Mt(St,St+1):Mt(St,St))}t.exports={SyntaxError:r,parse:i}})),pRe=u(oPe()),mRe=u(lRe()),hRe=u(uRe()),gRe=u(dRe()),_Re=u(fRe()),vRe=class e{constructor(){this.layout=new f.MemoryProjectLayout,this.DBMLCompiler=new f.Compiler(this.layout)}static parseJSONToDatabase(e){return new xm(e)}static parseMySQLToJSONv2(e){return Z7(e,`mysql`)}static parseMySQLToJSON(e){return hRe.default.parse(e)}static parsePostgresToJSONv2(e){return Z7(e,`postgres`)}static parsePostgresToJSON(e){return gRe.default.parse(e)}static parseDBMLToJSONv2(e){let t=new f.MemoryProjectLayout;t.setSource(f.DEFAULT_ENTRY,e);let n=new f.Compiler(t),r=bRe(n.parse.errors(f.DEFAULT_ENTRY));if(r.length>0)throw W7.create(r);return n.parse.rawDb(f.DEFAULT_ENTRY)}static parseDBMLToJSON(e){return pRe.default.parse(e)}static parseSchemaRbToJSON(e){return _Re.default.parse(e)}static parseMSSQLToJSON(e){return mRe.default.parseWithPegError(e)}static parseMSSQLToJSONv2(e){return Z7(e,`mssql`)}static parseSnowflakeToJSON(e){return Z7(e,`snowflake`)}static parseOracleToJSON(e){return Z7(e,`oracle`)}static parse(t,n){return new e().parse(t,n)}getDbmlSource(e){return e=typeof e==`string`?f.Filepath.from(e):e,this.layout.getSource(e)}setDbmlSource(e,t){e=typeof e==`string`?f.Filepath.from(e):e,t===void 0?this.layout.deleteSource(e):this.layout.setSource(e,t)}deleteDbmlSource(e){e=typeof e==`string`?f.Filepath.from(e):e,this.layout.deleteSource(e)}clearDbmlSource(){this.layout.clearSource()}parseDbmlProject(t){t=typeof t==`string`?f.Filepath.from(t):t;let n=this.DBMLCompiler.interpretFile(t),r=bRe(n.getErrors());if(r.length>0)throw W7.create(r);return e.parseJSONToDatabase(n.getValue()||{})}parse(t,n){try{let r={};switch(n){case`mysql`:r=e.parseMySQLToJSONv2(t);break;case`mysqlLegacy`:r=e.parseMySQLToJSON(t);break;case`postgres`:r=e.parsePostgresToJSONv2(t);break;case`snowflake`:r=e.parseSnowflakeToJSON(t);break;case`postgresLegacy`:r=e.parsePostgresToJSON(t);break;case`dbml`:r=e.parseDBMLToJSON(t);break;case`dbmlv2`:r=e.parseDBMLToJSONv2(t);break;case`schemarb`:r=e.parseSchemaRbToJSON(t);break;case`mssqlLegacy`:r=e.parseMSSQLToJSON(t);break;case`mssql`:r=e.parseMSSQLToJSONv2(t);break;case`oracle`:r=e.parseOracleToJSON(t);break;case`json`:r=typeof t==`object`?t:JSON.parse(t);break;default:break}return e.parseJSONToDatabase(r)}catch(e){throw W7.create(e)}}},yRe=vRe;function bRe(e){return e.map(e=>({message:e.diagnostic,location:{start:{line:e.nodeOrToken.startPos.line+1,column:e.nodeOrToken.startPos.column+1},end:{line:e.nodeOrToken.endPos.line+1,column:e.nodeOrToken.endPos.column+1}},code:e.code}))}function xRe(e,t,n={isNormalized:!0,includeRecords:!0}){let r=SRe(n),i=new yRe().parse(e,`dbmlv2`);return $9.export(i.normalize(),t,r)}function SRe(e={}){if(typeof e==`boolean`)return{isNormalized:e,includeRecords:!0};let{isNormalized:t=!0,includeRecords:n=!0}=e;return{isNormalized:t,includeRecords:n}}var CRe={export:xRe},wRe=class{static export(e,t,n={isNormalized:!0,includeRecords:!0}){let{isNormalized:r,includeRecords:i}=SRe(n),a=e instanceof xm?e.normalize():e,o=``;switch(t){case`dbml`:o=Pne.export(a,{includeRecords:i});break;case`mysql`:o=Ine.export(a);break;case`postgres`:o=Vne.export(a);break;case`json`:o=bre.export(e,{isNormalized:r});break;case`mssql`:o=Sre.export(a);break;case`oracle`:o=wre.export(a);break;default:break}return o}},$9=wRe,TRe=e=>new xm(e),ERe=e=>e.map(e=>{let{name:t,endpoints:n,onDelete:r,onUpdate:i}=e;return new RNe({name:t,endpoints:n.map(e=>new M7(e)),onDelete:r,onUpdate:i}).toJSON()}),DRe=e=>e.map(e=>{let{name:t,schemaName:n,values:r}=e;return new N7({name:t,schemaName:n,values:r})}),ORe=(e,t)=>e.map(e=>{let n=t[e.name]||{};return new A7({name:e.name,type:e.type,dbdefault:e.dbdefault,not_null:e.not_null,increment:e.increment,pk:n.pk||e.pk,unique:n.unique||e.unique,note:e.note,checks:n.checks})}),kRe=e=>e.map(e=>{let{name:t,unique:n,pk:r,type:i,columns:a}=e;return new k7({name:t,unique:n,pk:r,type:i,columns:a})}),ARe=(e,t,n,r,i)=>e.map(e=>{let{name:a,schemaName:o,note:s}=e,c=o?`${o}.${a}`:`${a}`,l=i[c]||{};return new j7({name:a,schemaName:o,fields:ORe(t[c],l),indexes:kRe(n[c]||[]),note:s,checks:r[c]})}),jRe=e=>{let{tables:t,fields:n,indexes:r,refs:i,enums:a,tableConstraints:o,checks:s}=e;try{let e=ARe(t,n,r,s,o),c=DRe(a);return TRe({schemas:[],tables:e,refs:ERe(i),enums:c,tableGroups:[],aliases:[],project:{}})}catch(e){throw Error(e)}};function MRe(e,t,n={includeRecords:!0}){let{includeRecords:r=!0}=n,i=new yRe().parse(e,t);return $9.export(i.normalize(),`dbml`,{includeRecords:r})}function NRe(e){let t=jRe(e);return $9.export(t.normalize(),`dbml`)}var PRe={import:MRe,generateDbml:NRe};function FRe(e,t,n){let r=new f.MemoryProjectLayout;return r.setSource(f.DEFAULT_ENTRY,n),new f.Compiler(r).renameTable(f.DEFAULT_ENTRY,e,t).get(f.DEFAULT_ENTRY.absolute)??n}function IRe(e,t,n){let r=new f.MemoryProjectLayout;return r.setSource(f.DEFAULT_ENTRY,e),new f.Compiler(r).syncDiagramView(f.DEFAULT_ENTRY,t,n)}function LRe(e){let t=new f.MemoryProjectLayout;return t.setSource(f.DEFAULT_ENTRY,e),new f.Compiler(t).findDiagramViewBlocks(f.DEFAULT_ENTRY)}const RRe=`8.1.0`,zRe=`8.1.0`;exports.CompilerError=W7,Object.defineProperty(exports,`DEFAULT_ENTRY`,{enumerable:!0,get:function(){return f.DEFAULT_ENTRY}}),Object.defineProperty(exports,`Filepath`,{enumerable:!0,get:function(){return f.Filepath}}),exports.ModelExporter=$9,exports.Parser=yRe,exports.VERSION=`8.1.0`,Object.defineProperty(exports,`addDoubleQuoteIfNeeded`,{enumerable:!0,get:function(){return f.addDoubleQuoteIfNeeded}}),Object.defineProperty(exports,`dbmlMonarchTokensProvider`,{enumerable:!0,get:function(){return f.dbmlMonarchTokensProvider}}),exports.exporter=CRe,exports.findDiagramViewBlocks=LRe,Object.defineProperty(exports,`formatRecordValue`,{enumerable:!0,get:function(){return f.formatRecordValue}}),exports.importer=PRe,Object.defineProperty(exports,`isBinaryType`,{enumerable:!0,get:function(){return f.isBinaryType}}),Object.defineProperty(exports,`isBooleanType`,{enumerable:!0,get:function(){return f.isBooleanType}}),Object.defineProperty(exports,`isDateTimeType`,{enumerable:!0,get:function(){return f.isDateTimeType}}),Object.defineProperty(exports,`isFloatType`,{enumerable:!0,get:function(){return f.isFloatType}}),Object.defineProperty(exports,`isIntegerType`,{enumerable:!0,get:function(){return f.isIntegerType}}),Object.defineProperty(exports,`isNumericType`,{enumerable:!0,get:function(){return f.isNumericType}}),Object.defineProperty(exports,`isSerialType`,{enumerable:!0,get:function(){return f.isSerialType}}),Object.defineProperty(exports,`isStringType`,{enumerable:!0,get:function(){return f.isStringType}}),exports.renameTable=FRe,exports.syncDiagramView=IRe,Object.defineProperty(exports,`tryExtractBoolean`,{enumerable:!0,get:function(){return f.tryExtractBoolean}}),Object.defineProperty(exports,`tryExtractDateTime`,{enumerable:!0,get:function(){return f.tryExtractDateTime}}),Object.defineProperty(exports,`tryExtractEnum`,{enumerable:!0,get:function(){return f.tryExtractEnum}}),Object.defineProperty(exports,`tryExtractInteger`,{enumerable:!0,get:function(){return f.tryExtractInteger}}),Object.defineProperty(exports,`tryExtractNumeric`,{enumerable:!0,get:function(){return f.tryExtractNumeric}}),Object.defineProperty(exports,`tryExtractString`,{enumerable:!0,get:function(){return f.tryExtractString}});
package/lib/index.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { Compiler, DEFAULT_ENTRY, MemoryProjectLayout, addDoubleQuoteIfNeeded, addDoubleQuoteIfNeeded as addDoubleQuoteIfNeeded$1, dbmlMonarchTokensProvider, formatRecordValue, formatRecordValue as formatRecordValue$1, isBinaryType, isBinaryType as isBinaryType$1, isBooleanType, isBooleanType as isBooleanType$1, isDateTimeType, isDateTimeType as isDateTimeType$1, isFloatType, isIntegerType, isNumericType, isNumericType as isNumericType$1, isSerialType, isStringType, isStringType as isStringType$1, tryExtractBoolean, tryExtractDateTime, tryExtractDateTime as tryExtractDateTime$1, tryExtractEnum, tryExtractInteger, tryExtractNumeric, tryExtractString } from "@dbml/parse";
1
+ import { Compiler, DEFAULT_ENTRY, DEFAULT_ENTRY as DEFAULT_ENTRY$1, Filepath, Filepath as Filepath$1, MemoryProjectLayout, addDoubleQuoteIfNeeded, addDoubleQuoteIfNeeded as addDoubleQuoteIfNeeded$1, dbmlMonarchTokensProvider, formatRecordValue, formatRecordValue as formatRecordValue$1, isBinaryType, isBinaryType as isBinaryType$1, isBooleanType, isBooleanType as isBooleanType$1, isDateTimeType, isDateTimeType as isDateTimeType$1, isFloatType, isIntegerType, isNumericType, isNumericType as isNumericType$1, isSerialType, isStringType, isStringType as isStringType$1, tryExtractBoolean, tryExtractDateTime, tryExtractDateTime as tryExtractDateTime$1, tryExtractEnum, tryExtractInteger, tryExtractNumeric, tryExtractString } from "@dbml/parse";
2
2
  var __create = Object.create, __defProp = Object.defineProperty, __getOwnPropDesc = Object.getOwnPropertyDescriptor, __getOwnPropNames = Object.getOwnPropertyNames, __getProtoOf = Object.getPrototypeOf, __hasOwnProp = Object.prototype.hasOwnProperty, __esmMin = (_, VY) => () => (_ && (VY = _(_ = 0)), VY), __commonJSMin = (_, VY) => () => (VY || _((VY = { exports: {} }).exports, VY), VY.exports), __export = (_, VY) => {
3
3
  let P = {};
4
4
  for (var HY in _) __defProp(P, HY, {
@@ -4762,14 +4762,14 @@ var DbmlExporter_default = class _ {
4762
4762
  }
4763
4763
  static exportTables(VY, P) {
4764
4764
  let HY = _.getTableContentArr(VY, P).map((VY) => {
4765
- let HY = P.tables[VY.tableId], WY = P.schemas[HY.schemaId], GY = _.getTableSettings(HY), KY = `"${HY.name}"`;
4766
- shouldPrintSchema(WY, P) && (KY = `"${WY.name}"."${HY.name}"`);
4765
+ let HY = P.tables[VY.tableId], UY = P.schemas[HY.schemaId], WY = _.getTableSettings(HY), GY = `"${HY.name}"`;
4766
+ shouldPrintSchema(UY, P) && (GY = `"${UY.name}"."${HY.name}"`);
4767
4767
  let qY = HY.alias ? ` as ${addDoubleQuoteIfNeeded$1(HY.alias)}` : "", JY = VY.fieldContents.map((_) => ` ${_}\n`).join(""), YY = "";
4768
4768
  isEmpty_default(VY.checkContents) || (YY = `\n Checks {\n${VY.checkContents.map((_) => ` ${_}\n`).join("")} }\n`);
4769
4769
  let XY = "";
4770
4770
  isEmpty_default(VY.indexContents) || (XY = `\n Indexes {\n${VY.indexContents.map((_) => ` ${_}\n`).join("")} }\n`);
4771
4771
  let ZY = HY.note ? ` Note: ${_.escapeNote(HY.note)}\n` : "";
4772
- return `Table ${KY}${qY}${GY} {\n${JY}${YY}${XY}${ZY}}\n`;
4772
+ return `Table ${GY}${qY}${WY} {\n${JY}${YY}${XY}${ZY}}\n`;
4773
4773
  });
4774
4774
  return HY.length ? HY.join("\n") : "";
4775
4775
  }
@@ -4813,9 +4813,9 @@ var DbmlExporter_default = class _ {
4813
4813
  if (!VY || isEmpty_default(VY)) return "";
4814
4814
  let P = groupBy_default(Object.values(VY), (_) => `${_.schemaName || ""}.${_.tableName}`);
4815
4815
  return Object.values(P).map((_) => {
4816
- let { schemaName: VY, tableName: P } = _[0], HY = VY ? `${addDoubleQuoteIfNeeded$1(VY)}.${addDoubleQuoteIfNeeded$1(P)}` : addDoubleQuoteIfNeeded$1(P), WY = [...new Set(_.flatMap((_) => _.columns))];
4817
- return `Records ${HY}(${WY.map(addDoubleQuoteIfNeeded$1).join(", ")}) {\n${_.flatMap((_) => {
4818
- let VY = WY.map((VY) => _.columns.indexOf(VY));
4816
+ let { schemaName: VY, tableName: P } = _[0], HY = VY ? `${addDoubleQuoteIfNeeded$1(VY)}.${addDoubleQuoteIfNeeded$1(P)}` : addDoubleQuoteIfNeeded$1(P), UY = [...new Set(_.flatMap((_) => _.columns))];
4817
+ return `Records ${HY}(${UY.map(addDoubleQuoteIfNeeded$1).join(", ")}) {\n${_.flatMap((_) => {
4818
+ let VY = UY.map((VY) => _.columns.indexOf(VY));
4819
4819
  return _.values.map((_) => VY.map((VY) => VY === -1 ? {
4820
4820
  value: null,
4821
4821
  type: "expression"
@@ -5942,8 +5942,8 @@ var tablePartial_default = class extends element_default {
5942
5942
  };
5943
5943
  init_lodash();
5944
5944
  var database_default = class extends element_default {
5945
- constructor({ schemas: _ = [], tables: VY = [], notes: P = [], enums: HY = [], refs: UY = [], tableGroups: WY = [], project: GY = {}, aliases: KY = [], records: qY = [], tablePartials: JY = [] }) {
5946
- super(), this.dbState = new DbState(), this.generateId(), this.hasDefaultSchema = !1, this.schemas = [], this.notes = [], this.note = GY.note ? get_default(GY, "note.value", GY.note) : null, this.noteToken = GY.note ? get_default(GY, "note.token", GY.noteToken) : null, this.databaseType = GY.database_type, this.name = GY.name, this.token = GY.token, this.aliases = KY, this.records = [], this.tablePartials = [], this.injectedRawRefs = [], this.processNotes(P), this.processRecords(qY), this.processTablePartials(JY), this.processSchemas(_), this.processSchemaElements(HY, ENUM), this.processSchemaElements(VY, TABLE), this.linkRecordsToTables(), this.processSchemaElements(P, "note"), this.processSchemaElements(UY, "ref"), this.processSchemaElements(WY, TABLE_GROUP), this.injectedRawRefs.forEach((_) => {
5945
+ constructor({ schemas: _ = [], tables: VY = [], notes: P = [], enums: HY = [], refs: UY = [], tableGroups: WY = [], project: GY = {}, aliases: KY = [], records: qY = [], tablePartials: JY = [], diagramViews: YY = [] }) {
5946
+ super(), this.dbState = new DbState(), this.generateId(), this.hasDefaultSchema = !1, this.schemas = [], this.notes = [], this.note = GY.note ? get_default(GY, "note.value", GY.note) : null, this.noteToken = GY.note ? get_default(GY, "note.token", GY.noteToken) : null, this.databaseType = GY.database_type, this.name = GY.name, this.token = GY.token, this.aliases = KY, this.records = [], this.tablePartials = [], this.diagramViews = YY, this.injectedRawRefs = [], this.processNotes(P), this.processRecords(qY), this.processTablePartials(JY), this.processSchemas(_), this.processSchemaElements(HY, ENUM), this.processSchemaElements(VY, TABLE), this.linkRecordsToTables(), this.processSchemaElements(P, "note"), this.processSchemaElements(UY, "ref"), this.processSchemaElements(WY, TABLE_GROUP), this.injectedRawRefs.forEach((_) => {
5947
5947
  let VY = this.findOrCreateSchema(DEFAULT_SCHEMA_NAME), P = new ref_default({
5948
5948
  ..._,
5949
5949
  schema: VY
@@ -1810729,7 +1810729,7 @@ var init_actions = __esmMin((() => {
1810729
1810729
  SyntaxError: HY,
1810730
1810730
  parse: UY
1810731
1810731
  };
1810732
- })), import_dbmlParser = /* @__PURE__ */ __toESM(require_dbmlParser()), import_mssqlParser = /* @__PURE__ */ __toESM(require_mssqlParser()), import_mysqlParser = /* @__PURE__ */ __toESM(require_mysqlParser()), import_postgresParser = /* @__PURE__ */ __toESM(require_postgresParser()), import_schemarbParser = /* @__PURE__ */ __toESM(require_schemarbParser()), Parser_default = class HY {
1810732
+ })), import_dbmlParser = /* @__PURE__ */ __toESM(require_dbmlParser()), import_mssqlParser = /* @__PURE__ */ __toESM(require_mssqlParser()), import_mysqlParser = /* @__PURE__ */ __toESM(require_mysqlParser()), import_postgresParser = /* @__PURE__ */ __toESM(require_postgresParser()), import_schemarbParser = /* @__PURE__ */ __toESM(require_schemarbParser()), Parser_default = class VY {
1810733
1810733
  constructor() {
1810734
1810734
  this.layout = new MemoryProjectLayout(), this.DBMLCompiler = new Compiler(this.layout);
1810735
1810735
  }
@@ -1810748,12 +1810748,12 @@ var init_actions = __esmMin((() => {
1810748
1810748
  static parsePostgresToJSON(_) {
1810749
1810749
  return import_postgresParser.default.parse(_);
1810750
1810750
  }
1810751
- static parseDBMLToJSONv2(HY) {
1810752
- let UY = new MemoryProjectLayout();
1810753
- UY.setSource(DEFAULT_ENTRY, HY);
1810754
- let WY = new Compiler(UY), GY = convertDbmlParserError(this.DBMLCompiler.parse.errors(DEFAULT_ENTRY));
1810751
+ static parseDBMLToJSONv2(VY) {
1810752
+ let HY = new MemoryProjectLayout();
1810753
+ HY.setSource(DEFAULT_ENTRY$1, VY);
1810754
+ let UY = new Compiler(HY), GY = convertDbmlParserError(UY.parse.errors(DEFAULT_ENTRY$1));
1810755
1810755
  if (GY.length > 0) throw CompilerError.create(GY);
1810756
- return WY.parse.rawDb(DEFAULT_ENTRY);
1810756
+ return UY.parse.rawDb(DEFAULT_ENTRY$1);
1810757
1810757
  }
1810758
1810758
  static parseDBMLToJSON(_) {
1810759
1810759
  return import_dbmlParser.default.parse(_);
@@ -1810773,56 +1810773,70 @@ var init_actions = __esmMin((() => {
1810773
1810773
  static parseOracleToJSON(_) {
1810774
1810774
  return parse(_, "oracle");
1810775
1810775
  }
1810776
- static parse(_, VY) {
1810777
- return new HY().parse(_, VY);
1810776
+ static parse(_, P) {
1810777
+ return new VY().parse(_, P);
1810778
+ }
1810779
+ getDbmlSource(_) {
1810780
+ return _ = typeof _ == "string" ? Filepath$1.from(_) : _, this.layout.getSource(_);
1810781
+ }
1810782
+ setDbmlSource(_, VY) {
1810783
+ _ = typeof _ == "string" ? Filepath$1.from(_) : _, VY === void 0 ? this.layout.deleteSource(_) : this.layout.setSource(_, VY);
1810784
+ }
1810785
+ deleteDbmlSource(_) {
1810786
+ _ = typeof _ == "string" ? Filepath$1.from(_) : _, this.layout.deleteSource(_);
1810787
+ }
1810788
+ clearDbmlSource() {
1810789
+ this.layout.clearSource();
1810790
+ }
1810791
+ parseDbmlProject(_) {
1810792
+ _ = typeof _ == "string" ? Filepath$1.from(_) : _;
1810793
+ let P = this.DBMLCompiler.interpretFile(_), HY = convertDbmlParserError(P.getErrors());
1810794
+ if (HY.length > 0) throw CompilerError.create(HY);
1810795
+ return VY.parseJSONToDatabase(P.getValue() || {});
1810778
1810796
  }
1810779
1810797
  parse(_, P) {
1810780
1810798
  try {
1810781
- let UY = {};
1810799
+ let HY = {};
1810782
1810800
  switch (P) {
1810783
1810801
  case "mysql":
1810784
- UY = HY.parseMySQLToJSONv2(_);
1810802
+ HY = VY.parseMySQLToJSONv2(_);
1810785
1810803
  break;
1810786
1810804
  case "mysqlLegacy":
1810787
- UY = HY.parseMySQLToJSON(_);
1810805
+ HY = VY.parseMySQLToJSON(_);
1810788
1810806
  break;
1810789
1810807
  case "postgres":
1810790
- UY = HY.parsePostgresToJSONv2(_);
1810808
+ HY = VY.parsePostgresToJSONv2(_);
1810791
1810809
  break;
1810792
1810810
  case "snowflake":
1810793
- UY = HY.parseSnowflakeToJSON(_);
1810811
+ HY = VY.parseSnowflakeToJSON(_);
1810794
1810812
  break;
1810795
1810813
  case "postgresLegacy":
1810796
- UY = HY.parsePostgresToJSON(_);
1810814
+ HY = VY.parsePostgresToJSON(_);
1810797
1810815
  break;
1810798
1810816
  case "dbml":
1810799
- UY = HY.parseDBMLToJSON(_);
1810817
+ HY = VY.parseDBMLToJSON(_);
1810800
1810818
  break;
1810801
- case "dbmlv2": {
1810802
- this.layout.setSource(DEFAULT_ENTRY, _);
1810803
- let P = convertDbmlParserError(this.DBMLCompiler.parse.errors(DEFAULT_ENTRY));
1810804
- if (P.length > 0) throw CompilerError.create(P);
1810805
- UY = this.DBMLCompiler.parse.rawDb(DEFAULT_ENTRY);
1810819
+ case "dbmlv2":
1810820
+ HY = VY.parseDBMLToJSONv2(_);
1810806
1810821
  break;
1810807
- }
1810808
1810822
  case "schemarb":
1810809
- UY = HY.parseSchemaRbToJSON(_);
1810823
+ HY = VY.parseSchemaRbToJSON(_);
1810810
1810824
  break;
1810811
1810825
  case "mssqlLegacy":
1810812
- UY = HY.parseMSSQLToJSON(_);
1810826
+ HY = VY.parseMSSQLToJSON(_);
1810813
1810827
  break;
1810814
1810828
  case "mssql":
1810815
- UY = HY.parseMSSQLToJSONv2(_);
1810829
+ HY = VY.parseMSSQLToJSONv2(_);
1810816
1810830
  break;
1810817
1810831
  case "oracle":
1810818
- UY = HY.parseOracleToJSON(_);
1810832
+ HY = VY.parseOracleToJSON(_);
1810819
1810833
  break;
1810820
1810834
  case "json":
1810821
- UY = typeof _ == "object" ? _ : JSON.parse(_);
1810835
+ HY = typeof _ == "object" ? _ : JSON.parse(_);
1810822
1810836
  break;
1810823
1810837
  default: break;
1810824
1810838
  }
1810825
- return HY.parseJSONToDatabase(UY);
1810839
+ return VY.parseJSONToDatabase(HY);
1810826
1810840
  } catch (_) {
1810827
1810841
  throw CompilerError.create(_);
1810828
1810842
  }
@@ -1810967,17 +1810981,17 @@ var import_default = {
1810967
1810981
  import: _import,
1810968
1810982
  generateDbml
1810969
1810983
  };
1810970
- function renameTable(HY, UY, WY) {
1810984
+ function renameTable(VY, HY, UY) {
1810971
1810985
  let GY = new MemoryProjectLayout();
1810972
- return GY.setSource(DEFAULT_ENTRY, WY), new Compiler(GY).renameTable(DEFAULT_ENTRY, HY, UY).get(DEFAULT_ENTRY.absolute) ?? WY;
1810986
+ return GY.setSource(DEFAULT_ENTRY$1, UY), new Compiler(GY).renameTable(DEFAULT_ENTRY$1, VY, HY).get(DEFAULT_ENTRY$1.absolute) ?? UY;
1810973
1810987
  }
1810974
- function syncDiagramView(HY, UY, WY) {
1810988
+ function syncDiagramView(VY, HY, UY) {
1810975
1810989
  let GY = new MemoryProjectLayout();
1810976
- return GY.setSource(DEFAULT_ENTRY, HY), new Compiler(GY).syncDiagramView(DEFAULT_ENTRY, UY, WY);
1810990
+ return GY.setSource(DEFAULT_ENTRY$1, VY), new Compiler(GY).syncDiagramView(DEFAULT_ENTRY$1, HY, UY);
1810977
1810991
  }
1810978
- function findDiagramViewBlocks(HY) {
1810979
- let UY = new MemoryProjectLayout();
1810980
- return UY.setSource(DEFAULT_ENTRY, HY), new Compiler(UY).findDiagramViewBlocks(DEFAULT_ENTRY);
1810992
+ function findDiagramViewBlocks(VY) {
1810993
+ let HY = new MemoryProjectLayout();
1810994
+ return HY.setSource(DEFAULT_ENTRY$1, VY), new Compiler(HY).findDiagramViewBlocks(DEFAULT_ENTRY$1);
1810981
1810995
  }
1810982
- const VERSION = "8.0.0-prerelease.1";
1810983
- 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 };
1810996
+ const VERSION = "8.1.0";
1810997
+ export { CompilerError, DEFAULT_ENTRY, Filepath, 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 };
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": "8.0.0-prerelease.1",
4
+ "version": "8.1.0",
5
5
  "description": "> TODO: description",
6
6
  "author": "Holistics <dev@holistics.io>",
7
7
  "license": "Apache-2.0",
@@ -46,6 +46,7 @@
46
46
  "lint:fix": "eslint --fix ."
47
47
  },
48
48
  "dependencies": {
49
+ "@dbml/parse": "^8.1.0",
49
50
  "antlr4": "^4.13.1",
50
51
  "lodash": "^4.18.1",
51
52
  "lodash-es": "^4.18.1",
@@ -53,13 +54,10 @@
53
54
  "parsimmon": "^1.13.0",
54
55
  "pluralize": "^8.0.0"
55
56
  },
56
- "peerDependencies": {
57
- "@dbml/parse": "^8.0.0-prerelease.1"
58
- },
59
57
  "devDependencies": {
60
58
  "bluebird": "^3.5.5"
61
59
  },
62
- "gitHead": "e5213e867f3820449fe1307bcd3f73e866f8e79f",
60
+ "gitHead": "deb659b7650a4204ad0a82e9d4e5e94f1d565465",
63
61
  "engines": {
64
62
  "node": ">=16"
65
63
  }
package/types/index.d.ts CHANGED
@@ -40,5 +40,43 @@ export {
40
40
  tryExtractEnum,
41
41
  addDoubleQuoteIfNeeded,
42
42
  formatRecordValue,
43
+ DEFAULT_ENTRY,
44
+ Filepath,
45
+ } from '@dbml/parse';
46
+
47
+ // Re-export types
48
+ export type {
49
+ Database,
50
+ MasterDatabase,
51
+ Table,
52
+ Note,
53
+ Column,
54
+ ColumnType,
55
+ Index,
56
+ Check,
57
+ InlineRef,
58
+ Ref,
59
+ RefEndpointPair,
60
+ RefEndpoint,
61
+ RelationCardinality,
62
+ Enum,
63
+ EnumField,
64
+ TableGroup,
65
+ TableGroupField,
66
+ Alias,
67
+ AliasKind,
68
+ TablePartial,
69
+ TablePartialInjection,
70
+ RecordValue,
71
+ RecordValueType,
72
+ TableRecord,
73
+ Project,
74
+ SchemaElement,
75
+ TokenPosition,
76
+ ElementRef,
77
+ DiagramView,
78
+ FilterConfig,
79
+ DiagramViewSyncOperation,
80
+ DiagramViewBlock,
81
+ TextEdit,
43
82
  } from '@dbml/parse';
44
- export type { DiagramView, DiagramViewSyncOperation, DiagramViewBlock, FilterConfig, TextEdit } from '@dbml/parse';
@@ -13,7 +13,7 @@ import { NormalizedIndexColumnIdMap } from './indexColumn';
13
13
  import { NormalizedIndexIdMap } from './indexes';
14
14
  import { NormalizedCheckIdMap } from './check';
15
15
  import TablePartial, { NormalizedTablePartialIdMap } from './tablePartial';
16
- import { TokenPosition } from '@dbml/parse';
16
+ import { TokenPosition, DiagramView } from '@dbml/parse';
17
17
  export interface Project {
18
18
  note: RawNote;
19
19
  database_type: string;
@@ -60,6 +60,7 @@ export interface RawDatabase {
60
60
  project: Project;
61
61
  records: RawTableRecord[];
62
62
  tablePartials: TablePartial[];
63
+ diagramViews: DiagramView[];
63
64
  }
64
65
 
65
66
  declare class Database extends Element {
@@ -72,6 +73,7 @@ declare class Database extends Element {
72
73
  databaseType: string;
73
74
  name: string;
74
75
  records: TableRecord[];
76
+ diagramViews: DiagramView[];
75
77
  id: number;
76
78
  constructor({ schemas, tables, enums, refs, tableGroups, project, records }: RawDatabase);
77
79
  generateId(): void;
@@ -1,3 +1,5 @@
1
+ import type { Filepath } from '@dbml/parse';
2
+
1
3
  export interface Token {
2
4
  end: {
3
5
  column: number;
@@ -9,6 +11,7 @@ export interface Token {
9
11
  line: number;
10
12
  offset: number;
11
13
  };
14
+ filepath?: Filepath;
12
15
  }
13
16
 
14
17
  export interface RawNote {
@@ -1,4 +1,4 @@
1
- import { Compiler, MemoryProjectLayout } from '@dbml/parse';
1
+ import { Compiler, Filepath, MemoryProjectLayout } from '@dbml/parse';
2
2
  import Database, { RawDatabase } from '../model_structure/database';
3
3
 
4
4
  export declare type ParseFormat = 'json'
@@ -11,7 +11,6 @@ export declare type ParseFormat = 'json'
11
11
  | 'oracle';
12
12
 
13
13
  declare class Parser {
14
- public layout: MemoryProjectLayout;
15
14
  public DBMLCompiler: Compiler;
16
15
  constructor();
17
16
  static parseJSONToDatabase(rawDatabase: RawDatabase): Database;
@@ -32,6 +31,11 @@ declare class Parser {
32
31
  static parseOracleToJSON(str: string): RawDatabase;
33
32
  static parse(str: string, format: ParseFormat): Database;
34
33
  static parse(str: RawDatabase, format: 'json'): Database;
34
+ getDbmlSource(filepath: Filepath | string): string | undefined;
35
+ setDbmlSource(filepath: Filepath | string, source: string | undefined): void;
36
+ deleteDbmlSource(filepath: Filepath | string): void;
37
+ clearDbmlSource(): void;
38
+ parseDbmlProject(entrypoint: Filepath | string): Database;
35
39
  parse(str: string, format: ParseFormat): Database;
36
40
  parse(str: RawDatabase, format: 'json'): Database;
37
41
  }