Skip to content
This repository has been archived by the owner on Aug 10, 2021. It is now read-only.

composa/bookshelf-dates

Repository files navigation

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