Skip to content

Convert to esm with backwards cjs compat #231

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
cjs
6 changes: 3 additions & 3 deletions lib/backend.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const { errors } = require('./errors.js')
, { entries, errorFields } = require('./types.js')
import { errors } from './errors.js'
import { entries, errorFields } from './types.js'

const char = (acc, [k, v]) => (acc[k.charCodeAt(0)] = v, acc)
, N = '\u0000'

module.exports = Backend
export default Backend

function Backend({
onparse,
Expand Down
2 changes: 1 addition & 1 deletion lib/bytes.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ const b = Object.assign(messages, {
}
})

module.exports = b
export default b

function fit(x) {
if (buffer.length - b.i < x) {
Expand Down
18 changes: 9 additions & 9 deletions lib/connection.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
const net = require('net')
const tls = require('tls')
const frontend = require('./frontend.js')
const Backend = require('./backend.js')
const Queue = require('./queue.js')
const { END, retryRoutines } = require('./types.js')
const { errors } = require('./errors.js')

module.exports = Connection
import net from 'net'
import tls from 'tls'
import frontend from './frontend.js'
import Backend from './backend.js'
import Queue from './queue.js'
import { END, retryRoutines } from './types.js'
import { errors } from './errors.js'

export default Connection

let count = 1

Expand Down
6 changes: 2 additions & 4 deletions lib/errors.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
class PostgresError extends Error {
export class PostgresError extends Error {
constructor(x) {
super(x.message)
this.name = this.constructor.name
Object.assign(this, x)
}
}

module.exports.PostgresError = PostgresError

module.exports.errors = {
export const errors = {
connection,
postgres,
generic,
Expand Down
10 changes: 5 additions & 5 deletions lib/frontend.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const crypto = require('crypto')
const bytes = require('./bytes.js')
const { entries } = require('./types.js')
const { errors } = require('./errors.js')
import crypto from 'crypto'
import bytes from './bytes.js'
import { entries } from './types.js'
import { errors } from './errors.js'

const N = String.fromCharCode(0)
const empty = Buffer.alloc(0)
Expand Down Expand Up @@ -36,7 +36,7 @@ const auths = {
12: SASLFinal
}

module.exports = {
export default {
StartupMessage,
SSLRequest,
auth,
Expand Down
23 changes: 12 additions & 11 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
const fs = require('fs')
const Url = require('url')
const Stream = require('stream')
const Connection = require('./connection.js')
const Queue = require('./queue.js')
const Subscribe = require('./subscribe.js')
const { errors, PostgresError } = require('./errors.js')
const {
import fs from 'fs'
import os from 'os'
import Url from 'url'
import Stream from 'stream'
import Connection from './connection.js'
import Queue from './queue.js'
import Subscribe from './subscribe.js'
import { errors, PostgresError } from './errors.js'
import {
mergeUserTypes,
arraySerializer,
arrayParser,
Expand All @@ -20,7 +21,7 @@ const {
escape,
types,
END
} = require('./types.js')
} from './types.js'

const notPromise = {
P: {},
Expand Down Expand Up @@ -51,7 +52,7 @@ Object.assign(Postgres, {

const originCache = new Map()

module.exports = Postgres
export default Postgres

function Postgres(a, b) {
if (arguments.length && !a)
Expand Down Expand Up @@ -704,7 +705,7 @@ function warn(x) {

function osUsername() {
try {
return require('os').userInfo().username // eslint-disable-line
return os.userInfo().username // eslint-disable-line
} catch (_) {
return
}
Expand Down
2 changes: 1 addition & 1 deletion lib/queue.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = Queue
export default Queue

function Queue() {
let xs = []
Expand Down
2 changes: 1 addition & 1 deletion lib/subscribe.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = function(postgres, a, b) {
export default function Subscribe(postgres, a, b) {
const listeners = new Map()

let connection
Expand Down
40 changes: 19 additions & 21 deletions lib/types.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const char = module.exports.char = (acc, [k, v]) => (acc[k.charCodeAt(0)] = v, acc)
const entries = o => Object.keys(o).map(x => [x, o[x]])
export const char = (acc, [k, v]) => (acc[k.charCodeAt(0)] = v, acc)
export const entries = o => Object.keys(o).map(x => [x, o[x]])

// These were the fastest ways to do it in Node.js v12.11.1 (add tests to revise if this changes)
const types = module.exports.types = {
export const types = {
string: {
to: 25,
from: null, // defaults to string
Expand Down Expand Up @@ -42,14 +42,12 @@ const types = module.exports.types = {

const defaultHandlers = typeHandlers(types)

const serializers = module.exports.serializers = defaultHandlers.serializers
const parsers = module.exports.parsers = defaultHandlers.parsers
export const serializers = defaultHandlers.serializers
export const parsers = defaultHandlers.parsers

module.exports.entries = entries
export const END = {}

module.exports.END = {}

module.exports.mergeUserTypes = function(types) {
export const mergeUserTypes = function(types) {
const user = typeHandlers(types || {})
return {
serializers: Object.assign({}, serializers, user.serializers),
Expand All @@ -65,7 +63,7 @@ function typeHandlers(types) {
}, { parsers: {}, serializers: {} })
}

module.exports.escape = function escape(str) {
export const escape = function escape(str) {
return '"' + str.replace(/"/g, '""').replace(/\./g, '"."') + '"'
}

Expand All @@ -75,7 +73,7 @@ const type = {
boolean: 16
}

module.exports.inferType = function inferType(x) {
export const inferType = function inferType(x) {
return (x && x.type) || (x instanceof Date
? 1184
: Array.isArray(x)
Expand All @@ -94,7 +92,7 @@ function arrayEscape(x) {
.replace(escapeQuote, '\\"')
}

module.exports.arraySerializer = function arraySerializer(xs, serializer) {
export const arraySerializer = function arraySerializer(xs, serializer) {
if (!xs.length)
return '{}'

Expand All @@ -116,7 +114,7 @@ const arrayParserState = {
last: 0
}

module.exports.arrayParser = function arrayParser(x, parser) {
export const arrayParser = function arrayParser(x, parser) {
arrayParserState.i = arrayParserState.last = 0
return arrayParserLoop(arrayParserState, x, parser)
}
Expand Down Expand Up @@ -156,27 +154,27 @@ function arrayParserLoop(s, x, parser) {
return xs
}

module.exports.toCamel = x => {
export const toCamel = x => {
let str = x[0]
for (let i = 1; i < x.length; i++)
str += x[i] === '_' ? x[++i].toUpperCase() : x[i]
return str
}

module.exports.toPascal = x => {
export const toPascal = x => {
let str = x[0].toUpperCase()
for (let i = 1; i < x.length; i++)
str += x[i] === '_' ? x[++i].toUpperCase() : x[i]
return str
}

module.exports.toKebab = x => x.replace(/_/g, '-')
export const toKebab = x => x.replace(/_/g, '-')

module.exports.fromCamel = x => x.replace(/([A-Z])/g, '_$1').toLowerCase()
module.exports.fromPascal = x => (x.slice(0, 1) + x.slice(1).replace(/([A-Z])/g, '_$1')).toLowerCase()
module.exports.fromKebab = x => x.replace(/-/g, '_')
export const fromCamel = x => x.replace(/([A-Z])/g, '_$1').toLowerCase()
export const fromPascal = x => (x.slice(0, 1) + x.slice(1).replace(/([A-Z])/g, '_$1')).toLowerCase()
export const fromKebab = x => x.replace(/-/g, '_')

module.exports.errorFields = entries({
export const errorFields = entries({
S: 'severity_local',
V: 'severity',
C: 'code',
Expand All @@ -197,7 +195,7 @@ module.exports.errorFields = entries({
R: 'routine'
}).reduce(char, {})

module.exports.retryRoutines = {
export const retryRoutines = {
FetchPreparedStatement: true,
RevalidateCachedQuery: true,
transformAssignedExpr: true
Expand Down
16 changes: 12 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,24 @@
"name": "postgres",
"version": "2.0.0-beta.9",
"description": "Fastest full featured PostgreSQL client for Node.js",
"main": "lib/index.js",
"type": "module",
"module": "lib/index.js",
"main": "cjs/index.js",
"exports": {
"import": "./lib/index.js",
"default": "./cjs/index.js"
},
"types": "types/index.d.ts",
"typings": "types/index.d.ts",
"type": "commonjs",
"scripts": {
"test": "node tests/index.js",
"build": "node transpile.cjs",
"test": "pushd tests && node index.js && popd && npm run build && pushd cjs/tests && node index.js",
"lint": "eslint lib && eslint tests",
"prepublishOnly": "npm run lint && npm test"
"prepare": "npm run build",
"prepublishOnly": "npm run lint"
},
"files": [
"/cjs",
"/lib",
"/types"
],
Expand Down
2 changes: 1 addition & 1 deletion tests/bootstrap.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const cp = require('child_process')
import cp from 'child_process'

exec('psql -c "create user postgres_js_test"')
exec('psql -c "alter system set password_encryption=md5"')
Expand Down
32 changes: 16 additions & 16 deletions tests/index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
/* eslint no-console: 0 */

require('./bootstrap.js')
import './bootstrap.js'

const { t, not, ot } = require('./test.js') // eslint-disable-line
const cp = require('child_process')
const path = require('path')
const net = require('net')
const fs = require('fs')
import { t, not, ot } from './test.js' // eslint-disable-line
import cp from 'child_process'
import path from 'path'
import net from 'net'
import fs from 'fs'

/** @type {import('../types')} */
const postgres = require('../lib')
import postgres from '../lib/index.js'
const delay = ms => new Promise(r => setTimeout(r, ms))

const login = {
Expand Down Expand Up @@ -397,13 +397,13 @@ t('Point type array', async() => {
})

t('sql file', async() =>
[1, (await sql.file(path.join(__dirname, 'select.sql')))[0].x]
[1, (await sql.file(path.join('select.sql')))[0].x]
)

t('sql file can stream', async() => {
let result
await sql
.file(path.join(__dirname, 'select.sql'), { cache: false })
.file(path.join('select.sql'), { cache: false })
.stream(({ x }) => result = x)

return [1, result]
Expand All @@ -414,15 +414,15 @@ t('sql file throws', async() =>
)

t('sql file cached', async() => {
await sql.file(path.join(__dirname, 'select.sql'))
await sql.file(path.join('select.sql'))
await delay(20)

return [1, (await sql.file(path.join(__dirname, 'select.sql')))[0].x]
return [1, (await sql.file(path.join('select.sql')))[0].x]
})

t('Parameters in file', async() => {
const result = await sql.file(
path.join(__dirname, 'select-param.sql'),
path.join('select-param.sql'),
['hello']
)
return ['hello', result[0].x]
Expand Down Expand Up @@ -1091,8 +1091,8 @@ t('numeric is returned as string', async() => [
t('Async stack trace', async() => {
const sql = postgres({ ...options, debug: false })
return [
parseInt(new Error().stack.split('\n')[1].split(':')[1]) + 1,
parseInt(await sql`select.sql`.catch(x => x.stack.split('\n').pop().split(':')[1]))
parseInt(new Error().stack.split('\n')[1].match(':([0-9]+):')[1]) + 1,
parseInt(await sql`select.sql`.catch(x => x.stack.split('\n').pop().match(':([0-9]+):')[1]))
]
})

Expand Down Expand Up @@ -1404,7 +1404,7 @@ t('Copy write as first works', async() => {
t('Copy from file works', async() => {
await sql`create table test (x int, y int, z int)`
await new Promise(r => fs
.createReadStream(path.join(__dirname, 'copy.csv'))
.createReadStream(path.join('copy.csv'))
.pipe(sql`copy test from stdin`.writable())
.on('finish', r)
)
Expand Down Expand Up @@ -1432,7 +1432,7 @@ t('Copy from works in transaction', async() => {

t('Copy from abort works', async() => {
const sql = postgres(options)
const readable = fs.createReadStream(path.join(__dirname, 'copy.csv'))
const readable = fs.createReadStream(path.join('copy.csv'))

await sql`create table test (x int, y int, z int)`
await sql`TRUNCATE TABLE test`
Expand Down
Loading