Schemas
Creating a schema for the database.
🪶 Create a schema for your model, schemas are useful to ensure consistency and facilitate data validation:
A schema in MerlinDB is a structure that defines the organization of data into documents within a collection.
1: Call it like this:
1 | import MerlinDB, {Schema} from "@chrisaxxwell/merlin-db"; |
2 | |
3 | |
4 | const merlin = MerlinDB(); |
5 | |
6 | |
7 | var MyCatsSchema = merlin.Schema(<your-schema>); |
8 | |
9 | var MyCatsSchema = Schema(<your-schema>); |
The Schema method accepts an 'Object' property:
1 | |
2 | const myCatSchema = merlin.Schema({ |
3 | name: String, |
4 | age: { type: Number, required: true}, |
5 | nick: { |
6 | type: [String, Array], |
7 | unique: true, |
8 | required: [true, "Enter your nickname!"], |
9 | }, |
10 | breed: [String, Object], |
11 | birth: Date |
12 | }); |
1: The available data types are:
String;
Number;
Date;
Object;
Array;
Boolean;
Function.
See more about data types in (schema-types);
2: Setting its property with { required: true } ensures that the data is required*.
3: To ensure that the field is unique, define { unique: true }.
Defining type as an Array ensures that multiple types are accepted, suppose in 'birth' you want to accept both string ('03/18/2019') and Date (Date[schema1 ]. now()), then define [String, Date].
NOTE: To call any method, like find, deleteOne, update... you need to define a Schema for your model first;