Skip to content

Instantly share code, notes, and snippets.

@dfa1234
Last active June 27, 2019 18:02
Show Gist options
  • Save dfa1234/1a16871068ff8c55813b34ce0005225b to your computer and use it in GitHub Desktop.
Save dfa1234/1a16871068ff8c55813b34ce0005225b to your computer and use it in GitHub Desktop.
generic api for accessing a mongodb collection using rxjs5
import {Request, Response} from "express";
import {
Collection,
DeleteWriteOpResultObject,
InsertOneWriteOpResult,
MongoClient,
ObjectID,
UpdateWriteOpResult
} from "mongodb";
import {dbConf} from "../config";
import {Observable} from "rxjs";
const getCollection$ = (collectionName: string): Observable<Collection> => Observable.fromPromise(MongoClient.connect(dbConf.URI, {useNewUrlParser: true}))
.map((clientMongo: MongoClient) => clientMongo.db(dbConf.database).collection(collectionName));
const getAllObjects$ = (collection: Collection): Observable<any[]> => Observable.fromPromise(collection.find().toArray());
const addObjectToCollection$ = (object: any) =>
(collection: Collection): Observable<InsertOneWriteOpResult> =>
Observable.fromPromise(collection.insertOne(object)).map(res => res.ops[0]);
const updateObject$ = (objectModified: any) =>
(collection: Collection): Observable<UpdateWriteOpResult> => {
let changeToCommit = Object.assign({}, objectModified);
delete changeToCommit._id;
return Observable.fromPromise(collection.updateOne({_id: new ObjectID(objectModified._id)}, {$set: changeToCommit}, {upsert: false}))
.map(res => objectModified);
};
const delObject$ = (_id: string) =>
(collection: Collection): Observable<DeleteWriteOpResultObject> =>
Observable.fromPromise(collection.deleteOne({_id: new ObjectID(_id)}));
const subscribeService = operation => (collectionName: string) => (res: Response) =>
getCollection$(collectionName)
.concatMap(collection => operation(collection))
.subscribe(
result => res.json(result),
error => res.json({error})
);
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// SERVICES
//
export const getObjects = (collectionName: string) => (req: Request, res: Response) => {
subscribeService(getAllObjects$)(collectionName)(res);
};
export const postObject = (collectionName: string) => (req: Request, res: Response) => {
subscribeService(addObjectToCollection$(req.body))(collectionName)(res);
};
export const putObject = (collectionName: string) => (req: Request, res: Response) => {
subscribeService(updateObject$(req.body))(collectionName)(res);
};
export const delObject = (collectionName: string) => (req: Request, res: Response) => {
subscribeService(delObject$(req.params.id))(collectionName)(res);
};
//How using the above file.
import {delObject, getObjects, postObject, putObject} from "./services/api-generic";
//app is your express server
app.post('/element', authSessionOnly, postObject('myElementsCollection'));
app.put('/element', authSessionOnly, putObject('myElementsCollection'));
app.get('/elements', authSessionOnly, getObjects('myElementsCollection'));
app.delete('/element/:id', authSessionOnly, delObject('myElementsCollection'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment