Table

In SchemaJS, the Table global represents the structure of a database table, defining its columns and the queries that can be performed on it. It provides an interface for easily constructing, manipulating, and querying database tables, making it highly flexible for various use cases.

Constructor

new Table(tblName: string);

addColumn

Adds a column to the table. Columns define the structure of each entry in the table, such as the data type and constraints.

table.addColumn(col: Column);

addQuery

Defines a custom query for the table. This query can be executed later, allowing dynamic and specific data retrieval operations.

table.addQuery("findById", async (req) => {
    let query = new QueryBuilder().where("id", "=", req.id);
    return await query(query);
});

on

The .on() method in the Table global allows you to register hooks (event listeners) that execute custom logic when specific actions, such as inserts, occur on the table. It provides a way to extend or modify the behavior of table operations, like inserting new rows, by running custom code before or after the operation completes.

table.on("insert", (rows: any[]) => {
        console.log("Insertion detected", rows);
});

Last updated