This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

serialise object before saving it

  • Loading branch information...
1 parent e85165d commit d4dcc1257658aab9a00c288e6a45c7809f7c9288 @garrensmith committed Jul 20, 2011
View
19 LICENSE
@@ -0,0 +1,19 @@
+Copyright (c) 2011 Garren Smith <garren.smith@gmail.com>
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
+
View
@@ -8,7 +8,6 @@ var Saveable = function() {
this.beforeSaveFn = function () {};
this.afterSaveFn = function () {};
-
this.save = function (cb) {
var self = this,
db = db_helper.connection();
@@ -27,7 +26,10 @@ var Saveable = function() {
logger.info("Saving %s",self);
- db.save(id,self, function (err, res) {
+ //self.save_attributes();
+ var item = self.serialise();
+
+ db.save(id,item, function (err, res) {
if (err) {
dumpError(err);
return cb(err, null);
@@ -39,7 +41,8 @@ var Saveable = function() {
self.afterSaveFn && self.afterSaveFn(self)
cb(null, self);
- })
+ });
+
};
};
View
@@ -7,6 +7,27 @@ var Saveable = require('./base').Saveable,
var defined_models = {};
var Document = function () {
+ this.save_attributes = function(cb) {
+ cb();
+ };
+
+ this.serialise = function () {
+ var self = this;
+ var serialised_doc = {};
+
+ Object.keys(self).forEach(function (key) {
+ var typeof_key = typeof(self[key]);
+
+ if (typeof_key === 'function') {
+ return;
+ }
+
+ serialised_doc[key] = self[key]
+ });
+
+ return serialised_doc;
+
+ };
}
@@ -52,8 +73,8 @@ var ModelDocument = function (model_type, schema, views) {
var model = new Document();
- model.__proto__.beforeSaveFn = self.beforeSaveFn;
- model.__proto__.afterSaveFn = self.beforeAfterFn;
+ model.beforeSaveFn = self.beforeSaveFn;
+ model.afterSaveFn = self.beforeAfterFn;
for (key in self.schema) {
View
@@ -61,6 +61,7 @@ Will return an array of all documents that fit the criteria
#What needs doing?
## Fixes
* Revision tests on save
+* Query mixin not call create but own afterLoad Function
Plenty:
* Should we allow custom ID's?
@@ -0,0 +1,41 @@
+var describe = require('Jody').describe,
+ cradle = require('cradle'),
+ Model = require('../lib/index'),
+ db;
+
+describe("Loading model with referenced models").
+beforeAll(function (done) {
+ db = new(cradle.Connection)().database('lazyboy_tests');
+
+ var Author = Model.define('Author', {
+ name: String,
+ surname: String
+ });
+
+ var Book = Model.define('Book', {
+ title: String,
+ author: {has_one: Author}
+ });
+
+ Model.load();
+
+ var book = Book.create({title: "Chasm City", author: Author.create({name: "Alastair", surname: "Reynolds"})});
+
+ book.save(function () {
+ done();
+ });
+
+}).
+it("Should load embedded document", function (async) {
+ var Book = Model('Book');
+ Book.where("title", "Chasm City", async(function (err, books) {
+ var book = books[0];
+ console.dir(book);
+ book.author.id.should().notBeEqual(null);
+ book.author.name.should().beEqual("Alastair");
+ book.author.surname.should().beEqual("Reynolds");
+
+ }));
+
+});
+
View
@@ -1,33 +0,0 @@
-var describe = require('Jody').describe,
- Model = require('../lib/index.js');
-
-
-describe("Embedding documents").
- it("Should work", function (async) {
- var Address = Model.define("Address", {street_name: String,
- number: Number
- });
-
- var Person = Model.define("Person",{ name: String,
- address: {hasone: Address}
- });
-
- var new_address = Address.create({street_name: "Main Str",
- number: 21
- });
-
- var new_person = Person.create({name: "Bruce Willis", address: new_address});
-
- new_person.save(async(function (err, res) {
- if (err) throw err;
-
- Person.find(res.id, async(function (err, loaded_person) {
- console.dir(loaded_person);
- loaded_person.name.should().beEqual(new_person.name);
- loaded_person.address.street_name.should().beEqual(new_person.address.street_name);
- loaded_person.address.number.should().beEqual(new_person.address.number);
- }));
-
- }));
- });
-
View
@@ -26,8 +26,9 @@ it("Should save item into db", function (async) {
user.save(async(function (err, result) {
if (err) throw err;
-
+
db.get(user.id,async(function (err, loaded_user) {
+ console.dir(loaded_user);
loaded_user.name.should().beEqual(user.name);
loaded_user.surname.should().beEqual(user.surname);
}));
@@ -0,0 +1,71 @@
+var cradle = require('cradle'),
+ describe = require('Jody').describe,
+ Model = require('../lib/index.js'),
+ db;
+
+
+describe("Embedding documents").
+ beforeAll(function (done) {
+ db = new(cradle.Connection)().database('lazyboy_tests');
+
+
+ var Address = Model.define("Address", {
+ street_name: String,
+ number: Number
+ });
+
+ var Person = Model.define("Person",{
+ name: String,
+ address: {hasone: Address}
+ });
+
+
+ done();
+ }).
+ it("Should save main document with referenced id for embedded doc", function (async) {
+ var Address = Model('Address');
+ var Person = Model('Person');
+
+ var new_address = Address.create({
+ street_name: "Cool Str",
+ number: 24
+ });
+
+ var new_person = Person.create({name: "John Malkovich", address: new_address});
+
+ new_person.save(async(function (err, saved_person) {
+ db.get(saved_person.id, async(function (err, person_doc) {
+ Address.where("number",24, async(function (err, address) {
+
+ person_doc.address_id.should().beEqual(address.id);
+
+ }));
+ }));
+ }));
+
+ }).
+ it("Should load model and referenced model", function (async) {
+ var Person = Model('Person');
+ var Address = Model('Address');
+
+ var new_address = Address.create({
+ street_name: "Main Str",
+ number: 21
+ });
+
+ var new_person = Person.create({name: "Bruce Willis", address: new_address});
+
+ new_person.save(async(function (err, res) {
+ if (err) throw err;
+
+ Person.find(res.id, async(function (err, loaded_person) {
+ //console.dir(loaded_person);
+ loaded_person.name.should().beEqual(new_person.name);
+ loaded_person.address.id.should().notBeEqual(null);
+ loaded_person.address.street_name.should().beEqual(new_person.address.street_name);
+ loaded_person.address.number.should().beEqual(new_person.address.number);
+ }));
+
+ }));
+ });
+
View
@@ -8,12 +8,13 @@ require('Jody').configure.beforeAll(function (done) {
db.destroy(function (err) {
if (err) {
- console.log("error on delete db");
- console.dir(err);
+ //console.log("error on delete db");
+ //console.dir(err);
}
- db.create();
- db_connection.create_connection('lazyboy_tests');
- done();
+ db.create(function () {
+ db_connection.create_connection('lazyboy_tests');
+ done();
+ });
});
});

0 comments on commit d4dcc12

Please sign in to comment.