# graphql-migrations

> Create & migrate Databases from GraphQL Schema

Latest version **1.1.2** (published 2021-02-24) · 0 weekly downloads

## Install

```sh
npm install graphql-migrations
pnpm add graphql-migrations
yarn add graphql-migrations
bun add graphql-migrations
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.2 |
| Published | 2021-02-24 |
| First published | 2019-11-13 |
| Weekly downloads | 0 |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >=10.15.3 |
| Dependencies | 12 |
| Unpacked size | 301.1 KB |
| Known vulnerabilities | 0 (+9 in 2 direct dependencies) |
| Install scripts | no |
| GitHub stars | 408 |
| Maintainers | ephelan, wtrocki, aerogear-ci |

## Links

- npm: https://www.npmjs.com/package/graphql-migrations
- Repository: https://github.com/aerogear/graphback
- Homepage: https://github.com/aerogear/graphback/tree/master/packages/graphql-migrations#readme
- Issues: https://github.com/aerogear/graphback/issues
- npm.io page: https://npm.io/package/graphql-migrations

## Dependencies (12)

- [case](https://npm.io/package/case.md) 1.6.3
- [glob](https://npm.io/package/glob.md) 7.1.6
- [pino](https://npm.io/package/pino.md) 6.8.0
- [chalk](https://npm.io/package/chalk.md) 3.0.0
- [lodash](https://npm.io/package/lodash.md) 4.17.20
- [ts-jest](https://npm.io/package/ts-jest.md) 26.4.4
- [lodash-es](https://npm.io/package/lodash-es.md) 4.17.15
- [node-emoji](https://npm.io/package/node-emoji.md) 1.10.0
- [@types/jest](https://npm.io/package/@types/jest.md) 26.0.19
- [@graphback/core](https://npm.io/package/@graphback/core.md) 1.1.2
- [graphql-metadata](https://npm.io/package/graphql-metadata.md) 0.7.6
- [@graphql-inspector/core](https://npm.io/package/@graphql-inspector/core.md) 2.3.0

## Recent versions

- 1.1.2 (latest) — 2021-02-24
- 1.1.0-alpha2 (next) — 2020-10-20
- 0.10.0-dev4 (dev) — 2019-12-11
- 1.1.1 — 2021-02-05
- 1.1.0 — 2020-11-10
- 1.1.0-alpha1 — 2020-10-08
- 1.0.1 — 2020-09-25
- 1.0.0 — 2020-09-25
- 1.0.0-beta7 — 2020-09-24
- 1.0.0-beta6 — 2020-09-23
- 1.0.0-beta5 — 2020-09-22
- 1.0.0-beta4 — 2020-09-21
- 1.0.0-beta3 — 2020-09-21
- 1.0.0-beta2 — 2020-09-18
- 1.0.0-beta1 — 2020-09-15
- … 65 more at https://npm.io/package/graphql-migrations/versions

## README

## Graphback

<p align="center">
  <img width="400" src="https://raw.githubusercontent.com/aerogear/graphback/master/website/static/img/logo.png"/>
  <br/>
  Auto generate database structure, <br/>
  GraphQL Resolvers and Queries from GraphQL types 🚀
</p>

**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.

```ts
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

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

### Skip table or field

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

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

### Rename

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

### Nullable and non-nullable field

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

```gql
type Note {
  id: ID!
  title: String # nullable
}
```

### Default value

```gql
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`.

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

### Foreign key

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

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

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

### Many-to-many

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

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

### Many-to-many on same type

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

### Simple index

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

### Multiple index

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

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

### Named index

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

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

You can specify the index type on PostgreSQL.

```gql
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

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

### Custom name

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

### Custom column type

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

### List

```gql
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.

---
_Source: https://npm.io/package/graphql-migrations · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
