@mrleebo/prisma-ast
Advanced tools
Comparing version 0.9.0 to 0.9.1
@@ -0,1 +1,2 @@ | ||
export * from './produceSchema'; | ||
export * from './getSchema'; | ||
@@ -2,0 +3,0 @@ export * from './printSchema'; |
@@ -1486,2 +1486,11 @@ 'use strict'; | ||
function produceSchema(source, producer, options) { | ||
if (options === void 0) { | ||
options = {}; | ||
} | ||
var builder = createPrismaSchemaBuilder(source); | ||
producer(builder); | ||
return builder.print(options); | ||
} | ||
exports.ConcretePrismaSchemaBuilder = ConcretePrismaSchemaBuilder; | ||
@@ -1493,2 +1502,3 @@ exports.PrismaParser = PrismaParser; | ||
exports.printSchema = printSchema; | ||
exports.produceSchema = produceSchema; | ||
//# sourceMappingURL=prisma-ast.cjs.development.js.map |
@@ -1,2 +0,2 @@ | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("chevrotain"),t=require("lilconfig"),n=require("os"),r=e.createToken({name:"Identifier",pattern:/[a-zA-Z]\w*/}),a=e.createToken({name:"Datasource",pattern:/datasource/,push_mode:"block"}),i=e.createToken({name:"Generator",pattern:/generator/,push_mode:"block"}),u=e.createToken({name:"Model",pattern:/model/,push_mode:"block"}),o=e.createToken({name:"View",pattern:/view/,push_mode:"block"}),s=e.createToken({name:"Enum",pattern:/enum/,push_mode:"block"}),c=e.createToken({name:"Type",pattern:/type/,push_mode:"block"}),m=e.createToken({name:"True",pattern:/true/,longer_alt:r}),p=e.createToken({name:"False",pattern:/false/,longer_alt:r}),l=e.createToken({name:"Null",pattern:/null/,longer_alt:r}),f=e.createToken({name:"Comment",pattern:e.Lexer.NA}),L=e.createToken({name:"DocComment",pattern:/\/\/\/[ \t]*(.*)/,categories:[f]}),d=e.createToken({name:"LineComment",pattern:/\/\/[ \t]*(.*)/,categories:[f]}),y=e.createToken({name:"Attribute",pattern:e.Lexer.NA}),h=e.createToken({name:"BlockAttribute",pattern:/@@/,label:"'@@'",categories:[y]}),v=e.createToken({name:"FieldAttribute",pattern:/@/,label:"'@'",categories:[y]}),E=e.createToken({name:"Dot",pattern:/\./,label:"'.'"}),b=e.createToken({name:"QuestionMark",pattern:/\?/,label:"'?'"}),g=e.createToken({name:"LCurly",pattern:/{/,label:"'{'"}),A=e.createToken({name:"RCurly",pattern:/}/,label:"'}'",pop_mode:!0}),U=e.createToken({name:"LRound",pattern:/\(/,label:"'('"}),k=e.createToken({name:"RRound",pattern:/\)/,label:"')'"}),S=e.createToken({name:"LSquare",pattern:/\[/,label:"'['"}),O=e.createToken({name:"RSquare",pattern:/\]/,label:"']'"}),T=e.createToken({name:"Comma",pattern:/,/,label:"','"}),B=e.createToken({name:"Colon",pattern:/:/,label:"':'"}),N=e.createToken({name:"Equals",pattern:/=/,label:"'='"}),C=e.createToken({name:"StringLiteral",pattern:/"(:?[^\\"\n\r]|\\(:?[bfnrtv"\\/]|u[0-9a-fA-F]{4}))*"/}),w=e.createToken({name:"NumberLiteral",pattern:/-?(0|[1-9]\d*)(\.\d+)?([eE][+-]?\d+)?/}),M=e.createToken({name:"WhiteSpace",pattern:/\s+/,group:e.Lexer.SKIPPED}),j=e.createToken({name:"LineBreak",pattern:/\n|\r\n/,line_breaks:!0,label:"LineBreak"}),R=[f,L,d,j,M],_={modes:{global:[].concat(R,[a,i,u,o,s,c]),block:[].concat(R,[y,h,v,E,b,g,A,S,O,U,k,T,B,N,m,p,l,C,w,r])},defaultMode:"global"},x=new e.Lexer(_);function P(e,t){e.prototype=Object.create(t.prototype),e.prototype.constructor=e,D(e,t)}function D(e,t){return D=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(e,t){return e.__proto__=t,e},D(e,t)}var I=["model","view","type"];function V(e){return null!=e&&"type"in e&&I.includes(e.type)}function F(e){return null!=e&&"type"in e&&"field"===e.type}function q(e){for(var t=arguments.length,n=new Array(t>1?t-1:0),r=1;r<t;r++)n[r-1]=arguments[r];var a=n.reduce((function(e,t){if(!t)return e;var n=e.endColumn,r=void 0===n?-Infinity:n,a=e.endLine,i=void 0===a?-Infinity:a,u=e.endOffset,o=void 0===u?-Infinity:u,s=e.startColumn,c=void 0===s?Infinity:s,m=e.startLine,p=e.startOffset,l=void 0===p?Infinity:p;return null!=t.startLine&&t.startLine<(void 0===m?Infinity:m)&&(e.startLine=t.startLine),null!=t.startColumn&&t.startColumn<c&&(e.startColumn=t.startColumn),null!=t.startOffset&&t.startOffset<l&&(e.startOffset=t.startOffset),null!=t.endLine&&t.endLine>i&&(e.endLine=t.endLine),null!=t.endColumn&&t.endColumn>r&&(e.endColumn=t.endColumn),null!=t.endOffset&&t.endOffset>o&&(e.endOffset=t.endOffset),e}),{});return Object.assign(e,{location:a})}var G,Y={parser:{nodeLocationTracking:"none"}},z=function(e){function t(t){var n;return(n=e.call(this,_,t)||this).config=void 0,n.break=n.RULE("break",(function(){n.CONSUME1(j),n.CONSUME2(j)})),n.keyedArg=n.RULE("keyedArg",(function(){n.CONSUME(r,{LABEL:"keyName"}),n.CONSUME(B),n.SUBRULE(n.value)})),n.array=n.RULE("array",(function(){n.CONSUME(S),n.MANY_SEP({SEP:T,DEF:function(){n.SUBRULE(n.value)}}),n.CONSUME(O)})),n.func=n.RULE("func",(function(){n.CONSUME(r,{LABEL:"funcName"}),n.CONSUME(U),n.MANY_SEP({SEP:T,DEF:function(){n.OR([{ALT:function(){return n.SUBRULE(n.keyedArg)}},{ALT:function(){return n.SUBRULE(n.value)}}])}}),n.CONSUME(k)})),n.value=n.RULE("value",(function(){n.OR([{ALT:function(){return n.CONSUME(C,{LABEL:"value"})}},{ALT:function(){return n.CONSUME(w,{LABEL:"value"})}},{ALT:function(){return n.SUBRULE(n.array,{LABEL:"value"})}},{ALT:function(){return n.SUBRULE(n.func,{LABEL:"value"})}},{ALT:function(){return n.CONSUME(m,{LABEL:"value"})}},{ALT:function(){return n.CONSUME(p,{LABEL:"value"})}},{ALT:function(){return n.CONSUME(l,{LABEL:"value"})}},{ALT:function(){return n.CONSUME(r,{LABEL:"value"})}}])})),n.property=n.RULE("property",(function(){n.CONSUME(r,{LABEL:"propertyName"}),n.CONSUME(N),n.SUBRULE(n.value,{LABEL:"propertyValue"})})),n.assignment=n.RULE("assignment",(function(){n.CONSUME(r,{LABEL:"assignmentName"}),n.CONSUME(N),n.SUBRULE(n.value,{LABEL:"assignmentValue"})})),n.field=n.RULE("field",(function(){n.CONSUME(r,{LABEL:"fieldName"}),n.SUBRULE(n.value,{LABEL:"fieldType"}),n.OPTION1((function(){n.OR([{ALT:function(){n.CONSUME(S,{LABEL:"array"}),n.CONSUME(O,{LABEL:"array"})}},{ALT:function(){return n.CONSUME(b,{LABEL:"optional"})}}])})),n.MANY((function(){n.SUBRULE(n.attribute,{LABEL:"attributeList"})})),n.OPTION2((function(){n.CONSUME(f,{LABEL:"comment"})}))})),n.block=n.RULE("block",(function(e){void 0===e&&(e={});var t=e.componentType,r="enum"===t,a="model"===t||"view"===t||"type"===t;n.CONSUME(g),n.CONSUME1(j),n.MANY((function(){n.OR([{ALT:function(){return n.SUBRULE(n.comment,{LABEL:"list"})}},{GATE:function(){return a},ALT:function(){return n.SUBRULE(n.property,{LABEL:"list"})}},{ALT:function(){return n.SUBRULE(n.attribute,{LABEL:"list"})}},{GATE:function(){return a},ALT:function(){return n.SUBRULE(n.field,{LABEL:"list"})}},{GATE:function(){return r},ALT:function(){return n.SUBRULE(n.enum,{LABEL:"list"})}},{GATE:function(){return!a},ALT:function(){return n.SUBRULE(n.assignment,{LABEL:"list"})}},{ALT:function(){return n.SUBRULE(n.break,{LABEL:"list"})}},{ALT:function(){return n.CONSUME2(j)}}])})),n.CONSUME(A)})),n.enum=n.RULE("enum",(function(){n.CONSUME(r,{LABEL:"enumName"}),n.OPTION((function(){n.CONSUME(f,{LABEL:"comment"})}))})),n.attribute=n.RULE("attribute",(function(){n.OR1([{ALT:function(){return n.CONSUME(h,{LABEL:"blockAttribute"})}},{ALT:function(){return n.CONSUME(v,{LABEL:"fieldAttribute"})}}]),n.OR2([{ALT:function(){n.CONSUME1(r,{LABEL:"groupName"}),n.CONSUME(E),n.CONSUME2(r,{LABEL:"attributeName"})}},{ALT:function(){return n.CONSUME(r,{LABEL:"attributeName"})}}]),n.OPTION((function(){n.CONSUME(U),n.MANY_SEP({SEP:T,DEF:function(){n.SUBRULE(n.attributeArg)}}),n.CONSUME(k)}))})),n.attributeArg=n.RULE("attributeArg",(function(){n.OR([{ALT:function(){return n.SUBRULE(n.keyedArg,{LABEL:"value"})}},{ALT:function(){return n.SUBRULE(n.value,{LABEL:"value"})}}])})),n.component=n.RULE("component",(function(){var e=n.OR1([{ALT:function(){return n.CONSUME(a,{LABEL:"type"})}},{ALT:function(){return n.CONSUME(i,{LABEL:"type"})}},{ALT:function(){return n.CONSUME(u,{LABEL:"type"})}},{ALT:function(){return n.CONSUME(o,{LABEL:"type"})}},{ALT:function(){return n.CONSUME(s,{LABEL:"type"})}},{ALT:function(){return n.CONSUME(c,{LABEL:"type"})}}]);n.OR2([{ALT:function(){n.CONSUME1(r,{LABEL:"groupName"}),n.CONSUME(E),n.CONSUME2(r,{LABEL:"componentName"})}},{ALT:function(){return n.CONSUME(r,{LABEL:"componentName"})}}]),n.SUBRULE(n.block,{ARGS:[{componentType:e.image}]})})),n.comment=n.RULE("comment",(function(){n.CONSUME(f,{LABEL:"text"})})),n.schema=n.RULE("schema",(function(){n.MANY((function(){n.OR([{ALT:function(){return n.SUBRULE(n.comment,{LABEL:"list"})}},{ALT:function(){return n.SUBRULE(n.component,{LABEL:"list"})}},{ALT:function(){return n.SUBRULE(n.break,{LABEL:"list"})}},{ALT:function(){return n.CONSUME(j)}}])}))})),n.performSelfAnalysis(),n.config=t,n}return P(t,e),t}(e.CstParser),W=new z(function(){if(null!=G)return G;var e=t.lilconfigSync("prisma-ast").search();return G=Object.assign(Y,null==e?void 0:e.config)}().parser),K=function(e){return function(t){function n(){var e;return(e=t.call(this)||this).validateVisitor(),e}P(n,t);var r=n.prototype;return r.schema=function(e){var t,n=this;return{type:"schema",list:(null==(t=e.list)?void 0:t.map((function(e){return n.visit([e])})))||[]}},r.component=function(e){var t=e.type[0],n=e.componentName[0],r=this.visit(e.block),a=function(){switch(t.image){case"datasource":return{type:"datasource",name:n.image,assignments:r};case"generator":return{type:"generator",name:n.image,assignments:r};case"model":return{type:"model",name:n.image,properties:r};case"view":return{type:"view",name:n.image,properties:r};case"enum":return{type:"enum",name:n.image,enumerators:r};case"type":return{type:"type",name:n.image,properties:r};default:throw new Error("Unexpected block type: "+t)}}();return this.maybeAppendLocationData(a,t,n)},r.break=function(){return{type:"break"}},r.comment=function(e){var t=e.text[0];return this.maybeAppendLocationData({type:"comment",text:t.image},t)},r.block=function(e){var t,n=this;return null==(t=e.list)?void 0:t.map((function(e){return n.visit([e])}))},r.assignment=function(e){var t=this.visit(e.assignmentValue),n=e.assignmentName[0];return this.maybeAppendLocationData({type:"assignment",key:n.image,value:t},n)},r.field=function(e){var t,n,r,a=this,i=this.visit(e.fieldType),u=e.fieldName[0],o=e.attributeList&&e.attributeList.map((function(e){return a.visit([e])})),s=null==(t=e.comment)||null==(t=t[0])?void 0:t.image;return this.maybeAppendLocationData({type:"field",name:u.image,fieldType:i,array:null!=e.array,optional:null!=e.optional,attributes:o,comment:s},u,null==(n=e.optional)?void 0:n[0],null==(r=e.array)?void 0:r[0])},r.attribute=function(e){var t=this,n=e.attributeName[0],r=(e.groupName||[{}])[0],a=e.attributeArg&&e.attributeArg.map((function(e){return t.visit(e)})),i=null!=e.blockAttribute?"object":"field";return this.maybeAppendLocationData.apply(this,[{type:"attribute",name:n.image,kind:i,group:r.image,args:a},n].concat("object"===i?e.blockAttribute:e.fieldAttribute,[r]))},r.attributeArg=function(e){return{type:"attributeArgument",value:this.visit(e.value)}},r.func=function(e){var t=this,n=e.funcName[0],r=e.value&&e.value.map((function(e){return t.visit([e])})),a=e.keyedArg&&e.keyedArg.map((function(e){return t.visit([e])})),i=(r||a)&&[].concat(null!=r?r:[],null!=a?a:[]);return this.maybeAppendLocationData({type:"function",name:n.image,params:i},n)},r.array=function(e){var t=this;return{type:"array",args:e.value&&e.value.map((function(e){return t.visit([e])}))}},r.keyedArg=function(e){var t=e.keyName[0],n=this.visit(e.value);return this.maybeAppendLocationData({type:"keyValue",key:t.image,value:n},t)},r.value=function(e){return"image"in e.value[0]?e.value[0].image:this.visit(e.value)},r.enum=function(e){var t,n=e.enumName[0],r=null==(t=e.comment)||null==(t=t[0])?void 0:t.image;return this.maybeAppendLocationData({type:"enumerator",name:n.image,comment:r},n)},r.maybeAppendLocationData=function(t){if("none"===e.config.nodeLocationTracking)return t;for(var n=arguments.length,r=new Array(n>1?n-1:0),a=1;a<n;a++)r[a-1]=arguments[a];return q.apply(void 0,[t].concat(r))},n}(e.getBaseCstVisitorConstructorWithDefaults())},Q=new(K(W));function Z(e,t){var n,r,a=x.tokenize(e),i=null!=(n=null==t?void 0:t.parser)?n:W;i.input=a.tokens;var u=i.schema();if(i.errors.length>0)throw i.errors[0];return(null!=(r=null==t?void 0:t.visitor)?r:Q).visit(u)}var H=["break","comment"],J=["generator","datasource","model","view","enum","break","comment"];function X(e,t){void 0===t&&(t={});var r=t.sort,a=t.locales,i=void 0===a?void 0:a,u=t.sortOrder,o=void 0===u?void 0:u,s=e.list;if(void 0!==r&&r){s=e.list=s.filter((function(e){return"break"!==e.type}));var c=function(e,t,n){return void 0===n&&(n=J),function(r,a){if(-1!==H.indexOf(r.type)!=(-1!==H.indexOf(a.type)))return e.list.indexOf(r)-e.list.indexOf(a);n!==J&&(n=n.concat(J));var i=n.indexOf(r.type)-n.indexOf(a.type);return 0!==i?i:"name"in r&&"name"in a?r.name.localeCompare(a.name,t):0}}(e,i,o);s.sort(c)}return s.map($).filter(Boolean).join(n.EOL).replace(/(\r?\n\s*){3,}/g,n.EOL+n.EOL)+n.EOL}function $(e){switch(e.type){case"comment":return ee(e);case"datasource":return r=se((t=e).assignments),"\ndatasource "+t.name+" {\n "+r+"\n}";case"enum":return function(e){var t=e.enumerators.map(ne).filter(Boolean).join(n.EOL+" ").replace(/(\r?\n\s*){3,}/g,n.EOL+n.EOL+" ");return"\nenum "+e.name+" {\n "+t+"\n}"}(e);case"generator":return function(e){var t=se(e.assignments);return"\ngenerator "+e.name+" {\n "+t+"\n}"}(e);case"model":case"view":case"type":return function(e){var t,r,a,i,u,o=(r=0,a=(t=e.properties).reduce((function(e,t,n,a){return"break"===t.type||(n>0&&"break"===a[n-1].type&&(e[++r]=[]),e[r].push(t)),e}),[[]]),i=a.map((function(e){return e.reduce((function(e,t){return Math.max(e,"field"===t.type?t.name.length:0)}),0)})),u=a.map((function(e){return e.reduce((function(e,t){return Math.max(e,"field"===t.type?ie(t).length:0)}),0)})),t.map((function(e,t,n){return t>0&&"break"!==e.type&&"break"===n[t-1].type&&(i.shift(),u.shift()),function(e,t,n){switch(void 0===t&&(t=0),void 0===n&&(n=0),e.type){case"attribute":return re(e);case"field":return function(e,t,n){void 0===t&&(t=0),void 0===n&&(n=0);var r=e.name.padEnd(t),a=ie(e).padEnd(n),i=e.attributes?e.attributes.map(re):[],u=e.comment;return[r,a].concat(i).filter(Boolean).join(" ").trim()+(u?" "+u:"")}(e,t,n);case"comment":return ee(e);case"break":return te();default:throw new Error("Unrecognized property type")}}(e,i[0],u[0])})).filter(Boolean).join(n.EOL+" ").replace(/(\r?\n\s*){3,}/g,n.EOL+n.EOL+" "));return"\n"+e.type+" "+e.name+" {\n "+o+"\n}"}(e);case"break":return te();default:throw new Error("Unrecognized block type")}var t,r}function ee(e){return e.text}function te(){return n.EOL}function ne(e){switch(e.type){case"enumerator":return[e.name,e.comment].filter(Boolean).join(" ");case"attribute":return re(e);case"comment":return ee(e);case"break":return te();default:throw new Error("Unexpected enumerator type")}}function re(e){var t=e.args&&e.args.length>0?"("+e.args.map(ae).filter(Boolean).join(", ")+")":"",n=[e.name];return e.group&&n.unshift(e.group),("field"===e.kind?"@":"@@")+n.join(".")+t}function ae(e){return oe(e.value)}function ie(e){var t=e.array?"[]":e.optional?"?":"";if("object"==typeof e.fieldType){if("function"===e.fieldType.type)return""+ue(e.fieldType)+t;throw new Error("Unexpected field type")}return""+e.fieldType+t}function ue(e){var t=e.params?e.params.map(oe):"";return e.name+"("+t+")"}function oe(e){if("object"==typeof e){if("type"in e)switch(e.type){case"keyValue":return e.key+": "+oe(e.value);case"function":return ue(e);case"array":return"["+(null!=e.args?e.args.map(oe).join(", "):"")+"]";default:throw new Error("Unexpected value type")}throw new Error("Unexpected object value")}return String(e)}function se(e){var t=0,r=e.reduce((function(e,n,r,a){return"break"===n.type||(r>0&&"break"===a[r-1].type&&(e[++t]=[]),e[t].push(n)),e}),[[]]).map((function(e){return e.reduce((function(e,t){return Math.max(e,"assignment"===t.type?t.key.length:0)}),0)}));return e.map((function(e,t,n){return t>0&&"break"!==e.type&&"break"===n[t-1].type&&r.shift(),function(e,t){switch(void 0===t&&(t=0),e.type){case"comment":return ee(e);case"break":return te();case"assignment":return e.key.padEnd(t)+" = "+oe(e.value);default:throw new Error("Unexpected assignment type")}}(e,r[0])})).filter(Boolean).join(n.EOL+" ").replace(/(\r?\n\s*){3,}/g,n.EOL+n.EOL+" ")}var ce=function(){function e(e){void 0===e&&(e=""),this.schema=void 0,this._subject=void 0,this._parent=void 0,this.schema=Z(e)}var t=e.prototype;return t.print=function(e){return void 0===e&&(e={}),X(this.schema,e)},t.getSchema=function(){return this.schema},t.generator=function(e,t){void 0===t&&(t="prisma-client-js");var n=this.schema.list.reduce((function(t,n){return"generator"===n.type&&n.name===e?n:t}),{type:"generator",name:e,assignments:[{type:"assignment",key:"provider",value:'"'+t+'"'}]});return this.schema.list.includes(n)||this.schema.list.push(n),this._subject=n,this},t.drop=function(e){var t=this.schema.list.findIndex((function(t){return"name"in t&&t.name===e}));return-1!==t&&this.schema.list.splice(t,1),this},t.datasource=function(e,t){var n={type:"datasource",name:"db",assignments:[{type:"assignment",key:"url",value:"string"==typeof t?'"'+t+'"':{type:"function",name:"env",params:['"'+t.env+'"']}},{type:"assignment",key:"provider",value:e}]},r=this.schema.list.findIndex((function(e){return"datasource"===e.type}));return this.schema.list.splice(r,-1!==r?1:0,n),this._subject=n,this},t.model=function(e){var t=this.schema.list.reduce((function(t,n){return"model"===n.type&&n.name===e?n:t}),{type:"model",name:e,properties:[]});return this.schema.list.includes(t)||this.schema.list.push(t),this._subject=t,this},t.view=function(e){var t=this.schema.list.reduce((function(t,n){return"view"===n.type&&n.name===e?n:t}),{type:"view",name:e,properties:[]});return this.schema.list.includes(t)||this.schema.list.push(t),this._subject=t,this},t.type=function(e){var t=this.schema.list.reduce((function(t,n){return"type"===n.type&&n.name===e?n:t}),{type:"type",name:e,properties:[]});return this.schema.list.includes(t)||this.schema.list.push(t),this._subject=t,this},t.enum=function(e,t){void 0===t&&(t=[]);var n=this.schema.list.reduce((function(t,n){return"enum"===n.type&&n.name===e?n:t}),{type:"enum",name:e,enumerators:t.map((function(e){return{type:"enumerator",name:e}}))});return this.schema.list.includes(n)||this.schema.list.push(n),this._subject=n,this},t.enumerator=function(e){var t=this.getSubject();if(!t||!("type"in t)||"enum"!==t.type)throw new Error("Subject must be a prisma enum!");return t.enumerators.push({type:"enumerator",name:e}),this},t.getSubject=function(){return this._subject},t.getParent=function(){return this._parent},t.blockAttribute=function(e,t){var n=this.getSubject();if(!V(n)){var r=this.getParent();if(!V(r))throw new Error("Subject must be a prisma model, view, or type!");n=this._subject=r}var a=t?"string"==typeof t?[{type:"attributeArgument",value:'"'+t+'"'}]:Array.isArray(t)?[{type:"attributeArgument",value:{type:"array",args:t}}]:Object.entries(t).map((function(e){return{type:"attributeArgument",value:{type:"keyValue",key:e[0],value:e[1]}}})):[];return n.properties.push({type:"attribute",kind:"object",name:e,args:a}),this},t.attribute=function(e,t){var n=this.getParent(),r=this.getSubject();if(!V(n))throw new Error("Parent must be a prisma model or view!");if(!F(r))throw new Error("Subject must be a prisma field!");r.attributes||(r.attributes=[]);var a=r.attributes.reduce((function(t,n){return"attribute"===n.type&&(n.group?n.group+".":"")+n.name===e?n:t}),{type:"attribute",kind:"field",name:e});if(Array.isArray(t)){var i=function e(t){var n,r;return"string"==typeof t?t:{type:"function",name:t.name,params:null!=(n=null==(r=t.function)?void 0:r.map(e))?n:[]}};t.length>0&&(a.args=t.map((function(e){return{type:"attributeArgument",value:i(e)}})))}else"object"==typeof t&&(a.args=Object.entries(t).map((function(e){return{type:"attributeArgument",value:{type:"keyValue",key:e[0],value:{type:"array",args:e[1]}}}})));return r.attributes.includes(a)||r.attributes.push(a),this},t.removeAttribute=function(e){var t=this.getParent(),n=this.getSubject();if(!V(t))throw new Error("Parent must be a prisma model or view!");if(!F(n))throw new Error("Subject must be a prisma field!");return n.attributes||(n.attributes=[]),n.attributes=n.attributes.filter((function(t){return!("attribute"===t.type&&t.name===e)})),this},t.assignment=function(e,t){var n=this.getSubject();if(!n||!("type"in n)||!["generator","datasource"].includes(n.type))throw new Error("Subject must be a prisma generator or datasource!");var r=n.assignments.reduce((function(n,r){return"assignment"===r.type&&r.key===e?function(e,n){return e.value='"'+t+'"',e}(r):n}),{type:"assignment",key:e,value:'"'+t+'"'});return n.assignments.includes(r)||n.assignments.push(r),this},t.blockInsert=function(e){var t=this.getSubject(),n=["datasource","enum","generator","model","view","type"];if(!t||!("type"in t)||!n.includes(t.type)){var r=this.getParent();if(!r||!("type"in r)||!n.includes(r.type))throw new Error("Subject must be a prisma block!");t=this._subject=r}switch(t.type){case"datasource":case"generator":t.assignments.push(e);break;case"enum":t.enumerators.push(e);break;case"model":t.properties.push(e)}return this},t.break=function(){return this.blockInsert({type:"break"})},t.comment=function(e,t){return void 0===t&&(t=!1),this.blockInsert({type:"comment",text:"//"+(t?"/":"")+" "+e})},t.schemaComment=function(e,t){return void 0===t&&(t=!1),this.schema.list.push({type:"comment",text:"//"+(t?"/":"")+" "+e}),this},t.field=function(e,t){void 0===t&&(t="String");var n=this.getSubject();if(!V(n)){var r=this.getParent();if(!V(r))throw new Error("Subject must be a prisma model or view or composite type!");n=this._subject=r}var a=n.properties.reduce((function(t,n){return"field"===n.type&&n.name===e?n:t}),{type:"field",name:e,fieldType:t});return n.properties.includes(a)||n.properties.push(a),this._parent=n,this._subject=a,this},t.removeField=function(e){var t=this.getSubject();if(!V(t)){var n=this.getParent();if(!V(n))throw new Error("Subject must be a prisma model or view or composite type!");t=this._subject=n}return t.properties=t.properties.filter((function(t){return!("field"===t.type&&t.name===e)})),this},t.then=function(e){return e(this._subject),this},e}();exports.ConcretePrismaSchemaBuilder=ce,exports.PrismaParser=z,exports.VisitorClassFactory=K,exports.createPrismaSchemaBuilder=function(e){return new ce(e)},exports.getSchema=Z,exports.printSchema=X; | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("chevrotain"),t=require("lilconfig"),n=require("os"),r=e.createToken({name:"Identifier",pattern:/[a-zA-Z]\w*/}),a=e.createToken({name:"Datasource",pattern:/datasource/,push_mode:"block"}),i=e.createToken({name:"Generator",pattern:/generator/,push_mode:"block"}),u=e.createToken({name:"Model",pattern:/model/,push_mode:"block"}),o=e.createToken({name:"View",pattern:/view/,push_mode:"block"}),s=e.createToken({name:"Enum",pattern:/enum/,push_mode:"block"}),c=e.createToken({name:"Type",pattern:/type/,push_mode:"block"}),m=e.createToken({name:"True",pattern:/true/,longer_alt:r}),p=e.createToken({name:"False",pattern:/false/,longer_alt:r}),l=e.createToken({name:"Null",pattern:/null/,longer_alt:r}),f=e.createToken({name:"Comment",pattern:e.Lexer.NA}),d=e.createToken({name:"DocComment",pattern:/\/\/\/[ \t]*(.*)/,categories:[f]}),L=e.createToken({name:"LineComment",pattern:/\/\/[ \t]*(.*)/,categories:[f]}),y=e.createToken({name:"Attribute",pattern:e.Lexer.NA}),h=e.createToken({name:"BlockAttribute",pattern:/@@/,label:"'@@'",categories:[y]}),v=e.createToken({name:"FieldAttribute",pattern:/@/,label:"'@'",categories:[y]}),E=e.createToken({name:"Dot",pattern:/\./,label:"'.'"}),b=e.createToken({name:"QuestionMark",pattern:/\?/,label:"'?'"}),g=e.createToken({name:"LCurly",pattern:/{/,label:"'{'"}),A=e.createToken({name:"RCurly",pattern:/}/,label:"'}'",pop_mode:!0}),U=e.createToken({name:"LRound",pattern:/\(/,label:"'('"}),k=e.createToken({name:"RRound",pattern:/\)/,label:"')'"}),S=e.createToken({name:"LSquare",pattern:/\[/,label:"'['"}),O=e.createToken({name:"RSquare",pattern:/\]/,label:"']'"}),T=e.createToken({name:"Comma",pattern:/,/,label:"','"}),B=e.createToken({name:"Colon",pattern:/:/,label:"':'"}),N=e.createToken({name:"Equals",pattern:/=/,label:"'='"}),C=e.createToken({name:"StringLiteral",pattern:/"(:?[^\\"\n\r]|\\(:?[bfnrtv"\\/]|u[0-9a-fA-F]{4}))*"/}),w=e.createToken({name:"NumberLiteral",pattern:/-?(0|[1-9]\d*)(\.\d+)?([eE][+-]?\d+)?/}),M=e.createToken({name:"WhiteSpace",pattern:/\s+/,group:e.Lexer.SKIPPED}),j=e.createToken({name:"LineBreak",pattern:/\n|\r\n/,line_breaks:!0,label:"LineBreak"}),R=[f,d,L,j,M],_={modes:{global:[].concat(R,[a,i,u,o,s,c]),block:[].concat(R,[y,h,v,E,b,g,A,S,O,U,k,T,B,N,m,p,l,C,w,r])},defaultMode:"global"},x=new e.Lexer(_);function P(e,t){e.prototype=Object.create(t.prototype),e.prototype.constructor=e,D(e,t)}function D(e,t){return D=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(e,t){return e.__proto__=t,e},D(e,t)}var I=["model","view","type"];function V(e){return null!=e&&"type"in e&&I.includes(e.type)}function F(e){return null!=e&&"type"in e&&"field"===e.type}function q(e){for(var t=arguments.length,n=new Array(t>1?t-1:0),r=1;r<t;r++)n[r-1]=arguments[r];var a=n.reduce((function(e,t){if(!t)return e;var n=e.endColumn,r=void 0===n?-Infinity:n,a=e.endLine,i=void 0===a?-Infinity:a,u=e.endOffset,o=void 0===u?-Infinity:u,s=e.startColumn,c=void 0===s?Infinity:s,m=e.startLine,p=e.startOffset,l=void 0===p?Infinity:p;return null!=t.startLine&&t.startLine<(void 0===m?Infinity:m)&&(e.startLine=t.startLine),null!=t.startColumn&&t.startColumn<c&&(e.startColumn=t.startColumn),null!=t.startOffset&&t.startOffset<l&&(e.startOffset=t.startOffset),null!=t.endLine&&t.endLine>i&&(e.endLine=t.endLine),null!=t.endColumn&&t.endColumn>r&&(e.endColumn=t.endColumn),null!=t.endOffset&&t.endOffset>o&&(e.endOffset=t.endOffset),e}),{});return Object.assign(e,{location:a})}var G,Y={parser:{nodeLocationTracking:"none"}},z=function(e){function t(t){var n;return(n=e.call(this,_,t)||this).config=void 0,n.break=n.RULE("break",(function(){n.CONSUME1(j),n.CONSUME2(j)})),n.keyedArg=n.RULE("keyedArg",(function(){n.CONSUME(r,{LABEL:"keyName"}),n.CONSUME(B),n.SUBRULE(n.value)})),n.array=n.RULE("array",(function(){n.CONSUME(S),n.MANY_SEP({SEP:T,DEF:function(){n.SUBRULE(n.value)}}),n.CONSUME(O)})),n.func=n.RULE("func",(function(){n.CONSUME(r,{LABEL:"funcName"}),n.CONSUME(U),n.MANY_SEP({SEP:T,DEF:function(){n.OR([{ALT:function(){return n.SUBRULE(n.keyedArg)}},{ALT:function(){return n.SUBRULE(n.value)}}])}}),n.CONSUME(k)})),n.value=n.RULE("value",(function(){n.OR([{ALT:function(){return n.CONSUME(C,{LABEL:"value"})}},{ALT:function(){return n.CONSUME(w,{LABEL:"value"})}},{ALT:function(){return n.SUBRULE(n.array,{LABEL:"value"})}},{ALT:function(){return n.SUBRULE(n.func,{LABEL:"value"})}},{ALT:function(){return n.CONSUME(m,{LABEL:"value"})}},{ALT:function(){return n.CONSUME(p,{LABEL:"value"})}},{ALT:function(){return n.CONSUME(l,{LABEL:"value"})}},{ALT:function(){return n.CONSUME(r,{LABEL:"value"})}}])})),n.property=n.RULE("property",(function(){n.CONSUME(r,{LABEL:"propertyName"}),n.CONSUME(N),n.SUBRULE(n.value,{LABEL:"propertyValue"})})),n.assignment=n.RULE("assignment",(function(){n.CONSUME(r,{LABEL:"assignmentName"}),n.CONSUME(N),n.SUBRULE(n.value,{LABEL:"assignmentValue"})})),n.field=n.RULE("field",(function(){n.CONSUME(r,{LABEL:"fieldName"}),n.SUBRULE(n.value,{LABEL:"fieldType"}),n.OPTION1((function(){n.OR([{ALT:function(){n.CONSUME(S,{LABEL:"array"}),n.CONSUME(O,{LABEL:"array"})}},{ALT:function(){return n.CONSUME(b,{LABEL:"optional"})}}])})),n.MANY((function(){n.SUBRULE(n.attribute,{LABEL:"attributeList"})})),n.OPTION2((function(){n.CONSUME(f,{LABEL:"comment"})}))})),n.block=n.RULE("block",(function(e){void 0===e&&(e={});var t=e.componentType,r="enum"===t,a="model"===t||"view"===t||"type"===t;n.CONSUME(g),n.CONSUME1(j),n.MANY((function(){n.OR([{ALT:function(){return n.SUBRULE(n.comment,{LABEL:"list"})}},{GATE:function(){return a},ALT:function(){return n.SUBRULE(n.property,{LABEL:"list"})}},{ALT:function(){return n.SUBRULE(n.attribute,{LABEL:"list"})}},{GATE:function(){return a},ALT:function(){return n.SUBRULE(n.field,{LABEL:"list"})}},{GATE:function(){return r},ALT:function(){return n.SUBRULE(n.enum,{LABEL:"list"})}},{GATE:function(){return!a},ALT:function(){return n.SUBRULE(n.assignment,{LABEL:"list"})}},{ALT:function(){return n.SUBRULE(n.break,{LABEL:"list"})}},{ALT:function(){return n.CONSUME2(j)}}])})),n.CONSUME(A)})),n.enum=n.RULE("enum",(function(){n.CONSUME(r,{LABEL:"enumName"}),n.OPTION((function(){n.CONSUME(f,{LABEL:"comment"})}))})),n.attribute=n.RULE("attribute",(function(){n.OR1([{ALT:function(){return n.CONSUME(h,{LABEL:"blockAttribute"})}},{ALT:function(){return n.CONSUME(v,{LABEL:"fieldAttribute"})}}]),n.OR2([{ALT:function(){n.CONSUME1(r,{LABEL:"groupName"}),n.CONSUME(E),n.CONSUME2(r,{LABEL:"attributeName"})}},{ALT:function(){return n.CONSUME(r,{LABEL:"attributeName"})}}]),n.OPTION((function(){n.CONSUME(U),n.MANY_SEP({SEP:T,DEF:function(){n.SUBRULE(n.attributeArg)}}),n.CONSUME(k)}))})),n.attributeArg=n.RULE("attributeArg",(function(){n.OR([{ALT:function(){return n.SUBRULE(n.keyedArg,{LABEL:"value"})}},{ALT:function(){return n.SUBRULE(n.value,{LABEL:"value"})}}])})),n.component=n.RULE("component",(function(){var e=n.OR1([{ALT:function(){return n.CONSUME(a,{LABEL:"type"})}},{ALT:function(){return n.CONSUME(i,{LABEL:"type"})}},{ALT:function(){return n.CONSUME(u,{LABEL:"type"})}},{ALT:function(){return n.CONSUME(o,{LABEL:"type"})}},{ALT:function(){return n.CONSUME(s,{LABEL:"type"})}},{ALT:function(){return n.CONSUME(c,{LABEL:"type"})}}]);n.OR2([{ALT:function(){n.CONSUME1(r,{LABEL:"groupName"}),n.CONSUME(E),n.CONSUME2(r,{LABEL:"componentName"})}},{ALT:function(){return n.CONSUME(r,{LABEL:"componentName"})}}]),n.SUBRULE(n.block,{ARGS:[{componentType:e.image}]})})),n.comment=n.RULE("comment",(function(){n.CONSUME(f,{LABEL:"text"})})),n.schema=n.RULE("schema",(function(){n.MANY((function(){n.OR([{ALT:function(){return n.SUBRULE(n.comment,{LABEL:"list"})}},{ALT:function(){return n.SUBRULE(n.component,{LABEL:"list"})}},{ALT:function(){return n.SUBRULE(n.break,{LABEL:"list"})}},{ALT:function(){return n.CONSUME(j)}}])}))})),n.performSelfAnalysis(),n.config=t,n}return P(t,e),t}(e.CstParser),W=new z(function(){if(null!=G)return G;var e=t.lilconfigSync("prisma-ast").search();return G=Object.assign(Y,null==e?void 0:e.config)}().parser),K=function(e){return function(t){function n(){var e;return(e=t.call(this)||this).validateVisitor(),e}P(n,t);var r=n.prototype;return r.schema=function(e){var t,n=this;return{type:"schema",list:(null==(t=e.list)?void 0:t.map((function(e){return n.visit([e])})))||[]}},r.component=function(e){var t=e.type[0],n=e.componentName[0],r=this.visit(e.block),a=function(){switch(t.image){case"datasource":return{type:"datasource",name:n.image,assignments:r};case"generator":return{type:"generator",name:n.image,assignments:r};case"model":return{type:"model",name:n.image,properties:r};case"view":return{type:"view",name:n.image,properties:r};case"enum":return{type:"enum",name:n.image,enumerators:r};case"type":return{type:"type",name:n.image,properties:r};default:throw new Error("Unexpected block type: "+t)}}();return this.maybeAppendLocationData(a,t,n)},r.break=function(){return{type:"break"}},r.comment=function(e){var t=e.text[0];return this.maybeAppendLocationData({type:"comment",text:t.image},t)},r.block=function(e){var t,n=this;return null==(t=e.list)?void 0:t.map((function(e){return n.visit([e])}))},r.assignment=function(e){var t=this.visit(e.assignmentValue),n=e.assignmentName[0];return this.maybeAppendLocationData({type:"assignment",key:n.image,value:t},n)},r.field=function(e){var t,n,r,a=this,i=this.visit(e.fieldType),u=e.fieldName[0],o=e.attributeList&&e.attributeList.map((function(e){return a.visit([e])})),s=null==(t=e.comment)||null==(t=t[0])?void 0:t.image;return this.maybeAppendLocationData({type:"field",name:u.image,fieldType:i,array:null!=e.array,optional:null!=e.optional,attributes:o,comment:s},u,null==(n=e.optional)?void 0:n[0],null==(r=e.array)?void 0:r[0])},r.attribute=function(e){var t=this,n=e.attributeName[0],r=(e.groupName||[{}])[0],a=e.attributeArg&&e.attributeArg.map((function(e){return t.visit(e)})),i=null!=e.blockAttribute?"object":"field";return this.maybeAppendLocationData.apply(this,[{type:"attribute",name:n.image,kind:i,group:r.image,args:a},n].concat("object"===i?e.blockAttribute:e.fieldAttribute,[r]))},r.attributeArg=function(e){return{type:"attributeArgument",value:this.visit(e.value)}},r.func=function(e){var t=this,n=e.funcName[0],r=e.value&&e.value.map((function(e){return t.visit([e])})),a=e.keyedArg&&e.keyedArg.map((function(e){return t.visit([e])})),i=(r||a)&&[].concat(null!=r?r:[],null!=a?a:[]);return this.maybeAppendLocationData({type:"function",name:n.image,params:i},n)},r.array=function(e){var t=this;return{type:"array",args:e.value&&e.value.map((function(e){return t.visit([e])}))}},r.keyedArg=function(e){var t=e.keyName[0],n=this.visit(e.value);return this.maybeAppendLocationData({type:"keyValue",key:t.image,value:n},t)},r.value=function(e){return"image"in e.value[0]?e.value[0].image:this.visit(e.value)},r.enum=function(e){var t,n=e.enumName[0],r=null==(t=e.comment)||null==(t=t[0])?void 0:t.image;return this.maybeAppendLocationData({type:"enumerator",name:n.image,comment:r},n)},r.maybeAppendLocationData=function(t){if("none"===e.config.nodeLocationTracking)return t;for(var n=arguments.length,r=new Array(n>1?n-1:0),a=1;a<n;a++)r[a-1]=arguments[a];return q.apply(void 0,[t].concat(r))},n}(e.getBaseCstVisitorConstructorWithDefaults())},Q=new(K(W));function Z(e,t){var n,r,a=x.tokenize(e),i=null!=(n=null==t?void 0:t.parser)?n:W;i.input=a.tokens;var u=i.schema();if(i.errors.length>0)throw i.errors[0];return(null!=(r=null==t?void 0:t.visitor)?r:Q).visit(u)}var H=["break","comment"],J=["generator","datasource","model","view","enum","break","comment"];function X(e,t){void 0===t&&(t={});var r=t.sort,a=t.locales,i=void 0===a?void 0:a,u=t.sortOrder,o=void 0===u?void 0:u,s=e.list;if(void 0!==r&&r){s=e.list=s.filter((function(e){return"break"!==e.type}));var c=function(e,t,n){return void 0===n&&(n=J),function(r,a){if(-1!==H.indexOf(r.type)!=(-1!==H.indexOf(a.type)))return e.list.indexOf(r)-e.list.indexOf(a);n!==J&&(n=n.concat(J));var i=n.indexOf(r.type)-n.indexOf(a.type);return 0!==i?i:"name"in r&&"name"in a?r.name.localeCompare(a.name,t):0}}(e,i,o);s.sort(c)}return s.map($).filter(Boolean).join(n.EOL).replace(/(\r?\n\s*){3,}/g,n.EOL+n.EOL)+n.EOL}function $(e){switch(e.type){case"comment":return ee(e);case"datasource":return r=se((t=e).assignments),"\ndatasource "+t.name+" {\n "+r+"\n}";case"enum":return function(e){var t=e.enumerators.map(ne).filter(Boolean).join(n.EOL+" ").replace(/(\r?\n\s*){3,}/g,n.EOL+n.EOL+" ");return"\nenum "+e.name+" {\n "+t+"\n}"}(e);case"generator":return function(e){var t=se(e.assignments);return"\ngenerator "+e.name+" {\n "+t+"\n}"}(e);case"model":case"view":case"type":return function(e){var t,r,a,i,u,o=(r=0,a=(t=e.properties).reduce((function(e,t,n,a){return"break"===t.type||(n>0&&"break"===a[n-1].type&&(e[++r]=[]),e[r].push(t)),e}),[[]]),i=a.map((function(e){return e.reduce((function(e,t){return Math.max(e,"field"===t.type?t.name.length:0)}),0)})),u=a.map((function(e){return e.reduce((function(e,t){return Math.max(e,"field"===t.type?ie(t).length:0)}),0)})),t.map((function(e,t,n){return t>0&&"break"!==e.type&&"break"===n[t-1].type&&(i.shift(),u.shift()),function(e,t,n){switch(void 0===t&&(t=0),void 0===n&&(n=0),e.type){case"attribute":return re(e);case"field":return function(e,t,n){void 0===t&&(t=0),void 0===n&&(n=0);var r=e.name.padEnd(t),a=ie(e).padEnd(n),i=e.attributes?e.attributes.map(re):[],u=e.comment;return[r,a].concat(i).filter(Boolean).join(" ").trim()+(u?" "+u:"")}(e,t,n);case"comment":return ee(e);case"break":return te();default:throw new Error("Unrecognized property type")}}(e,i[0],u[0])})).filter(Boolean).join(n.EOL+" ").replace(/(\r?\n\s*){3,}/g,n.EOL+n.EOL+" "));return"\n"+e.type+" "+e.name+" {\n "+o+"\n}"}(e);case"break":return te();default:throw new Error("Unrecognized block type")}var t,r}function ee(e){return e.text}function te(){return n.EOL}function ne(e){switch(e.type){case"enumerator":return[e.name,e.comment].filter(Boolean).join(" ");case"attribute":return re(e);case"comment":return ee(e);case"break":return te();default:throw new Error("Unexpected enumerator type")}}function re(e){var t=e.args&&e.args.length>0?"("+e.args.map(ae).filter(Boolean).join(", ")+")":"",n=[e.name];return e.group&&n.unshift(e.group),("field"===e.kind?"@":"@@")+n.join(".")+t}function ae(e){return oe(e.value)}function ie(e){var t=e.array?"[]":e.optional?"?":"";if("object"==typeof e.fieldType){if("function"===e.fieldType.type)return""+ue(e.fieldType)+t;throw new Error("Unexpected field type")}return""+e.fieldType+t}function ue(e){var t=e.params?e.params.map(oe):"";return e.name+"("+t+")"}function oe(e){if("object"==typeof e){if("type"in e)switch(e.type){case"keyValue":return e.key+": "+oe(e.value);case"function":return ue(e);case"array":return"["+(null!=e.args?e.args.map(oe).join(", "):"")+"]";default:throw new Error("Unexpected value type")}throw new Error("Unexpected object value")}return String(e)}function se(e){var t=0,r=e.reduce((function(e,n,r,a){return"break"===n.type||(r>0&&"break"===a[r-1].type&&(e[++t]=[]),e[t].push(n)),e}),[[]]).map((function(e){return e.reduce((function(e,t){return Math.max(e,"assignment"===t.type?t.key.length:0)}),0)}));return e.map((function(e,t,n){return t>0&&"break"!==e.type&&"break"===n[t-1].type&&r.shift(),function(e,t){switch(void 0===t&&(t=0),e.type){case"comment":return ee(e);case"break":return te();case"assignment":return e.key.padEnd(t)+" = "+oe(e.value);default:throw new Error("Unexpected assignment type")}}(e,r[0])})).filter(Boolean).join(n.EOL+" ").replace(/(\r?\n\s*){3,}/g,n.EOL+n.EOL+" ")}var ce=function(){function e(e){void 0===e&&(e=""),this.schema=void 0,this._subject=void 0,this._parent=void 0,this.schema=Z(e)}var t=e.prototype;return t.print=function(e){return void 0===e&&(e={}),X(this.schema,e)},t.getSchema=function(){return this.schema},t.generator=function(e,t){void 0===t&&(t="prisma-client-js");var n=this.schema.list.reduce((function(t,n){return"generator"===n.type&&n.name===e?n:t}),{type:"generator",name:e,assignments:[{type:"assignment",key:"provider",value:'"'+t+'"'}]});return this.schema.list.includes(n)||this.schema.list.push(n),this._subject=n,this},t.drop=function(e){var t=this.schema.list.findIndex((function(t){return"name"in t&&t.name===e}));return-1!==t&&this.schema.list.splice(t,1),this},t.datasource=function(e,t){var n={type:"datasource",name:"db",assignments:[{type:"assignment",key:"url",value:"string"==typeof t?'"'+t+'"':{type:"function",name:"env",params:['"'+t.env+'"']}},{type:"assignment",key:"provider",value:e}]},r=this.schema.list.findIndex((function(e){return"datasource"===e.type}));return this.schema.list.splice(r,-1!==r?1:0,n),this._subject=n,this},t.model=function(e){var t=this.schema.list.reduce((function(t,n){return"model"===n.type&&n.name===e?n:t}),{type:"model",name:e,properties:[]});return this.schema.list.includes(t)||this.schema.list.push(t),this._subject=t,this},t.view=function(e){var t=this.schema.list.reduce((function(t,n){return"view"===n.type&&n.name===e?n:t}),{type:"view",name:e,properties:[]});return this.schema.list.includes(t)||this.schema.list.push(t),this._subject=t,this},t.type=function(e){var t=this.schema.list.reduce((function(t,n){return"type"===n.type&&n.name===e?n:t}),{type:"type",name:e,properties:[]});return this.schema.list.includes(t)||this.schema.list.push(t),this._subject=t,this},t.enum=function(e,t){void 0===t&&(t=[]);var n=this.schema.list.reduce((function(t,n){return"enum"===n.type&&n.name===e?n:t}),{type:"enum",name:e,enumerators:t.map((function(e){return{type:"enumerator",name:e}}))});return this.schema.list.includes(n)||this.schema.list.push(n),this._subject=n,this},t.enumerator=function(e){var t=this.getSubject();if(!t||!("type"in t)||"enum"!==t.type)throw new Error("Subject must be a prisma enum!");return t.enumerators.push({type:"enumerator",name:e}),this},t.getSubject=function(){return this._subject},t.getParent=function(){return this._parent},t.blockAttribute=function(e,t){var n=this.getSubject();if(!V(n)){var r=this.getParent();if(!V(r))throw new Error("Subject must be a prisma model, view, or type!");n=this._subject=r}var a=t?"string"==typeof t?[{type:"attributeArgument",value:'"'+t+'"'}]:Array.isArray(t)?[{type:"attributeArgument",value:{type:"array",args:t}}]:Object.entries(t).map((function(e){return{type:"attributeArgument",value:{type:"keyValue",key:e[0],value:e[1]}}})):[];return n.properties.push({type:"attribute",kind:"object",name:e,args:a}),this},t.attribute=function(e,t){var n=this.getParent(),r=this.getSubject();if(!V(n))throw new Error("Parent must be a prisma model or view!");if(!F(r))throw new Error("Subject must be a prisma field!");r.attributes||(r.attributes=[]);var a=r.attributes.reduce((function(t,n){return"attribute"===n.type&&(n.group?n.group+".":"")+n.name===e?n:t}),{type:"attribute",kind:"field",name:e});if(Array.isArray(t)){var i=function e(t){var n,r;return"string"==typeof t?t:{type:"function",name:t.name,params:null!=(n=null==(r=t.function)?void 0:r.map(e))?n:[]}};t.length>0&&(a.args=t.map((function(e){return{type:"attributeArgument",value:i(e)}})))}else"object"==typeof t&&(a.args=Object.entries(t).map((function(e){return{type:"attributeArgument",value:{type:"keyValue",key:e[0],value:{type:"array",args:e[1]}}}})));return r.attributes.includes(a)||r.attributes.push(a),this},t.removeAttribute=function(e){var t=this.getParent(),n=this.getSubject();if(!V(t))throw new Error("Parent must be a prisma model or view!");if(!F(n))throw new Error("Subject must be a prisma field!");return n.attributes||(n.attributes=[]),n.attributes=n.attributes.filter((function(t){return!("attribute"===t.type&&t.name===e)})),this},t.assignment=function(e,t){var n=this.getSubject();if(!n||!("type"in n)||!["generator","datasource"].includes(n.type))throw new Error("Subject must be a prisma generator or datasource!");var r=n.assignments.reduce((function(n,r){return"assignment"===r.type&&r.key===e?function(e,n){return e.value='"'+t+'"',e}(r):n}),{type:"assignment",key:e,value:'"'+t+'"'});return n.assignments.includes(r)||n.assignments.push(r),this},t.blockInsert=function(e){var t=this.getSubject(),n=["datasource","enum","generator","model","view","type"];if(!t||!("type"in t)||!n.includes(t.type)){var r=this.getParent();if(!r||!("type"in r)||!n.includes(r.type))throw new Error("Subject must be a prisma block!");t=this._subject=r}switch(t.type){case"datasource":case"generator":t.assignments.push(e);break;case"enum":t.enumerators.push(e);break;case"model":t.properties.push(e)}return this},t.break=function(){return this.blockInsert({type:"break"})},t.comment=function(e,t){return void 0===t&&(t=!1),this.blockInsert({type:"comment",text:"//"+(t?"/":"")+" "+e})},t.schemaComment=function(e,t){return void 0===t&&(t=!1),this.schema.list.push({type:"comment",text:"//"+(t?"/":"")+" "+e}),this},t.field=function(e,t){void 0===t&&(t="String");var n=this.getSubject();if(!V(n)){var r=this.getParent();if(!V(r))throw new Error("Subject must be a prisma model or view or composite type!");n=this._subject=r}var a=n.properties.reduce((function(t,n){return"field"===n.type&&n.name===e?n:t}),{type:"field",name:e,fieldType:t});return n.properties.includes(a)||n.properties.push(a),this._parent=n,this._subject=a,this},t.removeField=function(e){var t=this.getSubject();if(!V(t)){var n=this.getParent();if(!V(n))throw new Error("Subject must be a prisma model or view or composite type!");t=this._subject=n}return t.properties=t.properties.filter((function(t){return!("field"===t.type&&t.name===e)})),this},t.then=function(e){return e(this._subject),this},e}();function me(e){return new ce(e)}exports.ConcretePrismaSchemaBuilder=ce,exports.PrismaParser=z,exports.VisitorClassFactory=K,exports.createPrismaSchemaBuilder=me,exports.getSchema=Z,exports.printSchema=X,exports.produceSchema=function(e,t,n){void 0===n&&(n={});var r=me(e);return t(r),r.print(n)}; | ||
//# sourceMappingURL=prisma-ast.cjs.production.min.js.map |
@@ -1482,3 +1482,12 @@ import { Lexer, createToken, CstParser } from 'chevrotain'; | ||
export { ConcretePrismaSchemaBuilder, PrismaParser, VisitorClassFactory, createPrismaSchemaBuilder, getSchema, printSchema }; | ||
function produceSchema(source, producer, options) { | ||
if (options === void 0) { | ||
options = {}; | ||
} | ||
var builder = createPrismaSchemaBuilder(source); | ||
producer(builder); | ||
return builder.print(options); | ||
} | ||
export { ConcretePrismaSchemaBuilder, PrismaParser, VisitorClassFactory, createPrismaSchemaBuilder, getSchema, printSchema, produceSchema }; | ||
//# sourceMappingURL=prisma-ast.esm.js.map |
{ | ||
"version": "0.9.0", | ||
"version": "0.9.1", | ||
"license": "MIT", | ||
@@ -4,0 +4,0 @@ "main": "dist/index.js", |
371
README.md
@@ -28,3 +28,3 @@ # @mrleebo/prisma-ast | ||
const input = ` | ||
const source = ` | ||
model User { | ||
@@ -117,371 +117,6 @@ id Int @id @default(autoincrement()) | ||
### Set a datasource | ||
### Need More SchemaBuilder Code snippets? | ||
Since a schema can only have one datasource, calling this command will override the existing datasource if the schema already has one, or create a datasource block if it doesn't. | ||
There is a lot that you can do with the schema builder. There are [additional sample references available](./EXAMPLES.md) for you to explore. | ||
```ts | ||
datasource(provider: string, url: string | { env: string }) | ||
``` | ||
You can set a datasource by passing in the provider and url parameters. | ||
```ts | ||
builder.datasource('postgresql', { env: 'DATABASE_URL' }); | ||
``` | ||
```prisma | ||
datasource db { | ||
provider = "postgresql" | ||
url = env("DATABASE_URL") | ||
} | ||
``` | ||
### Access a datasource programmatically | ||
If you want to perform a custom action that there isn't a Builder method for, you can access the underlying schema object programmatically. | ||
```ts | ||
import { Datasource } from '@mrleebo/prisma-ast'; | ||
// rename the datasource programmatically | ||
builder | ||
.datasource('postgresql', { env: 'DATABASE_URL' }) | ||
.then<Datasource>((datasource) => { | ||
datasource.name = 'DS'; | ||
}); | ||
``` | ||
```prisma | ||
datasource DS { | ||
provider = "postgresql" | ||
url = env("DATABASE_URL") | ||
} | ||
``` | ||
### Add or update a generator | ||
```ts | ||
generator(name: string, provider: string) | ||
``` | ||
If the schema already has a generator with the given name, it will be updated. Otherwise, a new generator will be created. | ||
```ts | ||
builder.generator('nexusPrisma', 'nexus-prisma'); | ||
``` | ||
```prisma | ||
generator nexusPrisma { | ||
provider = "nexus-prisma" | ||
} | ||
``` | ||
### Adding additional assignments to generators | ||
```ts | ||
assignment(key: string, value: string) | ||
``` | ||
If your generator accepts additional assignments, they can be added by chaining .assignment() calls to your generator. | ||
```ts | ||
builder.generator('client', 'prisma-client-js').assignment('output', 'db.js'); | ||
``` | ||
```prisma | ||
generator client { | ||
provider = "prisma-client-js" | ||
output = "db.js" | ||
} | ||
``` | ||
### Access a generator programmatically | ||
If you want to perform a custom action that there isn't a Builder method for, you can access the underlying schema object programmatically. | ||
```prisma | ||
generator client { | ||
provider = "prisma-client-js" | ||
output = "db.js" | ||
} | ||
``` | ||
```ts | ||
import { Generator } from '@mrleebo/prisma-ast'; | ||
// rename the generator programmatically | ||
builder.generator('client').then<Generator>((generator) => { | ||
generator.name = 'foo'; | ||
}); | ||
``` | ||
```prisma | ||
generator foo { | ||
provider = "prisma-client-js" | ||
output = "db.js" | ||
} | ||
``` | ||
### Add or update a model | ||
If the model with that name already exists in the schema, it will be selected and any fields that follow will be appended to the model. Otherwise, the model will be created and added to the schema. | ||
```ts | ||
builder.model('Project').field('name', 'String'); | ||
``` | ||
```prisma | ||
model Project { | ||
name String | ||
} | ||
``` | ||
### Add or update a view | ||
If the view with that name already exists in the schema, it will be selected and any fields that follow will be appended to the view. Otherwise, the view will be created and added to the schema. | ||
```ts | ||
builder.view('Project').field('name', 'String'); | ||
``` | ||
```prisma | ||
model Project { | ||
name String | ||
} | ||
``` | ||
### Add or update a composite type | ||
If the composite type with that name already exists in the schema, it will be selected and any fields that follow will be appended to the type. Otherwise, the composite type will be created and added to the schema. | ||
```ts | ||
builder.type('Photo').field('width', 'Int').field('height', 'Int').field('url', 'String'); | ||
``` | ||
```prisma | ||
type Photo { | ||
width Int | ||
height Int | ||
url String | ||
} | ||
``` | ||
### Access a model programmatically | ||
If you want to perform a custom action that there isn't a Builder method for, you can access the underlying schema object programmatically. | ||
```prisma | ||
model Project { | ||
name String | ||
} | ||
``` | ||
```ts | ||
import { Model } from '@mrleebo/prisma-ast'; | ||
// rename the datasource programmatically | ||
builder.model('Project').then<Model>((model) => { | ||
model.name = 'Task'; | ||
}); | ||
``` | ||
```prisma | ||
model Task { | ||
name String | ||
} | ||
``` | ||
### Add a field with an attribute to a model | ||
If the entered model name already exists, that model will be used as the subject for any field and attribute calls that follow. | ||
```prisma | ||
model Project { | ||
name String | ||
} | ||
``` | ||
```ts | ||
builder.model('Project').field('projectCode', 'String').attribute('unique'); | ||
``` | ||
```prisma | ||
model Project { | ||
name String | ||
projectCode String @unique | ||
} | ||
``` | ||
### Add an attribute to an existing field | ||
If the field already exists, you can add new attributes to it by making calls to `.attribute()`. | ||
```prisma | ||
model Project { | ||
name String | ||
projectCode String @unique | ||
} | ||
``` | ||
```ts | ||
builder.model('Project').field('name').attribute('unique'); | ||
``` | ||
```prisma | ||
model Project { | ||
name String @unique | ||
projectCode String @unique | ||
} | ||
``` | ||
### Remove a field | ||
You can remove an existing field with `.removeField()`. | ||
```prisma | ||
model Project { | ||
name String | ||
projectCode String @unique | ||
} | ||
``` | ||
```ts | ||
builder.model('Project').removeField('projectCode'); | ||
``` | ||
```prisma | ||
model Project { | ||
name String | ||
} | ||
``` | ||
### Remove an attribute from an existing field | ||
You can remove an attribute from a field with `.removeAttribute()`. | ||
```prisma | ||
model Project { | ||
name String | ||
projectCode String @unique | ||
} | ||
``` | ||
```ts | ||
builder.model('Project').field('projectCode').removeAttribute('unique'); | ||
``` | ||
```prisma | ||
model Project { | ||
name String | ||
projectCode String | ||
} | ||
``` | ||
### Access a field programmatically | ||
If you want to perform a custom action that there isn't a Builder method for, you can access the underlying schema object programmatically. | ||
```prisma | ||
model TaskMessage { | ||
createdAt DateTime? @db.Timestamptz(6) | ||
} | ||
``` | ||
```ts | ||
import { Field, Attribute } from '@mrleebo/prisma-ast'; | ||
// Replace the @db.Timestamptz(6) attribute with @default(now()) | ||
builder | ||
.model('TaskMessage') | ||
.field('createdAt') | ||
.then<Field>((field) => { | ||
const attribute: Attribute = { | ||
type: 'attribute', | ||
kind: 'field', | ||
name: 'default', | ||
args: [{ type: 'attributeArgument', value: 'now()' }], | ||
}; | ||
field.attributes = [attribute]; | ||
}); | ||
``` | ||
```prisma | ||
model TaskMessage { | ||
createdAt DateTime? @default(now()) | ||
} | ||
``` | ||
### Add an index to a model | ||
```prisma | ||
model Project { | ||
name String | ||
projectCode String @unique | ||
} | ||
``` | ||
```ts | ||
builder.model('Project').blockAttribute('index', ['name']); | ||
``` | ||
```prisma | ||
model Project { | ||
name String | ||
projectCode String @unique | ||
@@index([name]) | ||
} | ||
``` | ||
### Add an enum | ||
```ts | ||
builder.enum('Role', ['USER', 'ADMIN']); | ||
``` | ||
```prisma | ||
enum Role { | ||
USER | ||
ADMIN | ||
} | ||
``` | ||
Additional enumerators can also be added to an existing Enum | ||
```ts | ||
builder | ||
.enum('Role') | ||
.break() | ||
.comment('New role added for feature #12') | ||
.enumerator('ORGANIZATION'); | ||
``` | ||
```prisma | ||
enum Role { | ||
USER | ||
ADMIN | ||
// New role added for feature #12 | ||
ORGANIZATION | ||
} | ||
``` | ||
### Comments and Line breaks are also parsed and can be added to the schema | ||
```ts | ||
builder | ||
.model('Project') | ||
.break() | ||
.comment('I wish I could add a color to your rainbow'); | ||
``` | ||
```prisma | ||
model Project { | ||
name String | ||
projectCode String @unique | ||
@@index([name]) | ||
// I wish I could add a color to your rainbow | ||
} | ||
``` | ||
## Configuration Options | ||
@@ -488,0 +123,0 @@ |
@@ -0,1 +1,2 @@ | ||
export * from './produceSchema'; | ||
export * from './getSchema'; | ||
@@ -2,0 +3,0 @@ export * from './printSchema'; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
5026
498936
191