forked from mohammed-bahumaish/prisma-editor
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
aa96b0a
commit ba2b2ad
Showing
8 changed files
with
400 additions
and
288 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
.env | ||
Dockerfile | ||
.dockerignore | ||
node_modules | ||
**/node_modules | ||
npm-debug.log | ||
README.md | ||
.next | ||
.git |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
FROM --platform=linux/amd64 node:16-alpine3.17 AS base | ||
RUN apk add --no-cache libc6-compat openssl1.1-compat | ||
RUN apk update | ||
RUN npm install -g turbo pnpm | ||
|
||
|
||
FROM base AS builder | ||
WORKDIR /app | ||
COPY . . | ||
RUN turbo prune --scope=@prisma-editor/web --docker | ||
|
||
|
||
FROM base AS installer | ||
WORKDIR /app | ||
COPY .gitignore .gitignore | ||
COPY --from=builder /app/out/json/ . | ||
COPY --from=builder /app/out/pnpm-lock.yaml ./pnpm-lock.yaml | ||
RUN pnpm install | ||
COPY --from=builder /app/out/full/ . | ||
COPY turbo.json turbo.json | ||
RUN pnpm web postinstall | ||
RUN pnpm turbo run build --filter=@prisma-editor/web | ||
CMD [ "pnpm","start" ] | ||
|
||
# TODO: fix Cannot find module 'next/dist/server/next-server' & remove CMD [ "pnpm","start" ] | ||
# FROM --platform=linux/amd64 node:16-alpine3.17 AS runner | ||
# WORKDIR /app | ||
# RUN addgroup --system --gid 1001 nodejs | ||
# RUN adduser --system --uid 1001 nextjs | ||
# USER nextjs | ||
# COPY --from=installer /app/apps/web/next.config.mjs . | ||
# COPY --from=installer /app/apps/web/package.json . | ||
# COPY --from=installer --chown=nextjs:nodejs /app/apps/web/.next/standalone . | ||
# COPY --from=installer --chown=nextjs:nodejs /app/apps/web/.next/static ./apps/web/.next/static | ||
# COPY --from=installer --chown=nextjs:nodejs /app/apps/web/public ./apps/web/public | ||
# CMD node server.js |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,255 +1,28 @@ | ||
// src/util/parser.ts | ||
import { printGeneratorConfig } from "@prisma/engine-core"; | ||
var handlers = (type, kind) => { | ||
return { | ||
default: (value) => { | ||
if (kind === "enum") { | ||
return `@default(${value})`; | ||
} | ||
if (type === "Boolean") { | ||
return `@default(${value})`; | ||
} | ||
if (typeof value === "undefined" || value === null) { | ||
return ""; | ||
} | ||
if (typeof value === "object") { | ||
return `@default(${value.name}(${value.args}))`; | ||
} | ||
if (typeof value === "number") { | ||
return `@default(${value})`; | ||
} | ||
if (typeof value === "string") { | ||
return `@default("${value}")`; | ||
} | ||
throw new Error(`Unsupported field attribute ${value}`); | ||
}, | ||
isId: (value) => value ? "@id" : "", | ||
isUnique: (value) => value ? "@unique" : "", | ||
dbNames: (value) => { | ||
}, | ||
dbName: (value) => value ? `@map("${value}")` : "", | ||
native: (value) => value ? `${value}` : "", | ||
relationToFields: (value) => { | ||
}, | ||
relationOnDelete: (value) => { | ||
}, | ||
hasDefaultValue: (value) => { | ||
}, | ||
relationName: (value) => { | ||
}, | ||
documentation: (value) => { | ||
}, | ||
isReadOnly: (value) => { | ||
}, | ||
isGenerated: (value) => { | ||
}, | ||
isUpdatedAt: (value) => value ? "@updatedAt" : "", | ||
columnName: (value) => value ? `@map("${value}")` : "", | ||
comment: (value) => value ? `//${value}` : "" | ||
}; | ||
}; | ||
function handleAttributes(attributes, kind, type) { | ||
const { relationFromFields, relationToFields, relationName } = attributes; | ||
if (kind === "scalar") { | ||
return `${Object.keys(attributes).map((each) => handlers(type, kind)[each](attributes[each])).join(" ")}`; | ||
} | ||
if (kind === "object" && relationFromFields) { | ||
return relationFromFields.length > 0 ? `@relation(name: "${relationName}", fields: [${relationFromFields}], references: [${relationToFields}])` : `@relation(name: "${relationName}") ${(attributes == null ? void 0 : attributes.comment) ? "//" + attributes.comment : ""}`; | ||
} | ||
if (kind === "enum") | ||
return `${Object.keys(attributes).map((each) => handlers(type, kind)[each](attributes[each])).join(" ")}`; | ||
return ""; | ||
} | ||
function handleFields(fields) { | ||
return fields.map((fields2) => { | ||
const { name, kind, type, isRequired, isList, ...attributes } = fields2; | ||
if (kind === "scalar") { | ||
return ` ${name} ${type}${isList ? "[]" : isRequired ? "" : "?"} ${handleAttributes(attributes, kind, type)}`; | ||
} | ||
if (kind === "object") { | ||
return ` ${name} ${type}${isList ? "[]" : isRequired ? "" : "?"} ${handleAttributes(attributes, kind, type)}`; | ||
} | ||
if (kind === "enum") { | ||
return ` ${name} ${type}${isList ? "[]" : isRequired ? "" : "?"} ${handleAttributes(attributes, kind, type)}`; | ||
} | ||
if (kind === "comment") { | ||
return `//${name}`; | ||
} | ||
throw new Error(`Unsupported field kind "${kind}"`); | ||
}).join("\n"); | ||
} | ||
function handleIdFields(idFields) { | ||
return idFields && idFields.length > 0 ? `@@id([${idFields.join(", ")}])` : ""; | ||
} | ||
function handleUniqueFieds(uniqueFields) { | ||
return uniqueFields.length > 0 ? uniqueFields.map((eachUniqueField) => `@@unique([${eachUniqueField.join(", ")}])`).join("\n") : ""; | ||
} | ||
function handleDbName(dbName) { | ||
return dbName ? `@@map("${dbName}")` : ""; | ||
} | ||
function handleUrl(envValue) { | ||
const value = envValue.fromEnvVar ? `env("${envValue.fromEnvVar}")` : envValue.value; | ||
return `url = ${value}`; | ||
} | ||
function handleProvider(provider) { | ||
return `provider = "${provider}"`; | ||
} | ||
function deserializeModel(model) { | ||
const { name, uniqueFields, dbName, idFields, index } = model; | ||
const fields = model.fields; | ||
const output = ` | ||
model ${name} { | ||
${handleFields(fields)} | ||
${handleUniqueFieds(uniqueFields)} | ||
${handleDbName(dbName)} | ||
${handleIdFields(idFields)} | ||
${index || ""} | ||
}`; | ||
return output; | ||
} | ||
function deserializeDatasource(datasource) { | ||
const { activeProvider: provider, name, url } = datasource; | ||
return ` | ||
datasource ${name} { | ||
${handleProvider(provider)} | ||
${handleUrl(url)} | ||
}`; | ||
} | ||
function deserializeEnum({ name, values, dbName }) { | ||
const outputValues = values.map(({ name: name2, dbName: dbName2 }) => { | ||
let result = name2; | ||
if (name2 !== dbName2 && dbName2) | ||
result += `@map("${dbName2}")`; | ||
return result; | ||
}); | ||
return ` | ||
enum ${name} { | ||
${outputValues.join("\n ")} | ||
${handleDbName(dbName || null)} | ||
}`; | ||
} | ||
async function dmmfModelsdeserializer(models) { | ||
return models.map((model) => deserializeModel(model)).join("\n"); | ||
} | ||
async function datasourcesDeserializer(datasources) { | ||
return datasources.map((datasource) => deserializeDatasource(datasource)).join("\n"); | ||
} | ||
async function generatorsDeserializer(generators) { | ||
return generators.map((generator) => printGeneratorConfig(generator)).join("\n"); | ||
} | ||
async function dmmfEnumsDeserializer(enums) { | ||
return enums.map((each) => deserializeEnum(each)).join("\n"); | ||
} | ||
import{printGeneratorConfig as j}from"@prisma/engine-core";var F=(n,r)=>({default:e=>{if(r==="enum")return`@default(${e})`;if(n==="Boolean")return`@default(${e})`;if(typeof e>"u"||e===null)return"";if(typeof e=="object")return`@default(${e.name}(${e.args}))`;if(typeof e=="number")return`@default(${e})`;if(typeof e=="string")return`@default("${e}")`;throw new Error(`Unsupported field attribute ${e}`)},isId:e=>e?"@id":"",isUnique:e=>e?"@unique":"",dbNames:e=>{},dbName:e=>e?`@map("${e}")`:"",native:e=>e?`${e}`:"",relationToFields:e=>{},relationOnDelete:e=>{},hasDefaultValue:e=>{},relationName:e=>{},documentation:e=>{},isReadOnly:e=>{},isGenerated:e=>{},isUpdatedAt:e=>e?"@updatedAt":"",columnName:e=>e?`@map("${e}")`:"",comment:e=>e?`//${e}`:""});function p(n,r,e){let{relationFromFields:t,relationToFields:i,relationName:o}=n;return r==="scalar"?`${Object.keys(n).map(s=>F(e,r)[s](n[s])).join(" ")}`:r==="object"&&t?t.length>0?`@relation(name: "${o}", fields: [${t}], references: [${i}])`:`@relation(name: "${o}") ${n!=null&&n.comment?"//"+n.comment:""}`:r==="enum"?`${Object.keys(n).map(s=>F(e,r)[s](n[s])).join(" ")}`:""}function w(n){return n.map(r=>{let{name:e,kind:t,type:i,isRequired:o,isList:s,...a}=r;if(t==="scalar")return` ${e} ${i}${s?"[]":o?"":"?"} ${p(a,t,i)}`;if(t==="object")return` ${e} ${i}${s?"[]":o?"":"?"} ${p(a,t,i)}`;if(t==="enum")return` ${e} ${i}${s?"[]":o?"":"?"} ${p(a,t,i)}`;if(t==="comment")return`//${e}`;throw new Error(`Unsupported field kind "${t}"`)}).join(` | ||
`)}function z(n){return n&&n.length>0?`@@id([${n.join(", ")}])`:""}function C(n){return n.length>0?n.map(r=>`@@unique([${r.join(", ")}])`).join(` | ||
`):""}function D(n){return n?`@@map("${n}")`:""}function N(n){return`url = ${n.fromEnvVar?`env("${n.fromEnvVar}")`:n.value}`}function O(n){return`provider = "${n}"`}function U(n){let{name:r,uniqueFields:e,dbName:t,idFields:i,index:o}=n,s=n.fields;return` | ||
model ${r} { | ||
${w(s)} | ||
${C(e)} | ||
${D(t)} | ||
${z(i)} | ||
${o||""} | ||
}`}function q(n){let{activeProvider:r,name:e,url:t}=n;return` | ||
datasource ${e} { | ||
${O(r)} | ||
${N(t)} | ||
}`}function v({name:n,values:r,dbName:e}){let t=r.map(({name:i,dbName:o})=>{let s=i;return i!==o&&o&&(s+=`@map("${o}")`),s});return` | ||
enum ${n} { | ||
${t.join(` | ||
`)} | ||
${D(e||null)} | ||
}`}async function h(n){return n.map(r=>U(r)).join(` | ||
`)}async function M(n){return n.map(r=>q(r)).join(` | ||
`)}async function x(n){return n.map(r=>j(r)).join(` | ||
`)}async function b(n){return n.map(r=>v(r)).join(` | ||
`)}import{formatSchema as A}from"@prisma/internals";var K=async({dmmf:{models:n,enums:r},config:{datasources:e,generators:t}})=>{let i=[await M(e),await x(t),await h(n),await b(r)].filter(o=>o).join(` | ||
// src/dmmfToSchema.ts | ||
import { formatSchema } from "@prisma/internals"; | ||
var dmmfToSchema = async ({ | ||
dmmf: { models, enums }, | ||
config: { datasources, generators } | ||
}) => { | ||
const outputSchema = [ | ||
await datasourcesDeserializer(datasources), | ||
await generatorsDeserializer(generators), | ||
await dmmfModelsdeserializer(models), | ||
await dmmfEnumsDeserializer(enums) | ||
].filter((e) => e).join("\n\n\n"); | ||
return await formatSchema({ schema: outputSchema }); | ||
}; | ||
// src/schemaToDmmf.ts | ||
import { getConfig, getDMMF } from "@prisma/internals"; | ||
import stripAnsi from "strip-ansi"; | ||
var ErrorTypes = /* @__PURE__ */ ((ErrorTypes2) => { | ||
ErrorTypes2[ErrorTypes2["Prisma"] = 0] = "Prisma"; | ||
ErrorTypes2[ErrorTypes2["Other"] = 1] = "Other"; | ||
return ErrorTypes2; | ||
})(ErrorTypes || {}); | ||
var schemaToDmmf = async (schema) => { | ||
try { | ||
const { datamodel } = await getDMMF({ | ||
datamodel: schema | ||
}); | ||
const config = await getConfig({ | ||
datamodel: schema, | ||
ignoreEnvVarErrors: true | ||
}); | ||
const lines = schema.split("\n"); | ||
let model = ""; | ||
lines.forEach((line, index) => { | ||
if (line.includes("model")) | ||
model = line.trim().split(" ")[1]; | ||
if (line.includes("@db")) { | ||
const lineWords = line.trim().split(" "); | ||
const field = lineWords[0]; | ||
const nativeAttribute = lineWords[2]; | ||
const dmmfModel = datamodel.models.find((m) => m.name === model); | ||
const dmmfField = dmmfModel == null ? void 0 : dmmfModel.fields.find((f) => f.name === field); | ||
if (dmmfField) | ||
dmmfField["native"] = nativeAttribute; | ||
} | ||
if (line.includes("//")) { | ||
const dmmfModel = datamodel.models.find((m) => m.name === model); | ||
const lineWords = line.trim().split(" "); | ||
const comment = line.trim().split("//")[1]; | ||
const isCommentLine = lineWords[0].includes("//"); | ||
if (!isCommentLine) { | ||
const field = lineWords[0]; | ||
const dmmfField = dmmfModel == null ? void 0 : dmmfModel.fields.find((f) => f.name === field); | ||
if (dmmfField) | ||
dmmfField["comment"] = comment; | ||
} else { | ||
const lastLine = lines[index - 1]; | ||
const lineWords2 = lastLine.trim().split(" "); | ||
const field = lineWords2[0]; | ||
if (field === "model") { | ||
dmmfModel == null ? void 0 : dmmfModel.fields.unshift({ | ||
kind: "comment", | ||
name: comment | ||
}); | ||
} else { | ||
const dmmfFieldIndex = dmmfModel == null ? void 0 : dmmfModel.fields.findIndex( | ||
(f) => f.name === field | ||
); | ||
if (dmmfFieldIndex) | ||
dmmfModel == null ? void 0 : dmmfModel.fields.splice(dmmfFieldIndex + 1, 0, { | ||
kind: "comment", | ||
name: comment | ||
}); | ||
} | ||
} | ||
} | ||
if (line.includes("@@index")) { | ||
const index2 = line.trim(); | ||
const dmmfModel = datamodel.models.find((m) => m.name === model); | ||
if (dmmfModel) | ||
dmmfModel["index"] = index2; | ||
} | ||
}); | ||
return { datamodel, config }; | ||
} catch (error) { | ||
const message = stripAnsi(error.message); | ||
let errors; | ||
let errType; | ||
if (message.includes("error: ")) { | ||
errors = parseDMMFError(message); | ||
errType = 0 /* Prisma */; | ||
} else { | ||
console.error(error); | ||
errors = [{ reason: message, row: "0" }]; | ||
errType = 1 /* Other */; | ||
} | ||
return { errors, type: errType }; | ||
} | ||
}; | ||
var errRegex = /^(?:Error validating.*?:)?(.+?)\n --> schema\.prisma:(\d+)\n/; | ||
var parseDMMFError = (error) => error.split("error: ").slice(1).map((msg) => msg.match(errRegex).slice(1)).map(([reason, row]) => ({ reason, row })); | ||
|
||
// src/index.ts | ||
export * from "@prisma/generator-helper"; | ||
import {} from "@prisma/internals"; | ||
export { | ||
ErrorTypes, | ||
dmmfToSchema, | ||
schemaToDmmf | ||
}; | ||
`);return await A({schema:i})};import{getConfig as S,getDMMF as T}from"@prisma/internals";import G from"strip-ansi";var I=(e=>(e[e.Prisma=0]="Prisma",e[e.Other=1]="Other",e))(I||{}),Q=async n=>{try{let{datamodel:r}=await T({datamodel:n}),e=await S({datamodel:n,ignoreEnvVarErrors:!0}),t=n.split(` | ||
`),i="";return t.forEach((o,s)=>{if(o.includes("model")&&(i=o.trim().split(" ")[1]),o.includes("@db")){let a=o.trim().split(" "),l=a[0],m=a[2],c=r.models.find(u=>u.name===i),d=c==null?void 0:c.fields.find(u=>u.name===l);d&&(d.native=m)}if(o.includes("//")){let a=r.models.find(d=>d.name===i),l=o.trim().split(" "),m=o.trim().split("//")[1];if(l[0].includes("//")){let f=t[s-1].trim().split(" ")[0];if(f==="model")a==null||a.fields.unshift({kind:"comment",name:m});else{let y=a==null?void 0:a.fields.findIndex(E=>E.name===f);y&&(a==null||a.fields.splice(y+1,0,{kind:"comment",name:m}))}}else{let d=l[0],u=a==null?void 0:a.fields.find(f=>f.name===d);u&&(u.comment=m)}}if(o.includes("@@index")){let a=o.trim(),l=r.models.find(m=>m.name===i);l&&(l.index=a)}}),{datamodel:r,config:e}}catch(r){let e=G(r.message),t,i;return e.includes("error: ")?(t=R(e),i=0):(console.error(r),t=[{reason:e,row:"0"}],i=1),{errors:t,type:i}}},L=/^(?:Error validating.*?:)?(.+?)\n --> schema\.prisma:(\d+)\n/,R=n=>n.split("error: ").slice(1).map(r=>r.match(L).slice(1)).map(([r,e])=>({reason:r,row:e}));export*from"@prisma/generator-helper";import{}from"@prisma/internals";export{I as ErrorTypes,K as dmmfToSchema,Q as schemaToDmmf}; | ||
//# sourceMappingURL=index.mjs.map |
Large diffs are not rendered by default.
Oops, something went wrong.
Oops, something went wrong.