Skip to content
This repository has been archived by the owner on Mar 24, 2024. It is now read-only.

Commit

Permalink
Merge pull request #40 from eejdoowad/reorg
Browse files Browse the repository at this point in the history
Reorganization
  • Loading branch information
eejdoowad committed Oct 11, 2018
2 parents 336c3c9 + 3f93a69 commit d2923eb
Show file tree
Hide file tree
Showing 18 changed files with 524 additions and 24 deletions.
16 changes: 8 additions & 8 deletions docs/docs/tutorial.md
Expand Up @@ -18,20 +18,20 @@ Sqorn is a collection of libraries, one for each SQL dialect. Follow the instruc

### Postgres

Install `sqorn-pg`.
Install [Node Postgres](https://www.npmjs.com/package/pg) and [Sqorn Postgres](https://www.npmjs.com/package/sqorn-pg).

```sh
npm install --save sqorn-pg
npm install --save pg sqorn-pg
```

Create a query building instance connected to your database. Here, we connect to a local Postgres server using a [connection string](https://node-postgres.com/features/connecting#connection-uri):
Create a [Node Postgres connection pool](https://node-postgres.com/features/connecting). Then pass `pg` and `pool` as arguments to `sqorn()` to create a query builder `sq`.

```javascript
const sq = require('sqorn-pg')({
connection: {
connectionString: 'postgresql://postgres@localhost:5432/postgres'
}
})
const pg = require('pg')
const sqorn = require('sqorn-pg')

const pool = new pg.Pool()
const sq = sqorn({ pg, pool })
```

### MySQL
Expand Down
7 changes: 5 additions & 2 deletions examples/postgres/main.js
@@ -1,4 +1,5 @@
const sqorn = require('sqorn-pg')
const pg = require('pg')

const server = 'postgresql://postgres@localhost:5432/'
const adminDatabase = 'postgres'
Expand All @@ -8,15 +9,17 @@ const appConnection = { connectionString: server + appDatabase }

async function main() {
// connect to admin database
let sq = sqorn({ connection: adminConnection })
let pool = new pg.Pool(adminConnection)
let sq = sqorn({ pg, pool })
// delete app database if it exists
await sq.l`drop database if exists $${appDatabase}`
// create app database
await sq.l`create database $${appDatabase}`
// disconnect from admin database
await sq.end()
// connect to created database
sq = sqorn({ connection: appConnection })
pool = new pg.Pool(appConnection)
sq = sqorn({ pg, pool })
// create author table
await sq.l`create table author (
id serial primary key,
Expand Down
14 changes: 14 additions & 0 deletions packages/sqorn-adapter-mysql/package.json
@@ -0,0 +1,14 @@
{
"name": "sqorn-adapter-mysql",
"version": "0.0.35",
"description": "Sqorn Adapter MySQL",
"author": "Sufyan Dawoodjee <eejdoowad@gmail.com>",
"license": "MIT",
"engines": {
"node": ">=8.0.0"
},
"main": "./src/index.js",
"repository": "https://github.com/lusakasa/sqorn/tree/master/packages/sqorn-adapter-mysql",
"homepage": "https://sqorn.org",
"dependencies": {}
}
17 changes: 17 additions & 0 deletions packages/sqorn-adapter-pg/package.json
@@ -0,0 +1,17 @@
{
"name": "sqorn-adapter-pg",
"version": "0.0.35",
"description": "Sqorn Adapter Node Postgres",
"author": "Sufyan Dawoodjee <eejdoowad@gmail.com>",
"license": "MIT",
"engines": {
"node": ">=8.0.0"
},
"main": "./src/index.js",
"repository": "https://github.com/lusakasa/sqorn/tree/master/packages/sqorn-adapter-pg",
"homepage": "https://sqorn.org",
"dependencies": {
"pg": "^7.5.0",
"sqorn-sql": "^0.0.35"
}
}
71 changes: 71 additions & 0 deletions packages/sqorn-adapter-pg/src/index.js
@@ -0,0 +1,71 @@
const {
util: { camelCase }
} = require('sqorn-sql')

module.exports = ({ pg, pool }) => {
if (!pool) return undefined
if (!pg) throw Error('Sqorn missing argument "pg"')
monkeyPatchCamelCase(pg)
return {
end: () => pool.end(),
query: async ({ text, args }, trx) => {
const query = { text, values: args }
const client = trx || pool
const result = await client.query(query)
return result.rows
},
transactionCallback: async fn => {
const client = await pool.connect()
try {
await client.query('begin')
const result = await fn(client)
await client.query('commit')
return result
} catch (e) {
await client.query('rollback')
throw e
} finally {
client.release()
}
},
transactionObject: async () => {
const client = await pool.connect()
await client.query('begin')
return {
query: client.query.bind(client),
commit: async () => {
try {
await client.query('commit')
} finally {
client.release()
}
},
rollback: async () => {
try {
await client.query('rollback')
} finally {
client.release()
}
}
}
}
}
}

const monkeyPatchCamelCase = pg => {
if (!pg.__monkeyPatchCamelCase) {
pg.__monkeyPatchCamelCase = true
try {
const { prototype } = pg.Query
const { handleRowDescription } = prototype
prototype.handleRowDescription = function(msg) {
for (const field of msg.fields) {
field.name = camelCase(field.name)
}
return handleRowDescription.call(this, msg)
}
} catch (error) {
throw Error('Failed to monkey patch pg camelCase results')
}
}
}
14 changes: 14 additions & 0 deletions packages/sqorn-adapter-sqlite3/package.json
@@ -0,0 +1,14 @@
{
"name": "sqorn-adapter-sqlite3",
"version": "0.0.35",
"description": "Sqorn Adapter SQLite3",
"author": "Sufyan Dawoodjee <eejdoowad@gmail.com>",
"license": "MIT",
"engines": {
"node": ">=8.0.0"
},
"main": "./src/index.js",
"repository": "https://github.com/lusakasa/sqorn/tree/master/packages/sqorn-adapter-sqlite3",
"homepage": "https://sqorn.org",
"dependencies": {}
}
68 changes: 68 additions & 0 deletions packages/sqorn-adapter-sqlite3/src/index.js
@@ -0,0 +1,68 @@
const pg = require('pg')
const {
util: { camelCase }
} = require('sqorn-sql')

try {
const { prototype } = pg.Query
const { handleRowDescription } = prototype
prototype.handleRowDescription = function(msg) {
for (const field of msg.fields) {
field.name = camelCase(field.name)
}
return handleRowDescription.call(this, msg)
}
} catch (error) {
throw Error('Failed to monkey patch pg camelCase results')
}

const database = ({ connection }) => {
if (!connection) return undefined
const pool = new pg.Pool(connection)
return {
end: () => pool.end(),
query: async ({ text, args }, trx) => {
const query = { text, values: args }
const client = trx || pool
const result = await client.query(query)
return result.rows
},
transactionCallback: async fn => {
const client = await pool.connect()
try {
await client.query('begin')
const result = await fn(client)
await client.query('commit')
return result
} catch (e) {
await client.query('rollback')
throw e
} finally {
client.release()
}
},
transactionObject: async () => {
const client = await pool.connect()
await client.query('begin')
return {
query: client.query.bind(client),
commit: async () => {
try {
await client.query('commit')
} finally {
client.release()
}
},
rollback: async () => {
try {
await client.query('rollback')
} finally {
client.release()
}
}
}
}
}
}

module.exports = database
8 changes: 4 additions & 4 deletions packages/sqorn-core/src/index.js
@@ -1,7 +1,7 @@
/** Returns a new Sqorn SQL query builder */
module.exports = ({ database, dialect }) => (config = {}) => {
module.exports = ({ adapter, dialect }) => (config = {}) => {
const { newContext, queries, methods } = dialect
const client = database(config)
const client = adapter(config)
const reducers = createReducers(methods)
const updateContext = applyReducers(reducers)
reducers.extend = (ctx, args) => {
Expand All @@ -11,7 +11,7 @@ module.exports = ({ database, dialect }) => (config = {}) => {
const chain = createBuilder(builder)
Object.defineProperties(builder, {
...builderProperties({ chain, newContext, updateContext, queries }),
...(client && databaseProperties({ client })),
...(client && adapterProperties({ client })),
...methodProperties({ methods, chain }),
...dialect.properties,
...(client && client.properties)
Expand Down Expand Up @@ -79,7 +79,7 @@ const builderProperties = ({ chain, newContext, updateContext, queries }) => ({
}
})

const databaseProperties = ({ client }) => ({
const adapterProperties = ({ client }) => ({
end: {
value: async function() {
return client.end()
Expand Down
16 changes: 16 additions & 0 deletions packages/sqorn-dialect-mysql/package.json
@@ -0,0 +1,16 @@
{
"name": "sqorn-dialect-mysql",
"version": "0.0.35",
"description": "Sqorn Dialect MySQL",
"author": "Sufyan Dawoodjee <eejdoowad@gmail.com>",
"license": "MIT",
"engines": {
"node": ">=8.0.0"
},
"main": "./src/index.js",
"repository": "https://github.com/lusakasa/sqorn/tree/master/packages/sqorn-dialect-mysql",
"homepage": "https://sqorn.org",
"dependencies": {
"sqorn-sql": "^0.0.35"
}
}
16 changes: 16 additions & 0 deletions packages/sqorn-dialect-postgres/package.json
@@ -0,0 +1,16 @@
{
"name": "sqorn-dialect-postgres",
"version": "0.0.35",
"description": "Sqorn Dialect Postgres",
"author": "Sufyan Dawoodjee <eejdoowad@gmail.com>",
"license": "MIT",
"engines": {
"node": ">=8.0.0"
},
"main": "./src/index.js",
"repository": "https://github.com/lusakasa/sqorn/tree/master/packages/sqorn-dialect-postgres",
"homepage": "https://sqorn.org",
"dependencies": {
"sqorn-sql": "^0.0.35"
}
}

0 comments on commit d2923eb

Please sign in to comment.