Skip to content

Commit 310b8d6

Browse files
committed
Convert to esm
1 parent c841edd commit 310b8d6

File tree

5 files changed

+43
-17
lines changed

5 files changed

+43
-17
lines changed

index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
const fs = require('fs')
2-
const path = require('path')
1+
import fs from 'fs'
2+
import path from 'path'
33

44
const join = path.join
55

6-
module.exports = async function({
6+
export default async function({
77
sql,
88
path = join(process.cwd(), 'migrations'),
99
before = null,
@@ -48,7 +48,7 @@ module.exports = async function({
4848
}) {
4949
fs.existsSync(join(path, 'index.sql')) && !fs.existsSync(join(path, 'index.js'))
5050
? await sql.file(join(path, 'index.sql'))
51-
: await require(join(path, 'index.js'))(sql) // eslint-disable-line
51+
: await import(join(path, 'index.js')).then(x => x.default(sql)) // eslint-disable-line
5252

5353
await sql`
5454
insert into migrations (

package-lock.json

Lines changed: 24 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"version": "0.0.4",
44
"description": "A simple forwards only migration solution for [postgres.js](https://github.com/porsager/postgres)",
55
"main": "index.js",
6-
"type": "commonjs",
6+
"type": "module",
77
"scripts": {
88
"test": "node tests/index.js"
99
},
@@ -27,6 +27,6 @@
2727
"database"
2828
],
2929
"devDependencies": {
30-
"postgres": "1.0.1"
30+
"postgres": "3.3.2"
3131
}
3232
}

tests/index.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,25 @@
1-
const shift = require('../index.js')
2-
const postgres = require('postgres')
3-
const cp = require('child_process')
4-
const path = require('path')
1+
import shift from '../index.js'
2+
import postgres from 'postgres'
3+
import cp from 'child_process'
4+
import { fileURLToPath } from 'url'
55

66
const db = 'postgres_shift_test'
77
cp.execSync('dropdb ' + db + ';createdb ' + db)
88

99
const sql = postgres({
1010
db,
11-
timeout: 1
11+
idle_timeout: 1
1212
})
1313

1414
shift({
1515
sql,
16-
path: path.join(__dirname, 'migrations')
16+
path: fileURLToPath(new URL('migrations', import.meta.url)),
17+
before: ({
18+
migration_id,
19+
name
20+
}) => {
21+
console.log('Migrating', migration_id, name)
22+
}
1723
})
1824
.then(() => console.log('All good'))
1925
.catch(err => {

tests/migrations/00002_update/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module.exports = async(sql) => {
1+
export default async function(sql) {
22
await sql`
33
alter table test add column c timestamp with time zone
44
`

0 commit comments

Comments
 (0)