首页 > 解决方案 > 无法创建模块化的抽象函数来更新 mongodb (mongoose) 数据库

问题描述

我正在尝试创建一个模块化和抽象的函数组,以使用 Node JS 将数据保存到我的 Mongodb (Mongoose) 数据库,如下所示:

const mongoose = require('mongoose')
mongoose.connect('xyz123blahblahblah')
const db       = mongoose.connection
const Schema   = mongoose.Schema
const ObjectId = Schema.ObjectId

var doc
var docInstance

// Connect to the database.
function connect() {
    console.log('Establishing a connection.')
    db.on('error', console.error.bind(console, 'Connection error:'))

    db.once('open', function() {
      console.log('Connection established.')
      updateCollection() 
    })
}

function updateCollection(schemaName, schemaObject, schemaProperties) {
    doc         = mongoose.model(schemaName, schemaObject)
    docInstance = new doc(schemaProperties)
}

function save() {
    console.log('Saving document to the collection.')
    docInstance.save(function (err, docInstance) {
        if (err) { return console.error(err) } 
        else { console.log('Document was succesfully added.') }
    })
}

现在的想法是,稍后,我可以导出这些函数并按顺序调用它们,如下所示:

// Define the Collection model.
const User = new Schema( { name: String, email: String, id: ObjectId} )

connect()
updateCollection('users', User, { name: 'Testy McTest', email: 'test@test.co' })
save()

通过 connect() 函数传递 updateCollection() 函数的参数对我来说没有意义,因为它们本质上或多或少负责两个不同的事情,但是我的 updateCollection() 函数需要在我的内部调用连接()函数。

对我来说理想的解决方案是能够执行以下操作:

// Define the Collection model.
const User = new Schema( { name: String, email: String, id: ObjectId} 

connect(updateCollection('users', User, { name: 'Testy McTest', email: 'test@test.co' }))
save()

我现在对如何进行有点迷茫。是否可以以这种方式构造我的 connect() 和 updateCollection() 函数以确保关注点分离?任何帮助将非常感激!

标签: javascriptnode.jsmongodbfunctioncallback

解决方案


var doc
var docInstance

function connect(updateCollection) {
    console.log('Establishing a connection...')
    db.on('error', console.error.bind(console, 'Connection error:'))

    db.once('open', function() {
      console.log('Connection established.')
      updateCollection 
    })
}

function updateCollection(schemaName, schemaObject, schemaProperties) {
    doc = mongoose.model(schemaName, schemaObject)
    for (let i = 0; i < schemaProperties.length; i++) { 
        docInstance = new doc(schemaProperties[i])
        save() 
    }
    /*  
        TODO:
        The connection is currently being closed BEFORE
        all collections have been added to the database.
        We need a way to prevent the connection from closing
        until all Update operations are complete.
    */
    // console.log('Closing the connection.')
    // mongoose.connection.close()
}

function save() {
    console.log('Saving document to the collection...')
    docInstance.save(function (err, docInstance) {
        if (err) { return console.error(err) } 
        else { console.log('Document was succesfully added.') }
    })
}

在花了很多时间在这之后,我相信我已经找到了最佳解决方案。回调。我现在可以像这样调用我的 connect() 函数:

connect(updateCollection( 'users', User, [{ name: 'Testy 5', email: 'test5@test.co' }, { name: 'Testy 6', email: 'test6@test.co' }] ))

此外,我更新了我的 updateCollection() 方法,以获取对象数组中的 X 个对象来更新数据库中的行:)


推荐阅读