@mrleebo/prisma-ast
Advanced tools
Comparing version 0.5.0 to 0.5.1
@@ -1368,2 +1368,21 @@ 'use strict'; | ||
_proto.removeAttribute = function removeAttribute(name) { | ||
var parent = this.getParent(); | ||
var subject = this.getSubject(); | ||
if (!parent || !('type' in parent) || parent.type !== 'model') { | ||
throw new Error('Parent must be a prisma model!'); | ||
} | ||
if (!subject || !('type' in subject) || subject.type !== 'field') { | ||
throw new Error('Subject must be a prisma field!'); | ||
} | ||
if (!subject.attributes) subject.attributes = []; | ||
subject.attributes = subject.attributes.filter(function (attr) { | ||
return !(attr.type === 'attribute' && attr.name === name); | ||
}); | ||
return this; | ||
}; | ||
_proto.assignment = function assignment(key, value) { | ||
@@ -1492,2 +1511,17 @@ var subject = this.getSubject(); | ||
_proto.removeField = function removeField(name) { | ||
var subject = this.getSubject(); | ||
if (!subject || !('type' in subject) || subject.type !== 'model') { | ||
var parent = this.getParent(); | ||
if (!parent || !('type' in parent) || parent.type !== 'model') throw new Error('Subject must be a prisma model!'); | ||
subject = this._subject = parent; | ||
} | ||
subject.properties = subject.properties.filter(function (field) { | ||
return !(field.type === 'field' && field.name === name); | ||
}); | ||
return this; | ||
}; | ||
_proto.then = function then(callback) { | ||
@@ -1494,0 +1528,0 @@ callback(this._subject); |
@@ -1,2 +0,2 @@ | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("chevrotain"),t=require("os"),n=e.createToken({name:"Identifier",pattern:/[a-zA-Z]\w*/}),r=e.createToken({name:"Datasource",pattern:/datasource/,push_mode:"block"}),a=e.createToken({name:"Generator",pattern:/generator/,push_mode:"block"}),u=e.createToken({name:"Model",pattern:/model/,push_mode:"block"}),i=e.createToken({name:"Enum",pattern:/enum/,push_mode:"block"}),o=e.createToken({name:"True",pattern:/true/,longer_alt:n}),c=e.createToken({name:"False",pattern:/false/,longer_alt:n}),s=e.createToken({name:"Null",pattern:/null/,longer_alt:n}),m=e.createToken({name:"Comment",pattern:e.Lexer.NA}),l=e.createToken({name:"DocComment",pattern:/\/\/\/\s*(.+)/,categories:[m]}),p=e.createToken({name:"LineComment",pattern:/\/\/\s*(.+)/,categories:[m]}),f=e.createToken({name:"Attribute",pattern:e.Lexer.NA}),L=e.createToken({name:"ModelAttribute",pattern:/@@/,label:"'@@'",categories:[f]}),E=e.createToken({name:"FieldAttribute",pattern:/@/,label:"'@'",categories:[f]}),d=e.createToken({name:"Dot",pattern:/\./,label:"'.'"}),y=e.createToken({name:"QuestionMark",pattern:/\?/,label:"'?'"}),h=e.createToken({name:"LCurly",pattern:/{/,label:"'{'"}),v=e.createToken({name:"RCurly",pattern:/}/,label:"'}'",pop_mode:!0}),b=e.createToken({name:"LRound",pattern:/\(/,label:"'('"}),g=e.createToken({name:"RRound",pattern:/\)/,label:"')'"}),A=e.createToken({name:"LSquare",pattern:/\[/,label:"'['"}),U=e.createToken({name:"RSquare",pattern:/\]/,label:"']'"}),k=e.createToken({name:"Comma",pattern:/,/,label:"','"}),S=e.createToken({name:"Colon",pattern:/:/,label:"':'"}),O=e.createToken({name:"Equals",pattern:/=/,label:"'='"}),B=e.createToken({name:"StringLiteral",pattern:/"(:?[^\\"\n\r]|\\(:?[bfnrtv"\\/]|u[0-9a-fA-F]{4}))*"/}),T=e.createToken({name:"NumberLiteral",pattern:/-?(0|[1-9]\d*)(\.\d+)?([eE][+-]?\d+)?/}),N=e.createToken({name:"WhiteSpace",pattern:/\s+/,group:e.Lexer.SKIPPED}),M=e.createToken({name:"LineBreak",pattern:/\n|\r\n/,line_breaks:!0,label:"LineBreak"}),C=[m,l,p,M,N],R={modes:{global:[].concat(C,[r,a,u,i]),block:[].concat(C,[f,L,E,d,y,h,v,A,U,b,g,k,S,O,o,c,s,B,T,n])},defaultMode:"global"},w=new e.Lexer(R);function j(e,t){e.prototype=Object.create(t.prototype),e.prototype.constructor=e,(Object.setPrototypeOf||function(e,t){return e.__proto__=t,e})(e,t)}var x=new(function(e){function t(){var t;return(t=e.call(this,R)||this).break=t.RULE("break",(function(){t.CONSUME1(M),t.CONSUME2(M)})),t.keyedArg=t.RULE("keyedArg",(function(){t.CONSUME(n,{LABEL:"keyName"}),t.CONSUME(S),t.SUBRULE(t.value)})),t.array=t.RULE("array",(function(){t.CONSUME(A),t.MANY_SEP({SEP:k,DEF:function(){t.SUBRULE(t.value)}}),t.CONSUME(U)})),t.func=t.RULE("func",(function(){t.CONSUME(n,{LABEL:"funcName"}),t.CONSUME(b),t.MANY_SEP({SEP:k,DEF:function(){t.OR([{ALT:function(){return t.SUBRULE(t.keyedArg)}},{ALT:function(){return t.SUBRULE(t.value)}}])}}),t.CONSUME(g)})),t.value=t.RULE("value",(function(){t.OR([{ALT:function(){return t.CONSUME(B,{LABEL:"value"})}},{ALT:function(){return t.CONSUME(T,{LABEL:"value"})}},{ALT:function(){return t.SUBRULE(t.array,{LABEL:"value"})}},{ALT:function(){return t.SUBRULE(t.func,{LABEL:"value"})}},{ALT:function(){return t.CONSUME(o,{LABEL:"value"})}},{ALT:function(){return t.CONSUME(c,{LABEL:"value"})}},{ALT:function(){return t.CONSUME(s,{LABEL:"value"})}},{ALT:function(){return t.CONSUME(n,{LABEL:"value"})}}])})),t.property=t.RULE("property",(function(){t.CONSUME(n,{LABEL:"propertyName"}),t.CONSUME(O),t.SUBRULE(t.value,{LABEL:"propertyValue"})})),t.assignment=t.RULE("assignment",(function(){t.CONSUME(n,{LABEL:"assignmentName"}),t.CONSUME(O),t.SUBRULE(t.value,{LABEL:"assignmentValue"})})),t.field=t.RULE("field",(function(){t.CONSUME(n,{LABEL:"fieldName"}),t.SUBRULE(t.value,{LABEL:"fieldType"}),t.OPTION1((function(){t.OR([{ALT:function(){t.CONSUME(A,{LABEL:"array"}),t.CONSUME(U,{LABEL:"array"})}},{ALT:function(){return t.CONSUME(y,{LABEL:"optional"})}}])})),t.MANY((function(){t.SUBRULE(t.attribute,{LABEL:"attributeList"})})),t.OPTION2((function(){t.CONSUME(m,{LABEL:"comment"})}))})),t.block=t.RULE("block",(function(e){void 0===e&&(e={});var n=e.componentType,r="enum"===n,a="model"===n;t.CONSUME(h),t.CONSUME1(M),t.MANY((function(){t.OR([{ALT:function(){return t.SUBRULE(t.comment,{LABEL:"list"})}},{GATE:function(){return a},ALT:function(){return t.SUBRULE(t.property,{LABEL:"list"})}},{ALT:function(){return t.SUBRULE(t.attribute,{LABEL:"list"})}},{GATE:function(){return a},ALT:function(){return t.SUBRULE(t.field,{LABEL:"list"})}},{GATE:function(){return r},ALT:function(){return t.SUBRULE(t.enum,{LABEL:"list"})}},{GATE:function(){return!a},ALT:function(){return t.SUBRULE(t.assignment,{LABEL:"list"})}},{ALT:function(){return t.SUBRULE(t.break,{LABEL:"list"})}},{ALT:function(){return t.CONSUME2(M)}}])})),t.CONSUME(v)})),t.enum=t.RULE("enum",(function(){t.CONSUME(n,{LABEL:"enumName"}),t.OPTION((function(){t.CONSUME(m,{LABEL:"comment"})}))})),t.attribute=t.RULE("attribute",(function(){t.OR1([{ALT:function(){return t.CONSUME(L,{LABEL:"modelAttribute"})}},{ALT:function(){return t.CONSUME(E,{LABEL:"fieldAttribute"})}}]),t.OR2([{ALT:function(){t.CONSUME1(n,{LABEL:"groupName"}),t.CONSUME(d),t.CONSUME2(n,{LABEL:"attributeName"})}},{ALT:function(){return t.CONSUME(n,{LABEL:"attributeName"})}}]),t.OPTION((function(){t.CONSUME(b),t.MANY_SEP({SEP:k,DEF:function(){t.SUBRULE(t.attributeArg)}}),t.CONSUME(g)}))})),t.attributeArg=t.RULE("attributeArg",(function(){t.OR([{ALT:function(){return t.SUBRULE(t.keyedArg,{LABEL:"value"})}},{ALT:function(){return t.SUBRULE(t.value,{LABEL:"value"})}}])})),t.component=t.RULE("component",(function(){var e=t.OR1([{ALT:function(){return t.CONSUME(r,{LABEL:"type"})}},{ALT:function(){return t.CONSUME(a,{LABEL:"type"})}},{ALT:function(){return t.CONSUME(u,{LABEL:"type"})}},{ALT:function(){return t.CONSUME(i,{LABEL:"type"})}}]);t.OR2([{ALT:function(){t.CONSUME1(n,{LABEL:"groupName"}),t.CONSUME(d),t.CONSUME2(n,{LABEL:"componentName"})}},{ALT:function(){return t.CONSUME(n,{LABEL:"componentName"})}}]),t.SUBRULE(t.block,{ARGS:[{componentType:e.image}]})})),t.comment=t.RULE("comment",(function(){t.CONSUME(m,{LABEL:"text"})})),t.schema=t.RULE("schema",(function(){t.MANY((function(){t.OR([{ALT:function(){return t.SUBRULE(t.comment,{LABEL:"list"})}},{ALT:function(){return t.SUBRULE(t.component,{LABEL:"list"})}},{ALT:function(){return t.SUBRULE(t.break,{LABEL:"list"})}},{ALT:function(){return t.CONSUME(M)}}])}))})),t.performSelfAnalysis(),t}return j(t,e),t}(e.CstParser)),_=function(e){function t(){var t;return(t=e.call(this)||this).validateVisitor(),t}j(t,e);var n=t.prototype;return n.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])})))||[]}},n.component=function(e){var t=e.type[0].image,n=e.componentName[0].image,r=this.visit(e.block);switch(t){case"datasource":return{type:"datasource",name:n,assignments:r};case"generator":return{type:"generator",name:n,assignments:r};case"model":return{type:"model",name:n,properties:r};case"enum":return{type:"enum",name:n,enumerators:r};default:throw new Error("Unexpected block type: "+t)}},n.break=function(){return{type:"break"}},n.comment=function(e){return{type:"comment",text:e.text[0].image}},n.block=function(e){var t,n=this;return null==(t=e.list)?void 0:t.map((function(e){return n.visit([e])}))},n.assignment=function(e){var t=this.visit(e.assignmentValue);return{type:"assignment",key:e.assignmentName[0].image,value:t}},n.field=function(e){var t,n,r=this,a=this.visit(e.fieldType),u=e.fieldName[0].image,i=e.attributeList&&e.attributeList.map((function(e){return r.visit([e])})),o=null==(t=e.comment)||null==(n=t[0])?void 0:n.image;return{type:"field",name:u,fieldType:a,array:null!=e.array,optional:null!=e.optional,attributes:i,comment:o}},n.attribute=function(e){var t=this,n=e.attributeName[0].image,r=(e.groupName||[{}])[0].image,a=e.attributeArg&&e.attributeArg.map((function(e){return t.visit(e)}));return{type:"attribute",name:n,kind:null!=e.modelAttribute?"model":"field",group:r,args:a}},n.attributeArg=function(e){return{type:"attributeArgument",value:this.visit(e.value)}},n.func=function(e){var t=this,n=e.funcName[0].image,r=e.value&&e.value.map((function(e){return t.visit([e])})),a=e.keyedArg&&e.keyedArg.map((function(e){return t.visit([e])}));return{type:"function",name:n,params:(r||a)&&[].concat(null!=r?r:[],null!=a?a:[])}},n.array=function(e){var t=this;return{type:"array",args:e.value&&e.value.map((function(e){return t.visit([e])}))}},n.keyedArg=function(e){return{type:"keyValue",key:e.keyName[0].image,value:this.visit(e.value)}},n.value=function(e){return"image"in e.value[0]?e.value[0].image:this.visit(e.value)},n.enum=function(e){var t,n;return{type:"enumerator",name:e.enumName[0].image,comment:null==(t=e.comment)||null==(n=t[0])?void 0:n.image}},t}(x.getBaseCstVisitorConstructorWithDefaults());function P(e){var t=w.tokenize(e);x.input=t.tokens;var n=x.schema();if(x.errors.length>0)throw x.errors[0];return(new _).visit(n)}var I=["break","comment"],V=["generator","datasource","model","enum","break","comment"];function D(e,n){void 0===n&&(n={});var r=n.sort,a=n.locales,u=void 0===a?void 0:a,i=n.sortOrder,o=void 0===i?void 0:i,c=e.list;if(void 0!==r&&r){c=e.list=c.filter((function(e){return"break"!==e.type}));var s=function(e,t,n){return void 0===n&&(n=V),function(r,a){if(-1!==I.indexOf(r.type)!=(-1!==I.indexOf(a.type)))return e.list.indexOf(r)-e.list.indexOf(a);n!==V&&(n=n.concat(V));var u=n.indexOf(r.type)-n.indexOf(a.type);return 0!==u?u:"name"in r&&"name"in a?r.name.localeCompare(a.name,t):0}}(e,u,o);c.sort(s)}return c.map(F).filter(Boolean).join(t.EOL).replace(/(\r?\n\s*){3,}/g,t.EOL+t.EOL)+t.EOL}function F(e){switch(e.type){case"comment":return G(e);case"datasource":return r=H((n=e).assignments),"\ndatasource "+n.name+" {\n "+r+"\n}";case"enum":return function(e){var n=e.enumerators.map(q).filter(Boolean).join(t.EOL+" ").replace(/(\r?\n\s*){3,}/g,t.EOL+t.EOL+" ");return"\nenum "+e.name+" {\n "+n+"\n}"}(e);case"generator":return function(e){var t=H(e.assignments);return"\ngenerator "+e.name+" {\n "+t+"\n}"}(e);case"model":return function(e){var n,r,a,u,i,o=(r=0,a=(n=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}),[[]]),u=a.map((function(e){return e.reduce((function(e,t){return Math.max(e,"field"===t.type?t.name.length:0)}),0)})),i=a.map((function(e){return e.reduce((function(e,t){return Math.max(e,"field"===t.type?K(t).length:0)}),0)})),n.map((function(e,t,n){return t>0&&"break"!==e.type&&"break"===n[t-1].type&&(u.shift(),i.shift()),function(e,t,n){switch(void 0===t&&(t=0),void 0===n&&(n=0),e.type){case"attribute":return z(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=K(e).padEnd(n),u=e.attributes?e.attributes.map(z):[],i=e.comment;return[r,a].concat(u).filter(Boolean).join(" ").trim()+(i?" "+i:"")}(e,t,n);case"comment":return G(e);case"break":return Y();default:throw new Error("Unrecognized property type")}}(e,u[0],i[0])})).filter(Boolean).join(t.EOL+" ").replace(/(\r?\n\s*){3,}/g,t.EOL+t.EOL+" "));return"\nmodel "+e.name+" {\n "+o+"\n}"}(e);case"break":return Y();default:throw new Error("Unrecognized block type")}var n,r}function G(e){return e.text}function Y(){return t.EOL}function q(e){switch(e.type){case"enumerator":return[e.name,e.comment].filter(Boolean).join(" ");case"attribute":return z(e);case"comment":return G(e);case"break":return Y();default:throw new Error("Unexpected enumerator type")}}function z(e){var t=e.args&&e.args.length>0?"("+e.args.map(W).filter(Boolean).join(", ")+")":"",n=[e.name];return e.group&&n.unshift(e.group),("field"===e.kind?"@":"@@")+n.join(".")+t}function W(e){return Z(e.value)}function K(e){var t=e.array?"[]":e.optional?"?":"";if("object"==typeof e.fieldType)switch(e.fieldType.type){case"function":return""+Q(e.fieldType)+t;default:throw new Error("Unexpected field type")}return""+e.fieldType+t}function Q(e){var t=e.params?e.params.map(Z):"";return e.name+"("+t+")"}function Z(e){switch(typeof e){case"object":if("type"in e)switch(e.type){case"keyValue":return e.key+": "+Z(e.value);case"function":return Q(e);case"array":return"["+(null!=e.args?e.args.map(Z).join(", "):"")+"]";default:throw new Error("Unexpected value type")}throw new Error("Unexpected object value");default:return String(e)}}function H(e){var n=0,r=e.reduce((function(e,t,r,a){return"break"===t.type||(r>0&&"break"===a[r-1].type&&(e[++n]=[]),e[n].push(t)),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 G(e);case"break":return Y();case"assignment":return e.key.padEnd(t)+" = "+Z(e.value);default:throw new Error("Unexpected assignment type")}}(e,r[0])})).filter(Boolean).join(t.EOL+" ").replace(/(\r?\n\s*){3,}/g,t.EOL+t.EOL+" ")}var J=function(){function e(e){void 0===e&&(e=""),this.schema=P(e)}var t=e.prototype;return t.print=function(e){return void 0===e&&(e={}),D(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 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.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 model!");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(!n||!("type"in n)||"model"!==n.type){var r=this.getParent();if(!r||!("type"in r)||"model"!==r.type)throw new Error("Subject must be a prisma model!");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:"model",name:e,args:a}),this},t.attribute=function(e,t){var n=this.getParent(),r=this.getSubject();if(!n||!("type"in n)||"model"!==n.type)throw new Error("Parent must be a prisma model!");if(!r||!("type"in r)||"field"!==r.type)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.name===e?n:t}),{type:"attribute",kind:"field",name:e});if(Array.isArray(t)){var u=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:u(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.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"];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":t.assignments.push(e);break;case"enum":t.enumerators.push(e);break;case"generator":t.assignments.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(!n||!("type"in n)||"model"!==n.type){var r=this.getParent();if(!r||!("type"in r)||"model"!==r.type)throw new Error("Subject must be a prisma model!");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.then=function(e){return e(this._subject),this},e}();exports.ConcretePrismaSchemaBuilder=J,exports.createPrismaSchemaBuilder=function(e){return new J(e)},exports.getSchema=P,exports.printSchema=D; | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("chevrotain"),t=require("os"),n=e.createToken({name:"Identifier",pattern:/[a-zA-Z]\w*/}),r=e.createToken({name:"Datasource",pattern:/datasource/,push_mode:"block"}),a=e.createToken({name:"Generator",pattern:/generator/,push_mode:"block"}),i=e.createToken({name:"Model",pattern:/model/,push_mode:"block"}),u=e.createToken({name:"Enum",pattern:/enum/,push_mode:"block"}),o=e.createToken({name:"True",pattern:/true/,longer_alt:n}),s=e.createToken({name:"False",pattern:/false/,longer_alt:n}),c=e.createToken({name:"Null",pattern:/null/,longer_alt:n}),m=e.createToken({name:"Comment",pattern:e.Lexer.NA}),l=e.createToken({name:"DocComment",pattern:/\/\/\/\s*(.+)/,categories:[m]}),p=e.createToken({name:"LineComment",pattern:/\/\/\s*(.+)/,categories:[m]}),f=e.createToken({name:"Attribute",pattern:e.Lexer.NA}),L=e.createToken({name:"ModelAttribute",pattern:/@@/,label:"'@@'",categories:[f]}),d=e.createToken({name:"FieldAttribute",pattern:/@/,label:"'@'",categories:[f]}),y=e.createToken({name:"Dot",pattern:/\./,label:"'.'"}),E=e.createToken({name:"QuestionMark",pattern:/\?/,label:"'?'"}),h=e.createToken({name:"LCurly",pattern:/{/,label:"'{'"}),b=e.createToken({name:"RCurly",pattern:/}/,label:"'}'",pop_mode:!0}),v=e.createToken({name:"LRound",pattern:/\(/,label:"'('"}),g=e.createToken({name:"RRound",pattern:/\)/,label:"')'"}),A=e.createToken({name:"LSquare",pattern:/\[/,label:"'['"}),U=e.createToken({name:"RSquare",pattern:/\]/,label:"']'"}),S=e.createToken({name:"Comma",pattern:/,/,label:"','"}),k=e.createToken({name:"Colon",pattern:/:/,label:"':'"}),O=e.createToken({name:"Equals",pattern:/=/,label:"'='"}),B=e.createToken({name:"StringLiteral",pattern:/"(:?[^\\"\n\r]|\\(:?[bfnrtv"\\/]|u[0-9a-fA-F]{4}))*"/}),T=e.createToken({name:"NumberLiteral",pattern:/-?(0|[1-9]\d*)(\.\d+)?([eE][+-]?\d+)?/}),N=e.createToken({name:"WhiteSpace",pattern:/\s+/,group:e.Lexer.SKIPPED}),M=e.createToken({name:"LineBreak",pattern:/\n|\r\n/,line_breaks:!0,label:"LineBreak"}),C=[m,l,p,M,N],R={modes:{global:[].concat(C,[r,a,i,u]),block:[].concat(C,[f,L,d,y,E,h,b,A,U,v,g,S,k,O,o,s,c,B,T,n])},defaultMode:"global"},w=new e.Lexer(R);function j(e,t){e.prototype=Object.create(t.prototype),e.prototype.constructor=e,(Object.setPrototypeOf||function(e,t){return e.__proto__=t,e})(e,t)}var x=new(function(e){function t(){var t;return(t=e.call(this,R)||this).break=t.RULE("break",(function(){t.CONSUME1(M),t.CONSUME2(M)})),t.keyedArg=t.RULE("keyedArg",(function(){t.CONSUME(n,{LABEL:"keyName"}),t.CONSUME(k),t.SUBRULE(t.value)})),t.array=t.RULE("array",(function(){t.CONSUME(A),t.MANY_SEP({SEP:S,DEF:function(){t.SUBRULE(t.value)}}),t.CONSUME(U)})),t.func=t.RULE("func",(function(){t.CONSUME(n,{LABEL:"funcName"}),t.CONSUME(v),t.MANY_SEP({SEP:S,DEF:function(){t.OR([{ALT:function(){return t.SUBRULE(t.keyedArg)}},{ALT:function(){return t.SUBRULE(t.value)}}])}}),t.CONSUME(g)})),t.value=t.RULE("value",(function(){t.OR([{ALT:function(){return t.CONSUME(B,{LABEL:"value"})}},{ALT:function(){return t.CONSUME(T,{LABEL:"value"})}},{ALT:function(){return t.SUBRULE(t.array,{LABEL:"value"})}},{ALT:function(){return t.SUBRULE(t.func,{LABEL:"value"})}},{ALT:function(){return t.CONSUME(o,{LABEL:"value"})}},{ALT:function(){return t.CONSUME(s,{LABEL:"value"})}},{ALT:function(){return t.CONSUME(c,{LABEL:"value"})}},{ALT:function(){return t.CONSUME(n,{LABEL:"value"})}}])})),t.property=t.RULE("property",(function(){t.CONSUME(n,{LABEL:"propertyName"}),t.CONSUME(O),t.SUBRULE(t.value,{LABEL:"propertyValue"})})),t.assignment=t.RULE("assignment",(function(){t.CONSUME(n,{LABEL:"assignmentName"}),t.CONSUME(O),t.SUBRULE(t.value,{LABEL:"assignmentValue"})})),t.field=t.RULE("field",(function(){t.CONSUME(n,{LABEL:"fieldName"}),t.SUBRULE(t.value,{LABEL:"fieldType"}),t.OPTION1((function(){t.OR([{ALT:function(){t.CONSUME(A,{LABEL:"array"}),t.CONSUME(U,{LABEL:"array"})}},{ALT:function(){return t.CONSUME(E,{LABEL:"optional"})}}])})),t.MANY((function(){t.SUBRULE(t.attribute,{LABEL:"attributeList"})})),t.OPTION2((function(){t.CONSUME(m,{LABEL:"comment"})}))})),t.block=t.RULE("block",(function(e){void 0===e&&(e={});var n=e.componentType,r="enum"===n,a="model"===n;t.CONSUME(h),t.CONSUME1(M),t.MANY((function(){t.OR([{ALT:function(){return t.SUBRULE(t.comment,{LABEL:"list"})}},{GATE:function(){return a},ALT:function(){return t.SUBRULE(t.property,{LABEL:"list"})}},{ALT:function(){return t.SUBRULE(t.attribute,{LABEL:"list"})}},{GATE:function(){return a},ALT:function(){return t.SUBRULE(t.field,{LABEL:"list"})}},{GATE:function(){return r},ALT:function(){return t.SUBRULE(t.enum,{LABEL:"list"})}},{GATE:function(){return!a},ALT:function(){return t.SUBRULE(t.assignment,{LABEL:"list"})}},{ALT:function(){return t.SUBRULE(t.break,{LABEL:"list"})}},{ALT:function(){return t.CONSUME2(M)}}])})),t.CONSUME(b)})),t.enum=t.RULE("enum",(function(){t.CONSUME(n,{LABEL:"enumName"}),t.OPTION((function(){t.CONSUME(m,{LABEL:"comment"})}))})),t.attribute=t.RULE("attribute",(function(){t.OR1([{ALT:function(){return t.CONSUME(L,{LABEL:"modelAttribute"})}},{ALT:function(){return t.CONSUME(d,{LABEL:"fieldAttribute"})}}]),t.OR2([{ALT:function(){t.CONSUME1(n,{LABEL:"groupName"}),t.CONSUME(y),t.CONSUME2(n,{LABEL:"attributeName"})}},{ALT:function(){return t.CONSUME(n,{LABEL:"attributeName"})}}]),t.OPTION((function(){t.CONSUME(v),t.MANY_SEP({SEP:S,DEF:function(){t.SUBRULE(t.attributeArg)}}),t.CONSUME(g)}))})),t.attributeArg=t.RULE("attributeArg",(function(){t.OR([{ALT:function(){return t.SUBRULE(t.keyedArg,{LABEL:"value"})}},{ALT:function(){return t.SUBRULE(t.value,{LABEL:"value"})}}])})),t.component=t.RULE("component",(function(){var e=t.OR1([{ALT:function(){return t.CONSUME(r,{LABEL:"type"})}},{ALT:function(){return t.CONSUME(a,{LABEL:"type"})}},{ALT:function(){return t.CONSUME(i,{LABEL:"type"})}},{ALT:function(){return t.CONSUME(u,{LABEL:"type"})}}]);t.OR2([{ALT:function(){t.CONSUME1(n,{LABEL:"groupName"}),t.CONSUME(y),t.CONSUME2(n,{LABEL:"componentName"})}},{ALT:function(){return t.CONSUME(n,{LABEL:"componentName"})}}]),t.SUBRULE(t.block,{ARGS:[{componentType:e.image}]})})),t.comment=t.RULE("comment",(function(){t.CONSUME(m,{LABEL:"text"})})),t.schema=t.RULE("schema",(function(){t.MANY((function(){t.OR([{ALT:function(){return t.SUBRULE(t.comment,{LABEL:"list"})}},{ALT:function(){return t.SUBRULE(t.component,{LABEL:"list"})}},{ALT:function(){return t.SUBRULE(t.break,{LABEL:"list"})}},{ALT:function(){return t.CONSUME(M)}}])}))})),t.performSelfAnalysis(),t}return j(t,e),t}(e.CstParser)),_=function(e){function t(){var t;return(t=e.call(this)||this).validateVisitor(),t}j(t,e);var n=t.prototype;return n.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])})))||[]}},n.component=function(e){var t=e.type[0].image,n=e.componentName[0].image,r=this.visit(e.block);switch(t){case"datasource":return{type:"datasource",name:n,assignments:r};case"generator":return{type:"generator",name:n,assignments:r};case"model":return{type:"model",name:n,properties:r};case"enum":return{type:"enum",name:n,enumerators:r};default:throw new Error("Unexpected block type: "+t)}},n.break=function(){return{type:"break"}},n.comment=function(e){return{type:"comment",text:e.text[0].image}},n.block=function(e){var t,n=this;return null==(t=e.list)?void 0:t.map((function(e){return n.visit([e])}))},n.assignment=function(e){var t=this.visit(e.assignmentValue);return{type:"assignment",key:e.assignmentName[0].image,value:t}},n.field=function(e){var t,n,r=this,a=this.visit(e.fieldType),i=e.fieldName[0].image,u=e.attributeList&&e.attributeList.map((function(e){return r.visit([e])})),o=null==(t=e.comment)||null==(n=t[0])?void 0:n.image;return{type:"field",name:i,fieldType:a,array:null!=e.array,optional:null!=e.optional,attributes:u,comment:o}},n.attribute=function(e){var t=this,n=e.attributeName[0].image,r=(e.groupName||[{}])[0].image,a=e.attributeArg&&e.attributeArg.map((function(e){return t.visit(e)}));return{type:"attribute",name:n,kind:null!=e.modelAttribute?"model":"field",group:r,args:a}},n.attributeArg=function(e){return{type:"attributeArgument",value:this.visit(e.value)}},n.func=function(e){var t=this,n=e.funcName[0].image,r=e.value&&e.value.map((function(e){return t.visit([e])})),a=e.keyedArg&&e.keyedArg.map((function(e){return t.visit([e])}));return{type:"function",name:n,params:(r||a)&&[].concat(null!=r?r:[],null!=a?a:[])}},n.array=function(e){var t=this;return{type:"array",args:e.value&&e.value.map((function(e){return t.visit([e])}))}},n.keyedArg=function(e){return{type:"keyValue",key:e.keyName[0].image,value:this.visit(e.value)}},n.value=function(e){return"image"in e.value[0]?e.value[0].image:this.visit(e.value)},n.enum=function(e){var t,n;return{type:"enumerator",name:e.enumName[0].image,comment:null==(t=e.comment)||null==(n=t[0])?void 0:n.image}},t}(x.getBaseCstVisitorConstructorWithDefaults());function P(e){var t=w.tokenize(e);x.input=t.tokens;var n=x.schema();if(x.errors.length>0)throw x.errors[0];return(new _).visit(n)}var I=["break","comment"],V=["generator","datasource","model","enum","break","comment"];function D(e,n){void 0===n&&(n={});var r=n.sort,a=n.locales,i=void 0===a?void 0:a,u=n.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=V),function(r,a){if(-1!==I.indexOf(r.type)!=(-1!==I.indexOf(a.type)))return e.list.indexOf(r)-e.list.indexOf(a);n!==V&&(n=n.concat(V));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(F).filter(Boolean).join(t.EOL).replace(/(\r?\n\s*){3,}/g,t.EOL+t.EOL)+t.EOL}function F(e){switch(e.type){case"comment":return G(e);case"datasource":return r=H((n=e).assignments),"\ndatasource "+n.name+" {\n "+r+"\n}";case"enum":return function(e){var n=e.enumerators.map(q).filter(Boolean).join(t.EOL+" ").replace(/(\r?\n\s*){3,}/g,t.EOL+t.EOL+" ");return"\nenum "+e.name+" {\n "+n+"\n}"}(e);case"generator":return function(e){var t=H(e.assignments);return"\ngenerator "+e.name+" {\n "+t+"\n}"}(e);case"model":return function(e){var n,r,a,i,u,o=(r=0,a=(n=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?K(t).length:0)}),0)})),n.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 z(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=K(e).padEnd(n),i=e.attributes?e.attributes.map(z):[],u=e.comment;return[r,a].concat(i).filter(Boolean).join(" ").trim()+(u?" "+u:"")}(e,t,n);case"comment":return G(e);case"break":return Y();default:throw new Error("Unrecognized property type")}}(e,i[0],u[0])})).filter(Boolean).join(t.EOL+" ").replace(/(\r?\n\s*){3,}/g,t.EOL+t.EOL+" "));return"\nmodel "+e.name+" {\n "+o+"\n}"}(e);case"break":return Y();default:throw new Error("Unrecognized block type")}var n,r}function G(e){return e.text}function Y(){return t.EOL}function q(e){switch(e.type){case"enumerator":return[e.name,e.comment].filter(Boolean).join(" ");case"attribute":return z(e);case"comment":return G(e);case"break":return Y();default:throw new Error("Unexpected enumerator type")}}function z(e){var t=e.args&&e.args.length>0?"("+e.args.map(W).filter(Boolean).join(", ")+")":"",n=[e.name];return e.group&&n.unshift(e.group),("field"===e.kind?"@":"@@")+n.join(".")+t}function W(e){return Z(e.value)}function K(e){var t=e.array?"[]":e.optional?"?":"";if("object"==typeof e.fieldType)switch(e.fieldType.type){case"function":return""+Q(e.fieldType)+t;default:throw new Error("Unexpected field type")}return""+e.fieldType+t}function Q(e){var t=e.params?e.params.map(Z):"";return e.name+"("+t+")"}function Z(e){switch(typeof e){case"object":if("type"in e)switch(e.type){case"keyValue":return e.key+": "+Z(e.value);case"function":return Q(e);case"array":return"["+(null!=e.args?e.args.map(Z).join(", "):"")+"]";default:throw new Error("Unexpected value type")}throw new Error("Unexpected object value");default:return String(e)}}function H(e){var n=0,r=e.reduce((function(e,t,r,a){return"break"===t.type||(r>0&&"break"===a[r-1].type&&(e[++n]=[]),e[n].push(t)),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 G(e);case"break":return Y();case"assignment":return e.key.padEnd(t)+" = "+Z(e.value);default:throw new Error("Unexpected assignment type")}}(e,r[0])})).filter(Boolean).join(t.EOL+" ").replace(/(\r?\n\s*){3,}/g,t.EOL+t.EOL+" ")}var J=function(){function e(e){void 0===e&&(e=""),this.schema=P(e)}var t=e.prototype;return t.print=function(e){return void 0===e&&(e={}),D(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 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.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 model!");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(!n||!("type"in n)||"model"!==n.type){var r=this.getParent();if(!r||!("type"in r)||"model"!==r.type)throw new Error("Subject must be a prisma model!");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:"model",name:e,args:a}),this},t.attribute=function(e,t){var n=this.getParent(),r=this.getSubject();if(!n||!("type"in n)||"model"!==n.type)throw new Error("Parent must be a prisma model!");if(!r||!("type"in r)||"field"!==r.type)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.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(!t||!("type"in t)||"model"!==t.type)throw new Error("Parent must be a prisma model!");if(!n||!("type"in n)||"field"!==n.type)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"];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":t.assignments.push(e);break;case"enum":t.enumerators.push(e);break;case"generator":t.assignments.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(!n||!("type"in n)||"model"!==n.type){var r=this.getParent();if(!r||!("type"in r)||"model"!==r.type)throw new Error("Subject must be a prisma model!");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(!t||!("type"in t)||"model"!==t.type){var n=this.getParent();if(!n||!("type"in n)||"model"!==n.type)throw new Error("Subject must be a prisma model!");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=J,exports.createPrismaSchemaBuilder=function(e){return new J(e)},exports.getSchema=P,exports.printSchema=D; | ||
//# sourceMappingURL=prisma-ast.cjs.production.min.js.map |
@@ -1364,2 +1364,21 @@ import { Lexer, createToken, CstParser } from 'chevrotain'; | ||
_proto.removeAttribute = function removeAttribute(name) { | ||
var parent = this.getParent(); | ||
var subject = this.getSubject(); | ||
if (!parent || !('type' in parent) || parent.type !== 'model') { | ||
throw new Error('Parent must be a prisma model!'); | ||
} | ||
if (!subject || !('type' in subject) || subject.type !== 'field') { | ||
throw new Error('Subject must be a prisma field!'); | ||
} | ||
if (!subject.attributes) subject.attributes = []; | ||
subject.attributes = subject.attributes.filter(function (attr) { | ||
return !(attr.type === 'attribute' && attr.name === name); | ||
}); | ||
return this; | ||
}; | ||
_proto.assignment = function assignment(key, value) { | ||
@@ -1488,2 +1507,17 @@ var subject = this.getSubject(); | ||
_proto.removeField = function removeField(name) { | ||
var subject = this.getSubject(); | ||
if (!subject || !('type' in subject) || subject.type !== 'model') { | ||
var parent = this.getParent(); | ||
if (!parent || !('type' in parent) || parent.type !== 'model') throw new Error('Subject must be a prisma model!'); | ||
subject = this._subject = parent; | ||
} | ||
subject.properties = subject.properties.filter(function (field) { | ||
return !(field.type === 'field' && field.name === name); | ||
}); | ||
return this; | ||
}; | ||
_proto.then = function then(callback) { | ||
@@ -1490,0 +1524,0 @@ callback(this._subject); |
@@ -5,10 +5,10 @@ import * as schema from './getSchema'; | ||
declare type ExtractKeys = 'getSchema' | 'getSubject' | 'getParent' | 'print'; | ||
declare type NeutralKeys = 'break' | 'comment' | 'attribute' | 'enumerator'; | ||
declare type NeutralKeys = 'break' | 'comment' | 'attribute' | 'enumerator' | 'then'; | ||
declare type DatasourceOrGeneratorKeys = 'assignment'; | ||
declare type EnumKeys = 'enumerator'; | ||
declare type FieldKeys = 'attribute'; | ||
declare type ModelKeys = 'blockAttribute' | 'field'; | ||
declare type FieldKeys = 'attribute' | 'removeAttribute'; | ||
declare type ModelKeys = 'blockAttribute' | 'field' | 'removeField'; | ||
declare type PrismaSchemaSubset<Universe extends keyof ConcretePrismaSchemaBuilder, Method> = ReplaceReturnType<ConcretePrismaSchemaBuilder[Universe], PrismaSchemaBuilder<Exclude<keyof ConcretePrismaSchemaBuilder, Method>>>; | ||
declare type PrismaSchemaBuilder<K extends keyof ConcretePrismaSchemaBuilder> = { | ||
[U in K]: U extends ExtractKeys ? ConcretePrismaSchemaBuilder[U] : U extends NeutralKeys ? ConcretePrismaSchemaBuilder[U] : U extends 'datasource' ? PrismaSchemaSubset<U, 'datasource' | EnumKeys | FieldKeys | ModelKeys> : U extends 'generator' ? PrismaSchemaSubset<U, EnumKeys | FieldKeys | ModelKeys> : U extends 'model' ? PrismaSchemaSubset<U, DatasourceOrGeneratorKeys | EnumKeys | FieldKeys> : U extends 'field' ? PrismaSchemaSubset<U, DatasourceOrGeneratorKeys | EnumKeys> : U extends 'enum' ? PrismaSchemaSubset<U, DatasourceOrGeneratorKeys | ModelKeys | FieldKeys> : PrismaSchemaSubset<U, DatasourceOrGeneratorKeys | EnumKeys | FieldKeys | ModelKeys | 'comment'>; | ||
[U in K]: U extends ExtractKeys ? ConcretePrismaSchemaBuilder[U] : U extends NeutralKeys ? ConcretePrismaSchemaBuilder[U] : U extends 'datasource' ? PrismaSchemaSubset<U, 'datasource' | EnumKeys | FieldKeys | ModelKeys> : U extends 'generator' ? PrismaSchemaSubset<U, EnumKeys | FieldKeys | ModelKeys> : U extends 'model' ? PrismaSchemaSubset<U, DatasourceOrGeneratorKeys | EnumKeys | FieldKeys> : U extends 'field' ? PrismaSchemaSubset<U, DatasourceOrGeneratorKeys | EnumKeys> : U extends 'removeField' ? PrismaSchemaSubset<U, DatasourceOrGeneratorKeys | EnumKeys | FieldKeys> : U extends 'enum' ? PrismaSchemaSubset<U, DatasourceOrGeneratorKeys | ModelKeys | FieldKeys> : U extends 'removeAttribute' ? PrismaSchemaSubset<U, DatasourceOrGeneratorKeys | EnumKeys> : PrismaSchemaSubset<U, DatasourceOrGeneratorKeys | EnumKeys | FieldKeys | ModelKeys | 'comment'>; | ||
}; | ||
@@ -38,2 +38,3 @@ declare type Arg = string | { | ||
attribute<T extends schema.Field>(name: string, args?: Arg[] | Record<string, string[]>): this; | ||
removeAttribute<T extends schema.Field>(name: string): this; | ||
assignment<T extends schema.Generator | schema.Datasource>(key: string, value: string): this; | ||
@@ -45,5 +46,6 @@ private blockInsert; | ||
field(name: string, fieldType?: string | schema.Func): this; | ||
then<R extends schema.Block>(callback: (subject: R) => R): this; | ||
removeField(name: string): this; | ||
then<R extends schema.Block | schema.Property>(callback: (subject: R) => unknown): this; | ||
} | ||
export declare function createPrismaSchemaBuilder(source?: string): PrismaSchemaBuilder<Exclude<keyof ConcretePrismaSchemaBuilder, DatasourceOrGeneratorKeys | EnumKeys | FieldKeys | ModelKeys>>; | ||
export {}; |
{ | ||
"version": "0.5.0", | ||
"version": "0.5.1", | ||
"license": "MIT", | ||
@@ -4,0 +4,0 @@ "main": "dist/index.js", |
172
README.md
@@ -134,2 +134,22 @@ # @mrleebo/prisma-ast | ||
### Access a datasource programmatially | ||
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 | ||
@@ -168,6 +188,33 @@ | ||
provider = "prisma-client-js" | ||
output = "db.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 | ||
@@ -187,2 +234,28 @@ | ||
### 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 | ||
@@ -192,2 +265,8 @@ | ||
```prisma | ||
model Project { | ||
name String | ||
} | ||
``` | ||
```ts | ||
@@ -208,2 +287,9 @@ builder.model('Project').field('projectCode', 'String').attribute('unique') | ||
```prisma | ||
model Project { | ||
name String | ||
projectCode String @unique | ||
} | ||
``` | ||
```ts | ||
@@ -220,4 +306,88 @@ builder.model('Project').field('name').attribute('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 | ||
@@ -224,0 +394,0 @@ builder.model('Project').blockAttribute('index', ['name']) |
@@ -17,3 +17,3 @@ import * as schema from './getSchema'; | ||
/** These keys preserve the return value context that they were given */ | ||
type NeutralKeys = 'break' | 'comment' | 'attribute' | 'enumerator'; | ||
type NeutralKeys = 'break' | 'comment' | 'attribute' | 'enumerator' | 'then'; | ||
@@ -27,6 +27,6 @@ /** Keys allowed when you call .datasource() or .generator() */ | ||
/** Keys allowed when you call .field("name") */ | ||
type FieldKeys = 'attribute'; | ||
type FieldKeys = 'attribute' | 'removeAttribute'; | ||
/** Keys allowed when you call .model("name") */ | ||
type ModelKeys = 'blockAttribute' | 'field'; | ||
type ModelKeys = 'blockAttribute' | 'field' | 'removeField'; | ||
@@ -65,4 +65,8 @@ /** | ||
? PrismaSchemaSubset<U, DatasourceOrGeneratorKeys | EnumKeys> | ||
: U extends 'removeField' | ||
? PrismaSchemaSubset<U, DatasourceOrGeneratorKeys | EnumKeys | FieldKeys> | ||
: U extends 'enum' | ||
? PrismaSchemaSubset<U, DatasourceOrGeneratorKeys | ModelKeys | FieldKeys> | ||
: U extends 'removeAttribute' | ||
? PrismaSchemaSubset<U, DatasourceOrGeneratorKeys | EnumKeys> | ||
: PrismaSchemaSubset< | ||
@@ -312,2 +316,22 @@ U, | ||
/** Remove an attribute from the current field */ | ||
removeAttribute<T extends schema.Field>(name: string): this { | ||
const parent = this.getParent(); | ||
const subject = this.getSubject<T>(); | ||
if (!parent || !('type' in parent) || parent.type !== 'model') { | ||
throw new Error('Parent must be a prisma model!'); | ||
} | ||
if (!subject || !('type' in subject) || subject.type !== 'field') { | ||
throw new Error('Subject must be a prisma field!'); | ||
} | ||
if (!subject.attributes) subject.attributes = []; | ||
subject.attributes = subject.attributes.filter( | ||
attr => !(attr.type === 'attribute' && attr.name === name) | ||
); | ||
return this; | ||
} | ||
/** Add an assignment to a generator or datasource */ | ||
@@ -447,2 +471,19 @@ assignment<T extends schema.Generator | schema.Datasource>( | ||
/** Drop a field from the current model. */ | ||
removeField(name: string): this { | ||
let subject = this.getSubject<schema.Model>(); | ||
if (!subject || !('type' in subject) || subject.type !== 'model') { | ||
const parent = this.getParent<schema.Model>(); | ||
if (!parent || !('type' in parent) || parent.type !== 'model') | ||
throw new Error('Subject must be a prisma model!'); | ||
subject = this._subject = parent; | ||
} | ||
subject.properties = subject.properties.filter( | ||
field => !(field.type === 'field' && field.name === name) | ||
); | ||
return this; | ||
} | ||
/** | ||
@@ -452,3 +493,5 @@ * Returns the current subject, allowing for more advanced ways of | ||
* */ | ||
then<R extends schema.Block>(callback: (subject: R) => R): this { | ||
then<R extends schema.Block | schema.Property>( | ||
callback: (subject: R) => unknown | ||
): this { | ||
callback(this._subject as R); | ||
@@ -455,0 +498,0 @@ return this; |
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
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
403271
4438
483