# Schema

### Importing

```javascript
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.

```javascript
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                           | <p><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String">String</a></p><p>Model name, this will be the name of the JSON file and will be used to identify the database</p> |
| [schema](#schema-model-schema) | <p><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object">Object</a></p><p>The schema of the model.</p>                                                                     |

### 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

{% hint style="warning" %}
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`
{% endhint %}

#### Schema

| Parameter | Receive                                                                                                                                                                                                                                                                                                                                                                                                          |
| --------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| type      | [Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object) \| [String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String) \| [Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array) \| [Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean) |
| required  | [Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean) (optional)                                                                                                                                                                                                                                                                                                   |
| default   | <p>(optional)</p><p>Receives a default value if none is passed when creating a document, this default value must follow the <strong>type</strong> defined</p>                                                                                                                                                                                                                                                    |

### 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

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