Skip to content

Commit

Permalink
Merge 3dab814 into 0221590
Browse files Browse the repository at this point in the history
  • Loading branch information
Sharaal committed Nov 13, 2019
2 parents 0221590 + 3dab814 commit d2da096
Show file tree
Hide file tree
Showing 11 changed files with 124 additions and 53 deletions.
10 changes: 5 additions & 5 deletions src/manipulation-methods/sql.delete.js
@@ -1,11 +1,11 @@
module.exports = sql => {
sql.delete = async (...params) => {
if (typeof params[0] === 'string' || Array.isArray(params[0])) {
const [table, conditions] = params
params = [sql`DELETE FROM ${sql.table(table)}${sql.if(conditions, sql` WHERE ${sql.conditions(conditions)}`)}`]
const [, conditions] = params
if (!conditions) {
throw new Error('Expects to have conditions for the delete')
}
}
const [query] = params
const result = await sql.query(query)
return result.rowCount
return sql.deleteAll(...params)
}
}
11 changes: 11 additions & 0 deletions src/manipulation-methods/sql.deleteAll.js
@@ -0,0 +1,11 @@
module.exports = sql => {
sql.deleteAll = async (...params) => {
if (typeof params[0] === 'string' || Array.isArray(params[0])) {
const [table, conditions] = params
params = [sql`DELETE FROM ${sql.table(table)}${sql.if(conditions, sql` WHERE ${sql.conditions(conditions)}`)}`]
}
const [query] = params
const result = await sql.query(query)
return result.rowCount
}
}
10 changes: 5 additions & 5 deletions src/manipulation-methods/sql.update.js
@@ -1,11 +1,11 @@
module.exports = sql => {
sql.update = async (...params) => {
if (typeof params[0] === 'string' || Array.isArray(params[0])) {
const [table, updates, conditions] = params
params = [sql`UPDATE ${sql.table(table)} SET ${sql.assignments(updates)}${sql.if(conditions, sql` WHERE ${sql.conditions(conditions)}`)}`]
const [, , conditions] = params
if (!conditions) {
throw new Error('Expects to have conditions for the update')
}
}
const [query] = params
const result = await sql.query(query)
return result.rowCount
return sql.updateAll(...params)
}
}
11 changes: 11 additions & 0 deletions src/manipulation-methods/sql.updateAll.js
@@ -0,0 +1,11 @@
module.exports = sql => {
sql.updateAll = async (...params) => {
if (typeof params[0] === 'string' || Array.isArray(params[0])) {
const [table, updates, conditions] = params
params = [sql`UPDATE ${sql.table(table)} SET ${sql.assignments(updates)}${sql.if(conditions, sql` WHERE ${sql.conditions(conditions)}`)}`]
}
const [query] = params
const result = await sql.query(query)
return result.rowCount
}
}
2 changes: 2 additions & 0 deletions src/sql.js
Expand Up @@ -26,8 +26,10 @@ module.exports = (options = {}) => {

const extensions = [
'./manipulation-methods/sql.delete.js',
'./manipulation-methods/sql.deleteAll.js',
'./manipulation-methods/sql.insert.js',
'./manipulation-methods/sql.update.js',
'./manipulation-methods/sql.updateAll.js',
'./selection-methods/sql.any.js',
'./selection-methods/sql.many.js',
'./selection-methods/sql.manyOrNone.js',
Expand Down
4 changes: 2 additions & 2 deletions test/integration/sql.js
Expand Up @@ -11,11 +11,11 @@ describe('sql', () => {
client = require('../../pg')
await client.connect()
sql = require('../../')({ client })
await sql.delete('users')
await sql.deleteAll('users')
})

after(async () => {
await sql.delete('users')
await sql.deleteAll('users')
await client.end()
})

Expand Down
24 changes: 9 additions & 15 deletions test/unit/manipulation-methods/sql.delete.js
Expand Up @@ -32,26 +32,20 @@ describe('sql.delete', () => {
)
})

it('delete rows with the shorthand without any conditions', async () => {
const expectedRowCount = 5
it('delete rows with the shorthand without any conditions throws exception', async () => {
const client = {
query: sinon.fake.returns(Promise.resolve({ rowCount: expectedRowCount }))
query: sinon.fake()
}

sql.client = client
const actualRowCount = await sql.delete('table')

assert.equal(actualRowCount, expectedRowCount)

assert(client.query.calledOnce)
try {
await sql.delete('table')
assert(false)
} catch (e) {
assert.equal(e.message, 'Expects to have conditions for the delete')
}

assert.deepEqual(
client.query.getCall(0).args[0],
{
text: 'DELETE FROM "table"',
values: []
}
)
assert.equal(client.query.callCount, 0)
})

it('delete rows with the shorthand from a table with schema', async () => {
Expand Down
31 changes: 31 additions & 0 deletions test/unit/manipulation-methods/sql.deleteAll.js
@@ -0,0 +1,31 @@
const assert = require('power-assert')
const sinon = require('sinon')

describe('sql.deleteAll', () => {
let sql
beforeEach(() => {
sql = require('../../../')()
})

it('delete rows with the shorthand without any conditions', async () => {
const expectedRowCount = 5
const client = {
query: sinon.fake.returns(Promise.resolve({ rowCount: expectedRowCount }))
}

sql.client = client
const actualRowCount = await sql.deleteAll('table')

assert.equal(actualRowCount, expectedRowCount)

assert(client.query.calledOnce)

assert.deepEqual(
client.query.getCall(0).args[0],
{
text: 'DELETE FROM "table"',
values: []
}
)
})
})
34 changes: 14 additions & 20 deletions test/unit/manipulation-methods/sql.update.js
Expand Up @@ -33,29 +33,23 @@ describe('sql.update', () => {
)
})

it('update rows with the shorthand without any conditions', async () => {
const expectedRowCount = 5
it('update rows with the shorthand without any conditions throws exception', async () => {
const client = {
query: sinon.fake.returns(Promise.resolve({ rowCount: expectedRowCount }))
query: sinon.fake()
}

sql.client = client
const actualRowCount = await sql.update(
'table',
{ column1: 'value1', column2: 'value2' }
)

assert.equal(actualRowCount, expectedRowCount)

assert(client.query.calledOnce)
try {
await sql.update(
'table',
{ column1: 'value1', column2: 'value2' }
)
assert(false)
} catch (e) {
assert.equal(e.message, 'Expects to have conditions for the update')
}

assert.deepEqual(
client.query.getCall(0).args[0],
{
text: 'UPDATE "table" SET "column1" = $1, "column2" = $2',
values: ['value1', 'value2']
}
)
assert.equal(client.query.callCount, 0)
})

it('update rows with the shorthand in a table with schema', async () => {
Expand Down Expand Up @@ -92,7 +86,7 @@ describe('sql.update', () => {

sql.client = client
const actualRowCount = await sql.update(
sql`UPDATE "table" SET "column" = 'value'`
sql`UPDATE "table" SET "columnA" = 'valueA' WHERE "columnB" = 'valueB'`
)

assert.equal(actualRowCount, expectedRowCount)
Expand All @@ -102,7 +96,7 @@ describe('sql.update', () => {
assert.deepEqual(
client.query.getCall(0).args[0],
{
text: 'UPDATE "table" SET "column" = \'value\'',
text: 'UPDATE "table" SET "columnA" = \'valueA\' WHERE "columnB" = \'valueB\'',
values: []
}
)
Expand Down
34 changes: 34 additions & 0 deletions test/unit/manipulation-methods/sql.updateAll.js
@@ -0,0 +1,34 @@
const assert = require('power-assert')
const sinon = require('sinon')

describe('sql.updateAll', () => {
let sql
beforeEach(() => {
sql = require('../../../')()
})

it('update rows with the shorthand without any conditions', async () => {
const expectedRowCount = 5
const client = {
query: sinon.fake.returns(Promise.resolve({ rowCount: expectedRowCount }))
}

sql.client = client
const actualRowCount = await sql.updateAll(
'table',
{ column1: 'value1', column2: 'value2' }
)

assert.equal(actualRowCount, expectedRowCount)

assert(client.query.calledOnce)

assert.deepEqual(
client.query.getCall(0).args[0],
{
text: 'UPDATE "table" SET "column1" = $1, "column2" = $2',
values: ['value1', 'value2']
}
)
})
})
6 changes: 0 additions & 6 deletions test/unit/sql.query.js
Expand Up @@ -36,18 +36,12 @@ describe('sql.query', () => {
})

it('throw an exception "sql.query()" is used without assign a client', async () => {
const client = {
query: sinon.fake()
}

try {
sql.query('SELECT * FROM "table"')
assert(false)
} catch (e) {
assert.equal(e.message, 'Missing assignment of the initialized pg client to "sql.client"')
}

assert.equal(client.query.callCount, 0)
})

it('throw an exception if a string is used as query', async () => {
Expand Down

0 comments on commit d2da096

Please sign in to comment.