Permalink
Browse files

column converters

  • Loading branch information...
1 parent 23e63ef commit fc70bb9f07d733dd2f594451dd07b99131d41f40 @langpavel committed Jun 3, 2012
Showing with 68 additions and 18 deletions.
  1. +21 −0 MIT-LICENSE.txt
  2. +1 −1 index.js
  3. +2 −1 lib/columns/column.js
  4. +10 −10 lib/model.js
  5. +28 −0 lib/myorm.js
  6. +3 −3 package.json
  7. +2 −2 tests/classes/entity.js
  8. +1 −1 tests/columns/00-types.js
View
@@ -0,0 +1,21 @@
+Copyright (c) 2012 Pavel Lang <langpavel@phpskelet.org> (https://github.com/langpavel)
+
+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.
+
View
@@ -1 +1 @@
-exports.Model = require('./lib/model');
+module.exports = require('./lib/myorm');
View
@@ -3,7 +3,7 @@ var util = require('util');
/**
- * abstract class Column - constructor
+ * class Column - constructor
*/
function Column(column_definition) {
this.init(column_definition);
@@ -31,6 +31,7 @@ Column.prototype.init = function(column_definition) {
if(typeof column_definition === 'undefined')
column_definition = {};
+ this.nullable = !! column_definition.nullable;
this.defaultValue = column_definition.defaultValue;
if(typeof this.defaultValue === 'undefined')
this.defaultValue = null;
View
@@ -12,7 +12,7 @@ Model.inherits = function(child, parent) {
child.findBy = Model.findBy;
child.save = Model.save;
child.init = Model.init;
- child.getConverter = Model.getConverter;
+ child.getColumn = Model.getColumn;
};
@@ -115,24 +115,24 @@ Model.prototype.setValues = function(names, values) {
-Model.getConverter = function(column_name) {
+Model.getColumn = function(column_name) {
// this means Model descendant constructor
// converter should be always defined, if not, correct your code not patch this
- return this.columns[column_name].converter;
+ return this.columns[column_name];
}
-Model.prototype.getValue = function(column, version) {
- var converter = this.constructor.getConverter(column);
+Model.prototype.getValue = function(column_name, version) {
+ var column = this.constructor.getColumn(column_name);
if(typeof version === 'undefined')
version = 'current';
- if(typeof this[version] !== 'undefined' && typeof this[version][column] !== 'undefined')
- return this[version][column];
+ if(typeof this[version] !== 'undefined' && typeof this[version][column_name] !== 'undefined')
+ return this[version][column_name];
- return this[version][column] = converter.deserializeValue(this.original[column]);
+ return this[version][column_name] = column.converter.deserializeValue(this.original[column_name]);
};
@@ -265,7 +265,7 @@ Model.insert = function(entity, cb) {
column = columns[i];
sql.push('`'+column+'`');
sqlvals.push('?');
- converter = this.getConverter(column);
+ converter = this.getColumn(column).converter;
values.push(converter.serializeValue(entity.current[column]));
}
entity._saving = true;
@@ -336,7 +336,7 @@ Model.save = function(entity, cb) {
var i,l=columns.length, column;
for(i=0;i<l;i++) {
column = columns[i];
- converter = this.getConverter(column);
+ converter = this.getColumn(column).converter;
value = converter.serializeValue(entity.current[column]);
if(value === entity.original[column]) {
delete entity.current[column];
View
@@ -0,0 +1,28 @@
+/*
+ * This file is package main entry
+ */
+
+// imports ===================================================================
+var package = require('./../package.json');
+var Model = require('./model');
+var columns = require('./columns');
+
+
+
+function MyORM() {};
+
+
+module.exports = exports = MyORM;
+
+
+
+// class static constants ====================================================
+MyORM.version = package.version;
+
+
+// class static classes ======================================================
+MyORM.Model = Model;
+
+
+/// this is actually a hash containing column classes in lowercase by filename
+MyORM.columns = columns;
View
@@ -1,13 +1,13 @@
{
"name": "myorm",
+ "version": "0.1.0alpha1",
"description": "Object-relational mapper (for MySQL at this time)",
"keywords": [
"orm",
"database",
"mysql",
"sql"
],
- "version": "0.0.0",
"author": "Pavel Lang <langpavel@phpskelet.org>",
"homepage": "https://github.com/langpavel/node-myorm",
"repository": {
@@ -17,8 +17,8 @@
"bugs": {
"url": "https://github.com/langpavel/node-myorm/issues"
},
- "main": "./lib/index.js",
- "bin": "./bin/myorm-cli.js",
+ "main": "./lib/myorm",
+ "bin": "./bin/myorm-cli",
"directories": {
"lib": "./lib",
"bin": "./bin"
View
@@ -1,4 +1,4 @@
-var Model = require('../..').Model;
+var Model = require('../../.').Model;
function Entity() {};
@@ -10,7 +10,7 @@ Entity.table = 'SESSION';
Entity.primaryKey = ['id'];
Entity.columns = {
id: { type: 'Integer', notNull: true, sqlType: "int(11)", autoIncrement: true },
- id: { type: Number, notNull: true, sqlType: "double" },
+ id2: { type: Number, notNull: true, sqlType: "double" },
str50: { type: String, length: 50, notNull: true, sqlType: "varchar(50)", comment: "text data varchar 50" },
bool_null: { type: Boolean, sqlType: "tinyint(4)" },
bool: { type: Boolean, notNull: true, defaultValue: false, sqlType: "tinyint(4)" }
@@ -1,4 +1,4 @@
-var columns = require('../../lib/columns');
+var columns = require('../../.').columns;

0 comments on commit fc70bb9

Please sign in to comment.