Skip to content

Commit 792ae98

Browse files
authored
Rewrite with typescript (#6)
* rewrite in typescript (wip) * add build to gitignore * fix bug * fix bug (bis) * update jest config * update helpers * update tests * remove pg.js and sql.js * update build script * include right files when publishing * improve scripts * remove types folder * remove types folder (bis) * clean stuff * clean stuff (bis) * fix linter * fix linting issues * remove dtslint in circleci * simplify things * fix tests * better types/interfaces * update readme * fix yarn run lint * add IPGQueryable * fix tests * move expression counter into SqlContainer
1 parent f728347 commit 792ae98

File tree

17 files changed

+209
-949
lines changed

17 files changed

+209
-949
lines changed

.circleci/config.yml

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ jobs:
2424
- restore-cache:
2525
key: yarn-packages-{{ checksum "yarn.lock" }}
2626
- run: yarn
27-
- run: yarn dtslint --installAll
2827
- save-cache:
2928
key: yarn-packages-{{ checksum "yarn.lock" }}
3029
paths:
@@ -39,12 +38,6 @@ jobs:
3938
- attach_workspace: *attach_environment
4039
- run: yarn run lint
4140

42-
dtslint:
43-
<<: *defaults
44-
steps:
45-
- attach_workspace: *attach_environment
46-
- run: yarn run dtslint
47-
4841
test:
4942
<<: *defaults
5043
steps:
@@ -63,10 +56,6 @@ workflows:
6356
requires:
6457
- setup
6558

66-
- dtslint:
67-
requires:
68-
- setup
69-
7059
- test:
7160
requires:
7261
- setup

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
node_modules
2-
coverage
2+
coverage
3+
4+
*.d.ts
5+
*.js

lib/utils.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
export class SqlContainer {
2+
public readonly chains: ReadonlyArray<string>
3+
public readonly expressions: any[]
4+
public readonly count: number
5+
constructor(
6+
chains: ReadonlyArray<string>,
7+
expressions: any[],
8+
count: number
9+
) {
10+
this.chains = chains
11+
this.expressions = expressions
12+
this.count = count
13+
}
14+
}
15+
16+
export type TemplateLiteralFunc<T> = (
17+
chains: ReadonlyArray<string>,
18+
...expressions: any[]
19+
) => T
20+
21+
export interface IPGQueryConfig {
22+
_sql?: SqlContainer
23+
text: string
24+
values: any[]
25+
}
26+
27+
export interface IPGQueryResult {
28+
rowCount: number
29+
rows: any[]
30+
}
31+
32+
export interface IPGQueryable {
33+
readonly query: (query: IPGQueryConfig) => Promise<IPGQueryResult>
34+
}

package.json

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"name": "@sequencework/sql",
33
"version": "2.0.1",
44
"main": "sql.js",
5+
"types": "sql.d.ts",
56
"license": "MIT",
67
"description": "Javascript tag to format SQL template literals",
78
"keywords": [
@@ -12,55 +13,62 @@
1213
],
1314
"files": [
1415
"sql.js",
16+
"sql.d.ts",
1517
"pg.js",
16-
"types/index.d.ts"
18+
"pg.d.ts"
1719
],
18-
"types": "types",
1920
"scripts": {
21+
"build": "tsc",
22+
"clean": "rimraf pg.js pg.d.ts sql.js sql.d.ts lib/**/*.js lib/**/*.d.ts coverage",
2023
"test": "jest",
21-
"prettier": "prettier --write '**/*.{js,json,css,md}' --ignore-path .gitignore",
22-
"lint": "eslint '**/*.js' --ignore-path .gitignore",
23-
"dtslint": "dtslint types"
24+
"prettier": "prettier --write '**/*.{ts,js,json,css,md}' --ignore-path .gitignore",
25+
"lint": "tslint *.ts **/*.ts"
2426
},
2527
"devDependencies": {
26-
"@sequencework/eslint-config": "^0.0.7",
28+
"@types/jest": "^23.3.2",
2729
"codecov": "3.1.0",
28-
"dtslint": "^0.3.0",
29-
"eslint": "5.4.0",
3030
"husky": "1.0.0-rc.13",
3131
"jest": "^23.5.0",
3232
"lint-staged": "7.2.2",
33-
"prettier": "1.14.2"
33+
"prettier": "1.14.2",
34+
"rimraf": "^2.6.2",
35+
"ts-jest": "^23.1.4",
36+
"tslint": "^5.11.0",
37+
"tslint-config-prettier": "^1.15.0",
38+
"typescript": "^3.0.3"
3439
},
3540
"dependencies": {},
41+
"jest": {
42+
"collectCoverage": true,
43+
"transform": {
44+
"^.+\\.ts$": "ts-jest"
45+
},
46+
"testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.ts$",
47+
"moduleFileExtensions": [
48+
"ts",
49+
"js",
50+
"json",
51+
"node"
52+
]
53+
},
3654
"husky": {
3755
"hooks": {
3856
"pre-commit": "lint-staged"
3957
}
4058
},
41-
"jest": {
42-
"collectCoverage": true
43-
},
4459
"prettier": {
4560
"printWidth": 80,
4661
"tabWidth": 2,
4762
"singleQuote": true,
4863
"semi": false
4964
},
50-
"eslintConfig": {
51-
"extends": "@sequencework"
52-
},
5365
"lint-staged": {
54-
"*.js": [
55-
"prettier --write",
56-
"eslint",
57-
"git add"
58-
],
59-
"*.{json,css,md}": [
66+
"*.ts": [
6067
"prettier --write",
68+
"tslint",
6169
"git add"
6270
],
63-
"*.ts": [
71+
"*.{ts,js,json,css,md}": [
6472
"prettier --write",
6573
"git add"
6674
]

pg.js

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

pg.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import {
2+
IPGQueryable,
3+
IPGQueryConfig,
4+
IPGQueryResult,
5+
TemplateLiteralFunc
6+
} from './lib/utils'
7+
import _sql = require('./sql')
8+
9+
type PGSqlHelper<T> = (db: IPGQueryable) => TemplateLiteralFunc<Promise<T>>
10+
11+
type PGSql = TemplateLiteralFunc<IPGQueryConfig> & {
12+
query: PGSqlHelper<IPGQueryResult>
13+
many: PGSqlHelper<any[]>
14+
one: PGSqlHelper<any>
15+
count: PGSqlHelper<number>
16+
}
17+
18+
const sql = ((chains, ...expressions) => _sql(chains, ...expressions)) as PGSql
19+
20+
sql.query = db => (chains, ...expressions) =>
21+
db.query(_sql(chains, ...expressions))
22+
23+
sql.one = db => async (chains, ...expressions) => {
24+
const {
25+
rows: [row]
26+
} = await db.query(_sql(chains, ...expressions))
27+
return row
28+
}
29+
30+
sql.many = db => async (chains, ...expressions) => {
31+
const { rows } = await db.query(_sql(chains, ...expressions))
32+
return rows
33+
}
34+
35+
sql.count = db => async (chains, ...expressions) => {
36+
const { rowCount } = await db.query(_sql(chains, ...expressions))
37+
return rowCount
38+
}
39+
40+
export = sql

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Transforms a template literal in an object that can be read by [node-postgres](h
1111

1212
### Features
1313

14-
- [Typescript friendly](#usage-with-typescript)
14+
- Written in [Typescript](#usage-with-typescript)
1515
- Lightweight (less than 50 lines of code)
1616
- Fully tested (100% coverage)
1717
- Works with **nested sql tags**

sql.js renamed to sql.ts

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
const sqlText = (count, chains, expressions) => {
1+
import { IPGQueryConfig, SqlContainer, TemplateLiteralFunc } from './lib/utils'
2+
3+
function sqlText(
4+
count: number,
5+
chains: ReadonlyArray<string>,
6+
expressions: any[]
7+
): IPGQueryConfig {
28
let text = chains[0]
39
const values = []
410

@@ -33,17 +39,7 @@ const sqlText = (count, chains, expressions) => {
3339
}
3440
}
3541

36-
const sql = (chains, ...expressions) => {
37-
const { _sql, text, values } = sqlText(1, chains, expressions)
38-
return { _sql, text, values }
39-
}
40-
41-
class SqlContainer {
42-
constructor(chains, expressions, count) {
43-
this.chains = chains
44-
this.expressions = expressions
45-
this.count = count
46-
}
47-
}
42+
const sql: TemplateLiteralFunc<IPGQueryConfig> = (chains, ...expressions) =>
43+
sqlText(1, chains, expressions)
4844

49-
module.exports = sql
45+
export = sql

test/pg.test.js renamed to test/pg.test.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
const sql = require('../pg')
1+
import { IPGQueryable } from '../lib/utils'
2+
import sql = require('../pg')
23

34
const sampleBooks = ['book1', 'book2']
4-
const db = {
5+
const db: IPGQueryable = {
56
query: async ({ text, values }) => {
67
if (text === 'select * from books') {
78
return { rows: sampleBooks, rowCount: sampleBooks.length }
@@ -10,6 +11,11 @@ const db = {
1011
}
1112
}
1213

14+
test('sql should return the query config', async () => {
15+
const queryConfig = sql`select * from books`
16+
expect(queryConfig._sql).toBeTruthy()
17+
})
18+
1319
test("sql.query should return pg's query result", async () => {
1420
const { rows, rowCount } = await sql.query(db)`select * from books`
1521
expect(rows).toBe(sampleBooks)

test/sql.test.js renamed to test/sql.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const sql = require('../sql')
1+
import sql = require('../sql')
22

33
const trimSpaces = str => str.trim().replace(/\s+/g, ' ')
44

0 commit comments

Comments
 (0)