UNPKG

cypress-mongodb

Version:
107 lines (106 loc) 4.28 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.findOne = findOne; exports.findOneAndUpdate = findOneAndUpdate; exports.findOneAndDelete = findOneAndDelete; exports.findMany = findMany; var validator_1 = require("../utils/validator"); var bson_1 = require("bson"); function findOne(query, options) { var args = { uri: Cypress.env('mongodb').uri, database: (options === null || options === void 0 ? void 0 : options.database) || Cypress.env('mongodb').database, collection: (options === null || options === void 0 ? void 0 : options.collection) || Cypress.env('mongodb').collection, options: options, query: query, }; (0, validator_1.validate)(args); if (!query) { throw new Error('Query must be specified'); } else if (typeof query !== 'object' || Array.isArray(query)) { throw new Error('Query must be a valid mongodb query object'); } args.query = (0, bson_1.serialize)(args.query); return cy.task('findOne', args).then(function (result) { if (result !== null) return (0, bson_1.deserialize)(Buffer.from(result)); else return null; }); } function findOneAndUpdate(filter, document, options) { var args = { uri: Cypress.env('mongodb').uri, database: (options === null || options === void 0 ? void 0 : options.database) || Cypress.env('mongodb').database, collection: (options === null || options === void 0 ? void 0 : options.collection) || Cypress.env('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('findOneAndUpdate', args).then(function (result) { if (result !== null) return (0, bson_1.deserialize)(Buffer.from(result)); else return null; }); } function findOneAndDelete(filter, options) { var args = { uri: Cypress.env('mongodb').uri, database: (options === null || options === void 0 ? void 0 : options.database) || Cypress.env('mongodb').database, collection: (options === null || options === void 0 ? void 0 : options.collection) || Cypress.env('mongodb').collection, options: options, filter: filter, }; (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'); } args.filter = (0, bson_1.serialize)(args.filter); args.options = (0, bson_1.serialize)(args.options); return cy.task('findOneAndDelete', args).then(function (result) { if (result !== null) return (0, bson_1.deserialize)(Buffer.from(result)); else return null; }); } function findMany(query, options) { var args = { uri: Cypress.env('mongodb').uri, database: (options === null || options === void 0 ? void 0 : options.database) || Cypress.env('mongodb').database, collection: (options === null || options === void 0 ? void 0 : options.collection) || Cypress.env('mongodb').collection, options: options, query: query, }; (0, validator_1.validate)(args); if (!query) { throw new Error('Query must be specified'); } else if (typeof query !== 'object' || Array.isArray(query)) { throw new Error('Query must be a valid mongodb query object'); } args.query = (0, bson_1.serialize)(args.query); return cy.task('findMany', args).then(function (result) { return Object.values((0, bson_1.deserialize)(Buffer.from(result))); }); }