Prisma1: [RFC] Datamodel v1.1 - Database names and types

Created on 1 Nov 2018  路  1Comment  路  Source: prisma/prisma1

This part of the spec describes directives that are used to control details like names and concrete types in the underlying database. See the issue #3408 to learn about the other parts of the spec.

The db directive

  • valid locations: type, field
  • behaviour: This directive is used to control details in the underlying database schema.

On a type

  • optional: yes
  • arguments:

    • name: String



      • Specifies the name of the underlying database table or collection.


      • optional: yes. defaults to the name of the type.



Examples

A type omitting the directive and therefore using the defaults.

type Blog {
  id: ID! @id
  name: String!
}

A type changing the name of the underlying database table or collection.

type Blog @db(name: "blog") {
  id: ID! @id
  name: String!
}

On a field

  • optional: yes
  • arguments:

    • name: String



      • Specifies the name of the underlying database column or field.


      • optional: yes. defaults to the name of the field.



    • type: String (Future. won't implement now)



      • Specifies the underlying database type for a field in schemaful databases, e.g. column type in SQL.


      • optional: yes. defaults to a value from our existing default mapping, e.g. String maps to text in Postgres



Examples

A field omitting the directive and therefore using the defaults.

type Blog {
  id: ID! @id
  name: String!
}

A field using the directive to change the name of the underlying database field or column.

type Blog {
  id: ID! @id
  name: String! @db(name: "title_column")
}

A field using the directive to change the type of the underlying column in the database to use varchar(20) instead of the default text for the String type. (This example assumes Postgres)

type Blog {
  id: ID! @id
  name: String! @db(type: "varchar(20)")
}
aremigrations aredatamodel rf2-accepted arenext

Most helpful comment

I would really like two options to set a default naming strategy for columns and table names. For example:

namingStrategy: "default"|"snake_case" 
tableNamingStrategy: "singular"|"plural"
type Blog { => Maps to "blogs" with tableNamingStrategy "plural"
  id: ID! @id
  publishedAt: DateTime!  => Maps to "published_at" using  namingStrategy "snake_case"
}

In a model I use camelCase properties, but in the database (postgres) I always use snakeCase columns and plural table names.
Allow per field naming using the annotations.

>All comments

I would really like two options to set a default naming strategy for columns and table names. For example:

namingStrategy: "default"|"snake_case" 
tableNamingStrategy: "singular"|"plural"
type Blog { => Maps to "blogs" with tableNamingStrategy "plural"
  id: ID! @id
  publishedAt: DateTime!  => Maps to "published_at" using  namingStrategy "snake_case"
}

In a model I use camelCase properties, but in the database (postgres) I always use snakeCase columns and plural table names.
Allow per field naming using the annotations.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

AlessandroAnnini picture AlessandroAnnini  路  3Comments

sedubois picture sedubois  路  3Comments

marktani picture marktani  路  3Comments

nikolasburk picture nikolasburk  路  3Comments

ragnorc picture ragnorc  路  3Comments