Comparing version 0.3.0 to 0.3.1
@@ -1,16 +0,64 @@ | ||
const { MongoClient } = require('mongodb') | ||
const { MongoClient, Cursor } = require("mongodb"); | ||
function applyToArray(onFullfil, onReject) { | ||
const arrayPromise = this.toArray() | ||
if (onReject) { | ||
return arrayPromise.then(onFullfil, onReject) | ||
} | ||
return arrayPromise.then(onFullfil) | ||
} | ||
const collectionMethodProxyDescriptor = { | ||
apply(method, thisArg, argumentsList) { | ||
const value = method.apply(thisArg, argumentsList); | ||
if (value instanceof Cursor) { | ||
value.then = applyToArray | ||
} | ||
return value | ||
} | ||
} | ||
const collectionProxyDescriptor = { | ||
get(target, name) { | ||
const property = target[name] | ||
if (typeof property !== 'function') { | ||
return property | ||
} | ||
return new Proxy(property, collectionMethodProxyDescriptor); | ||
} | ||
} | ||
const Collection = native => | ||
new Proxy(native, collectionProxyDescriptor); | ||
const databaseProxyDescriptor = { | ||
get(target, name) { | ||
if (name in target) { | ||
return target[name]; | ||
} | ||
return Collection(target.collection(name)); | ||
} | ||
} | ||
/** | ||
* Simulates db.collectionName | ||
* @param {Db} native | ||
* @param {Db} native | ||
* @returns {Promise<Db>} | ||
*/ | ||
const Database = (native) => new Proxy(native, { | ||
const Database = native => | ||
new Proxy(native, databaseProxyDescriptor); | ||
const clientProxyDescriptor = { | ||
get(target, name) { | ||
if (name in target) { | ||
return target[name] | ||
return target[name]; | ||
} | ||
return target.collection(name) | ||
return Database(target.db(name)); | ||
} | ||
}) | ||
} | ||
@@ -23,9 +71,6 @@ /** | ||
*/ | ||
exports.Client = (...args) => MongoClient.connect(...args).then(client => new Proxy(client, { | ||
get(target, name) { | ||
if (name in target) { | ||
return target[name]; | ||
} | ||
return Database(target.db(name)); | ||
} | ||
})) | ||
exports.Client = (...args) => | ||
MongoClient.connect(...args).then( | ||
client => | ||
new Proxy(client, clientProxyDescriptor) | ||
); |
{ | ||
"name": "enormis", | ||
"version": "0.3.0", | ||
"version": "0.3.1", | ||
"description": "Simple modern MongoDB driver", | ||
@@ -5,0 +5,0 @@ "main": "enormis.js", |
@@ -12,3 +12,5 @@ # Enormis | ||
const client = await Client('mongodb://localhost:27017'); | ||
return Array.from(client.test.user.find({})) | ||
const users = await client.test.user.find({}); | ||
client.close(); | ||
return users; | ||
} | ||
@@ -23,5 +25,7 @@ ``` | ||
async function getUsers() { | ||
const client = await MongoClient.connect('mongodb://localhost:27017') | ||
return Array.from(client.db('test').collection('user').find({})) | ||
const client = await MongoClient.connect('mongodb://localhost:27017'); | ||
const users = await client.db('test').collection('user').find({}).toArray(); | ||
client.close(); | ||
return users; | ||
} | ||
``` |
15
test.js
@@ -1,7 +0,10 @@ | ||
const { Client } = require('./enormis') | ||
const { Client } = require("./enormis"); | ||
;(async function() { | ||
const client = await Client('mongodb://localhost:27017') | ||
const users = await client.test.user.find({}).toArray() | ||
console.log(users) | ||
})() | ||
async function getUsers() { | ||
const client = await Client("mongodb://localhost:27017"); | ||
const users = await client.test.user.find({}); | ||
client.close(); | ||
return users; | ||
} | ||
getUsers().then(console.log).catch(console.error) |
6431
6
70
30