Skip to content

Commit

Permalink
Pre-release
Browse files Browse the repository at this point in the history
  • Loading branch information
1602 committed Oct 5, 2011
1 parent c5da2b5 commit 5a11108
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 9 deletions.
1 change: 1 addition & 0 deletions README.md
@@ -0,0 +1 @@
Read the tests!
5 changes: 4 additions & 1 deletion index.js
Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
Expand Down
7 changes: 7 additions & 0 deletions lib/mongoose.js
Expand Up @@ -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;
};
Expand Down
5 changes: 3 additions & 2 deletions lib/neo4j.js
Expand Up @@ -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);
Expand Down Expand Up @@ -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];
Expand Down
45 changes: 41 additions & 4 deletions lib/sequelize.js
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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];
};
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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);
};

Expand Down Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions test/common_test.js
Expand Up @@ -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: {},
Expand Down

0 comments on commit 5a11108

Please sign in to comment.