Skip to content

Commit 996f27f

Browse files
committed
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.
1 parent be9faff commit 996f27f

File tree

6 files changed

+147
-15
lines changed

6 files changed

+147
-15
lines changed

Changelog.md

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
1+
### v6.0.0
2+
- [POSSIBLY BREAKING] Set internal default property value to `undefined` instead of `null` ([855](../../pull/855)).
3+
- 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.
4+
- Properties with an internal value of `undefined` will still return `null` when accessed.
5+
- Setting a previously filled property to `undefined` will still set it to null in the DB.
6+
- No ORM tests were broken by this change, and as such, the impact of this should be limited to a very small number of corner cases.
7+
- Add PostgreSQL `uuid` column support ([855](../../pull/855)).
8+
- Allow specifying `defaultExpression` (eg. `uuid_generate_v4()` for PostgreSQL or `uuid()` for MySQL) to be executed by the database engine for generating default values ([855](../../pull/855)).
9+
110
### v5.0.9
2-
- Add async versions of driver functions ([851](../../pull/851)
11+
- Add async versions of driver functions ([851](../../pull/851))
312

413
### v5.0.8
5-
- Improve Typescript typings - add offset prop to find options ([850](../../pull/850)
14+
- Improve Typescript typings - add offset prop to find options ([850](../../pull/850))
615

716
### v5.0.7
817
- Resolve security vulnerabilities
@@ -13,12 +22,12 @@
1322
- If using Postgres and Nodejs v14+, you must use `pg` driver >= 8.1. The cause of this is unclear, but tests timeout.
1423

1524
### v5.0.5
16-
- Update lodash & sql-ddl-sync version to address security vulnerabilities ([845](../../pull/845)
25+
- Update lodash & sql-ddl-sync version to address security vulnerabilities ([845](../../pull/845))
1726
- Node 11+ support (stable sort; see https://github.com/nodejs/node/issues/24294 for details)
1827
- Test against node 12 & 13
1928

2029
### v5.0.4
21-
- Update sql-query version to address security vulnerabilities ([841](../../pull/841)
30+
- Update sql-query version to address security vulnerabilities ([841](../../pull/841))
2231

2332
### v5.0.3
2433
- Update dependencies to address security vulnerabilities

lib/Instance.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,9 +153,11 @@ function Instance(Model, opts) {
153153
prop = Model.allProperties[k];
154154

155155
if (prop) {
156+
/*
156157
if (opts.data[k] == null && (prop.type == 'serial' || typeof prop.defaultValue == 'function')) {
157158
continue;
158159
}
160+
*/
159161

160162
if (opts.driver.propertyToValue) {
161163
data[k] = opts.driver.propertyToValue(opts.data[k], prop);
@@ -484,7 +486,7 @@ function Instance(Model, opts) {
484486
}
485487

486488
var addInstanceProperty = function (key) {
487-
var defaultValue = null;
489+
var defaultValue = undefined;
488490
var prop = Model.allProperties[key];
489491

490492
// This code was first added, and then commented out in a later commit.
@@ -502,7 +504,13 @@ function Instance(Model, opts) {
502504

503505
Object.defineProperty(instance, key, {
504506
get: function () {
505-
return opts.data[key];
507+
var val = opts.data[key];
508+
509+
if (val === undefined) {
510+
return null;
511+
} else {
512+
return opts.data[key];
513+
}
506514
},
507515
set: function (val) {
508516
if (prop.key === true) {
@@ -731,7 +739,7 @@ function Instance(Model, opts) {
731739

732740
if (!asc.reversed && !asc.extension) {
733741
for (k in asc.field) {
734-
if (!opts.data.hasOwnProperty(k)) {
742+
if (!instance.hasOwnProperty(k)) {
735743
addInstanceProperty(k);
736744
}
737745
}

lib/Property.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ var ORMError = require("./Error");
33

44
var KNOWN_TYPES = [
55
"text", "number", "integer", "boolean", "date", "enum", "object",
6-
"binary", "point", "serial"
6+
"binary", "point", "serial", "uuid"
77
];
88

99
exports.normalize = function (opts) {

package-lock.json

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"sqlite",
1313
"mongodb"
1414
],
15-
"version": "5.0.9",
15+
"version": "6.0.0",
1616
"license": "MIT",
1717
"homepage": "http://dresende.github.io/node-orm2",
1818
"repository": "http://github.com/dresende/node-orm2.git",
@@ -67,7 +67,7 @@
6767
"hat": "0.0.3",
6868
"lodash": "^4.17.21",
6969
"path-is-absolute": "1.0.1",
70-
"sql-ddl-sync": "0.3.16",
70+
"sql-ddl-sync": "0.3.18",
7171
"sql-query": "0.1.27"
7272
},
7373
"devDependencies": {

test/integration/property-uuid.js

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
var _ = require('lodash');
2+
var should = require('should');
3+
var helper = require('../support/spec_helper');
4+
var common = require('../common');
5+
var ORM = require('../../');
6+
7+
if (common.protocol() !== "postgres") return;
8+
9+
describe("Property", function() {
10+
describe("type uuid", function () {
11+
var db = null;
12+
13+
before(function (done) {
14+
helper.connect(function (connection) {
15+
db = connection;
16+
17+
done();
18+
});
19+
});
20+
21+
after(function () {
22+
db.close();
23+
});
24+
25+
var Thing = null;
26+
27+
before(function (done) {
28+
db.driver.execQuery('CREATE EXTENSION IF NOT EXISTS "uuid-ossp";', function (err) {
29+
should.not.exist(err);
30+
31+
Thing = db.define('thing', {
32+
id: { type: 'uuid', key: true, defaultExpression: 'uuid_generate_v4()' },
33+
//id: { type: 'serial' },
34+
name: { type: 'text' }
35+
});
36+
37+
helper.dropSync(Thing, done);
38+
});
39+
});
40+
41+
it("should create the table", function () {
42+
should(true);
43+
});
44+
45+
var infoSQL = "SELECT * FROM information_schema.columns WHERE table_name = 'thing' AND column_name = 'id'";
46+
47+
it("should have the correct type", function (done) {
48+
db.driver.execQuery(infoSQL, function (err, cols) {
49+
should.not.exist(err);
50+
51+
var uuidCol = cols[0];
52+
53+
should.exist(uuidCol);
54+
should.equal(uuidCol.data_type, 'uuid');
55+
done();
56+
});
57+
});
58+
59+
it("should have the correct default value", function (done) {
60+
db.driver.execQuery(infoSQL, function (err, cols) {
61+
should.not.exist(err);
62+
63+
var uuidCol = cols[0];
64+
65+
should.exist(uuidCol);
66+
should.equal(uuidCol.column_default, 'uuid_generate_v4()');
67+
done();
68+
});
69+
});
70+
71+
it("should set id automatically", function (done) {
72+
var chair = new Thing({ name: 'chair' });
73+
74+
chair.save(function (err) {
75+
should.not.exist(err);
76+
77+
Thing.find().all(function (err, items) {
78+
should.not.exist(err);
79+
should.equal(items.length, 1);
80+
should.equal(items[0].name, 'chair');
81+
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);
82+
83+
done();
84+
});
85+
});
86+
});
87+
88+
it("should save", function (done) {
89+
var horse = new Thing({ name: 'horse' });
90+
91+
horse.save(function (err) {
92+
should.not.exist(err);
93+
94+
Thing.get(horse.id, function (err, item) {
95+
should.not.exist(err);
96+
97+
item.name = 'horsey';
98+
99+
item.save(function (err) {
100+
should.not.exist(err);
101+
102+
Thing.get(horse.id, function (err, item) {
103+
should.not.exist(err);
104+
should.equal(item.id, horse.id);
105+
should.equal(item.name, 'horsey');
106+
107+
done();
108+
});
109+
});
110+
});
111+
});
112+
});
113+
114+
});
115+
});

0 commit comments

Comments
 (0)