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

prisma-schema-migrator

v0.2.7

Published

Unofficial migrations-handler for prisma

Downloads

15

Readme

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.