Skip to content

Latest commit

 

History

History
93 lines (65 loc) · 2.87 KB

Readme.md

File metadata and controls

93 lines (65 loc) · 2.87 KB

EasyBack

Motivation

Developing a backend for a project often involves writing repetitive code for database operations such as insert, update, create, list, and more. When dealing with multiple tables, this redundancy can quickly become a burden. EasyBack simplifies backend development by automaticly providing rest interface for all It derives table definitions from your database and prepares a user-friendly interface API.

Insert

Perform an insert operation with a simple POST request. If you provide an ID, it updates the record with that identity. If the ID is null, 0, undefined, or an empty string, EasyBack creates a new record.

fetch("/api/v1/products", {
  method: "POST",
  header: { "content-type": "application/json" },
  body: JSON.stringify({ name: "Jean", user: 123, description: "My Jean" }),
});

Update

Update a field by specifying the ID of the record.

fetch("/api/v1/products", {
  method: "POST",
  header: { "content-type": "application/json" },
  body: JSON.stringify({ id: 97, name: "Red jean" }),
});

List of Records

Retrieve data with flexible filtering using the where parameter. You can specify conditions like id,eq,97, name,like,ice, or even multiple conditions like language,in,en,tr,ua. EasyBack also supports sorting with the orderBy option.

fetch(
  "/api/v1/products?where=name,like,jean|category,eq,45|orderby=name&orderby=name"
);

Return data

The return data is always a JSON object with the structure { msg: string, error: boolean, data: any[] } .

  • If no records are found in the database, it returns an empty array ([]).
  • On successful insert or update, it provides information like { affected: 1, insertedId: 132 }.

Recipes

Sequental

Here's an example of how to use EasyBack to retrieve images for a specific page using its slug:

let pageData = await fetch(`/api/v1/pages?where=id,eq,${slug}`).then((r) =>
  r.json()
);
let images = [];
if (!pageData.error && pageData.data.length) {
  images = await fetch(
    `/api/v1/images?where=page,eq,${pageData.data[0].id}`
  ).then((r) => r.json());
}

Simplify your backend development with EasyBack and focus on building your application's unique features instead of writing repetitive code.

Authentication

We prepared login/auth tracking but it can be shilded with your implementation on app.

Type Generation

After initializing, you can call easyback.generateTypes() which returns string represatation that you can save to a file.

const fs = require('fs');
easyback(app, db,{subPath:'/api'} )
const typeDefinitions = easyback.generateTypes();
fs.writeFile('src/types/db.ts', typeDefinitions, (err) => {
  if(err) {
  throw err;
  console.log("Types has been exported successfully.");
});

Todo

1) pageing
2) example to include custom auth
3) seperate type generator