Permalink
Browse files

Get the fucking auto-increment tests working

  • Loading branch information...
1 parent 951b214 commit 4d7ec92474153a5e44d1caaac89de9ee6000b705 @mde mde committed Jun 1, 2014
View
@@ -305,13 +305,13 @@ var adapter = model.createAdapter('postgres', {
});
```
-#### model.defaultAdapter
+#### model.config.defaultAdapter
-Use the `defaultAdapter` property on `model` to set a default adapter for all models that don't manually specify
+Use the `defaultAdapter` property on `model.config` to set a default adapter for all models that don't manually specify
`.setAdapter` in the model definition.
```javascript
-model.defaultAdapter = model.createAdapter('postgres', {
+model.config.defaultAdapter = model.createAdapter('postgres', {
host: 'localhost',
username: 'user',
password: 'password',
View
@@ -321,25 +321,25 @@ utils.mixin(Adapter.prototype, new (function () {
return sql;
};
- this._createInsertStatement = function (item, props, useAutoIncrementId) {
+ this._createInsertStatement = function (item, props) {
var sql = ''
, modelName = item.type
, cols = []
, vals = [];
// If using string UUID ids
- if (!useAutoIncrementId) {
+ if (model.config.autoIncrementId) {
+ cols.push(this._columnizePropertyName('id'));
+ vals.push('DEFAULT');
+ }
+ else {
item.id = item.id || utils.string.uuid();
cols.push(this._columnizePropertyName('id'));
vals.push(datatypes.string.serialize(item.id, {
escape: 'sql'
, useQuotes: true
}));
}
- else {
- cols.push(this._columnizePropertyName('id'));
- vals.push('DEFAULT');
- }
for (var p in props) {
def = props[p];
@@ -202,7 +202,7 @@ utils.mixin(Adapter.prototype, new (function () {
, sql = '';
items.forEach(function (item) {
- var statement = self._createInsertStatement(item, props, model.autoIncrementId);
+ var statement = self._createInsertStatement(item, props);
sql += statement;
});
@@ -161,7 +161,7 @@ utils.mixin(Adapter.prototype, new (function () {
, sql = '';
items.forEach(function (item) {
- var statement = self._createInsertStatement(item, props, model.autoIncrementId);
+ var statement = self._createInsertStatement(item, props);
statement = statement.replace(/;$/, ' RETURNING id;');
sql += statement;
});
@@ -181,7 +181,7 @@ utils.mixin(Adapter.prototype, new (function () {
, sql = '';
items.forEach(function (item) {
- var statement = self._createInsertStatement(item, props, model.autoIncrementId);
+ var statement = self._createInsertStatement(item, props);
sql += statement;
});
View
@@ -276,7 +276,7 @@ datatypes = {
, serialize: function (input, options) {
var val
, opts = options || {};
- if (model.useUTC) {
+ if (model.config.useUTC) {
val = utils.date.toUTC(input);
}
else {
@@ -306,7 +306,7 @@ datatypes = {
, serialize: function (input, options) {
var val
, opts = options || {};
- if (model.useUTC) {
+ if (model.config.useUTC) {
val = utils.date.toUTC(input);
}
else {
View
@@ -81,7 +81,7 @@ StandardGenerator.prototype = new (function () {
// Use DB auto-increment
// FIXME: Is this syntax Postgres-specific?
- if (model.autoIncrementId) {
+ if (model.config.autoIncrementId) {
idCol = this.addColumnStatement({
name: 'id'
}, {append: 'BIGSERIAL PRIMARY KEY'});
View
@@ -45,6 +45,7 @@ var util = require('util') // Native Node util module
, model = {}
, EventEmitter = require('events').EventEmitter
, utils = require('utilities')
+ , config = require('./base_config')
, adapters = require('./adapters')
, Query
, query // Lazy-load query; it depends on model/index
@@ -60,15 +61,11 @@ var _foreignKeyCreators = []
utils.mixin(model, new (function () {
+ this.config = config;
this.ModelBase = function () {};
this.adapters = {};
this.loadedAdapters = {};
this.descriptionRegistry = {};
- this.useTimestamps = true;
- this.useUTC = true;
- this.forceCamel = true;
- this.autoIncrementId = false;
- this.defaultAdapter = null;
this.datatypes = null // Lazy-load query; it depends on model/index
this.validators = require('./validators');
@@ -450,7 +447,7 @@ utils.mixin(model, new (function () {
}
// Already existing instance, use update
if (saved) {
- if (model.useTimestamps) {
+ if (model.config.useTimestamps) {
data.updatedAt = new Date();
}
// Re-route to update
@@ -645,7 +642,7 @@ utils.mixin(model, new (function () {
this.validateAndUpdateFromParams(item, params, opts);
- if (this.useTimestamps && !item.createdAt) {
+ if (this.config.useTimestamps && !item.createdAt) {
item.createdAt = new Date();
}
@@ -704,7 +701,7 @@ utils.mixin(model, new (function () {
delete item.errors;
// Convert snake_case names in params to camelCase
- if (this.forceCamel) {
+ if (this.config.forceCamel) {
params = {};
for (var p in passedParams) {
// Allow leading underscores in the keys for pseudo-privates
@@ -853,7 +850,7 @@ utils.mixin(model, new (function () {
this.getAdapterForModel = function (modelName) {
var ctor = this[modelName];
- return ctor && ctor.adapter || this.defaultAdapter;
+ return ctor && ctor.adapter || this.config.defaultAdapter;
};
// FIXME: Move this into an associations lib
@@ -900,10 +897,6 @@ utils.mixin(model, new (function () {
this.localRequireError = msg;
};
- this.setAutoIncrementId = function (val) {
- this.autoIncrementId = val;
- }
-
})());
model.ModelDefinitionBase = function (name) {
@@ -1004,7 +997,7 @@ model.ModelDefinitionBase = function (name) {
}
// Add the base model properties -- these should not be handled by user input
- if (model.useTimestamps) {
+ if (model.config.useTimestamps) {
this.property('createdAt', 'datetime', {isSystem: true});
this.property('updatedAt', 'datetime', {isSystem: true});
}
@@ -1062,7 +1055,7 @@ model.ModelDefinitionBase = function (name) {
if (!reg[ownedModelName].properties[idKey]) {
def = model[ownerModelName];
- datatype = model.autoIncrementId ? 'int' : 'string';
+ datatype = model.config.autoIncrementId ? 'int' : 'string';
reg[ownedModelName].properties[idKey] =
new model.PropertyDescription(idKey, datatype);
@@ -113,7 +113,7 @@ Migration.prototype = new (function () {
definition(col);
- if (model.useTimestamps) {
+ if (model.config.useTimestamps) {
col.cols.createdAt = {
name: 'createdAt'
, datatype: 'datetime'
View
@@ -224,7 +224,7 @@ Query.prototype = new (function () {
// FIXME: How the fuck to handle IDs?
// id isn't in the defined props
if (keyName == 'id') {
- if (model.autoIncrementId) {
+ if (model.config.autoIncrementId) {
datatype = 'int';
}
else {
View
@@ -22,8 +22,6 @@ Event.findByTitle = function (t, cb) {
Event.all({title: t}, cb);
};
-Event = model.register('Event', Event);
-
module.exports.Event = Event;
@@ -12,8 +12,6 @@ var Friendship = function () {
Friendship.prototype.someMethod = function () {
};
-Friendship = model.register('Friendship', Friendship);
-
module.exports.Friendship = Friendship;
@@ -9,8 +9,6 @@ var FunActivity = function () {
FunActivity.prototype.someMethod = function () {
};
-FunActivity = model.register('FunActivity', FunActivity);
-
module.exports.FunActivity = FunActivity;
View
@@ -11,8 +11,6 @@ var Message = function () {
Message.prototype.someMethod = function () {
};
-Message = model.register('Message', Message);
-
module.exports.Message = Message;
@@ -10,8 +10,6 @@ var Participation = function () {
Participation.prototype.someMethod = function () {
};
-Participation = model.register('Participation', Participation);
-
module.exports.Participation = Participation;
View
@@ -16,8 +16,6 @@ var Person = function () {
Person.prototype.someMethod = function () {
};
-Person = model.register('Person', Person);
-
module.exports.Person = Person;
View
@@ -11,8 +11,6 @@ var Photo = function () {
Photo.prototype.someMethod = function () {
};
-Photo = model.register('Photo', Photo);
-
module.exports.Photo = Photo;
@@ -11,8 +11,6 @@ var Schedule = function () {
Schedule.prototype.someMethod = function () {
};
-Schedule = model.register('Schedule', Schedule);
-
module.exports.Schedule = Schedule;
View
@@ -39,6 +39,4 @@ User.findByLogin = function (login, callback) {
User.all({login: login}, callback);
}
-User = model.register('User', User);
-
module.exports.User = User;
View
@@ -4,6 +4,4 @@ var Wooby = function () {
this.property('foo', 'string');
};
-Wooby = model.register('Wooby', Wooby);
-
module.exports.Wooby = Wooby;
View
@@ -14,6 +14,4 @@ var Zooby = function () {
};
-Zooby = model.register('Zooby', Zooby);
-
module.exports.Zooby = Zooby;
@@ -1,12 +1,34 @@
var model = require('../../../lib')
- , fixtures = ['Event', 'Person', 'Participation', 'Message',
- 'Photo', 'Schedule', 'Friendship', 'FunActivity'];
+ , fs = require('fs')
+ , utils = require('utilities')
+ , helpers
+ , fixtureNameList = ['event', 'person', 'participation', 'message',
+ 'photo', 'schedule', 'friendship', 'fun_activity']
+ , fixtures = (function () {
+ var fixtureList = [];
+ fixtureNameList.forEach(function (item) {
+ var ctor
+ , ctorName;
+ ctorName = utils.string.camelize(item, {initialCap: true});
+ ctor = require('../../fixtures/' + item)[ctorName];
+ fixtureList.push({
+ ctorName: ctorName
+ , ctor: ctor
+ });
+ });
+ return fixtureList;
+ })()
+ , fixtureNames = fixtures.map(function (f) {
+ return f.ctorName;
+ });
-module.exports = {
+helpers = {
fixtures: fixtures
+, fixtureNames: fixtureNames
+
, createFixtures: function (cb) {
- var relations = fixtures.slice()
+ var relations = fixtureNames.slice()
, doIt = function () {
var relation = relations.shift()
, items = []
@@ -22,7 +44,9 @@ module.exports = {
}));
});
model[relation].save(items, function (err, data) {
- if (err) { throw err; }
+ if (err) {
+ return cb(err);
+ }
doIt();
});
}
@@ -34,7 +58,7 @@ module.exports = {
}
, deleteFixtures: function (cb) {
- var relations = fixtures.slice()
+ var relations = fixtureNames.slice()
, doIt = function () {
var relation = relations.shift();
if (relation) {
@@ -45,7 +69,7 @@ module.exports = {
ids.push(item.id);
});
model[relation].remove({id: ids}, function (err, data) {
- if (err) { throw err; }
+ if (err) { return cb(err); }
doIt();
});
});
@@ -92,3 +116,4 @@ module.exports = {
};
+module.exports = helpers;
Oops, something went wrong.

0 comments on commit 4d7ec92

Please sign in to comment.