Skip to content

Commit

Permalink
Merge pull request #23 from strongloop/feature/add-geopoint-support
Browse files Browse the repository at this point in the history
Add support for GeoPoint type
  • Loading branch information
raymondfeng committed Jan 29, 2015
2 parents 89c5042 + 8938255 commit d08dad7
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 4 deletions.
2 changes: 1 addition & 1 deletion example/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ var Account = ds.createModel('account', {
name: String,
emails: [String],
age: Number},
{strcit: true});
{strict: true});

ds.automigrate('account', function(err) {
// Create two instances
Expand Down
49 changes: 47 additions & 2 deletions lib/postgresql.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,25 @@ PostgreSQL.prototype._categorizeProperties = function(model, data) {
};
};

PostgreSQL.prototype.mapToDB = function (model, data) {
var dbData = {};
if (!data) {
return dbData;
}
var props = this._models[model].properties;
for (var p in data) {
if(props[p]) {
var pType = props[p].type && props[p].type.name;
if (pType === 'GeoPoint' && data[p]) {
dbData[p] = '(' + data[p].lat + ',' + data[p].lng + ')';
} else {
dbData[p] = data[p];
}
}
}
return dbData;
}

/**
* Create the data model in PostgreSQL
*
Expand All @@ -236,6 +255,7 @@ PostgreSQL.prototype._categorizeProperties = function(model, data) {
*/
PostgreSQL.prototype.create = function (model, data, callback) {
var self = this;
data = self.mapToDB(model, data);
var props = self._categorizeProperties(model, data);

var sql = [];
Expand Down Expand Up @@ -274,6 +294,7 @@ PostgreSQL.prototype.create = function (model, data, callback) {
/*
PostgreSQL.prototype.updateOrCreate = function (model, data, callback) {
var self = this;
data = self.mapToDB(model, data);
var props = self._categorizeProperties(model, data);
var idColumns = props.ids.map(function(key) {
return self.columnEscaped(model, key); }
Expand Down Expand Up @@ -322,6 +343,7 @@ PostgreSQL.prototype.updateOrCreate = function (model, data, callback) {
*/
PostgreSQL.prototype.save = function (model, data, callback) {
var self = this;
data = self.mapToDB(model, data);
var props = self._categorizeProperties(model, data);

var sql = [];
Expand All @@ -344,6 +366,7 @@ PostgreSQL.prototype.update =
var sql = ['UPDATE ', this.tableEscaped(model), ' SET ',
this.toFields(model, data), ' ', whereClause].join('');

data = this.mapToDB(model, data);
var props = this._categorizeProperties(model, data);

this.query(sql, generateQueryParams(data, props), function (err, result) {
Expand Down Expand Up @@ -554,12 +577,24 @@ PostgreSQL.prototype.fromDatabase = function (model, data) {
}
// console.log(key, val);
var prop = props[p];
if (prop && prop.type && prop.type.name === 'Boolean') {
var type = prop.type && prop.type.name;
if (prop && type === 'Boolean') {
if(typeof val === 'boolean') {
json[p] = val;
} else {
json[p] = (val === 'Y' || val === 'y' || val === 'T' || val === 't' || val === '1');
}
} else if (prop && type === 'GeoPoint' || type === 'Point') {
if (typeof val === 'string') {
// The point format is (x,y)
var point = val.split(/[\(\)\s,]+/).filter(Boolean);
json[p] = {
lat: +point[0],
lng: +point[1]
};
} else {
json[p] = val;
}
} else {
json[p] = val;
}
Expand Down Expand Up @@ -592,7 +627,7 @@ PostgreSQL.prototype.escapeName = function (name) {
if (!name) {
return name;
}
return '"' + name.replace(/\./g, '"."').toLowerCase() + '"';
return '"' + name.replace(/\./g, '"."') + '"';
};

PostgreSQL.prototype.schemaName = function (model) {
Expand All @@ -603,6 +638,13 @@ PostgreSQL.prototype.schemaName = function (model) {
return schemaName;
};

PostgreSQL.prototype.table = function (model) {
// Check if there is a 'table' property for postgresql
var dbMeta = this._models[model].settings && this._models[model].settings.postgresql;
var tableName = (dbMeta && (dbMeta.table || dbMeta.tableName)) || model.toLowerCase();
return tableName;
};

PostgreSQL.prototype.tableEscaped = function (model) {
return this.escapeName(this.schemaName(model)) + '.' + this.escapeName(this.table(model));
};
Expand Down Expand Up @@ -1427,6 +1469,9 @@ PostgreSQL.prototype.columnDataType = function (model, property) {
return 'TIMESTAMP WITH TIME ZONE';
case 'Timestamp':
return 'TIMESTAMP WITH TIME ZONE';
case 'GeoPoint':
case 'Point':
return 'POINT';
case 'Boolean':
return 'BOOLEAN'; // PostgreSQL doesn't have built-in boolean
}
Expand Down
14 changes: 14 additions & 0 deletions test/postgresql.discover.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,3 +195,17 @@ describe('Discover LDL schema from a table', function () {
});
});
});

describe('Discover and build models', function() {
it('should build a model from discovery', function(done) {

db.discoverAndBuildModels('GeoPoint', {schema: 'strongloop'}, function(err, schema) {
schema.Geopoint.find(function(err, data) {
assert(!err);
assert(Array.isArray(data));
assert(data[0].location);
done();
});
});
});
});
18 changes: 17 additions & 1 deletion test/postgresql.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
var juggler = require('loopback-datasource-juggler');
require('loopback-datasource-juggler/test/common.batch.js');
require('loopback-datasource-juggler/test/include.test.js');

Expand All @@ -14,6 +15,7 @@ describe('postgresql connector', function () {
Post = db.define('PostWithBoolean', {
title: { type: String, length: 255, index: true },
content: { type: String },
loc: 'GeoPoint',
approved: Boolean
});
});
Expand Down Expand Up @@ -124,6 +126,20 @@ describe('postgresql connector', function () {
});
});

it('should support GeoPoint types', function(done) {
var GeoPoint = juggler.ModelBuilder.schemaTypes.geopoint;
var loc = new GeoPoint({lng: 10, lat: 20});
Post.create({title: 'T1', content: 'C1', loc: loc}, function(err, p) {
should.not.exists(err);
Post.findById(p.id, function(err, p) {
should.not.exists(err);
p.loc.lng.should.be.eql(10);
p.loc.lat.should.be.eql(20);
done();
});
});
});

});

// FIXME: The following test cases are to be reactivated for PostgreSQL
Expand Down Expand Up @@ -231,4 +247,4 @@ describe('postgresql connector', function () {
});
});
});
*/
*/

0 comments on commit d08dad7

Please sign in to comment.