# Schemas

Every collection (table) in the database has its own schema file. This file defines the schema for the collection and the types for all properties. Here is an example of a schema

import { Schema, Document } from "mongoose";

export const DashboardSchema = new Schema({
    resort_id: {type: String, default: ''},
    data: {type: String, default: ''}
    // visits is not here because it's a complex object (dynamic) that mongoose does not understand
}, {collection: 'dashboard'});

export interface DashboardModel extends Document
{
    resort_id: string;
    data: string;
}

This schema defines that the collection has 2 properties. One is called resort_id and is a string that default to empty. The second, named data and of type string also defaults to empty.

The second part of the schema is the name we wish to use in the database.

The interface being exported here is the type that can be used in your code to define something as an instance of data coming from this collection.

let myObject: DashboardModel | null = null;

This interface defines all the properties from this document type. It must have all the properties from the schema above with the same types or custom types (depending on the complexity of your schema). But it can also have properies that are not in the database, like computed values or extra pieces of data. To do that you can use the basic typescript optional property like so:

export interface DashboardModel extends Document
{
    resort_id: string;
    data: string;
    extra_number?: number;
}

Once you are done with your schema, you need to tell the system to load it and share it. You first go in the model.ts file to define all of that.

First, you import your schema file.

import * as NameOfYourSchema from "./nameof.schema";

Then, you export the model from this file. This will expose it to the rest of the platform

export { NameOfSchemaModel } from "./nameof.schema";

And finally you add it to the warmInstances object by giving it a name as the key and you create and instance of your model with the following code:

yourschemakey: model<NameOfYourSchema.YourSchema.Model>('reference_name', NameOfYourSchema.Schema);

This may look like a crazy line of code but it will save you from retyping this line over and over and over again when you need your model somewhere in the code.

To access your model instance, you can simply call:

import { NameOfSchemaModel, fetchModel } from '../schema/models';

fetchModel<NameOfSchemaModel>('name_key');

Here name_key is the key you set for your model in the warmInstances object in the model.ts file. In our example, that is yourschemakey.

One last thing, you need to create a type for your model. To do this, go to core/types.ts. In this file, you can see many type definitions for model related things at the top of the file. Add yours there using the Mode<name> format. like this:

export type YourSchemaModelType = Model<YourSchemaModel>;

For each type we add, the name must finish with ModelType to be clear to other developers as to what it really is. Normally it's modelnameModelType. For example: UserModelType for the User model.

Once all of this is done, you can now create your own Provider and Writer.

Last Updated: 7/15/2022, 3:12:37 PM