https://github.com/sultan99/sql-fns
🧱 Yet another SQL builder for Node.js.
https://github.com/sultan99/sql-fns
Last synced: 5 months ago
JSON representation
🧱 Yet another SQL builder for Node.js.
- Host: GitHub
- URL: https://github.com/sultan99/sql-fns
- Owner: sultan99
- License: mit
- Created: 2020-01-01T11:51:14.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2024-05-08T03:11:16.000Z (about 2 years ago)
- Last Synced: 2025-10-13T18:57:03.809Z (8 months ago)
- Language: JavaScript
- Size: 890 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 13
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# sql-fns
Yet another SQL builder for Node.js.
At this moment just blue-prints of api. Implementation would be later, the final aim is to make "ORM" base on this SQL builder with the same FP approach.
Features:
- less verbose & human readable syntax
- curried & composable functions
- compatible with fp libraries like [ramda](https://github.com/ramda/ramda), [lodash/fp](https://github.com/lodash/lodash/wiki/FP-Guide)
### Templates
Dead simple just use plain text to build SQL:
```js
import {sql} from 'sql-fns'
const findUserByName = name => sql`
SELECT * FROM "user"
WHERE name = '${name}'
LIMIT 1
`
const bob = await findUserByName(`Bob`)
// SELECT * FROM "user" WHERE name = 'Bob' LIMIT 1
```
### Query function
Plain text is ok in some cases, but much better to use functions to build more reusable queries.
```js
import {query, select, from, limit, gt} from 'sql-fns'
const findUser = query(
select(`*`),
from(`user`),
limit(1)
)
const bob = await findUser({name: `Bob`})
const pal = await findUser({isCrazy: `yes`, age: gt(21)})
// SELECT * FROM "user" WHERE name = 'Bob' LIMIT 1
// SELECT * FROM "user" WHERE is_crazy = 'yes' AND age > 21 LIMIT 1
/* middleware applied to convert camelCase to snake_case */
```
### Recomposing existing query
Here we reuse the existing `findUser` query by adding new conditions and rewriting limit to a new value.
```js
import {where, like} from 'sql-fns'
const findBrides = findUser(user => [
where(
user.age.between(21, 28),
user.gender.equals(`female`),
),
limit(100),
])
const brides = await findBrides({bio: like(`%sexy%`)})
// SELECT * FROM "user"
// WHERE age BETWEEN 21 AND 28
// AND gender = 'female'
// AND bio like '%sexy%'
// LIMIT 100
```
### Join and rule
Alias and table join
```js
import {table, join} from 'sql-fns'
const country = table(`country`, `c`)
const {code: countryCode} = country
const findTeenagers = findUser(user => [
join(country).on(user.countryId, country.id),
limit(100),
])
// for where clause we can pass json or function
const users = await findTeenagers(({age}) => [
age.between(10, 19),
countryCode.equals(`LU`),
])
// SELECT * FROM "user" u
// JOIN "country" c ON u.country_id = c.id
// WHERE u.age BETWEEN 10 AND 19
// AND c.code = 'LU'
// LIMIT 100
```
### Mutations
Insert one ore more records.
```js
import {insert} from 'sql-fns'
const createUsers = insert(`user`)
const id = await createUsers({name: `Bob`, age: 13})
const ids = await createUsers([
{name: `Foo`, age: 12},
{name: `Bar`, age: 34},
])
```
Update records
```js
import {update, lt} from 'sql-fns'
const updateUsers = update(`user`)
await updateUsers(
set({status: `banned`}),
where({age: lt(21)}),
)
```
All mutation functions are extendable similar to queries.
```js
const banUsers = updateUsers(user =>
user.role.not(`admin`),
)
await banUsers()
// UPDATE "user" SET status = 'banned' WHERE age < 21 AND role <> 'admin';
```
## To be continued ...
You are welcome to [follow](https://github.com/sultan99/sql-fns/stargazers) or [join](https://github.com/sultan99/sql-fns/network/members) the project.