codehooks-mongodb
v1.0.10
Published
MongoDB implementation of a Codehooks datastore
Downloads
13
Maintainers
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.