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

hive-model-mongoose

v0.0.3

Published

A model class that merged mongoose with the hive-model structure

Downloads

2

Readme

Hive-model-mongoose

Hive-model-mongoose is a superclass for mongoose model. It exists for several purposes:

  1. to create a more REST-like API for the mongoose model methods
  2. to insulate all of the mongoose model methods from your particular model class to reduce namespace collision.
  3. to bundle a set of features with all models, including archiving and soft deletion

Note that in hive-model-mongoose, the assumption is that the models functions, not the records' native functions, are called.

Added Functionality

All models have "soft deletion" built in to the delete method. if Model.delete(record, callback, true) is called, the models' delete flag is set to true, but the record itself is preserved.

Also, you can archive your data -- clone its properties into an _archive array - if you include the property _archive ['mixed'] in your schema. This is a handy way to "back up" your data before updating it.

API

Hive_Model_Mongoose is a hive-component (https://github.com/bingomanatee/hive-component). Instances have the mongoose Model in their model property.

Constructor

Mongoose_Model is a factory that returns the mongoose_model via callback (and directly).


Mongoose_Model(
    {
        name: 'tribbles'
    } // mixins
    , {
        mongoose:   mongoose,
        schema_def: object || mongoose.Schema || path_to.jso
    } // configurations
    , dataspace // hive-model.Dataspace || Object (optional)
    , callback (optional)
    );

Mixins {object}

Any methods you want to attach to the model go here. The only requirement is the name property, which must be a unique string (unique to your database/the dataspace)

Configuration {Object}

as a hive-component, a hive-model has a configuration registry that can be accessed with

  • model.get_config(key):value,
  • model.set_config(key, value)
  • model.has_config(key):boolean

There are two required configurations:

  1. mongoose -- an instance of your Mongoose module
  2. schema_def -- either a Mongoose Schema, an object, or a path to a JSON file.

Dataspace {hive-model.Dataspace || Objet}

Dataspace can be a formal model registry (as part of the hive-model system) or a basic object that you want to use to collect your models by name. You can pass a naked object {} in as well.

Callback function(err, my_mongoose_model)

a function that returns the model.

add([records] {array}, callback{function}, as_group{boolean});

Adds a series of objects to the collection. Can be raw objects, Documents, or a combination. If records is not an array, it routes to put(..). If as_group != true, it feeds records one at a time to put and returns the aggregate results to the callback. If it is true, it uses this.model.collection.insert -- meaning no property validation is done, and the callback will not receive the new documents.

put ( doc {mongoose.Document || Object}, options {object}, callback {function}) || (doc, callback)

post " "

Inserts or updates a single document.

get (id, fields, options, callback)

Gets a single document, by ID; a passthrough to findById.

revise(data {object}, callback{function})

update a single document's selected fields. data must have an _id property. The other fields update the content of the object on a field by field basis.

  • if your data has a __REMOVE property, whose values are an array of strings, those properties are removed from the document. However if the property is an array, it is emptied rather than deleted.

all(callback {function} [optional])

returns the entire collection in the callback, or a query.

active(callback {function} [optional])

returns the records collection in the callback, or a query. Respects soft deletes.

inactive(callback {function} [optional])

returns the soft-deleted records in the collection in the callback, or a query.

find (crit, field, options, callback)

passthrough to Mongoose Model.find

find_one (crit, field, options, callback)

passthrough to Mongoose Model.findOne

count()

passthrough to Mongoose Model.count

empty()

drops the entire collection