This repository has been archived by the owner on Mar 24, 2024. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #40 from eejdoowad/reorg
Reorganization
- Loading branch information
Showing
18 changed files
with
524 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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": {} | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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') | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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": {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
} | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
} | ||
} |
Oops, something went wrong.