Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
447bdc8
rewrite in typescript (wip)
lucleray Sep 15, 2018
f4de67f
add build to gitignore
lucleray Sep 15, 2018
8dc038e
Merge branch 'master' into rewrite-typescript
lucleray Sep 15, 2018
9b3876e
fix bug
lucleray Sep 15, 2018
aba6d0b
fix bug (bis)
lucleray Sep 15, 2018
19f18c8
update jest config
lucleray Sep 15, 2018
dab7d14
update helpers
lucleray Sep 15, 2018
7940a12
update tests
lucleray Sep 15, 2018
b622402
remove pg.js and sql.js
lucleray Sep 15, 2018
9566321
update build script
lucleray Sep 15, 2018
aa43a75
include right files when publishing
lucleray Sep 15, 2018
debb5c5
improve scripts
lucleray Sep 15, 2018
a4c81d2
remove types folder
lucleray Sep 15, 2018
571bb52
remove types folder (bis)
lucleray Sep 15, 2018
c929a2c
clean stuff
lucleray Sep 15, 2018
d5c308c
clean stuff (bis)
lucleray Sep 15, 2018
0dc79ae
fix linter
lucleray Sep 15, 2018
a7cfda0
fix linting issues
lucleray Sep 15, 2018
9abad46
remove dtslint in circleci
lucleray Sep 15, 2018
2a5fadb
simplify things
lucleray Sep 15, 2018
820e31a
fix tests
lucleray Sep 15, 2018
7bed7bf
Merge branch 'master' into rewrite-typescript
lucleray Sep 15, 2018
4273943
better types/interfaces
lucleray Sep 15, 2018
f406577
update readme
lucleray Sep 15, 2018
f0106a9
fix yarn run lint
lucleray Sep 15, 2018
60a6b25
add IPGQueryable
lucleray Sep 15, 2018
a1835d4
Merge branch 'master' into rewrite-typescript
lucleray Sep 18, 2018
4a8e7ad
fix tests
yannvgn Sep 20, 2018
75a1392
move expression counter into SqlContainer
yannvgn Sep 20, 2018
091f4c3
Merge branch 'master' into rewrite-typescript
lucleray Sep 20, 2018
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
11 changes: 0 additions & 11 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ jobs:
- restore-cache:
key: yarn-packages-{{ checksum "yarn.lock" }}
- run: yarn
- run: yarn dtslint --installAll
- save-cache:
key: yarn-packages-{{ checksum "yarn.lock" }}
paths:
Expand All @@ -39,12 +38,6 @@ jobs:
- attach_workspace: *attach_environment
- run: yarn run lint

dtslint:
<<: *defaults
steps:
- attach_workspace: *attach_environment
- run: yarn run dtslint

test:
<<: *defaults
steps:
Expand All @@ -63,10 +56,6 @@ workflows:
requires:
- setup

- dtslint:
requires:
- setup

- test:
requires:
- setup
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
node_modules
coverage
coverage

*.d.ts
*.js
34 changes: 34 additions & 0 deletions lib/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
export class SqlContainer {
public readonly chains: ReadonlyArray<string>
public readonly expressions: any[]
public readonly count: number
constructor(
chains: ReadonlyArray<string>,
expressions: any[],
count: number
) {
this.chains = chains
this.expressions = expressions
this.count = count
}
}

export type TemplateLiteralFunc<T> = (
chains: ReadonlyArray<string>,
...expressions: any[]
) => T

export interface IPGQueryConfig {
_sql?: SqlContainer
text: string
values: any[]
}

export interface IPGQueryResult {
rowCount: number
rows: any[]
}

