-
Notifications
You must be signed in to change notification settings - Fork 376
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Set internal default property value to undefined instead of null.
This will prevent null values being explicitly sent to the database when no value was assigned and instead result in the database setting the column to null, or generating a default value. Properties with an internal value of undefined will still return null when accessed. Setting a previously filled property to undefined will still set it to null in the DB. No ORM tests were broken by this change, and as such, the impact of this should be limited ot a very small number of corner cases. Add PostgreSQL uuid column support. Allow specifying defaultExpression (eg. uuid_generate_v4() for PostgreSQL or uuid() for MySQL) to be executed by the database engine for generating default values.
- Loading branch information
Showing
6 changed files
with
143 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
var _ = require('lodash'); | ||
var should = require('should'); | ||
var helper = require('../support/spec_helper'); | ||
var common = require('../common'); | ||
var ORM = require('../../'); | ||
|
||
if (common.protocol() !== "postgres") return; | ||
|
||
describe("Property", function() { | ||
describe("type uuid", function () { | ||
var db = null; | ||
|
||
before(function (done) { | ||
helper.connect(function (connection) { | ||
db = connection; | ||
|
||
done(); | ||
}); | ||
}); | ||
|
||
after(function () { | ||
db.close(); | ||
}); | ||
|
||
var Thing = null; | ||
|
||
before(function (done) { | ||
db.driver.execQuery('CREATE EXTENSION IF NOT EXISTS "uuid-ossp";', function (err) { | ||
should.not.exist(err); | ||
|
||
Thing = db.define('thing', { | ||
id: { type: 'uuid', key: true, defaultExpression: 'uuid_generate_v4()' }, | ||
//id: { type: 'serial' }, | ||
name: { type: 'text' } | ||
}); | ||
|
||
helper.dropSync(Thing, done); | ||
}); | ||
}); | ||
|
||
it("should create the table", function () { | ||
should(true); | ||
}); | ||
|
||
var infoSQL = "SELECT * FROM information_schema.columns WHERE table_name = 'thing' AND column_name = 'id'"; | ||
|
||
it("should have the correct type", function (done) { | ||
db.driver.execQuery(infoSQL, function (err, cols) { | ||
should.not.exist(err); | ||
|
||
var uuidCol = cols[0]; | ||
|
||
should.exist(uuidCol); | ||
should.equal(uuidCol.data_type, 'uuid'); | ||
done(); | ||
}); | ||
}); | ||
|
||
it("should have the correct default value", function (done) { | ||
db.driver.execQuery(infoSQL, function (err, cols) { | ||
should.not.exist(err); | ||
|
||
var uuidCol = cols[0]; | ||
|
||
should.exist(uuidCol); | ||
should.equal(uuidCol.column_default, 'uuid_generate_v4()'); | ||
done(); | ||
}); | ||
}); | ||
|
||
it("should set id automatically", function (done) { | ||
var chair = new Thing({ name: 'chair' }); | ||
|
||
chair.save(function (err) { | ||
should.not.exist(err); | ||
|
||
Thing.find().all(function (err, items) { | ||
should.not.exist(err); | ||
should.equal(items.length, 1); | ||
should.equal(items[0].name, 'chair'); | ||
items[0].id.should.match(/^[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i); | ||
|
||
done(); | ||
}); | ||
}); | ||
}); | ||
|
||
it("should save", function (done) { | ||
var horse = new Thing({ name: 'horse' }); | ||
|
||
horse.save(function (err) { | ||
should.not.exist(err); | ||
|
||
Thing.get(horse.id, function (err, item) { | ||
should.not.exist(err); | ||
|
||
item.name = 'horsey'; | ||
|
||
item.save(function (err) { | ||
should.not.exist(err); | ||
|
||
Thing.get(horse.id, function (err, item) { | ||
should.not.exist(err); | ||
should.equal(item.id, horse.id); | ||
should.equal(item.name, 'horsey'); | ||
|
||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
|
||
}); | ||
}); |