Skip to content

Commit 410355b

Browse files
author
Peter Svetlichny
committed
fix(index): escape single quote characters
1 parent f8d2724 commit 410355b

File tree

3 files changed

+10
-5
lines changed

3 files changed

+10
-5
lines changed

src/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ module.exports = class {
7474
if (typeof data[key] === 'object' && data[key] !== null) {
7575
data[key] = JSON.stringify(data[key])
7676
}
77-
vals.push(`'${data[key]}'`)
77+
vals.push(typeof data[key] === 'string' ? `'${data[key].replace(/'/g, "''")}'` : data[key])
7878
acc.push(key)
7979
return acc
8080
}, [])
@@ -122,6 +122,7 @@ module.exports = class {
122122
if (typeof data[key] === 'object' && data[key] !== null) {
123123
data[key] = JSON.stringify(data[key])
124124
}
125+
if (typeof data[key] === 'string') data[key] = data[key].replace(/'/g, "''")
125126
let comma = (i !== len) ? ', ' : ''
126127
changes += `${key}='${data[key]}'${comma}`
127128
i++

test/fixtures/postgres.fixture.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ fixd.postgres = {
99
fname: 'John',
1010
lname: 'Smith',
1111
email: 'jsmith@gmail.com',
12+
age: 30,
1213
address: {
1314
street: '123 Fake St',
1415
city: 'Nashville'
@@ -19,6 +20,7 @@ fixd.postgres = {
1920
fname: [ 'varchar(255)' ],
2021
lname: [ 'varchar(255)' ],
2122
email: [ 'varchar(255)' ],
23+
age: [ 'integer' ],
2224
address: [ 'jsonb' ]
2325
}
2426
}

test/src/index.spec.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ describe('postgres', () => {
99
inst.tableName = 'foo'
1010
// Mock validation method, this is automatically done by the model
1111
inst.validate = (body) => Promise.resolve(body)
12-
// Mock sanitize method, this is automatically done by the model
13-
inst.sanitize = (body) => body
1412
})
1513
after(() => inst.pg.end())
1614
describe('query', () => {
@@ -50,9 +48,12 @@ describe('postgres', () => {
5048
})
5149
})
5250
describe('read', () => {
53-
it('reads all when no query specified', () => {
51+
afterEach(() => { inst.sanitize = null })
52+
it('reads all when no query specified, calling existing sanitize method', () => {
53+
inst.sanitize = sandbox.spy(body => body)
5454
return inst.read()
5555
.then((res) => {
56+
expect(inst.sanitize).to.be.calledOnce()
5657
expect(res.length).to.equal(1)
5758
expect(res[0].email).to.equal(fixd.postgres.testData.email)
5859
})
@@ -73,7 +74,8 @@ describe('postgres', () => {
7374
const query = 'fname=\'John\''
7475
const body = {
7576
fname: 'Bob',
76-
email: 'bsmith@gmail.com'
77+
email: 'bsmith@gmail.com',
78+
age: 31
7779
}
7880
return inst.update(query, body)
7981
.then((res) => {

0 commit comments

Comments
 (0)