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

quick-crud

v0.2.1

Published

A quick mongoose crud helper

Downloads

11

Readme

QuickCrud.js

An easy CRUD operation based on Factory pattern with Mongoose. There are four CRUD operations we can do:

  • index({mode, where, populateOptions, paginationOptions}) : Fetch all documents with pagination.

    • model - Mongoose model.
    • where - MongoDB filter object.
    • populateOptions - Mongoose population object/string.
    • paginationOptions can get three parameters.
      • limit:number - Resource count to show. Default is 10
      • page:number - Pagination page number. Default is 1.
      • sort:string - MongoDB property sort key. Default is '-createdAt'
  • store({model, data}) - You can create a doc and store it to MongoDB.

    • model - Mongoose model.
    • data - An object of data to store in MongoDB based on Mongoose Schema
  • show({model, where, populationOptions}): Fetch a single document via filter key.

    • model - Mongoose model.
    • where - MongoDB filter object.
    • populateOptions - Mongoose population object/string.
  • update({model, where, data}) - updates the first document that matches where. data is the object where you want to update the data.

    • model - Mongoose model.
    • where - MongoDB filter object.
    • data - An object of data to update that matches with where filter key(s).
  • destroy({model, where}) - Deletes the first documents that matches where from the collection. it returns the document that has been deleted.

    • model - Mongoose model.
    • where - MongoDB filter object.
  • destroyAll({model: where}) - Deletes all documents that matches where from the collection.

    • model - Mongoose model.
    • where - MongoDB filter object.

Examples

Store

const qc = require('quick-crud')

qc.store({model: UserModel, data: {
	name: 'John',
	username: 'johnDoe'
}}).then((d) => console.log(d))

Example Response

{
  _id: 'xxxx',
  name: 'John',
  username: 'johnDoe',
  __v: 0
}

Index

Fetch all resources with pagination

Fetch all users

const qc = require('quick-crud')

qc.index({model: UserModel}).then((doc) => {
	console.log(doc)
})

Response

{
  currentPage: 1,
  pageCount: 1,
  resourceCount: 4,
  data: [
    {
      _id: 'xxxxxx',
      name: 'User 1',
      username: 'username1',
      __v: 0
    },
    {
      _id: 'xxxxxx',
      name: 'User 2',
      username: 'username2',
      __v: 0
    },
    //..............
  ]
}

Fetch all posts with user polulation

const qc = require('quick-crud')

qc.index({model: PostModel, paginationOptions: {
	page: 2,
	limit: 5
} , populationOptions: {path : 'user'}}).then((doc) => {
	console.log(doc)
})

Response

{
	"currentPage": 2,
	"pageCount": 2,
	"resourceCount": 10,
	"data": [
		{
			"_id": "xxxx",
			"title": "example title 1",
			"body": "example body 1",
			"user": {
				"_id": "xxxx",
				"name": "User 1",
				"username": "username1",
				"__v": 0
			},
			"__v": 0
		},
		{
			"_id": "xxxx",
			"title": "example title 1",
			"body": "example body 1",
			"user": {
				"_id": "xxxx",
				"name": "User 2",
				"username": "username2",
				"__v": 0
			},
			"__v": 0
		}
		// ........................
		// ........................
	]
}

Fetch all posts those are published

qc.index({model: PostModel, where: { published: true }, paginationOptions: {
	page: 2,
	limit: 5
}}).then((doc) => {
	console.log(doc)
})

Response

{
  currentPage: 1,
  pageCount: 1,
  resourceCount: 3,
  data: [
    {
      _id: 'xxxx',
      title: 'post1',
      published: true,
      __v: 0
    },
    {
      _id: 'xxxx',
      title: 'post1',
      published: true,
      __v: 0
    },
    {
      _id: 'xxxx',
      title: 'post1',
      published: true,
      __v: 0
    }
  ]
}

Update

Update a post by id

const qc = require('quick-crud')

qc.update(
{	model: Post,
	where: { _id: 'xxxx' },
  	data: {
		title: 'title updated'
	}}
).then((doc) => console.log(doc))

Response

{
    _id: 'xxxx',
    title: 'title updated',
    body: 'example body 10',
    user: 'xxxx',
    __v: 0
}

Destroy

Delete a user with username

const qc = require('quick-crud')

qc.destroy({model: User, where: {
	username: 'newusername'
}}).then((doc) => {
	console.log(doc)
})

Response

{
  _id: 'xxxx',
  name: 'new name',
  __v: 0,
  username: 'newusername'
}

Throws exception if no document found

// Exception
UnhandledPromiseRejectionWarning: QuickCrudException: Resource not found