Mongoose Sync local schema with mongoose document

Viewed 91

Im trying to get this going or wondering if this is even necessary. I have a local schema I define for my user profiles. What im trying to do is write a code block that checks my local schema with what the database has. If a field is missing, add a default value.

My goal for this is to be able to add fields locally in my schema and have the database update when I add a new field.

My schema is as follows:

import mongoose, { Schema } from "mongoose";

const reqString = {
  type: String,
  required: true,
};

const reqNumber = {
  type: Number,
  required: true,
};

const userProfileSchema = new Schema({
  //Discord User ID - Primary Key
  _id: reqString,
  wallet: reqNumber,
  bank: reqNumber,
  net_worth: reqNumber,
  classID: reqNumber,
});

const name = "core-userprofile";
export default mongoose.models[name] ||
  mongoose.model(name, userProfileSchema, name);

I have a class that pulls the user profile for the rest of the code to access. Id like a function in there that looks at the local schema, realizes there isnt classID, figures out its a type of number, and just places 0 there.

I believe from what im understanding I can use update/upsert when accessing it. Im more wondering if there is a way to sync these two and add/delete anything that doesn't match the local version.

Thanks in advance!

0 Answers
Related