Skip to main content

A8Astro

The A8Astro SDK provides developers with tools to interact with Astro. Unlike other A8Studio components such as A8Functions, A8Data, A8Config, and Variablegroup, Astro is not created, managed, or deployed within A8Studio.

Connecting to the Database from A8Studio

a8Astro.connect

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

Method:
a8Astro.connect();

Connects to the database with the identifier name "a8cloud".

Usage:
// 'a8cloud' represents the identifier name for the connection.
const db = a8Astro.connect("a8cloud");

Data Manipulation and Querying

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

find

You can retrieve data from a database with the find function.

Method:
db.demoschema.user_details.find();
// db.user_details.find();
// If a schema is not specified, the search will default to the "Public" schema.

Returns the first 100 records from the table in batches of 100.

Usage:
// 'db' contains a8Astro connection.
// 'demoschema' refers to the schema.
// 'user_details' represents the table name.
db.demoschema.user_details.find({
data: {
take: 100,
skip: 0,
},
});

Update

The "update" function allows you to update an existing row.

Method:
db.demoschema.user_details.update();

Updates the "age" of the person with the "name: John" in the database.

Usage:
db.user_details.update({
where: {
name: "John",
},
Set: {
age: 18,
},
});

Create

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

Method:
db.demoschema.user_details.create();

Adds a new row for John Doe with his email ID.

Usage:
db.user_details.create({
first_: "John",
last_: "Doe",
email_: "john.doe@example.com"
});

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:
db.demoschema.user_details.createMany();

Adds two new rows to the table, one for John Doe and another for Jane Doe, along with their email IDs.

Usage:
db.user_details.createMany([
{
first_: "John",
last_: "Doe",
email_: "john.doe@example.com",
},
{
first_: "Jane",
last_: "Doe",
email_: "jane.doe@example.com",
},
]);

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:
db.demoschema.user_details.delete();

Deletes the row associated with the email ID john.doe@example.com.

Usage:
db.user_details.delete({
where: {
email_: "john.doe@example.com",
},
});

Transaction Function

Astro 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.
Usage:
try {
const updateAccountBalance = await db.bank_account.update(
{
where: {
account_number: accountNumber,
},
set: {
balance: `${totalBalance}`,
},
},
{
begin: true,
}
);

console.log(updateAccountBalance);

await db.transactions.create(
{
account_number: accountNumber,
transaction_amount: `+${depositAmount}`,
transaction_date: "now()",
},
{
id: updateAccountBalance.transaction.getId(),
}
);

const transactionId = updateAccountBalance.transaction.getId();

// To commit
updateAccountBalance.transaction.commit();
} catch (e) {
// To rollback
updateAccountBalance.transaction.rollback();
}

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 batch = db.demoschema.user_details.batchOperation();
// ...
batch.execute();

The following operation executes a series of actions such as Create, Delete, Find, and Update within a Batch Operation.

Usage:
const batch = db.user_details.batchOperation();

batch.create({
data: {
first_name: "jack",
last_name: "J",
email: "jack@gmail.com",
address: "121 main st",
phone: "555-555-1213",
},
});

batch.delete({
where: {
email: "jane@gmail.com",
},
});

batch.find({
where: {
last_name: "Doe",
},
limit: 10,
});

batch.update({
where: {
email: "john@gmail.com",
},
data: {
phone: "555-555-1234",
},
});

batch.execute();