Coder Social home page Coder Social logo

miazzy / node-pg-migrate Goto Github PK

View Code? Open in Web Editor NEW

This project forked from salsita/node-pg-migrate

1.0 2.0 0.0 3.24 MB

Node.js database migration management for Postgresql

Home Page: https://salsita.github.io/node-pg-migrate

License: MIT License

JavaScript 18.90% TypeScript 81.10%

node-pg-migrate's Introduction

node-pg-migrate

Dependency Status devDependency Status NPM version Downloads Licence Known Vulnerabilities Renovate enabled CircleCI

Node.js database migration management built exclusively for postgres. (But can also be used for other DBs conforming to SQL standard - e.g. CockroachDB.) Started by Theo Ephraim, now maintained by Salsita Software.

Looking for v3 docs?

see v3 branch.

Installation

$ npm install node-pg-migrate pg

Installing this module adds a runnable file into your node_modules/.bin directory. If installed globally (with the -g option), you can run node-pg-migrate and if not, you can run ./node_modules/.bin/node-pg-migrate

It will also install pg library as it is peer dependency used for migrations.

Quick Example

Add "migrate": "node-pg-migrate" to scripts section of package.json so you are able to quickly run commands.

Run npm run migrate create my first migration. It will create file xxx_my-first-migration.js in migrations folder. Open it and change contents to:

exports.up = (pgm) => {
  pgm.createTable('users', {
    id: 'id',
    name: { type: 'varchar(1000)', notNull: true },
    createdAt: {
      type: 'timestamp',
      notNull: true,
      default: pgm.func('current_timestamp'),
    },
  })
  pgm.createTable('posts', {
    id: 'id',
    userId: {
      type: 'integer',
      notNull: true,
      references: '"users"',
      onDelete: 'cascade',
    },
    body: { type: 'text', notNull: true },
    createdAt: {
      type: 'timestamp',
      notNull: true,
      default: pgm.func('current_timestamp'),
    },
  })
  pgm.createIndex('posts', 'userId')
}

Save migration file.

Now you should put your DB connection string to DATABASE_URL environment variable and run npm run migrate up. (e.g. DATABASE_URL=postgres://test:test@localhost:5432/test npm run migrate up)

You should now have two tables in your DB ๐ŸŽ‰

If you will want to change your schema later, you can e.g. add lead paragraph to posts:

Run npm run migrate create posts lead, edit xxx_posts_lead.js:

exports.up = (pgm) => {
  pgm.addColumns('posts', {
    lead: { type: 'text', notNull: true },
  })
}

Run npm run migrate up and there will be new column in posts table ๐ŸŽ‰ ๐ŸŽ‰

Want to know more? Read docs:

Docs

Full docs are available at https://salsita.github.io/node-pg-migrate

Explanation & Goals

Why only Postgres? - By writing this migration tool specifically for postgres instead of accommodating many databases, we can actually provide a full featured tool that is much simpler to use and maintain. I was tired of using crippled database tools just in case one day we switch our database.

Async / Sync - Everything is async in node, and that's great, but a migration tool should really just be a fancy wrapper that generates SQL. Most other migration tools force you to bring in control flow libraries or wrap everything in callbacks as soon as you want to do more than a single operation in a migration. Plus by building up a stack of operations, we can automatically infer down migrations (sometimes) to save even more time.

Naming / Raw Sql - Many tools force you to use their constants to do things like specify data types. Again, this tool should be a fancy wrapper that generates SQL, so whenever possible, it should just pass through user values directly to the SQL. The hard part is remembering the syntax of the specific operation, not remembering how to type "timestamp"!

License

The MIT License (MIT)

Copyright (c) 2016-2020 Salsita Software <[email protected]>

Copyright (c) 2014-2016 Theo Ephraim

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

node-pg-migrate's People

Contributors

dolezel avatar renovate-bot avatar theoephraim avatar renovate[bot] avatar greenkeeper[bot] avatar charsleysa avatar causal-agent avatar aschrab avatar bradleyayers avatar ef4 avatar ryands17 avatar shinigami92 avatar cretezy avatar raddevon avatar dylanyang0523 avatar indrek-rajamets avatar goce-cz avatar flux627 avatar potibas avatar ab-pm avatar arthurfranca avatar dependabot[bot] avatar vonforum avatar 0xflotus avatar atistler avatar alastaircoote avatar alem0lars avatar alexcouret avatar aliksend avatar bobthekingofegypt avatar

Stargazers

่ตตๆข“ๅฎ‡ avatar

Watchers

James Cloos avatar  avatar

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.