Skip to content

Fix default clause #42

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,7 @@ The model definition consists of the following properties:
"createdOn": {
"type": "Date",
"required": false,
"sqlite3": {
"dbDefault": "now"
}
"default": "$now"
}
}}

Expand Down
13 changes: 8 additions & 5 deletions lib/sqlite3.js
Original file line number Diff line number Diff line change
Expand Up @@ -378,20 +378,23 @@ function _convertBoolean(value) {
}

SQLite3.prototype._getDefaultClause = function(property) {
if (!property || !property.sqlite3 || !property.sqlite3.dbDefault) return '';
var value = property.sqlite3.dbDefault;
if (!property || property.default === undefined) return '';
var value = property.default;

switch (property.type.name) {
case 'Number':
if (isNaN(value)) return Error(g.f('Invalid numeric default: %s', value));
return ' DEFAULT ' + Number(value);
case 'Boolean':
return 'DEFAULT ' + _convertBoolean(value);
return ' DEFAULT ' + _convertBoolean(value);
case 'Date':
if (value === 'now') {
if (value === '$now') {
// store as ms since 1970
return ' DEFAULT (CAST(STRFTIME(\'%s\', \'now\') AS INTEGER)*1000)';
}
if (typeof value === 'function') {
return ' DEFAULT (CAST(STRFTIME(\'%s\', \'now\') AS INTEGER)*1000)';
}
if (!isNaN(value))
return ' DEFAULT ' + value;
if (typeof value === 'string') {
Expand All @@ -403,7 +406,7 @@ SQLite3.prototype._getDefaultClause = function(property) {
}
return Error(g.f('Invalid date default: %s', value));
case 'String':
return 'DEFAULT "' + this.escapeValue(value) + '"';
return ' DEFAULT "' + this.escapeValue(value) + '"';
case 'GeoPoint':
case 'Point':
case 'List':
Expand Down
Loading