damless-mongo
Advanced tools
Comparing version 0.4.2 to 1.0.0
@@ -14,3 +14,3 @@ { | ||
"--grep", | ||
"parse multiple or with regex", | ||
"delete", | ||
"${workspaceRoot}/tests/**.spec.js" | ||
@@ -17,0 +17,0 @@ ], |
/*! | ||
* damless-mongo | ||
* Copyright(c) 2018 Benoît Claveau <benoit.claveau@gmail.com> | ||
* Copyright(c) 2021 Benoît Claveau <benoit.claveau@gmail.com> | ||
* MIT Licensed | ||
@@ -8,5 +8,3 @@ */ | ||
const { | ||
Aggregate, | ||
Crud, | ||
Find, | ||
Http, | ||
@@ -22,5 +20,3 @@ MongoQueryString, | ||
module.exports = DamlessMongo; | ||
module.exports.Aggregate = Aggregate; | ||
module.exports.Crud = Crud; | ||
module.exports.Find = Find; | ||
module.exports.Http = Http; | ||
@@ -27,0 +23,0 @@ module.exports.MongoQueryString = MongoQueryString; |
/*! | ||
* damless-mongo | ||
* Copyright(c) 2018 Benoît Claveau <benoit.claveau@gmail.com> | ||
* Copyright(c) 2021 Benoît Claveau <benoit.claveau@gmail.com> | ||
* MIT Licensed | ||
@@ -5,0 +5,0 @@ */ |
/*! | ||
* damless-mongo | ||
* Copyright(c) 2018 Benoît Claveau <benoit.claveau@gmail.com> / CABASI | ||
* Copyright(c) 2021 Benoît Claveau <benoit.claveau@gmail.com> / CABASI | ||
* MIT Licensed | ||
@@ -8,5 +8,3 @@ */ | ||
const { Error, UndefinedError } = require("oups"); | ||
const { streamify, transform } = require("damless"); | ||
const Find = require("./find"); | ||
const Aggregate = require("./aggregate"); | ||
const { transform } = require("damless"); | ||
@@ -31,16 +29,3 @@ class CrudService { | ||
/* stream methods */ | ||
// deprecated Use insert, update instead | ||
saveOne(filter, document, options) { | ||
return streamify(async () => { | ||
const { _id, ...doc } = document; | ||
const previous = this.mongoFindOne(filter, options); | ||
const current = previous ? { ...previous, ...doc } : doc; | ||
const res = await this.mongoSave(current, options); | ||
if (!res) throw new Error("Fail to save document.", { document }); | ||
return current; | ||
}); | ||
} | ||
// deprecated Use insert, update instead | ||
save(filter = ({_id}) => ({_id}), options, output) { | ||
@@ -60,10 +45,2 @@ return transform(async (chunk, enc) => { | ||
insertOne(document, options) { | ||
return streamify(async () => { | ||
const res = await this.mongoInsertOne(document, options) | ||
if (!res) throw new Error("Fail to insert document.", { document: d }); | ||
return wrap(res); | ||
}); | ||
} | ||
insert(options, output) { | ||
@@ -79,10 +56,2 @@ return transform(async (chunk, enc) => { | ||
updateOne(filter, update, options) { | ||
return streamify(async () => { | ||
const res = await this.mongoUpdateOne(filter, update, options); | ||
if (!res) throw new Error("Fail to update document.", { filter: f, update: u }); | ||
return wrap(res); | ||
}); | ||
} | ||
update(filter = ({_id}) => ({_id}), update, options = {}, output) { | ||
@@ -101,10 +70,2 @@ return transform(async (chunk, enc) => { | ||
replaceOne(filter, replacement, options) { | ||
return streamify(async () => { | ||
const res = await this.mongoReplaceOne(filter, replacement, options); | ||
if (!res) throw new Error("Fail to replace document.", { filter: f, replacement: r }); | ||
return wrap(res); | ||
}); | ||
} | ||
replace(filter = ({_id}) => ({_id}), replacement, options, output) { | ||
@@ -123,10 +84,2 @@ return transform(async (chunk, enc) => { | ||
deleteOne(filter, options) { | ||
return streamify(async () => { | ||
const res = await this.mongoDeleteOne(filter, options); | ||
if (!res) throw new Error("Fail to delete document.", { document: chunk }); | ||
return wrap(res); | ||
}); | ||
} | ||
delete(filter = ({_id}) => ({_id}), options, output) { | ||
@@ -143,14 +96,3 @@ return transform(async (chunk, enc) => { | ||
deleteMany(filter, options) { | ||
return streamify(async () => { | ||
const res = await this.mongoDeleteMany(filter, options); | ||
if (!res) throw new Error("Fail to delete document.", { document: chunk }); | ||
return wrap(res); | ||
}); | ||
} | ||
/** | ||
* Delete many + stream | ||
*/ | ||
deleteManyeam(filter = ({_id}) => ({_id}), options, output) { | ||
deleteMany(filter = ({_id}) => ({_id}), options, output) { | ||
return transform(async (chunk, enc) => { | ||
@@ -165,34 +107,3 @@ const f = typeof filter == "function" ? filter(chunk) : filter; | ||
} | ||
findOne(filter, options) { | ||
return streamify(async () => { | ||
const res = await this.mongoFindOne(filter, options); | ||
if (!res) throw new Error("Fail to find document.", { filter }); | ||
return wrap(res); | ||
}); | ||
} | ||
find(filter, options) { | ||
return new Find(this, filter, options); | ||
} | ||
aggregate(pipeline, options) { | ||
if (!pipeline) throw new Error("Pipeline is not defined."); | ||
return new Aggregate(this, pipeline.filter(e => !!e), options); | ||
} | ||
countDocuments(filter, options) { | ||
return streamify(async () => { | ||
const count = await this.mongoCountDocuments(filter, options) | ||
return { count }; | ||
}); | ||
} | ||
estimatedDocumentCount(options) { | ||
return streamify(async () => { | ||
const count = await this.mongoEstimatedDocumentCount(options) | ||
return { count }; | ||
}); | ||
} | ||
/* mongo api */ | ||
@@ -255,2 +166,7 @@ | ||
async dropDatabase(options) { | ||
const collection = await this.collection(); | ||
return await collection.dropDatabase(options); | ||
} | ||
async mongoDrop(options) { | ||
@@ -286,5 +202,11 @@ const collection = await this.collection(); | ||
async mongoFind(filter, options) { | ||
async mongoFind(filter, options, operations = {}) { | ||
const collection = await this.collection(); | ||
return await collection.find(filter, options); | ||
let cursor = await collection.find(filter, options); | ||
const { skip, limit, sort, project } = operations; | ||
if (skip) cursor = cursor.skip(skip); | ||
if (limit) cursor = cursor.limit(limit); | ||
if (sort) cursor = cursor.sort(sort); | ||
if (project) cursor = cursor.project(project); | ||
return cursor; | ||
} | ||
@@ -291,0 +213,0 @@ |
/*! | ||
* remit | ||
* Copyright(c) 2018 Benoît Claveau <benoit.claveau@gmail.com> / CABASI | ||
* Copyright(c) 2021 Benoît Claveau <benoit.claveau@gmail.com> / CABASI | ||
* MIT Licensed | ||
@@ -5,0 +5,0 @@ */ |
/*! | ||
* damless-mongo | ||
* Copyright(c) 2018 Benoît Claveau <benoit.claveau@gmail.com> / CABASI | ||
* Copyright(c) 2021 Benoît Claveau <benoit.claveau@gmail.com> / CABASI | ||
* MIT Licensed | ||
@@ -12,4 +12,4 @@ */ | ||
const { promisify } = require("util"); | ||
const { pipeline } = require("stream"); | ||
const pipelineAsync = promisify(pipeline); | ||
const stream = require("stream"); | ||
const pipeline = promisify(stream.pipeline); | ||
@@ -52,6 +52,6 @@ /** | ||
return await pipelineAsync( | ||
this.findOne(filter, options), | ||
stream.mode("object") | ||
); | ||
const res = await this.mongoFindOne(filter, options); | ||
stream | ||
.mode("object") | ||
.end(wrap(res)); | ||
} | ||
@@ -65,11 +65,7 @@ | ||
let { filter, options, skip, limit, sort, project } = context.mongo || this.qs.parse(context.querystring) || {}; | ||
const { filter, options, ...operations } = context.mongo || this.qs.parse(context.querystring) || {}; | ||
let cursor = this.find(filter, options); | ||
if (skip) cursor = cursor.skip(skip); | ||
if (limit) cursor = cursor.limit(limit); | ||
if (sort) cursor = cursor.sort(sort); | ||
if (project) cursor = cursor.project(project); | ||
const cursor = await this.mongoFind(filter, options, operations); | ||
return await pipelineAsync( | ||
return await pipeline( | ||
cursor, | ||
@@ -85,6 +81,8 @@ stream | ||
let { pipeline, options } = context.mongo || {}; | ||
const { pipeline, options } = context.mongo || {}; | ||
return await pipelineAsync( | ||
this.aggregate(pipeline, options), | ||
const cursor = await this.mongoAggregate(pipeline, options); | ||
return await pipeline( | ||
cursor, | ||
stream | ||
@@ -99,3 +97,3 @@ ); | ||
return await pipelineAsync( | ||
return await pipeline( | ||
stream.mode("object"), | ||
@@ -112,3 +110,3 @@ this.insert(), | ||
return await pipelineAsync( | ||
return await pipeline( | ||
stream, | ||
@@ -129,3 +127,3 @@ this.insert(), | ||
return await pipelineAsync( | ||
return await pipeline( | ||
stream.mode("object"), | ||
@@ -145,3 +143,3 @@ this.update(filter, options), | ||
return await pipelineAsync( | ||
return await pipeline( | ||
stream, | ||
@@ -161,3 +159,3 @@ this.update(filter, options), | ||
return await pipelineAsync( | ||
return await pipeline( | ||
stream.mode("object"), | ||
@@ -176,3 +174,3 @@ this.replace(filter, options), | ||
return await pipelineAsync( | ||
return await pipeline( | ||
stream, | ||
@@ -192,6 +190,6 @@ this.replace(filter, options), | ||
return await pipelineAsync( | ||
this.deleteOne(filter, options), | ||
stream.mode("object") | ||
); | ||
const res = await this.mongoDeleteOne(filter, options); | ||
stream | ||
.mode("object") | ||
.end(wrap(res)); | ||
} | ||
@@ -206,6 +204,6 @@ | ||
return await pipelineAsync( | ||
this.deleteMany(filter, options), | ||
stream | ||
); | ||
const res = await this.mongoDeleteMany(filter, options); | ||
stream | ||
.mode("object") | ||
.end(wrap(res)); | ||
} | ||
@@ -221,3 +219,3 @@ | ||
return await pipelineAsync( | ||
return await pipeline( | ||
stream.mode("object"), | ||
@@ -236,5 +234,5 @@ this.save(filter, options), | ||
return await pipelineAsync( | ||
return await pipeline( | ||
stream, | ||
this.saveOne(filter, options), | ||
this.save(filter, options), | ||
stream | ||
@@ -251,6 +249,6 @@ ); | ||
return await pipelineAsync( | ||
this.countDocuments(filter, options), | ||
stream.mode("object") | ||
); | ||
const count = await this.mongoCountDocuments(filter, options); | ||
stream | ||
.mode("object") | ||
.end({ count }); | ||
} | ||
@@ -257,0 +255,0 @@ } |
@@ -1,6 +0,4 @@ | ||
module.exports.Aggregate = require("./aggregate"); | ||
module.exports.Crud = require("./crud"); | ||
module.exports.Find = require("./find"); | ||
module.exports.Http = require("./http"); | ||
module.exports.MongoQueryString = require("./mongo-querystring"); | ||
module.exports.Mutex = require("./mutex"); |
/*! | ||
* damless-mongo | ||
* Copyright(c) 2018 Benoît Claveau <benoit.claveau@gmail.com> / CABASI | ||
* Copyright(c) 2021 Benoît Claveau <benoit.claveau@gmail.com> / CABASI | ||
* Inspire by https://github.com/nswbmw/qs-mongodb | ||
@@ -5,0 +5,0 @@ * MIT Licensed |
{ | ||
"name": "damless-mongo", | ||
"version": "0.4.2", | ||
"version": "1.0.0", | ||
"description": "Mongo client for damless", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -112,8 +112,8 @@ # damless-mongo | ||
const { promisify } = require("util"); | ||
const { pipeline } = require("stream"); | ||
const pipelineAsync = promisify(pipeline); | ||
const stream = require("stream"); | ||
const pipeline = promisify(stream.pipeline); | ||
class Api extends Http { | ||
//giveme is the dependency injection service used by damless | ||
// giveme is the dependency injection service used by damless | ||
constructor(giveme) { | ||
@@ -123,6 +123,8 @@ super(giveme, "<collectionName>"); | ||
//override the default httpFind an use Crud primitives | ||
// override the default httpFind an use Crud primitives | ||
async httpFind(context, stream, headers) { | ||
await pipelineAsync( | ||
this.findWords(context.query.q), | ||
// get a mongo cursor | ||
const cursor = this.findWords(context.query.q); | ||
await pipeline( | ||
cursor, | ||
stream | ||
@@ -129,0 +131,0 @@ ); |
/*! | ||
* damless-mongo | ||
* Copyright(c) 2018 Benoît Claveau <benoit.claveau@gmail.com> | ||
* Copyright(c) 2021 Benoît Claveau <benoit.claveau@gmail.com> | ||
* MIT Licensed | ||
@@ -5,0 +5,0 @@ */ |
/*! | ||
* damless-mongo | ||
* Copyright(c) 2018 Benoît Claveau <benoit.claveau@gmail.com> | ||
* Copyright(c) 2021 Benoît Claveau <benoit.claveau@gmail.com> | ||
* MIT Licensed | ||
@@ -5,0 +5,0 @@ */ |
/*! | ||
* damless-mongo | ||
* Copyright(c) 2018 Benoît Claveau <benoit.claveau@gmail.com> | ||
* Copyright(c) 2021 Benoît Claveau <benoit.claveau@gmail.com> | ||
* MIT Licensed | ||
@@ -5,0 +5,0 @@ */ |
/*! | ||
* damless-mongo | ||
* Copyright(c) 2018 Benoît Claveau <benoit.claveau@gmail.com> | ||
* Copyright(c) 2021 Benoît Claveau <benoit.claveau@gmail.com> | ||
* MIT Licensed | ||
@@ -5,0 +5,0 @@ */ |
/*! | ||
* damless | ||
* Copyright(c) 2018 Benoît Claveau <benoit.claveau@gmail.com> | ||
* Copyright(c) 2021 Benoît Claveau <benoit.claveau@gmail.com> | ||
* MIT Licensed | ||
@@ -8,4 +8,4 @@ */ | ||
const { promisify } = require("util"); | ||
const { pipeline } = require("stream"); | ||
const pipelineAsync = promisify(pipeline); | ||
const stream = require("stream"); | ||
const pipeline = promisify(stream.pipeline); | ||
@@ -19,4 +19,7 @@ class Users extends Http { | ||
async customHttpFind(context, stream, headers) { | ||
await pipelineAsync( | ||
this.find(), | ||
const { filter, options } = this.qs.parse(context.querystring); | ||
const cursor = await this.mongoFind(filter, options); | ||
await pipeline( | ||
cursor, | ||
stream.on("data", data => console.log(data)) | ||
@@ -27,8 +30,11 @@ ); | ||
async httpUserByCity(context, stream, headers) { | ||
await pipelineAsync( | ||
this.aggregate([ | ||
{ $group: { _id: "$address.city", count: { $sum: 1 }, logins: { $push: { login: "$login" }}}}, | ||
{ $project: { _id: 0, city: "$_id", count: 1, logins: 1}}, //_id: 0 -> remove id | ||
{ $sort: { city: 1 } }, | ||
]), | ||
const cursor = await this.mongoAggregate([ | ||
{ $group: { _id: "$address.city", count: { $sum: 1 }, logins: { $push: { login: "$login" }}}}, | ||
{ $project: { _id: 0, city: "$_id", count: 1, logins: 1}}, //_id: 0 -> remove id | ||
{ $sort: { city: 1 } }, | ||
]); | ||
await pipeline( | ||
cursor, | ||
stream | ||
@@ -35,0 +41,0 @@ ); |
/*! | ||
* damless-mongo | ||
* Copyright(c) 2018 Benoît Claveau <benoit.claveau@gmail.com> | ||
* Copyright(c) 2021 Benoît Claveau <benoit.claveau@gmail.com> | ||
* MIT Licensed | ||
@@ -35,4 +35,4 @@ */ | ||
const config = await damless.resolve("config"); | ||
if (config.mongo.host !== "localhost") throw new Error("Inconherent mongo connectionString."); | ||
if (config.mongo.database !== "test") throw new Error("Inconherent mongo connectionString."); | ||
if (config.mongo.host !== "localhost") throw new Error("Unexpected mongo connectionString."); | ||
if (config.mongo.database !== "test") throw new Error("Unexpected mongo connectionString."); | ||
@@ -51,4 +51,4 @@ await this.clear(); | ||
const { damless } = this; | ||
let mongo = await damless.resolve("mongo"); | ||
let db = await mongo.connect(); | ||
const mongo = await damless.resolve("mongo"); | ||
const db = await mongo.connect(); | ||
await db.createCollection("users"); | ||
@@ -76,5 +76,5 @@ }; | ||
const { damless } = this; | ||
let mongo = await damless.resolve("mongo"); | ||
let db = await mongo.connect(); | ||
await db.collection("users").deleteMany(); | ||
const mongo = await damless.resolve("mongo"); | ||
const db = await mongo.connect(); | ||
await db.dropDatabase(); | ||
}; | ||
@@ -81,0 +81,0 @@ |
Sorry, the diff of this file is not supported yet
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
0
187
329225
26
1409