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

codehooks-mongodb

v1.0.10

Published

MongoDB implementation of a Codehooks datastore

Downloads

13

Readme

codehooks-mongodb

This repository is an open source implementation of a codehooks.io backend with a MongoDB database for persistence. This package enables a standard serverless codehooks.io backend to run as a standalone express.js instance.

codehooks-mongodb is a great option to avoid service lock-in or if you just want to run and manage it yourself.

Usage

Consider the standard Codehooks serverless CRUD app in the index.js file below.

/* index.js
*  A minimal codehooks.io backend app
*/
import { app } from 'codehooks-js';
import crudlify from 'codehooks-crudlify';

app.all('/myroute', (req, res) => {
    res.end('Hit me again with any method!')
})
// Add CRUD routes for any collection
crudlify(app);

export default app.init();

The codehooks backend above can be deployed to the cloud serverless runtime with the coho deploy command.

However, the same app can also run as a standalone node.js express app. The trick is to add a separate JavaScript startup file, e.g. standalone.js. An example startup file is shown below, adapt it to your needs, express settings, MongoDB connection string, etc.

/* 
* standalone.js
* Example express app for running codehooks.io standalone using MongoDB
*/
import coho from './index.js';
import mongoStore from 'codehooks-mongodb';
import express from 'express';
import bodyParser from 'body-parser';
const app = express();
import Debug from "debug";
const debug = Debug("codehooks-standalone");

app.use(bodyParser.json({ limit: '10mb' }));

const options = {
    "datastore": new mongoStore(process.env.MONGODB_URI || 'mongodb://localhost:27017')
}

// important, make codehooks use express and MongoDB
coho.useExpress(app, options);

app.listen(3000, () => {
    console.log("Running standalone on port 3000")
})

Create a package.json file for your app.

npm install codehooks-mongodb codehooks-js codehooks-crudlify express body-parser mongodb debug --save

This should create something like the following package.json file. To enable JavaScript ES6 you need to set "type":"module" manually. Also add a scripts.start command to start the express server.

{
  "type": "module",
  "scripts": {
    "start": "node standalone.js"
  },
  "dependencies": {
    "body-parser": "^1.20.1",
    "codehooks-crudlify": "^1.0.7",
    "codehooks-js": "^1.0.4",
    "codehooks-mongodb": "^1.0.3",
    "debug": "^4.3.4",
    "express": "^4.18.2",
    "mongodb": "^5.0.1"
  } 
}

Alternatively you can copy the package.json above and install the dependencies with npm install.

If you don't have a MongoDB instance already, you you can start a local MongoDB as a docker container.

docker run -d -p 27017:27017 --name mongodb mongo:latest

Finally, start your serverless node.js app locally with the npm start command. It should output the following message.

> start
> node standalone.js

Running standalone on port 3000

Your app is now listening on http://localhost:3000/dev/myroute.

The Crudlify package generates GET, PUT, POST, PATCH and DELETE on any collection route, e.g. /dev/mycollection.

Tip: Read the docs for the open source Crudlify package which creates a full CRUD REST API for your serverless node.js app.

Documentation