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)

Parameter

Description

name

Stringarrow-up-right

Model name, this will be the name of the JSON file and will be used to identify the database

Objectarrow-up-right

The schema of the model.

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

circle-exclamation

Schema

Parameter
Receive

required

default

(optional)

Receives a default value if none is passed when creating a document, this default value must follow the type defined

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

Last updated