Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

update code to modeler 0.2

  • Loading branch information...
commit c58899238a37799d14b0541a8fcbc57c0475afac 1 parent 33f8bd0
@carlos8f authored
Showing with 30 additions and 19 deletions.
  1. +18 −11 index.js
  2. +1 −1  package.json
  3. +11 −7 test/common.js
View
29 index.js
@@ -8,24 +8,27 @@ module.exports = function (_opts) {
var table = api.options.table || api.options.name;
var serialPrefix = api.options.serialPrefix || '~s:';
- api._list = function (options, cb) {
- var offset = options.start || 0;
- var limit = options.stop && options.stop - offset || '18446744073709551610';
- client.query('SELECT id FROM ?? ORDER BY created DESC LIMIT ?, ?',
- [table, offset, limit], function (err, rows) {
- if (err) return cb(err);
- console.log(rows);
- });
+ api._tail = function (limit, cb) {
+ if (limit) limit = ' LIMIT ' + limit;
+ else limit = '';
+ client.query('SELECT id FROM ?? ORDER BY __seq DESC' + limit, table, function (err, rows) {
+ if (err) return cb(err);
+ cb(null, rows.map(function (row) {
+ return row.id;
+ }))
+ });
};
api._save = function (entity, cb) {
var c = api.copy(entity);
var errored = false;
Object.keys(c).forEach(function (k) {
if (errored) return;
+ if (k === 'created' || k === 'updated') {
+ c[k] = c[k].getTime();
+ }
switch (typeof c[k]) {
case 'boolean':
case 'object':
- if (c[k].getTime) return; // dates dealt with by node-mysql
try {
c[k] = serialPrefix + JSON.stringify(c[k]);
}
@@ -36,7 +39,6 @@ module.exports = function (_opts) {
}
});
if (errored) return;
- //console.log('save', c);
client.query('INSERT INTO ?? SET ? ON DUPLICATE KEY UPDATE ?', [table, c, c], cb);
};
api._load = function (id, cb) {
@@ -55,8 +57,13 @@ module.exports = function (_opts) {
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);
});
};
View
2  package.json
@@ -4,7 +4,7 @@
"description": "mysql-powered functional entity system",
"main": "index.js",
"dependencies": {
- "modeler": "~0.1.2"
+ "modeler": "~0.2.0"
},
"devDependencies": {
"mocha": "*",
View
18 test/common.js
@@ -33,21 +33,25 @@ setUp = function (done) {
if (err) return done(err);
client.query("CREATE TABLE IF NOT EXISTS `apples` ("
+ "`id` VARCHAR(255) NOT NULL,"
- + "`created` DATETIME,"
- + "`updated` DATETIME,"
+ + "`__seq` BIGINT NOT NULL AUTO_INCREMENT,"
+ + "`created` BIGINT,"
+ + "`updated` BIGINT,"
+ "`rev` INT,"
+ "`size` VARCHAR(255),"
+ "`condition` TEXT,"
+ "`type` VARCHAR(255),"
- + "`internal` VARCHAR(255),"
- + "PRIMARY KEY (`id`)"
+ + "`__internal` VARCHAR(255),"
+ + "PRIMARY KEY (`__seq`),"
+ + "UNIQUE KEY (`id`)"
+ ") ENGINE=InnoDB", tryDone);
client.query("CREATE TABLE IF NOT EXISTS `oranges` ("
+ "`id` VARCHAR(255) NOT NULL,"
- + "`created` DATETIME,"
- + "`updated` DATETIME,"
+ + "`__seq` BIGINT NOT NULL AUTO_INCREMENT,"
+ + "`created` BIGINT,"
+ + "`updated` BIGINT,"
+ "`rev` INT,"
- + "PRIMARY KEY (`id`)"
+ + "PRIMARY KEY (`__seq`),"
+ + "UNIQUE KEY (`id`)"
+ ") ENGINE=InnoDB", tryDone);
});
});
Please sign in to comment.
Something went wrong with that request. Please try again.