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.)
- Method
- Usage
- Response
a8Astro.connect("your_connection_name"); // By default connects to the 'public' schema.
a8Astro.connect("your_connection_name", { dbSchema: "your_schema_id" }); // You specify the schema here.
// 'a8cloud' represents the connection's identifier name.
const dbTables = await a8Astro.connect("a8cloud");
// 'school' represents the schema name (within the 'a8cloud' database).
const dbTables = await a8Astro.connect("a8cloud", { dbSchema: "school" });
["user_details", "account", .....]
["student_details", "address", .....]
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_ |
---|---|---|---|---|
1 | John | Hamilton | john.hamilton@example.com | 35 |
Find
With the 'find' function, you can pull up a specific data from the table.
- Method
- Usage
- Response
const dbTables = await a8Astro.connect("a8cloud");
await dbTables.user_details.find();
// 'user_details' is the table name.
const dbTables = await a8Astro.connect("a8cloud")
await dbTables.user_details.find({
"select": {
"first_": true,
"last_": true,
"email_": true,
"id_": true,
"age_": false,
},
"where": {
"email_": "john.hamilton@example.com"
}
})
const dbTables = await a8Astro.connect("a8cloud")
await dbTables.user_details.find({
"select": {
"first_": true,
"last_": true,
"email_": true,
"id_": true,
"age_": false,
},
"where": {
"email_": "john@gmail.com" // Wrong Email ID.
}
})
{
"first_": "John",
"last_": "Deo"
"id_": 1,
"email_": "john.hamilton@example.com",
}
{
// Empty Output
}
Update
The 'update' function allows you to add data to an existing row.
- Method
- Usage
- Response
const dbTables = await a8Astro.connect("a8cloud");
await dbTables.user_details.update();
const dbTables = await a8Astro.connect("a8cloud")
await dbTables.user_details.update({
"where": {
"first_": "John"
},
"set": {
"last_": "Doe" // Changing Last Name
"email_": "john.doe@example.com" // Changing Email ID
}
})
noOfRowsAffected:1
id_ | first_ | last_ | email_ | age_ |
---|---|---|---|---|
1 | John | Doe | john.doe@example.com | 35 |
Create
The "create" function allows you to insert new values into the table. When using the "create" function, mandatory fields must be filled.
- Method
- Usage
- Response
const dbTables = await a8Astro.connect("a8cloud");
await dbTables.user_details.create();
const dbTables = await a8Astro.connect("a8cloud");
await dbTables.user_details.create({
"first_": "Ethan",
"last_": "Lee",
"email_": "ethan.lee@example.com",
"age_": "34",
})
noOfRowsAffected:1
id_ | first_ | last_ | email_ | age_ |
---|---|---|---|---|
1 | John | Doe | john.doe@example.com | 35 |
2 | Ethan | Lee | ethan.lee@eample.com | 34 |
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
- Usage
- Response
const dbTables = await a8Astro.connect("a8cloud");
await db.demoschema.user_details.createMany();
Adds two new rows to the table, one for Sophia Noah and another for Jane Doe, along with their email IDs and age.
const dbTables = await a8Astro.connect("a8cloud");
await dbTables.user_details.createMany([
{
"first_": "Sophia",
"last_": "Noah",
"email_": "sophia.noah@example.com",
"age_": "31",
},
{
"first_": "Jane",
"last_": "Doe",
"email_": "jane.doe@example.com",
"age_": "33",
},
{
"first_": "David",
"last_": "Richards",
"email_": "david.richards@example.com",
"age_": "36",
},
]);
noOfRowsAffected:2
id_ | first_ | last_ | email_ | age_ |
---|---|---|---|---|
1 | John | Doe | john.doe@example.com | 35 |
2 | Ethan | Lee | ethan.lee@eample.com | 34 |
3 | Sophia | Noah | sophia.noah@example.com | 31 |
4 | Jane | Doe | jane.doe@example.com | 33 |
5 | David | Richards | david.richards@example.com | 36 |
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
- Usage
- Response
const dbTables = await a8Astro.connect("a8cloud");
await dbTables.user_details.delete();
const dbTables = await a8Astro.connect("a8cloud");
await dbTables.user_details.delete({
"where": {
"email_": "ethan.lee@eample.com",
}
})
noOfRowsAffected:1
id_ | first_ | last_ | email_ | age_ |
---|---|---|---|---|
1 | John | Doe | john.doe@example.com | 35 |
3 | Sophia | Noah | sophia.noah@example.com | 31 |
4 | Jane | Doe | jane.doe@example.com | 33 |
5 | David | Richards | david.richards@example.com | 36 |
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.
- Method
- Usage
- Response
To implement a transaction, the following operations are used:
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.
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
- Usage
- Response
const dbTables = await a8Astro.connect("a8cloud");
const batch = await dbTables.user_details.batchOperation();
// ...
batch.execute();
The following operation executes a series of actions such as Create, Delete, Find, and Update within a Batch Operation.
const dbTables = await a8Astro.connect("a8cloud");
const batch = await dbTables.user_details.batchOperation();
batch.create({
"first_": "Jack",
"last_": "Jones",
"email_": "jack.jones@example.com",
"age_": "38",
});
batch.delete({
"where": {
"email_": "jane.doe@example.com",
},
});
batch.update({
"where": {
"email_": "sophia.noah@example.com",
},
"set": {
"age_": "32",
},
});
batch.update({
"where": {
"first_": "David",
},
"set": {
"last_": "Miller",
"email_": "david.miller@example.com",
},
});
batch.execute();
noOfRowsAffected:4
id_ | first_ | last_ | email_ | age_ |
---|---|---|---|---|
1 | John | Doe | john.doe@example.com | 35 |
3 | Sophia | Noah | sophia.noah@example.com | 32 |
5 | David | Miller | david.miller@example.com | 36 |
6 | Jack | Jones | jack.jones@example.com | 38 |