Skip to content

Commit 9c9f1e8

Browse files
authored
Separate pg shorthand in sql/pg (#3)
* separate pg shorthand * add pg.js to package.json's files * update readme * update types
1 parent 736d19b commit 9c9f1e8

9 files changed

Lines changed: 62 additions & 52 deletions

File tree

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
],
1313
"files": [
1414
"sql.js",
15+
"pg.js",
1516
"types/index.d.ts"
1617
],
1718
"types": "types",

pg.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const sql = require('./sql')
2+
3+
const sqlPG = db => async (...args) => {
4+
const { rows } = await db.query(sql(...args))
5+
return rows
6+
}
7+
8+
module.exports = sqlPG

readme.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,16 +162,20 @@ Since we ❤️ [node-postgres](https://github.com/brianc/node-postgres) so much
162162

163163
```js
164164
// long-version
165+
const sql = require('@sequencework/sql')
165166
const { rows: movies } = await db.query(sql`select * from movies`)
166167

167168
// equivalent, short-version
169+
const sql = require('@sequencework/sql/pg') // ⚠️ we import @sequencework/sql/pg
168170
const movies = await sql(db)`select * from movies`
169171
// sql(db) just calls db.query so db can be a client or a pool :)
170172
```
171173

172174
You can then rewrite the previous `listMoviesByYear` function in a much more concise way 😎
173175

174176
```js
177+
const sql = require('@sequencework/sql/pg') // ⚠️ we import @sequencework/sql/pg
178+
175179
const listMoviesByYear = async (db, yearRange) => sql(db)`
176180
select * from movies
177181
where

sql.js

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,21 +32,7 @@ const sqlText = (count, chains, expressions) => {
3232
}
3333
}
3434

35-
const sql = (chains, ...expressions) => {
36-
// if first argument is a db, then the tag is used like this :
37-
// sql(db)`...`
38-
if (chains.query) {
39-
const db = chains
40-
return async (chains, ...expressions) => {
41-
const { rows } = await db.query(sqlText(1, chains, expressions))
42-
return rows
43-
}
44-
}
45-
46-
// basic usage
47-
// sql`...`
48-
return sqlText(1, chains, expressions)
49-
}
35+
const sql = (chains, ...expressions) => sqlText(1, chains, expressions)
5036

5137
class SqlContainer {
5238
constructor(chains, expressions) {

test/pg.test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const sqlPG = require('../pg')
2+
3+
test('shorthand for node-postgres', async () => {
4+
const sampleBooks = ['book1', 'book2']
5+
const db = {
6+
query: async ({ text, values }) => {
7+
if (text === 'select * from books') {
8+
return { rows: sampleBooks }
9+
}
10+
return { rows: [] }
11+
}
12+
}
13+
14+
const books = await sqlPG(db)`select * from books`
15+
expect(books).toBe(sampleBooks)
16+
})

test/sql.test.js

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -77,21 +77,6 @@ test('imbricated sql tags (2 levels)', () => {
7777
expect(query.values[2]).toBe(expr2)
7878
})
7979

80-
test('shorthand for node-postgres', async () => {
81-
const sampleBooks = ['book1', 'book2']
82-
const db = {
83-
query: async ({ text, values }) => {
84-
if (text === 'select * from books') {
85-
return { rows: sampleBooks }
86-
}
87-
return { rows: [] }
88-
}
89-
}
90-
91-
const books = await sql(db)`select * from books`
92-
expect(books).toBe(sampleBooks)
93-
})
94-
9580
test('json as query parameter', () => {
9681
const jsonValue = { _sql: { some: 'data' }, item: 'value' }
9782
const query = sql`select obj from movies where obj = ${jsonValue}`

types/index.d.ts

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,36 @@
11
// TypeScript Version: 2.9
22

3-
export = sql
4-
5-
declare function sql(chains: {
6-
readonly query: (
7-
queryExpression: sql.QueryConfig
8-
) => Promise<{
9-
rows: any[]
10-
}>
11-
}): (chains: ReadonlyArray<string>, ...expressions: any[]) => Promise<any[]>
12-
13-
declare function sql(
14-
chains: ReadonlyArray<string>,
15-
...expressions: any[]
16-
): sql.QueryConfig
17-
18-
declare namespace sql {
3+
declare namespace sqlElements {
194
interface QueryConfig {
205
_sql?: SqlContainer
216
text: string
227
values: any[]
238
}
9+
10+
class SqlContainer {
11+
constructor(chains: ReadonlyArray<string>, expressions: any[])
12+
readonly chains: ReadonlyArray<string>
13+
readonly expressions: any[]
14+
}
2415
}
2516

26-
declare class SqlContainer {
27-
constructor(chains: ReadonlyArray<string>, expressions: any[])
28-
readonly chains: ReadonlyArray<string>
29-
readonly expressions: any[]
17+
declare module '@sequencework/sql' {
18+
function sql(
19+
chains: ReadonlyArray<string>,
20+
...expressions: any[]
21+
): sqlElements.QueryConfig
22+
23+
export = sql
24+
}
25+
26+
declare module '@sequencework/sql/pg' {
27+
function sqlPG(chains: {
28+
readonly query: (
29+
queryExpression: sqlElements.QueryConfig
30+
) => Promise<{
31+
rows: any[]
32+
}>
33+
}): (chains: ReadonlyArray<string>, ...expressions: any[]) => Promise<any[]>
34+
35+
export = sqlPG
3036
}

types/test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// See https://github.com/DefinitelyTyped/DefinitelyTyped
33

44
import sql = require('@sequencework/sql')
5+
import sqlPG = require('@sequencework/sql/pg')
56

67
const yearRange = [1983, 1992]
78

@@ -61,7 +62,7 @@ const db = {
6162
}
6263
}
6364
const getBooks = async (): Promise<string[]> => {
64-
const rows = await sql(db)`select * from books`
65+
const rows = await sqlPG(db)`select * from books`
6566
return rows as string[]
6667
}
6768
// $ExpectType Promise<string[]>

types/tsconfig.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
// If the library is an external module (uses `export`), this allows your test file to import "mylib" instead of "./index".
1212
// If the library is global (cannot be imported via `import` or `require`), leave this out.
1313
"baseUrl": ".",
14-
"paths": { "@sequencework/sql": ["."] }
14+
"paths": {
15+
"@sequencework/sql": ["."],
16+
"@sequencework/sql/pg": ["./pg"]
17+
}
1518
}
1619
}

0 commit comments

Comments
 (0)