Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

rest-easy-loki

Package Overview
Dependencies
Maintainers
1
Versions
35
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

rest-easy-loki

A REST interface for lokijs supporting CRUD operations and automatic creation of new collections.

  • 0.7.5
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
2
decreased by-60%
Maintainers
1
Weekly downloads
 
Created
Source

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 # Or pnpm i
npm start # Will transpile the TypeScript project to JavaScript and run node on every change.

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 * as Koa from 'koa'; // You only need to include @types/koa in your devDependencies, not Koa itself.
import { createApi, db } from 'rest-easy-loki';

export const collectionName = 'documents';

const port = process.env.LOKI_PORT || '3000';
const dbName = process.env.LOKI_DB;
const cors = (process.env.LOKI_CORS || 'true') === 'true';
const sizeLimit = process.env.LOKI_SIZE_LIMIT || '25mb';

export const startService = () => {
  db.startDatabase(dbName, () => {
    const api = createApi({ cors, sizeLimit }) as Koa;
    api.listen(port);
    console.log(`Server running on port ${port}.`);
  });
};
startService();

Configuration

  • Reads .env file for specifying the database name, port, CORS and message size limits.

Managing collections (CRUD)

export interface IMutation extends ILokiObj {
  /**
   * Save changes to collection: if set, save this object,
   * except the `saveChanges` property, to the `saveChanges` collection
   */
  saveChanges?: string;
  /** RFC6902 JSON patch */
  patch?: Operation[];
}

Filtering collections

Sharing the public folder

You can use the public folder for sharing static files or your own web application.

Uploading files

You can use the upload folder for uploading files to a (automatically created) CONTEXT folder, if enabled on start-up using the -u instruction. Test it via curl -F "file=@filename.jpg" http://localhost:3030/upload/:CONTEXT. Files will be served from http://localhost:3030/:CONTEXT/ORG_FILENAME. When uploading the same filename in the same context, the previous version will be overwritten. No index file is created, so the contents of the locally created folders are not visible externally. Also note that the CONTEXT supports sub-folders too.

Socket.io support

If enabled using the io flag (or -i) so clients can subscribe to receive updates when a value has changed. Clients can either subscribe to a collection socket.subscribe('COLLECTION_NAME'), or to a collection item socket.subscribe('COLLECTION_NAME/$LOKI'). The latter is, for example, useful when you have multiple editors. Subscribers receive the updated item.

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.

Keywords

FAQs

Package last updated on 01 Feb 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc