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

Commit 69ab3165 by Jan Aagaard Meier

Support for plain strings, ints and bools in JSON

1 parent b8e3ed59
# 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)
......
......@@ -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;
// Notice: [] will be replaced by dialect specific tick/quote character when there is not dialect specific expectation but only a default expectation
if (Support.getTestDialect() === 'postgres') {
suite(Support.getTestDialectTeaser('SQL'), function() {
suite('JSON', function () {
suite('escape', function () {
test('plain string', function () {
expectsql(sql.escape('string', { type: DataTypes.JSON}), {
default: '\'"string"\''
});
});
test('plain int', function () {
expectsql(sql.escape(123, { type: DataTypes.JSON}), {
default: "'123'"
});
});
test('nested object', function () {
expectsql(sql.escape({ some: 'nested', more: { nested: true }, answer: 42 }, { type: 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!