1.1.2 • Published 3 years ago

graphql-migrations v1.1.2

Weekly downloads
429
License
-
Repository
github
Last release
3 years ago

Graphback

Documentation: https://graphback.dev Repository: https://github.com/aerogear/graphback/

graphql-migrations

Automatically create and update your database tables from a GraphQL schema.

Usage

The migrateDB method creates and updates your tables and columns to match your GraphQL schema.

All the database operations are wrapped in a single transaction, so your database will be fully rolled back to its initial state if an error occurs.

import { migrateDB } from 'graphql-migrations';

const dbConfig = {
  client: 'pg',
  connection: {
    host: 'localhost',
    user: 'your-user',
    password: 'secret-password',
    database: 'note-db',
  },
};

const schema = `
type Note {
  id: ID!
  title: String!
  description: String
  comments: [Comment]!
}

type Comment {
  id: ID!
  description: String
  note: Note!
}
`;

migrateDB(dbConfig, schema, {
  // Additional options
}).then(() => {
  console.log('Database updated');
});

Migration Options

  • config: database configuration options.
  • schema: a GraphQL schema object.
  • options:
    • dbSchemaName (default: 'public'): table schema: <schemaName>.<tableName>.
    • dbTablePrefix (default: ''): table name prefix: <prefix><tableName>.
    • dbColumnPrefix (default: ''): column name prefix: <prefix><columnName>.
    • updateComments: overwrite comments on table and columns.
    • scalarMap (default: null): Custom scalar mapping..
    • mapListToJson (default: true): Map scalar lists to JSON column type by default.
    • plugins (default: []): List of graphql-migrations plugins.
    • debug (default: false): display debugging information and SQL queries.

Model Definition

"""
Notes table
"""
type Note {
  """
  Primary key
  """
  id: ID!
  """
  The note title
  """
  title: String!
}

Skip table or field

"""
@db.skip
"""
type Error {
  code: Int!
  message: String!
}

type Note {
  id: ID!
  title: String
  """
  @db.skip: true
  """
  computedField: Boolean
}

Rename

@db.oldNames: ['task']
type Note {
  id: ID!
  """
  @db.oldNames: ['text']
  """
  title: String!
}

Nullable and non-nullable field

type Note {
  id: ID!
  title: String! # not null
}
type Note {
  id: ID!
  title: String # nullable
}

Default value

type Note {
  id: ID!
  """
  default(value: 'Note title')
  """
  title: String
}

Primary key

Each type must have a primary key. The primary key field must be id and the type must be ID.

type Note {
  id: ID!
  title: String!
}

Foreign key

To set a foreign key, set a field reference to the related type.

type Comment {
  id: ID!
  note: Note! # this creates a `noteId` column in the `comment` table.
}

type Note {
  id: ID!
  title: String!
}

Many-to-many

type User {
  id: ID!
  """
  @db.manyToMany: 'users'
  """
  messages: [Message]
}

type Message {
  id: ID!
  """
  @db.manyToMany: 'messages'
  """
  users: [User]
}

Many-to-many on same type

type User {
  id: ID!
  friends: [User]
}

Simple index

type User {
  id: ID!
  """
  @db.index
  """
  email: String!
}

Multiple index

type User {
  """
  @db.index
  """
  id: String!

  """
  @db.index
  """
  email: String!
}

Named index

type User {
  """
  @db.index: 'myIndex'
  """
  email: String!

  """
  @db.index: 'myIndex'
  """
  name: String!
}

You can specify the index type on PostgreSQL.

type User {
  """
  @db.index: { name: 'myIndex', type: 'hash' }
  """
  email: String!

  """
  You don't need to specify the type again.
  @db.index: 'myIndex'
  """
  name: String!
}

Unique constraint

type User {
  id: ID!
  """
  @db.unique
  """
  email: String!
}

Custom name

"""
@db.name: 'people'
"""
type Note {
  id: ID!
  """
  @db.name: 'noteTitle'
  """
  title: String!
}

Custom column type

type Note {
  id: ID!
  """
  @db.type: 'string'
  @db.length: 100
  """
  title: String!
}

List

type Note {
  id: ID!
  title: String!
  """
  @db.types: 'json'
  """
  comments: [String]
}

You can also set mapListToJson to true in the migrate options to automatically map scalar and enum lists to JSON.

1.1.2

3 years ago

1.1.1

3 years ago

1.1.0

3 years ago

1.1.0-alpha2

4 years ago

1.1.0-alpha1

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago

1.0.0-beta7

4 years ago

1.0.0-beta6

4 years ago

1.0.0-beta5

4 years ago

1.0.0-beta3

4 years ago

1.0.0-beta4

4 years ago

1.0.0-beta2

4 years ago

1.0.0-beta1

4 years ago

0.16.2

4 years ago

0.16.1

4 years ago

0.16.0

4 years ago

0.16.0-beta4

4 years ago

0.16.0-beta3

4 years ago

0.16.0-beta2

4 years ago

0.16.0-beta1

4 years ago

0.16.0-alpha7

4 years ago

0.16.0-alpha6

4 years ago

0.16.0-alpha5

4 years ago

0.16.0-alpha4

4 years ago

0.16.0-alpha3

4 years ago

0.16.0-alpha2

4 years ago

0.16.0-alpha1

4 years ago

0.15.1

4 years ago

0.15.0

4 years ago

0.15.0-alpha1

4 years ago

0.14.1

4 years ago

0.14.0

4 years ago

0.14.0-beta6

4 years ago

0.14.0-beta5

4 years ago

0.14.0-beta4

4 years ago

0.14.0-beta1

4 years ago

0.14.0-beta3

4 years ago

0.14.0-alpha14

4 years ago

0.14.0-alpha13

4 years ago

0.14.0-alpha12

4 years ago

0.14.0-alpha11

4 years ago

0.14.0-alpha10

4 years ago

0.14.0-alpha9

4 years ago

0.14.0-alpha8

4 years ago

0.14.0-alpha6

4 years ago

0.14.0-alpha7

4 years ago

0.14.0-alpha5

4 years ago

0.14.0-alpha4

4 years ago

0.14.0-alpha3

4 years ago

0.12.1

4 years ago

0.14.0-alpha2

4 years ago

0.14.0-alpha1

4 years ago

0.14.0-dev3

4 years ago

0.14.0-dev2

4 years ago

0.14.0-dev1

4 years ago

0.12.0

4 years ago

0.11.4

4 years ago

0.11.3

4 years ago

0.11.2

4 years ago

0.11.1

4 years ago

0.11.0

4 years ago

0.11.0-rc6

4 years ago

0.11.0-rc5

4 years ago

0.11.0-rc4

4 years ago

0.11.0-rc2

4 years ago

0.11.0-rc3

4 years ago

0.11.0-dev2

4 years ago

0.11.0-dev3

4 years ago

0.11.0-rc1

4 years ago

0.11.0-alpha1

4 years ago

0.10.2

4 years ago

0.10.1

4 years ago

0.10.0

4 years ago

0.10.0-rc3

4 years ago

0.10.0-dev4

4 years ago

0.10.0-rc2

4 years ago

0.10.0-dev2

4 years ago

0.1.0-dev2

4 years ago

0.0.1

4 years ago