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

crud-clues

v0.0.2

Published

generic crud implementation using mongoose and clues.

Downloads

6

Readme

Example CRUD with clues

clues-crud is a very flexibile implemention of CRUD on top of mongoose. In the default setting you just need to provide a $model and define a function for where user_input comes from (if any) and how the _id of each query is defined.

Example:

crud({
  $model : exampleModel,
  user_access : {},
  input_data : ['input.exampleInput', Object],
  id : ['input.id', Object]
})

The following CRUD commands are available:

  • create - creates a record if one doesn't exist already
  • read - returns saved data (it exists)
  • update - update a record (if exists)
  • delete - deletes a record

A more advanced endpoint is valid_data. This endpoint allows for a final transform/validation and returns either the input data that was just saved (if valid input data was provided and saved successfully) or the most recently saved data (in any other case).

There are two points of validation, the first one on the way in valid_input and the second one on the way out valid_data. Usually you only need to define one of them, depending on whether you want partial/unvalidated data saved or not. If valid_input is a simple passthrough and valid_data performs the validation, any changes/updates are saved but further use of the data will be trapped by the valid_data validator. If 'valid_input' performs comprehensive validations, updates will not be saved unless they pass validation.

Crud dependency graph

Dependency graph

API Reference

$model (required)

A placeholder for the mongoose data model

user_access (required)

Should return an object that constrains any queries to objects that should be accessable to this user. For more fine-grained control you can define read_access and write_access (both by default mirror the user_access object). For unlimited access simply set user_access to an empty object {}.

input_data (required)

Custom function required to provide the set of inputs for any update/create. Example:

input_data : ['input.data_entry', Object]

valid_input (optional)

This function checks the input_data for potential errors before passing the data to update/create. By default, this function goes through the mongoose validators and throws an error with object xflash containing the field-level messages. It also enforces the defined mongoose models (fields that are not in the model aren't propagated).

id (optional)

The string representation of the mongo id. By default this value is fetched from either the submitted input object or input.id. Should be overwritten to provide a more precice mapping.

mongo_id (internal)

Function that converts the string id into a proper ObjectId if necessary.

query (optional)

The default query is simply a search for an _id that matches the supplied mongo_id. In some cases you need to overwrite the query function to use a different a different unique identifier than _id (for example when multiple users might have overlapping unique identifiers for any records).

create

This function creates a record from valid_input if the record doesn't already exist and input was successfully validated.

read

Reads a single record and returns the result.

update

Updates a record found with 'query' using 'valid_input'. If the record does not exists, validation failed or the user has no access, an error is thrown.

delete

Deletes a record specified with 'query'

save

Saves the record, either by updating a previously existing record or by creating a new one (if a corresponding record does not exists)

data

Returns either the updated/created data (if valid_input was provided and save successful) or the previously saved data (if a record exists).

valid_data (optional)

This is a placeholder for any validation of the data coming out.