Skip to content

Commit

Permalink
Merge pull request #11 from SPARTAN563/feature/table-options
Browse files Browse the repository at this point in the history
Add table specific option overrides
  • Loading branch information
notheotherben committed Jul 2, 2014
2 parents 9a798ad + 6a33760 commit dbc6a3b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 14 deletions.
23 changes: 17 additions & 6 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ orm.connect("mysql://username:password@host/database", function(err, db) {
db.use(modts, {
createdProperty: 'created_at',
modifiedProperty: 'modified_at',
expiresProperty: false,
expireProperty: false,
dbtype: { type: 'date', time: true },
now: function() { return new Date(); },
expiry: function() { var d = new Date(); return d.setMinutes(d.getMinutes() + 60); },
expire: function() { var d = new Date(); return d.setMinutes(d.getMinutes() + 60); },
persist: true
});

Expand All @@ -37,25 +37,35 @@ orm.connect("mysql://username:password@host/database", function(err, db) {
email: String,
password: String
}, {
id: 'username',
timestamp: true
});
});


var userDetails = db.define('user_details', {
username: String,
first_name: String,
last_name: String
}, {
id: 'username',
timestamp: {
createdProperty: false
}
});
});
```

## Options
- `createdProperty` **string|false**
Determines the name of the property use to store the created timestamp (default `"created_at"`). If set to `false`, disables this property.
- `modifiedProperty` **string|false**
Determines the name of the property used to store the modified timestamp (default `"modified_at"`). If set to `false`, disables this property.
- `expiresProperty` **string|false**
- `expireProperty` **string|false**
Determines the name of the property used to store the expiry timestamp for example `"expires_at"` (default `false`). If set to `false`, disables this property.
- `dbtype` **object**
Allows you to set the type of column used by the DB to allow for custom data types (default `{ type: 'date', time: true }`).
- `now` **function**
Allows you to specify a custom function used to set the current time data for the database (default `function() { return new Date(); }`).
- `expiry` **function**
- `expire` **function**
Allows you to specify a custom function used to set the expiry time data for the database (default `function() { var d = new Date(); d.setMinutes(d.getMinutes() + 60); return d; }`).
- `persist` **boolean**
Used to prevent creation and modification timestamps from being stored in the database (default `true`).
Expand All @@ -65,5 +75,6 @@ orm.connect("mysql://username:password@host/database", function(err, db) {
- Highly customizable
- Supports existing beforeCreate/beforeSave hooks through the use of a robust wrapper function
- Allows values to be stored "in-memory" if usage scenarios don't require them to be stored in the database.
- Allows custom options to be set on a per-table basis if needed

[node-orm2]: https://github.com/dresende/node-orm2
18 changes: 10 additions & 8 deletions lib/orm-timestamps.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
function Plugin(db, opts) {
var options = {
var defaults = {
createdProperty: 'created_at',
modifiedProperty: 'modified_at',
expiresProperty: false,
Expand Down Expand Up @@ -51,15 +51,18 @@ function Plugin(db, opts) {
}

function monitor(name, properties, opts) {
if(!opts.timestamp) return;

if(opts.timestamp !== true) return;
var options = defaults;
if(typeof opts.timestamp == 'object')
options = extend(defaults, opts.timestamp);

if(options.persist && options.createdProperty !== false)
properties[options.createdProperty] = options.dbtype;
if(options.persist && options.modifiedProperty !== false)
properties[options.modifiedProperty] = options.dbtype;
if(options.persist && options.expiresProperty !== false)
properties[options.expiresProperty] = options.dbtype;
if(options.persist && options.expireProperty !== false)
properties[options.expireProperty] = options.dbtype;

opts.hooks = opts.hooks || {};

Expand All @@ -78,14 +81,13 @@ function Plugin(db, opts) {
this[options.modifiedProperty] = options.now();
});

if(options.expiresProperty !== false)
if(options.expireProperty !== false)
wrapHook(opts.hooks, 'beforeSave', function() {
this[options.expiresProperty] = options.expiry();
this[options.expireProperty] = options.expire();
});
}


options = extend(options, opts);
defaults = extend(defaults, opts);

return {
beforeDefine: monitor
Expand Down

0 comments on commit dbc6a3b

Please sign in to comment.