@dbml/core 9.0.0-alpha.1 → 9.0.0-alpha.3
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 +2 -2
- package/lib/index.mjs +13 -4
- package/package.json +3 -3
- package/types/index.d.ts +8 -0
- package/types/model_structure/dep.d.ts +6 -1
- package/types/transform/index.d.ts +14 -1
package/lib/index.cjs
CHANGED
|
@@ -31,7 +31,7 @@ Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});var e=Object.
|
|
|
31
31
|
`;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 ${Ep(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+=`;
|
|
32
32
|
`,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 ${Ep(i,t)?`"${i.name}".`:``}"${r.name}" IS '${r.note.replace(/'/g,`''`)}'`;break;case`column`:{let a=t.fields[e.fieldId];n+=` COLUMN ${Ep(i,t)?`"${i.name}".`:``}"${r.name}"."${a.name}" IS '${a.note.replace(/'/g,`''`)}'`;break}default:break}return n+=`;
|
|
33
33
|
`,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 Ep(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(`
|
|
34
|
-
`)}},jee=class{constructor(){this.dbId=1,this.schemaId=1,this.enumId=1,this.tableGroupId=1,this.refId=1,this.depId=1,this.depEdgeId=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)}},Nee=class extends Mp{constructor({upstream:e,downstream:t,token:n,dep:r}){super(n),this.upstream={schemaName:e.schemaName??null,tableName:e.tableName,fieldNames:e.fieldNames??[]},this.downstream={schemaName:t.schemaName??null,tableName:t.tableName,fieldNames:t.fieldNames??[]},this.dep=r,this.dbState=this.dep.dbState,this.generateId();let i=this.dep.schema.database,a=this.resolveEndpoint(this.upstream,i),o=this.resolveEndpoint(this.downstream,i);this.upstreamTable=a.table,this.upstreamFields=a.fields,this.downstreamTable=o.table,this.downstreamFields=o.fields,this.upstreamFields.forEach(e=>e.pushDepEdge(this)),this.downstreamFields.forEach(e=>e.pushDepEdge(this))}resolveEndpoint(e,t){let n=e.schemaName||`public`,r=t.findTable(n,e.tableName);return r||this.error(`Can't find table ${jp(n)?`"${n}".`:``}"${e.tableName}" referenced in Dep edge`),{table:r,fields:(e.fieldNames??[]).map(e=>{let t=r.findField(e);return t||this.error(`Can't find field ${Ap(r.schema)?`"${r.schema.name}".`:``}"${e}" in table "${r.name}" referenced in Dep edge`),t})}}generateId(){this.id=this.dbState.generateId(`depEdgeId`)}export(){return this.shallowExport()}shallowExport(){return{upstream:this.upstream,downstream:this.downstream}}exportParentIds(){return{depId:this.dep.id,upstreamTableId:this.upstreamTable?this.upstreamTable.id:null,upstreamFieldIds:this.upstreamFields.map(e=>e.id),downstreamTableId:this.downstreamTable?this.downstreamTable.id:null,downstreamFieldIds:this.downstreamFields.map(e=>e.id)}}normalize(e){e.depEdges||={},e.depEdges[this.id]={id:this.id,...this.shallowExport(),...this.exportParentIds()}}};Ec();var Pee=class extends Mp{constructor({name:e,note:t,custom:n,edges:r,token:i,schema:a={}}={}){super(i),this.name=e??null,this.note=t?T(t,`value`,t):null,this.noteToken=t?T(t,`token`,null):null,this.custom=n??null,this.edges=[],this.schema=a,this.dbState=this.schema.dbState,this.generateId(),this.processEdges(r??[])}generateId(){this.id=this.dbState.generateId(`depId`)}processEdges(e){e.forEach(e=>{let t=e.upstream?.schemaName||`public`,n=e.downstream?.schemaName||`public`,r=e.upstream?.tableName,i=e.downstream?.tableName;t===n&&r===i&&this.error(`Self-loop Dep edge not allowed: "${t}"."${r}" cannot depend on itself`),this.edges.push(new Nee({...e,dep:this}))})}export(){return{...this.shallowExport(),...this.exportChild()}}shallowExport(){return{name:this.name,note:this.note,custom:this.custom}}exportChild(){return{edges:this.edges.map(e=>e.export())}}exportChildIds(){return{edgeIds:this.edges.map(e=>e.id)}}exportParentIds(){return{schemaId:this.schema.id}}normalize(e){e.deps[this.id]={id:this.id,...this.shallowExport(),...this.exportChildIds(),...this.exportParentIds()},this.edges.forEach(t=>t.normalize(e))}};Ec();var Fee=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 Iee=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 Fee({...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 ${Ap(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 ${Ap(e.table.schema)?`"${e.table.schema.name}".`:``}"${e.table.name}"."${e.name}" already associated with enum ${Ap(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))}},Lee=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 ${jp(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 ${Ap(t.schema)?`"${t.schema.name}".`:``}"${t.name}"`)}}n.forEach(e=>{let n=t.findField(e);n||this.error(`Can't find field ${Ap(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 Ree(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 Lee({...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 Ree(this.endpoints,e.endpoints)||Ree(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 zee=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.depEdges=[],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)}pushDepEdge(e){this.depEdges.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),depEdgeIds:this.depEdges.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}))})}},Bee=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()}}},Vee=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 Bee({...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 Hee=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 zee({...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 ${Ap(this.schema)?`"${this.schema.name}".`:``}"${this.name}"`)}processIndexes(e){e.forEach(e=>{this.pushIndex(new Vee({...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 ${Ap(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 zee({...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 Vee({...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 Uee=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 ${Ap(e.schema)?`"${e.schema.name}".`:``}.${e.name} is already in the group`),e.group&&this.error(`Table ${Ap(e.schema)?`"${e.schema.name}".`:``}.${e.name} is already in group ${Ap(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 Wee=class extends Mp{constructor({name:e,alias:t,note:n,tables:r=[],refs:i=[],deps:a=[],enums:o=[],tableGroups:s=[],token:c,database:l={},noteToken:u=null}={}){super(c),this.tables=[],this.enums=[],this.tableGroups=[],this.refs=[],this.deps=[],this.name=e,this.note=n?T(n,`value`,n):null,this.noteToken=n?T(n,`token`,u):null,this.alias=t,this.database=l,this.dbState=this.database.dbState,this.generateId(),this.processEnums(o),this.processTables(r),this.processRefs(i),this.processDeps(a),this.processTableGroups(s)}generateId(){this.id=this.dbState.generateId(`schemaId`)}processTables(e){e.forEach(e=>{this.pushTable(new Hee({...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 ${Ap(this)?`"${this.name}".`:``}"${e.name}" existed`)}findTable(e){return this.tables.find(t=>t.name===e)}processEnums(e){e.forEach(e=>{this.pushEnum(new Iee({...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 ${Ap(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}`)}}processDeps(e){e.forEach(e=>{this.pushDep(new Pee({...e,schema:this}))})}pushDep(e){this.deps.push(e)}processTableGroups(e){e.forEach(e=>{this.pushTableGroup(new Uee({...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 ${Ap(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()),deps:this.deps.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),depIds:this.deps.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)),this.deps.forEach(t=>t.normalize(e))}},Gee=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 Kee=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=[],deps:a=[],tableGroups:o=[],project:s={},aliases:c=[],records:l=[],tablePartials:u=[],diagramViews:d=[]}){super(),this.dbState=new jee,this.generateId(),this.hasDefaultSchema=!1,this.schemas=[],this.notes=[],this.note=s.note?T(s,`note.value`,s.note):null,this.noteToken=s.note?T(s,`note.token`,s.noteToken):null,this.databaseType=s.database_type,this.name=s.name,this.token=s.token,this.aliases=c,this.records=[],this.tablePartials=[],this.diagramViews=d,this.injectedRawRefs=[],this.processNotes(n),this.processRecords(l),this.processTablePartials(u),this.processSchemas(e),this.processSchemaElements(r,vee),this.processSchemaElements(t,gee),this.linkRecordsToTables(),this.processSchemaElements(n,_ee),this.processSchemaElements(i,`ref`),this.processSchemaElements(a,`dep`),this.processSchemaElements(o,yee),this.injectedRawRefs.forEach(e=>{let t=this.findOrCreateSchema(Tp),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 Gee({...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 Kee({...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 Wee({...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(Tp),t){case gee:n.pushTable(new Hee({...e,schema:n}));break;case vee:n.pushEnum(new Iee({...e,schema:n}));break;case yee:n.pushTableGroup(new Uee({...e,schema:n}));break;case`ref`:n.pushRef(new Np({...e,schema:n}));break;case`dep`:n.pushDep(new Pee({...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 Wee({name:e,note:{value:e===`public`?`Default ${fa(Tp)} 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:{},deps:{},depEdges:{},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}},qee=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 Jee=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(`,
|
|
34
|
+
`)}},jee=class{constructor(){this.dbId=1,this.schemaId=1,this.enumId=1,this.tableGroupId=1,this.refId=1,this.depId=1,this.depEdgeId=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)}},Nee=class extends Mp{constructor({upstream:e,downstream:t,token:n,dep:r}){super(n),this.upstream={schemaName:e.schemaName??null,tableName:e.tableName,fieldNames:e.fieldNames??[]},this.downstream={schemaName:t.schemaName??null,tableName:t.tableName,fieldNames:t.fieldNames??[]},this.dep=r,this.dbState=this.dep.dbState,this.generateId();let i=this.dep.schema.database,a=this.resolveEndpoint(this.upstream,i),o=this.resolveEndpoint(this.downstream,i);this.upstreamTable=a.table,this.upstreamFields=a.fields,this.downstreamTable=o.table,this.downstreamFields=o.fields,this.upstreamFields.forEach(e=>e.pushDepEdge(this)),this.downstreamFields.forEach(e=>e.pushDepEdge(this))}resolveEndpoint(e,t){let n=e.schemaName||`public`,r=t.findTable(n,e.tableName);return r||this.error(`Can't find table ${jp(n)?`"${n}".`:``}"${e.tableName}" referenced in Dep edge`),{table:r,fields:(e.fieldNames??[]).map(e=>{let t=r.findField(e);return t||this.error(`Can't find field ${Ap(r.schema)?`"${r.schema.name}".`:``}"${e}" in table "${r.name}" referenced in Dep edge`),t})}}generateId(){this.id=this.dbState.generateId(`depEdgeId`)}export(){return this.shallowExport()}shallowExport(){return{upstream:this.upstream,downstream:this.downstream}}exportParentIds(){return{depId:this.dep.id,upstreamTableId:this.upstreamTable?this.upstreamTable.id:null,upstreamFieldIds:this.upstreamFields.map(e=>e.id),downstreamTableId:this.downstreamTable?this.downstreamTable.id:null,downstreamFieldIds:this.downstreamFields.map(e=>e.id)}}normalize(e){e.depEdges||={},e.depEdges[this.id]={id:this.id,...this.shallowExport(),...this.exportParentIds()}}};Ec();var Pee=class extends Mp{constructor({name:e,color:t,note:n,custom:r,edges:i,token:a,schema:o={}}={}){super(a),this.name=e??null,this.color=t,this.note=n?T(n,`value`,n):null,this.noteToken=n?T(n,`token`,null):null,this.custom=r??null,this.edges=[],this.schema=o,this.dbState=this.schema.dbState,this.generateId(),this.processEdges(i??[])}generateId(){this.id=this.dbState.generateId(`depId`)}processEdges(e){e.forEach(e=>{let t=e.upstream?.schemaName||`public`,n=e.downstream?.schemaName||`public`,r=e.upstream?.tableName,i=e.downstream?.tableName;t===n&&r===i&&this.error(`Self-loop Dep edge not allowed: "${t}"."${r}" cannot depend on itself`),this.edges.push(new Nee({...e,dep:this}))})}export(){return{...this.shallowExport(),...this.exportChild()}}shallowExport(){return{name:this.name,color:this.color,note:this.note,custom:this.custom}}exportChild(){return{edges:this.edges.map(e=>e.export())}}exportChildIds(){return{edgeIds:this.edges.map(e=>e.id)}}exportParentIds(){return{schemaId:this.schema.id}}normalize(e){e.deps[this.id]={id:this.id,...this.shallowExport(),...this.exportChildIds(),...this.exportParentIds()},this.edges.forEach(t=>t.normalize(e))}};Ec();var Fee=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 Iee=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 Fee({...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 ${Ap(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 ${Ap(e.table.schema)?`"${e.table.schema.name}".`:``}"${e.table.name}"."${e.name}" already associated with enum ${Ap(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))}},Lee=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 ${jp(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 ${Ap(t.schema)?`"${t.schema.name}".`:``}"${t.name}"`)}}n.forEach(e=>{let n=t.findField(e);n||this.error(`Can't find field ${Ap(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 Ree(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 Lee({...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 Ree(this.endpoints,e.endpoints)||Ree(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 zee=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.depEdges=[],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)}pushDepEdge(e){this.depEdges.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),depEdgeIds:this.depEdges.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}))})}},Bee=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()}}},Vee=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 Bee({...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 Hee=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 zee({...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 ${Ap(this.schema)?`"${this.schema.name}".`:``}"${this.name}"`)}processIndexes(e){e.forEach(e=>{this.pushIndex(new Vee({...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 ${Ap(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 zee({...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 Vee({...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 Uee=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 ${Ap(e.schema)?`"${e.schema.name}".`:``}.${e.name} is already in the group`),e.group&&this.error(`Table ${Ap(e.schema)?`"${e.schema.name}".`:``}.${e.name} is already in group ${Ap(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 Wee=class extends Mp{constructor({name:e,alias:t,note:n,tables:r=[],refs:i=[],deps:a=[],enums:o=[],tableGroups:s=[],token:c,database:l={},noteToken:u=null}={}){super(c),this.tables=[],this.enums=[],this.tableGroups=[],this.refs=[],this.deps=[],this.name=e,this.note=n?T(n,`value`,n):null,this.noteToken=n?T(n,`token`,u):null,this.alias=t,this.database=l,this.dbState=this.database.dbState,this.generateId(),this.processEnums(o),this.processTables(r),this.processRefs(i),this.processDeps(a),this.processTableGroups(s)}generateId(){this.id=this.dbState.generateId(`schemaId`)}processTables(e){e.forEach(e=>{this.pushTable(new Hee({...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 ${Ap(this)?`"${this.name}".`:``}"${e.name}" existed`)}findTable(e){return this.tables.find(t=>t.name===e)}processEnums(e){e.forEach(e=>{this.pushEnum(new Iee({...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 ${Ap(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}`)}}processDeps(e){e.forEach(e=>{this.pushDep(new Pee({...e,schema:this}))})}pushDep(e){this.deps.push(e)}processTableGroups(e){e.forEach(e=>{this.pushTableGroup(new Uee({...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 ${Ap(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()),deps:this.deps.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),depIds:this.deps.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)),this.deps.forEach(t=>t.normalize(e))}},Gee=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 Kee=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=[],deps:a=[],tableGroups:o=[],project:s={},aliases:c=[],records:l=[],tablePartials:u=[],diagramViews:d=[]}){super(),this.dbState=new jee,this.generateId(),this.hasDefaultSchema=!1,this.schemas=[],this.notes=[],this.note=s.note?T(s,`note.value`,s.note):null,this.noteToken=s.note?T(s,`note.token`,s.noteToken):null,this.databaseType=s.database_type,this.name=s.name,this.token=s.token,this.aliases=c,this.records=[],this.tablePartials=[],this.diagramViews=d,this.injectedRawRefs=[],this.processNotes(n),this.processRecords(l),this.processTablePartials(u),this.processSchemas(e),this.processSchemaElements(r,vee),this.processSchemaElements(t,gee),this.linkRecordsToTables(),this.processSchemaElements(n,_ee),this.processSchemaElements(i,`ref`),this.processSchemaElements(a,`dep`),this.processSchemaElements(o,yee),this.injectedRawRefs.forEach(e=>{let t=this.findOrCreateSchema(Tp),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 Gee({...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 Kee({...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 Wee({...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(Tp),t){case gee:n.pushTable(new Hee({...e,schema:n}));break;case vee:n.pushEnum(new Iee({...e,schema:n}));break;case yee:n.pushTableGroup(new Uee({...e,schema:n}));break;case`ref`:n.pushRef(new Np({...e,schema:n}));break;case`dep`:n.pushDep(new Pee({...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 Wee({name:e,note:{value:e===`public`?`Default ${fa(Tp)} 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:{},deps:{},depEdges:{},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}},qee=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 Jee=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(`,
|
|
35
35
|
`)};\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 ${Ep(i,n)?`[${i.name}].`:``}[${r.name}] (\n${t.map(e=>` ${e}`).join(`,
|
|
36
36
|
`)}\n)\nGO\n`})}static buildTableManyToMany(e,t,n,r,i){let a=`CREATE TABLE ${Ep(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+=`);
|
|
37
37
|
GO
|
|
@@ -125,4 +125,4 @@ function print() { __p += __j.call(arguments, '') }
|
|
|
125
125
|
`,!1),pt=`
|
|
126
126
|
`,mt=Dt(`
|
|
127
127
|
`,!1),ht=At(`whitespace`),gt=/^[ \t\r\n\r]/,_t=Ot([` `,` `,`\r`,`
|
|
128
|
-
`,`\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}})),YNe=u(mAe()),XNe=u(GNe()),ZNe=u(KNe()),QNe=u(qNe()),$Ne=u(JNe()),ePe=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 ZNe.default.parse(e)}static parsePostgresToJSONv2(e){return Z7(e,`postgres`)}static parsePostgresToJSON(e){return QNe.default.parse(e)}static parseDBMLToJSONv2(e){let t=new f.MemoryProjectLayout;t.setSource(f.DEFAULT_ENTRY,e);let n=new f.Compiler(t),r=tPe(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 YNe.default.parse(e)}static parseSchemaRbToJSON(e){return $Ne.default.parse(e)}static parseMSSQLToJSON(e){return XNe.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=tPe(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 tPe(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 nPe(e,t,n={isNormalized:!0,includeRecords:!0}){let r=rPe(n),i=new ePe().parse(e,`dbmlv2`);return $9.export(i.normalize(),t,r)}function rPe(e={}){if(typeof e==`boolean`)return{isNormalized:e,includeRecords:!0};let{isNormalized:t=!0,includeRecords:n=!0}=e;return{isNormalized:t,includeRecords:n}}var iPe={export:nPe},$9=class{static export(e,t,n={isNormalized:!0,includeRecords:!0}){let{isNormalized:r,includeRecords:i}=rPe(n),a=e instanceof Fp?e.normalize():e,o=``;switch(t){case`dbml`:o=Eee.export(a,{includeRecords:i});break;case`mysql`:o=Dee.export(a);break;case`postgres`:o=Aee.export(a);break;case`json`:o=qee.export(e,{isNormalized:r});break;case`mssql`:o=Jee.export(a);break;case`oracle`:o=Yee.export(a);break;default:break}return o}},aPe=e=>new Fp(e),oPe=e=>e.map(e=>{let{name:t,endpoints:n,onDelete:r,onUpdate:i}=e;return new Uke({name:t,endpoints:n.map(e=>new j7(e)),onDelete:r,onUpdate:i}).toJSON()}),sPe=e=>e.map(e=>{let{name:t,schemaName:n,values:r}=e;return new M7({name:t,schemaName:n,values:r})}),cPe=(e,t)=>e.map(e=>{let n=t[e.name]||{};return new k7({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})}),lPe=e=>e.map(e=>{let{name:t,unique:n,pk:r,type:i,columns:a}=e;return new O7({name:t,unique:n,pk:r,type:i,columns:a})}),uPe=(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 A7({name:a,schemaName:o,fields:cPe(t[c],l),indexes:lPe(n[c]||[]),note:s,checks:r[c]})}),dPe=e=>{let{tables:t,fields:n,indexes:r,refs:i,enums:a,tableConstraints:o,checks:s}=e;try{let e=uPe(t,n,r,s,o),c=sPe(a);return aPe({schemas:[],tables:e,refs:oPe(i),enums:c,tableGroups:[],aliases:[],project:{}})}catch(e){throw Error(e)}};function fPe(e,t,n={includeRecords:!0}){let{includeRecords:r=!0}=n,i=new ePe().parse(e,t);return $9.export(i.normalize(),`dbml`,{includeRecords:r})}function pPe(e){let t=dPe(e);return $9.export(t.normalize(),`dbml`)}var mPe={import:fPe,generateDbml:pPe};function hPe(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 gPe(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 _Pe(e){let t=new f.MemoryProjectLayout;return t.setSource(f.DEFAULT_ENTRY,e),new f.Compiler(t).findDiagramViewBlocks(f.DEFAULT_ENTRY)}var vPe=`9.0.0-alpha.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=ePe,exports.VERSION=vPe,Object.defineProperty(exports,"addDoubleQuoteIfNeeded",{enumerable:!0,get:function(){return f.addDoubleQuoteIfNeeded}}),Object.defineProperty(exports,"dbmlMonarchTokensProvider",{enumerable:!0,get:function(){return f.dbmlMonarchTokensProvider}}),exports.exporter=iPe,exports.findDiagramViewBlocks=_Pe,Object.defineProperty(exports,"formatRecordValue",{enumerable:!0,get:function(){return f.formatRecordValue}}),exports.importer=mPe,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=hPe,exports.syncDiagramView=gPe,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}});
|
|
128
|
+
`,`\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}})),YNe=u(mAe()),XNe=u(GNe()),ZNe=u(KNe()),QNe=u(qNe()),$Ne=u(JNe()),ePe=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 ZNe.default.parse(e)}static parsePostgresToJSONv2(e){return Z7(e,`postgres`)}static parsePostgresToJSON(e){return QNe.default.parse(e)}static parseDBMLToJSONv2(e){let t=new f.MemoryProjectLayout;t.setSource(f.DEFAULT_ENTRY,e);let n=new f.Compiler(t),r=tPe(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 YNe.default.parse(e)}static parseSchemaRbToJSON(e){return $Ne.default.parse(e)}static parseMSSQLToJSON(e){return XNe.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=tPe(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 tPe(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 nPe(e,t,n={isNormalized:!0,includeRecords:!0}){let r=rPe(n),i=new ePe().parse(e,`dbmlv2`);return $9.export(i.normalize(),t,r)}function rPe(e={}){if(typeof e==`boolean`)return{isNormalized:e,includeRecords:!0};let{isNormalized:t=!0,includeRecords:n=!0}=e;return{isNormalized:t,includeRecords:n}}var iPe={export:nPe},$9=class{static export(e,t,n={isNormalized:!0,includeRecords:!0}){let{isNormalized:r,includeRecords:i}=rPe(n),a=e instanceof Fp?e.normalize():e,o=``;switch(t){case`dbml`:o=Eee.export(a,{includeRecords:i});break;case`mysql`:o=Dee.export(a);break;case`postgres`:o=Aee.export(a);break;case`json`:o=qee.export(e,{isNormalized:r});break;case`mssql`:o=Jee.export(a);break;case`oracle`:o=Yee.export(a);break;default:break}return o}},aPe=e=>new Fp(e),oPe=e=>e.map(e=>{let{name:t,endpoints:n,onDelete:r,onUpdate:i}=e;return new Uke({name:t,endpoints:n.map(e=>new j7(e)),onDelete:r,onUpdate:i}).toJSON()}),sPe=e=>e.map(e=>{let{name:t,schemaName:n,values:r}=e;return new M7({name:t,schemaName:n,values:r})}),cPe=(e,t)=>e.map(e=>{let n=t[e.name]||{};return new k7({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})}),lPe=e=>e.map(e=>{let{name:t,unique:n,pk:r,type:i,columns:a}=e;return new O7({name:t,unique:n,pk:r,type:i,columns:a})}),uPe=(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 A7({name:a,schemaName:o,fields:cPe(t[c],l),indexes:lPe(n[c]||[]),note:s,checks:r[c]})}),dPe=e=>{let{tables:t,fields:n,indexes:r,refs:i,enums:a,tableConstraints:o,checks:s}=e;try{let e=uPe(t,n,r,s,o),c=sPe(a);return aPe({schemas:[],tables:e,refs:oPe(i),enums:c,tableGroups:[],aliases:[],project:{}})}catch(e){throw Error(e)}};function fPe(e,t,n={includeRecords:!0}){let{includeRecords:r=!0}=n,i=new ePe().parse(e,t);return $9.export(i.normalize(),`dbml`,{includeRecords:r})}function pPe(e){let t=dPe(e);return $9.export(t.normalize(),`dbml`)}var mPe={import:fPe,generateDbml:pPe};function hPe(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 gPe(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 _Pe(e){let t=new f.MemoryProjectLayout;return t.setSource(f.DEFAULT_ENTRY,e),new f.Compiler(t).findDiagramViewBlocks(f.DEFAULT_ENTRY)}function vPe(e,t,n){let r=new f.MemoryProjectLayout;return r.setSource(f.DEFAULT_ENTRY,e),new f.Compiler(r).syncDep(f.DEFAULT_ENTRY,t,n)}function yPe(e){let t=new f.MemoryProjectLayout;return t.setSource(f.DEFAULT_ENTRY,e),new f.Compiler(t).findDepBlocks(f.DEFAULT_ENTRY)}var bPe=`9.0.0-alpha.3`;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=ePe,exports.VERSION=bPe,Object.defineProperty(exports,"addDoubleQuoteIfNeeded",{enumerable:!0,get:function(){return f.addDoubleQuoteIfNeeded}}),Object.defineProperty(exports,"dbmlMonarchTokensProvider",{enumerable:!0,get:function(){return f.dbmlMonarchTokensProvider}}),exports.exporter=iPe,exports.findDepBlocks=yPe,exports.findDiagramViewBlocks=_Pe,Object.defineProperty(exports,"formatRecordValue",{enumerable:!0,get:function(){return f.formatRecordValue}}),exports.importer=mPe,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=hPe,exports.syncDep=vPe,exports.syncDiagramView=gPe,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
|
@@ -5476,8 +5476,8 @@ var Yee = /* @__PURE__ */ "SMALLINT.INTEGER.INT.BIGINT.DECIMAL.NUMERIC.REAL.DOUB
|
|
|
5476
5476
|
//#region src/model_structure/dep.js
|
|
5477
5477
|
el();
|
|
5478
5478
|
var tte = class extends Hp {
|
|
5479
|
-
constructor({ name: e,
|
|
5480
|
-
super(
|
|
5479
|
+
constructor({ name: e, color: t, note: n, custom: r, edges: i, token: a, schema: o = {} } = {}) {
|
|
5480
|
+
super(a), this.name = e ?? null, this.color = t, this.note = n ? Qi(n, "value", n) : null, this.noteToken = n ? Qi(n, "token", null) : null, this.custom = r ?? null, this.edges = [], this.schema = o, this.dbState = this.schema.dbState, this.generateId(), this.processEdges(i ?? []);
|
|
5481
5481
|
}
|
|
5482
5482
|
generateId() {
|
|
5483
5483
|
this.id = this.dbState.generateId("depId");
|
|
@@ -5500,6 +5500,7 @@ var tte = class extends Hp {
|
|
|
5500
5500
|
shallowExport() {
|
|
5501
5501
|
return {
|
|
5502
5502
|
name: this.name,
|
|
5503
|
+
color: this.color,
|
|
5503
5504
|
note: this.note,
|
|
5504
5505
|
custom: this.custom
|
|
5505
5506
|
};
|
|
@@ -1811714,8 +1811715,16 @@ function JPe(t) {
|
|
|
1811714
1811715
|
let r = new a();
|
|
1811715
1811716
|
return r.setSource(n, t), new e(r).findDiagramViewBlocks(n);
|
|
1811716
1811717
|
}
|
|
1811718
|
+
function YPe(t, r, i) {
|
|
1811719
|
+
let o = new a();
|
|
1811720
|
+
return o.setSource(n, t), new e(o).syncDep(n, r, i);
|
|
1811721
|
+
}
|
|
1811722
|
+
function XPe(t) {
|
|
1811723
|
+
let r = new a();
|
|
1811724
|
+
return r.setSource(n, t), new e(r).findDepBlocks(n);
|
|
1811725
|
+
}
|
|
1811717
1811726
|
//#endregion
|
|
1811718
1811727
|
//#region src/utils/version.ts
|
|
1811719
|
-
var
|
|
1811728
|
+
var ZPe = "9.0.0-alpha.3";
|
|
1811720
1811729
|
//#endregion
|
|
1811721
|
-
export { W7 as CompilerError, t as DEFAULT_ENTRY, r as Filepath, $9 as ModelExporter, jPe as Parser,
|
|
1811730
|
+
export { W7 as CompilerError, t as DEFAULT_ENTRY, r as Filepath, $9 as ModelExporter, jPe as Parser, ZPe as VERSION, o as addDoubleQuoteIfNeeded, c as dbmlMonarchTokensProvider, FPe as exporter, XPe as findDepBlocks, JPe as findDiagramViewBlocks, l as formatRecordValue, GPe 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, KPe as renameTable, YPe as syncDep, qPe 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": "9.0.0-alpha.
|
|
4
|
+
"version": "9.0.0-alpha.3",
|
|
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": "^9.0.0-alpha.
|
|
49
|
+
"@dbml/parse": "^9.0.0-alpha.3",
|
|
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": "
|
|
60
|
+
"gitHead": "c25a2e99a38fdfabf87161919e6ba50e93a9bcf1",
|
|
61
61
|
"engines": {
|
|
62
62
|
"node": ">=16"
|
|
63
63
|
}
|
package/types/index.d.ts
CHANGED
|
@@ -6,11 +6,15 @@ import {
|
|
|
6
6
|
renameTable,
|
|
7
7
|
syncDiagramView,
|
|
8
8
|
findDiagramViewBlocks,
|
|
9
|
+
syncDep,
|
|
10
|
+
findDepBlocks,
|
|
9
11
|
} from './transform';
|
|
10
12
|
export {
|
|
11
13
|
renameTable,
|
|
12
14
|
syncDiagramView,
|
|
13
15
|
findDiagramViewBlocks,
|
|
16
|
+
syncDep,
|
|
17
|
+
findDepBlocks,
|
|
14
18
|
importer,
|
|
15
19
|
exporter,
|
|
16
20
|
ModelExporter,
|
|
@@ -51,5 +55,9 @@ export type {
|
|
|
51
55
|
FilterConfig,
|
|
52
56
|
DiagramViewSyncOperation,
|
|
53
57
|
DiagramViewBlock,
|
|
58
|
+
DepSyncOperation,
|
|
59
|
+
DepSyncEdge,
|
|
60
|
+
DepEndpointRef,
|
|
61
|
+
DepBlock,
|
|
54
62
|
TextEdit,
|
|
55
63
|
} from '@dbml/parse';
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import Element, { Token } from './element';
|
|
1
|
+
import Element, { Token, Color } from './element';
|
|
2
2
|
import DepEdge from './dep_edge';
|
|
3
3
|
import Schema from './schema';
|
|
4
4
|
import DbState from './dbState';
|
|
@@ -18,6 +18,7 @@ export interface RawDepEdgeInput {
|
|
|
18
18
|
|
|
19
19
|
export interface RawDep {
|
|
20
20
|
name?: string | null;
|
|
21
|
+
color?: Color;
|
|
21
22
|
note?: { value?: string; token?: Token } | string | null;
|
|
22
23
|
custom?: Record<string, string | number | boolean | null> | null;
|
|
23
24
|
edges: RawDepEdgeInput[];
|
|
@@ -27,6 +28,7 @@ export interface RawDep {
|
|
|
27
28
|
|
|
28
29
|
declare class Dep extends Element {
|
|
29
30
|
name: string | null;
|
|
31
|
+
color?: Color;
|
|
30
32
|
note: string | null;
|
|
31
33
|
noteToken: Token;
|
|
32
34
|
custom: Record<string, string | number | boolean | null> | null;
|
|
@@ -40,6 +42,7 @@ declare class Dep extends Element {
|
|
|
40
42
|
processEdges(rawEdges: RawDepEdgeInput[]): void;
|
|
41
43
|
export(): {
|
|
42
44
|
name: string | null;
|
|
45
|
+
color?: Color;
|
|
43
46
|
note: string | null;
|
|
44
47
|
custom: Record<string, string | number | boolean | null> | null;
|
|
45
48
|
edges: {
|
|
@@ -49,6 +52,7 @@ declare class Dep extends Element {
|
|
|
49
52
|
};
|
|
50
53
|
shallowExport(): {
|
|
51
54
|
name: string | null;
|
|
55
|
+
color?: Color;
|
|
52
56
|
note: string | null;
|
|
53
57
|
custom: Record<string, string | number | boolean | null> | null;
|
|
54
58
|
};
|
|
@@ -70,6 +74,7 @@ declare class Dep extends Element {
|
|
|
70
74
|
export interface NormalizedDep {
|
|
71
75
|
id: number;
|
|
72
76
|
name: string | null;
|
|
77
|
+
color?: Color;
|
|
73
78
|
note: string | null;
|
|
74
79
|
custom: Record<string, string | number | boolean | null> | null;
|
|
75
80
|
schemaId: number;
|
|
@@ -1,4 +1,7 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type {
|
|
2
|
+
DiagramViewSyncOperation, DiagramViewBlock, TextEdit,
|
|
3
|
+
DepSyncOperation, DepBlock,
|
|
4
|
+
} from '@dbml/parse';
|
|
2
5
|
|
|
3
6
|
export type TableNameInput = string | { schema?: string; table: string };
|
|
4
7
|
|
|
@@ -17,3 +20,13 @@ export function syncDiagramView(
|
|
|
17
20
|
export function findDiagramViewBlocks(
|
|
18
21
|
dbmlCode: string,
|
|
19
22
|
): DiagramViewBlock[];
|
|
23
|
+
|
|
24
|
+
export function syncDep(
|
|
25
|
+
dbmlCode: string,
|
|
26
|
+
operations: DepSyncOperation[],
|
|
27
|
+
blocks?: DepBlock[],
|
|
28
|
+
): { newDbml: string; edits: TextEdit[] };
|
|
29
|
+
|
|
30
|
+
export function findDepBlocks(
|
|
31
|
+
dbmlCode: string,
|
|
32
|
+
): DepBlock[];
|