1.0.6 • Published 6 years ago
commodo-fields-storage-crud-logs v1.0.6
commodo-fields-storage-crud-logs
Appends createdOn, updatedOn, and savedOn fields to your model. The fields are automatically updated with current time before the model is about to be saved.
Install
npm install --save commodo-fields-storage-crud-logsOr if you prefer yarn:
yarn add commodo-fields-storage-crud-logsQuick Example:
import { compose } from "ramda";
import { withFields, string } from "@commodo/fields";
import { withCrudLogs } from "commodo-fields-storage-crud-logs";
const Company = compose(
withFields({
name: string(),
// Other fields you might need...
}),
withCrudLogs(),
withStorage(...), // Required.
// Other higher order functions (HOFs) you might need...
)();
const company = new Company();
company.name = "Acme Corporation";
// All fields null as a default value.
console.log(company.createdOn); // Logs null.
console.log(company.updatedOn); // Logs null.
console.log(company.savedOn); // Logs null.
// Upon saving the model for the first time, "createdOn" and "savedOn" fields will receive a value:
await company.save();
console.log(company.createdOn); // Logs "2020-04-19T14:34:42.993Z" (same as savedOn).
console.log(company.updatedOn); // Logs null.
console.log(company.savedOn); // Logs "2020-04-19T14:34:42.993Z" (same as createdOn).
// On every following save, only the "updatedOn" and "savedOn" fields will be updated:
await company.save();
console.log(company.createdOn); // Logs "2020-04-19T14:34:42.993Z" (no change here).
console.log(company.updatedOn); // Logs "2020-04-19T14:37:05.726Z" (same as savedOn).
console.log(company.savedOn); // Logs "2020-04-19T14:34:42.993Z" (same as updatedOn).withCrudLogs is used with the withStorage function
In order to update the field values accordingly, the withCrudLogs internally utilizes the beforeSave, beforeCreate, and beforeUpdate hooks, which are exposed by the withStorage function.
Contributors
Thanks goes to these wonderful people (emoji key):
| Adrian Smijulj💻 📖 💡 👀 ⚠️ |
|---|
This project follows the all-contributors specification. Contributions of any kind welcome!