Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/sqorn/sqorn
A Javascript library for building SQL queries
https://github.com/sqorn/sqorn
javascript nodejs postgres postgresql query-builder sql sql-query-builder tagged-template-literals template-strings
Last synced: 3 months ago
JSON representation
A Javascript library for building SQL queries
- Host: GitHub
- URL: https://github.com/sqorn/sqorn
- Owner: sqorn
- License: mit
- Archived: true
- Created: 2018-07-21T21:50:30.000Z (over 6 years ago)
- Default Branch: main
- Last Pushed: 2023-02-07T16:49:27.000Z (almost 2 years ago)
- Last Synced: 2024-09-27T04:53:20.579Z (3 months ago)
- Topics: javascript, nodejs, postgres, postgresql, query-builder, sql, sql-query-builder, tagged-template-literals, template-strings
- Language: JavaScript
- Homepage: https://sqorn.org
- Size: 1.46 MB
- Stars: 1,873
- Watchers: 37
- Forks: 49
- Open Issues: 13
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome - sqorn - A Javascript library for building SQL queries (JavaScript)
- awesome-github-star - sqorn
- awesome-list - sqorn
- jimsghstars - sqorn/sqorn - A Javascript library for building SQL queries (JavaScript)
README
# [ Sqorn](https://sqorn.org) · [![License](https://img.shields.io/github/license/sqorn/sqorn.svg)](https://github.com/sqorn/sqorn/blob/master/LICENSE) [![npm](https://img.shields.io/npm/v/sqorn.svg)](https://www.npmjs.com/package/sqorn) ![Supports Node 8+](https://img.shields.io/node/v/sqorn.svg) [![npm](https://img.shields.io/travis/sqorn/sqorn.svg)](https://travis-ci.org/sqorn/sqorn) [![Coverage Status](https://coveralls.io/repos/github/sqorn/sqorn/badge.svg)](https://coveralls.io/github/sqorn/sqorn)
Sqorn is a Javascript library for building SQL queries.
**Composable:** Build complex queries from simple parts. Chain, extend, and embed queries.
**Intuitive**: Sqorn's use of modern Javascript language features like tagged template literals and promises makes building and issuing SQL queries a breeze.
**Concise:** Sqorn provides concise syntax for common CRUD operations.
[**Fast:**](https://sqorn.org/benchmarks.html) 10x faster than [Knex.js](https://knexjs.org/) and 200x faster than [Squel](https://github.com/hiddentao/squel)
**Secure:** Sqorn generates parameterized queries safe from SQL injection. Sqorn has no external dependencies.
## Install
Sqorn requires Node version 8 or above.
```sh
npm install --save @sqorn/pg # only Postgres is currently supported
```Then read the [tutorial](https://sqorn.org/docs/tutorial.html) and [try the online demo](https://sqorn.org/demo.html).
## Examples
CRUD Operations are dead simple.
```js
const sq = require('@sqorn/pg')()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 set name = 'Rob' where id = 23"```
Build complex queries from simple parts.
```js
// CHAIN QUERIES
sq.from`book`
.return`distinct author`
.where({ genre: 'Fantasy' })
.where({ language: 'French' })
// select distinct author from book
// where language = 'French' and genre = 'Fantasy'// 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 = 'Fantasy'// 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](https://sqorn.org/docs/tutorial.html).
## Contributing
Sqorn is a monorepo managed with npm workspaces.
Clone the repo then run the following commands to install all dependencies:
```sh
npm install
````npm test` runs all tests. `npm run clean` removes all dependencies.
To run tests that connect to Postgres, you need a running postgres instance from somewhere. Then, you need to set the standard Postgres environment variables to tell the tests where to connect. If you add a `.env` file to this repo, the tests will use environment variables from there to connect.
```
// in `.env`
PGHOST=127.0.0.1
PGUSER=some-user
PGPASSWORD=some-password
PGPORT=5432
PGDATABASE=postgres
```## Maintainers
Sqorn is currently maintained by the @gadgetinc development team. Please direct support requests, bug reports, and other communication about Sqorn to this github repo.
Sqorn was originally created and maintained by @eejdoowad. Thanks to Sufyan for all their hard work creating the project!
## LicenseMIT Licensed, Copyright (c) 2018 Sufyan Dawoodjee