Skip to content

Commit

Permalink
Modernize codebase
Browse files Browse the repository at this point in the history
  • Loading branch information
apazzolini committed Sep 10, 2020
1 parent 243779b commit 10bb304
Show file tree
Hide file tree
Showing 14 changed files with 3,509 additions and 2,690 deletions.
68 changes: 30 additions & 38 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,40 +1,32 @@
{
'plugins': [
'jest',
],
'extends': [
'airbnb-base',
'plugin:jest/recommended'
],
'rules': {
'arrow-body-style': 0,
'arrow-parens': [2, 'as-needed', { 'requireForBlockBody': false }],
'comma-dangle': [2, 'always-multiline'],
'consistent-return': 0,
'dot-notation': 0,
'function-paren-newline': 0,
'indent': ['error', 4, { 'ignoredNodes': [ 'TemplateLiteral *' ], 'ignoreComments': true }],
'max-len': ['error', 110, 4, { 'ignoreUrls': true }],
'new-cap': 0,
'newline-per-chained-call': 0,
'no-await-in-loop': 0,
'no-confusing-arrow': 0,
'no-console': 0,
'no-continue': 0,
'no-mixed-operators': 0,
'no-multi-assign': 0,
'no-param-reassign': [2, { 'props': false }],
'no-plusplus': 0,
'no-restricted-globals': 0,
'no-underscore-dangle': 0,
'no-unused-expressions': 0,
'no-unused-vars': [2, { 'args': 'none' }],
'object-curly-newline': 0,
'prefer-destructuring': 0,
'semi': [2, 'never', { 'beforeStatementContinuationChars': 'always' }],
},
'env': {
'jest/globals': true,
'node': true,
},
"plugins":[
"jest",
],
"extends": [
"eslint:recommended",
"plugin:import/errors",
"plugin:jest/recommended",
"prettier",
],
"parserOptions": {
"ecmaVersion": 2020,
"sourceType": "module",
},
"rules": {
"no-console": "off",
"strict": ["error", "global"],
"curly": "off",
"no-unused-vars": [2, { "args": "none" }],
"no-use-before-define": "error",
"no-prototype-builtins": "off",
"prefer-const": ["error", { "destructuring": "all", "ignoreReadBeforeAssign": false }],

"import/first": "error",
"import/order": ["error", { "groups": [["builtin", "external", "internal"]] }],
"import/newline-after-import": "error",
},
"env": {
"jest/globals": true,
"node": true,
},
}
9 changes: 9 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"parser": "flow",
"tabWidth": 2,
"useTabs": false,
"semi": false,
"singleQuote": true,
"printWidth": 90,
"trailingComma": "all",
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ const knownEmails = await query(sql`SELECT email FROM users`, {
Invoked exactly like `query`, except that instead of returning an array of rows, it will return one object. If your query results in no rows, it will return a null. If your query returns more than one row, it will throw an Error. You can also use rowMapper here.
```js
const { email } = await query(sql`SELECT email FROM users WHERE id = ${currentUserId}`)
const { email } = await query.one(sql`SELECT email FROM users WHERE id = ${currentUserId}`)
console.log(email) // '[email protected]'
```
Expand Down
72 changes: 42 additions & 30 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,32 +1,44 @@
{
"name": "pgr",
"version": "0.2.0",
"description": "A wrapper for pg",
"main": "src/index.js",
"author": "Andre Azzolini (apazzolini)",
"repository": {
"type": "git",
"url": "git+https://github.com/apazzolini/pgr.git"
},
"license": "MIT",
"dependencies": {
"dedent": "0.7.0",
"lodash": "4.17.10",
"pg": "7.4.3",
"pg-format": "1.0.4"
},
"scripts": {
"check": "npm run lint && npm run test",
"lint": "eslint -c .eslintrc src",
"test": "jest",
"test:watch": "jest --watch"
},
"devDependencies": {
"dotenv": "5.0.1",
"eslint": "4.19.1",
"eslint-config-airbnb-base": "12.1.0",
"eslint-plugin-import": "2.11.0",
"eslint-plugin-jest": "21.15.1",
"jest": "22.4.3"
}
"name": "pgr",
"version": "0.3.0",
"description": "A wrapper for pg",
"main": "src/index.js",
"author": "Andre Azzolini (apazzolini)",
"type": "module",
"repository": {
"type": "git",
"url": "git+https://github.com/apazzolini/pgr.git"
},
"license": "MIT",
"dependencies": {
"dedent": "0.7.0"
},
"peerDependencies": {
"pg": "^8.3.3",
"pg-format": "^1.0.4"
},
"scripts": {
"check": "yarn run lint && yarn run test",
"format": "prettier --write \"src/**/*.js\"",
"lint": "eslint -c .eslintrc src",
"test": "NODE_NO_WARNINGS=1 node --experimental-vm-modules node_modules/jest/bin/jest.js",
"test:watch": "yarn run test --watch"
},
"devDependencies": {
"dotenv": "8.2.0",
"eslint": "^7.7.0",
"eslint-config-prettier": "^6.11.0",
"eslint-plugin-import": "^2.22.0",
"eslint-plugin-jest": "^24.0.0",
"jest": "26.4.2",
"jest-environment-node": "^26.3.0",
"pg": "^8.3.3",
"pg-format": "^1.0.4",
"prettier": "^2.1.1",
"typescript": "^4.0.2"
},
"jest": {
"testEnvironment": "jest-environment-node",
"transform": {}
}
}
16 changes: 5 additions & 11 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
const pg = require('pg')
const { createPool, getPool } = require('./pool.js')
const sql = require('./sql.js')
const query = require('./query.js')
import pg from 'pg'
import { createPool, getPool } from './pool.js'
import sql from './sql.js'
import query from './query.js'

module.exports = {
pg,
sql,
query,
createPool,
getPool,
}
export { pg, sql, query, createPool, getPool }
43 changes: 20 additions & 23 deletions src/pool.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,30 @@
const { Pool } = require('pg')
import PG from 'pg'

const { Pool } = PG

const registeredPools = {}

const createPool = (name, config) => {
const pool = new Pool(config)
export function createPool(name, config) {
const pool = new Pool(config)

pool.on('error', (err, client) => {
console.error(err, 'Unexpected error on idle client')
process.exit(-1)
})
pool.on('error', (err, client) => {
console.error(err, 'Unexpected error on idle client')
process.exit(-1)
})

pool.metrics = {
queries: {},
}
pool.metrics = {
queries: {},
}

registeredPools[name] = pool
return true
registeredPools[name] = pool
return true
}

const getPool = name => {
if (typeof name === 'undefined' && Object.keys(registeredPools).length === 1) {
[name] = Object.keys(registeredPools) // eslint-disable-line no-param-reassign
}

if (!registeredPools[name]) throw Error(`Unknown pool [${name}]`)
return registeredPools[name]
}
export function getPool(name) {
if (typeof name === 'undefined' && Object.keys(registeredPools).length === 1) {
;[name] = Object.keys(registeredPools) // eslint-disable-line no-param-reassign
}

module.exports = {
createPool,
getPool,
if (!registeredPools[name]) throw Error(`Unknown pool [${name}]`)
return registeredPools[name]
}
38 changes: 19 additions & 19 deletions src/pool.test.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
const { createPool, getPool } = require('./index.js')
import { createPool, getPool } from './index.js'

describe('pool', () => {
test('creates a pool', () => {
expect(createPool('pool1', { hostname: 'one' })).toBe(true)
const pool = getPool('pool1')
expect(pool).toBeDefined()
expect(pool.options.hostname).toBe('one')
})
test('creates a pool', () => {
expect(createPool('pool1', { hostname: 'one' })).toBe(true)
const pool = getPool('pool1')
expect(pool).toBeDefined()
expect(pool.options.hostname).toBe('one')
})

test('retrieves the default pool with no name', () => {
expect(createPool('pool1', { hostname: 'one' })).toBe(true)
expect(getPool()).toBe(getPool('pool1'))
})
test('retrieves the default pool with no name', () => {
expect(createPool('pool1', { hostname: 'one' })).toBe(true)
expect(getPool()).toBe(getPool('pool1'))
})

test('creates a secondary pool', () => {
expect(createPool('pool2', { hostname: 'two' })).toBe(true)
expect(getPool('pool1').options.hostname).toBe('one')
expect(getPool('pool2').options.hostname).toBe('two')
})
test('creates a secondary pool', () => {
expect(createPool('pool2', { hostname: 'two' })).toBe(true)
expect(getPool('pool1').options.hostname).toBe('one')
expect(getPool('pool2').options.hostname).toBe('two')
})

test('cannot retrieve a pool without a name if more than one exists', () => {
expect(getPool).toThrow()
})
test('cannot retrieve a pool without a name if more than one exists', () => {
expect(getPool).toThrow()
})
})
14 changes: 7 additions & 7 deletions src/query-id.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
const crypto = require('crypto')
import crypto from 'crypto'

const sha1 = data => {
const generator = crypto.createHash('sha1')
generator.update(data)
return generator.digest('hex')
function sha1(data) {
const generator = crypto.createHash('sha1')
generator.update(data)
return generator.digest('hex')
}

module.exports = q => {
return sha1(q.getBaseStatement()).substring(0, 6)
export default function queryId(q) {
return sha1(q.getBaseStatement()).substring(0, 6)
}
Loading

0 comments on commit 10bb304

Please sign in to comment.