Skip to main content

A8Astro


The A8Astro SDK enables A8Studio users to manage data stored in external databases, providing the flexibility to meet specific data privacy or data integrity requirements. This allows for seamless integration of data into your A8Studio applications.

Connecting to the Database from A8Studio

a8Astro.connect

To establish a connection between the database and A8Studio, you will use the following code snippet. (Example: In the onClick event handler of a button.)

Connects to the 'public' schema:
a8Astro.connect("your_connection_name"); // By default connects to the 'public' schema.
Connects to the 'specified' schema:
a8Astro.connect("your_connection_name", { dbSchema: "your_schema_id" }); // You specify the schema here.

Data Manipulation and Querying

Once connected to the database, you can use these comprehensive set of methods that A8Astro provides. These methods include the ability to retrieve data, create new entries, and update existing ones.

Let's consider the following Table: 'user_details':

id_first_last_email_age_
1JohnHamiltonjohn.hamilton@example.com35

Find

With the 'find' function, you can pull up a specific data from the table.

Method:
const dbTables = await a8Astro.connect("a8cloud");
await dbTables.user_details.find();
// 'user_details' is the table name.

Update

The 'update' function allows you to add data to an existing row.

Method:
const dbTables = await a8Astro.connect("a8cloud");
await dbTables.user_details.update();
id_first_last_email_age_
1JohnDoejohn.doe@example.com35

Create

The "create" function allows you to insert new values into the table. When using the "create" function, mandatory fields must be filled.

Method:
const dbTables = await a8Astro.connect("a8cloud");
await dbTables.user_details.create();
id_first_last_email_age_
1JohnDoejohn.doe@example.com35
2EthanLeeethan.lee@eample.com34

Create Many

You can use the "create many" function to insert multiple rows by providing an array of objects. Each object will represent a row in the table.

Method:
const dbTables = await a8Astro.connect("a8cloud");
await db.demoschema.user_details.createMany();
id_first_last_email_age_
1JohnDoejohn.doe@example.com35
2EthanLeeethan.lee@eample.com34
3SophiaNoahsophia.noah@example.com31
4JaneDoejane.doe@example.com33
5DavidRichardsdavid.richards@example.com36

Delete

To delete a row, you can use the "delete" function. (Please note that deleting just one field in a row is not supported; however, you can set it to "Null", which is allowed through the update function.)

Method:
const dbTables = await a8Astro.connect("a8cloud");
await dbTables.user_details.delete();
id_first_last_email_age_
1JohnDoejohn.doe@example.com35
3SophiaNoahsophia.noah@example.com31
4JaneDoejane.doe@example.com33
5DavidRichardsdavid.richards@example.com36

Transaction Function

A8Astro supports the concept of transactions, where a series of queries are executed together, ensuring atomicity. If one query fails, all changes made by prior queries within the same transaction are rolled back. The transaction function can operate across multiple tables.

To implement a transaction, the following operations are used:

Method:
db.begin();
db.commit();
db.rollback();
  • Begin: Starts a transaction and returns a transaction ID that can be used to target further queries under the same transaction.
  • Commit: Confirms and applies the changes made within the transaction.
  • Rollback: Reverses all changes made within the transaction, cancelling the transaction.

If a transaction has not been committed or rolled back, the database cannot indefinitely keep the transaction on hold; instead, it will automatically terminate the operation after a default timer expires.

Batch Operation

Batch operations allow multiple actions (create, update, delete) to be executed as a single request. By using the '.execute()' method, the entire query is executed at once, and the result is returned. Failures in any of the queries are handled automatically, eliminating the need for manual error handling by the coder.

Method:
const dbTables = await a8Astro.connect("a8cloud");
const batch = await dbTables.user_details.batchOperation();
// ...
batch.execute();
id_first_last_email_age_
1JohnDoejohn.doe@example.com35
3SophiaNoahsophia.noah@example.com32
5DavidMillerdavid.miller@example.com36
6JackJonesjack.jones@example.com38