Skip to content
This repository has been archived by the owner on Apr 17, 2020. It is now read-only.

Commit

Permalink
Merge pull request #91 from knorm/v2
Browse files Browse the repository at this point in the history
V2
  • Loading branch information
joelmukuthu committed Feb 3, 2019
2 parents e5814ea + 93d9d8d commit 2fdc284
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 96 deletions.
55 changes: 3 additions & 52 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,56 +7,7 @@

Timestamps plugin for [@knorm/knorm](https://www.npmjs.com/package/@knorm/knorm).

This plugin adds the [knex timestamp](http://knexjs.org/#Schema-timestamps)
fields to your models and also updates your query methods so that `createdAt`
and `updatedAt` are set to the current time (i.e. `new Date()`) for `insert`
calls and the `updatedAt` field is set to the current time for any `update`
calls. It will also ensure that any `update` calls do not overwrite the
`createdAt` field.
## [Documentation](https://knorm.netlify.com/plugins/timestamps.html)

## Installation

```bash
npm install --save @knorm/knorm @knorm/timestamps
```

> @knorm/timestamps has a peer dependency on [@knorm/knorm](https://www.npmjs.com/package/@knorm/knorm)
## Usage

```js
const knorm = require('@knorm/knorm');
const knormTimestamps = require('@knorm/timestamps');

const orm = knorm({
// knorm options
}).use(
knormTimestamps({
// knormTimestamps options
})
);
```

## Options

### name

The name of the plugin, defaults to `'timestamps'`.

### createdAt

> type: object, default: `{ name: 'createdAt', column: 'created_at' }`
The `createdAt` field can be configured with these options:

* `name` _string, default: createdAt_: the field name
* `column` _string, default: created_at_: the column name

### updatedAt

> type: object, default: `{ name: 'updatedAt', column: 'updated_at' }`
The `updatedAt` field can be configured with these options:

* `name` _string, default: updatedAt_: the field name
* `column` _string, default: updated_at_: the column name
View the docs on @knorm/knorm's documentation site
[here](https://knorm.netlify.com/plugins/timestamps.html).
11 changes: 8 additions & 3 deletions lib/KnormTimestamps.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,16 @@ class KnormTimestamps {
}

updateModel(knorm) {
knorm.Model = class TimestampsModel extends knorm.Model {};
knorm.Model.fields = this.fields;
class TimestampsModel extends knorm.Model {}

TimestampsModel.fields = this.fields;

knorm.updateModel(TimestampsModel);
}

updateQuery(knorm) {
const { updatedAtFieldName } = this;

class TimestampsQuery extends knorm.Query {
async update(data, options) {
if (data) {
Expand All @@ -36,7 +40,8 @@ class KnormTimestamps {
return super.update(data, options);
}
}
knorm.Model.Query = knorm.Query = TimestampsQuery;

knorm.updateQuery(TimestampsQuery);
}

init(knorm) {
Expand Down
52 changes: 18 additions & 34 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 4 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,16 @@
"test": "docker-compose run --rm knorm-timestamps",
"test:mocha": "mocha --exit --recursive",
"coverage": "nyc npm run test:mocha",
"coveralls": "nyc report --reporter=text-lcov | coveralls",
"postgres": "docker-compose exec postgres echo 'postgres running ...' || docker-compose up -d postgres",
"postgres:stop": "docker-compose stop postgres",
"postgres:remove": "docker-compose rm -sf postgres"
"coveralls": "nyc report --reporter=text-lcov | coveralls"
},
"author": "Joel Mukuthu <joelmukuthu@gmail.com>",
"license": "MIT",
"peerDependencies": {
"@knorm/knorm": "^1.9.0"
"@knorm/knorm": "^2.0.0"
},
"devDependencies": {
"@knorm/knorm": "1.11.2",
"@knorm/postgres": "1.3.4",
"@knorm/knorm": "2.0.0",
"@knorm/postgres": "2.0.0",
"@semantic-release/changelog": "3.0.2",
"@semantic-release/git": "7.0.8",
"@semantic-release/github": "5.2.10",
Expand Down

0 comments on commit 2fdc284

Please sign in to comment.