Javascript 创建模块化抽象函数以更新mongodb(mongoose)数据库时遇到问题
我正在尝试创建一组模块化的抽象函数,以便使用节点JS将数据持久化到我的Mongodb(Mongoose)数据库,如下所示:Javascript 创建模块化抽象函数以更新mongodb(mongoose)数据库时遇到问题,javascript,node.js,mongodb,function,callback,Javascript,Node.js,Mongodb,Function,Callback,我正在尝试创建一组模块化的抽象函数,以便使用节点JS将数据持久化到我的Mongodb(Mongoose)数据库,如下所示: const mongoose = require('mongoose') mongoose.connect('xyz123blahblahblah') const db = mongoose.connection const Schema = mongoose.Schema const ObjectId = Schema.ObjectId var doc
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()函数的参数对我来说没有意义,因为它们基本上负责两件不同的事情,但是需要在connect()函数内部调用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.') }
})
}
此外,我还更新了updateCollection()方法,以在对象数组中获取X个对象来更新数据库中的行:)
connect(updateCollection( 'users', User, [{ name: 'Testy 5', email: 'test5@test.co' }, { name: 'Testy 6', email: 'test6@test.co' }] ))