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

morepo

v1.0.3

Published

A JavaScript Mongoose Repository class

Downloads

23

Readme

Mongoose-Repo (morepo)

A Mongoose Repository Class

Build Status Dependency Status NSP Status

Requirements

Make sure you're using Nodejs 8.x or higher, for async/await to work

If you're working on Nodejs 6.x, please install morepo version 0.8

Installation

You can install this module with NPM:

npm install --save morepo

Getting started

You can use this class like it is or make a specific Repository class that extends this class

Usecase 1:

const mongoose = require('mongoose');
const Repository = require('morepo');

const postRepo = new Repository(mongoose.model('Post'));

Usecase 2:

const mongoose = require('mongoose');
const Repository = require('morepo');

class PostRepository extends Repository {

    constructor(model) {
        super(model || mongoose.model('Post'));
    }

    // Override an existing function to add some custom functionality
    async findByObjectId(objectId) {
        try {
            if (!objectId) {
                return {error: 'No ObjectId given'};
            }

            const body = await this.find(
                {_id: objectId},
                this.generateOptions({multiple: false})
            );
            if (body === null) {
                return {error: 'No posts found'};
            }
            return body;
        } catch (error) {

            return {error: error.message};
        }
    }

    generateOptions(options) {
        const base = {
            populate: [
                {path: 'author', select: '-password -registration_date -__v'},
                {path: 'comments'}
            ],
            sort: {date_created: 'descending'},
            multiple: true
        };
        return super.generateOptions(base, options);
    }
}

const postRepo = new PostRepository(mongoose.model('Post'));

Functions

Constructor

Creates a new instance of the Repository

Params

  • model - Mongoose Model
  • Options:
    • applyStatics - default: true Applies all static model functions to the Repo

Returns

New instance of the Repository

create

Params

  • obj - Object to save to the database

Returns

Given object saved to the database

find

Executes the given query

Params

  • query - Object
  • Options:
    • select
    • populate
      • If something else than an Array, String or Object is given, the value is skipped
      • Examples Populate examples: String: created_by Object (with multilevel populate):
        {
           path: 'friends',
           // Get friends of friends - populate the 'friends' array for every friend
           populate: { path: 'friends' }
        }
      Array: [One of the 2 above]
    • limit
    • skip
    • sort
    • lean - default: false
    • count - default: false
    • multiple - default: true

Returns

Query result If multiple is true: it'll return an Array, else it'll return an Object

findByObjectId

Looks for 1 document based on the objectId

Params

  • query - Object
  • Options:
    • select
    • populate
    • lean - default: false

Returns

The request document

findAll

Retrieves all documents in that collection

Params

none

Returns

Array with all documents in that collection

update

Retrieves a document by it's ID, assigns the new values to it (thanks you lodash) and saves it

Params

  • objectId - Object || String
  • newValues - Object

Returns

The updated document

remove

Removes the document identified by the given ObjectID from the collection

Params

  • objectId - Object || String

Returns

The remove's result

Roadmap

v1.0.0

  • Tests
  • Folder with examples
  • Better Doc and readme