npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

qute-bookshelf-repository

v0.0.2

Published

A simple repository that manages a **`single`** [bookshelf-modelbase](https://github.com/bsiddiqui/bookshelf-modelbase) model which is in turn a [bookshelf model](http://bookshelfjs.org/). This repository provides many commonly used methods that are fou

Downloads

9

Readme

# Qute Bookshelf Repository

A simple repository that manages a single bookshelf-modelbase model which is in turn a bookshelf model. This repository provides many commonly used methods that are found in repository classes / objects This repository should be used as as abstraction layer between the bookshelf model and services to prevent the bookshelf models from leaking into services (i.e. encourages loose coupling) This module should be extended / inherited to add additional functionality qute-bookshelf-repository will return JSON objects by default. This behaviour can be controlled by the bConvertResultToJson (defaults to true) variable that is passed to every method in the API . If bConvertResultToJson is set to false, the bookshelf model will be returned.

Unless there is a really good reason to do so, you should not be returning the bookshelf model which is passed to each method

### NOTE

This module wraps the original bookshelf-modelbase/ bookshelf errors with quantal-errors. The reason being that it makes handling certain bookshelf-modelbase / bookshelferrors easier e.g. (Collection.EmptyError and Model.NotFoundError are simply coerced to NotFoundError)

Install

npm install quantal-base-model

Example Usage

The bookshelf model

// giphy-model.js
'use strict'
const bookshelf = require('./../../db')
const ModelBase = require('bookshelf-modelbase')(bookshelf)

const Giphy = ModelBase.extend({
  tableName: 'giphys',
  idAttribute: 'giphy_id'
})

module.exports = bookshelf.model('Giphy', Giphy)

The repository to manage the model

// giphy-repository.js
'use strict'
const BaseRepository = require('qute-bookshelf-repository')
const GiphyModel = require('./models/giphy-model')

class GiphyRepository extends BaseRepository {
  /**
   * Repositories must call BaseRepository constructor
   */
  constructor () {
    super(GiphyModel)
  }
}

module.exports = GiphyRepository

The usage of the repository

// giphy-service.js

'use strict'
const GiphyRepository = require('./giphy-repository')
const giphyRepository = new GiphyRepository()

module.exports = Object.freeze({

  findOrCreate (data) {
    return giphyRepository.findOrCreate(data)
  }
})

API

constructor

 /**
   *
   * @param model {Object}  - The type (class type) of the model that this repository manages e.g. MyModel
   */
  constructor (model)

repository.findOne

/**
      * Returns a single instance of a model
      * @param filter - the filter to pass fetch
      * @param options {Object} - the options to pass to model.fetch
      * @param {Boolean} [bConvertResultToJson=true] - if true, result will be converted to JSON, otherwise Bookself model instance will be returned
      * @returns {Promise}
      */
     findOne (filter, options, bConvertResultToJson = true)

repository.create

 /**
       * Naive add - create and save a model based on data
       * @param {Object} data
       * @param {Object} options (optional)
       * @param {Boolean} [bConvertResultToJson=true] - if true, result will be converted to JSON, otherwise Bookself model instance will be returned
       * @return {Promise(bookshelf.Model)} single Model
       */
      create (data, options, bConvertResultToJson = true)

repository.destroy

      /**
       * Naive destroy
       * @param {Object} options - The bookshelf destroy options
       * @param options.id {Number|String} - The id of the model to be deleted
       * @param {Boolean} [bConvertResultToJson=true] - if true, result will be converted to JSON, otherwise Bookself model instance will be returned
       * @return {Promise(bookshelf.Model)} empty Model
       */
      destroy (options, bConvertResultToJson = true)

repository.findAll

 /**
       * Naive findAll - fetches all data for `this`
       * @param {Object} filter - The filter/ query that will be applied to the find all query
       * @param {Object} [options] - The bookshelf destroy options
       * @param {Boolean} [bConvertResultToJson=true] - if true, result will be converted to JSON, otherwise Bookself model instance will be returned
       * @return {Promise(bookshelf.Collection)} Bookshelf Collection of all Models
       */
      findAll (filter, options, bConvertResultToJson = true) 

repository.findWhere

    /**
       * Naive findWhere - fetches all data for `this`. This method is an alias for findAll
       * @param {Object} filter - The filter/ query that will be applied to the find all query
       * @param {Object} options (optional)
       * @param {Boolean} [bConvertResultToJson=true] - if true, result will be converted to JSON, otherwise Bookself model instance will be returned
       * @return {Promise(bookshelf.Collection)} Bookshelf Collection of all Models
       */
      findWhere (filter, options, bConvertResultToJson = true)

repository.findById


      /**
       * Find a model based on it's ID
       * @param {String | Number} id The model's ID
       * @param {Object} [options] Options used of model.fetch
       * @param {Boolean} [bConvertResultToJson=true] - if true, result will be converted to JSON, otherwise Bookself model instance will be returned
       * @return {Promise(bookshelf.Model)}
       */
      findById (id, options, bConvertResultToJson = true)

repository.findOrCreate

    /**
       * Find or create - try and find the model, create one if not found
       * @param {Object} data - The data to use to find a model. If The model does not exist, this data will be persisted
       * @param {Object} options - The options thar are passed to fetch and create
       * @param {Boolean} [bConvertResultToJson=true] - if true, result will be converted to JSON, otherwise Bookself model instance will be returned
       * @return {Promise(bookshelf.Model)} single Model
       */
      findOrCreate (data, options, bConvertResultToJson = true)

repository.update

      /**
       * Naive update - update a model based on data
       * @param {Object} data - The data to be used to update a model
       * @param {Object} options - The options to pass to save
       * @param {Boolean} [bConvertResultToJson=true] - if true, result will be converted to JSON, otherwise Bookself model instance will be returned
       * @return {Promise(bookshelf.Model)} edited Model
       */
      update (data, options, bConvertResultToJson = true)

repository.upsert

      /**
       * Upsert - select a model based on data and update if found, insert if not found
       * @param {Object} selectData - Data for select
       * @param {Object} updateData -  Data for update
       * @param {Object} [options] options - for model.save
       * @param {Boolean} [bConvertResultToJson=true] - if true, result will be converted to JSON, otherwise Bookself model instance will be returned
       * @return {Promise(bookshelf.Model)} edited Model
       */
      upsert (selectData, updateData, options, bConvertResultToJson = true)

Licence

MIT