Node.js database migration management for PostgreSQL
Installations
npm install node-pg-migrate
Developer Guide
Typescript
Yes
Module System
CommonJS, ESM
Min. Node Version
>=18.19.0
Node Version
20.18.0
NPM Version
10.8.2
Score
74.3
Supply Chain
99.6
Quality
89.2
Maintenance
100
Vulnerability
100
License
Releases
Contributors
Unable to fetch Contributors
Languages
TypeScript (87.77%)
JavaScript (12.23%)
Developer
Download Statistics
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
GitHub Statistics
1,304 Stars
1,214 Commits
180 Forks
14 Watching
14 Branches
77 Contributors
Package Meta Information
Latest Version
7.8.0
Package Id
node-pg-migrate@7.8.0
Unpacked Size
531.07 kB
Size
95.94 kB
File Count
279
NPM Version
10.8.2
Node Version
20.18.0
Publised On
14 Nov 2024
Total Downloads
Cumulative downloads
Total Downloads
0
Last day
0%
0
Compared to previous day
Last week
0%
0
Compared to previous week
Last month
0%
0
Compared to previous month
Last year
0%
0
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dev Dependencies
34
node-pg-migrate
The core maintainer of this project moved to @Shinigami92 (also core maintainer of FakerJS and core member of Vite).
The project is and remains under the MIT license.
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, then handed over to Salsita Software and now maintained by @Shinigami92.
Preconditions
- Node.js 18 or higher
- PostgreSQL 12.8 or higher (lower versions may work but are not supported officially)
If you don't already have the pg
library installed, you will need to add pg as either a direct or dev dependency
1npm add pg
Installation
1npm add --save-dev node-pg-migrate
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.js
Quick Example
Add "migrate": "node-pg-migrate"
to scripts
section of your 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:
1exports.up = (pgm) => {
2 pgm.createTable('users', {
3 id: 'id',
4 name: { type: 'varchar(1000)', notNull: true },
5 createdAt: {
6 type: 'timestamp',
7 notNull: true,
8 default: pgm.func('current_timestamp'),
9 },
10 });
11 pgm.createTable('posts', {
12 id: 'id',
13 userId: {
14 type: 'integer',
15 notNull: true,
16 references: '"users"',
17 onDelete: 'cascade',
18 },
19 body: { type: 'text', notNull: true },
20 createdAt: {
21 type: 'timestamp',
22 notNull: true,
23 default: pgm.func('current_timestamp'),
24 },
25 });
26 pgm.createIndex('posts', 'userId');
27};
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 :tada:
If you 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
:
1exports.up = (pgm) => {
2 pgm.addColumns('posts', {
3 lead: { type: 'text', notNull: true },
4 });
5};
Run npm run migrate up
and there will be a new column in posts
table :tada:
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"!
Contributing
š Welcome, new contributors!
Whether you're a seasoned developer or just getting started, your contributions are valuable to us. Don't hesitate to jump in, explore the project, and make an impact.
License
No vulnerabilities found.
Reason
22 commit(s) and 3 issue activity found in the last 90 days -- score normalized to 10
Reason
no binaries found in the repo
Reason
no dangerous workflow patterns detected
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
all dependencies are pinned
Details
- Info: 56 out of 56 GitHub-owned GitHubAction dependencies pinned
- Info: 26 out of 26 third-party GitHubAction dependencies pinned
Reason
1 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-mwcw-c2x4-8c55
Reason
Found 6/10 approved changesets -- score normalized to 6
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
detected GitHub workflow tokens with excessive permissions
Details
- Info: jobLevel 'pull-requests' permission set to 'read': .github/workflows/semantic-pull-request.yml:15
- Warn: jobLevel 'statuses' permission set to 'write': .github/workflows/semantic-pull-request.yml:16
- Info: topLevel 'contents' permission set to 'read': .github/workflows/ci.yml:10
- Info: topLevel 'contents' permission set to 'read': .github/workflows/cockroach-test.yml:10
- Warn: no topLevel permission defined: .github/workflows/coverage-report.yml:1
- Info: topLevel 'contents' permission set to 'read': .github/workflows/coverage.yml:8
- Info: topLevel 'contents' permission set to 'read': .github/workflows/docs.yml:14
- Info: topLevel 'contents' permission set to 'read': .github/workflows/postgres-test.yml:10
- Warn: no topLevel permission defined: .github/workflows/publish.yml:1
- Info: found token with 'none' permissions: .github/workflows/semantic-pull-request.yml:1
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 30 are checked with a SAST tool
Score
6
/10
Last Scanned on 2024-12-16
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn MoreOther packages similar to node-pg-migrate
node-pg-migrate-exp
PostgreSQL database migration management tool for node.js
@liveaxle/node-pg-migrate
Synchronous (async/await) migration assistant for PostgreSQL
@osdiab/node-pg-migrate
Postgresql database migration management tool for node.js
grunt-node-pg-migrate
Migrate database schema and data with node-pg-migrate