Skip to content
This repository was archived by the owner on Aug 5, 2021. It is now read-only.

Commit 0aee8c4

Browse files
author
Philipp Waldmann
committed
add toJson() method to Record and Collection
closes #7
1 parent 887fa9a commit 0aee8c4

File tree

4 files changed

+127
-1
lines changed

4 files changed

+127
-1
lines changed

lib/base/json.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
var Utils = require('../utils');
2+
3+
exports.record = {
4+
/**
5+
* Returns an object which represent the record in plain json
6+
*
7+
* @area Record
8+
* @method toJson
9+
*
10+
* @return {object}
11+
*/
12+
toJson: function(){
13+
var tmp = Utils.clone(this.attributes);
14+
15+
for(var i in this.model.definition.relations){
16+
var relation = this.model.definition.relations[i];
17+
if(this[relation.name]){
18+
tmp[relation.name] = this[relation.name].toJson();
19+
}
20+
}
21+
22+
return tmp;
23+
}
24+
};
25+
26+
27+
exports.chain = {
28+
/**
29+
* Returns an array of objects which represent the records in plain json
30+
*
31+
* @area Collection
32+
* @method toJson
33+
*
34+
* @return {array}
35+
*/
36+
toJson: function(){
37+
var tmp = [];
38+
this.each(function(record){
39+
tmp.push(record.toJson());
40+
});
41+
return tmp;
42+
}
43+
};

lib/utils.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,14 @@ exports.loopProperties = function(obj, fn, scope){
1919
}
2020
};
2121

22+
exports.clone = function(obj){
23+
var tmp = {};
24+
Object.keys(obj).forEach(function (name) {
25+
tmp[name] = obj[name];
26+
});
27+
return tmp;
28+
}
29+
2230
exports.require = function(path, options){
2331
if(!options) options = {};
2432
if(!(path instanceof Array)) path = [path];

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "openrecord",
3-
"version": "0.2.4",
3+
"version": "0.2.5",
44
"description": "Active record like ORM for nodejs",
55
"keywords": ["orm", "record", "sql", "sqlite3", "postgres", "pg", "mysql", "database", "activerecord"],
66
"author": "Philipp Waldmann <philipp.waldmann@s-team.at>",

test/base/json-test.js

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
var should = require('should');
2+
3+
var Store = require('../../lib/store');
4+
5+
describe('JSON', function(){
6+
var store = new Store();
7+
8+
store.Model('User', function(){
9+
this.attribute('login');
10+
this.hasMany('posts')
11+
});
12+
13+
store.Model('Post', function(){
14+
this.attribute('title');
15+
this.belongsTo('user');
16+
});
17+
18+
var User = store.Model('User');
19+
20+
var posts = [{title:'foo'}, {title: 'bar'}];
21+
22+
var phil = User.new({login: 'phil', foo: 'bar'});
23+
var michl = User.new({login: 'michl', foo: 'bar', posts:posts});
24+
25+
Collection = User.chain().add(phil).add(michl);
26+
27+
describe('Record toJson()', function(){
28+
29+
it('method exists', function(){
30+
phil.toJson.should.be.a.Function;
31+
})
32+
33+
it('returns a new object', function(){
34+
var json = phil.toJson();
35+
json.should.not.be.eql(phil);
36+
json.login.should.be.equal('phil');
37+
should.not.exist(json.foo);
38+
json.posts.should.be.eql([]);
39+
});
40+
41+
it('returns a new object with relations', function(){
42+
var json = michl.toJson();
43+
json.should.not.be.eql(michl);
44+
json.login.should.be.equal('michl');
45+
should.not.exist(json.foo);
46+
json.posts.should.be.eql(posts);
47+
michl.posts.should.not.be.eql(json.posts);
48+
});
49+
50+
});
51+
52+
53+
describe('Collection toJson()', function(){
54+
55+
it('method exists', function(){
56+
Collection.toJson.should.be.a.Function;
57+
})
58+
59+
it('returns a new object', function(){
60+
var json = Collection.toJson();
61+
json.should.not.be.eql(Collection);
62+
json.should.be.instanceof(Array);
63+
json[0].login.should.be.equal('phil');
64+
});
65+
66+
it('returns a new object with relations', function(){
67+
var json = Collection.toJson();
68+
json[1].login.should.be.equal('michl');
69+
json[1].posts.should.be.eql(posts);
70+
michl.posts.should.not.be.eql(json[1].posts);
71+
});
72+
73+
});
74+
75+
});

0 commit comments

Comments
 (0)