@knorm/timestamps
Knorm plugin that automates working with created_at and updated_at table
fields.
Installation
npm install --save @knorm/knorm @knorm/timestamps
@knorm/timestamps has a peer dependency on @knorm/knorm
Usage
const { knorm } = require('@knorm/knorm');
const { knormTimestamps } = require('@knorm/timestamps');
const orm = knorm({
// knorm options
}).use(
knormTimestamps({
// knormTimestamps options
})
);
Options
| Option | Type | Default | Description |
|---|---|---|---|
| [name] | string | timestamps | The name of the plugin, allows accessing the plugin instance via Knorm's plugin registry (Knorm.prototype.plugins) |
| [createdAt] | object | Config options for the createdAt field. | |
| [createdAt.name] | string | createdAt | The field-name for the createdAt field. |
| [createdAt.column] | string | created_at | The column-name for the createdAt field. |
| [updatedAt] | object | Config options for the updatedAt field. | |
| [updatedAt.name] | string | updatedAt | The field-name for the updatedAt field. |
| [updatedAt.column] | string | updated_at | The column-name for the updatedAt field. |
Features
This plugin:
- adds
createdAtandupdatedAtfields to theModelclass - modifies
Query.prototype.insertmethod such that thecreatedAtandupdatedAtfields are set to the current time (i.e.new Date()) - modifies
Query.prototype.updatemethod to set theupdatedAtfield is set to the current time (i.e.new Date()) before update.
info
- If
Query.prototype.insertis passed data wherecreatedAtandupdatedAtare already set, they are left as is. - If
Query.prototype.updateis passed data wherecreatedAtis set, it's removed before the updated.updatedAtis always set to the current time.