@dbml/core 8.3.1-custom-metadata.1 → 8.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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 ${Dp(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 ${Dp(i,t)?`"${i.name}".`:``}"${r.name}" IS '${r.note.replace(/'/g,`''`)}'`;break;case`column`:{let a=t.fields[e.fieldId];n+=` COLUMN ${Dp(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 Dp(a,t)&&n.schemas.push(`CREATE SCHEMA "${a.name}";\n`),dc(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];dc(o)||n.tables.push(...e.exportTables(o,t,i));let c=Ai(o.map(e=>t.tables[e].indexIds));dc(c)||n.indexes.push(...e.exportIndexes(c,t));let l=Ai(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 dc(l)||n.comments.push(...e.exportComments(l,t)),dc(s)||n.refs.push(...e.exportRefs(s,t,r)),n},a),s=e.exportRecords(t),c=dc(s)?[]:[`-- Defer constraint checking for INSERT`,`BEGIN;`,`SET CONSTRAINTS ALL DEFERRED;`,``,...s,``,`SET CONSTRAINTS ALL IMMEDIATE;`,`COMMIT;`];return oo(o.schemas,o.enums,o.tables,o.indexes,o.comments,o.refs,c).join(`
30
- `)}},jee=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}},Mee=class extends Error{constructor(e,t={start:{line:1,column:1}}){super(e),this.location=t,this.error=`error`}},Mp=class{constructor(e){this.token=e}bind(e){this.selection=e}error(e){throw new Mee(e,this.token)}};Ec();var Nee=class extends Mp{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?T(n,`value`,n):null,this.noteToken=n?T(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()}}};Ec();var Pee=class extends Mp{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?T(r,`value`,r):null,this.noteToken=r?T(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 Nee({...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 ${jp(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 ${jp(e.table.schema)?`"${e.table.schema.name}".`:``}"${e.table.name}"."${e.name}" already associated with enum ${jp(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))}},Fee=class extends Mp{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 ${Dee(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 ${jp(t.schema)?`"${t.schema.name}".`:``}"${t.name}"`)}}n.forEach(e=>{let n=t.findField(e);n||this.error(`Can't find field ${jp(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()}}};function Iee(e,t){return e[0].equals(t[0])&&e[1].equals(t[1])}var Np=class extends Mp{constructor({name:e,color:t,endpoints:n,onDelete:r,onUpdate:i,inactive:a,token:o,schema:s={},injectedPartial:c=null}={}){super(o),this.name=e,this.color=t,this.onDelete=r,this.onUpdate=i,this.inactive=a,this.endpoints=[],this.schema=s,this.injectedPartial=c,this.dbState=this.schema.dbState,this.generateId(),this.processEndpoints(n)}generateId(){this.id=this.dbState.generateId(`refId`)}processEndpoints(e){e.forEach(e=>{this.endpoints.push(new Fee({...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 Iee(this.endpoints,e.endpoints)||Iee(this.endpoints,e.endpoints.slice().reverse())}export(){return{...this.shallowExport(),...this.exportChild()}}shallowExport(){return{name:this.name,color:this.color,onDelete:this.onDelete,onUpdate:this.onUpdate,inactive:this.inactive,injectedPartialId:this.injectedPartial?.id}}exportChild(){return{endpoints:this.endpoints.map(e=>e.export())}}exportChildIds(){return{endpointIds:this.endpoints.map(e=>e.id)}}exportParentIds(){return{schemaId:this.schema.id}}normalize(e){e.refs[this.id]={id:this.id,...this.shallowExport(),...this.exportChildIds(),...this.exportParentIds()},this.endpoints.forEach(t=>t.normalize(e))}},Pp=class extends Mp{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()}}};Ec();var Lee=class extends Mp{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,metadata:m={}}={}){super(i),this.metadata=m,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?T(o,`value`,o):null,this.noteToken=o?T(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),metadata:this.metadata}}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 Pp({...e,table:this.table,column:this}))})}},Ree=class extends Mp{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()}}},zee=class extends Mp{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 Ree({...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))}};Ec();var Bee=class extends Mp{constructor({name:e,alias:t,note:n,fields:r=[],indexes:i=[],checks:a=[],schema:o={},token:s,headerColor:c,noteToken:l=null,partials:u=[],metadata:d}={}){super(s),this.metadata=d??{},this.name=e,this.alias=t,this.note=n?T(n,`value`,n):null,this.noteToken=n?T(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 Lee({...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 ${jp(this.schema)?`"${this.schema.name}".`:``}"${this.name}"`)}processIndexes(e){e.forEach(e=>{this.pushIndex(new zee({...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 ${jp(this.schema)?`"${this.schema.name}".`:``}"${this.name}"`)})}processChecks(e){e.forEach(e=>{this.pushCheck(new Pp({...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;gc(this.note)||t.add(`note`),gc(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 Lee({...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`)&&!gc(r.note)&&(this.noteToken=null,this.note=r.note,t.add(`note`)),!t.has(`headerColor`)&&!gc(r.headerColor)&&(this.headerColor=r.headerColor,t.add(`headerColor`)),r.indexes.forEach(e=>{this.indexes.push(new zee({...e,table:this,injectedPartial:r}))}),r.checks.forEach(e=>{this.checks.push(new Pp({...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),metadata:this.metadata}}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))}};Ec();var Vee=class extends Mp{constructor({name:e,token:t,tables:n=[],schema:r={},note:i,color:a,noteToken:o=null,metadata:s={}}){super(t),this.metadata=s,this.name=e,this.tables=[],this.schema=r,this.dbState=this.schema.dbState,this.note=i?T(i,`value`,i):null,this.noteToken=i?T(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 ${jp(e.schema)?`"${e.schema.name}".`:``}.${e.name} is already in the group`),e.group&&this.error(`Table ${jp(e.schema)?`"${e.schema.name}".`:``}.${e.name} is already in group ${jp(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,metadata:this.metadata}}normalize(e){e.tableGroups[this.id]={id:this.id,...this.shallowExport(),...this.exportChildIds(),...this.exportParentIds()}}};Ec();var Hee=class extends Mp{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?T(n,`value`,n):null,this.noteToken=n?T(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 Bee({...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 ${jp(this)?`"${this.name}".`:``}"${e.name}" existed`)}findTable(e){return this.tables.find(t=>t.name===e)}processEnums(e){e.forEach(e=>{this.pushEnum(new Pee({...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 ${jp(this)?`"${this.name}".`:``}"${e.name}" existed`)}processRefs(e){e.forEach(e=>{this.pushRef(new Np({...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 Vee({...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 ${jp(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))}},Uee=class extends Mp{constructor({name:e,content:t,color:n,token:r,database:i={},metadata:a={}}={}){super(r),this.metadata=a,this.name=e,this.content=t,this.color=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,color:this.color,metadata:this.metadata}}normalize(e){e.notes[this.id]={id:this.id,...this.export()}}};Ec();var Wee=class extends Mp{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?T(t,`value`,t):null,this.noteToken=t?T(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()}}};Ec();var Fp=class extends Mp{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 jee,this.generateId(),this.hasDefaultSchema=!1,this.schemas=[],this.notes=[],this.note=o.note?T(o,`note.value`,o.note):null,this.noteToken=o.note?T(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,_ee),this.processSchemaElements(t,hee),this.linkRecordsToTables(),this.processSchemaElements(n,gee),this.processSchemaElements(i,`ref`),this.processSchemaElements(a,vee),this.injectedRawRefs.forEach(e=>{let t=this.findOrCreateSchema(Ep),n=new Np({...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 Uee({...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 Wee({...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 Hee({...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(Ep),t){case hee:n.pushTable(new Bee({...e,schema:n}));break;case _ee:n.pushEnum(new Pee({...e,schema:n}));break;case vee:n.pushTableGroup(new Vee({...e,schema:n}));break;case`ref`:n.pushRef(new Np({...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 Hee({name:e,note:{value:e===`public`?`Default ${fa(Ep)} 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}},Gee=class{static export(e,t){let{isNormalized:n}=t;return!n&&e instanceof Fp?JSON.stringify(e.export(),null,2):JSON.stringify(e,null,2)}};Ec();var Kee=class e{static exportRecords(e){let t=Object.values(e.records||{});return dc(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
+ `)}},jee=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}},Mee=class extends Error{constructor(e,t={start:{line:1,column:1}}){super(e),this.location=t,this.error=`error`}},Mp=class{constructor(e){this.token=e}bind(e){this.selection=e}error(e){throw new Mee(e,this.token)}};Ec();var Nee=class extends Mp{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?T(n,`value`,n):null,this.noteToken=n?T(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()}}};Ec();var Pee=class extends Mp{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?T(r,`value`,r):null,this.noteToken=r?T(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 Nee({...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 ${jp(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 ${jp(e.table.schema)?`"${e.table.schema.name}".`:``}"${e.table.name}"."${e.name}" already associated with enum ${jp(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))}},Fee=class extends Mp{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 ${Dee(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 ${jp(t.schema)?`"${t.schema.name}".`:``}"${t.name}"`)}}n.forEach(e=>{let n=t.findField(e);n||this.error(`Can't find field ${jp(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()}}};function Iee(e,t){return e[0].equals(t[0])&&e[1].equals(t[1])}var Np=class extends Mp{constructor({name:e,color:t,endpoints:n,onDelete:r,onUpdate:i,inactive:a,token:o,schema:s={},injectedPartial:c=null}={}){super(o),this.name=e,this.color=t,this.onDelete=r,this.onUpdate=i,this.inactive=a,this.endpoints=[],this.schema=s,this.injectedPartial=c,this.dbState=this.schema.dbState,this.generateId(),this.processEndpoints(n)}generateId(){this.id=this.dbState.generateId(`refId`)}processEndpoints(e){e.forEach(e=>{this.endpoints.push(new Fee({...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 Iee(this.endpoints,e.endpoints)||Iee(this.endpoints,e.endpoints.slice().reverse())}export(){return{...this.shallowExport(),...this.exportChild()}}shallowExport(){return{name:this.name,color:this.color,onDelete:this.onDelete,onUpdate:this.onUpdate,inactive:this.inactive,injectedPartialId:this.injectedPartial?.id}}exportChild(){return{endpoints:this.endpoints.map(e=>e.export())}}exportChildIds(){return{endpointIds:this.endpoints.map(e=>e.id)}}exportParentIds(){return{schemaId:this.schema.id}}normalize(e){e.refs[this.id]={id:this.id,...this.shallowExport(),...this.exportChildIds(),...this.exportParentIds()},this.endpoints.forEach(t=>t.normalize(e))}},Pp=class extends Mp{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()}}};Ec();var Lee=class extends Mp{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?T(o,`value`,o):null,this.noteToken=o?T(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 Pp({...e,table:this.table,column:this}))})}},Ree=class extends Mp{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()}}},zee=class extends Mp{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 Ree({...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))}};Ec();var Bee=class extends Mp{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?T(n,`value`,n):null,this.noteToken=n?T(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 Lee({...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 ${jp(this.schema)?`"${this.schema.name}".`:``}"${this.name}"`)}processIndexes(e){e.forEach(e=>{this.pushIndex(new zee({...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 ${jp(this.schema)?`"${this.schema.name}".`:``}"${this.name}"`)})}processChecks(e){e.forEach(e=>{this.pushCheck(new Pp({...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;gc(this.note)||t.add(`note`),gc(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 Lee({...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`)&&!gc(r.note)&&(this.noteToken=null,this.note=r.note,t.add(`note`)),!t.has(`headerColor`)&&!gc(r.headerColor)&&(this.headerColor=r.headerColor,t.add(`headerColor`)),r.indexes.forEach(e=>{this.indexes.push(new zee({...e,table:this,injectedPartial:r}))}),r.checks.forEach(e=>{this.checks.push(new Pp({...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))}};Ec();var Vee=class extends Mp{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?T(i,`value`,i):null,this.noteToken=i?T(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 ${jp(e.schema)?`"${e.schema.name}".`:``}.${e.name} is already in the group`),e.group&&this.error(`Table ${jp(e.schema)?`"${e.schema.name}".`:``}.${e.name} is already in group ${jp(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()}}};Ec();var Hee=class extends Mp{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?T(n,`value`,n):null,this.noteToken=n?T(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 Bee({...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 ${jp(this)?`"${this.name}".`:``}"${e.name}" existed`)}findTable(e){return this.tables.find(t=>t.name===e)}processEnums(e){e.forEach(e=>{this.pushEnum(new Pee({...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 ${jp(this)?`"${this.name}".`:``}"${e.name}" existed`)}processRefs(e){e.forEach(e=>{this.pushRef(new Np({...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 Vee({...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 ${jp(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))}},Uee=class extends Mp{constructor({name:e,content:t,color:n,token:r,database:i={}}={}){super(r),this.name=e,this.content=t,this.color=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,color:this.color}}normalize(e){e.notes[this.id]={id:this.id,...this.export()}}};Ec();var Wee=class extends Mp{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?T(t,`value`,t):null,this.noteToken=t?T(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()}}};Ec();var Fp=class extends Mp{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 jee,this.generateId(),this.hasDefaultSchema=!1,this.schemas=[],this.notes=[],this.note=o.note?T(o,`note.value`,o.note):null,this.noteToken=o.note?T(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,_ee),this.processSchemaElements(t,hee),this.linkRecordsToTables(),this.processSchemaElements(n,gee),this.processSchemaElements(i,`ref`),this.processSchemaElements(a,vee),this.injectedRawRefs.forEach(e=>{let t=this.findOrCreateSchema(Ep),n=new Np({...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 Uee({...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 Wee({...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 Hee({...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(Ep),t){case hee:n.pushTable(new Bee({...e,schema:n}));break;case _ee:n.pushEnum(new Pee({...e,schema:n}));break;case vee:n.pushTableGroup(new Vee({...e,schema:n}));break;case`ref`:n.pushRef(new Np({...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 Hee({name:e,note:{value:e===`public`?`Default ${fa(Ep)} 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}},Gee=class{static export(e,t){let{isNormalized:n}=t;return!n&&e instanceof Fp?JSON.stringify(e.export(),null,2):JSON.stringify(e,null,2)}};Ec();var Kee=class e{static exportRecords(e){let t=Object.values(e.records||{});return dc(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 ${Dp(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 ${Dp(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}})),UNe=u(cAe()),WNe=u(zNe()),GNe=u(BNe()),KNe=u(VNe()),qNe=u(HNe()),JNe=class e{constructor(){this.layout=new f.MemoryProjectLayout,this.DBMLCompiler=new f.Compiler(this.layout)}static parseJSONToDatabase(e){return new Fp(e)}static parseMySQLToJSONv2(e){return Z7(e,`mysql`)}static parseMySQLToJSON(e){return GNe.default.parse(e)}static parsePostgresToJSONv2(e){return Z7(e,`postgres`)}static parsePostgresToJSON(e){return KNe.default.parse(e)}static parseDBMLToJSONv2(e){let t=new f.MemoryProjectLayout;t.setSource(f.DEFAULT_ENTRY,e);let n=new f.Compiler(t),r=YNe(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 UNe.default.parse(e)}static parseSchemaRbToJSON(e){return qNe.default.parse(e)}static parseMSSQLToJSON(e){return WNe.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=YNe(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)}}};function YNe(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 XNe(e,t,n={isNormalized:!0,includeRecords:!0}){let r=ZNe(n),i=new JNe().parse(e,`dbmlv2`);return $9.export(i.normalize(),t,r)}function ZNe(e={}){if(typeof e==`boolean`)return{isNormalized:e,includeRecords:!0};let{isNormalized:t=!0,includeRecords:n=!0}=e;return{isNormalized:t,includeRecords:n}}var QNe={export:XNe},$9=class{static export(e,t,n={isNormalized:!0,includeRecords:!0}){let{isNormalized:r,includeRecords:i}=ZNe(n),a=e instanceof Fp?e.normalize():e,o=``;switch(t){case`dbml`:o=Tee.export(a,{includeRecords:i});break;case`mysql`:o=Eee.export(a);break;case`postgres`:o=Aee.export(a);break;case`json`:o=Gee.export(e,{isNormalized:r});break;case`mssql`:o=Kee.export(a);break;case`oracle`:o=qee.export(a);break;default:break}return o}},$Ne=e=>new Fp(e),ePe=e=>e.map(e=>{let{name:t,endpoints:n,onDelete:r,onUpdate:i}=e;return new Bke({name:t,endpoints:n.map(e=>new M7(e)),onDelete:r,onUpdate:i}).toJSON()}),tPe=e=>e.map(e=>{let{name:t,schemaName:n,values:r}=e;return new N7({name:t,schemaName:n,values:r})}),nPe=(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})}),rPe=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})}),iPe=(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:nPe(t[c],l),indexes:rPe(n[c]||[]),note:s,checks:r[c]})}),aPe=e=>{let{tables:t,fields:n,indexes:r,refs:i,enums:a,tableConstraints:o,checks:s}=e;try{let e=iPe(t,n,r,s,o),c=tPe(a);return $Ne({schemas:[],tables:e,refs:ePe(i),enums:c,tableGroups:[],aliases:[],project:{}})}catch(e){throw Error(e)}};function oPe(e,t,n={includeRecords:!0}){let{includeRecords:r=!0}=n,i=new JNe().parse(e,t);return $9.export(i.normalize(),`dbml`,{includeRecords:r})}function sPe(e){let t=aPe(e);return $9.export(t.normalize(),`dbml`)}var cPe={import:oPe,generateDbml:sPe};function lPe(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 uPe(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 dPe(e){let t=new f.MemoryProjectLayout;return t.setSource(f.DEFAULT_ENTRY,e),new f.Compiler(t).findDiagramViewBlocks(f.DEFAULT_ENTRY)}var fPe=`8.3.1-custom-metadata.1`;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=JNe,exports.VERSION=fPe,Object.defineProperty(exports,"addDoubleQuoteIfNeeded",{enumerable:!0,get:function(){return f.addDoubleQuoteIfNeeded}}),Object.defineProperty(exports,"dbmlMonarchTokensProvider",{enumerable:!0,get:function(){return f.dbmlMonarchTokensProvider}}),exports.exporter=QNe,exports.findDiagramViewBlocks=dPe,Object.defineProperty(exports,"formatRecordValue",{enumerable:!0,get:function(){return f.formatRecordValue}}),exports.importer=cPe,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=lPe,exports.syncDiagramView=uPe,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}})),UNe=u(cAe()),WNe=u(zNe()),GNe=u(BNe()),KNe=u(VNe()),qNe=u(HNe()),JNe=class e{constructor(){this.layout=new f.MemoryProjectLayout,this.DBMLCompiler=new f.Compiler(this.layout)}static parseJSONToDatabase(e){return new Fp(e)}static parseMySQLToJSONv2(e){return Z7(e,`mysql`)}static parseMySQLToJSON(e){return GNe.default.parse(e)}static parsePostgresToJSONv2(e){return Z7(e,`postgres`)}static parsePostgresToJSON(e){return KNe.default.parse(e)}static parseDBMLToJSONv2(e){let t=new f.MemoryProjectLayout;t.setSource(f.DEFAULT_ENTRY,e);let n=new f.Compiler(t),r=YNe(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 UNe.default.parse(e)}static parseSchemaRbToJSON(e){return qNe.default.parse(e)}static parseMSSQLToJSON(e){return WNe.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=YNe(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)}}};function YNe(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 XNe(e,t,n={isNormalized:!0,includeRecords:!0}){let r=ZNe(n),i=new JNe().parse(e,`dbmlv2`);return $9.export(i.normalize(),t,r)}function ZNe(e={}){if(typeof e==`boolean`)return{isNormalized:e,includeRecords:!0};let{isNormalized:t=!0,includeRecords:n=!0}=e;return{isNormalized:t,includeRecords:n}}var QNe={export:XNe},$9=class{static export(e,t,n={isNormalized:!0,includeRecords:!0}){let{isNormalized:r,includeRecords:i}=ZNe(n),a=e instanceof Fp?e.normalize():e,o=``;switch(t){case`dbml`:o=Tee.export(a,{includeRecords:i});break;case`mysql`:o=Eee.export(a);break;case`postgres`:o=Aee.export(a);break;case`json`:o=Gee.export(e,{isNormalized:r});break;case`mssql`:o=Kee.export(a);break;case`oracle`:o=qee.export(a);break;default:break}return o}},$Ne=e=>new Fp(e),ePe=e=>e.map(e=>{let{name:t,endpoints:n,onDelete:r,onUpdate:i}=e;return new Bke({name:t,endpoints:n.map(e=>new M7(e)),onDelete:r,onUpdate:i}).toJSON()}),tPe=e=>e.map(e=>{let{name:t,schemaName:n,values:r}=e;return new N7({name:t,schemaName:n,values:r})}),nPe=(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})}),rPe=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})}),iPe=(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:nPe(t[c],l),indexes:rPe(n[c]||[]),note:s,checks:r[c]})}),aPe=e=>{let{tables:t,fields:n,indexes:r,refs:i,enums:a,tableConstraints:o,checks:s}=e;try{let e=iPe(t,n,r,s,o),c=tPe(a);return $Ne({schemas:[],tables:e,refs:ePe(i),enums:c,tableGroups:[],aliases:[],project:{}})}catch(e){throw Error(e)}};function oPe(e,t,n={includeRecords:!0}){let{includeRecords:r=!0}=n,i=new JNe().parse(e,t);return $9.export(i.normalize(),`dbml`,{includeRecords:r})}function sPe(e){let t=aPe(e);return $9.export(t.normalize(),`dbml`)}var cPe={import:oPe,generateDbml:sPe};function lPe(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 uPe(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 dPe(e){let t=new f.MemoryProjectLayout;return t.setSource(f.DEFAULT_ENTRY,e),new f.Compiler(t).findDiagramViewBlocks(f.DEFAULT_ENTRY)}var fPe=`8.3.1`;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=JNe,exports.VERSION=fPe,Object.defineProperty(exports,"addDoubleQuoteIfNeeded",{enumerable:!0,get:function(){return f.addDoubleQuoteIfNeeded}}),Object.defineProperty(exports,"dbmlMonarchTokensProvider",{enumerable:!0,get:function(){return f.dbmlMonarchTokensProvider}}),exports.exporter=QNe,exports.findDiagramViewBlocks=dPe,Object.defineProperty(exports,"formatRecordValue",{enumerable:!0,get:function(){return f.formatRecordValue}}),exports.importer=cPe,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=lPe,exports.syncDiagramView=uPe,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
@@ -5640,8 +5640,8 @@ var Hp = class extends Vp {
5640
5640
  //#region src/model_structure/field.js
5641
5641
  el();
5642
5642
  var ate = class extends Vp {
5643
- 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, metadata: m = {} } = {}) {
5644
- super(i), this.metadata = m, 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 ? Qi(o, "value", o) : null, this.noteToken = o ? Qi(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);
5643
+ 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 } = {}) {
5644
+ 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 ? Qi(o, "value", o) : null, this.noteToken = o ? Qi(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);
5645
5645
  }
5646
5646
  generateId() {
5647
5647
  this.id = this.dbState.generateId("fieldId");
@@ -5687,8 +5687,7 @@ var ate = class extends Vp {
5687
5687
  dbdefault: this.dbdefault,
5688
5688
  increment: this.increment,
5689
5689
  injectedPartialId: this.injectedPartial?.id ?? null,
5690
- checkIds: this.checks.map((e) => e.id),
5691
- metadata: this.metadata
5690
+ checkIds: this.checks.map((e) => e.id)
5692
5691
  };
5693
5692
  }
5694
5693
  normalize(e) {
@@ -5793,8 +5792,8 @@ var ate = class extends Vp {
5793
5792
  //#region src/model_structure/table.js
5794
5793
  el();
5795
5794
  var cte = class extends Vp {
5796
- constructor({ name: e, alias: t, note: n, fields: r = [], indexes: i = [], checks: a = [], schema: o = {}, token: s, headerColor: c, noteToken: l = null, partials: u = [], metadata: d } = {}) {
5797
- super(s), this.metadata = d ?? {}, this.name = e, this.alias = t, this.note = n ? Qi(n, "value", n) : null, this.noteToken = n ? Qi(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);
5795
+ constructor({ name: e, alias: t, note: n, fields: r = [], indexes: i = [], checks: a = [], schema: o = {}, token: s, headerColor: c, noteToken: l = null, partials: u = [] } = {}) {
5796
+ super(s), this.name = e, this.alias = t, this.note = n ? Qi(n, "value", n) : null, this.noteToken = n ? Qi(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);
5798
5797
  }
5799
5798
  generateId() {
5800
5799
  this.id = this.dbState.generateId("tableId");
@@ -5935,8 +5934,7 @@ var cte = class extends Vp {
5935
5934
  note: this.note,
5936
5935
  headerColor: this.headerColor,
5937
5936
  partials: this.partials,
5938
- recordIds: this.records.map((e) => e.id),
5939
- metadata: this.metadata
5937
+ recordIds: this.records.map((e) => e.id)
5940
5938
  };
5941
5939
  }
5942
5940
  normalize(e) {
@@ -5952,8 +5950,8 @@ var cte = class extends Vp {
5952
5950
  //#region src/model_structure/tableGroup.js
5953
5951
  el();
5954
5952
  var lte = class extends Vp {
5955
- constructor({ name: e, token: t, tables: n = [], schema: r = {}, note: i, color: a, noteToken: o = null, metadata: s = {} }) {
5956
- super(t), this.metadata = s, this.name = e, this.tables = [], this.schema = r, this.dbState = this.schema.dbState, this.note = i ? Qi(i, "value", i) : null, this.noteToken = i ? Qi(i, "token", o) : null, this.color = a, this.generateId(), this.processTables(n);
5953
+ constructor({ name: e, token: t, tables: n = [], schema: r = {}, note: i, color: a, noteToken: o = null }) {
5954
+ super(t), this.name = e, this.tables = [], this.schema = r, this.dbState = this.schema.dbState, this.note = i ? Qi(i, "value", i) : null, this.noteToken = i ? Qi(i, "token", o) : null, this.color = a, this.generateId(), this.processTables(n);
5957
5955
  }
5958
5956
  generateId() {
5959
5957
  this.id = this.dbState.generateId("tableGroupId");
@@ -5992,8 +5990,7 @@ var lte = class extends Vp {
5992
5990
  return {
5993
5991
  name: this.name,
5994
5992
  note: this.note,
5995
- color: this.color,
5996
- metadata: this.metadata
5993
+ color: this.color
5997
5994
  };
5998
5995
  }
5999
5996
  normalize(e) {
@@ -6121,8 +6118,8 @@ var ute = class extends Vp {
6121
6118
  }, 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));
6122
6119
  }
6123
6120
  }, dte = class extends Vp {
6124
- constructor({ name: e, content: t, color: n, token: r, database: i = {}, metadata: a = {} } = {}) {
6125
- super(r), this.metadata = a, this.name = e, this.content = t, this.color = n, this.database = i, this.dbState = this.database.dbState, this.generateId();
6121
+ constructor({ name: e, content: t, color: n, token: r, database: i = {} } = {}) {
6122
+ super(r), this.name = e, this.content = t, this.color = n, this.database = i, this.dbState = this.database.dbState, this.generateId();
6126
6123
  }
6127
6124
  generateId() {
6128
6125
  this.id = this.dbState.generateId("noteId");
@@ -6131,8 +6128,7 @@ var ute = class extends Vp {
6131
6128
  return {
6132
6129
  name: this.name,
6133
6130
  content: this.content,
6134
- color: this.color,
6135
- metadata: this.metadata
6131
+ color: this.color
6136
6132
  };
6137
6133
  }
6138
6134
  normalize(e) {
@@ -1811301,6 +1811297,6 @@ function HPe(t) {
1811301
1811297
  }
1811302
1811298
  //#endregion
1811303
1811299
  //#region src/utils/version.ts
1811304
- var UPe = "8.3.1-custom-metadata.1";
1811300
+ var UPe = "8.3.1";
1811305
1811301
  //#endregion
1811306
1811302
  export { U7 as CompilerError, t as DEFAULT_ENTRY, r as Filepath, $9 as ModelExporter, Q9 as Parser, UPe as VERSION, o as addDoubleQuoteIfNeeded, c as dbmlMonarchTokensProvider, kPe as exporter, HPe as findDiagramViewBlocks, l as formatRecordValue, zPe as importer, d as isBinaryType, p as isBooleanType, h as isDateTimeType, _ as isFloatType, ee as isIntegerType, v as isNumericType, b as isSerialType, te as isStringType, BPe as renameTable, VPe as syncDiagramView, ne as tryExtractBoolean, re as tryExtractDateTime, ae as tryExtractEnum, oe as tryExtractInteger, se as tryExtractNumeric, ce as 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.3.1-custom-metadata.1",
4
+ "version": "8.3.1",
5
5
  "description": "> TODO: description",
6
6
  "author": "Holistics <dev@holistics.io>",
7
7
  "license": "Apache-2.0",
@@ -46,7 +46,7 @@
46
46
  "lint:fix": "eslint --fix ."
47
47
  },
48
48
  "dependencies": {
49
- "@dbml/parse": "^8.3.1-custom-metadata.1",
49
+ "@dbml/parse": "^8.3.1",
50
50
  "antlr4": "^4.13.1",
51
51
  "lodash": "^4.18.1",
52
52
  "lodash-es": "^4.18.1",
@@ -57,7 +57,7 @@
57
57
  "devDependencies": {
58
58
  "bluebird": "^3.5.5"
59
59
  },
60
- "gitHead": "152988f537ab87a9d8320bd41b478495c1acbb0c",
60
+ "gitHead": "b062d22d3608d8bb2969051721237cf5de28e069",
61
61
  "engines": {
62
62
  "node": ">=16"
63
63
  }
package/types/index.d.ts CHANGED
@@ -40,7 +40,6 @@ export {
40
40
  tryExtractEnum,
41
41
  addDoubleQuoteIfNeeded,
42
42
  formatRecordValue,
43
- dbmlMonarchTokensProvider,
44
43
  DEFAULT_ENTRY,
45
44
  Filepath,
46
45
  } from '@dbml/parse';
@@ -13,5 +13,5 @@ export default class DbState {
13
13
  fieldId: number;
14
14
  indexColumnId: number;
15
15
  tablePartialId: number;
16
- generateId(el: Exclude<keyof DbState, 'generateId'>): number;
16
+ generateId(el: string): number;
17
17
  }
@@ -21,8 +21,6 @@ export interface RawNote {
21
21
  token: Token;
22
22
  }
23
23
 
24
- export type Metadata = Record<string, unknown>;
25
-
26
24
  declare class Element {
27
25
  token: Token;
28
26
  id: number;
@@ -1,6 +1,6 @@
1
1
  import { NormalizedModel } from './database';
2
2
  import DbState from './dbState';
3
- import Element, { Token, RawNote, Metadata } from './element';
3
+ import Element, { Token, RawNote } from './element';
4
4
  import Endpoint from './endpoint';
5
5
  import Enum from './enum';
6
6
  import Table from './table';
@@ -85,7 +85,6 @@ declare class Field extends Element {
85
85
  increment: boolean;
86
86
  injectedPartialId?: number;
87
87
  checkIds: number[];
88
- metadata: Metadata;
89
88
  };
90
89
  normalize(model: NormalizedModel): void;
91
90
  }
@@ -111,7 +110,6 @@ export interface NormalizedField {
111
110
  enumId: number | null;
112
111
  injectedPartialId: number | null;
113
112
  checkIds: number[];
114
- metadata: Metadata;
115
113
  }
116
114
 
117
115
  export interface NormalizedFieldIdMap {
@@ -1,4 +1,4 @@
1
- import Element, { Token, Color, Metadata } from './element';
1
+ import Element, { Token, Color } from './element';
2
2
  import Database from './database';
3
3
  import DbState from './dbState';
4
4
  import { NormalizedModel } from './database';
@@ -25,7 +25,6 @@ declare class StickyNote extends Element {
25
25
  name: string;
26
26
  content: string;
27
27
  color?: Color;
28
- metadata: Metadata;
29
28
  };
30
29
  normalize(model: NormalizedModel): void;
31
30
  }
@@ -34,7 +33,6 @@ export interface NormalizedNote {
34
33
  name: string;
35
34
  content: string;
36
35
  color?: Color;
37
- metadata: Metadata;
38
36
  }
39
37
 
40
38
  export interface NormalizedNoteIdMap {
@@ -1,4 +1,4 @@
1
- import Element, { RawNote, Token, Color, Metadata } from './element';
1
+ import Element, { RawNote, Token, Color } from './element';
2
2
  import Field from './field';
3
3
  import Index from './indexes';
4
4
  import Check from './check';
@@ -19,7 +19,6 @@ export interface RawTable {
19
19
  token: Token;
20
20
  headerColor: Color;
21
21
  partials: TablePartial[];
22
- metadata?: Metadata;
23
22
  }
24
23
 
25
24
  declare class Table extends Element {
@@ -118,7 +117,6 @@ declare class Table extends Element {
118
117
  headerColor: Color;
119
118
  partials: TablePartial[];
120
119
  recordIds: number[];
121
- metadata: Metadata;
122
120
  };
123
121
  normalize(model: NormalizedModel): void;
124
122
  }
@@ -136,7 +134,6 @@ export interface NormalizedTable {
136
134
  schemaId: number;
137
135
  groupId: number | null;
138
136
  partials: TablePartial[];
139
- metadata: Metadata;
140
137
  }
141
138
 
142
139
  export interface NormalizedTableIdMap {
@@ -1,6 +1,6 @@
1
1
  import { NormalizedModel } from './database';
2
2
  import DbState from './dbState';
3
- import Element, { RawNote, Token, Color, Metadata } from './element';
3
+ import Element, { RawNote, Token, Color } from './element';
4
4
  import Schema from './schema';
5
5
  import Table from './table';
6
6
 
@@ -52,7 +52,6 @@ declare class TableGroup extends Element {
52
52
  name: string;
53
53
  note: string;
54
54
  color: Color;
55
- metadata: Metadata;
56
55
  };
57
56
  normalize(model: NormalizedModel): void;
58
57
  }
@@ -63,7 +62,6 @@ export interface NormalizedTableGroup {
63
62
  color: Color;
64
63
  tableIds: number[];
65
64
  schemaId: number;
66
- metadata: Metadata;
67
65
  }
68
66
 
69
67
  export interface NormalizedTableGroupIdMap {