Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

wip

  • Loading branch information...
commit 33f8bd011c232c5f4b45856f13f04bfae9cd8f59 0 parents
@carlos8f authored
1  .gitignore
@@ -0,0 +1 @@
+node_modules
5 .travis.yml
@@ -0,0 +1,5 @@
+language: node_js
+node_js:
+ - 0.6
+ - 0.8
+ - '0.10'
8 Makefile
@@ -0,0 +1,8 @@
+test:
+ @./node_modules/.bin/mocha \
+ --reporter spec \
+ --bail \
+ --timeout 5s \
+ --require test/common.js
+
+.PHONY: test
37 README.md
@@ -0,0 +1,37 @@
+modeler-mysql
+=============
+
+mysql-powered functional entity system
+
+[![build status](https://secure.travis-ci.org/carlos8f/modeler-mysql.png)](http://travis-ci.org/carlos8f/modeler-mysql)
+
+- - -
+
+### Developed by [Terra Eclipse](http://www.terraeclipse.com)
+Terra Eclipse, Inc. is a nationally recognized political technology and
+strategy firm located in Aptos, CA and Washington, D.C.
+
+- - -
+
+### License: MIT
+
+- Copyright (C) 2013 Carlos Rodriguez (http://s8f.org/)
+- Copyright (C) 2013 Terra Eclipse, Inc. (http://www.terraeclipse.com/)
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is furnished
+to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
68 index.js
@@ -0,0 +1,68 @@
+var modeler = require('modeler');
+
+module.exports = function (_opts) {
+ var api = modeler(_opts);
+
+ if (!api.options.client) throw new Error('must pass a mysql client with options.client');
+ var client = api.options.client;
+ 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._save = function (entity, cb) {
+ var c = api.copy(entity);
+ var errored = false;
+ Object.keys(c).forEach(function (k) {
+ if (errored) return;
+ 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]);
+ }
+ catch (e) {
+ errored = true;
+ return cb(e);
+ }
+ }
+ });
+ 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) {
+ client.query('SELECT * FROM ?? WHERE id = ? LIMIT 1', [table, id], function (err, rows) {
+ if (err) return cb(err);
+ if (!rows.length) return cb(null, null);
+ var entity = rows.pop();
+
+ Object.keys(entity).forEach(function (k) {
+ if (typeof entity[k] === 'string' && entity[k].indexOf(serialPrefix) === 0) {
+ try {
+ entity[k] = JSON.parse(entity[k].replace(serialPrefix, ''));
+ }
+ catch (e) {
+ errored = true;
+ return cb(err);
+ }
+ }
+ });
+ //console.log('load', entity);
+ cb(null, entity);
+ });
+ };
+ api._destroy = function (id, cb) {
+ client.query('DELETE FROM ?? WHERE id = ?', [table, id], cb);
+ };
+
+ return api;
+};
34 package.json
@@ -0,0 +1,34 @@
+{
+ "name": "modeler-mysql",
+ "version": "0.0.0",
+ "description": "mysql-powered functional entity system",
+ "main": "index.js",
+ "dependencies": {
+ "modeler": "~0.1.2"
+ },
+ "devDependencies": {
+ "mocha": "*",
+ "mysql": "~2.0.0-alpha8",
+ "idgen": "~1.3.0"
+ },
+ "scripts": {
+ "test": "make test"
+ },
+ "repository": {
+ "type": "git",
+ "url": "git://github.com/carlos8f/modeler-mysql.git"
+ },
+ "homepage": "https://github.com/carlos8f/modeler-mysql",
+ "keywords": [
+ "mysql",
+ "model",
+ "entity",
+ "orm",
+ "bacon"
+ ],
+ "author": "Carlos Rodriguez <carlos@s8f.org> (http://s8f.org/)",
+ "license": "MIT",
+ "bugs": {
+ "url": "https://github.com/carlos8f/modeler-mysql/issues"
+ }
+}
1  test/basic.js
@@ -0,0 +1 @@
+require('modeler/test/basic');
61 test/common.js
@@ -0,0 +1,61 @@
+assert = require('assert');
+util = require('util');
+modeler = require('../');
+idgen = require('idgen');
+
+var client = require('mysql').createConnection({
+ host: 'localhost',
+ user: 'root',
+ password: ''
+});
+var testDb = 'modeler-mysql-test-' + idgen();
+
+extraOptions = {
+ client: client
+};
+
+setUp = function (done) {
+ var latch = 2, errored = false;
+
+ client.query('CREATE DATABASE ??', testDb, function (err) {
+ if (err) return done(err);
+
+ function tryDone (err) {
+ if (errored) return;
+ if (err) {
+ errored = true;
+ return done();
+ }
+ if (!--latch) done();
+ }
+
+ client.query('USE ??', testDb, function (err) {
+ if (err) return done(err);
+ client.query("CREATE TABLE IF NOT EXISTS `apples` ("
+ + "`id` VARCHAR(255) NOT NULL,"
+ + "`created` DATETIME,"
+ + "`updated` DATETIME,"
+ + "`rev` INT,"
+ + "`size` VARCHAR(255),"
+ + "`condition` TEXT,"
+ + "`type` VARCHAR(255),"
+ + "`internal` VARCHAR(255),"
+ + "PRIMARY KEY (`id`)"
+ + ") ENGINE=InnoDB", tryDone);
+ client.query("CREATE TABLE IF NOT EXISTS `oranges` ("
+ + "`id` VARCHAR(255) NOT NULL,"
+ + "`created` DATETIME,"
+ + "`updated` DATETIME,"
+ + "`rev` INT,"
+ + "PRIMARY KEY (`id`)"
+ + ") ENGINE=InnoDB", tryDone);
+ });
+ });
+};
+
+tearDown = function (done) {
+ client.query('DROP DATABASE ??', testDb, function (err) {
+ assert.ifError(err);
+ extraOptions.client.end(done);
+ });
+};
Please sign in to comment.
Something went wrong with that request. Please try again.