Skip to content

Latest commit

 

History

History
372 lines (285 loc) · 6.91 KB

EXAMPLES.md

File metadata and controls

372 lines (285 loc) · 6.91 KB

SchemaBuilder

Additional examples

Set a datasource

Since a schema can only have one datasource, calling this command will override the existing datasource if the schema already has one, or create a datasource block if it doesn't.

datasource(provider: string, url: string | { env: string })

You can set a datasource by passing in the provider and url parameters.

builder.datasource('postgresql', { env: 'DATABASE_URL' });
datasource db {
  provider = "postgresql"
  url      = env("DATABASE_URL")
}

Access a datasource programmatically

If you want to perform a custom action that there isn't a Builder method for, you can access the underlying schema object programmatically.

import { Datasource } from '@mrleebo/prisma-ast';

// rename the datasource programmatically
builder
  .datasource('postgresql', { env: 'DATABASE_URL' })
  .then<Datasource>((datasource) => {
    datasource.name = 'DS';
  });
datasource DS {
  provider = "postgresql"
  url      = env("DATABASE_URL")
}

Add or update a generator

generator(name: string, provider: string)

If the schema already has a generator with the given name, it will be updated. Otherwise, a new generator will be created.

builder.generator('nexusPrisma', 'nexus-prisma');
generator nexusPrisma {
  provider = "nexus-prisma"
}

Adding additional assignments to generators

assignment(key: string, value: string)

If your generator accepts additional assignments, they can be added by chaining .assignment() calls to your generator.

builder.generator('client', 'prisma-client-js').assignment('output', 'db.js');
generator client {
  provider = "prisma-client-js"
  output   = "db.js"
}

Access a generator programmatically

If you want to perform a custom action that there isn't a Builder method for, you can access the underlying schema object programmatically.

generator client {
  provider = "prisma-client-js"
  output   = "db.js"
}
import { Generator } from '@mrleebo/prisma-ast';

// rename the generator programmatically
builder.generator('client').then<Generator>((generator) => {
  generator.name = 'foo';
});
generator foo {
  provider = "prisma-client-js"
  output   = "db.js"
}

Add or update a model

If the model with that name already exists in the schema, it will be selected and any fields that follow will be appended to the model. Otherwise, the model will be created and added to the schema.

builder.model('Project').field('name', 'String');
model Project {
  name String
}

Add or update a view

If the view with that name already exists in the schema, it will be selected and any fields that follow will be appended to the view. Otherwise, the view will be created and added to the schema.

builder.view('Project').field('name', 'String');
model Project {
  name String
}

Add or update a composite type

If the composite type with that name already exists in the schema, it will be selected and any fields that follow will be appended to the type. Otherwise, the composite type will be created and added to the schema.

builder.type('Photo').field('width', 'Int').field('height', 'Int').field('url', 'String');
type Photo {
  width   Int
  height  Int
  url     String
}

Access a model programmatically

If you want to perform a custom action that there isn't a Builder method for, you can access the underlying schema object programmatically.

model Project {
  name String
}
import { Model } from '@mrleebo/prisma-ast';

// rename the datasource programmatically
builder.model('Project').then<Model>((model) => {
  model.name = 'Task';
});
model Task {
  name String
}

Add a field with an attribute to a model

If the entered model name already exists, that model will be used as the subject for any field and attribute calls that follow.

model Project {
  name        String
}
builder.model('Project').field('projectCode', 'String').attribute('unique');
model Project {
  name        String
  projectCode String @unique
}

Add an attribute to an existing field

If the field already exists, you can add new attributes to it by making calls to .attribute().

model Project {
  name        String
  projectCode String @unique
}
builder.model('Project').field('name').attribute('unique');
model Project {
  name        String @unique
  projectCode String @unique
}

Remove a field

You can remove an existing field with .removeField().

model Project {
  name        String
  projectCode String @unique
}
builder.model('Project').removeField('projectCode');
model Project {
  name String
}

Remove an attribute from an existing field

You can remove an attribute from a field with .removeAttribute().

model Project {
  name        String
  projectCode String @unique
}
builder.model('Project').field('projectCode').removeAttribute('unique');
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.

model TaskMessage {
  createdAt DateTime? @db.Timestamptz(6)
}
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];
  });
model TaskMessage {
  createdAt DateTime? @default(now())
}

Add an index to a model

model Project {
  name        String
  projectCode String @unique
}
builder.model('Project').blockAttribute('index', ['name']);
model Project {
  name        String
  projectCode String @unique
  @@index([name])
}

Add an enum

builder.enum('Role', ['USER', 'ADMIN']);
enum Role {
  USER
  ADMIN
}

Additional enumerators can also be added to an existing Enum

builder
  .enum('Role')
  .break()
  .comment('New role added for feature #12')
  .enumerator('ORGANIZATION');
enum Role {
  USER
  ADMIN

  // New role added for feature #12
  ORGANIZATION
}

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

builder
  .model('Project')
  .break()
  .comment('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
}