@knorm/postgres
Advanced tools
Comparing version 2.0.3 to 2.0.4
@@ -0,1 +1,8 @@ | ||
## [2.0.4](https://github.com/knorm/postgres/compare/v2.0.3...v2.0.4) (2019-07-24) | ||
### Bug Fixes | ||
* do not JSON.parse values already parsed by the postgres driver ([55eaf6b](https://github.com/knorm/postgres/commit/55eaf6b)) | ||
## [2.0.3](https://github.com/knorm/postgres/compare/v2.0.2...v2.0.3) (2019-07-24) | ||
@@ -2,0 +9,0 @@ |
@@ -21,31 +21,11 @@ const { Knorm } = require('@knorm/knorm'); | ||
if ( | ||
(this.type !== 'json' && this.type !== 'jsonb') || | ||
value instanceof knorm.Query.prototype.sql | ||
(this.type === 'json' || this.type === 'jsonb') && | ||
!(value instanceof knorm.Query.prototype.sql) && | ||
options.forSave && | ||
!(this.castors && this.castors.forSave) | ||
) { | ||
return super.cast(value, modelInstance, options); | ||
return JSON.stringify(value); | ||
} | ||
if (value !== null) { | ||
if (options.forSave) { | ||
if (this.castors && this.castors.forSave) { | ||
return super.cast(value, modelInstance, options); | ||
} | ||
return JSON.stringify(value); | ||
} | ||
if (options.forFetch) { | ||
if (this.castors && this.castors.forFetch) { | ||
return super.cast(value, modelInstance, options); | ||
} | ||
// only values coming from the database will be strings | ||
if (typeof value === 'string') { | ||
try { | ||
return JSON.parse(value); | ||
} catch (e) { | ||
// root-level string JSON values are already "parsed" | ||
// TODO: should this error be propagated? | ||
} | ||
} | ||
} | ||
} | ||
return super.cast(value, modelInstance, options); | ||
} | ||
@@ -52,0 +32,0 @@ |
{ | ||
"name": "@knorm/postgres", | ||
"version": "2.0.3", | ||
"version": "2.0.4", | ||
"description": "Postgres plugin for knorm", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -153,7 +153,2 @@ const knorm = require('@knorm/knorm'); | ||
it('does not stringify `null` values', () => { | ||
const field = new Field({ name: 'foo', model: Model, type: 'json' }); | ||
expect(field.cast(null, null, { forSave: true }), 'to be undefined'); | ||
}); | ||
it('does not stringify raw sql values', () => { | ||
@@ -229,22 +224,6 @@ const field = new Field({ name: 'foo', model: Model, type: 'json' }); | ||
describe('forFetch', () => { | ||
it('parses values', () => { | ||
it('does not parse values (already parsed by the postgres driver)', () => { | ||
const field = new Field({ name: 'foo', model: Model, type: 'json' }); | ||
expect( | ||
field.cast(JSON.stringify({ foo: 'bar' }), null, { | ||
forFetch: true | ||
}), | ||
'to equal', | ||
{ foo: 'bar' } | ||
); | ||
}); | ||
it('does not parse `null` values', () => { | ||
const field = new Field({ name: 'foo', model: Model, type: 'json' }); | ||
expect(field.cast(null, { forFetch: true }), 'to be undefined'); | ||
}); | ||
it('does nothing for already parsed values', () => { | ||
const field = new Field({ name: 'foo', model: Model, type: 'json' }); | ||
expect( | ||
field.cast({ foo: 'bar' }, null, { forFetch: true }), | ||
field.cast('foo', null, { forFetch: true }), | ||
'to be undefined' | ||
@@ -254,17 +233,4 @@ ); | ||
it('does nothing for already parsed string values', () => { | ||
const field = new Field({ name: 'foo', model: Model, type: 'json' }); | ||
expect( | ||
field.cast('bar', null, { forFetch: true }), | ||
'to be undefined' | ||
); | ||
}); | ||
it('parses json string values', () => { | ||
const field = new Field({ name: 'foo', model: Model, type: 'json' }); | ||
expect(field.cast('"bar"', null, { forFetch: true }), 'to be', 'bar'); | ||
}); | ||
describe('with a forFetch cast function configured', () => { | ||
it('uses the configred function', () => { | ||
it('uses the configured function', () => { | ||
const field = new Field({ | ||
@@ -857,9 +823,23 @@ name: 'foo', | ||
await new Query(Foo).insert({ id: 1, json: '"foo"' }); | ||
await expect( | ||
new Query(Foo).update({ id: 1, json: '"bar"' }), | ||
new Query(Foo).insert({ id: 1, json: 'foo' }), | ||
'to be fulfilled with sorted rows satisfying', | ||
[{ id: 1, json: 'bar' }] | ||
[{ id: 1, json: 'foo' }] | ||
); | ||
await expect( | ||
new Query(Foo).update({ id: 1, json: { foo: 'foo' } }), | ||
'to be fulfilled with sorted rows satisfying', | ||
[{ id: 1, json: { foo: 'foo' } }] | ||
); | ||
await expect( | ||
new Query(Foo).update({ id: 1, json: '1' }), | ||
'to be fulfilled with sorted rows satisfying', | ||
[{ id: 1, json: '1' }] | ||
); | ||
await expect( | ||
new Query(Foo).update({ id: 1, json: 10 }), | ||
'to be fulfilled with sorted rows satisfying', | ||
[{ id: 1, json: 10 }] | ||
); | ||
await expect( | ||
new Query(Foo).update({ id: 1, json: null }), | ||
@@ -866,0 +846,0 @@ 'to be fulfilled with sorted rows satisfying', |
76791
1915