I have many promises that have a resulting value I need to export. This value is in fact a connection to an embedded database.
The problem is I have several middleware promises that work on the connection before resolving the final db resource. I do this for each collection in the DB.
If I use the method many times I am creating several connections to this DB with many copies of its own middleware which can grow rather large. How Do I export this promise and all the others in one final object?
Should I create a singleton class that in the constructor resolves the promises and has the collection items as public variables? Or should I some how resolve to export them in an object?
Edit - show example promise. One of many
This is just one of the collection connections. Notice that it creates a new collection when this method is called. An in memory binary tree is created with the IndexLoader for each collection.
This is why I wish to resolve this method and return place the result which is the collection into an object to be used there. If I were to call this method all over the application then it would be creating a new binary tree each time.
export const Users = (): Promise<Collection> => {
  return new Promise<Collection>((resolve, reject) => {
      const User: Collection = new Collection("Users");
      const indexPromises: Array<Promise<any>> = [];
      const uniqIndices: string[] = ["username"];
      User.pre("save", setDefaults);
      User.pre("update", updateDefaults);
      uniqIndices.forEach((v) => {
        indexPromises.push(IndexLoader(User, {fieldName: v, unique: true}));
      });
      Promise.all(indexPromises)
          .then(() => {
              resolve(User);
          })
          .catch(reject);
  });
};
Edit 2 - what I meant by singleton resolution export
I feel like this use of a singleton is sound.
import {Users} from "./file"; // this is the method above
export interface IDB {
    Users: Collection;
}
export class DB implements IDB {
    public static getInstance() {
        if (!DB.instance) {
            DB.instance = new DB();
        }
        return DB.instance;
    }
    private static instance: DB;
    public Users: Collection;
    
    constructor() {
        Users()
          .then((res) => {
              this.Users = res;
          })
          .catch(/** error handler */);
     }
}
Using class in other file to interact with same connection and binary tree
import {DB} from "..";
let db = DB.getInstance();
db.Users.insert({/** new object */})
    .then()
    .catch()
 
     
    