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

node-objection

v1.4.0

Published

A small library providing an easy to use API endpoint for storing persistent data in JSON files in express apps. This is configured out of the box to work with angular's $resource module, making it easy to get test data to and from your client code.

Downloads

12

Readme

Objection

"node-objection"

A small library for express providing an easy to use API endpoint for storing persistent data in JSON files in express apps. It also provides predefined schemas for simple configuration of normally complex tasks.

This is configured out of the box to work with angular's $resource module, making it easy to get test data to and from your client code.

Installation

npm install node-objection --save

Please be sure to create a /db directory in the root directory of your application or the database will not connect. This is where diskdb will store is JSON files. The JSON files are stored here by the name of the collection ie... objection("users") will create /db/users.json file.

Dependencies

  • lodash
  • diskdb
  • body-parser
  • express
  • morgan
  • bcrypt

Usage

var objection = require("node-objection")();

app.use("/employees", objection.collection("employees"));

This will produce an endpoint at /employees with the following restful methods:

  • GET /employees - This method returns an array of employees or an empty array if none exist.
  • GET /employees/:_id - This method returns one employee or an empty object if it is not found.
  • POST /employees - This method inserts an employee with the data sent in the request body, and returns the new employee.
  • PUT /employees/:_id - updates a employee with the _id parameter, and request body content, then returns an updated count.
  • DELETE /employees/:_id - This method deletes a employee with the _id param.

This allows you to configure any collection on the fly just by using the objection("[object]") format. For example... If i wanted to create an todo collection i would simply do the following.

var objection = require("node-objection");

app.use("/todos", objection.collection("todos"));

Filter and Sort

You can also use query parameters with the GET /[collection]. This makes sorting and filtering possible. To Filter a collection use the following.

GET /users/?lastName=Smith

This will return all users with the last name of Smith

[
	{fisrtName:"Bob", lastName:"Smith"}, 
	{fisrtName:"Nancy", lastName:"Smith"}, 
	{fisrtName:"Jason", lastName:"Smith"}
]

To sort a collection use the orderBy query parameter with the key you want to orderBy. Right now you can only order by asc.

GET /users/?orderBy=firstName

This will return the following.

[
	{fisrtName:"Bob", lastName:"Smith"}, 
	{fisrtName:"Jason", lastName:"Smith"},
	{fisrtName:"Nancy", lastName:"Smith"}
]

Predefined Schemas

Objection includes some predefined schema for common use cases. This allows you get up and running with your client side application without having wrestle with backend logic.

User Schema

The User Schema provides built in password hashing and authentication. To implement a user schema do the following:

var objection = require("node-objection");

app.use("/users", objection.user());

This provides an endpoint at /users with all of the RESTful methods mentioned above, a predefined schema, and some bonus features.

{
	email:[email],					// User Email, must be unique
	password:[hash of password],	// Password hashed
	username:[username],			// Username, must be unique.
	role:[role],					// The role of a user, defaults to "user".
	created:[date created],			// The date this user was created.
	updated:[date updated],			// The date this user was last updated.
	_id:[GUID]						// The unique identifier of this user.
}
  • If you send a password in the request body @ POST /users request, it will be hashed and stored, but not returned.
  • Anytime you update a user @ PUT /users/:_id the new password will be hashed and stored, and not returned.
  • For authentication purposes, the email & username key is set to unique. You will get a 400 status bad request if you try to store duplicate emails or usernames.
  • An additional route is made for authentication @ /users/login. This is route to post your email and password.
  • A token validation route is made @ /users/validate. This is route to validate your tokens.
  • A role key with a default value of "user" is added to each user so you can test roles and permissions in you app.

In addition to the db/user.json, the user schema also creates back end goodies for logging, and testing purposes.

  • /db/jwt.json If JSON Web Tokens are enable, this logs all json web token issued with the following schema.
	{
		user:[_id], 			// _id of the user the token was issued to.
		token:[access_token], 	// The actual token that was issued to the user.
		date:current_date,		// The date the token was issued
		expires:expires			/ The date the token expires
	};
  • /db/attempts.json This simply logs login attempts and
		{
			date:[moment date],	// The date a login attempt was made.
			user:[user],		// The user attempting to login. This is the username || email and pass
			allowed:[boolean],	// Did the login attempt succeed.
		};

Model API

Behind the scenes, there is is a model API that wraps the diskdb mongodb-like methods. These methods are mapped to the RESTful request as follows.

  • GET /[collection] - model.select(); - db.find();
  • GET /[collection]/[_id] - model.findOne({_id:_id}); - db.findOne({_id:_id});
  • POST /[collection]/[_id] - model.insert({data}); - db.save({data})
  • PUT /[collection]/[_id] - model.update([_id], {data}); - db.update([_id], {data});
  • DELETE /[collection]/[_id] - model.remove({_id:_id}); - db.remove({_id:_id});

The PUT, and DELETE, methods retrieve the _id parameter first from the query parameters. If it does not find it there, it will search the request body for an _id key. The key will be removed from the data before it is updated.

Contributing

Feel free to contribute to the library as you see fit. I have not created any test or error handling yet as this started as a library to help be build test API endpoints for angular's $resource module.

Road Map

Features I would like to implement in the future.

  • Implement token revocation on user schema.
  • Predefined Schema for common models such as user, role, and groups.
  • Pagination