0.2.7 • Published 2 years ago

@gogoout/prisma-ast v0.2.7

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

@mrleebo/prisma-ast

This library uses an abstract syntax tree to parse schema.prisma files into an object in JavaScript.

It is similar to @prisma/sdk except that it preserves comments and model attributes. It also doesn't attempt to validate the correctness of the schema at all; the focus is instead on the ability to parse the schema into an object, manipulate it using JavaScript, and re-print the schema back to a file without losing information that isn't captured by other parsers.

It is probable that a future version of @prisma/sdk will render this library obsolete.

Install

npm install @mrleebo/prisma-ast

Examples

Parse a schema.prisma file into a JS object

import { getSchema } from '@mrleebo/prisma-ast'

const source = `
model User {
  id   Int    @id @default(autoincrement())
  name String @unique
}
`

const schema = getSchema(source)

Print a schema back out as a string

import { printSchema } from '@mrleebo/prisma-ast'

const source = printSchema(schema)

Add a datasource

schema.list.push({
  type: "datasource",
  name: "db",
  assignments: [
    {type: "assignment", key: "provider", value: '"postgresql"'},
    {
      type: "assignment",
      key: "url",
      value: {type: "function", name: "env", params: ['"DATABASE_URL"']},
    },
  ],
})
datasource db {
  provider = "postgresql"
  url      = env("DATABASE_URL")
}

Add a generator

schema.list.push({
  type: "generator",
  name: "nexusPrisma",
  assignments: [{type: "assignment", key: "provider", value: '"nexus-prisma"'}],
})
generator nexusPrisma {
  provider = "nexus-prisma"
}

Add a model

const model = schema.list.push({
  type: "model",
  name: "Project",
  properties: [
    { type: "field", name: "name", fieldType: "String" }
  ]
})
model Project {
  name String
}

Add a field to a model

const field = model.properties.push({
  type: "field",
  name: "projectCode",
  fieldType: "String",
  optional: false,
  attributes: [{type: "attribute", kind: "field", name: "unique"}],
})
model Project {
  name        String
  projectCode String @unique
}

Add an index to a model

model.properties.push({
  type: "attribute",
  kind: "model",
  name: "index",
  args: [{ type: "attributeArgument", value: { type: "array", args: ["name"] } }]
})
model Project {
  name        String
  projectCode String @unique
  @@index([name])
}

Add an enum

schema.list.push({
  type: "enum",
  name: "Role",
  enumerators: [
    {type: "enumerator", name: "USER"},
    {type: "enumerator", name: "ADMIN"},
  ],
})
enum Role {
  USER
  ADMIN
}

Comments and Line breaks are also parsed and can be added to the schema

model.properties.push({
  type: "break"
}, {
  type: "comment",
  text: "// I wish I could add a color to your rainbow"
})
model Project {
  name        String
  projectCode String @unique
  @@index([name])

  // I wish I could add a color to your rainbow
}