Research
Security News
Malicious npm Package Targets Solana Developers and Hijacks Funds
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
node-objection
Advanced tools
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.
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.
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.
var objection = require("node-objection")();
app.use("/employees", objection.collection("employees"));
This will produce an endpoint at /employees with the following restful methods:
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"));
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"}
]
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.
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.
}
In addition to the db/user.json, the user schema also creates back end goodies for logging, and testing purposes.
{
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
};
{
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.
};
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.
model.select();
- db.find();
model.findOne({_id:_id});
- db.findOne({_id:_id});
model.insert({data});
- db.save({data})
model.update([_id], {data});
- db.update([_id], {data});
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.
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.
Features I would like to implement in the future.
FAQs
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.
We found that node-objection demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.