Last active
April 13, 2022 17:40
-
-
Save taylortom/9ffc3f05a74e75ae761202da88b7ac32 to your computer and use it in GitHub Desktop.
MongoDBModule examples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* General notes: | |
* - find is the only action which returns a cursor, the rest return a Promise | |
* - With the AbstractAPIModule, we've added some restrictions. Should we map these | |
* to also restrict MongoDBModule to these: | |
* - POST: only ever create one => insertOne | |
* - GET: with _id param, returns one, otherwise array => findOne/findMany | |
* - UPDATE: must provide _id, only ever update one => insertOne | |
* - PATCH: NO SUPPORT | |
* - DELETE: must provide _id, only ever update one => deleteOne | |
* Questions: | |
* - What do we name functions? (create/retrieve/update/delete, insert/find/replace/delete, insertOne/find/updateOne/deleteOne) | |
* - How much of the mongo API should we support/expose | |
* - How consistent do we need to be with return data? (both from function, e.g. Promise, and what structure that data takes) | |
* - How should queries be specified? object, functions on a cursor, something else... | |
* - How do we handle errors? (opening up mongo API might lead to unhelpful/non-specific errors) | |
*/ | |
/** | |
* Example #1 | |
* Most basic, literally a thin wrapper for mongo which just connects to DB, and | |
* lets you do the rest. | |
*/ | |
class MongoDB { | |
connect():Promise {} | |
getCollection():Collection {} | |
} | |
// usage | |
const cursor = mdb.getCollection('courses').find({ _isDeleted: false }, { limit: 5 }); | |
const results = await cursor.toArray(); | |
/** | |
* Example #2 | |
* Adds wrappers for the basic CRUD functions, named exactly as with mongo. Returns same | |
* as mongo (e.g. find === Cursor). Requires dev to handle cursor results (e.g. cursor.toArray()) | |
*/ | |
class MongoDB { | |
connect():Promise {} | |
getCollection():Collection {} | |
insertOne():Promise {} | |
find():Cursor {} | |
updateOne():Promise {} | |
deleteOne():Promise {} | |
} | |
// usage | |
const cursor1 = mdb.find('courses', { _isDeleted: false }, { limit: 5 }); | |
const cursor2 = mdb.find('courses').filter({ _isDeleted: false }).limit(5); | |
const results = await cursor1.toArray(); | |
/** | |
* Example #3 | |
* Adds more abstraction to mongo functions. Uses a similar naming convention, but restricts | |
* one/many based on the action (i.e. insert === insertOne). Avoids dealing with cursors by | |
* using an arbitrary object param with all params/options. Also returns a promise with the | |
* relvant data, no need to deal cursor.toArray or similar. | |
*/ | |
class MongoDB { | |
connect():Promise {} | |
getCollection():Collection {} | |
insert():Promise {} | |
find():Promise {} | |
update():Promise {} | |
delete():Promise {} | |
} | |
// usage | |
const results = await mdb.find('courses', { filter: { _isDeleted: false }, limit: 5 }); | |
/** | |
* MongoDB Node.js Driver API | |
*/ | |
class MongoDBDriver { | |
insertOne(doc={}, options={}):Promise {} // resolves with insertOneWriteOpResultObject | |
find(filter={}, options={}):Cursor => Promise {} // resolves with Array<Object> | |
replaceOne(filter={}, doc={}, options={}):Promise {} // resolves with updateWriteOpResult | |
deleteOne(filter={}, options={}):Promise {} // resolves with deleteWriteOpResultObject | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment