Coverage added to gitignore

This commit is contained in:
Anatoliy Chakkaev 2012-05-29 15:16:24 +04:00
parent e1bd896292
commit 40c9923053
6 changed files with 40 additions and 65 deletions

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
node_modules
doc
coverage.html

File diff suppressed because one or more lines are too long

View File

@ -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);
/*

View File

@ -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;

View File

@ -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 <rpm1602@gmail.com>",
"contributors": [
{ "name": "Anatoliy Chakkaev", "email": "rpm1602@gmail.com" },

View File

@ -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';