Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

query should throw an error when an SQL error is handled #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions driver.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ module.exports = (app) => {
* @param {Object[]} data - array of data to pass in the prepared query
* @param {function} cb - callback
*/
async function query(sql, data){
async function query(sql, data) {
const client = await pool.connect()
try {
const result = await client.query(sql, data)
return result.rows
} catch(error) {
} catch (error) {
Logger.error('Drivers - postgresql', error)
return error
throw error
} finally {
client.release()
}
Expand All @@ -53,11 +53,12 @@ module.exports = (app) => {
return values.map(val => {
if (typeof val == 'object') {
if (Array.isArray(val))
return JSON.stringify(val).replace('[', '{').replace(']', '}')
return JSON.stringify(val)
.replace('[', '{')
.replace(']', '}')
else
return JSON.stringify(val)
}
else
} else
return val
})
}
Expand Down Expand Up @@ -126,10 +127,14 @@ module.exports = (app) => {
if (!item.data || !item.data.id)
throw Error(`Cannot update an item without an 'id'`)

let columns = Object.keys(_.omit(item.body(), 'id')).map(c => `"${c}"`),
let columns = Object.keys(_.omit(item.body(), 'id'))
.map(c => `"${c}"`), //removing reserved keywords
values = Object.values(_.omit(item.body(), 'id'))

let q = `UPDATE ${item.params.table || (item.params.type + 's')} SET ${columns.map((c, idx) => `${c}=$${idx+1}`).join(',')} WHERE id=$${columns.length+1} RETURNING *`
let q = `UPDATE ${item.params.table || (item.params.type + 's')}
SET ${columns.map((c, idx) => `${c}=$${idx + 1}`).join(',')}
WHERE id=$${columns.length + 1}
RETURNING *`
values = values.concat([item.data.id])

try {
Expand Down
2 changes: 1 addition & 1 deletion test/models/schemas/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const Joi = require('joi')
module.exports = Joi.object().keys({
firstname: Joi.string().optional(),
lastname: Joi.string().optional(),
favorites: Joi.any().optional(),
favorites: Joi.object().optional(),
order: Joi.any(),
preferences: Joi.array()
})
26 changes: 11 additions & 15 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const blockbase = require('blockbase')
let driver
let application

blockbase({root: __dirname}, async app => {
blockbase({ root: __dirname }, async app => {
driver = app.drivers.postgresql = require('../driver')(app)
application = app
})
Expand All @@ -38,13 +38,13 @@ describe('Postgresql driver tests', async function () {
let id
let firstname = 'toto',
lastname = 'robert',
favorites = {a: [1, 34, {'a': 2}]},
favorites = { a: ['a', 34, { 'a': 2 }] },
preferences = [1, 2, 3],
order = 1
it('should save a user', async function () {

const User = application.models.user
const UserModel = new User({firstname, lastname, favorites, order, preferences})
const UserModel = new User({ firstname, lastname, favorites, order, preferences })
try {
let user = await UserModel.save()
should.exist(user)
Expand All @@ -59,8 +59,7 @@ describe('Postgresql driver tests', async function () {
should.exist(user.data.preferences)
should.equal(JSON.stringify(user.data.preferences), JSON.stringify(preferences))
id = user.data.id
}
catch (e) {
} catch (e) {
console.log('e', e)

should.not.exist(e)
Expand All @@ -70,7 +69,7 @@ describe('Postgresql driver tests', async function () {
it('should read a user', async function () {

const User = application.models.user
const UserModel = new User({id})
const UserModel = new User({ id })

try {
let existing = await UserModel.read()
Expand All @@ -85,19 +84,18 @@ describe('Postgresql driver tests', async function () {
should.equal(existing.data.order, order)
should.exist(existing.data.preferences)
should.equal(JSON.stringify(existing.data.preferences), JSON.stringify(preferences))
}
catch (e) {
} catch (e) {
should.not.exist(e)
}
})


it('should update a user', async function () {

let firstname = 'toto2', lastname = 'robert2', favorites = [1, 2, {'a': 2}]
let firstname = 'toto2', lastname = 'robert2'
const User = application.models.user

const UserModel = new User({id, firstname, lastname, favorites})
const UserModel = new User({ id, firstname, lastname, favorites })

try {
let existing = await UserModel.update()
Expand All @@ -115,8 +113,7 @@ describe('Postgresql driver tests', async function () {
should.equal(existing.data.order, order)
should.exist(existing.data.preferences)
should.equal(JSON.stringify(existing.data.preferences), JSON.stringify(preferences))
}
catch (e) {
} catch (e) {
should.not.exist(e)
}
})
Expand All @@ -127,14 +124,13 @@ describe('Postgresql driver tests', async function () {
let firstname = 'toto2', lastname = 'robert2'
const User = application.models.user

const UserModel = new User({id, firstname, lastname})
const UserModel = new User({ id, firstname, lastname })

try {
let done = await UserModel.delete()
should.exist(done)
should.equal(done, true)
}
catch (e) {
} catch (e) {
should.not.exist(e)
}
})
Expand Down