REST-EASY-LOKI
A simple REST interface for the in-memory database, lokijs
, featuring:
- Automatic creation of collections, including CRUD actions, pagination and MongoDB-like queries.
- Simple authorization using whitelisting domain names and API keys via environment variables.
- Statically sharing the public folder
- Retrieving environment variables starting with
LOKI_
via REST
Development
npm install
npm start
Usage
To simply run the lokijs
server and expose the CRUD services.
npm run serve
To embed it in your own project, do something like the following:
import { createApi, db } from 'rest-easy-loki';
export const collectionName = 'documents';
export const startService = (done: () => void) => {
db.startDatabase('my_database_file.json', () => {
const api = createApi({ cors: true });
api.listen(options.port).on('listening', () => {
const exists = db.collections().reduce((acc, cur) => acc || cur.name === collectionName, false);
if (!exists) {
db.createCollection(collectionName, ['file']);
}
console.info(`Server running on port ${options.port}.`);
done();
});
});
};
Configuration
- Reads
.env
file for specifying the database name, port, CORS and message size limits.
Managing collections (CRUD)
Filtering collections
Sharing the public folder
- You can use the
public
folder for sharing static files or your own web application.
Serving environment variables
- The http://localhost:3000/api/env serves all environment variables that start with
LOKI_
(except the LOKI_AUTHZ_
, so you don't accidentally share secrets). Since all key-value pairs are strings, a type conversion to boolean, number and arrays (using the , as separator) is performed.
Authorization
- Simple authorization can be enabled by specifying environment variables:
LOKI_AUTHZ_CREATE
, LOKI_AUTHZ_READ
, LOKI_AUTHZ_UPDATE
, LOKI_AUTHZ_DELETE
, where the value is a comma-separated list of API keys. If no authz
is set, all CRUD actions are allowed. Otherwise, your query needs to set the x-api-key
header. - You can require it in your own project.