javascript - 无法创建模块化的抽象函数来更新 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() 函数以确保关注点分离?任何帮助将非常感激!
解决方案
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 个对象来更新数据库中的行:)
推荐阅读
- javascript - 如何编写代码来计算特定 div 在 JavaScript 中出现的次数
- svg - SVG颜色从页面元素到光标不同
- ios - URLSession 凭据缓存允许使用不正确的凭据进行身份验证
- spring - Spring Boot 在 Tomcat 8.5.x 但不在 8.0.37 上运行 java.lang.NoClassDefFoundError: org/apache/coyote/UpgradeProtocol
- python - 使用 Python 从大型非结构化文本文件中提取数据元素
- java - JHipster - Hibernate 2nd cache / ehcache 试图从 JHipster 生成的项目中弹出 JHipster 的问题
- ios - iOS Swift:从 UILabel 创建掩码
- php - 从 ACF 字段自动填充自定义帖子类型标题
- python - 将可变长度的嵌套数组输出到 CSV
- sql-server - 循环遍历表以动态创建表结构