UNPKG

cypress-mongodb

Version:
51 lines 1.87 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.insertOne = insertOne; exports.insertMany = insertMany; const validator_1 = require("../utils/validator"); const bson_1 = require("bson"); function insertOne(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, 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((result) => { return result; }); }); } function insertMany(documents, options) { return cy.env(['mongodb']).then(({ mongodb }) => { const args = { uri: mongodb.uri, database: options?.database || mongodb.database, collection: options?.collection || 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((result) => { return result; }); }); } //# sourceMappingURL=insert.js.map