Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Properties refactor #318

Merged
merged 4 commits into from
Aug 29, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/Associations/One.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ exports.prepare = function (Model, associations, association_properties, model_f
for (k in association.field) {
association_properties.push(k);
if (!association.reversed) {
Model.allProperties[k] = _.omit(association.field[k], 'klass');
Model.allProperties[k].klass = 'hasOne';
model_fields.push(k);
}
}
Expand Down
41 changes: 17 additions & 24 deletions lib/Drivers/DDL/mysql.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,39 +23,29 @@ exports.drop = function (driver, opts, cb) {
exports.sync = function (driver, opts, cb) {
var queries = [];
var definitions = [];
var k, i, pending;
var k, i, pending, prop;
var primary_keys = opts.id.map(function (k) { return driver.query.escapeId(k); });
var keys = [];

for (i = 0; i < opts.id.length; i++) {
if (opts.properties.hasOwnProperty(opts.id[i])) continue;

keys.push(driver.query.escapeId(opts.id[i]));
}

for (i = 0; i < keys.length; i++) {
definitions.push(keys[i] + " INT(10) UNSIGNED NOT NULL");
}
if (opts.id.length == 1 && !opts.extension) {
definitions[definitions.length - 1] += " AUTO_INCREMENT";
}

for (k in opts.properties) {
definitions.push(buildColumnDefinition(driver, k, opts.properties[k]));
for (k in opts.allProperties) {
prop = opts.allProperties[k];
definitions.push(buildColumnDefinition(driver, k, prop));
}

for (i = 0; i < opts.one_associations.length; i++) {
if (opts.one_associations[i].extension) continue;
if (opts.one_associations[i].reversed) continue;
for (k in opts.one_associations[i].field) {
definitions.push(buildColumnDefinition(driver, k, opts.one_associations[i].field[k]));
for (k in opts.allProperties) {
prop = opts.allProperties[k];
if (prop.unique === true) {
definitions.push("UNIQUE (" + driver.query.escapeId(k) + ")");
} else if (prop.index) {
definitions.push("INDEX (" + driver.query.escapeId(k) + ")");
}
}

for (k in opts.properties) {
if (opts.properties[k].unique === true) {
for (k in opts.allProperties) {
prop = opts.allProperties[k];
if (prop.unique === true) {
definitions.push("UNIQUE KEY " + driver.query.escapeId(k) + " (" + driver.query.escapeId(k) + ")");
} else if (opts.properties[k].index) {
} else if (prop.index) {
definitions.push("INDEX (" + driver.query.escapeId(k) + ")");
}
}
Expand Down Expand Up @@ -142,6 +132,9 @@ function buildColumnDefinition(driver, name, prop) {
def = driver.query.escapeId(name) + " VARCHAR(" + Math.min(Math.max(parseInt(prop.size, 10) || 255, 1), 65535) + ")";
}
break;
case "serial":
def = driver.query.escapeId(name) + " INT(10) UNSIGNED NOT NULL AUTO_INCREMENT";
break;
case "number":
if (prop.rational === false) {
def = driver.query.escapeId(name) + " " + colTypes.integer[prop.size || 4];
Expand Down
48 changes: 16 additions & 32 deletions lib/Drivers/DDL/postgres.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,46 +24,27 @@ exports.sync = function (driver, opts, cb) {
var subqueries = [];
var typequeries = [];
var definitions = [];
var k, i, pending;
var k, i, pending, prop;
var primary_keys = opts.id.map(function (k) { return driver.query.escapeId(k); });
var keys = [];
for (i = 0; i < opts.id.length; i++) {
if (opts.properties.hasOwnProperty(opts.id[i])) continue;

keys.push(driver.query.escapeId(opts.id[i]));
}

for (i = 0; i < keys.length; i++) {
definitions.push(keys[i] + " INTEGER NOT NULL");
}

if (opts.id.length == 1 && !opts.extension) {
definitions[definitions.length - 1] = keys[0] + " SERIAL";
}
for (k in opts.allProperties) {
prop = opts.allProperties[k];
definitions.push(buildColumnDefinition(driver, opts.table, k, prop));

for (k in opts.properties) {
definitions.push(buildColumnDefinition(driver, opts.table, k, opts.properties[k]));

if (opts.properties[k].type == "enum") {
if (prop.type == "enum") {
typequeries.push(
"CREATE TYPE " + driver.query.escapeId("enum_" + opts.table + "_" + k) + " AS ENUM (" +
opts.properties[k].values.map(driver.query.escapeVal.bind(driver)) + ")"
prop.values.map(driver.query.escapeVal.bind(driver)) + ")"
);
}
}

for (i = 0; i < opts.one_associations.length; i++) {
if (opts.one_associations[i].extension) continue;
if (opts.one_associations[i].reversed) continue;
for (k in opts.one_associations[i].field) {
definitions.push(buildColumnDefinition(driver, opts.table, k, opts.one_associations[i].field[k]));
}
}

for (k in opts.properties) {
if (opts.properties[k].unique === true) {
for (k in opts.allProperties) {
prop = opts.allProperties[k];
if (prop.unique === true) {
definitions.push("UNIQUE (" + driver.query.escapeId(k) + ")");
} else if (opts.properties[k].index) {
} else if (prop.index) {
definitions.push("INDEX (" + driver.query.escapeId(k) + ")");
}
}
Expand All @@ -77,7 +58,7 @@ exports.sync = function (driver, opts, cb) {
typequeries: typequeries,
subqueries : subqueries
});

for (i = 0; i < opts.one_associations.length; i++) {
if (opts.one_associations[i].extension) continue;
if (opts.one_associations[i].reversed) continue;
Expand All @@ -101,7 +82,7 @@ exports.sync = function (driver, opts, cb) {
for (i = 0; i < opts.many_associations.length; i++) {
definitions = [];
typequeries = [];

for (k in opts.many_associations[i].mergeId) {
definitions.push(buildColumnDefinition(driver, opts.many_associations[i].mergeTable, k, opts.many_associations[i].mergeId[k]));
}
Expand All @@ -120,7 +101,7 @@ exports.sync = function (driver, opts, cb) {
);
}
}

var index = null;
for (k in opts.many_associations[i].mergeId) {
if (index == null) index = driver.query.escapeId(k);
Expand Down Expand Up @@ -207,6 +188,9 @@ function buildColumnDefinition(driver, table, name, prop) {
def = driver.query.escapeId(name) + " VARCHAR(" + Math.max(parseInt(prop.size, 10) || 255, 1) + ")";
}
break;
case "serial":
def = driver.query.escapeId(name) + " SERIAL";
break;
case "number":
if (prop.rational === false) {
def = driver.query.escapeId(name) + " " + colTypes.integer[prop.size || 4];
Expand Down
36 changes: 9 additions & 27 deletions lib/Drivers/DDL/sqlite.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,34 +22,13 @@ exports.drop = function (driver, opts, cb) {
exports.sync = function (driver, opts, cb) {
var queries = [];
var definitions = [];
var k, i, pending;
var k, i, pending, prop;
var primary_keys = opts.id.map(function (k) { return driver.query.escapeId(k); });
var keys = [];

for (i = 0; i < opts.id.length; i++) {
if (opts.properties.hasOwnProperty(opts.id[i])) continue;

keys.push(driver.query.escapeId(opts.id[i]));
}

for (i = 0; i < keys.length; i++) {
definitions.push(keys[i] + " INTEGER UNSIGNED NOT NULL");
}

if (opts.id.length == 1 && !opts.extension) {
definitions[definitions.length - 1] = keys[0] + " INTEGER PRIMARY KEY AUTOINCREMENT";
}

for (k in opts.properties) {
definitions.push(buildColumnDefinition(driver, k, opts.properties[k]));
}

for (i = 0; i < opts.one_associations.length; i++) {
if (opts.one_associations[i].extension) continue;
if (opts.one_associations[i].reversed) continue;
for (k in opts.one_associations[i].field) {
definitions.push(buildColumnDefinition(driver, k, opts.one_associations[i].field[k]));
}
for (k in opts.allProperties) {
prop = opts.allProperties[k];
definitions.push(buildColumnDefinition(driver, k, prop));
}

if (keys.length > 1) {
Expand All @@ -75,7 +54,7 @@ exports.sync = function (driver, opts, cb) {
);
}
}

for (i = 0; i < opts.one_associations.length; i++) {
if (opts.one_associations[i].extension) continue;
if (opts.one_associations[i].reversed) continue;
Expand Down Expand Up @@ -112,7 +91,7 @@ exports.sync = function (driver, opts, cb) {
for (k in opts.many_associations[i].props) {
definitions.push(buildColumnDefinition(driver, k, opts.many_associations[i].props[k]));
}

var index = null;
for (k in opts.many_associations[i].mergeId) {
if (index == null) index = driver.query.escapeId(k);
Expand Down Expand Up @@ -153,6 +132,9 @@ function buildColumnDefinition(driver, name, prop) {
case "text":
def = driver.query.escapeId(name) + " TEXT";
break;
case "serial":
def = driver.query.escapeId(name) + " INTEGER PRIMARY KEY AUTOINCREMENT";
break;
case "number":
if (prop.rational === false) {
def = driver.query.escapeId(name) + " INTEGER";
Expand Down
21 changes: 12 additions & 9 deletions lib/Drivers/DML/postgres.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,32 +238,35 @@ Driver.prototype.clear = function (table, cb) {
};

Driver.prototype.valueToProperty = function (value, property) {
var v;

switch (property.type) {
case "object":
if (typeof value == "object" && !Buffer.isBuffer(value)) {
return value;
break;
}
try {
return JSON.parse(value);
value = JSON.parse(value);
} catch (e) {
return null;
value = null;
}
break;
case "point":
if (typeof value == "string") {
var m = value.match(/\((\-?[\d\.]+)[\s,]+(\-?[\d\.]+)\)/);
if (m) {
return { x : parseFloat(m[1], 10) , y : parseFloat(m[2], 10) };
value = { x : parseFloat(m[1], 10) , y : parseFloat(m[2], 10) };
}
}
return value;
break;
case "number":
if (value !== null) {
return Number(value);
if (typeof value != 'number' && value !== null) {
v = Number(value);
if (!isNaN(v)) value = v;
}
default:
return value;
break;
}
return value;
};

Driver.prototype.propertyToValue = function (value, property) {
Expand Down
19 changes: 15 additions & 4 deletions lib/Drivers/DML/sqlite.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,9 +205,12 @@ Driver.prototype.clear = function (table, cb) {
};

Driver.prototype.valueToProperty = function (value, property) {
var v;

switch (property.type) {
case "boolean":
return !!value;
value = !!value;
break;
case "object":
if (typeof value == "object" && !Buffer.isBuffer(value)) {
return value;
Expand All @@ -218,16 +221,24 @@ Driver.prototype.valueToProperty = function (value, property) {
return null;
}
break;
case "number":
if (typeof value != 'number' && value !== null) {
v = Number(value);
if (!isNaN(v)) {
return v;
}
}
break;
case "date":
if (typeof value === 'string') {
if (value.indexOf('Z', value.length - 1) === -1) {
return new Date(value + 'Z');
}
}
return new Date(value);
default:
return value;
value = new Date(value);
break;
}
return value;
};

Driver.prototype.propertyToValue = function (value, property) {
Expand Down
Loading