Skip to content

Commit

Permalink
update code to modeler 0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
Carlos Rodriguez committed Jul 2, 2013
1 parent 33f8bd0 commit c588992
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 19 deletions.
29 changes: 18 additions & 11 deletions index.js
Expand Up @@ -8,24 +8,27 @@ module.exports = function (_opts) {
var table = api.options.table || api.options.name; var table = api.options.table || api.options.name;
var serialPrefix = api.options.serialPrefix || '~s:'; var serialPrefix = api.options.serialPrefix || '~s:';


api._list = function (options, cb) { api._tail = function (limit, cb) {
var offset = options.start || 0; if (limit) limit = ' LIMIT ' + limit;
var limit = options.stop && options.stop - offset || '18446744073709551610'; else limit = '';
client.query('SELECT id FROM ?? ORDER BY created DESC LIMIT ?, ?', client.query('SELECT id FROM ?? ORDER BY __seq DESC' + limit, table, function (err, rows) {
[table, offset, limit], function (err, rows) { if (err) return cb(err);
if (err) return cb(err); cb(null, rows.map(function (row) {
console.log(rows); return row.id;
}); }))
});
}; };
api._save = function (entity, cb) { api._save = function (entity, cb) {
var c = api.copy(entity); var c = api.copy(entity);
var errored = false; var errored = false;
Object.keys(c).forEach(function (k) { Object.keys(c).forEach(function (k) {
if (errored) return; if (errored) return;
if (k === 'created' || k === 'updated') {
c[k] = c[k].getTime();
}
switch (typeof c[k]) { switch (typeof c[k]) {
case 'boolean': case 'boolean':
case 'object': case 'object':
if (c[k].getTime) return; // dates dealt with by node-mysql
try { try {
c[k] = serialPrefix + JSON.stringify(c[k]); c[k] = serialPrefix + JSON.stringify(c[k]);
} }
Expand All @@ -36,7 +39,6 @@ module.exports = function (_opts) {
} }
}); });
if (errored) return; if (errored) return;
//console.log('save', c);
client.query('INSERT INTO ?? SET ? ON DUPLICATE KEY UPDATE ?', [table, c, c], cb); client.query('INSERT INTO ?? SET ? ON DUPLICATE KEY UPDATE ?', [table, c, c], cb);
}; };
api._load = function (id, cb) { api._load = function (id, cb) {
Expand All @@ -55,8 +57,13 @@ module.exports = function (_opts) {
return cb(err); return cb(err);
} }
} }
else if (k === 'created' || k === 'updated') {
entity[k] = new Date(entity[k]);
}
else if (entity[k] === null) {
delete entity[k];
}
}); });
//console.log('load', entity);
cb(null, entity); cb(null, entity);
}); });
}; };
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -4,7 +4,7 @@
"description": "mysql-powered functional entity system", "description": "mysql-powered functional entity system",
"main": "index.js", "main": "index.js",
"dependencies": { "dependencies": {
"modeler": "~0.1.2" "modeler": "~0.2.0"
}, },
"devDependencies": { "devDependencies": {
"mocha": "*", "mocha": "*",
Expand Down
18 changes: 11 additions & 7 deletions test/common.js
Expand Up @@ -33,21 +33,25 @@ setUp = function (done) {
if (err) return done(err); if (err) return done(err);
client.query("CREATE TABLE IF NOT EXISTS `apples` (" client.query("CREATE TABLE IF NOT EXISTS `apples` ("
+ "`id` VARCHAR(255) NOT NULL," + "`id` VARCHAR(255) NOT NULL,"
+ "`created` DATETIME," + "`__seq` BIGINT NOT NULL AUTO_INCREMENT,"
+ "`updated` DATETIME," + "`created` BIGINT,"
+ "`updated` BIGINT,"
+ "`rev` INT," + "`rev` INT,"
+ "`size` VARCHAR(255)," + "`size` VARCHAR(255),"
+ "`condition` TEXT," + "`condition` TEXT,"
+ "`type` VARCHAR(255)," + "`type` VARCHAR(255),"
+ "`internal` VARCHAR(255)," + "`__internal` VARCHAR(255),"
+ "PRIMARY KEY (`id`)" + "PRIMARY KEY (`__seq`),"
+ "UNIQUE KEY (`id`)"
+ ") ENGINE=InnoDB", tryDone); + ") ENGINE=InnoDB", tryDone);
client.query("CREATE TABLE IF NOT EXISTS `oranges` (" client.query("CREATE TABLE IF NOT EXISTS `oranges` ("
+ "`id` VARCHAR(255) NOT NULL," + "`id` VARCHAR(255) NOT NULL,"
+ "`created` DATETIME," + "`__seq` BIGINT NOT NULL AUTO_INCREMENT,"
+ "`updated` DATETIME," + "`created` BIGINT,"
+ "`updated` BIGINT,"
+ "`rev` INT," + "`rev` INT,"
+ "PRIMARY KEY (`id`)" + "PRIMARY KEY (`__seq`),"
+ "UNIQUE KEY (`id`)"
+ ") ENGINE=InnoDB", tryDone); + ") ENGINE=InnoDB", tryDone);
}); });
}); });
Expand Down

0 comments on commit c588992

Please sign in to comment.