Skip to content

Commit

Permalink
Version up to v0.2.0
Browse files Browse the repository at this point in the history
Adds option to non persist values in the database.
  • Loading branch information
notheotherben committed Nov 4, 2013
1 parent fc858c9 commit 28be469
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 12 deletions.
18 changes: 13 additions & 5 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ orm.connect("mysql://username:password@host/database", function(err, db) {
createdProperty: 'created_at',
modifiedProperty: 'modified_at',
dbtype: { type: 'date', time: true },
now: function() { return new Date(); }
now: function() { return new Date(); },
persist: true
});

var user = db.define('user', {
Expand All @@ -42,14 +43,21 @@ orm.connect("mysql://username:password@host/database", function(err, db) {
```

## 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.
- `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(); }`)
- `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.
- `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(); }`).
- `persist` **boolean**
Used to prevent creation and modification timestamps from being stored in the database (default `true`).

## Features
- Easy to add created and modified date/time information to your models
- 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.

[node-orm2]: https://github.com/dresende/node-orm2
18 changes: 12 additions & 6 deletions lib/orm-timestamps.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ function Plugin(db, opts) {
createdProperty: 'created_at',
modifiedProperty: 'modified_at',
dbtype: { type: 'date', time: true },
now: function() { return new Date(); }
now: function() { return new Date(); },
persist: true
};


Expand All @@ -27,7 +28,7 @@ function Plugin(db, opts) {
next();
}

var that = this;
var that = this;
oldHook.call(this, function() {
cont.call(that);
});
Expand All @@ -43,23 +44,28 @@ function Plugin(db, opts) {

if(opts.timestamp !== true) return;

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

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

if(options.createdProperty !== false)
wrapHook(opts.hooks, 'beforeCreate', function() {
this[options.createdProperty] = options.now();
if(!persist && this[options.createdProperty] === undefined)
Object.defineProperty(this, options.createdProperty, {
value: options.now()
})
else if(persist)
this[options.createdProperty] = options.now();
});

if(options.modifiedProperty !== false)
wrapHook(opts.hooks, 'beforeSave', function() {
this[options.modifiedProperty] = options.now();
});
};
}


options = extend(options, opts);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"creation",
"date"
],
"version": "0.1.2",
"version": "0.2.0",
"license": "MIT",
"repository": {
"url": "http://github.com/SPARTAN563/node-orm-timestamps"
Expand Down

0 comments on commit 28be469

Please sign in to comment.