diff --git a/README.md b/README.md new file mode 100644 index 00000000..c3cb78b1 --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +Read the tests! diff --git a/index.js b/index.js index ac6f3d7f..1f14112b 100644 --- a/index.js +++ b/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; diff --git a/lib/mongoose.js b/lib/mongoose.js index 8224c42a..504e9074 100644 --- a/lib/mongoose.js +++ b/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; }; diff --git a/lib/neo4j.js b/lib/neo4j.js index 8097df40..adcf5332 100644 --- a/lib/neo4j.js +++ b/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]; diff --git a/lib/sequelize.js b/lib/sequelize.js index 2d97fa4c..c9ca0403 100644 --- a/lib/sequelize.js +++ b/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) { diff --git a/test/common_test.js b/test/common_test.js index 49be4827..27b81b1f 100644 --- a/test/common_test.js +++ b/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: {},