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")
}
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")
}
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"
}
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"
}
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"
}
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
}
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
}
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
}
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
}
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
}
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
}
You can remove an existing field with .removeField()
.
model Project {
name String
projectCode String @unique
}
builder.model('Project').removeField('projectCode');
model Project {
name String
}
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
}
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())
}
model Project {
name String
projectCode String @unique
}
builder.model('Project').blockAttribute('index', ['name']);
model Project {
name String
projectCode String @unique
@@index([name])
}
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
}
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
}