New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.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 - npm Package Compare versions

Comparing version 0.2.0 to 0.3.0

dist/serve.d.ts

2

bin/run.js
#! /usr/bin/env node
var server = require('../dist/index.js');
var server = require('../dist/serve.js');
server

@@ -0,3 +1,5 @@

export declare const startDatabase: (file?: string, cb?: (() => void) | undefined) => void;
export declare const createCollection: (collectionName: string, indices?: string[] | undefined) => void;
export declare const post: (collectionName: string, item: unknown) => any;
export declare const dbCollections: () => {
export declare const collections: () => {
name: string;

@@ -9,3 +11,8 @@ entries: number;

export declare const updateItem: (collectionName: string, item: any) => any;
export declare const get: (collectionName: string, query: unknown) => any;
export declare const get: (collectionName: string, query: string | number | {
[key: string]: any;
}) => any;
export declare const findOne: (collectionName: string, query: string | number | {
[key: string]: any;
}) => any;
export declare const all: (collectionName: string, query?: string | undefined) => any[] | undefined;

@@ -12,7 +12,8 @@ "use strict";

const utils_1 = require("./utils");
const collections = {};
let db;
const collectionStore = {};
const databaseInitialize = () => {
if (db.collections && db.collections.length > 0) {
db.collections.forEach(c => {
collections[c.name] = db.getCollection(c.name);
collectionStore[c.name] = db.getCollection(c.name);
});

@@ -23,19 +24,35 @@ }

};
const runProgramLogic = () => {
Object.keys(collections)
.map(name => collections[name])
.map(collection => {
console.log(`Number of entries in collection ${collection.name}: ${collection.count()}`);
const runProgramLogic = () => Object.keys(collectionStore)
.map(name => collectionStore[name])
.map(collection => {
console.log(`Number of entries in collection '${collection.name}': ${collection.count()}`);
});
exports.startDatabase = (file = 'rest_easy_loki.db', cb) => {
const autoloadCallback = cb
? () => {
databaseInitialize();
cb();
}
: () => databaseInitialize();
db = new lokijs_1.default(file, {
// Since our LokiFsStructuredAdapter is partitioned, the default 'rest_easy_loki.db'
// file will actually contain only the loki database shell and each of the collections
// will be saved into independent 'partition' files with numeric suffix.
adapter: new lokijs_1.LokiFsAdapter(),
autoload: true,
autoloadCallback,
autosave: true,
autosaveInterval: 4000,
});
};
const db = new lokijs_1.default('rest_easy_loki.db', {
// Since our LokiFsStructuredAdapter is partitioned, the default 'rest_easy_loki.db'
// file will actually contain only the loki database shell and each of the collections
// will be saved into independent 'partition' files with numeric suffix.
adapter: new lokijs_1.LokiFsAdapter(),
autoload: true,
autoloadCallback: databaseInitialize,
autosave: true,
autosaveInterval: 4000,
});
// const db2 = new loki('rest_easy_loki.db', {
// // Since our LokiFsStructuredAdapter is partitioned, the default 'rest_easy_loki.db'
// // file will actually contain only the loki database shell and each of the collections
// // will be saved into independent 'partition' files with numeric suffix.
// adapter: new LokiFsAdapter(),
// autoload: true,
// autoloadCallback: databaseInitialize,
// autosave: true,
// autosaveInterval: 4000,
// } as Partial<LokiConfigOptions>);
// Since autosave timer keeps program from exiting, we exit this program by ctrl-c.

@@ -49,18 +66,21 @@ // (optionally) For best practice, lets use the standard exit events to force a db flush to disk

});
exports.createCollection = (collectionName, indices) => {
collectionStore[collectionName] = db.addCollection(collectionName, { indices });
};
exports.post = (collectionName, item) => {
if (!collections.hasOwnProperty(collectionName)) {
collections[collectionName] = db.addCollection(collectionName);
if (!collectionStore.hasOwnProperty(collectionName)) {
exports.createCollection(collectionName);
}
return collections[collectionName].insert(item);
return collectionStore[collectionName].insert(item);
};
exports.dbCollections = () => Object.keys(collections)
.map(key => collections[key])
exports.collections = () => Object.keys(collectionStore)
.map(key => collectionStore[key])
.map(col => ({ name: col.name, entries: col.count() }));
exports.del = (collectionName, id) => {
if (!collections.hasOwnProperty(collectionName)) {
if (!collectionStore.hasOwnProperty(collectionName)) {
return false;
}
const item = collections[collectionName].get(id);
const item = collectionStore[collectionName].get(id);
if (item) {
return collections[collectionName].remove(item);
return collectionStore[collectionName].remove(item);
}

@@ -70,28 +90,52 @@ return false;

exports.update = (collectionName, id, item) => {
if (!collections.hasOwnProperty(collectionName)) {
if (!collectionStore.hasOwnProperty(collectionName)) {
return false;
}
const it = collections[collectionName].get(id);
return collections[collectionName].update(Object.assign(it, item));
const collection = collectionStore[collectionName];
const it = collection.get(id);
return collection.update(Object.assign(it, item));
};
exports.updateItem = (collectionName, item) => {
if (!collections.hasOwnProperty(collectionName)) {
if (!collectionStore.hasOwnProperty(collectionName)) {
return false;
}
return collections[collectionName].update(item);
return collectionStore[collectionName].update(item);
};
exports.get = (collectionName, query) => {
if (!collections.hasOwnProperty(collectionName)) {
if (!collectionStore.hasOwnProperty(collectionName)) {
return;
}
const collection = collections[collectionName];
const collection = collectionStore[collectionName];
if (typeof query === 'number') {
return collection.get(query);
}
if (typeof query === 'string') {
const q = query
? JSON.parse(query)
: undefined;
return q ? collection.find(q) : undefined;
}
return collection.find(query);
};
exports.findOne = (collectionName, query) => {
if (!collectionStore.hasOwnProperty(collectionName)) {
return;
}
const collection = collectionStore[collectionName];
if (typeof query === 'number') {
return collection.get(query);
}
if (typeof query === 'string') {
const q = query
? JSON.parse(query)
: undefined;
return q ? collection.findOne(q) : undefined;
}
return collection.findOne(query);
};
exports.all = (collectionName, query) => {
if (!collections.hasOwnProperty(collectionName)) {
if (!collectionStore.hasOwnProperty(collectionName)) {
return;
}
const collection = collections[collectionName];
const collection = collectionStore[collectionName];
const q = query

@@ -98,0 +142,0 @@ ? JSON.parse(query)

@@ -1,1 +0,21 @@

export {};
import Koa from 'koa';
export declare const db: {
all: (collectionName: string, query?: string | undefined) => any[] | undefined;
createCollection: (collectionName: string, indices?: string[] | undefined) => void;
collections: () => {
name: string;
entries: number;
}[];
del: (collectionName: string, id: number) => any;
get: (collectionName: string, query: string | number | {
[key: string]: any;
}) => any;
findOne: (collectionName: string, query: string | number | {
[key: string]: any;
}) => any;
post: (collectionName: string, item: unknown) => any;
startDatabase: (file?: string, cb?: (() => void) | undefined) => void;
update: (collectionName: string, id: number, item: any) => any;
updateItem: (collectionName: string, item: any) => any;
};
export declare const api: Koa;

@@ -9,12 +9,22 @@ "use strict";

const koa_static_1 = __importDefault(require("koa-static"));
const config_1 = require("./config");
const database_1 = require("./database");
const logging_1 = require("./logging");
const routes_1 = require("./routes");
const app = new koa_1.default();
app.use(koa_body_1.default());
app.use(logging_1.logger);
app.use(koa_static_1.default('./public'));
app.use(routes_1.routes);
app.listen(config_1.config.port);
console.log(`Server running on port ${config_1.config.port}.`);
exports.db = {
all: database_1.all,
createCollection: database_1.createCollection,
collections: database_1.collections,
del: database_1.del,
get: database_1.get,
findOne: database_1.findOne,
post: database_1.post,
startDatabase: database_1.startDatabase,
update: database_1.update,
updateItem: database_1.updateItem,
};
exports.api = new koa_1.default();
exports.api.use(koa_body_1.default());
exports.api.use(logging_1.logger);
exports.api.use(koa_static_1.default('./public'));
exports.api.use(routes_1.routes);
//# sourceMappingURL=index.js.map

@@ -12,4 +12,17 @@ "use strict";

ctx.status = 201;
ctx.body = database_1.dbCollections();
ctx.body = database_1.collections();
});
/**
* Request the whole collection but only returns a subset of all properties
* - Specify `props` containing a comma separted array of top-level properties.
* - Optionally, specify from and to as query params for pagination, e.g. ?from=0&to=5
*/
router.get('/:collection/view', async (ctx) => {
const { collection } = ctx.params;
const map = utils_1.propertyMap(ctx.query);
const filter = utils_1.paginationFilter(ctx.query);
const results = database_1.all(collection, ctx.query.q);
ctx.body = map && results ? (filter ? results.filter(filter).map(map) : results.map(map)) : results;
});
/** Get by ID */
router.get('/:collection/:id', async (ctx) => {

@@ -25,5 +38,5 @@ const { collection, id } = ctx.params;

const { collection } = ctx.params;
const filter = utils_1.paginationFilter(ctx.query);
const pages = utils_1.paginationFilter(ctx.query);
const results = database_1.all(collection, ctx.query.q);
ctx.body = filter && results ? results.filter(filter) : results;
ctx.body = pages && results ? results.filter(pages) : results;
});

@@ -30,0 +43,0 @@ router.post('/:collection', async (ctx) => {

@@ -7,3 +7,11 @@ /// <reference types="lokijs" />

}) => ((_: any, index: number) => boolean) | undefined;
/** Creates a property map, i.e. it will only return a subset of all data */
export declare const propertyMap: ({ props }: {
props?: string | undefined;
}) => ((cur: {
[key: string]: any;
}) => {
[key: string]: any;
}) | undefined;
/** Sort descending by updated date (when updated is not available, use created date) */
export declare const sortByDateDesc: (obj1: LokiObj, obj2: LokiObj) => 1 | -1 | 0;

@@ -12,2 +12,14 @@ "use strict";

};
/** Creates a property map, i.e. it will only return a subset of all data */
exports.propertyMap = ({ props }) => {
if (!props) {
return undefined;
}
const p = props.split(',').map(prop => prop.trim());
return (cur) => {
const obj = {};
p.filter(prop => cur.hasOwnProperty(prop)).forEach(prop => (obj[prop] = cur[prop]));
return obj;
};
};
/** Sort descending by updated date (when updated is not available, use created date) */

@@ -14,0 +26,0 @@ exports.sortByDateDesc = (obj1, obj2) => {

{
"name": "rest-easy-loki",
"version": "0.2.0",
"version": "0.3.0",
"description": "A REST interface for lokijs supporting CRUD operations and automatic creation of new collections.",

@@ -12,3 +12,3 @@ "main": "./dist/index.js",

"clean": "rimraf ./dist",
"serve": "node ./dist/index.js",
"serve": "node ./dist/serve.js",
"build": "tsc",

@@ -15,0 +15,0 @@ "start": "tsc-watch --onSuccess \"node ./dist/index.js\" --onFailure \"echo Beep! Compilation Failed\" --compiler typescript/bin/tsc",

@@ -15,3 +15,5 @@ # REST-EASY-LOKI

- Automatic creation of new collections: when you post a message to a non-existing collection, it is automatically created.
- You can filter the properties that get returned (simple GraphQL-like filter) using a collection's 'view', e.g. [http://localhost:3000/COLLECTION_NAME/view?props=title,$loki,file](http://localhost:3000/COLLECTION_NAME/view?props=title,$loki,file).
- You can use the `public` folder for sharing static files.
- You can require it in your own project.

@@ -27,3 +29,3 @@ ## Installation

To simply run the `lokijs` server
To simply run the `lokijs` server and expose the CRUD services.

@@ -33,1 +35,22 @@ ```bash

```
To embed it in your own project, do something like the following:
```ts
import { api, db } from 'rest-easy-loki';
export const collectionName = 'documents';
export const startService = (done: () => void) => {
db.startDatabase('my_database_file.json', () => {
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();
});
});
};
```

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

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