rest-easy-loki
Advanced tools
Comparing version 0.5.1 to 0.6.0
@@ -42,3 +42,5 @@ "use strict"; | ||
if (apiKeys.whitelist.length > 0) { | ||
// console.table(apiKeys); | ||
const hostname = ctx.hostname ? ctx.hostname.toUpperCase() : undefined; | ||
// console.table(hostname); | ||
if (hostname && apiKeys.whitelist.indexOf(hostname) >= 0) { | ||
@@ -77,10 +79,7 @@ return true; | ||
exports.pep = async (ctx, next) => { | ||
if (!apiKeys) { | ||
if (pdp(ctx)) { | ||
// Use await next. See here: https://github.com/ZijianHe/koa-router/issues/358 | ||
await next(); | ||
} | ||
else if (pdp(ctx)) { | ||
await next(); | ||
} | ||
}; | ||
//# sourceMappingURL=authorization.js.map |
@@ -7,2 +7,3 @@ "use strict"; | ||
const koa_router_1 = __importDefault(require("koa-router")); | ||
const rfc6902_1 = require("rfc6902"); | ||
const database_1 = require("./database"); | ||
@@ -29,3 +30,8 @@ const environment_1 = require("./environment"); | ||
const results = database_1.all(collection, ctx.query.q); | ||
ctx.body = map && results ? (filter ? results.filter(filter).map(map) : results.map(map)) : results; | ||
ctx.body = | ||
map && results | ||
? filter | ||
? results.filter(filter).map(map) | ||
: results.map(map) | ||
: results; | ||
}); | ||
@@ -57,2 +63,26 @@ /** Get by ID */ | ||
}); | ||
exports.router.patch('/api/:collection/:id', async (ctx) => { | ||
const { collection, id } = ctx.params; | ||
if (id) { | ||
const item = database_1.get(collection, +id); | ||
const mutation = ctx.request.body; | ||
if (item && mutation && mutation.patch) { | ||
const { saveChanges, patch } = mutation; | ||
const errors = rfc6902_1.applyPatch(item, patch); | ||
const hasErrors = errors.some(e => e !== null); | ||
if (hasErrors) { | ||
errors.forEach(e => e && console.error(e)); | ||
ctx.status = 409; | ||
ctx.body = errors; | ||
} | ||
else { | ||
if (saveChanges) { | ||
delete mutation.saveChanges; | ||
database_1.post(saveChanges, mutation); | ||
} | ||
ctx.body = database_1.update(collection, +id, item); | ||
} | ||
} | ||
} | ||
}); | ||
exports.router.put('/api/:collection', async (ctx) => { | ||
@@ -59,0 +89,0 @@ const { collection } = ctx.params; |
{ | ||
"name": "rest-easy-loki", | ||
"version": "0.5.1", | ||
"version": "0.6.0", | ||
"description": "A REST interface for lokijs supporting CRUD operations and automatic creation of new collections.", | ||
@@ -14,2 +14,3 @@ "main": "./dist/index.js", | ||
"build": "tsc", | ||
"watch": "tsc -w", | ||
"start": "tsc-watch --onSuccess \"node ./dist/cli.js\" --onFailure \"echo Beep! Compilation Failed\" --compiler typescript/bin/tsc", | ||
@@ -38,5 +39,5 @@ "dry-run": "npm publish --dry-run", | ||
"command-line-args": "^5.1.1", | ||
"command-line-usage": "^6.0.2", | ||
"command-line-usage": "^6.1.0", | ||
"dotenv": "^8.2.0", | ||
"koa": "^2.10.0", | ||
"koa": "^2.11.0", | ||
"koa-body": "^4.1.1", | ||
@@ -46,17 +47,17 @@ "koa-router": "^7.4.0", | ||
"@koa/cors": "^3.0.0", | ||
"lokijs": "^1.5.7" | ||
"lokijs": "^1.5.8", | ||
"rfc6902": "^3.0.4" | ||
}, | ||
"devDependencies": { | ||
"@types/command-line-args": "^5.0.0", | ||
"@types/dotenv": "^6.1.1", | ||
"@types/koa": "^2.0.51", | ||
"@types/koa-compose": "^3.2.4", | ||
"@types/koa": "^2.0.52", | ||
"@types/koa-compose": "^3.2.5", | ||
"@types/koa-router": "^7.0.42", | ||
"@types/koa-static": "^4.0.1", | ||
"@types/lokijs": "^1.5.2", | ||
"@types/node": "^12.11.1", | ||
"@types/node": "^12.12.11", | ||
"rimraf": "^3.0.0", | ||
"tsc-watch": "^4.0.0", | ||
"typescript": "^3.6.4" | ||
"typescript": "^3.7.2" | ||
} | ||
} |
@@ -28,2 +28,3 @@ # REST-EASY-LOKI | ||
```ts | ||
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'; | ||
@@ -33,15 +34,15 @@ | ||
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(); | ||
}); | ||
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(); | ||
``` | ||
@@ -62,3 +63,16 @@ | ||
- Update the item by ID. PUT the item as an `application/json` body to [https://localhost:3000/api/COLLECTION_NAME/ID](https://localhost:3000/api/COLLECTION_NAME/ID). Alternatively, change the original item (from the GET, so including `$loki` ID) and PUT it back to [https://localhost:3000/api/COLLECTION_NAME](https://localhost:3000/api/COLLECTION_NAME) | ||
- Patch the item by ID, where the patch is based on [RFC6902](https://www.npmjs.com/package/rfc6902). PATCH item is an `application/json` body to [https://localhost:3000/api/COLLECTION_NAME/ID](https://localhost:3000/api/COLLECTION_NAME/ID). The send patch object is defined as specified below. In case `saveChanges` is specified, the patch is also saved to the appropriate collection (after removing the `saveChanges` property). | ||
```ts | ||
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 | ||
@@ -65,0 +79,0 @@ |
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
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
58054
10
52
786
93
10
+ Addedrfc6902@^3.0.4
+ Addedrfc6902@3.1.1(transitive)
Updatedcommand-line-usage@^6.1.0
Updatedkoa@^2.11.0
Updatedlokijs@^1.5.8