Schema

Importing

const { Schema } = new dbLocal({ path: "./databases" });

Schema is basically a model to be followed and also a separate database.

It receives two parameters, the first is the name of the model and the second is the standard object of the model, which will be a basis for each document inserted in it.

const ModelName = "User"
const BaseModel = {
  _id: { 
    type: Number
  },
  name: String,
  bag: {
    items: Array,
    id: String
  }
}

const User = Schema(ModelName, BaseModel)

Schema(model, schema)

Model base

This is where the magic happens, the base model will do the job of typing documents and maintaining a pattern between them, nothing escapes the base model.

Each property of the base model can receive the following properties

The base model must always have a property called _id of type String or Number, since searches in the database will be done from _id

Schema

Example

In this example I will create the model of a user, which must have a _id, username that will be required, age that by default it will be 15 and cats that by default will come with a registered cat

const User = Schema("User", {
  _id: Number,
  username: { type: String, required: true },
  age: { type: Number, default: 15 },
  cats: { type: Array, default: [ { cat: "my cat" } ] }
});

Last updated