Giter Club home page Giter Club logo

prisma-schema-migrations's Introduction

Prisma-migrations

Unofficial migrations-handler for prisma

Prisma is great and supports basic migrations on its own. However, as of writing, it cannot do any logic in these migrations, like converting a users name into two new fields, firstName and lastName etc.

Prisma is working on implementing it, see issue Database migrations.

In the meantime, I created a cli, which creates migrations as steps. This is influenced by other migration-tools like alembic, but only implements basic functionality.

Getting started

npm add --save-dev prisma-schema-migrator
yarn add -D prisma-schema-migrator

Either add a command for prisma-schema-migrator to your package.json

...
"scripts": {
  ...
  "migrate": "prisma-schema-migrator",
  ...
}

or run it on your own with npx prisma-schema-migrator

Now we need to add a table to the schema. This is used to keep track of the current head of the migrations-steps.

type Migration  {
  id: ID! @unique
  createdAt: DateTime!
  migrationName: String! @unique
}

Settings

You can set settings with a .prisma-schema-migrator.json-file in your project.

It takes a few parameters:

schemaDir | string | The directory of the schema. Defaults to ./database prismaEndpoint | string | Endpoint of your prisma-server migrationsDir | string | The directory to generate magrtions to ./<schemaDir>/migrations generateDiff | boolean | string | If you want to generate a diff, set it to true, or as a string for a command. Two args will be appended, the two folders of the diff to generate.

The CLI

The cli has a help-screen, accessable from prisma-schema-migrator --help, which will list all options.

Creating migrations

prisma-schema-migrator create <name>

This will create a migration in the <migrationDir>-directory, with a name being <current-date>_<name>. The name you specify should be short and descriptive of the migration-step. The name of the folder will be used as the migrationName in the Migration-table. You should not rename this folder. The date is needed to sort the migrations, so that each step will be performed in the correct order.

Logic-step (job.js)

Each migration should have a job.js, and this will be created for you from a template. You can supply your own template by putting it in mgirations/template.js.

The function recieves one function containing:

{
  action: 'upBefore' | 'upAfter' | 'downBefore' | 'downAfter',
  client: (input: { query: string, variables?: any } | string) => Promise
}

When migrating forwards, the action will be upX, and backwards downX. Before and after refers to before prisma deploy, is ran this migration-step.

In each of these steps, you should return a truthy-value, unless there was an error. if a falsy return-value is provided, the program will stop.

You may also return an object of arguments:

prismaParams: any arguments you want to pass to `prisma deploy`, like -f (be careful)

Diff

By default, a diff will be generated from the previous migration, if it exists.

From template.js, you can specify where in the file you want it to be generated with {{diff}}. Indentation will be kept at the same level.

prisma-schema-migrations's People

Contributors

dependabot[bot] avatar runar-indico avatar runar-rkmedia avatar

Watchers

 avatar

Forkers

kamilernerd

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.