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

mongo-crudify

v1.1.6

Published

Generates crud operations for given mongoclient

Downloads

26

Readme

Build Status Coverage Status

Mongo-Crudify:

Mongo-crudify is a library that allows to generate CRUD dao for given collection and database. Also it allows to add custom operations via operators auto-generation. // Since 1.1.0

Crudify provise next methods by default:

  1. findAll - returns all documents;
  2. findOne - find one document by using _id;
  3. insertOne - insert new record;
  4. deleteOne - delete one by _id;
  5. updateOne - update all fields except _id . _id field used in find query.

All methods return promise as a result.

Install

npm i mongo-crudify

How to use

Init connection:

const {MongoConnector} = require('mongo-crudify');
MongoConnector.init('mongodb://localhost:27017', {}); // Here you can specify all MongoOptions 

Create repository

const {crudify} = require('mongo-crudify');
const Crudify = crudify('test', 'testCollection', ['findAuthorByTitle']);
// Here we create repository with predefined crud methods and one additional that will
// returns document with exactly one field using title as a filter

API

Crudify factory method has 3 params: crudify(db name, collection name, [operations])

Operations field is an array of strings where you can specify operators that will be automatically generated. Currently available signature is: find + Projection Fields + By + Filter Query Fields + Order By + Sort Fields All fields should be specified starting with a uppercase letter.

  1. Projection fields (should be separated by And) - retains only these fields in document. If id is not specified retains id, otherwise use it as _id
  2. Filter query fields (should be separated by And): currently available only equality statements. Id will be replaced with _id automatically.
  3. Sort query (each fields should end with Asc or Desc): used to sort output documents.

Example: findIdByAuthorAndText - returns all ids that match given author and text

crudify.use(<operation>, plugin) - you can define your plugin in order to add some behavior to existing operation crudify.register(named-function | , function) - you can specify new action that will available to be augmented with plugins

Currently available plugins

plugins/created-at - allows to add createdAt field to inserted documents plugins/modified-at - allows to add modifiedAt field to updated documents

Examples:

1. Crud for users collection.

// users.js
   const {MongoConnector, crudify} = require('mongo-crudify');
   MongoConnector.init('mongodb://localhost:27017', {}); // Here you can specify all MongoOptions
  
   const [dbName, collection] = ['auth', 'users'];
   module.exports = crudify(dbName, collection);
   
// app.js
   const UserModel = require('./model/users');
   
   app.get('/users', async (req, res) => {
      const users = await UserModel.findAll();
      res.json({data: users});
   });
   //... other crud operations omitted

2. Add custom operation to crudified dao e.g. findByUsername.

const {MongoConnector} = require('mongo-crudify');
MongoConnector.init('mongodb://localhost:27017', {}); // Here you can specify all MongoOptions

const dao = crudify(dbName, collection);
dao.findByUsername = async function(username) {
    const client = await MongoConnector.client;
    return client.db(dbName).collection(collection)
        .findOne({
            username
        });
};
module.exports = dao;

3. Usage of created-at plugin

Lets consider that you need to persist object's date of creation. You can get rid of boilerplate code by using created-at plugin that was introduce in v1.0.5:

const {crudify,plugins, MongoConnector} = require('mongo-crudify');
const {createdAt} = plugins;
MongoConnector.init('mongodb://localhost:27017', {}); // Here you can specify all MongoOptions

const crud = crudify('test', 'todo');
crud.use(createdAt());

crud.insertOne({
        title: 'What is Lorem Ipsum?',
        text: `Lorem Ipsum is si.....`
 });

// this method will insert new object along with new field createAt
// Notes: you can specify fieldName by passing  fieldName option to createdAt {fieldName: myDateField}

4. Usage operation recognition for generation the findOne method:

Lets consider that you need to find article's text by author and it should be only one article.

const {crudify, MongoConnector} = require('mongo-crudify');

MongoConnector.init('mongodb://localhost:27017', {}); // Here you can specify all MongoOptions

const crud = crudify('test', 'articles', 'findOneTextByAuthor');
crud.use(createdAt());

crud.findOneTextByAuthor('Smith').then(text => {
    // here we get the text of the article that was written by Smith
});

Changes

1.1.1

  1. Add support of the findOne query. FindOne query supports projection, sort and filtering.

1.1.0

  1. Introduce mongo-connector that will be a bridge between Mongo and Mongo-Crudify library There is no reason to manually initiate the connection and pass it to crudify method
  2. Introduce operators auto-generation.

1.0.8

  1. Completely rewrite to typescript. Now it is available to use both js/ts
  2. Extract all operations to separate modules
  3. Now operations don't depend on mongo driver and will use only collection as an input

1.0.7

  1. Fix issue with single instance of the middleware list

1.0.6

  1. Fix not working plugins
  2. Add typescript definition files

1.0.5:

  1. Add plugins support
  2. Add created-at plugin
  3. Add modified-at plugin