dfdb-express
v0.0.20
Published
This is a simple extension to easily expose a DocsOnFileDB through Express as a RESTful API.
Downloads
4
Readme
DocsOnFileDB (ExpressJS connector)
This is a simple extension to easily expose a DocsOnFileDB through Express as a RESTful API.
Contents
Installation
To install this module you may run:
npm install --save dfdb-express
How to use
This code example shows a simple express server and how to set express to expose certain DocsOnFileDB:
'use strict';
//
// What port should be use?
const port = process.env.PORT || 3000;
//
// Basic required libraries.
const bodyParser = require('body-parser');
const express = require('express');
const http = require('http');
const path = require('path');
//
// Creating an express library.
const app = express();
//
// Basic middlewares.
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
//
// Importing 'dfdb-express' middleware.
const expressConnector = require('dfdb-express').middleware;
//
// Telling express to handle database API accesses on the URI '/rest/mydb'.
// Of course 'dbname' and 'dbpath' are the basic parameters required by
// 'DocsOnFileDB' to access certain database, in this case, the one that's going
// to be exposed.
// Also, in this configuraion the collection 'my_private_collection' won't be
// exposed.
app.use(expressConnector({
dbname: 'mydb',
dbpath: path.join(__dirname, 'db-dir'),
restPath: '/rest/mydb',
hide:[
'my_private_collection'
]
}));
//
// Capturing unkwnon route requests.
app.all('*', (req, res) => {
res.status(404).json({
message: `Path '${req.url}' was not found.`
});
});
//
// Starting server.
http.createServer(app).listen(port, () => {
console.log(`listening on port ${port}`);
});
Endpoints
These are endpoints provided by this connector:
[GET] /rest/mydb/$info
Provides information about current database connection and it's assets.[POST] /rest/mydb/$initializer
Replaces a database initialization spec.[POST] /rest/mydb/$reinitialize
Triggers a check-up of a database initialization spec.[POST] /rest/mydb/:collection/$createIndex?field=:name
Creates a field index for a collection and indexes it.[GET] /rest/mydb/:collection/$create
Triggers the creation of certain collection.[DELETE] /rest/mydb/:collection/$dropIndex?field=:name
Drops a field index from a collection.[DELETE] /rest/mydb/:collection/$drop
[GET] /rest/mydb/:collection/$indexes
[GET] /rest/mydb/:collection/$schema
Retrieves a collection's schema definition.[PUT] /rest/mydb/:collection/$schema
Updates a collection's schema specification.[POST] /rest/mydb/:collection/$truncate
Remove all documents from a collection. It doesn't reset indexes.[DELETE] /rest/mydb/:collection/:id
[GET] /rest/mydb/:collection/:id
[PUT] /rest/mydb/:collection/:id
[GET] /rest/mydb/:collection
Retrieves all the information inside certain collection.[POST] /rest/mydb/:collection
Basic server
If you don't want to go through all the step of creating a server but you still
want to see your database in a web interfase, you may run something like this
(assuming your database is at /path/to/mydb.dfdb
):
dfdb-server --database /path/to/mydb
This will load this two urls:
http://localhost:3005/rest
: RESTful access to your database.http://localhost:3005/ui
: Web-UI access to your database.
Licence
MIT © 2018 Alejandro Dario Simi