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

@dinamodigitale/resourcify

v1.0.9

Published

Resourcify is a library for creating RESTful APIs for mongoose models in express

Downloads

23

Readme

Resourcify

A Library for creating RESTful API with mongoose and express

⚠️ This project is in the Alpha stage. All API's might change without warning and no guarantees are given about stability. Do not use it in production.

Installation

npm install @dinamodigitale/resourcify

Usage

You can check the basic example from the src/examples directory or paste this in a js/ts file

import bodyParser from 'body-parser';
import express from 'express';
import mongoose from 'mongoose';
import { resourcify } from '../../resourcify';
import { Author, Post } from './models';

const app = express()
const port = 3000

mongoose.connect('mongodb://localhost/test');

app.use(bodyParser.json())

app.use('/admin', resourcify(Post, {
  sort: {
    _id: -1
  },
  populate: {
    show: [{
      path: 'author',
    }],
    update: [{
      path: 'author',
    }]
  },
  select: {title: 1}
}))

app.use('/posts', resourcify(Post, {
  declareRouteFor: ['index', 'show'],
  sort: {
    _id: -1
  },
  populate: {
    show: [{
      path: 'author',
    }],
    update: [{
      path: 'author',
    }]
  },
  select: {title: 1}
}))

app.use('/authors', resourcify(Author, {
  pagination: true,
}))

app.listen(port, () => {
  console.log(`Example app listening on port ${port}`)
})

Options

Sorry for being lazy, I am pasting the interface here

ResourcifyOptionsInterface {
  /**
   * Policies list, this is deisgnated to be used for authorization only like roles, auth, etc.
   * @example create: [IsAuthorized, (req,res,next) => req.something ? next() : res.status(401).send('Unauthorized')]
   */
  policies?: {
    create?: RequestHandler | RequestHandler[]
    index?: RequestHandler | RequestHandler[]
    show?: RequestHandler | RequestHandler[]
    update?: RequestHandler | RequestHandler[]
    delete?: RequestHandler | RequestHandler[]
  }
  /**
   * If true, only the routes index, resource and show will be declared
   */
  readOnly?: Boolean
  /**
   * Array of actions to be declared
   * @example ['index', 'show']
   */
  declareRouteFor?: Array<ResourcifyActions>
  /**
   * Mongoose populate options
   * @example {index: [{ path: 'user', select: 'name' }]}
   */
  populate?: {
    index?: PopulateOptions | PopulateOptions[];
    show?: PopulateOptions | PopulateOptions[];
    create?: PopulateOptions | PopulateOptions[];
    update?: string | PopulateOptions | PopulateOptions[];
  }
  /**
   * Mongoose FilterQuery to be passed for index or show
   * @example {index: { $or: [{ name: 'John' }, { name: 'Jane' }] }}
   */
  query?: {
    index?: (req: Request) => FilterQuery<unknown>;
    show?: (req: Request) => FilterQuery<unknown>;
  }
  /**
   * Sorting object
   * @example {_id: -1}
   */
  sort?: Record<string, 1 | -1>
  /**
   * Select fields (projection) object
   * @example {_id: 1, name: 1}
   * @example {password: 0}
   */
  select?: Record<string, 1 | 0> | string[] | string
  /**
   * Enable pagination for index, will use req.body.offset and req.body.limit or req.query.offset and req.query.limit
   */
  pagination?: boolean
  /**
   * Middleware to be executed before the route handler
   * @example {create: [(req, res, next)=>next()]}
   */
  middleware?: {
    create?: RequestHandler | RequestHandler[]
    index?: RequestHandler | RequestHandler[]
    show?: RequestHandler | RequestHandler[]
    update?: RequestHandler | RequestHandler[]
    delete?: RequestHandler | RequestHandler[]
  }
}

Routes

Here is a list of all the routes that the library supports:

Create

POST /routename Create a new document.

Example:

curl --request POST \
 --url http://localhost:3000/authors \
 --header 'Content-Type: application/json' \
 --data '{
   "name": "Author 1645715862"
}'

Update

PATCH /routename/:id Update a document by id.

Example:

curl --request PATCH \
 --url http://localhost:3000/posts/621490fe3115044c1af7dd7c \
 --header 'Content-Type: application/json' \
 --data '{
   "title": "Bla bla bla"
}'

Delete

DELETE /routename/:id Delete a document by id.

Example:

curl --request DELETE --url http://localhost:3000/posts/621490fe3115044c1af7dd7c

Index

GET /routename Get a list of documents from a collection.

Example:

curl --request GET --url http://localhost:3000/posts

One document

GET /routename/:id Get a document by id

Example:

curl --request GET --url http://localhost:3000/posts/621490fe3115044c1af7dd7c

Resource

POST /routename/resource Build query, section, projection, pagination, sort on a collection

Example:

curl --request POST \
 --url http://localhost:3000/posts/resource \
 --header 'Content-Type: application/json' \
 --data '{
   "limit": 10,
   "sort": {"_id": -1},
   "populate": "author",
   "offset": 0,
   "query": {
   	"author": { "$ne": null }
   },
   "pagination": true
}'

Note that the pagination details are injected on the response header as follows:

x-limit	10
x-offset	0
x-total-records	1
x-total-pages	1

License

MIT

Author

@moty66, Dinamo Digitale