Skip to content

Commit

Permalink
Merge next branch
Browse files Browse the repository at this point in the history
  • Loading branch information
absolux committed Aug 14, 2017
2 parents 1ad0d68 + 52168d5 commit 29fc709
Show file tree
Hide file tree
Showing 84 changed files with 3,407 additions and 6,082 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
TODO.md

lib
dist
.vscode

# Logs
Expand Down Expand Up @@ -30,6 +33,7 @@ coverage
build/Release

# Dependency directories
package-lock.json
node_modules
jspm_packages

Expand Down
149 changes: 85 additions & 64 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
[![Build Status](https://travis-ci.org/vitaminjs/query-builder.svg?branch=master)](https://travis-ci.org/vitaminjs/query-builder)

A fluent SQL query builder for Node.
A fluent SQL query builder for Node.js
It provides support for:
- **PostgreSQL** (v9.5+)
- **SQLite** (v3.15.0+)
- **MySQL** (v5.7+)
- **PostgreSQL** (v9.5+)
- **MSSQL** (2012+)

## Installing
Expand All @@ -15,91 +15,110 @@ $ npm install --save vitamin-query

## Getting started

`vitamin-query` is composed of a set of useful functions to build SQL queries easily
`vitamin-query` is composed of a set of useful expressions and helpers to build SQL queries easily

### building queries

```js
// import the query builder
import { select, insert, update, deleteFrom, raw, not, lt, table, column } from 'vitamin-query'

import { table, select, selectFrom } from 'vitamin-query'

let selectQuery = select().from('employees').where(raw('salary > ?', 1500)).build('pg')
assert.equal(selectQuery.sql, 'select * from employees where salary > $1')
assert.deepEqual(selectQuery.params, [ 1500 ])
// Select query
let query = table('employees').select('count(*)').where('salary between ? and (?1 * 2)', 1500).toQuery('pg')
assert.equal(query.sql, 'select count(*) from employees where salary between $1 and ($2 * 2)')
assert.deepEqual(query.params, [ 1500, 1500 ])

// Compound query
let query = selectFrom('t').where('a is null').union(selectFrom('t').where('b is null')).toQuery('pg')
assert.equal(query.sql, 'select * from t where a is null union select * from t where b is null')

// Insert query
let fred = { name: "Fred", score: 30 }
let insertQuery = insert(fred).into('players').returning('*').build('mssql')
assert.equal(insertQuery.sql, 'insert into players (name, score) output inserted.* values (?, ?)')
assert.deepEqual(insertQuery.params, [ 'Fred', 30 ])


let updateQuery = update('books').set('status', 'archived').where({ publish_date: not(lt(2000)) }).build('mysql')
assert.equal(updateQuery.sql, 'update books set status = ? where (publish_date >= ?)')
assert.deepEqual(updateQuery.params, [ 'archived', 2000 ])


let deleteQuery = deleteFrom(table('accounts')).where(column('activated'), false).build('sqlite')
assert.equal(deleteQuery.sql, 'delete from "accounts" where "activated" = ?')
assert.deepEqual(deleteQuery.params, [ false ])
let query = table('players').insert(fred).returning('*').toQuery('mssql')
assert.equal(query.sql, 'insert into players (name, score) output inserted.* values (?, ?)')
assert.deepEqual(query.params, [ 'Fred', 30 ])

// Update query
let query = table('books').update('status', 'archived').where('publish_date <= ?', 2000).toQuery('mysql')
assert.equal(query.sql, 'update books set status = ? where publish_date <= ?')
assert.deepEqual(query.params, [ 'archived', 2000 ])

// Delete query
let query = table('accounts').delete().where({ activated: false, deleted: true }).toQuery('sqlite')
assert.equal(query.sql, 'delete from accounts where activated = ? and deleted = ?')
assert.deepEqual(query.params, [ false, true ])
```

### Custom compiler

If you may use a custom query compiler instead of the built-in ones, you can pass its instance to `build()`
If you may use a custom query compiler instead of the built-in ones, you can pass its instance to `toQuery()`

```js
// in path/to/maria-compiler.js
import MysqlCompiler from 'vitamin-query/compiler/mysql'

class MariaCompiler extends MysqlCompiler {

...

}
class MariaCompiler extends MysqlCompiler { ... }

// later, you can use its instance with any query instance
import * as qb from 'vitamin-query'
import qb from 'vitamin-query'

let query = qb.selectFrom('table').build(new MariaCompiler())
let query = qb.select().from('table').toQuery(new MariaCompiler({ /* options */ }))
```

### API

For examples of usage, please refer to the tests.

| query | Expression | Aggregates | Conditional | Functions |
| ----- | ---------- | ---------- | ----------- | --------- |
| select | column | sum | eq | upper, ucase |
| selectFrom | table | avg | ne | lower, lcase |
| insert | sq | max | gt | replace |
| insertInto | raw | min | lt | substr, substring |
| update | esc | count | gte | concat |
| deleteFrom | cast | | lte | len, length |
| | cte | | between | repeat |
| | values | | in, $in | space |
| | | | between | strpos, position |
| | | | startsWith | left |
| | | | like | right |
| | | | and | trim |
| | | | not | ltrim |
| | | | or | rtrim |
| | | | exists | abs |
| | | | contatins | round |
| | | | | rand, random |
| | | | | now, datetime |
| | | | | utc |
| | | | | today, curdate |
| | | | | clock, curtime |
| | | | | date |
| | | | | time |
| | | | | day |
| | | | | month |
| | | | | year |
| | | | | hour |
| | | | | minute |
| | | | | second |
## API

For examples of usage, please refer to the tests

### Expression helpers

These Helpers are functions that return Expression instances:

- **alias**(expr, name: string; ...columns: string[]): IAlias
- **table**(value: string | IExpression): ITable
- **func**(name: string, ...args): IFunction
- **raw**(expr: string, ...args): ILiteral
- **values**(data: any[][]): IValues
- **id**(name: string): IIdentifier
- **esc**(value: string): ILiteral
- **selectFrom**(table): ISelect
- **select**(...fields): ISelect
- **val**(value): ILiteral
- **desc**(expr): IOrder
- **asc**(expr): IOrder

### Function helpers

Helpers to emulate the SQL built-in functions

- **substr | substring**(expr, start: number, length?: number): IFunction
- **replace**(expr, pattern, replacement): IFunction
- **strpos | position**(str, substr): IFunction
- **repeat**(expr, count: number): IFunction
- **right**(expr, length: number): IFunction
- **left**(expr, length: number): IFunction
- **round**(expr, n: number): IFunction
- **space**(length: number): IFunction
- **upper | ucase**(expr): IFunction
- **lower | lcase**(expr): IFunction
- **len | length**(expr): IFunction
- **today | curdate**(): IFunction
- **clock | curtime**(): IFunction
- **concat**(...parts): IFunction
- **now | datetime**(): IFunction
- **rand | random**(): IFunction
- **minute**(expr): IFunction
- **second**(expr): IFunction
- **ltrim**(expr): IFunction
- **rtrim**(expr): IFunction
- **month**(expr): IFunction
- **date**(expr): IFunction
- **time**(expr): IFunction
- **trim**(expr): IFunction
- **year**(expr): IFunction
- **hour**(expr): IFunction
- **abs**(expr): IFunction
- **day**(expr): IFunction
- **utc**(): IFunction

## Testing

Expand All @@ -109,6 +128,8 @@ $ npm test

## Change log

- **v1.0.0-alpha** - _TypeScript version of the library_

- **v0.2.1** - _Add support for common table expressions_
- Deprecate `Query::toSQL()` and add `Query::build()` instead
- Add Support for named parameters within `raw` helper (issue #6)
Expand Down
10 changes: 10 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

var helpers = require('./lib/helpers')

exports = require('./lib/builder').factory

exports.Builder = require('./lib/builder').default

Object.keys(helpers).forEach(function (name) {
exports[name] = helpers[name]
})
19 changes: 9 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
{
"name": "vitamin-query",
"version": "0.2.1",
"version": "1.0.0-alpha",
"description": "A fluent SQL query builder for node.js applications",
"main": "lib/index.js",
"main": "index.js",
"scripts": {
"prepublish": "npm run build",
"build": "babel -q -L -D src -d lib --presets es2015",
"test": "npm run build && mocha --recursive"
"prepublish": "tsc",
"test": "tsc && mocha"
},
"repository": {
"type": "git",
Expand All @@ -32,9 +31,9 @@
"lodash": "^4.16.6"
},
"devDependencies": {
"assert": "^1.4.1",
"babel-cli": "^6.11.4",
"babel-preset-es2015": "^6.9.0",
"mocha": "^3.2.0"
"@types/lodash": "^4.14.70",
"assert": "*",
"mocha": "^3.2.0",
"typescript": "^2.4.2"
}
}
}
71 changes: 0 additions & 71 deletions src/api/aggregate.js

This file was deleted.

Loading

0 comments on commit 29fc709

Please sign in to comment.