EzModel - CRUD Generation
Overview
The EzModel is a combination of two EzApps, namely the EzRepo and the EzRouter
EzApp | Description |
---|---|
EzRepo | 1. Creates a Table in your Database of choice 2. Creates a Repository that you can use to access the table |
EzRouter | From 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
Property | JS Type |
---|---|
name | string |
species | string |
age | integer |
- Sample
- Full Sample
const pets = new EzModel("Pets", {
name: Type.VARCHAR, //String
species: Type.VARCHAR,
age: Type.INT, //Integer
});
app.addApp(pets,{prefix:'pets'})
import { EzBackend, EzModel, Type } from "@ezbackend/common";
const app = new EzBackend()
const pets = new EzModel("Pets", {
name: Type.VARCHAR, //String
species: Type.VARCHAR,
age: Type.INT, //Integer
});
app.addApp(pets,{prefix:'pets'})
app.start()
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
create
update
find
delete
count
- ...and many more
For example, if you want to create a route to get the count of pets:
- Sample
- Full Sample
const pets = new EzModel("Pets", {...});
pets.get('/count', async (req,res) => {
const petRepo = pets.getRepo()
const petCount = petRepo.count()
return {"petCount": petCount}
})
import { EzBackend, EzModel, Type } from "@ezbackend/common";
const app = new EzBackend()
const pets = new EzModel("Pets", {
name: Type.VARCHAR, //String
species: Type.VARCHAR,
age: Type.INT, //Integer
});
pets.get('/count', async (req,res) => {
const petRepo = pets.getRepo()
const petCount = petRepo.count()
return {"petCount": petCount}
})
app.addApp(pets,{prefix:'pets'})
app.start()
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
- Sample
- Full Sample
const repo = pets.getRepo()
const newPet = await repo.create({
name: "Mr Woof",
species: "Poodle",
age: 5
})
import { EzModel, EzBackend } from "@ezbackend/common";
const app = new EzBackend();
const pets = new EzModel("Pets", {
name: Type.VARCHAR, //String
species: Type.VARCHAR,
age: Type.INT, //Integer
});
pets.post("/create", async (req, res) => {
const repo = pets.getRepo();
const newPet = await repo.create({
name: "Mr Woof",
species: "Poodle",
age: 5,
});
return newPet;
});
app.addApp(pets, { prefix: "pets" });
app.start()
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.
- Sample
- Full Sample
const repo = pets.getRepo();
await repo.insert([
{
name: "Mr Doof",
species: "Labrador",
age: 7,
},
{
name: "Mr Hoof",
species: "Horse",
age: 13,
},
]);
import { EzModel, EzBackend } from "@ezbackend/common";
const app = new EzBackend();
const pets = new EzModel("Pets", {
name: Type.VARCHAR, //String
species: Type.VARCHAR,
age: Type.INT, //Integer
});
pets.post("/insert", async (req, res) => {
const repo = pets.getRepo();
await repo.insert({
name: "Mr Woof",
species: "Poodle",
age: 5,
});
await repo.insert([
{
name: "Mr Doof",
species: "Labrador",
age: 7,
},
{
name: "Mr Hoof",
species: "Horse",
age: 13,
},
]);
return {inserted: true}
});
app.addApp(pets, { prefix: "pets" });
app.start();
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.
- Sample
- Full Sample
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"
import { EzModel, EzBackend } from "@ezbackend/common";
const app = new EzBackend();
const pets = new EzModel("Pets", {
name: Type.VARCHAR, //String
species: Type.VARCHAR,
age: Type.INT, //Integer
});
pets.post("/update", async (req, res) => {
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"
return {updated: true}
});
app.addApp(pets, { prefix: "pets" });
app.start();
Delete
Delete an object in the database by search term, id, or array of ids
- Sample
- Full Sample
const repo = pets.getRepo();
await repo.delete(1);
await repo.delete([1,2,3]);
await repo.delete({ name: "Mr Woof" });
import { EzModel, EzBackend } from "@ezbackend/common";
const app = new EzBackend();
const pets = new EzModel("Pets", {
name: Type.VARCHAR, //String
species: Type.VARCHAR,
age: Type.INT, //Integer
});
pets.post("/delete", async (req, res) => {
const repo = pets.getRepo();
await repo.delete(1);
await repo.delete([1,2,3]);
await repo.delete({ name: "Mr Woof" });
return {deleted: true}
});
app.addApp(pets, { prefix: "pets" });
app.start();
Count
Count the number of object in the database by search term or id
- Sample
- Full Sample
const repo = pets.getRepo();
const numPets = await repo.count();
const numHorses = await repo.count({species: "horse"})
import { EzModel, EzBackend } from "@ezbackend/common";
const app = new EzBackend();
const pets = new EzModel("Pets", {
name: Type.VARCHAR, //String
species: Type.VARCHAR,
age: Type.INT, //Integer
});
pets.post("/count", async (req, res) => {
const repo = pets.getRepo();
const numPets = await repo.count();
const numHorses = await repo.count({species: "horse"})
return {
numPets: numPets,
numHorses: numHorses
};
});
app.addApp(pets, { prefix: "pets" });
app.start();
Find
Find an object in the database by search term
- Sample
- Full Sample
const repo = pets.getRepo();
const horses = await repo.find({species: "horse"})
import { EzModel, EzBackend } from "@ezbackend/common";
const app = new EzBackend();
const pets = new EzModel("Pets", {
name: Type.VARCHAR, //String
species: Type.VARCHAR,
age: Type.INT, //Integer
});
pets.post("/find", async (req, res) => {
const repo = pets.getRepo();
const horses = await repo.find({species: "horse"})
return horses;
});
app.addApp(pets, { prefix: "pets" });
app.start();
Find By Ids
Find an object in the database by multiple ids
- Sample
- Full Sample
const repo = pets.getRepo();
const horses = await repo.find([1,2,3])
import { EzModel, EzBackend } from "@ezbackend/common";
const app = new EzBackend();
const pets = new EzModel("Pets", {
name: Type.VARCHAR, //String
species: Type.VARCHAR,
age: Type.INT, //Integer
});
pets.post("/find-by-id", async (req, res) => {
const repo = pets.getRepo();
const horses = await repo.find([1,2,3])
return horses;
});
app.addApp(pets, { prefix: "pets" });
app.start();
Find One
Find an object in the database by single id
- Sample
- Full Sample
const repo = pets.getRepo();
const horses = await repo.findOne(1)
import { EzModel, EzBackend } from "@ezbackend/common";
const app = new EzBackend();
const pets = new EzModel("Pets", {
name: Type.VARCHAR, //String
species: Type.VARCHAR,
age: Type.INT, //Integer
});
pets.post("/find-one", async (req, res) => {
const repo = pets.getRepo();
const horses = await repo.findOne(1)
return horses;
});
app.addApp(pets, { prefix: "pets" });
app.start();