Permalink
Browse files

Release v0.5.0b1

  • Loading branch information...
1 parent 961bb04 commit 2a53c2cb5ab878aae5db166ee88200ded6a83f11 @pgte committed Jan 28, 2011
View
@@ -22,7 +22,7 @@ test: mkdirtmp
operators/test_find_stream operators/test_find_stream_chained \
recovery/collection_recovery_test \
replication/test_master replication/test_slave replication/test_slave_reconnect replication/test_master_temp_roll replication/test_master_seek \
- model/test_create model/test_properties model/test_inspect model/test_validations model/test_destroy model/test_atomic
+ model/test_create model/test_properties model/test_inspect model/test_validations model/test_destroy model/test_atomic model/test_find
benchmark: mkdirtmp mkdirresults
node tools/benchmarks.js benchmark_find benchmark_collection benchmark_collection_filter benchmark_key_map benchmark_key_map_each_with_pos benchmark_indexed_key_map \
@@ -274,6 +274,10 @@ module.exports.create = function(key_map, query) {
}
};
+ chainable.setKeyMap = function(key_map) {
+ finder.key_map = key_map;
+ }
+
return chainable;
};
@@ -23,7 +23,24 @@ var Model = function(database, name, schema, options) {
database.ensure(self.name, options, function(err, coll) {
if (err) { callback(err); return; }
self.collection = coll;
- callback(null);
+ if (self.schema.indexes) {
+ var indexesLeft = self.schema.indexes.length;
+ if (indexesLeft === 0) {
+ callback(null);
+ return;
+ }
+ self.schema.indexes.forEach(function(indexDef) {
+ coll.ensureIndex(indexDef.name, indexDef.options || {ordered: true}, indexDef.fn, function(err) {
+ if (err) { callback(err); return; }
+ if (-- indexesLeft === 0) {
+ callback(null);
+ }
+ });
+ });
+ } else {
+ callback(null);
+ }
+
});
}, function(err) {
self.database.emit('error', err);
@@ -104,48 +121,55 @@ Model.prototype.find = function(query) {
old_all = finder.all;
finder.execute = function(callback) {
- this.initiatedPromise(function() {
- finder.key_map = self.collection;
+ self.initiatedPromise(function() {
+ finder.setKeyMap(self.collection);
old_execute.call(finder, function(err, key, value) {
if (err) { self._error(err); return; }
var doc = self.instantiate(value);
doc.id = key;
callback(doc);
});
});
+ return finder;
};
finder.all = function(callback) {
- this.initiatedPromise(function() {
- finder.key_map = self.collection;
+ self.initiatedPromise(function() {
+ finder.setKeyMap(self.collection);
old_all.call(finder, function(err, records) {
if (err) { self._error(err); return; }
var docs = [];
records.forEach(function(record) {
- docs.push(self.instantiate(record));
+ var doc = self.instantiate(record.value);
+ doc.id = record.key;
+ docs.push(doc);
});
callback(docs);
});
});
+ return finder;
};
finder.stream = function(callback) {
- this.initiatedPromise(function() {
- finder.key_map = self.collection;
+ self.initiatedPromise(function() {
+ finder.setKeyMap(self.collection);
old_stream.call(finder, function(stream) {
stream.on('error', function(err) {
self._error(err);
});
- stream.on('record', function(key, value) {
+ stream.on('record', function(key, record) {
value.id = key;
- stream.emit('document', self.instantiate(value));
+ var doc = self.instantiate(record.value);
+ doc.id = record.key;
+ stream.emit('document', doc);
});
return stream;
});
});
+ return finder;
};
return finder;
};
View
@@ -1,6 +1,6 @@
{ "name" : "alfred"
, "description" : "In-process key-value store"
-, "version" : "0.3.0b3"
+, "version" : "0.5.0b1"
, "homepage" : "http://pgte.github.com/alfred"
, "author" : "Pedro Teixeira <pedro.teixeira@gmail.com> (http://metaduck.com)"
, "contributors" :
View
@@ -1,15 +1,27 @@
var assert = require('assert')
, fs = require('fs')
- , util = require('util');
+ , util = require('util')
+ , path = require('path');
var DB_PATH = __dirname + '/../../tmp/db';
var USER = {name: 'Pedro', age: 30, sex: 'm', address: 'Estrada 1', email: 'pupup@gugu.co.uk', married: true};
module.exports.setup = function(next) {
- fs.readdirSync(DB_PATH).forEach(function(dir) {
- fs.unlinkSync(DB_PATH + '/' + dir);
- });
+ (function removeFilesUnder(dir) {
+ if (path.existsSync(dir)) {
+ fs.readdirSync(dir).forEach(function(path) {
+ var path = dir + '/' + path;
+ var stat = fs.statSync(path);
+ if (stat.isFile()) {
+ fs.unlinkSync(path);
+ } else {
+ removeFilesUnder(path);
+ fs.rmdirSync(path);
+ }
+ });
+ }
+ })(DB_PATH);
next();
};
View
@@ -1,6 +1,7 @@
var assert = require('assert')
, fs = require('fs')
- , util = require('util');
+ , util = require('util')
+ , path = require('path');
var DB_PATH = __dirname + '/../../tmp/db';
@@ -15,9 +16,20 @@ var USERS = [
];
module.exports.setup = function(next) {
- fs.readdirSync(DB_PATH).forEach(function(dir) {
- fs.unlinkSync(DB_PATH + '/' + dir);
- });
+ (function removeFilesUnder(dir) {
+ if (path.existsSync(dir)) {
+ fs.readdirSync(dir).forEach(function(path) {
+ var path = dir + '/' + path;
+ var stat = fs.statSync(path);
+ if (stat.isFile()) {
+ fs.unlinkSync(path);
+ } else {
+ removeFilesUnder(path);
+ fs.rmdirSync(path);
+ }
+ });
+ }
+ })(DB_PATH);
next();
};
View
@@ -1,6 +1,7 @@
var assert = require('assert')
, fs = require('fs')
- , util = require('util');
+ , util = require('util')
+ , path = require('path');
var DB_PATH = __dirname + '/../../tmp/db';
@@ -15,9 +16,20 @@ var USERS = [
];
module.exports.setup = function(next) {
- fs.readdirSync(DB_PATH).forEach(function(dir) {
- fs.unlinkSync(DB_PATH + '/' + dir);
- });
+ (function removeFilesUnder(dir) {
+ if (path.existsSync(dir)) {
+ fs.readdirSync(dir).forEach(function(path) {
+ var path = dir + '/' + path;
+ var stat = fs.statSync(path);
+ if (stat.isFile()) {
+ fs.unlinkSync(path);
+ } else {
+ removeFilesUnder(path);
+ fs.rmdirSync(path);
+ }
+ });
+ }
+ })(DB_PATH);
next();
};
View
@@ -0,0 +1,82 @@
+var assert = require('assert')
+ , fs = require('fs')
+ , util = require('util')
+ , path = require('path');
+
+var DB_PATH = __dirname + '/../../tmp/db';
+
+var USER = {name: 'Pedro', age: 30, sex: 'm', address: 'Estrada 1', email: 'pupup@gugu.co.uk', married: true};
+
+module.exports.setup = function(next) {
+ (function removeFilesUnder(dir) {
+ if (path.existsSync(dir)) {
+ fs.readdirSync(dir).forEach(function(path) {
+ var path = dir + '/' + path;
+ var stat = fs.statSync(path);
+ if (stat.isFile()) {
+ fs.unlinkSync(path);
+ } else {
+ removeFilesUnder(path);
+ fs.rmdirSync(path);
+ }
+ });
+ }
+ })(DB_PATH);
+ next();
+};
+
+module.exports.run = function(next) {
+ var alfred = require('../../lib/alfred');
+
+ var timeout = setTimeout(function() {
+ throw new Error('timeout');
+ }, 5000);
+
+ alfred.open(DB_PATH, function(err, db) {
+ if (err) { next(err); return; }
+
+ db.on('error', function(err) {
+ next(err);
+ });
+
+ var User = db.define('User', {
+ indexes: [
+ {
+ name: 'age',
+ fn: function(user) {
+ return user.age;
+ }
+ }
+ ]
+ });
+ User.property('name');
+ User.property('address');
+ User.property('age', Number);
+ User.property('married', 'boolean');
+ User.property('email', 'string');
+
+ var user = User.new(USER);
+ user.save(function(errors) {
+ assert.ok(errors === null, 'errors is not null');
+ var left = 3;
+ var done = function() {
+ if (-- left == 0) {
+ clearTimeout(timeout);
+ db.close(next);
+ }
+ }
+ User.find({age: {$lte: 30}}) (function(gotUser) {
+ assert.ok(user.equal(gotUser), 'user ('+user.toString()+') and gotten user ('+gotUser.toString()+') are different');
+ done();
+ }).all(function(users) {
+ assert.equal(1, users.length);
+ assert.ok(user.equal(users[0]), 'user ('+user.toString()+') and gotten user ('+users[0].toString()+') are different');
+ done();
+ }).where({age: {$gt: 30}}).all(function(users) {
+ assert.equal(0, users.length);
+ done();
+ });
+ });
+
+ });
+};
View
@@ -2,7 +2,8 @@
var assert = require('assert')
, fs = require('fs')
- , util = require('util');
+ , util = require('util')
+ , path = require('path');
var DB_PATH = __dirname + '/../../tmp/db';
@@ -13,9 +14,20 @@ var EXPECTED_INSPECT = "{ name: 'Pedro',\n\
id: '<<<ID>>>' }";
module.exports.setup = function(next) {
- fs.readdirSync(DB_PATH).forEach(function(dir) {
- fs.unlinkSync(DB_PATH + '/' + dir);
- });
+ (function removeFilesUnder(dir) {
+ if (path.existsSync(dir)) {
+ fs.readdirSync(dir).forEach(function(path) {
+ var path = dir + '/' + path;
+ var stat = fs.statSync(path);
+ if (stat.isFile()) {
+ fs.unlinkSync(path);
+ } else {
+ removeFilesUnder(path);
+ fs.rmdirSync(path);
+ }
+ });
+ }
+ })(DB_PATH);
next();
};
@@ -1,16 +1,28 @@
var assert = require('assert')
, fs = require('fs')
- , util = require('util');
+ , util = require('util')
+ , path = require('path');
var DB_PATH = __dirname + '/../../tmp/db';
var USER = {name: 'Pedro', age: 35, sex: 'm'};
var UNKNOWN_PROP_NAME = 'unknownProperty';
module.exports.setup = function(next) {
- fs.readdirSync(DB_PATH).forEach(function(dir) {
- fs.unlinkSync(DB_PATH + '/' + dir);
- });
+ (function removeFilesUnder(dir) {
+ if (path.existsSync(dir)) {
+ fs.readdirSync(dir).forEach(function(path) {
+ var path = dir + '/' + path;
+ var stat = fs.statSync(path);
+ if (stat.isFile()) {
+ fs.unlinkSync(path);
+ } else {
+ removeFilesUnder(path);
+ fs.rmdirSync(path);
+ }
+ });
+ }
+ })(DB_PATH);
next();
};
Oops, something went wrong.

0 comments on commit 2a53c2c

Please sign in to comment.