Skip to content

Commit

Permalink
Merge pull request #18 from tonila/master
Browse files Browse the repository at this point in the history
new mongodb v5 and v6 driver support
  • Loading branch information
koresar authored Dec 27, 2023
2 parents fa672fe + 6d6a0b9 commit 2d96f0a
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 78 deletions.
22 changes: 11 additions & 11 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest]
node: ['12', '14', '16']
node: ['16', '18', '20']
steps:
- uses: actions/checkout@v2

Expand All @@ -30,8 +30,8 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest]
node: ['12', '14', '16']
redis-version: [4, 5, 6]
node: ['16', '18', '20']
redis-version: [5, 6, 7]
steps:
- uses: actions/checkout@v2

Expand All @@ -57,8 +57,8 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest]
node: ['12', '14', '16']
mongodb-version: [4.0, 5.0]
node: ['16', '18', '20']
mongodb-version: [6, 7]
steps:
- uses: actions/checkout@v2

Expand All @@ -74,9 +74,9 @@ jobs:

- run: npm install

- run: npm install mongodb@4
- run: npm install mongodb@6
- run: npm run test_mongo
- run: npm install mongodb@3
- run: npm install mongodb@5
- run: npm run test_mongo

test_mongo_single:
Expand All @@ -85,8 +85,8 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest]
node: ['12', '14', '16']
mongodb-version: [4.0, 5.0]
node: ['16', '18', '20']
mongodb-version: [6, 7]
steps:
- uses: actions/checkout@v2

Expand All @@ -102,7 +102,7 @@ jobs:

- run: npm install

- run: npm install mongodb@4
- run: npm install mongodb@6
- run: npm run test_mongo_single
- run: npm install mongodb@3
- run: npm install mongodb@5
- run: npm run test_mongo_single
128 changes: 61 additions & 67 deletions lib/mongodb-backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
"use strict";

var contract = require("./contract");
var async = require("async");
var _ = require("lodash");

// Name of the collection where meta and allowsXXX are stored.
Expand All @@ -21,9 +20,15 @@ function MongoDBBackend({ client, db, prefix, useSingle, useRawCollectionNames }
}