export interface IPGQueryable {
readonly query: (query: IPGQueryConfig) => Promise<IPGQueryResult>
}
52 changes: 30 additions & 22 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "@sequencework/sql",
"version": "2.0.1",
"main": "sql.js",
"types": "sql.d.ts",
"license": "MIT",
"description": "Javascript tag to format SQL template literals",
"keywords": [
Expand All @@ -12,55 +13,62 @@
],
"files": [
"sql.js",
"sql.d.ts",
"pg.js",
"types/index.d.ts"
"pg.d.ts"
],
"types": "types",
"scripts": {
"build": "tsc",
"clean": "rimraf pg.js pg.d.ts sql.js sql.d.ts lib/**/*.js lib/**/*.d.ts coverage",
"test": "jest",
"prettier": "prettier --write '**/*.{js,json,css,md}' --ignore-path .gitignore",
"lint": "eslint '**/*.js' --ignore-path .gitignore",
"dtslint": "dtslint types"
"prettier": "prettier --write '**/*.{ts,js,json,css,md}' --ignore-path .gitignore",
"lint": "tslint *.ts **/*.ts"
},
"devDependencies": {
"@sequencework/eslint-config": "^0.0.7",
"@types/jest": "^23.3.2",
"codecov": "3.1.0",
"dtslint": "^0.3.0",
"eslint": "5.4.0",
"husky": "1.0.0-rc.13",
"jest": "^23.5.0",
"lint-staged": "7.2.2",
"prettier": "1.14.2"
"prettier": "1.14.2",
"rimraf": "^2.6.2",
"ts-jest": "^23.1.4",
"tslint": "^5.11.0",
"tslint-config-prettier": "^1.15.0",
"typescript": "^3.0.3"
},
"dependencies": {},
"jest": {
"collectCoverage": true,
"transform": {
"^.+\\.ts$": "ts-jest"
},
"testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.ts$",
"moduleFileExtensions": [
"ts",
"js",
"json",
"node"
]
},
"husky": {
"hooks": {
"pre-commit": "lint-staged"
}
},
"jest": {
"collectCoverage": true
},
"prettier": {
"printWidth": 80,
"tabWidth": 2,
"singleQuote": true,
"semi": false
},
"eslintConfig": {
"extends": "@sequencework"
},
"lint-staged": {
"*.js": [
"prettier --write",
"eslint",
"git add"
],
"*.{json,css,md}": [
"*.ts": [
"prettier --write",
"tslint",
"git add"
],
"*.ts": [
"*.{ts,js,json,css,md}": [
"prettier --write",
"git add"
]
Expand Down
22 changes: 0 additions & 22 deletions pg.js

This file was deleted.

40 changes: 40 additions & 0 deletions pg.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import {
IPGQueryable,
IPGQueryConfig,
IPGQueryResult,
TemplateLiteralFunc
} from './lib/utils'
import _sql = require('./sql')

type PGSqlHelper<T> = (db: IPGQueryable) => TemplateLiteralFunc<Promise<T>>

type PGSql = TemplateLiteralFunc<IPGQueryConfig> & {
query: PGSqlHelper<IPGQueryResult>
many: PGSqlHelper<any[]>
one: PGSqlHelper<any>
count: PGSqlHelper<number>
}

const sql = ((chains, ...expressions) => _sql(chains, ...expressions)) as PGSql

sql.query = db => (chains, ...expressions) =>
db.query(_sql(chains, ...expressions))

sql.one = db => async (chains, ...expressions) => {
const {
rows: [row]
} = await db.query(_sql(chains, ...expressions))
return row
}

sql.many = db => async (chains, ...expressions) => {
const { rows } = await db.query(_sql(chains, ...expressions))
return rows
}

sql.count = db => async (chains, ...expressions) => {
const { rowCount } = await db.query(_sql(chains, ...expressions))
return rowCount
}

export = sql
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Transforms a template literal in an object that can be read by [node-postgres](h

### Features

- [Typescript friendly](#usage-with-typescript)
- Written in [Typescript](#usage-with-typescript)
- Lightweight (less than 50 lines of code)
- Fully tested (100% coverage)
- Works with **nested sql tags**
Expand Down
24 changes: 10 additions & 14 deletions sql.js → sql.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
const sqlText = (count, chains, expressions) => {
import { IPGQueryConfig, SqlContainer, TemplateLiteralFunc } from './lib/utils'

function sqlText(
count: number,
chains: ReadonlyArray<string>,
expressions: any[]
): IPGQueryConfig {
let text = chains[0]
const values = []

Expand Down Expand Up @@ -33,17 +39,7 @@ const sqlText = (count, chains, expressions) => {
}
}

const sql = (chains, ...expressions) => {
const { _sql, text, values } = sqlText(1, chains, expressions)
return { _sql, text, values }
}

class SqlContainer {
constructor(chains, expressions, count) {
this.chains = chains
this.expressions = expressions
this.count = count
}
}
const sql: TemplateLiteralFunc<IPGQueryConfig> = (chains, ...expressions) =>
sqlText(1, chains, expressions)

module.exports = sql
export = sql
10 changes: 8 additions & 2 deletions test/pg.test.js → test/pg.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
const sql = require('../pg')
import { IPGQueryable } from '../lib/utils'
import sql = require('../pg')

const sampleBooks = ['book1', 'book2']
const db = {
const db: IPGQueryable = {
query: async ({ text, values }) => {
if (text === 'select * from books') {
return { rows: sampleBooks, rowCount: sampleBooks.length }
Expand All @@ -10,6 +11,11 @@ const db = {
}
}

test('sql should return the query config', async () => {
const queryConfig = sql`select * from books`
expect(queryConfig._sql).toBeTruthy()
})

test("sql.query should return pg's query result", async () => {
const { rows, rowCount } = await sql.query(db)`select * from books`
expect(rows).toBe(sampleBooks)
Expand Down
2 changes: 1 addition & 1 deletion test/sql.test.js → test/sql.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const sql = require('../sql')
import sql = require('../sql')

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

Expand Down
8 changes: 8 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"declaration": true
},
"files": ["sql.ts", "pg.ts"]
}
6 changes: 6 additions & 0 deletions tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": ["tslint:recommended", "tslint-config-prettier"],
"rules": {
"variable-name": false
}
}
69 changes: 0 additions & 69 deletions types/index.d.ts

This file was deleted.

Loading