node-pg-migrate
Advanced tools
Comparing version 7.9.1 to 8.0.0-experimental.1328.2
#!/usr/bin/env node | ||
"use strict"; | ||
var __create = Object.create; | ||
var __defProp = Object.defineProperty; | ||
var __getOwnPropDesc = Object.getOwnPropertyDescriptor; | ||
var __getOwnPropNames = Object.getOwnPropertyNames; | ||
var __getProtoOf = Object.getPrototypeOf; | ||
var __hasOwnProp = Object.prototype.hasOwnProperty; | ||
var __copyProps = (to, from, except, desc) => { | ||
if (from && typeof from === "object" || typeof from === "function") { | ||
for (let key of __getOwnPropNames(from)) | ||
if (!__hasOwnProp.call(to, key) && key !== except) | ||
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); | ||
} | ||
return to; | ||
}; | ||
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( | ||
// If the importer is in node compatibility mode or this is not an ESM | ||
// file that has been converted to a CommonJS file using a Babel- | ||
// compatible transform (i.e. "__esModule" has not been set), then set | ||
// "default" to the CommonJS "module.exports" for node compatibility. | ||
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, | ||
mod | ||
)); | ||
var import_node_pg_migrate = __toESM(require("node-pg-migrate")); | ||
var import_node_fs = require("node:fs"); | ||
var import_node_module = require("node:module"); | ||
var import_node_path = require("node:path"); | ||
var import_node_process = require("node:process"); | ||
var import_node_util = require("node:util"); | ||
var import_connection_parameters = __toESM(require("pg/lib/connection-parameters.js")); | ||
var import_yargs = __toESM(require("yargs/yargs")); | ||
import { Migration, default as migrationRunner } from "node-pg-migrate"; | ||
import { readFileSync } from "node:fs"; | ||
import { register } from "node:module"; | ||
import { join, resolve } from "node:path"; | ||
import { cwd } from "node:process"; | ||
import { pathToFileURL } from "node:url"; | ||
import { format } from "node:util"; | ||
import ConnectionParameters from "pg/lib/connection-parameters.js"; | ||
import yargs from "yargs/yargs"; | ||
process.on("uncaughtException", (err) => { | ||
@@ -37,10 +15,11 @@ console.error(err); | ||
}); | ||
const crossRequire = (0, import_node_module.createRequire)((0, import_node_path.resolve)("_")); | ||
function tryRequire(moduleName) { | ||
async function tryImport(moduleName) { | ||
try { | ||
return crossRequire(moduleName); | ||
const module = await import(moduleName); | ||
return module.default || module; | ||
} catch (error) { | ||
console.log(`Error importing module ${moduleName}:`, error); | ||
if ( | ||
// @ts-expect-error: TS doesn't know about code property | ||
error?.code !== "MODULE_NOT_FOUND" | ||
error?.code !== "ERR_MODULE_NOT_FOUND" | ||
) { | ||
@@ -79,3 +58,3 @@ throw error; | ||
const envPathArg = "envPath"; | ||
const parser = (0, import_yargs.default)(process.argv.slice(2)).usage("Usage: $0 [up|down|create|redo] [migrationName] [options]").options({ | ||
const parser = yargs(process.argv.slice(2)).usage("Usage: $0 [up|down|create|redo] [migrationName] [options]").options({ | ||
[databaseUrlVarArg]: { | ||
@@ -235,6 +214,6 @@ alias: "d", | ||
} | ||
const dotenv = tryRequire("dotenv"); | ||
const dotenv = await tryImport("dotenv"); | ||
if (dotenv) { | ||
const myEnv = dotenv.config(dotenvConfig); | ||
const dotenvExpand = tryRequire("dotenv-expand"); | ||
const dotenvExpand = await tryImport("dotenv-expand"); | ||
if (dotenvExpand && dotenvExpand.expand) { | ||
@@ -262,8 +241,8 @@ dotenvExpand.expand(myEnv); | ||
let useTsx = argv[tsxArg]; | ||
function readTsconfig() { | ||
async function readTsconfig() { | ||
if (tsconfigPath) { | ||
let tsconfig; | ||
const json5 = tryRequire("json5"); | ||
const json5 = await tryImport("json5"); | ||
try { | ||
const config2 = (0, import_node_fs.readFileSync)((0, import_node_path.resolve)((0, import_node_process.cwd)(), tsconfigPath), { | ||
const config2 = readFileSync(resolve(cwd(), tsconfigPath), { | ||
encoding: "utf8" | ||
@@ -290,3 +269,3 @@ }); | ||
} else if (useTsNode) { | ||
const tsnode = tryRequire("ts-node"); | ||
const tsnode = await tryImport("ts-node"); | ||
if (!tsnode) { | ||
@@ -298,3 +277,3 @@ console.error( | ||
if (tsconfig && tsnode) { | ||
tsnode.register(tsconfig); | ||
register("ts-node/esm", pathToFileURL("./")); | ||
if (!MIGRATIONS_FILE_LANGUAGE) { | ||
@@ -403,3 +382,3 @@ MIGRATIONS_FILE_LANGUAGE = "ts"; | ||
process.env.SUPPRESS_NO_CONFIG_WARNING = "yes"; | ||
const config = tryRequire("config"); | ||
const config = await tryImport("config"); | ||
if (config && config.has(argv[configValueArg])) { | ||
@@ -412,8 +391,8 @@ const db = config.get(argv[configValueArg]); | ||
if (configFileName) { | ||
const jsonConfig = crossRequire((0, import_node_path.resolve)(configFileName)); | ||
const jsonConfig = await import(resolve(configFileName)); | ||
readJson(jsonConfig); | ||
} | ||
readTsconfig(); | ||
await readTsconfig(); | ||
if (useTsx) { | ||
const tsx = tryRequire("tsx/cjs"); | ||
const tsx = await tryImport("tsx/esm"); | ||
if (!tsx) { | ||
@@ -424,3 +403,3 @@ console.error("For TSX support, please install 'tsx' module"); | ||
const action = argv._.shift(); | ||
MIGRATIONS_DIR ??= (0, import_node_path.join)((0, import_node_process.cwd)(), "migrations"); | ||
MIGRATIONS_DIR ??= join(cwd(), "migrations"); | ||
USE_GLOB ??= false; | ||
@@ -441,3 +420,3 @@ MIGRATIONS_FILE_LANGUAGE ??= "js"; | ||
} | ||
import_node_pg_migrate.Migration.create(newMigrationName, MIGRATIONS_DIR, { | ||
Migration.create(newMigrationName, MIGRATIONS_DIR, { | ||
filenameFormat: MIGRATIONS_FILENAME_FORMAT, | ||
@@ -450,3 +429,3 @@ ...TEMPLATE_FILE_NAME ? { templateFileName: TEMPLATE_FILE_NAME } : { | ||
(migrationPath) => { | ||
console.log((0, import_node_util.format)("Created migration -- %s", migrationPath)); | ||
console.log(format("Created migration -- %s", migrationPath)); | ||
process.exit(0); | ||
@@ -460,3 +439,3 @@ } | ||
if (!DB_CONNECTION) { | ||
const cp = new import_connection_parameters.default(); | ||
const cp = new ConnectionParameters(); | ||
if (!cp.host && !cp.port && !cp.database) { | ||
@@ -533,5 +512,5 @@ console.error( | ||
}; | ||
const promise = action === "redo" ? (0, import_node_pg_migrate.default)(options("down")).then( | ||
() => (0, import_node_pg_migrate.default)(options("up", Number.POSITIVE_INFINITY, false)) | ||
) : (0, import_node_pg_migrate.default)(options(action)); | ||
const promise = action === "redo" ? migrationRunner(options("down")).then( | ||
() => migrationRunner(options("up", Number.POSITIVE_INFINITY, false)) | ||
) : migrationRunner(options(action)); | ||
promise.then(() => { | ||
@@ -538,0 +517,0 @@ console.log("Migrations complete!"); |
@@ -9,5 +9,6 @@ #!/usr/bin/env node | ||
import { readFileSync } from 'node:fs'; | ||
import { createRequire } from 'node:module'; | ||
import { register } from 'node:module'; | ||
import { join, resolve } from 'node:path'; | ||
import { cwd } from 'node:process'; | ||
import { pathToFileURL } from 'node:url'; | ||
import { format } from 'node:util'; | ||
@@ -26,16 +27,18 @@ import type { ClientConfig } from 'pg'; | ||
const crossRequire = createRequire(resolve('_')); | ||
/** | ||
* Try to require a module and return null if it doesn't exist. | ||
* Try to import a module and return null if it doesn't exist. | ||
* | ||
* @param moduleName The name of the module to require. | ||
* @param moduleName The name of the module to import. | ||
*/ | ||
function tryRequire<TModule = unknown>(moduleName: string): TModule | null { | ||
async function tryImport<TModule = unknown>( | ||
moduleName: string | ||
): Promise<TModule | null> { | ||
try { | ||
return crossRequire(moduleName); | ||
const module = await import(moduleName); | ||
return module.default || module; | ||
} catch (error) { | ||
console.log(`Error importing module ${moduleName}:`, error); | ||
if ( | ||
// @ts-expect-error: TS doesn't know about code property | ||
error?.code !== 'MODULE_NOT_FOUND' | ||
error?.code !== 'ERR_MODULE_NOT_FOUND' | ||
) { | ||
@@ -252,3 +255,3 @@ throw error; | ||
const dotenv = tryRequire<typeof import('dotenv')>('dotenv'); | ||
const dotenv = await tryImport<typeof import('dotenv')>('dotenv'); | ||
if (dotenv) { | ||
@@ -258,3 +261,3 @@ // Load config from ".env" file | ||
const dotenvExpand = | ||
tryRequire<typeof import('dotenv-expand')>('dotenv-expand'); | ||
await tryImport<typeof import('dotenv-expand')>('dotenv-expand'); | ||
if (dotenvExpand && dotenvExpand.expand) { | ||
@@ -289,6 +292,6 @@ dotenvExpand.expand(myEnv); | ||
function readTsconfig(): void { | ||
async function readTsconfig(): Promise<void> { | ||
if (tsconfigPath) { | ||
let tsconfig; | ||
const json5 = tryRequire<typeof import('json5')>('json5'); | ||
const json5 = await tryImport<typeof import('json5')>('json5'); | ||
@@ -320,3 +323,3 @@ try { | ||
} else if (useTsNode) { | ||
const tsnode = tryRequire<typeof import('ts-node')>('ts-node'); | ||
const tsnode = await tryImport<typeof import('ts-node')>('ts-node'); | ||
if (!tsnode) { | ||
@@ -329,3 +332,3 @@ console.error( | ||
if (tsconfig && tsnode) { | ||
tsnode.register(tsconfig); | ||
register('ts-node/esm', pathToFileURL('./')); | ||
if (!MIGRATIONS_FILE_LANGUAGE) { | ||
@@ -459,3 +462,3 @@ MIGRATIONS_FILE_LANGUAGE = 'ts'; | ||
process.env.SUPPRESS_NO_CONFIG_WARNING = 'yes'; | ||
const config = tryRequire<typeof import('config')>('config'); | ||
const config = await tryImport<typeof import('config')>('config'); | ||
if (config && config.has(argv[configValueArg])) { | ||
@@ -470,11 +473,11 @@ const db = config.get(argv[configValueArg]); | ||
if (configFileName) { | ||
const jsonConfig = crossRequire(resolve(configFileName)); | ||
const jsonConfig = await import(resolve(configFileName)); | ||
readJson(jsonConfig); | ||
} | ||
readTsconfig(); | ||
await readTsconfig(); | ||
if (useTsx) { | ||
const tsx = | ||
tryRequire<typeof import('tsx/dist/cjs/api/index.cjs')>('tsx/cjs'); | ||
await tryImport<typeof import('tsx/dist/esm/api/index.mjs')>('tsx/esm'); | ||
if (!tsx) { | ||
@@ -481,0 +484,0 @@ console.error("For TSX support, please install 'tsx' module"); |
{ | ||
"name": "node-pg-migrate", | ||
"version": "7.9.1", | ||
"version": "8.0.0-experimental.1328.2", | ||
"description": "PostgreSQL database migration management tool for node.js", | ||
"scripts": { | ||
"clean": "rimraf .eslintcache dist pnpm-lock.yaml node_modules", | ||
"build:bin": "tsup-node --config tsup-bin.config.ts", | ||
"build:clean": "rimraf dist", | ||
"build:code": "tsup-node", | ||
"build:types": "tsc --project tsconfig.build.json", | ||
"build": "run-s build:clean build:code build:types build:bin", | ||
"format": "prettier --cache --write .", | ||
"lint": "eslint --cache --cache-strategy content .", | ||
"ts-check": "tsc", | ||
"test": "vitest", | ||
"test:update-snapshots": "vitest run -u", | ||
"test:coverage": "vitest --coverage", | ||
"docs:dev": "vitepress dev docs", | ||
"docs:build": "vitepress build docs", | ||
"docs:preview": "vitepress preview docs", | ||
"premigrate": "run-s build:bin", | ||
"migrate": "node bin/node-pg-migrate.js", | ||
"prepublishOnly": "pnpm install && pnpm run build", | ||
"preflight": "pnpm install && run-s format build lint test:update-snapshots ts-check" | ||
}, | ||
"keywords": [ | ||
@@ -40,13 +61,5 @@ "db", | ||
], | ||
"type": "commonjs", | ||
"type": "module", | ||
"exports": { | ||
"./dist/*": { | ||
"types": "./dist/*.d.ts", | ||
"require": "./dist/*.js", | ||
"import": "./dist/esm/*.mjs", | ||
"default": "./dist/*.js" | ||
}, | ||
"./bin/*": { | ||
"require": "./bin/*.js", | ||
"import": "./bin/*.mjs", | ||
"default": "./bin/*.js" | ||
@@ -56,4 +69,2 @@ }, | ||
"types": "./dist/index.d.ts", | ||
"require": "./dist/index.js", | ||
"import": "./dist/esm/index.mjs", | ||
"default": "./dist/index.js" | ||
@@ -63,3 +74,3 @@ }, | ||
"types": "./dist/*.d.ts", | ||
"require": "./dist/*.js" | ||
"default": "./dist/*.js" | ||
}, | ||
@@ -69,8 +80,6 @@ "./package.json": "./package.json" | ||
"main": "dist/index.js", | ||
"module": "dist/esm/index.mjs", | ||
"module": "dist/index.js", | ||
"types": "dist/index.d.ts", | ||
"bin": { | ||
"node-pg-migrate": "bin/node-pg-migrate.js", | ||
"node-pg-migrate-cjs": "bin/node-pg-migrate.js", | ||
"node-pg-migrate-esm": "bin/node-pg-migrate.mjs" | ||
"node-pg-migrate": "bin/node-pg-migrate.js" | ||
}, | ||
@@ -92,3 +101,3 @@ "files": [ | ||
"@types/eslint__js": "8.42.3", | ||
"@types/node": "22.13.1", | ||
"@types/node": "20.17.17", | ||
"@types/pg": "8.11.11", | ||
@@ -135,25 +144,6 @@ "@types/yargs": "17.0.33", | ||
}, | ||
"packageManager": "pnpm@10.2.1", | ||
"engines": { | ||
"node": ">=18.19.0" | ||
}, | ||
"scripts": { | ||
"clean": "rimraf .eslintcache dist pnpm-lock.yaml node_modules", | ||
"build:bin": "tsup-node --config tsup-bin.config.ts", | ||
"build:clean": "rimraf dist", | ||
"build:code": "tsup-node", | ||
"build:types": "tsc --project tsconfig.build.json", | ||
"build": "run-s build:clean build:code build:types build:bin", | ||
"format": "prettier --cache --write .", | ||
"lint": "eslint --cache --cache-strategy content .", | ||
"ts-check": "tsc", | ||
"test": "vitest", | ||
"test:update-snapshots": "vitest run -u", | ||
"test:coverage": "vitest --coverage", | ||
"docs:dev": "vitepress dev docs", | ||
"docs:build": "vitepress build docs", | ||
"docs:preview": "vitepress preview docs", | ||
"premigrate": "run-s build:bin", | ||
"migrate": "node bin/node-pg-migrate.js", | ||
"preflight": "pnpm install && run-s format build lint test:update-snapshots ts-check" | ||
"node": ">=20.11.0" | ||
} | ||
} | ||
} |
@@ -18,3 +18,3 @@ # node-pg-migrate | ||
- Node.js 18 or higher | ||
- Node.js 20.11 or higher | ||
- PostgreSQL 12.8 or higher (lower versions may work but are not supported officially) | ||
@@ -44,3 +44,3 @@ | ||
```js | ||
exports.up = (pgm) => { | ||
export const up = (pgm) => { | ||
pgm.createTable('users', { | ||
@@ -86,3 +86,3 @@ id: 'id', | ||
```js | ||
exports.up = (pgm) => { | ||
export const up = (pgm) => { | ||
pgm.addColumns('posts', { | ||
@@ -89,0 +89,0 @@ lead: { type: 'text', notNull: true }, |
/** | ||
* @type {import('node-pg-migrate').ColumnDefinitions | undefined} | ||
*/ | ||
exports.shorthands = undefined; | ||
export const shorthands = undefined; | ||
@@ -11,3 +11,3 @@ /** | ||
*/ | ||
exports.up = (pgm) => {}; | ||
export const up = (pgm) => {}; | ||
@@ -19,2 +19,2 @@ /** | ||
*/ | ||
exports.down = (pgm) => {}; | ||
export const down = (pgm) => {}; |
Sorry, the diff of this file is too big to display
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
5
Yes
255432
143
6238
1
1