npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

graphql-migrations

v1.1.2

Published

Create & migrate Databases from GraphQL Schema

Downloads

393

Readme

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.