@composa/bookshelf-dates

Bookshelf plugin for converting dates

Usage no npm install needed!

<script type="module">
  import composaBookshelfDates from 'https://cdn.skypack.dev/@composa/bookshelf-dates';
</script>

README

Build Status codecov

bookshelf-dates

Automatically converts SQL dates to JavaScript Date objects.

const user = await User.where({ id: 1 }).fetch();
user.get('dateSubscribed') === new Date('1.1.2017') // true

Installation

Install via npm:

npm install --save @composa/bookshelf-dates

Install via Yarn:

yarn add @composa/bookshelf-dates

Usage

Register the plugin:

import Bookshelf from 'bookshelf';
import bookshelf-dates from '@composa/bookshelf-dates';
import Knex from 'knex';
import knexfile from './knexfile';

const knex = Knex(knexfile);
const bookshelf = Bookshelf(knex);

bookshelf.plugin(dates, { fields: ['dateCreated', 'dateDeleted'] });

Enable it on your models:

const User = bookshelf.Model.extend({
    tableName: 'users',
    convertDates: true
});

You can also define local settings:

const User = bookshelf.Model.extend({
    tableName: 'users',
    convertDates: ['dateSubscribed']
});

Testing

git clone git@github.com:composa/bookshelf-dates.git
cd bookshelf-dates && yarn install && yarn test