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,EAAM,QAAQ,GAAa,MAAM,IACf,MAAV,EACA,EAAM,OAAO,GAAM,MAAM,IACrB,GAAQ,IACT,OAAO,IAAW,EAAO,EAAK,IAC9B,GAAQ,IAChB,OAAO,IAAW,EAAO,EAAK,GAAE,GAE3C,EAEA,OAAO,QAAU,IAAI","file":"/npm/good-mongoose-seeder@1.1.2/seed.js","sourceRoot":"","sourcesContent":["/**\n * Minified by jsDelivr using Terser v5.39.0.\n * Original file: /npm/good-mongoose-seeder@1.1.2/seed.js\n *\n * Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files\n */\n","/**\r\n * @module seed\r\n * @author John L. Carveth\r\n * @version 1.1.0\r\n * A module to persist data to a MongoDB system on startup, 'seeding' the database.\r\n */\r\n\r\n/**\r\n * Dependencies\r\n */\r\nvar mongoose = require('mongoose')\r\nmongoose.Promise = global.Promise\r\n\r\n/**\r\n * @memberof module:seed\r\n * @class Seeder\r\n */\r\nconst Seeder = function () {\r\n    this.connected = false;\r\n    this.listeners = []\r\n}\r\n\r\n/**\r\n * @memberof module:seed.Seeder\r\n * @function connect opens a connection to the MongoDB server\r\n * @param {String} url the mongodb access url \r\n */\r\nSeeder.prototype.connect = function (url, opts) {\r\n    return mongoose.connect(url, opts).then(() => {\r\n        const connected = mongoose.connection.readyState == 1\r\n        this.connected = connected\r\n        this.listeners.forEach((l) => {\r\n            if (l.cause == 'onConnect') l.effect()\r\n        })\r\n        return connected;\r\n    });\r\n}\r\n\r\n/**\r\n * @memberof module:seed.Seeder\r\n * @function seedData\r\n * @param {Object} data the data to be seeded\r\n * @param {String} data.model the name of the model to which the data will be seeded\r\n * @param {Array} data.documents the documents to be seeded \r\n */\r\nSeeder.prototype.seedData = function (data) {\r\n    // Mongoose might have been connected by another source, so check\r\n    const connected = mongoose.connection.readyState == 1\r\n    this.connected = connected\r\n    if (!this.connected) {\r\n        console.error(new Error('Not connected to MongoDB'))\r\n        return false\r\n    }\r\n    // Stores all promises to be resolved\r\n    var promises = []\r\n    // Fetch the model via its name string from mongoose\r\n    const Model = mongoose.model(data.model)\r\n    // For each object in the 'documents' field of the main object\r\n    data.documents.forEach((item) => {\r\n        promises.push(promise(Model, item))\r\n    })\r\n    // Fulfil each Promise in parallel\r\n    return Promise.all(promises).then(() => {\r\n        return true\r\n    }).catch(() => {\r\n        console.log(`Connected:\\t${this.connected}`)\r\n    })\r\n}\r\n\r\n/**\r\n * @memberof module:seed.Seeder\r\n * @function addListener\r\n * @param {String} cause action flag the listener is listening for\r\n * @param {function} effect the function to be called when listener is triggered \r\n * Adds a new listener to the Seeder object, which will be called when an \r\n * action is triggered by the seeder. Supported causes are:\r\n *      - 'onConnect',\r\n *      - 'onDisconnect' \r\n */\r\nSeeder.prototype.addListener = function (cause, effect) {\r\n    this.listeners.push({'cause':cause,'effect':effect})\r\n}\r\n\r\n/**\r\n * @memberof module:seed.Seeder\r\n * @function disconnect\r\n * Closes the connection to MongoDB\r\n */\r\nSeeder.prototype.disconnect = function () {\r\n    mongoose.disconnect()\r\n    this.connected = false\r\n    this.listeners.forEach((l) => {\r\n        if (l.cause == 'onDisconnect') l.effect()\r\n   })\r\n}\r\n\r\n/**\r\n * @memberof module:seed.Seeder\r\n * @function isConnected\r\n * @return true if Seeder has an active connection to MongoDB\r\n */\r\nSeeder.prototype.isConnected = function () {\r\n    return this.connected\r\n}\r\n\r\n/**\r\n * Generates a Promise that seeds item to model\r\n * @param {mongoose.Model} model \r\n * @param {Object} item \r\n */\r\nconst promise = function (model, item) {\r\n    return new Promise((resolve, reject) => {\r\n        // To accurately query MongoDB, we need to extract a column name and value\r\n        var key = Object.keys(item)[0]\r\n        var value = item[key]\r\n        var queryParams = {}\r\n        queryParams[key] = value\r\n        // Find if a similar item already exists\r\n        model.findOne(queryParams).then((result) => {\r\n            if (result == null) {\r\n                model.create(item).then((result) => {\r\n                    resolve()\r\n                }).catch((error) => {reject(error)})\r\n            } else resolve() // Already exists so do not seed\r\n        }).catch((error) => {reject(error)})\r\n    })\r\n}\r\n\r\nmodule.exports = new Seeder()"]}