thaw-argon-promisified-mongodb
Version:
A promisified TypeScript interface to MongoDB intended for Node.js lts/argon (version 4.9.1) and WINDOWS XP ONLY.
89 lines (87 loc) • 3.7 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.createCollection = void 0;
var mongodb_1 = require("mongodb");
var thaw_argon_promisify_1 = require("thaw-argon-promisify");
// critter() : The Criteria Generator.
function critter(id) {
var criteria = {};
// Prevent 'prettier' from removing the quotes around '_id';
// they are necessary.
criteria['_id'] = new mongodb_1.ObjectID(id);
return criteria;
}
var PromisifiedCollection = /** @class */ (function () {
function PromisifiedCollection(collection) {
this.collection = collection;
}
PromisifiedCollection.prototype.createOne = function (dataToInsert) {
return thaw_argon_promisify_1.promisify(this.collection.insert, this.collection)(dataToInsert);
};
PromisifiedCollection.prototype.read = function (criteria) {
if (criteria === void 0) { criteria = {}; }
var cursor = this.collection.find(criteria);
return thaw_argon_promisify_1.promisify(cursor.toArray, cursor)();
};
PromisifiedCollection.prototype.readOneById = function (id) {
return thaw_argon_promisify_1.promisify(this.collection.findOne, this.collection)(critter(id));
};
PromisifiedCollection.prototype.readAll = function () {
return this.read({});
};
PromisifiedCollection.prototype.updateOneById = function (id, replacementData) {
// options: { safe?: any; remove?: boolean; upsert?: boolean; new?: boolean },
return thaw_argon_promisify_1.promisify(this.collection.findAndModify, this.collection)(critter(id), // query
[], // sort: any[]
replacementData, // doc
{ remove: false, upsert: false } // options
);
/* .then((result: any) => {
return Promise.resolve(result); // Returns the old version of the record
})
.catch((error: Error) => {
console.error(
'updateOneById() : error is',
typeof error,
error
);
return Promise.reject(error);
}); */
};
PromisifiedCollection.prototype.deleteOneById = function (id) {
return thaw_argon_promisify_1.promisify(this.collection.findAndRemove, this.collection)(critter(id), undefined, undefined)
.then(function (result) {
console.log('deleteOneById() : result is', typeof result, result);
if (result !== null) {
console.log('Record was found and removed.');
}
else {
console.log('Record was not found.');
}
return Promise.resolve(result !== null);
})
.catch(function (error) {
console.error('deleteOneById() : error is', typeof error, error);
return Promise.reject(error);
});
};
PromisifiedCollection.prototype.deleteAll = function () {
return thaw_argon_promisify_1.promisify(this.collection.drop, this.collection)()
.then(function (result) { return Promise.resolve(true); })
.catch(function (error) {
var errorCast = error;
if (typeof errorCast === 'undefined' ||
errorCast.errmsg !== 'ns not found') {
return Promise.reject(error);
}
else {
return Promise.resolve(false);
}
});
};
return PromisifiedCollection;
}());
function createCollection(client, databaseName, collectionName) {
return new PromisifiedCollection(client.db(databaseName).collection(collectionName));
}
exports.createCollection = createCollection;