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

@leonardosarmentocastro/crud

v2.1.3

Published

crud

Downloads

32

Readme

crud

create endpoints for mongoose models

const mongoose = require('mongoose');
const server = require('@leonardosarmentocastro/server');
const i18n = require('@leonardosarmentocastro/i18n'); // mandatory
const { crud } = require('@leonardosarmentocastro/crud');

(async () => {
  const api = await server.start(8080, {
    middlewares: (app) => {
      i18n.connect(app); // mandatory
    },
    routes: (app) => {
      const schema = new mongoose.Schema({ name: String });
      const model = new mongoose.model('Product', schema);
      crud.connect(app, model); // creates "[GET, POST] /users" + "[GET, PUT, DELETE] /users/:id"
    },
  });
})();

then see it yourself

const got = require('got');

const PRODUCT1 = { name: 'product1' };
const url = (id = '') => `http://127.0.0.1:8080/products${id ? '/' + id : ''}`;

(async () => {
  const response1 = await got.post(url(), { json: PRODUCT1 }); // "[POST] /products"
  const { _id, name } = response1.body; // crud [C]reated

  const response2 = await got(url(_id)); // "[GET] /products/:id"
  console.log('products:', response2.body); // crud [R]ead...
})();

api

crud.connect(app = express(), model = mongoose.model())

Connects a mongoose model to a set of CRUD (create, read, update, delete) routes.

  • app: an express app (const app = require('express')())
  • model: a mongoose model (which represents a document in your MongoDB)

e.g.

const mongoose = require('mongoose');
const { crud } = require('@leonardosarmentocastro/crud');

const app = require('express')();
const schema = new mongoose.Schema({ name: String });
const model = new mongoose.model('Product', schema);
crud.connect(app, model);

// following endpoints are created:
// [GET] /users (comes with "@leonardosarmentocastro/pagination")
// [POST] /users
// [GET] /users/:id
// [PUT] /users/:id
// [DELETE] /users/:id

peer dependencies

  • @leonardosarmentocastro/pagination
  • @leonardosarmentocastro/i18n

translations

its mandatory to set these translations codes to their respective files:

  • ERROR_DOCUMENT_NOT_FOUND
  • ERROR_UNEXPECTED

e.g.

# ./translations/en-us.yml
ERROR_DOCUMENT_NOT_FOUND: Document for id "{id}" was not found.
ERROR_UNEXPECTED: Sorry, an unexpected error has occurred.

# ./translations/pt-br.yml
ERROR_DOCUMENT_NOT_FOUND: Documento para id "{id}" não foi encontrado.
ERROR_UNEXPECTED: Desculpe, ocorreu um erro inesperado.

thats it.