Friday 14 April 2017

Concept for modular mongoose schema system

I am currently developing a complete modular (web backend) system. It currently features a module loader that loads modules in an order, which resolves dependencies.


One core module is the database module. It basically features a database connection. What I realized is that, if I split up everything, I end up with tons of single schemas / collections since every module has to register its schema and I can't edit schemas 'on the fly'.


An example:

  • Database module
  • User module (features a user schema - therefore depends on the Database module)
  • User <-> Steam link module (User can link their account w/ a Steam account, but since I cannot edit the user schema after the User module got loaded, I need to create a separate schema, which contains the userID and the verification data)

Right now, I am thinking about how to handle schema creation. My current idea is to register schemata in the database module and let them be extended by later modules. After all modules have been loaded, I register the schemata to the database. For this concept I disregard the conflicts that might come up.

Problems:

A basic TypeScript schema implementation looks like the following:

import { Document, Model, Schema, model } from 'mongoose';
import { IConfig } from '../interfaces/IConfig';

export interface IConfigModel extends IConfig, Document { }

export const configSchema: Schema = new Schema({
    web: {
        maintenance: { type: Boolean, index: { unique: true }, required: true, default: 0 }
    }
});

export const Config: Model<IConfigModel> = model<IConfigModel>('Config', configSchema);

export default Config;

So adding to the configSchema won't be that big of a problem, but how could I handle the interfaces, which I need for methods and proper auto completion?

Do you have any concepts or tips for me? I wanna make this as modular as possible and don't want to 'hardcode' stuff for one specific project.



via Cludch

No comments:

Post a Comment