Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
124 additions
and
53 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} | ||
} |
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,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 | ||
} | ||
} |
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 |
---|---|---|
@@ -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) | ||
} | ||
} |
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,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 | ||
} | ||
} |
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
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,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: [] | ||
} | ||
) | ||
}) | ||
}) |
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,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'] | ||
} | ||
) | ||
}) | ||
}) |
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