Skip to content

Commit

Permalink
Conditions should be optional in all query methods #30
Browse files Browse the repository at this point in the history
  • Loading branch information
Christoph Herrmann committed Aug 13, 2019
1 parent 83240c8 commit 57599e0
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 4 deletions.
8 changes: 4 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ module.exports = ({
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)} WHERE ${sql.conditions(conditions)}`]
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)
Expand All @@ -88,7 +88,7 @@ module.exports = ({
sql.delete = async (...params) => {
if (typeof params[0] === 'string' || Array.isArray(params[0])) {
const [table, conditions] = params
params = [sql`DELETE FROM ${sql.table(table)} WHERE ${sql.conditions(conditions)}`]
params = [sql`DELETE FROM ${sql.table(table)}${sql.if(conditions, sql` WHERE ${sql.conditions(conditions)}`)}`]
}
const [query] = params
const result = await sql.query(query)
Expand All @@ -102,7 +102,7 @@ module.exports = ({
conditions = columns
columns = ['*']
}
params = [sql`SELECT ${sql.columns(columns)} FROM ${sql.table(table)} WHERE ${sql.conditions(conditions)}`]
params = [sql`SELECT ${sql.columns(columns)} FROM ${sql.table(table)}${sql.if(conditions, sql` WHERE ${sql.conditions(conditions)}`)}`]
}
const [query] = params
const result = await sql.query(query)
Expand Down Expand Up @@ -193,7 +193,7 @@ module.exports = ({
)
}

function pairs (pairs, separator) {
function pairs (pairs = {}, separator) {
return parameterPosition => {
const queries = []
for (const column of Object.keys(pairs)) {
Expand Down
22 changes: 22 additions & 0 deletions test/manipulation-methods/sql.delete.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,28 @@ describe('sql.delete', () => {
)
})

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.delete('table')

assert.equal(actualRowCount, expectedRowCount)

assert(client.query.calledOnce)

testSql(
client.query.getCall(0).args[0],
{
text: 'DELETE FROM "table"',
parameters: []
}
)
})

it('delete rows with the shorthand from a table with schema', async () => {
const expectedRowCount = 5
const client = {
Expand Down
28 changes: 28 additions & 0 deletions test/manipulation-methods/sql.update.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,34 @@ describe('sql.update', () => {
)
})

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.update(
'table',
{ column1: 'value1', column2: 'value2' }
)

assert.equal(actualRowCount, expectedRowCount)

assert(client.query.calledOnce)

testSql(
client.query.getCall(0).args[0],
{
text: {
0: 'UPDATE "table" SET "column1" = $1, "column2" = $2',
5: 'UPDATE "table" SET "column1" = $6, "column2" = $7'
},
parameters: ['value1', 'value2']
}
)
})

it('update rows with the shorthand in a table with schema', async () => {
const expectedRowCount = 5
const client = {
Expand Down
22 changes: 22 additions & 0 deletions test/selection-methods/sql.any.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,28 @@ describe('sql.any', () => {
)
})

it('supports shorthands to select all columns without any conditions', async () => {
const expectedRows = []
const client = {
query: sinon.fake.returns(Promise.resolve({ rows: expectedRows }))
}

sql.client = client
const actualRows = await sql.any('table')

assert.deepEqual(actualRows, expectedRows)

assert(client.query.calledOnce)

testSql(
client.query.getCall(0).args[0],
{
text: 'SELECT "*" FROM "table"',
parameters: []
}
)
})

it('supports shorthands to select all columns from a table with schema', async () => {
const expectedRows = []
const client = {
Expand Down

0 comments on commit 57599e0

Please sign in to comment.