Skip to content

Commit

Permalink
Modify schema types
Browse files Browse the repository at this point in the history
  • Loading branch information
biggora committed Apr 6, 2015
1 parent 4233b13 commit b8efd83
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 52 deletions.
8 changes: 8 additions & 0 deletions lib/adapters/mysql.js
Original file line number Diff line number Diff line change
Expand Up @@ -719,10 +719,18 @@ function datatype(p) {
case 'text':
dt = 'TEXT';
break;
case 'int':
case 'integer':
case 'number':
var ftype = (parseFloat(p.limit) > 11) ? "BIGINT" : "INT";
dt = ftype + '(' + (p.limit || 11) + ')';
break;
case 'float':
case 'double':
dt = 'FLOAT';
case 'real':
dt = 'REAL';
break;
case 'date':
dt = 'DATETIME';
break;
Expand Down
26 changes: 15 additions & 11 deletions lib/adapters/postgres.js
Original file line number Diff line number Diff line change
Expand Up @@ -653,26 +653,31 @@ function escape(val) {
});
return "E'" + val + "'";
}
;


function datatype(p) {
switch (p.type.name) {
switch ((p.type.name || 'string').toLowerCase()) {
default:
case 'String':
case 'Varchar':
case 'string':
case 'varchar':
return 'varchar';
case 'JSON':
case 'Text':
case 'json':
case 'text':
return 'text';
case 'Number':
case 'int':
case 'integer':
case 'number':
return 'integer';
case 'Date':
case 'real':
case 'float':
case 'double':
return 'double precision';
case 'date':
return 'timestamp';
case 'Boolean':
case 'boolean':
return 'boolean';
}
}
;

function mapPostgresDatatypes(typeName) {
//TODO there are a lot of synonymous type names that should go here-- this is just what i've run into so far
Expand All @@ -685,7 +690,6 @@ function mapPostgresDatatypes(typeName) {
return typeName;
}
}
;

function propertyHasNotBeenDeleted(model, propName) {
return !!this._models[model].properties[propName];
Expand Down
7 changes: 6 additions & 1 deletion lib/adapters/sqlite3.js
Original file line number Diff line number Diff line change
Expand Up @@ -497,9 +497,14 @@ function datatype(p) {
case 'json':
case 'text':
return 'TEXT';
case 'number':
case 'int':
case 'integer':
case 'number':
return 'INT(' + (p.limit || 11) + ')';
case 'real':
case 'float':
case 'double':
return 'REAL';
case 'date':
return 'DATETIME';
case 'boolean':
Expand Down
79 changes: 40 additions & 39 deletions lib/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,16 @@ function Schema(name, settings) {
// create blank models pool
schema.models = {};
schema.definitions = {};
// define custom types
schema.Text = function Text() {
};
schema.JSON = function JSON() {
};
schema.Date = Date;
// define schema types
schema.Text = function Text() {};
schema.JSON = schema.Json = function JSON() {};
schema.Float = function Float() {};
schema.Real = schema.Double = function Real() {};
schema.Int = schema.Integer = function Integer() {};
schema.Date = schema.Timestamp = Date;
schema.Boolean = schema.Tinyint = Boolean;
schema.Number = Number;
schema.String = String;
schema.Boolean = Boolean;
schema.String = schema.Varchar = String;
// and initialize schema using adapter
// this is only one initialization entry point of adapter
// this module should define `adapter` member of `this` (schema)
Expand All @@ -110,21 +111,21 @@ function Schema(name, settings) {
}
}

adapter.initialize(schema, function() {
adapter.initialize(schema, function () {

// we have an adaper now?
if (!schema.adapter) {
throw new Error('Adapter is not defined correctly: it should create `adapter` member of schema');
}

schema.adapter.log = function(query, start) {
schema.adapter.log = function (query, start) {
schema.log(query, start);
};

schema.adapter.logger = function(query) {
schema.adapter.logger = function (query) {
var t1 = Date.now();
var log = schema.log;
return function(q) {
return function (q) {
log(q || query, t1);
};
};
Expand Down Expand Up @@ -175,7 +176,7 @@ Schema.prototype.define = function defineClass(className, properties, settings)
var schema = this;
var args = slice.call(arguments);

if (!className){
if (!className) {
throw new Error('Class name required');
}
if (args.length === 1)
Expand Down Expand Up @@ -224,26 +225,26 @@ Schema.prototype.define = function defineClass(className, properties, settings)
settings: settings
});

NewClass.prototype.__defineGetter__('id', function() {
NewClass.prototype.__defineGetter__('id', function () {
return this.__data.id;
});

properties.id = properties.id || {type: Number};

NewClass.forEachProperty = function(cb) {
NewClass.forEachProperty = function (cb) {
Object.keys(properties).forEach(cb);
};

NewClass.registerProperty = function(attr) {
NewClass.registerProperty = function (attr) {
Object.defineProperty(NewClass.prototype, attr, {
get: function() {
get: function () {
if (NewClass.getter[attr]) {
return NewClass.getter[attr].call(this);
} else {
return this.__data[attr];
}
},
set: function(value) {
set: function (value) {
if (NewClass.setter[attr]) {
NewClass.setter[attr].call(this, value);
} else {
Expand All @@ -254,15 +255,15 @@ Schema.prototype.define = function defineClass(className, properties, settings)
enumerable: true
});

NewClass.prototype.__defineGetter__(attr + '_was', function() {
NewClass.prototype.__defineGetter__(attr + '_was', function () {
return this.__dataWas[attr];
});

Object.defineProperty(NewClass.prototype, '_' + attr, {
get: function() {
get: function () {
return this.__data[attr];
},
set: function(value) {
set: function (value) {
this.__data[attr] = value;
},
configurable: true,
Expand All @@ -276,12 +277,12 @@ Schema.prototype.define = function defineClass(className, properties, settings)
};

function standartize(properties, settings) {
Object.keys(properties).forEach(function(key) {
Object.keys(properties).forEach(function (key) {
var v = properties[key];
if (
typeof v === 'function' ||
typeof v === 'object' && v && v.constructor.name === 'Array'
) {
typeof v === 'function' ||
typeof v === 'object' && v && v.constructor.name === 'Array'
) {
properties[key] = {type: v};
}
});
Expand All @@ -297,7 +298,7 @@ function standartize(properties, settings) {
* @param {String} prop - name of propery
* @param {Object} params - property settings
*/
Schema.prototype.defineProperty = function(model, prop, params) {
Schema.prototype.defineProperty = function (model, prop, params) {
this.definitions[model].properties[prop] = params;
this.models[model].registerProperty(prop);
if (this.adapter.defineProperty) {
Expand Down Expand Up @@ -328,10 +329,10 @@ Schema.prototype.defineProperty = function(model, prop, params) {
* isExpired: { type: Boolean, index: true }
* });
*/
Schema.prototype.extendModel = function(model, props) {
Schema.prototype.extendModel = function (model, props) {
var t = this;
standartize(props, {});
Object.keys(props).forEach(function(propName) {
Object.keys(props).forEach(function (propName) {
var definition = props[propName];
t.defineProperty(model, propName, definition);
});
Expand All @@ -344,7 +345,7 @@ Schema.prototype.extendModel = function(model, props) {
*
* @warning All data will be lost! Use autoupdate if you need your data.
*/
Schema.prototype.automigrate = function(cb) {
Schema.prototype.automigrate = function (cb) {
this.freeze();
if (this.adapter.automigrate) {
this.adapter.automigrate(cb);
Expand All @@ -358,7 +359,7 @@ Schema.prototype.automigrate = function(cb) {
* This method make sense only for sql adapters.
* @param {Function} cb
*/
Schema.prototype.autoupdate = function(cb) {
Schema.prototype.autoupdate = function (cb) {
this.freeze();
if (this.adapter.autoupdate) {
this.adapter.autoupdate(cb);
Expand All @@ -372,7 +373,7 @@ Schema.prototype.autoupdate = function(cb) {
* This method make sense only for sql adapters.
* @param {Function} cb
*/
Schema.prototype.isActual = function(cb) {
Schema.prototype.isActual = function (cb) {
this.freeze();
if (this.adapter.isActual) {
this.adapter.isActual(cb);
Expand All @@ -389,7 +390,7 @@ Schema.prototype.isActual = function(cb) {
*
* @private used by adapters
*/
Schema.prototype.log = function(sql, t) {
Schema.prototype.log = function (sql, t) {
this.emit('log', sql, t);
};

Expand All @@ -406,7 +407,7 @@ Schema.prototype.freeze = function freeze() {
* Return table name for specified `modelName`
* @param {String} modelName
*/
Schema.prototype.tableName = function(modelName) {
Schema.prototype.tableName = function (modelName) {
return this.definitions[modelName].settings.table = this.definitions[modelName].settings.table || modelName;
};

Expand All @@ -421,7 +422,7 @@ Schema.prototype.defineForeignKey = function defineForeignKey(className, key) {
return;

if (this.adapter.defineForeignKey) {
this.adapter.defineForeignKey(className, key, function(err, keyType) {
this.adapter.defineForeignKey(className, key, function (err, keyType) {
if (err)
throw err;
this.definitions[className].properties[key] = {type: keyType};
Expand All @@ -440,7 +441,7 @@ Schema.prototype.defineForeignKey = function defineForeignKey(className, key) {
*/
Schema.prototype.begin = function begin(params, callback) {
if (typeof callback === 'undefined') {
callback = function(err) {
callback = function (err) {
return err;
};
}
Expand All @@ -460,7 +461,7 @@ Schema.prototype.begin = function begin(params, callback) {
*/
Schema.prototype.commit = function commit(params, callback) {
if (typeof callback === 'undefined') {
callback = function(err) {
callback = function (err) {
return err;
};
}
Expand All @@ -480,7 +481,7 @@ Schema.prototype.commit = function commit(params, callback) {
*/
Schema.prototype.rollback = function rollback(params, callback) {
if (typeof callback === 'undefined') {
callback = function(err) {
callback = function (err) {
return err;
};
}
Expand Down Expand Up @@ -513,7 +514,7 @@ Schema.prototype.disconnect = function disconnect() {
* @return {Array}
*/

Schema.prototype.modelNames = function() {
Schema.prototype.modelNames = function () {
var names = Object.keys(this.models);
return names;
};
Expand Down Expand Up @@ -558,7 +559,7 @@ Schema.prototype.modelNames = function() {
* @api public
*/

Schema.prototype.model = function(name, schema) {
Schema.prototype.model = function (name, schema) {
if ('string' === typeof schema) {
schema = false;
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "caminte",
"description": "ORM for every database: redis, mysql, neo4j, mongodb, rethinkdb, postgres, sqlite, tingodb",
"version": "0.0.22",
"version": "0.0.23",
"author": {
"name": "Aleksej Gordejev",
"email" : "[email protected]",
Expand Down

0 comments on commit b8efd83

Please sign in to comment.