Skip to content

Commit 3da4e00

Browse files
committed
Merge branch 'spatial' of github.com:MipMapApp/node-orm2 into MipMapApp-spatial
* 'spatial' of github.com:MipMapApp/node-orm2: Adding a test to ensure that point properties are pulled from the database correctly save support for point types DDL support for POINT types Support for 'point' type as a property
2 parents e5dd381 + c26b0ff commit 3da4e00

File tree

6 files changed

+58
-1
lines changed

6 files changed

+58
-1
lines changed

lib/Drivers/DDL/mysql.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,9 @@ function buildColumnDefinition(driver, name, prop) {
172172
def = driver.query.escapeId(name) + " ENUM (" +
173173
prop.values.map(driver.query.escapeVal.bind(driver.query)) +
174174
")";
175+
break;
176+
case "point":
177+
def = driver.query.escapeId(name) + " POINT";
175178
break;
176179
default:
177180
throw new Error("Unknown property type: '" + prop.type + "'");

lib/Drivers/DML/mysql.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,8 @@ Driver.prototype.propertyToValue = function (value, property) {
246246
return (value) ? 1 : 0;
247247
case "object":
248248
return JSON.stringify(value);
249+
case "point":
250+
return function() { return 'POINT(' + value.x + ', ' + value.y + ')'; };
249251
default:
250252
return value;
251253
}

lib/Property.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ exports.normalize = function (prop, Settings) {
3030
prop = { type: "enum", values: prop };
3131
}
3232

33-
if ([ "text", "number", "boolean", "date", "enum", "object", "binary" ].indexOf(prop.type) == -1) {
33+
if ([ "text", "number", "boolean", "date", "enum", "object", "binary", "point" ].indexOf(prop.type) == -1) {
3434
throw ErrorCodes.generateError(ErrorCodes.NO_SUPPORT, "Unknown property type: " + prop.type);
3535
}
3636

test/integration2/model-get.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,4 +276,33 @@ describe("Model.get()", function() {
276276
});
277277
});
278278
});
279+
280+
describe("with a point property type", function() {
281+
before(function (done) {
282+
Person = db.define("person", {
283+
name : String,
284+
location: {type: 'point'}
285+
});
286+
287+
ORM.singleton.clear();
288+
289+
return helper.dropSync(Person, function () {
290+
Person.create([{
291+
name : "John Doe",
292+
location: {x: 51.5177, y: -0.0968}
293+
}], done);
294+
});
295+
});
296+
297+
it("should deserialize the point to an array", function (done) {
298+
Person.get("John Doe", function(err, person) {
299+
should.equal(err, null);
300+
301+
person.location.should.be.an.instanceOf(Object);
302+
person.location.should.have.property('x', 51.5177);
303+
person.location.should.have.property('y', -0.0968);
304+
return done();
305+
});
306+
});
307+
});
279308
});

test/integration2/model-save.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,4 +196,22 @@ describe("Model.save()", function() {
196196
});
197197
});
198198
});
199+
200+
describe("with a point property", function () {
201+
before(setup({type: 'point'}, null));
202+
203+
it("should save the instance as a geospatial point", function (done) {
204+
var John = new Person({
205+
name: {x: 51.5177, y: -0.0968}
206+
});
207+
John.save(function (err) {
208+
should.equal(err, null);
209+
210+
John.name.should.be.an.instanceOf(Object);
211+
John.name.should.have.property('x', 51.5177);
212+
John.name.should.have.property('y', -0.0968);
213+
return done();
214+
});
215+
});
216+
});
199217
});

test/integration2/property.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ describe("Property", function () {
6161

6262
return done();
6363
});
64+
it("should accept: 'point'", function(done) {
65+
Property.normalize("point", ORM.settings).type.should.equal("point");
66+
67+
return done();
68+
});
6469

6570
describe("if not valid", function () {
6671
it("should throw", function (done) {

0 commit comments

Comments
 (0)