不要怂,就是干,撸起袖子干!

Commit 9612da15 by Jan Aagaard Meier

Merge pull request #3081 from janmeier/jsonfix

Support for plain strings, ints and bools in JSON
2 parents 1e990afd cd1206b1
# Next
- [BUG] Support for plain strings, ints and bools on JSON insert
- [BUG] Fixed regression where `{$in: []}` would result in `IN ()` rather than `IN (NULL)` [#3105](https://github.com/sequelize/sequelize/issues/3105) [#3132](https://github.com/sequelize/sequelize/issues/3132)
- [BUG] Fixed bug where 2 x `belongsToMany` with `foreignKey` but no `otherKey` defined would result in 3 keys instead of 2. [#2991](https://github.com/sequelize/sequelize/issues/2991)
......
......@@ -45,7 +45,8 @@ AbstractDialect.prototype.supports = {
type: false,
using: true,
},
joinTableDependent: true
joinTableDependent: true,
JSON: false,
};
module.exports = AbstractDialect;
......@@ -33,7 +33,8 @@ PostgresDialect.prototype.supports = _.merge(_.cloneDeep(Abstract.prototype.supp
index: {
concurrently: true,
using: 2,
}
},
JSON: true,
});
PostgresDialect.prototype.Query = Query;
......
......@@ -871,7 +871,7 @@ module.exports = (function() {
} else if (DataTypes.ARRAY.is(field.type, DataTypes.HSTORE)) {
return "ARRAY[" + Utils._.map(value, function(v){return "'" + hstore.stringify(v) + "'::hstore";}).join(",") + "]::HSTORE[]";
}
} else if (Utils._.isObject(value) && field && (field.type instanceof DataTypes.JSON || field.type instanceof DataTypes.JSONB)) {
} else if (field && (field.type instanceof DataTypes.JSON || field.type instanceof DataTypes.JSONB)) {
value = JSON.stringify(value);
} else if (Array.isArray(value) && field && DataTypes.ARRAY.is(field.type, DataTypes.JSON)) {
return "ARRAY[" + value.map(function (v) {
......
'use strict';
var Support = require(__dirname + '/../support')
, DataTypes = require(__dirname + '/../../../lib/data-types')
, util = require('util')
, expectsql = Support.expectsql
, current = Support.sequelize
, sql = current.dialect.QueryGenerator
, current = Support.sequelize;
// Notice: [] will be replaced by dialect specific tick/quote character when there is not dialect specific expectation but only a default expectation
if (current.dialect.supports.JSON) {
suite(Support.getTestDialectTeaser('SQL'), function() {
suite('JSON', function () {
suite('escape', function () {
test('plain string', function () {
expectsql(sql.escape('string', { type: new DataTypes.JSON() }), {
default: '\'"string"\''
});
});
test('plain int', function () {
expectsql(sql.escape(123, { type: new DataTypes.JSON() }), {
default: "'123'"
});
});
test('nested object', function () {
expectsql(sql.escape({ some: 'nested', more: { nested: true }, answer: 42 }, { type: new DataTypes.JSON() }), {
default: "'{\"some\":\"nested\",\"more\":{\"nested\":true},\"answer\":42}'"
});
});
test('array of JSON', function () {
expectsql(sql.escape([
{ some: 'nested', more: { nested: true }, answer: 42 },
43,
'joe'
], { type: DataTypes.ARRAY(DataTypes.JSON)}), {
postgres: 'ARRAY[\'{"some":"nested","more":{"nested":true},"answer":42}\',\'43\',\'"joe"\']::JSON[]'
});
});
});
});
});
}
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!