MongoDBBackend.prototype = {
close(cb) {
if (this.client) this.client.close(cb);
else cb();
async close(cb) {
try {
if (this.client) {
await this.client.close()
}
cb();
} catch(err) {
cb(err)
}
},

/**
Expand All @@ -37,69 +42,71 @@ MongoDBBackend.prototype = {
/**
Ends a transaction (and executes it)
*/
end(transaction, cb) {
async end(transaction, cb) {
contract(arguments).params("array", "function").end();
async.series(transaction, function (err) {
cb(err instanceof Error ? err : undefined);
});
try {
const promises = transaction.map(fn => fn())
await Promise.all(promises);
cb();
} catch(err) {
cb(err)
}
},

/**
Cleans the whole storage.
*/
clean(cb) {
async clean(cb) {
contract(arguments).params("function").end();
this.db.collections(function (err, collections) {
if (err instanceof Error) return cb(err);
async.forEach(
collections,
function (coll, innercb) {
coll.drop(function () {
innercb();
}); // ignores errors
},
cb
);
});
try {
const collections = await this.db.collections()
const promises = collections.map(coll => coll.drop())
await Promise.all(promises);
cb();
} catch(err) {
cb(err)
}
},

/**
Gets the contents at the bucket's key.
*/
get(bucket, key, cb) {
async get(bucket, key, cb) {
contract(arguments).params("string", "string|number", "function").end();
key = encodeText(key);
var searchParams = this.useSingle ? { _bucketname: bucket, key: key } : { key: key };
var collName = this.useSingle ? aclCollectionName : bucket;
try {
key = encodeText(key);
var searchParams = this.useSingle ? { _bucketname: bucket, key: key } : { key: key };
var collName = this.useSingle ? aclCollectionName : bucket;

let collection = this.db.collection(this.prefix + this.removeUnsupportedChar(collName));
let collection = this.db.collection(this.prefix + this.removeUnsupportedChar(collName));

if (!collection) return cb(new Error(`Cannot find the collection ${collName}`));
if (!collection) throw new Error(`Cannot find the collection ${collName}`);

// Excluding bucket field from search result
collection.findOne(searchParams, { projection: { _bucketname: 0 } }, function (err, doc) {
if (err) return cb(err);
// Excluding bucket field from search result
var doc = await collection.findOne(searchParams, { projection: { _bucketname: 0 } })
if (!_.isObject(doc)) return cb(undefined, []);
doc = fixKeys(doc);
cb(undefined, _.without(_.keys(doc), "key", "_id", "_bucketname"));
});
} catch(err) {
cb(err)
}
},

/**
Returns the union of the values in the given keys.
*/
union(bucket, keys, cb) {
async union(bucket, keys, cb) {
contract(arguments).params("string", "array", "function").end();
keys = encodeAll(keys);
var searchParams = this.useSingle ? { _bucketname: bucket, key: { $in: keys } } : { key: { $in: keys } };
var collName = this.useSingle ? aclCollectionName : bucket;
try {
keys = encodeAll(keys);
var searchParams = this.useSingle ? { _bucketname: bucket, key: { $in: keys } } : { key: { $in: keys } };
var collName = this.useSingle ? aclCollectionName : bucket;

let collection = this.db.collection(this.prefix + this.removeUnsupportedChar(collName));
let collection = this.db.collection(this.prefix + this.removeUnsupportedChar(collName));

if (!collection) return cb(new Error(`Cannot find the collection ${collName}`));
if (!collection) throw new Error(`Cannot find the collection ${collName}`);

collection.find(searchParams, { projection: { _bucketname: 0 } }).toArray(function (err, docs) {
if (err instanceof Error) return cb(err);
var docs = await collection.find(searchParams, { projection: { _bucketname: 0 } }).toArray()
if (!docs.length) return cb(undefined, []);

var keyArrays = [];
Expand All @@ -108,7 +115,9 @@ MongoDBBackend.prototype = {
keyArrays.push(...Object.keys(doc));
}
cb(undefined, _.without(_.union(keyArrays), "key", "_id", "_bucketname"));
});
} catch(err) {
cb(err)
}
},

/**
Expand All @@ -122,12 +131,12 @@ MongoDBBackend.prototype = {
var collectionIndex = this.useSingle ? { _bucketname: 1, key: 1 } : { key: 1 };
var updateParams = this.useSingle ? { _bucketname: bucket, key: key } : { key: key };
var collName = this.useSingle ? aclCollectionName : bucket;
transaction.push((cb) => {
transaction.push(async () => {
values = makeArray(values);

let collection = this.db.collection(this.prefix + this.removeUnsupportedChar(collName));

if (!collection) return cb(new Error(`Cannot find the collection ${collName}`));
if (!collection) throw new Error(`Cannot find the collection ${collName}`);

// build doc from array values
var doc = {};
Expand All @@ -136,24 +145,15 @@ MongoDBBackend.prototype = {
}

// update documents
collection.updateMany(updateParams, { $set: doc }, { safe: true, upsert: true }, (err) => {
if (err instanceof Error) return cb(err);
cb(undefined);
});
await collection.updateMany(updateParams, { $set: doc }, { safe: true, upsert: true })
});

transaction.push((cb) => {
transaction.push(async () => {
let collection = this.db.collection(this.prefix + this.removeUnsupportedChar(collName));

if (!collection) return cb(new Error(`Cannot find the collection ${collName}`));
if (!collection) throw new Error(`Cannot find the collection ${collName}`);

collection.createIndex(collectionIndex, (err) => {
if (err instanceof Error) {
return cb(err);
} else {
cb(undefined);
}
});
await collection.createIndex(collectionIndex)
});
},

Expand All @@ -166,15 +166,12 @@ MongoDBBackend.prototype = {
var updateParams = this.useSingle ? { _bucketname: bucket, key: { $in: keys } } : { key: { $in: keys } };
var collName = this.useSingle ? aclCollectionName : bucket;

transaction.push((cb) => {
transaction.push(async () => {
let collection = this.db.collection(this.prefix + this.removeUnsupportedChar(collName));

if (!collection) return cb(new Error(`Cannot find the collection ${collName}`));
if (!collection) throw new Error(`Cannot find the collection ${collName}`);

collection.deleteMany(updateParams, { safe: true }, (err) => {
if (err instanceof Error) return cb(err);
cb(undefined);
});
await collection.deleteMany(updateParams, { safe: true })
});
},

Expand All @@ -188,10 +185,10 @@ MongoDBBackend.prototype = {
var collName = this.useSingle ? aclCollectionName : bucket;

values = makeArray(values);
transaction.push((cb) => {
transaction.push(async () => {
let collection = this.db.collection(this.prefix + this.removeUnsupportedChar(collName));

if (!collection) return cb(new Error(`Cannot find the collection ${collName}`));
if (!collection) throw new Error(`Cannot find the collection ${collName}`);

// build doc from array values
var doc = {};
Expand All @@ -200,10 +197,7 @@ MongoDBBackend.prototype = {
}

// update documents
collection.updateMany(updateParams, { $unset: doc }, { safe: true, upsert: true }, (err) => {
if (err instanceof Error) return cb(err);
cb(undefined);
});
await collection.updateMany(updateParams, { $unset: doc }, { safe: true, upsert: true })
});
},

Expand Down

0 comments on commit 2d96f0a

Please sign in to comment.