UNPKG

cypress-mongodb

Version:
47 lines (46 loc) 1.86 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.insertOne = insertOne; exports.insertMany = insertMany; var validator_1 = require("../utils/validator"); var bson_1 = require("bson"); function insertOne(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, document: document, }; (0, validator_1.validate)(args); 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.document = (0, bson_1.serialize)(args.document); return cy.task('insertOne', args).then(function (result) { return result; }); } function insertMany(documents, 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, documents: documents, }; (0, validator_1.validate)(args); if (!documents) { throw new Error('Documents must be specified'); } else if (!Array.isArray(documents)) { throw new Error('Documents must be an array'); } args.documents = (0, bson_1.serialize)(Object.fromEntries(args.documents.entries())); return cy.task('insertMany', args).then(function (result) { return result; }); }