80

GitHub - lusakasa/sqorn: A Javascript library for building SQL queries

 6 years ago
source link: https://github.com/lusakasa/sqorn
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

README.md

logo_blue.svg?sanitize=true Sqorn · License npm Supports Node 8+ npm PRs Welcome

Sqorn is a Javascript library for building SQL queries.

Ergonomic: Sqorn's intuitive API might make you forget you're building SQL.

Composable: Build complex queries from simple parts. Chain, extend, and embed queries.

Boilerplate free: Sqorn provides concise syntax for common CRUD operations.

Fast: 10x faster than Knex.js and 200x faster than Squel

Install

Sqorn requires Node version 8 or above.

npm install --save sqorn
npm install --save pg # only Postgres is currently supported

Then read the tutorial and try the online demo.

Examples

CRUD Operations are dead simple.

const sq = require('sqorn')()

const Person = sq`person`, Book = sq`book`

// SELECT
const children = await Person`age < ${13}`
// "select * from person where age < 13"

// DELETE
const [deleted] = await Book.delete({ id: 7 })`title`
// "delete from book where id = 7 returning title"

// INSERT
await Person.insert({ firstName: 'Rob' })
// "insert into person (first_name) values ('Rob')"

// UPDATE
await Person({ id: 23 }).set({ name: 'Rob' })
// "update person where id = 23 set name = 'Rob'"

Build complex queries from simple parts.

// CHAIN QUERIES
sq.from`book`
  .return`distinct author`
  .where({ genre: 'Fantasy' })
  .where({ language: 'French' })
// select distinct author from book
// where language = 'French' and genre = 'Fantsy'

// EXTEND QUERIES
sq.extend(
  sq.from`book`,
  sq.return`distinct author`,
  sq.where({ genre: 'Fantasy' }),
  sq.where({ language: 'French' })
)
// select distinct author from book
// where language = 'French' and genre = 'Fantsy'

// EMBED Queries
sq.return`now() today, ${sq.return`now() + '1 day'`} tomorrow`
// select now() today, (select now() + '1 day') tomorrow

Learn more in the tutorial.

License

MIT Licensed, Copyright (c) 2018 Sufyan Dawoodjee


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK