diff --git a/.gitignore b/.gitignore index 50fce31a..6e8cdaae 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ node_modules doc +coverage.html diff --git a/coverage.html b/coverage.html deleted file mode 100644 index acf06f49..00000000 --- a/coverage.html +++ /dev/null @@ -1,57 +0,0 @@ - - - - - - - - - -
-
30% [281/548]
-
49% [77/161]
-
50% [4/12]
-
- - diff --git a/lib/abstract-class.js b/lib/abstract-class.js index 1a098888..bf540321 100644 --- a/lib/abstract-class.js +++ b/lib/abstract-class.js @@ -60,6 +60,12 @@ AbstractClass.prototype._initProperties = function (data, applySetters) { )) }); + if (properties[attr].type.name === 'JSON' && typeof this[_attr] === 'string') { + try { + this[_attr] = JSON.parse(this[_attr]); + } catch (e) { } + } + // Public setters and getters Object.defineProperty(this, attr, { get: function () { @@ -125,7 +131,19 @@ AbstractClass.defineProperty = function (prop, params) { AbstractClass.whatTypeName = function (propName) { var ds = this.schema.definitions[this.modelName]; - return ds.properties[propName].type.name; + return ds.properties[propName] && ds.properties[propName].type.name; +}; + +AbstractClass._forDB = function (data) { + var res = {}; + Object.keys(data).forEach(function (propName) { + if (this.whatTypeName(propName) === 'JSON') { + res[propName] = JSON.stringify(data[propName]); + } else { + res[propName] = data[propName]; + } + }.bind(this)); + return res; }; AbstractClass.prototype.whatTypeName = function (propName) { @@ -179,7 +197,7 @@ AbstractClass.create = function (data, callback) { function create() { obj.trigger('create', function (done) { - this._adapter().create(modelName, data, function (err, id) { + this._adapter().create(modelName, this.constructor._forDB(data), function (err, id) { if (id) { defineReadonlyProp(obj, 'id', id); addToCache(this.constructor, obj); @@ -447,7 +465,7 @@ AbstractClass.prototype.save = function (options, callback) { var inst = this; if (inst.id) { inst.trigger('update', function (updateDone) { - inst._adapter().save(modelName, data, function (err) { + inst._adapter().save(modelName, inst.constructor._forDB(data), function (err) { if (err) { console.log(err); } else { @@ -574,7 +592,7 @@ AbstractClass.prototype.updateAttributes = function updateAttributes(data, cb) { data[key] = inst[key]; }); - inst._adapter().updateAttributes(model, inst.id, data, function (err) { + inst._adapter().updateAttributes(model, inst.id, inst.constructor._forDB(data), function (err) { if (!err) { inst._initProperties(data, false); /* diff --git a/lib/adapters/redis.js b/lib/adapters/redis.js index 97599240..c416c34b 100644 --- a/lib/adapters/redis.js +++ b/lib/adapters/redis.js @@ -367,6 +367,7 @@ function applyFilter(filter) { var keys = Object.keys(filter.where || {}); return function (obj) { var pass = true; + if (!obj) return false; keys.forEach(function (key) { if (!test(filter.where[key], obj[key])) { pass = false; diff --git a/package.json b/package.json index 3630ea0c..649cebd7 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "jugglingdb", "description": "ORM for every database: redis, mysql, neo4j, mongodb, postgres, sqlite", - "version": "0.1.8", + "version": "0.1.10", "author": "Anatoliy Chakkaev ", "contributors": [ { "name": "Anatoliy Chakkaev", "email": "rpm1602@gmail.com" }, diff --git a/test/common_test.js b/test/common_test.js index 98520adc..149066eb 100644 --- a/test/common_test.js +++ b/test/common_test.js @@ -53,13 +53,14 @@ function testOrm(schema) { it('should define class', function (test) { User = schema.define('User', { - name: { type: String, index: true }, - email: { type: String, index: true }, + name: { type: String, index: true }, + email: { type: String, index: true }, bio: Text, approved: Boolean, joinedAt: Date, age: Number, - passwd: String + passwd: String, + settings: { type: { name: 'JSON' }} }); Post = schema.define('Post', { @@ -305,6 +306,17 @@ function testOrm(schema) { test.done(); }); + it('should serialize JSON type', function (test) { + User.create({settings: {hello: 'world'}}, function (err, user) { + test.ok(user.id); + test.equal(user.settings.hello, 'world'); + User.find(user.id, function (err, u) { + test.equal(u.settings.hello, 'world'); + test.done(); + }); + }); + }); + it('should update single attribute', function (test) { Post.create({title: 'title', content: 'content', published: true}, function (err, post) { post.content = 'New content';