Hello World

Also known as "Your First Table"

Read about "Setting up your environment" before writing your first table.

Content

  • You will create a table called books

  • You will insert a row

  • You will query the inserted row

Example Table

After having set-up your environment, we will create a table called books. For this, we will create a file named books.ts inside your tables folder. It will look like this:

./public/tables/books.ts
export default function main() {
    const { Table, Column } = SchemaJS;
    return new Table("books")
        .addColumn(new Column("id").string())
        .addColumn(new Column("name").string())
        .addColumn(new Column("author").string())
}

Insert a row

After having created our table, we will run the engine by executing the following terminal command from the root of where our SchemaJS.toml is.

schemajs start

Running start will open up a REPL after having initialized our database.

In our REPL, we will type use to access the table. For this, we will first need to access the database and then the table context.

use("public")

use("books")

and finally, after being in the context of the table books, we will proceed to run the following Javascript code.

SchemaJS.insert({ id: "SOME-ID", "name": "The Lord of the Rings", "author": "J.R.R Tolkien" });

Querying a row

Finally, to query a row, we can run the following JS command:

SchemaJS.query(
  new SchemaJS.QueryBuilder()
    .and((and) => and.where("author", "=", "The Lord of the Rings"))
)
.then((res) => {
  SchemaJS.print(JSON.stringify(res));
});

Last updated