I am looking for a way to use 1 schema for multiple databases and collections. I have 3 different databases and each holds 3 to 4 collections, which use all the same schema. I started just duplicating the schema for each collection and database, but that will add up to the amount of work. then I tried to export a function that returns the schema with set parameter, but I think that creates a new connection each time I call the function and eventually I get error. I can not find how to solve my problem the right way. Please someone can help me out a little.
What I currently try to use.
import mongoose from 'mongoose';
import Config from '../components/config.js';
const {database, localDatabase} = Config();
export default function(dex, network){
const db = mongoose.createConnection(localDatabase);
const transactions = db.useDb(network); // each network gets its own database
const txSchema = new mongoose.Schema({
uniquePoint:{
type: String,
required: true,
index: true,
unique : true,
},
version:{
type: String,
required: true,
},
network:{
type: String,
required: true,
},
},{collection: dex}); // each dex gets own collection
return transactions.model('TX', txSchema);
}
But this seems to give error after a while "MongoNetworkError: connect EADDRNOTAVAIL"