Skip to content

Commit aa6bea7

Browse files
committed
feat: refactor sql`` template tag from pg-async
1 parent b450eca commit aa6bea7

27 files changed

+823
-234
lines changed

CHANGELOG.md

Whitespace-only changes.

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
> Write SQL queries for 🐘 Postgres in safe way
44
5-
[![Build Status](https://travis-ci.com/the-gear/sql-fragment.svg?branch=master)](https://travis-ci.org/the-gear/sql-fragment)
5+
[![Build Status](https://travis-ci.com/the-gear/sql-fragment.svg?branch=master)](https://travis-ci.com/the-gear/sql-fragment)
66
[![NPM version](https://img.shields.io/npm/v/sql-fragment.svg)](https://www.npmjs.com/package/sql-fragment)
77
![Downloads](https://img.shields.io/npm/dm/sql-fragment.svg)
88
[![Standard Version](https://img.shields.io/badge/release-standard%20version-brightgreen.svg)](https://github.com/conventional-changelog/standard-version)

jest.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const config = {
55
preset: 'ts-jest',
66
rootDir: '.',
77
testMatch: [
8-
'<rootDir>/src/**/__tests__/**/*.ts?(x)',
8+
'<rootDir>/src/**/__tests__/**/*.(spec|test).ts?(x)',
99
'<rootDir>/src/**/?(*.)+(spec|test).ts?(x)',
1010
],
1111
modulePathIgnorePatterns: ['dist'],

package.json

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@
44
"description": "Write SQL queries for 🐘 Postgres in safe way",
55
"keywords": [
66
"SQL",
7+
"sql tag",
78
"sql template string",
9+
"sql tagged template string",
810
"sql query",
911
"postgres",
1012
"pg"
1113
],
12-
"main": "./bundles/index.umd.js",
14+
"main": "./lib/index.js",
1315
"module": "./esm5/index.js",
1416
"es2015": "./esm2015/index.js",
1517
"typings": "./types/index.d.ts",
@@ -38,24 +40,22 @@
3840
"husky": {
3941
"hooks": {
4042
"commit-msg": "commitlint --config config/commitlint.config.js -E HUSKY_GIT_PARAMS",
41-
"pre-commit": "lint-staged"
43+
"pre-commit": "yarn run lint:fix && lint-staged"
4244
}
4345
},
4446
"lint-staged": {
4547
"**/*.{ts,tsx,js,jsx,css,scss,sass,less,md}": [
4648
"prettier --write",
4749
"git add"
48-
],
49-
"src/**/*.{ts,tsx}": [
50-
"yarn run lint:fix",
51-
"git add"
5250
]
5351
},
5452
"devDependencies": {
5553
"@commitlint/cli": "^7.3.2",
5654
"@commitlint/config-conventional": "^7.3.1",
55+
"@types/invariant": "^2.2.29",
5756
"@types/jest": "^23.3.12",
5857
"@types/node": "^8.10.*",
58+
"@types/pg": "^7.4.11",
5959
"@types/prettier": "^1.15.2",
6060
"@types/webpack-config-utils": "^2.3.0",
6161
"brotli-size": "^0.0.3",
@@ -68,6 +68,7 @@
6868
"jest-watch-typeahead": "^0.2.0",
6969
"kleur": "^3.0.1",
7070
"lint-staged": "^8.1.0",
71+
"pg": "^7.8.0",
7172
"prettier": "^1.15.3",
7273
"pretty-bytes": "^5.1.0",
7374
"rollup": "^1.1.0",
@@ -92,13 +93,16 @@
9293
"webpack-config-utils": "^2.3.1"
9394
},
9495
"peerDependencies": {
96+
"pg": "^7.8.0",
9597
"tslib": "^1.9.0"
9698
},
97-
"dependencies": {},
99+
"dependencies": {
100+
"invariant": "^2.2.4"
101+
},
98102
"scripts": {
99103
"cleanup": "shx rm -rf dist",
100104
"prebuild": "yarn run cleanup && yarn run verify",
101-
"build": "tsc && tsc --target es2018 --outDir dist/esm2015 && rollup -c && rollup -c --environment NODE_ENV:production",
105+
"build": "tsc && tsc --module esnext --target es5 --outDir dist/esm5 && tsc --target es2018 --outDir dist/esm2015 && rollup -c && rollup -c --environment NODE_ENV:production",
102106
"postbuild": "node scripts/copy.js && yarn run size",
103107
"docs": "typedoc -p . --theme minimal --target 'es6' --excludeNotExported --excludePrivate --ignoreCompilerErrors --exclude \"**/src/**/__tests__/*.*\" --out docs src/",
104108
"test": "jest",
@@ -125,4 +129,4 @@
125129
"size:umd": "node scripts/file-size.js ./dist/bundles/index.umd.min.js",
126130
"size:fesm": "node scripts/file-size.js ./dist/bundles/index.esm.min.js"
127131
}
128-
}
132+
}

rollup.config.js

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import nodeResolve from 'rollup-plugin-node-resolve';
44
import json from 'rollup-plugin-json';
55
import commonjs from 'rollup-plugin-commonjs';
66
import replace from 'rollup-plugin-replace';
7-
import { uglify } from 'rollup-plugin-uglify';
87
import { terser } from 'rollup-plugin-terser';
98
import { getIfUtils, removeEmpty } from 'webpack-config-utils';
109

@@ -40,23 +39,23 @@ const PATHS = {
4039
/**
4140
* @type {string[]}
4241
*/
43-
const external = Object.keys(pkg.peerDependencies) || [];
42+
const external = [...Object.keys(pkg.peerDependencies), 'pg', 'pg-types'];
4443

4544
/**
4645
* @type {Plugin[]}
4746
*/
4847
const plugins = /** @type {Plugin[]} */ ([
48+
// Allow node_modules resolution, so you can use 'external' to control
49+
// which external modules to include in the bundle
50+
// https://github.com/rollup/rollup-plugin-node-resolve#usage
51+
nodeResolve(),
52+
4953
// Allow json resolution
5054
json(),
5155

5256
// Allow bundling cjs modules (unlike webpack, rollup doesn't understand cjs)
5357
commonjs(),
5458

55-
// Allow node_modules resolution, so you can use 'external' to control
56-
// which external modules to include in the bundle
57-
// https://github.com/rollup/rollup-plugin-node-resolve#usage
58-
nodeResolve(),
59-
6059
// Resolve source maps to the original source
6160
sourceMaps(),
6261

@@ -72,7 +71,9 @@ const plugins = /** @type {Plugin[]} */ ([
7271
*/
7372
const CommonConfig = {
7473
input: {},
75-
output: {},
74+
output: {
75+
exports: 'named',
76+
},
7677
inlineDynamicImports: true,
7778
// Indicate here external modules you don't wanna include in your bundle (i.e.: 'lodash')
7879
external,
@@ -85,12 +86,13 @@ const UMDconfig = {
8586
...CommonConfig,
8687
input: resolve(PATHS.entry.esm5, 'index.js'),
8788
output: {
89+
...CommonConfig.output,
8890
file: getOutputFileName(resolve(PATHS.bundles, 'index.umd.js'), ifProduction()),
8991
format: 'umd',
9092
name: LIB_NAME,
9193
sourcemap: true,
9294
},
93-
plugins: removeEmpty(/** @type {Plugin[]} */ ([...plugins, ifProduction(uglify())])),
95+
plugins: removeEmpty(/** @type {Plugin[]} */ ([...plugins, ifProduction(terser())])),
9496
};
9597

9698
/**
@@ -101,6 +103,7 @@ const FESMconfig = {
101103
input: resolve(PATHS.entry.esm2015, 'index.js'),
102104
output: [
103105
{
106+
...CommonConfig.output,
104107
file: getOutputFileName(resolve(PATHS.bundles, 'index.esm.js'), ifProduction()),
105108
format: 'es',
106109
sourcemap: true,

scripts/build.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ function getDtsHeader(pkgName, version, author, repoUrl, tsVersion) {
5252
/**
5353
*
5454
* @param {string} path
55-
* @param {string | Blob} data
55+
* @param {string | Buffer} data
5656
*/
5757
function prependFileSync(path, data) {
5858
const existingFileContent = readFileSync(path, {

src/@types/pg.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
declare module 'pg/lib/utils' {
2+
export function prepareValue(value: unknown): unknown;
3+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`sql tag composes well 1`] = `
4+
-- name: undefined
5+
VALUES ($1, $2, $3, $4), ($5, $6, $7, $8)
6+
-- values: Array [
7+
1,
8+
2,
9+
3,
10+
4,
11+
2,
12+
3,
13+
2,
14+
3,
15+
]
16+
`;
17+
18+
exports[`sql tag should accept $id transform 1`] = `
19+
-- name: undefined
20+
SELECT * from "table"
21+
-- values: Array []
22+
`;
23+
24+
exports[`sql tag should accept parametrized string 1`] = `
25+
-- name: undefined
26+
SELECT $1 as i, $2 as s
27+
-- values: Array [
28+
1,
29+
"string value",
30+
]
31+
`;
32+
33+
exports[`sql tag should accept parametrized string: JSON serialized 1`] = `
34+
Object {
35+
"name": undefined,
36+
"text": "SELECT $1 as i, $2 as s",
37+
"values": Array [
38+
1,
39+
"string value",
40+
],
41+
}
42+
`;
43+
44+
exports[`sql tag should accept single string 1`] = `
45+
-- name: undefined
46+
SELECT * FROM foo
47+
-- values: Array []
48+
`;
49+
50+
exports[`sql tag should mix transforms and variables correctly 1`] = `
51+
-- name: undefined
52+
SELECT $1 as "col1", $2 as "col2" from "table"
53+
-- values: Array [
54+
1,
55+
2,
56+
]
57+
`;

src/__tests__/greeter.spec.ts

Lines changed: 0 additions & 30 deletions
This file was deleted.

src/__tests__/serializer.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// tslint:disable:no-unsafe-any
2+
import { SqlFragment } from '../sql-fragment';
3+
4+
expect.addSnapshotSerializer({
5+
test(value) {
6+
return value && value instanceof SqlFragment;
7+
},
8+
print(value, serialize, indent) {
9+
return [`-- name: ${value.name}`, value.text, `-- values: ${serialize(value.values)}`].join(
10+
'\n',
11+
);
12+
},
13+
});

0 commit comments

Comments
 (0)