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

@neode/express

v0.1.0

Published

Neode REST endpoints for Express

Downloads

3

Readme

@neode/express

The idea behind this repository is remove the CRUD boilerplate around Express applications interacting with Neo4j by providing a simple API to build Resource Controller around your neode models.

Usage

With one extra dependency and one line of code, you can quickly add a set of REST endpoints.

Say for example, you have a graph of (:User)s and their (:Skill)s - you would have a models directory with two files:

Then index.js would look a little like this:

const express = require('express')
const bodyParser = require('body-parser')

const neode = require('neode')
    .fromEnv()
    .withDirectory(__dirname +'/models')

const app = express()

app.use(bodyParser.json())
// ...

By adding two lines, you can create a set of REST endpoints that use the information defined in the Neo4j models to validate and process the requests.

const resource = require('@neode/express')

app.use('/api/users', resource(neode, 'User'))

The resource function

This takes two arguments, first your neode instance, and secondly the name of the model. That's it. You'll be able to GET /api/users to retrieve a list of User nodes, POST /api/users to create a new user, GET /api/users/{primary key} to view an individual resource PUT /api/users/{primary key} to update the node, and DELETE /api/users/{primary key} to delete the node.

Endpoints

GET / - List

The list endpoint will return a paginated list of nodes. The list can be filtered based on any property that is listed in the model definition as an index, unique or a primary key. All you need to do is pass it as part of the query string.

Key | Action | Notes -- | -- | -- order | Property name to order by sort | Order to return the records | asc or desc limit | Number of records to return | Default is 10 page | Page number to return | Offset is calculated as page-1 * limit

POST / - Create

Posting to the root will attempt to create a node based on the neode model definition. If any validation fails, the server wll return a 422 status code and an object including details - a list of the Joi validation errors.

This takes a combination of the post body and request params - so for example, you could define the primary key of the parent object into the URL.

// Post.js
module.exports = {
    body: 'string',
    user: {
        type: 'node',
        target: 'User',
        relationship: 'POSTED',
        direction: 'in',
    },
}

// Resource
api.use('/api/users/:user/posts', resource(neode, 'Post'))

// Application call
axios.post('http://localhost:3000/api/users/user', { data: { content: 'Lorem ipsum' } })

This would take the :user value from the URL, combine it with data in the request and pass the info through to neode.create().

GET /:id Show

A GET request to /{id} will attempt to load the model with the primary key {id}. The primary key is defined as the primary: true in the model definition. The properties and relationships defined with eager: true will be returned as a JSON object.

PUT /:id - Update

A PUT request to /{id} will attempt to load the model with the primary key {id}, then use a combination of the request params and post body to update the node.

DELETE /:id - Destroy

A PUT request to /{id} will attempt to load the model with the primary key {id}, then delete it via neode - including any cascade deletion defined in the model.