New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@mrleebo/prisma-ast

Package Overview
Dependencies
Maintainers
1
Versions
30
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@mrleebo/prisma-ast - npm Package Compare versions

Comparing version 0.5.2 to 0.6.0

dist/schemaUtils.d.ts

22

dist/getSchema.d.ts

@@ -6,8 +6,14 @@ export declare function getSchema(source: string): Schema;

}
export declare type Block = Model | Datasource | Generator | Enum | Comment | Break;
export interface Model {
type: 'model';
export declare type Block = Model | View | Datasource | Generator | Enum | Comment | Break;
export interface Object {
type: 'model' | 'view';
name: string;
properties: Array<Property | Comment | Break>;
}
export interface Model extends Object {
type: 'model';
}
export interface View extends Object {
type: 'view';
}
export interface Datasource {

@@ -35,3 +41,3 @@ type: 'datasource';

}
export declare type Property = GroupedModelAttribute | ModelAttribute | Field;
export declare type Property = GroupedBlockAttribute | BlockAttribute | Field;
export interface Assignment {

@@ -48,5 +54,5 @@ type: 'assignment';

}
export interface ModelAttribute {
export interface BlockAttribute {
type: 'attribute';
kind: 'model';
kind: 'object' | 'view';
group?: string;

@@ -56,3 +62,3 @@ name: string;

}
export declare type GroupedModelAttribute = ModelAttribute & {
export declare type GroupedBlockAttribute = BlockAttribute & {
group: string;

@@ -69,3 +75,3 @@ };

}
export declare type Attr = Attribute | GroupedAttribute | ModelAttribute | GroupedModelAttribute;
export declare type Attr = Attribute | GroupedAttribute | BlockAttribute | GroupedBlockAttribute;
export interface Attribute {

@@ -72,0 +78,0 @@ type: 'attribute';

@@ -6,2 +6,3 @@ import { Lexer, IMultiModeLexerDefinition } from 'chevrotain';

export declare const Model: import("chevrotain").TokenType;
export declare const View: import("chevrotain").TokenType;
export declare const Enum: import("chevrotain").TokenType;

@@ -15,3 +16,3 @@ export declare const True: import("chevrotain").TokenType;

export declare const Attribute: import("chevrotain").TokenType;
export declare const ModelAttribute: import("chevrotain").TokenType;
export declare const BlockAttribute: import("chevrotain").TokenType;
export declare const FieldAttribute: import("chevrotain").TokenType;

@@ -18,0 +19,0 @@ export declare const Dot: import("chevrotain").TokenType;

@@ -5,4 +5,4 @@ import * as Types from './getSchema';

locales?: string | string[];
sortOrder?: Array<'generator' | 'datasource' | 'model' | 'enum'>;
sortOrder?: Array<'generator' | 'datasource' | 'model' | 'view' | 'enum'>;
}
export declare function printSchema(schema: Types.Schema, options?: PrintOptions): string;

@@ -27,2 +27,7 @@ 'use strict';

});
var View = /*#__PURE__*/chevrotain.createToken({
name: 'View',
pattern: /view/,
push_mode: 'block'
});
var Enum = /*#__PURE__*/chevrotain.createToken({

@@ -66,4 +71,4 @@ name: 'Enum',

});
var ModelAttribute = /*#__PURE__*/chevrotain.createToken({
name: 'ModelAttribute',
var BlockAttribute = /*#__PURE__*/chevrotain.createToken({
name: 'BlockAttribute',
pattern: /@@/,

@@ -157,4 +162,4 @@ label: "'@@'",

modes: {
global: /*#__PURE__*/[].concat(naTokens, [Datasource, Generator, Model, Enum]),
block: /*#__PURE__*/[].concat(naTokens, [Attribute, ModelAttribute, FieldAttribute, Dot, QuestionMark, LCurly, RCurly, LSquare, RSquare, LRound, RRound, Comma, Colon, Equals, True, False, Null, StringLiteral, NumberLiteral, Identifier])
global: /*#__PURE__*/[].concat(naTokens, [Datasource, Generator, Model, View, Enum]),
block: /*#__PURE__*/[].concat(naTokens, [Attribute, BlockAttribute, FieldAttribute, Dot, QuestionMark, LCurly, RCurly, LSquare, RSquare, LRound, RRound, Comma, Colon, Equals, True, False, Null, StringLiteral, NumberLiteral, Identifier])
},

@@ -360,3 +365,3 @@ defaultMode: 'global'

var isEnum = componentType === 'enum';
var isModel = componentType === 'model';
var isObject = componentType === 'model' || componentType === 'view';

@@ -376,3 +381,3 @@ _this.CONSUME(LCurly);

GATE: function GATE() {
return isModel;
return isObject;
},

@@ -392,3 +397,3 @@ ALT: function ALT() {

GATE: function GATE() {
return isModel;
return isObject;
},

@@ -411,3 +416,3 @@ ALT: function ALT() {

GATE: function GATE() {
return !isModel;
return !isObject;
},

@@ -448,4 +453,4 @@ ALT: function ALT() {

ALT: function ALT() {
return _this.CONSUME(ModelAttribute, {
LABEL: 'modelAttribute'
return _this.CONSUME(BlockAttribute, {
LABEL: 'blockAttribute'
});

@@ -530,2 +535,8 @@ }

ALT: function ALT() {
return _this.CONSUME(View, {
LABEL: 'type'
});
}
}, {
ALT: function ALT() {
return _this.CONSUME(Enum, {

@@ -663,2 +674,9 @@ LABEL: 'type'

case 'view':
return {
type: 'view',
name: name,
properties: list
};
case 'enum':

@@ -746,3 +764,3 @@ return {

});
var kind = ctx.modelAttribute != null ? 'model' : 'field';
var kind = ctx.blockAttribute != null ? 'object' : 'field';
return {

@@ -847,3 +865,3 @@ type: 'attribute',

var unsorted = ['break', 'comment'];
var defaultSortOrder = ['generator', 'datasource', 'model', 'enum', 'break', 'comment'];
var defaultSortOrder = ['generator', 'datasource', 'model', 'view', 'enum', 'break', 'comment'];
var schemaSorter = function schemaSorter(schema, locales, sortOrder) {

@@ -910,3 +928,4 @@ if (sortOrder === void 0) {

case 'model':
return printModel(block);
case 'view':
return printObject(block);

@@ -963,5 +982,5 @@ case 'break':

function printModel(model) {
var children = computePropertyFormatting(model.properties);
return "\nmodel " + model.name + " {\n " + children + "\n}";
function printObject(object) {
var children = computePropertyFormatting(object.properties);
return "\n" + object.type + " " + object.name + " {\n " + children + "\n}";
}

@@ -1141,2 +1160,10 @@

var schemaObjects = ['model', 'view'];
function isSchemaObject(obj) {
return obj != null && 'type' in obj && schemaObjects.includes(obj.type);
}
function isSchemaField(field) {
return field != null && 'type' in field && field.type === 'field';
}
var ConcretePrismaSchemaBuilder = /*#__PURE__*/function () {

@@ -1233,2 +1260,15 @@ function ConcretePrismaSchemaBuilder(source) {

_proto.view = function view(name) {
var view = this.schema.list.reduce(function (memo, block) {
return block.type === 'view' && block.name === name ? block : memo;
}, {
type: 'view',
name: name,
properties: []
});
if (!this.schema.list.includes(view)) this.schema.list.push(view);
this._subject = view;
return this;
};
_proto["enum"] = function _enum(name, enumeratorNames) {

@@ -1260,3 +1300,3 @@ if (enumeratorNames === void 0) {

if (!subject || !('type' in subject) || subject.type !== 'enum') {
throw new Error('Subject must be a prisma model!');
throw new Error('Subject must be a prisma enum!');
}

@@ -1282,5 +1322,5 @@

if (!subject || !('type' in subject) || subject.type !== 'model') {
if (!isSchemaObject(subject)) {
var parent = this.getParent();
if (!parent || !('type' in parent) || parent.type !== 'model') throw new Error('Subject must be a prisma model!');
if (!isSchemaObject(parent)) throw new Error('Subject must be a prisma model or view!');
subject = this._subject = parent;

@@ -1318,3 +1358,3 @@ }

type: 'attribute',
kind: 'model',
kind: 'object',
name: name,

@@ -1331,7 +1371,7 @@ args: attributeArgs

if (!parent || !('type' in parent) || parent.type !== 'model') {
throw new Error('Parent must be a prisma model!');
if (!isSchemaObject(parent)) {
throw new Error('Parent must be a prisma model or view!');
}
if (!subject || !('type' in subject) || subject.type !== 'field') {
if (!isSchemaField(subject)) {
throw new Error('Subject must be a prisma field!');

@@ -1392,7 +1432,7 @@ }

if (!parent || !('type' in parent) || parent.type !== 'model') {
throw new Error('Parent must be a prisma model!');
if (!isSchemaObject(parent)) {
throw new Error('Parent must be a prisma model or view!');
}
if (!subject || !('type' in subject) || subject.type !== 'field') {
if (!isSchemaField(subject)) {
throw new Error('Subject must be a prisma field!');

@@ -1432,3 +1472,3 @@ }

var subject = this.getSubject();
var allowed = ['datasource', 'enum', 'generator', 'model'];
var allowed = ['datasource', 'enum', 'generator', 'model', 'view'];

@@ -1513,5 +1553,5 @@ if (!subject || !('type' in subject) || !allowed.includes(subject.type)) {

if (!subject || !('type' in subject) || subject.type !== 'model') {
if (!isSchemaObject(subject)) {
var parent = this.getParent();
if (!parent || !('type' in parent) || parent.type !== 'model') throw new Error('Subject must be a prisma model!');
if (!isSchemaObject(parent)) throw new Error('Subject must be a prisma model or view!');
subject = this._subject = parent;

@@ -1536,5 +1576,5 @@ }

if (!subject || !('type' in subject) || subject.type !== 'model') {
if (!isSchemaObject(subject)) {
var parent = this.getParent();
if (!parent || !('type' in parent) || parent.type !== 'model') throw new Error('Subject must be a prisma model!');
if (!isSchemaObject(parent)) throw new Error('Subject must be a prisma model or view!');
subject = this._subject = parent;

@@ -1541,0 +1581,0 @@ }

@@ -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"}),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.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(!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;
"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:"View",pattern:/view/,push_mode:"block"}),o=e.createToken({name:"Enum",pattern:/enum/,push_mode:"block"}),s=e.createToken({name:"True",pattern:/true/,longer_alt:n}),c=e.createToken({name:"False",pattern:/false/,longer_alt:n}),m=e.createToken({name:"Null",pattern:/null/,longer_alt:n}),l=e.createToken({name:"Comment",pattern:e.Lexer.NA}),p=e.createToken({name:"DocComment",pattern:/\/\/\/\s*(.+)/,categories:[l]}),f=e.createToken({name:"LineComment",pattern:/\/\/\s*(.+)/,categories:[l]}),L=e.createToken({name:"Attribute",pattern:e.Lexer.NA}),E=e.createToken({name:"BlockAttribute",pattern:/@@/,label:"'@@'",categories:[L]}),d=e.createToken({name:"FieldAttribute",pattern:/@/,label:"'@'",categories:[L]}),h=e.createToken({name:"Dot",pattern:/\./,label:"'.'"}),y=e.createToken({name:"QuestionMark",pattern:/\?/,label:"'?'"}),v=e.createToken({name:"LCurly",pattern:/{/,label:"'{'"}),b=e.createToken({name:"RCurly",pattern:/}/,label:"'}'",pop_mode:!0}),g=e.createToken({name:"LRound",pattern:/\(/,label:"'('"}),A=e.createToken({name:"RRound",pattern:/\)/,label:"')'"}),U=e.createToken({name:"LSquare",pattern:/\[/,label:"'['"}),k=e.createToken({name:"RSquare",pattern:/\]/,label:"']'"}),S=e.createToken({name:"Comma",pattern:/,/,label:"','"}),O=e.createToken({name:"Colon",pattern:/:/,label:"':'"}),B=e.createToken({name:"Equals",pattern:/=/,label:"'='"}),T=e.createToken({name:"StringLiteral",pattern:/"(:?[^\\"\n\r]|\\(:?[bfnrtv"\\/]|u[0-9a-fA-F]{4}))*"/}),N=e.createToken({name:"NumberLiteral",pattern:/-?(0|[1-9]\d*)(\.\d+)?([eE][+-]?\d+)?/}),w=e.createToken({name:"WhiteSpace",pattern:/\s+/,group:e.Lexer.SKIPPED}),C=e.createToken({name:"LineBreak",pattern:/\n|\r\n/,line_breaks:!0,label:"LineBreak"}),M=[l,p,f,C,w],R={modes:{global:[].concat(M,[r,a,i,u,o]),block:[].concat(M,[L,E,d,h,y,v,b,U,k,g,A,S,O,B,s,c,m,T,N,n])},defaultMode:"global"},j=new e.Lexer(R);function _(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(C),t.CONSUME2(C)})),t.keyedArg=t.RULE("keyedArg",(function(){t.CONSUME(n,{LABEL:"keyName"}),t.CONSUME(O),t.SUBRULE(t.value)})),t.array=t.RULE("array",(function(){t.CONSUME(U),t.MANY_SEP({SEP:S,DEF:function(){t.SUBRULE(t.value)}}),t.CONSUME(k)})),t.func=t.RULE("func",(function(){t.CONSUME(n,{LABEL:"funcName"}),t.CONSUME(g),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(A)})),t.value=t.RULE("value",(function(){t.OR([{ALT:function(){return t.CONSUME(T,{LABEL:"value"})}},{ALT:function(){return t.CONSUME(N,{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(s,{LABEL:"value"})}},{ALT:function(){return t.CONSUME(c,{LABEL:"value"})}},{ALT:function(){return t.CONSUME(m,{LABEL:"value"})}},{ALT:function(){return t.CONSUME(n,{LABEL:"value"})}}])})),t.property=t.RULE("property",(function(){t.CONSUME(n,{LABEL:"propertyName"}),t.CONSUME(B),t.SUBRULE(t.value,{LABEL:"propertyValue"})})),t.assignment=t.RULE("assignment",(function(){t.CONSUME(n,{LABEL:"assignmentName"}),t.CONSUME(B),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(U,{LABEL:"array"}),t.CONSUME(k,{LABEL:"array"})}},{ALT:function(){return t.CONSUME(y,{LABEL:"optional"})}}])})),t.MANY((function(){t.SUBRULE(t.attribute,{LABEL:"attributeList"})})),t.OPTION2((function(){t.CONSUME(l,{LABEL:"comment"})}))})),t.block=t.RULE("block",(function(e){void 0===e&&(e={});var n=e.componentType,r="enum"===n,a="model"===n||"view"===n;t.CONSUME(v),t.CONSUME1(C),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(C)}}])})),t.CONSUME(b)})),t.enum=t.RULE("enum",(function(){t.CONSUME(n,{LABEL:"enumName"}),t.OPTION((function(){t.CONSUME(l,{LABEL:"comment"})}))})),t.attribute=t.RULE("attribute",(function(){t.OR1([{ALT:function(){return t.CONSUME(E,{LABEL:"blockAttribute"})}},{ALT:function(){return t.CONSUME(d,{LABEL:"fieldAttribute"})}}]),t.OR2([{ALT:function(){t.CONSUME1(n,{LABEL:"groupName"}),t.CONSUME(h),t.CONSUME2(n,{LABEL:"attributeName"})}},{ALT:function(){return t.CONSUME(n,{LABEL:"attributeName"})}}]),t.OPTION((function(){t.CONSUME(g),t.MANY_SEP({SEP:S,DEF:function(){t.SUBRULE(t.attributeArg)}}),t.CONSUME(A)}))})),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"})}},{ALT:function(){return t.CONSUME(o,{LABEL:"type"})}}]);t.OR2([{ALT:function(){t.CONSUME1(n,{LABEL:"groupName"}),t.CONSUME(h),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(l,{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(C)}}])}))})),t.performSelfAnalysis(),t}return _(t,e),t}(e.CstParser)),P=function(e){function t(){var t;return(t=e.call(this)||this).validateVisitor(),t}_(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"view":return{type:"view",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.blockAttribute?"object":"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 I(e){var t=j.tokenize(e);x.input=t.tokens;var n=x.schema();if(x.errors.length>0)throw x.errors[0];return(new P).visit(n)}var V=["break","comment"],D=["generator","datasource","model","view","enum","break","comment"];function F(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=D),function(r,a){if(-1!==V.indexOf(r.type)!=(-1!==V.indexOf(a.type)))return e.list.indexOf(r)-e.list.indexOf(a);n!==D&&(n=n.concat(D));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(G).filter(Boolean).join(t.EOL).replace(/(\r?\n\s*){3,}/g,t.EOL+t.EOL)+t.EOL}function G(e){switch(e.type){case"comment":return Y(e);case"datasource":return r=J((n=e).assignments),"\ndatasource "+n.name+" {\n "+r+"\n}";case"enum":return function(e){var n=e.enumerators.map(z).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=J(e.assignments);return"\ngenerator "+e.name+" {\n "+t+"\n}"}(e);case"model":case"view":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?Q(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 W(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=Q(e).padEnd(n),i=e.attributes?e.attributes.map(W):[],u=e.comment;return[r,a].concat(i).filter(Boolean).join(" ").trim()+(u?" "+u:"")}(e,t,n);case"comment":return Y(e);case"break":return q();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"\n"+e.type+" "+e.name+" {\n "+o+"\n}"}(e);case"break":return q();default:throw new Error("Unrecognized block type")}var n,r}function Y(e){return e.text}function q(){return t.EOL}function z(e){switch(e.type){case"enumerator":return[e.name,e.comment].filter(Boolean).join(" ");case"attribute":return W(e);case"comment":return Y(e);case"break":return q();default:throw new Error("Unexpected enumerator type")}}function W(e){var t=e.args&&e.args.length>0?"("+e.args.map(K).filter(Boolean).join(", ")+")":"",n=[e.name];return e.group&&n.unshift(e.group),("field"===e.kind?"@":"@@")+n.join(".")+t}function K(e){return H(e.value)}function Q(e){var t=e.array?"[]":e.optional?"?":"";if("object"==typeof e.fieldType)switch(e.fieldType.type){case"function":return""+Z(e.fieldType)+t;default:throw new Error("Unexpected field type")}return""+e.fieldType+t}function Z(e){var t=e.params?e.params.map(H):"";return e.name+"("+t+")"}function H(e){switch(typeof e){case"object":if("type"in e)switch(e.type){case"keyValue":return e.key+": "+H(e.value);case"function":return Z(e);case"array":return"["+(null!=e.args?e.args.map(H).join(", "):"")+"]";default:throw new Error("Unexpected value type")}throw new Error("Unexpected object value");default:return String(e)}}function J(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 Y(e);case"break":return q();case"assignment":return e.key.padEnd(t)+" = "+H(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 X=["model","view"];function $(e){return null!=e&&"type"in e&&X.includes(e.type)}function ee(e){return null!=e&&"type"in e&&"field"===e.type}var te=function(){function e(e){void 0===e&&(e=""),this.schema=I(e)}var t=e.prototype;return t.print=function(e){return void 0===e&&(e={}),F(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.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.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(!$(n)){var r=this.getParent();if(!$(r))throw new Error("Subject must be a prisma model or view!");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(!$(n))throw new Error("Parent must be a prisma model or view!");if(!ee(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(!$(t))throw new Error("Parent must be a prisma model or view!");if(!ee(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"];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)){var r=this.getParent();if(!$(r))throw new Error("Subject must be a prisma model or view!");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)){var n=this.getParent();if(!$(n))throw new Error("Subject must be a prisma model or view!");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=te,exports.createPrismaSchemaBuilder=function(e){return new te(e)},exports.getSchema=I,exports.printSchema=F;
//# sourceMappingURL=prisma-ast.cjs.production.min.js.map

@@ -23,2 +23,7 @@ import { Lexer, createToken, CstParser } from 'chevrotain';

});
var View = /*#__PURE__*/createToken({
name: 'View',
pattern: /view/,
push_mode: 'block'
});
var Enum = /*#__PURE__*/createToken({

@@ -62,4 +67,4 @@ name: 'Enum',

});
var ModelAttribute = /*#__PURE__*/createToken({
name: 'ModelAttribute',
var BlockAttribute = /*#__PURE__*/createToken({
name: 'BlockAttribute',
pattern: /@@/,

@@ -153,4 +158,4 @@ label: "'@@'",

modes: {
global: /*#__PURE__*/[].concat(naTokens, [Datasource, Generator, Model, Enum]),
block: /*#__PURE__*/[].concat(naTokens, [Attribute, ModelAttribute, FieldAttribute, Dot, QuestionMark, LCurly, RCurly, LSquare, RSquare, LRound, RRound, Comma, Colon, Equals, True, False, Null, StringLiteral, NumberLiteral, Identifier])
global: /*#__PURE__*/[].concat(naTokens, [Datasource, Generator, Model, View, Enum]),
block: /*#__PURE__*/[].concat(naTokens, [Attribute, BlockAttribute, FieldAttribute, Dot, QuestionMark, LCurly, RCurly, LSquare, RSquare, LRound, RRound, Comma, Colon, Equals, True, False, Null, StringLiteral, NumberLiteral, Identifier])
},

@@ -356,3 +361,3 @@ defaultMode: 'global'

var isEnum = componentType === 'enum';
var isModel = componentType === 'model';
var isObject = componentType === 'model' || componentType === 'view';

@@ -372,3 +377,3 @@ _this.CONSUME(LCurly);

GATE: function GATE() {
return isModel;
return isObject;
},

@@ -388,3 +393,3 @@ ALT: function ALT() {

GATE: function GATE() {
return isModel;
return isObject;
},

@@ -407,3 +412,3 @@ ALT: function ALT() {

GATE: function GATE() {
return !isModel;
return !isObject;
},

@@ -444,4 +449,4 @@ ALT: function ALT() {

ALT: function ALT() {
return _this.CONSUME(ModelAttribute, {
LABEL: 'modelAttribute'
return _this.CONSUME(BlockAttribute, {
LABEL: 'blockAttribute'
});

@@ -526,2 +531,8 @@ }

ALT: function ALT() {
return _this.CONSUME(View, {
LABEL: 'type'
});
}
}, {
ALT: function ALT() {
return _this.CONSUME(Enum, {

@@ -659,2 +670,9 @@ LABEL: 'type'

case 'view':
return {
type: 'view',
name: name,
properties: list
};
case 'enum':

@@ -742,3 +760,3 @@ return {

});
var kind = ctx.modelAttribute != null ? 'model' : 'field';
var kind = ctx.blockAttribute != null ? 'object' : 'field';
return {

@@ -843,3 +861,3 @@ type: 'attribute',

var unsorted = ['break', 'comment'];
var defaultSortOrder = ['generator', 'datasource', 'model', 'enum', 'break', 'comment'];
var defaultSortOrder = ['generator', 'datasource', 'model', 'view', 'enum', 'break', 'comment'];
var schemaSorter = function schemaSorter(schema, locales, sortOrder) {

@@ -906,3 +924,4 @@ if (sortOrder === void 0) {

case 'model':
return printModel(block);
case 'view':
return printObject(block);

@@ -959,5 +978,5 @@ case 'break':

function printModel(model) {
var children = computePropertyFormatting(model.properties);
return "\nmodel " + model.name + " {\n " + children + "\n}";
function printObject(object) {
var children = computePropertyFormatting(object.properties);
return "\n" + object.type + " " + object.name + " {\n " + children + "\n}";
}

@@ -1137,2 +1156,10 @@

var schemaObjects = ['model', 'view'];
function isSchemaObject(obj) {
return obj != null && 'type' in obj && schemaObjects.includes(obj.type);
}
function isSchemaField(field) {
return field != null && 'type' in field && field.type === 'field';
}
var ConcretePrismaSchemaBuilder = /*#__PURE__*/function () {

@@ -1229,2 +1256,15 @@ function ConcretePrismaSchemaBuilder(source) {

_proto.view = function view(name) {
var view = this.schema.list.reduce(function (memo, block) {
return block.type === 'view' && block.name === name ? block : memo;
}, {
type: 'view',
name: name,
properties: []
});
if (!this.schema.list.includes(view)) this.schema.list.push(view);
this._subject = view;
return this;
};
_proto["enum"] = function _enum(name, enumeratorNames) {

@@ -1256,3 +1296,3 @@ if (enumeratorNames === void 0) {

if (!subject || !('type' in subject) || subject.type !== 'enum') {
throw new Error('Subject must be a prisma model!');
throw new Error('Subject must be a prisma enum!');
}

@@ -1278,5 +1318,5 @@

if (!subject || !('type' in subject) || subject.type !== 'model') {
if (!isSchemaObject(subject)) {
var parent = this.getParent();
if (!parent || !('type' in parent) || parent.type !== 'model') throw new Error('Subject must be a prisma model!');
if (!isSchemaObject(parent)) throw new Error('Subject must be a prisma model or view!');
subject = this._subject = parent;

@@ -1314,3 +1354,3 @@ }

type: 'attribute',
kind: 'model',
kind: 'object',
name: name,

@@ -1327,7 +1367,7 @@ args: attributeArgs

if (!parent || !('type' in parent) || parent.type !== 'model') {
throw new Error('Parent must be a prisma model!');
if (!isSchemaObject(parent)) {
throw new Error('Parent must be a prisma model or view!');
}
if (!subject || !('type' in subject) || subject.type !== 'field') {
if (!isSchemaField(subject)) {
throw new Error('Subject must be a prisma field!');

@@ -1388,7 +1428,7 @@ }

if (!parent || !('type' in parent) || parent.type !== 'model') {
throw new Error('Parent must be a prisma model!');
if (!isSchemaObject(parent)) {
throw new Error('Parent must be a prisma model or view!');
}
if (!subject || !('type' in subject) || subject.type !== 'field') {
if (!isSchemaField(subject)) {
throw new Error('Subject must be a prisma field!');

@@ -1428,3 +1468,3 @@ }

var subject = this.getSubject();
var allowed = ['datasource', 'enum', 'generator', 'model'];
var allowed = ['datasource', 'enum', 'generator', 'model', 'view'];

@@ -1509,5 +1549,5 @@ if (!subject || !('type' in subject) || !allowed.includes(subject.type)) {

if (!subject || !('type' in subject) || subject.type !== 'model') {
if (!isSchemaObject(subject)) {
var parent = this.getParent();
if (!parent || !('type' in parent) || parent.type !== 'model') throw new Error('Subject must be a prisma model!');
if (!isSchemaObject(parent)) throw new Error('Subject must be a prisma model or view!');
subject = this._subject = parent;

@@ -1532,5 +1572,5 @@ }

if (!subject || !('type' in subject) || subject.type !== 'model') {
if (!isSchemaObject(subject)) {
var parent = this.getParent();
if (!parent || !('type' in parent) || parent.type !== 'model') throw new Error('Subject must be a prisma model!');
if (!isSchemaObject(parent)) throw new Error('Subject must be a prisma model or view!');
subject = this._subject = parent;

@@ -1537,0 +1577,0 @@ }

@@ -9,6 +9,6 @@ import * as schema from './getSchema';

declare type FieldKeys = 'attribute' | 'removeAttribute';
declare type ModelKeys = 'blockAttribute' | 'field' | 'removeField';
declare type BlockKeys = '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 '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'>;
[U in K]: U extends ExtractKeys ? ConcretePrismaSchemaBuilder[U] : U extends NeutralKeys ? ConcretePrismaSchemaBuilder[U] : U extends 'datasource' ? PrismaSchemaSubset<U, 'datasource' | EnumKeys | FieldKeys | BlockKeys> : U extends 'generator' ? PrismaSchemaSubset<U, EnumKeys | FieldKeys | BlockKeys> : U extends 'model' ? PrismaSchemaSubset<U, DatasourceOrGeneratorKeys | EnumKeys | FieldKeys> : U extends 'view' ? 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 | BlockKeys | FieldKeys> : U extends 'removeAttribute' ? PrismaSchemaSubset<U, DatasourceOrGeneratorKeys | EnumKeys> : PrismaSchemaSubset<U, DatasourceOrGeneratorKeys | EnumKeys | FieldKeys | BlockKeys | 'comment'>;
};

@@ -32,2 +32,3 @@ declare type Arg = string | {

model(name: string): this;
view(name: string): this;
enum(name: string, enumeratorNames?: string[]): this;

@@ -49,3 +50,3 @@ enumerator(value: string): this;

}
export declare function createPrismaSchemaBuilder(source?: string): PrismaSchemaBuilder<Exclude<keyof ConcretePrismaSchemaBuilder, DatasourceOrGeneratorKeys | EnumKeys | FieldKeys | ModelKeys>>;
export declare function createPrismaSchemaBuilder(source?: string): PrismaSchemaBuilder<Exclude<keyof ConcretePrismaSchemaBuilder, DatasourceOrGeneratorKeys | EnumKeys | FieldKeys | BlockKeys>>;
export {};

@@ -34,3 +34,3 @@ import { CstNode, IToken } from '@chevrotain/types';

attribute(ctx: CstNode & {
modelAttribute: [IToken];
blockAttribute: [IToken];
fieldAttribute: [IToken];

@@ -37,0 +37,0 @@ groupName: [IToken];

{
"version": "0.5.2",
"version": "0.6.0",
"license": "MIT",

@@ -20,4 +20,3 @@ "main": "dist/index.js",

"prepare": "tsdx build",
"size": "size-limit",
"analyze": "size-limit --why",
"size": "NODE_OPTIONS=--openssl-legacy-provider size-limit",
"publish-better": "npx np"

@@ -43,7 +42,7 @@ },

"path": "dist/prisma-ast.cjs.production.min.js",
"limit": "45 KB"
"limit": "56 KB"
},
{
"path": "dist/prisma-ast.esm.js",
"limit": "45 KB"
"limit": "56 KB"
}

@@ -57,3 +56,3 @@ ],

"husky": "^6.0.0",
"size-limit": "^4.10.2",
"size-limit": "^8.2.4",
"tsdx": "^0.14.1",

@@ -64,3 +63,3 @@ "tslib": "^2.4.0",

"dependencies": {
"chevrotain": "^10.4.2"
"chevrotain": "^10.5.0"
},

@@ -67,0 +66,0 @@ "publishConfig": {

@@ -19,6 +19,13 @@ import { PrismaLexer } from './lexer';

export type Block = Model | Datasource | Generator | Enum | Comment | Break;
export type Block =
| Model
| View
| Datasource
| Generator
| Enum
| Comment
| Break;
export interface Model {
type: 'model';
export interface Object {
type: 'model' | 'view';
name: string;

@@ -28,2 +35,10 @@ properties: Array<Property | Comment | Break>;

export interface Model extends Object {
type: 'model';
}
export interface View extends Object {
type: 'view';
}
export interface Datasource {

@@ -56,3 +71,3 @@ type: 'datasource';

export type Property = GroupedModelAttribute | ModelAttribute | Field;
export type Property = GroupedBlockAttribute | BlockAttribute | Field;

@@ -72,5 +87,5 @@ export interface Assignment {

export interface ModelAttribute {
export interface BlockAttribute {
type: 'attribute';
kind: 'model';
kind: 'object' | 'view';
group?: string;

@@ -81,3 +96,3 @@ name: string;

export type GroupedModelAttribute = ModelAttribute & { group: string };
export type GroupedBlockAttribute = BlockAttribute & { group: string };

@@ -97,4 +112,4 @@ export interface Field {

| GroupedAttribute
| ModelAttribute
| GroupedModelAttribute;
| BlockAttribute
| GroupedBlockAttribute;

@@ -101,0 +116,0 @@ export interface Attribute {

@@ -22,2 +22,7 @@ import { createToken, Lexer, IMultiModeLexerDefinition } from 'chevrotain';

});
export const View = createToken({
name: 'View',
pattern: /view/,
push_mode: 'block',
});
export const Enum = createToken({

@@ -62,4 +67,4 @@ name: 'Enum',

});
export const ModelAttribute = createToken({
name: 'ModelAttribute',
export const BlockAttribute = createToken({
name: 'BlockAttribute',
pattern: /@@/,

@@ -155,7 +160,7 @@ label: "'@@'",

modes: {
global: [...naTokens, Datasource, Generator, Model, Enum],
global: [...naTokens, Datasource, Generator, Model, View, Enum],
block: [
...naTokens,
Attribute,
ModelAttribute,
BlockAttribute,
FieldAttribute,

@@ -162,0 +167,0 @@ Dot,

import { CstParser } from 'chevrotain';
import * as lexer from './lexer';
type ComponentType = 'datasource' | 'generator' | 'model' | 'enum';
type ComponentType = 'datasource' | 'generator' | 'model' | 'view' | 'enum';
export class PrismaParser extends CstParser {

@@ -104,3 +104,3 @@ constructor() {

const isEnum = componentType === 'enum';
const isModel = componentType === 'model';
const isObject = componentType === 'model' || componentType === 'view';

@@ -113,3 +113,3 @@ this.CONSUME(lexer.LCurly);

{
GATE: () => isModel,
GATE: () => isObject,
ALT: () => this.SUBRULE(this.property, { LABEL: 'list' }),

@@ -119,3 +119,3 @@ },

{
GATE: () => isModel,
GATE: () => isObject,
ALT: () => this.SUBRULE(this.field, { LABEL: 'list' }),

@@ -128,3 +128,3 @@ },

{
GATE: () => !isModel,
GATE: () => !isObject,
ALT: () => this.SUBRULE(this.assignment, { LABEL: 'list' }),

@@ -150,3 +150,3 @@ },

ALT: () =>
this.CONSUME(lexer.ModelAttribute, { LABEL: 'modelAttribute' }),
this.CONSUME(lexer.BlockAttribute, { LABEL: 'blockAttribute' }),
},

@@ -199,2 +199,3 @@ {

{ ALT: () => this.CONSUME(lexer.Model, { LABEL: 'type' }) },
{ ALT: () => this.CONSUME(lexer.View, { LABEL: 'type' }) },
{ ALT: () => this.CONSUME(lexer.Enum, { LABEL: 'type' }) },

@@ -201,0 +202,0 @@ ]);

@@ -8,3 +8,3 @@ import * as Types from './getSchema';

locales?: string | string[];
sortOrder?: Array<'generator' | 'datasource' | 'model' | 'enum'>;
sortOrder?: Array<'generator' | 'datasource' | 'model' | 'view' | 'enum'>;
}

@@ -45,3 +45,4 @@

case 'model':
return printModel(block);
case 'view':
return printObject(block);
case 'break':

@@ -110,7 +111,7 @@ return printBreak();

function printModel(model: Types.Model) {
const children = computePropertyFormatting(model.properties);
function printObject(object: Types.Object) {
const children = computePropertyFormatting(object.properties);
return `
model ${model.name} {
${object.type} ${object.name} {
${children}

@@ -155,3 +156,3 @@ }`;

function printAttribute(attribute: Types.Attribute | Types.ModelAttribute) {
function printAttribute(attribute: Types.Attribute | Types.BlockAttribute) {
const args =

@@ -158,0 +159,0 @@ attribute.args && attribute.args.length > 0

import * as schema from './getSchema';
import { isSchemaField, isSchemaObject } from './schemaUtils';
import { PrintOptions, printSchema } from './printSchema';

@@ -29,3 +30,3 @@

/** Keys allowed when you call .model("name") */
type ModelKeys = 'blockAttribute' | 'field' | 'removeField';
type BlockKeys = 'blockAttribute' | 'field' | 'removeField';

@@ -57,7 +58,9 @@ /**

: U extends 'datasource'
? PrismaSchemaSubset<U, 'datasource' | EnumKeys | FieldKeys | ModelKeys>
? PrismaSchemaSubset<U, 'datasource' | EnumKeys | FieldKeys | BlockKeys>
: U extends 'generator'
? PrismaSchemaSubset<U, EnumKeys | FieldKeys | ModelKeys>
? PrismaSchemaSubset<U, EnumKeys | FieldKeys | BlockKeys>
: U extends 'model'
? PrismaSchemaSubset<U, DatasourceOrGeneratorKeys | EnumKeys | FieldKeys>
: U extends 'view'
? PrismaSchemaSubset<U, DatasourceOrGeneratorKeys | EnumKeys | FieldKeys>
: U extends 'field'

@@ -68,3 +71,3 @@ ? PrismaSchemaSubset<U, DatasourceOrGeneratorKeys | EnumKeys>

: U extends 'enum'
? PrismaSchemaSubset<U, DatasourceOrGeneratorKeys | ModelKeys | FieldKeys>
? PrismaSchemaSubset<U, DatasourceOrGeneratorKeys | BlockKeys | FieldKeys>
: U extends 'removeAttribute'

@@ -74,3 +77,3 @@ ? PrismaSchemaSubset<U, DatasourceOrGeneratorKeys | EnumKeys>

U,
DatasourceOrGeneratorKeys | EnumKeys | FieldKeys | ModelKeys | 'comment'
DatasourceOrGeneratorKeys | EnumKeys | FieldKeys | BlockKeys | 'comment'
>;

@@ -165,3 +168,3 @@ };

/** Adds or updates a model based on the name. Can be chained with .field() or .modelAttribute() to add to it. */
/** Adds or updates a model based on the name. Can be chained with .field() or .blockAttribute() to add to it. */
model(name: string): this {

@@ -178,2 +181,14 @@ const model = this.schema.list.reduce<schema.Model>(

/** Adds or updates a view based on the name. Can be chained with .field() or .blockAttribute() to add to it. */
view(name: string): this {
const view = this.schema.list.reduce<schema.View>(
(memo, block) =>
block.type === 'view' && block.name === name ? block : memo,
{ type: 'view', name, properties: [] }
);
if (!this.schema.list.includes(view)) this.schema.list.push(view);
this._subject = view;
return this;
}
/** Adds or updates an enum based on the name. Can be chained with .enumerator() to add a value to it. */

@@ -201,3 +216,3 @@ enum(name: string, enumeratorNames: string[] = []): this {

if (!subject || !('type' in subject) || subject.type !== 'enum') {
throw new Error('Subject must be a prisma model!');
throw new Error('Subject must be a prisma enum!');
}

@@ -218,4 +233,4 @@

/** Returns the parent of the current subject when in a nested context. The parent of a field is its model. */
private getParent<S extends Parent = schema.Model>(): S {
/** Returns the parent of the current subject when in a nested context. The parent of a field is its model or view. */
private getParent<S extends Parent = schema.Object>(): S {
return this._parent as S;

@@ -235,7 +250,7 @@ }

): 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!');
let subject = this.getSubject<schema.Object>();
if (!isSchemaObject(subject)) {
const parent = this.getParent<schema.Object>();
if (!isSchemaObject(parent))
throw new Error('Subject must be a prisma model or view!');

@@ -257,5 +272,5 @@ subject = this._subject = parent;

const property: schema.ModelAttribute = {
const property: schema.BlockAttribute = {
type: 'attribute',
kind: 'model',
kind: 'object',
name,

@@ -275,7 +290,7 @@ args: attributeArgs,

const subject = this.getSubject<T>();
if (!parent || !('type' in parent) || parent.type !== 'model') {
throw new Error('Parent must be a prisma model!');
if (!isSchemaObject(parent)) {
throw new Error('Parent must be a prisma model or view!');
}
if (!subject || !('type' in subject) || subject.type !== 'field') {
if (!isSchemaField(subject)) {
throw new Error('Subject must be a prisma field!');

@@ -331,7 +346,7 @@ }

const subject = this.getSubject<T>();
if (!parent || !('type' in parent) || parent.type !== 'model') {
throw new Error('Parent must be a prisma model!');
if (!isSchemaObject(parent)) {
throw new Error('Parent must be a prisma model or view!');
}
if (!subject || !('type' in subject) || subject.type !== 'field') {
if (!isSchemaField(subject)) {
throw new Error('Subject must be a prisma field!');

@@ -388,3 +403,3 @@ }

let subject = this.getSubject<schema.Block>();
const allowed = ['datasource', 'enum', 'generator', 'model'];
const allowed = ['datasource', 'enum', 'generator', 'model', 'view'];
if (!subject || !('type' in subject) || !allowed.includes(subject.type)) {

@@ -458,7 +473,7 @@ const parent = this.getParent<schema.Block>();

field(name: string, fieldType: string | schema.Func = '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!');
let subject = this.getSubject<schema.Object>();
if (!isSchemaObject(subject)) {
const parent = this.getParent<schema.Object>();
if (!isSchemaObject(parent))
throw new Error('Subject must be a prisma model or view!');

@@ -484,9 +499,9 @@ subject = this._subject = parent;

/** Drop a field from the current model. */
/** Drop a field from the current model or view. */
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!');
let subject = this.getSubject<schema.Object>();
if (!isSchemaObject(subject)) {
const parent = this.getParent<schema.Object>();
if (!isSchemaObject(parent))
throw new Error('Subject must be a prisma model or view!');

@@ -519,3 +534,3 @@ subject = this._subject = parent;

keyof ConcretePrismaSchemaBuilder,
DatasourceOrGeneratorKeys | EnumKeys | FieldKeys | ModelKeys
DatasourceOrGeneratorKeys | EnumKeys | FieldKeys | BlockKeys
>

@@ -522,0 +537,0 @@ > {

@@ -8,2 +8,3 @@ import { Block, Schema } from './getSchema';

'model',
'view',
'enum',

@@ -10,0 +11,0 @@ 'break',

@@ -35,2 +35,4 @@ import { CstNode, IToken } from '@chevrotain/types';

return { type: 'model', name, properties: list };
case 'view':
return { type: 'view', name, properties: list };
case 'enum':

@@ -92,3 +94,3 @@ return { type: 'enum', name, enumerators: list };

ctx: CstNode & {
modelAttribute: [IToken];
blockAttribute: [IToken];
fieldAttribute: [IToken];

@@ -104,3 +106,3 @@ groupName: [IToken];

ctx.attributeArg && ctx.attributeArg.map(attr => this.visit(attr));
const kind = ctx.modelAttribute != null ? 'model' : 'field';
const kind = ctx.blockAttribute != null ? 'object' : 'field';

@@ -115,7 +117,13 @@ return { type: 'attribute', name, kind, group, args };

func(ctx: CstNode & { funcName: [IToken]; value: CstNode[]; keyedArg: CstNode[] }): Types.Func {
func(
ctx: CstNode & { funcName: [IToken]; value: CstNode[]; keyedArg: CstNode[] }
): Types.Func {
const [{ image: name }] = ctx.funcName;
const params = ctx.value && ctx.value.map(item => this.visit([item]));
const keyedParams = ctx.keyedArg && ctx.keyedArg.map(item => this.visit([item]));
const pars = (params || keyedParams) && [...(params ?? []), ...(keyedParams ?? [])];
const keyedParams =
ctx.keyedArg && ctx.keyedArg.map(item => this.visit([item]));
const pars = (params || keyedParams) && [
...(params ?? []),
...(keyedParams ?? []),
];
return { type: 'function', name, params: pars };

@@ -122,0 +130,0 @@ }

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc