Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Pre-release

  • Loading branch information...
commit 5a111089b3f459b0b3d98e124941fec8088a3ffb 1 parent c5da2b5
@1602 authored
View
1  README.md
@@ -0,0 +1 @@
+Read the tests!
View
5 index.js
@@ -39,6 +39,9 @@ function Text() {
Schema.Text = Text;
Schema.prototype.automigrate = function (cb) {
+ if (this.adapter.freezeSchema) {
+ this.adapter.freezeSchema();
+ }
if (this.adapter.automigrate) {
this.adapter.automigrate(cb);
} else {
@@ -410,7 +413,7 @@ AbstractClass.hasMany = function (anotherClass, params) {
AbstractClass.belongsTo = function (anotherClass, params) {
var methodName = params.as;
var fk = params.foreignKey;
- anotherClass.schema.defineForeignKey(anotherClass.modelName, fk);
+ // anotherClass.schema.defineForeignKey(anotherClass.modelName, fk);
this.prototype[methodName] = function (p, cb) {
if (p instanceof AbstractClass) { // acts as setter
this[fk] = p.id;
View
7 lib/mongoose.js
@@ -25,6 +25,13 @@ MongooseAdapter.prototype.define = function (descr) {
this.cache[descr.model.modelName] = {};
};
+MongooseAdapter.prototype.defineForeignKey = function (model, key, cb) {
+ var piece = {};
+ piece[key] = {type: mongoose.Schema.ObjectId, index: true};
+ this._models[model].schema.add(piece);
+ cb(null, String);
+};
+
MongooseAdapter.prototype.setCache = function (model, instance) {
this.cache[model][instance.id] = instance;
};
View
5 lib/neo4j.js
@@ -31,7 +31,8 @@ Neo4j.prototype.node = function find(id, callback) {
Neo4j.prototype.create = function create(model, data, callback) {
data.nodeType = model;
- var node = this.client.createNode(cleanup(data));
+ var node = this.client.createNode();
+ node.data = cleanup(data);
node.save(function (err) {
if (err) {
return callback && callback(err);
@@ -152,7 +153,7 @@ Neo4j.prototype.updateAttributes = function updateAttributes(model, id, data, cb
};
function cleanup(data) {
- if (!data) return {};
+ if (!data) return console.log('no data!') && {};
var res = {};
Object.keys(data).forEach(function (key) {
var v = data[key];
View
45 lib/sequelize.js
@@ -8,6 +8,7 @@ function SequelizeAdapter(schema) {
this.schema = schema;
this._models = {};
this._modelDefinitions = {};
+ this._modelSettings = {};
this.client = new Sequelize(
schema.settings.database,
schema.settings.username,
@@ -24,6 +25,7 @@ SequelizeAdapter.prototype.define = function (d) {
var model = d.model;
var settings = d.settings;
var properties = d.properties;
+ var m = model.modelName;
var translate = {
'String': Sequelize.STRING,
'Text': Sequelize.TEXT,
@@ -38,11 +40,20 @@ SequelizeAdapter.prototype.define = function (d) {
props[property] = translate[properties[property].type.name];
});
- this._modelDefinitions[model.modelName] = props;
- this._models[model.modelName] = this.client.define(model.modelName, props, settings);
+ this._modelDefinitions[m] = props;
+ this._modelSettings[m] = settings;
+};
+
+SequelizeAdapter.prototype.defineSequelizeModel = function (m) {
+ this._models[m] = this.client.define(m, this._modelDefinitions[m], this._modelSettings[m]);
+};
+SequelizeAdapter.prototype.defineForeignKey = function (model, key, cb) {
+ this._modelDefinitions[model][key] = {type: Sequelize.INTEGER, index: true};
+ cb(null, Number);
};
+
SequelizeAdapter.prototype.model = function getModel(name) {
return this._models[name];
};
@@ -85,6 +96,12 @@ SequelizeAdapter.prototype.create = function (model, data, callback) {
.on('failure', callback);
};
+SequelizeAdapter.prototype.freezeSchema = function () {
+ Object.keys(this._modelDefinitions).forEach(function (m) {
+ this.defineSequelizeModel(m);
+ }.bind(this));
+};
+
SequelizeAdapter.prototype.automigrate = function (cb) {
this.client.sync({force: true})
.on('success', cb.bind(this, null))
@@ -136,7 +153,7 @@ SequelizeAdapter.prototype.destroy = function destroy(model, id, callback) {
SequelizeAdapter.prototype.all = function all(model, filter, callback) {
this.model(model).all.on('success', function (data) {
// TODO: filter
- callback(null, data);
+ callback(null, filter ? data.filter(applyFilter(filter)) : data);
}).on('failure', callback);
};
@@ -165,7 +182,27 @@ function applyFilter(filter) {
}
SequelizeAdapter.prototype.destroyAll = function destroyAll(model, callback) {
- callback();
+ var wait;
+ this.model(model)
+ .all
+ .on('success', function (data) {
+ wait = data.length;
+ data.forEach(function (obj) {
+ obj.destroy()
+ .on('success', done)
+ .on('false', error)
+ });
+ }.bind(this))
+ .on('failure', callback);
+
+ var err = null;
+ function done() {
+ if (--wait === 0) callback(err);
+ }
+ function error(e) {
+ err = e;
+ if (--wait === 0) callback(err);
+ }
};
SequelizeAdapter.prototype.count = function count(model, callback) {
View
4 test/common_test.js
@@ -6,11 +6,11 @@ require('./spec_helper').init(exports);
var schemas = {
/*
riak: {},
+ */
sequelize: {
database: 'sequ-test',
username: 'root'
- }
- */
+ },
neo4j: { url: 'http://localhost:7474/' },
mongoose: { url: 'mongodb://localhost/test' },
redis: {},
Please sign in to comment.
Something went wrong with that request. Please try again.