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

@disco/base-driver

v1.0.0

Published

Base model driver for disco

Downloads

26

Readme

@disco/base-driver

CI status Coverage Status

This is the base driver for disco which database-specific drivers should derive from. It provides a large and friendly API surface over a more limited selection of basics that can be worked with generically. For better performance, drivers may opt to override a larger selection of the API to make more efficient queries.

Usage

const BaseDriver = require('@disco/base-driver')

const data = {}

class MemoryDriver extends BaseDriver {
  static ensureData() {
    data[this.name] = data[this.name] || []
    return data[this.name]
  }

  // Called by model.fetch()
  async _fetch () {
    const data = this.ensureData()
    for (let item of data) {
      if (item.id === this.id) {
        return results[0]
      }
    }

    throw new Error(`Failed to fetch item #${this.id}`)
  }

  // Called by model.save()
  async _save () {
    const data = this.ensureData()
    const { length } = data
    data.push({ id: length + 1, ...this })
    return data[length].id
  }

  // Called by model.update()
  async _update () {
    return Object.assign(this._fetch(), this)
  }

  // Called by model.remove()
  async _remove () {
    const data = this.ensureData()
    for (let i = 0; i < data.length; i++) {
      if (data[i].id === this.id) {
        data.splice(i, 1)
        return
      }
    }

    throw new Error(`Failed to remove item #${this.id}`)
  }

  // Called by all find* operations, both singular and plural
  static async * findIterator (query) {
    for (const item of this.ensureData()) {
      if (objectContains(item, query)) {
        yield this.build(item)
      }
    }
  }
}

A driver must implement the _fetch, _save, _update and _remove instance methods along with the findIterator static method. All other model APIs include default implementations but can be overriden to allow for making more performant queries.

A driver may choose to implement additional helpful methods which are not specifically required by the disco base driver. A common example of this would be a count method to count records. This functionality is not strictly necessary for disco so it is not included in the base or required methods to implement, but it is often helpful to the user.

The driver functions as a base class for the models generated by disco and will have Model.name and Model.schema properties added to it. These can be used to

Model API

Statics

Model.build(data: Object) : Model

Build a model instance.

Model.create(data: Object): Promise

Build a model instance and save it.

Model.find(query: Object): Promise

Find an array of model instances matching a given query object.

Model.findOne(query: Object): Promise

Find one model instance matching a given query object.

Model.findById(id: ID): Promise

Find one model instance by id. The id should be whatever type the driver expects. Some drivers have string ids others have numeric ids.

Model.findOrCreate(data: Object): Promise

Find or create a model instance given a set of data.

Model.createOrUpdate(query: Object, changes: Object): Promise

Create or update a model instance given a query and change set.

Model.update(query: Object, changes: Object): Promise<Array>

Update any records that match the query with the given change set.

Model.updateById(id: ID, changes: Object): Promise

Update a record by id with the given change set.

Model.remove(query: Object): Promise<Array>

Remove any records that match the query. This will return the records with their IDs cleared, allowing them to be saved again to create new records, if necessary.

Model.removeById(id: ID): Promise

Remove a record by id. This will return the record with the id cleared, allowing it to be saved again to create a new record.

Properties

model.isNew

This property is mostly used internally to detect if a model exists in the database already. Currently, it simply checks for existence of a _id property.

Methods

model.save(): Promise

Insert new models or update already persisted models.

model.update(changes: Object): Promise

Apply the input data to the model and save it.

model.remove(): Promise

Remove the model from the database.

model.fetch(): Promise

Fetch the latest model state from the database.

Hooks

There are several async hook methods that can be overridden to trigger things before or after various interactions. These methods include:

  • beforeSave
  • beforeCreate
  • beforeUpdate
  • beforeRemove
  • beforeValidate
  • afterSave
  • afterCreate
  • afterUpdate
  • afterRemove
  • afterValidate

License

MIT