Skip to main content

EzModel - CRUD Generation

Overview

ezmodel

The EzModel is a combination of two EzApps, namely the EzRepo and the EzRouter

EzAppDescription
EzRepo1. Creates a Table in your Database of choice
2. Creates a Repository that you can use to access the table
EzRouterFrom a Repository, automatically generate Create, Read, Update and Delete (CRUD) endpoints

Simple Explanation

Schemas represent how data gets put into the database (DB)

You can imagine this by comparing it to excel,

  • The DB is an excel file where the data is stored
  • The Table refers to a single excel sheet where the data is stored. A single schema makes a table
  • The column names which specify what goes into the excel sheet are the schema properties

Create your first EzModel

If we want a Table to store information for Pets

Table Name - Pets

PropertyJS Type
namestring
speciesstring
ageinteger
const pets = new EzModel("Pets", {
name: Type.VARCHAR, //String
species: Type.VARCHAR,
age: Type.INT, //Integer
});

app.addApp(pets,{prefix:'pets'})
const pets = new EzModel('Pets',{...})

We make a new model using the new EzModel(). It will be stored with the name Pets in the database

{
name: Type.VARCHAR, //String
species: Type.VARCHAR,
age: Type.INT //Integer
}

You can view all of the available types here

info

EzBackend automatically generates default CRUD routes for each of your defined models

Database Access

You may want to access the database directly in your code. You can obtain the Repository directly from the EzModel

Understanding the Repository

With the repo, some of the actions you can perform on your database are

  1. create
  2. update
  3. find
  4. delete
  5. count
  6. ...and many more

For example, if you want to create a route to get the count of pets:

const pets = new EzModel("Pets", {...});

pets.get('/count', async (req,res) => {
const petRepo = pets.getRepo()
const petCount = petRepo.count()
return {"petCount": petCount}
})
caution

You can only access the Repository within endpoints or handlers after the postInit lifecycle.

Bad Examples
const petRepo = pets.getRepo() //BAD

pets.setInit(() => {
const petRepo = pets.getRepo() //BAD
})
Good Examples
pets.get('/count', async (req,res) => {
const petRepo = pets.getRepo() //GOOD
})

pets.setHandler(() => {
const petRepo = pets.getRepo() //GOOD
}))

Create

Create a row in the database with the specified data

When creating an entry, it must match the schema specified, otherwise an error will be thrown.

returns newly created object

const repo = pets.getRepo()
const newPet = await repo.create({
name: "Mr Woof",
species: "Poodle",
age: 5
})

Insert

Insert one or more rows into the database.

When creating an entry, it must match the schema specified, otherwise an error will be thrown.

const repo = pets.getRepo();
await repo.insert([
{
name: "Mr Doof",
species: "Labrador",
age: 7,
},
{
name: "Mr Hoof",
species: "Horse",
age: 13,
},
]);

Update

Update an object in the database by search term or id

When updating an entry, it must match the schema specified, otherwise an error will be thrown.

const repo = pets.getRepo();

await repo.update(1, { name: "Mr Cuddles" });
//Update the pet with ID 1 with the name "Mr Cuddles"

await repo.update({ name: "Mr Woof" }, { name: "Mr Cuddles" });
//Update the pet with current name "Mr Woof" with the name "Mr Cuddles"

Delete

Delete an object in the database by search term, id, or array of ids

const repo = pets.getRepo();
await repo.delete(1);

await repo.delete([1,2,3]);

await repo.delete({ name: "Mr Woof" });

Count

Count the number of object in the database by search term or id

const repo = pets.getRepo();

const numPets = await repo.count();

const numHorses = await repo.count({species: "horse"})

Find

Find an object in the database by search term

const repo = pets.getRepo();

const horses = await repo.find({species: "horse"})

Find By Ids

Find an object in the database by multiple ids

const repo = pets.getRepo();

const horses = await repo.find([1,2,3])

Find One

Find an object in the database by single id

const repo = pets.getRepo();

const horses = await repo.findOne(1)