@jyv/mongo
v1.0.4
Published
Out-of-the-box Generic Repository via MongoClient for NodeJS
Downloads
9
Readme
Out-of-the-box Generic Repo for MongoDB and NodeJS
Common Usage
Extend a Repository on a Data-set
MongoDB
import { ODataV4MongoDbGenericRepo } from '@jyv/mongo';
class Book {
_id:string;
title:string;
}
export class BooksService : extends ODataV4MongoDbGenericRepo<Book> {
super ('books-collection');
/* Inherited methods
//GET /
async query (query?: ExpressLikeODataQuery) : Promise<Array<Book>>
//GET /:id
async read(key:string, query?:ExpressLikeODataQuery) : Promise<Book>
// POST /
async create(data:Book) : Promise<Book>
// PUT /:id
async upsert(key: string,data: any) : Promise<Book>
// PATCH /
async update(query: ExpressLikeODataQuery, delta: any) : Promise<number>
// DELETE /
async delete(query?: ExpressLikeODataQuery) : Promise<number>
*/
}
const express = require(express), app = express();
app.use('/books', async (req, res , next)=>
var list = await new BooksService().query(req.query);
req.json(list);
return list;
);
Passing to Context
Throughout the managment of the operation lifespan; metadata is maintained on an optional context
object
Injecting Express Route Handling Intances
import { ODataV4MongoDbGenericRepo } from '@jyv/mongo';
class Book {
_id:string;
title:string;
}
export class BooksService : extends ODataV4MongoDbGenericRepo<Book> {
constructor() {
super ('books-collection');
}
static instance = new BooksService();
/*
//GET /
async query (query?: ExpressLikeODataQuery, context?:OperationContext<Book>) : Promise<Array<Book>>
//GET /:id
async read(key:string, query?:ExpressLikeODataQuery, context?:OperationContext<Book>) : Promise<Book>
// POST /
async create(data:Book, context?:OperationContext<Book>) : Promise<Book>
// PUT /:id
async upsert(key: string,data: any, context?:OperationContext<Book>) : Promise<Book>
// PATCH /
async update(query: ExpressLikeODataQuery, delta: any, context?:OperationContext<Book>) : Promise<number>
// DELETE /
async delete(query?: ExpressLikeODataQuery, context?:OperationContext<Book>) : Promise<number>
*/
}
const express = require(express), app = express();
app.use('/books',(req, res,next)=>BooksService.indysnvr.query(req.query, {http:{req:req}));
/*
export class OperationContext<T extends HasKey>{
instance?: ODataV4GenericRepo<T>;
data?:T;
result:any;
http: any; // req, res, next, etc.
key: any;
query: any;
db:any;
client:any;
}
*/
Developer Setup
First Setup
yarn install
- Installs sub-project dependencies
- Builds sub-projects
- If you wish to use integrated code-coverage reporting with Mocha Sidebar on OSX:
cd test && yarn fix-mocha-sidebar-coverage
will add the needed execute permissions