cypress-mongodb
Version:
Cypress MongoDB plugin
67 lines • 2.58 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.updateOne = updateOne;
exports.updateMany = updateMany;
const validator_1 = require("../utils/validator");
const bson_1 = require("bson");
function updateOne(filter, document, options) {
return cy.env(['mongodb']).then(({ mongodb }) => {
const args = {
uri: mongodb.uri,
database: options?.database || mongodb.database,
collection: options?.collection || mongodb.collection,
options: options,
filter: filter,
document: document,
};
(0, validator_1.validate)(args);
if (!filter) {
throw new Error('Filter must be specified');
}
else if (typeof filter !== 'object' || Array.isArray(filter)) {
throw new Error('Filter must be an object');
}
if (!document) {
throw new Error('Document must be specified');
}
else if (typeof document !== 'object' || Array.isArray(document)) {
throw new Error('Document must be an object');
}
args.filter = (0, bson_1.serialize)(args.filter);
args.document = (0, bson_1.serialize)(args.document);
return cy.task('updateOne', args).then((result) => {
return (0, bson_1.deserialize)(Buffer.from(result));
});
});
}
function updateMany(filter, document, options) {
return cy.env(['mongodb']).then(({ mongodb }) => {
const args = {
uri: mongodb.uri,
database: options?.database || mongodb.database,
collection: options?.collection || mongodb.collection,
options: options,
filter: filter,
document: document,
};
(0, validator_1.validate)(args);
if (!filter) {
throw new Error('Filter must be specified');
}
else if (typeof filter !== 'object' || Array.isArray(filter)) {
throw new Error('Filter must be an object');
}
if (!document) {
throw new Error('Document must be specified');
}
else if (typeof document !== 'object' || Array.isArray(document)) {
throw new Error('Document must be an object');
}
args.filter = (0, bson_1.serialize)(args.filter);
args.document = (0, bson_1.serialize)(args.document);
return cy.task('updateMany', args).then((result) => {
return (0, bson_1.deserialize)(Buffer.from(result));
});
});
}
//# sourceMappingURL=update.js.map