June 14, 2024
If you use Google Apps Script, you are surely as tired as I am of getting data from Google Sheets in the form of multidimensional arrays, running endless loops to identify the row where the element is inserted to modify it without risking compromising the values of other rows.
For this reason, together with Sintropia I created ORMA an ORM for Google Apps Script aimed at being simple and straightforward.
There are some key points to know before deciding if Orma is right for you. Let's take a look at them right away:
So, if you use Google Sheets as a database and Google Apps Script to create, modify, and delete data, Orma is the tool for you.
β
The complete documentation is available at this link; below we will only cover some of the fundamental steps for using the library in Google Apps Script.
β
Access the Google Apps Script file where you want to install Orma and enter the following Script Id as a new library.
1CA-kvfiZjY2Ex2C84hL46Vn2F8GijPbfSy9J3vZQUzzbal5ExhQyG0Z7
β
Below are just some of the methods available for retrieving data from your Google Sheets. To view all the other methods, please refer to the documentation available here.
//Collegarsi al File Spreadsheet
const ssId = "your_spreadsheet_id";
const db = ORMA.openDbById(ssId);
//Ottenere gli Sheets disponibili
const tables = db._getTables();
const {users} = tables;
//Ottenere tutti i record del foglio
const allUsers = users._all();
//Ottenere un record cercandolo per id
const userById = users._find(1);
//Ottenere un record cercandolo secondo la corrispondenza del valore di una colonna
const firstUserBy = users._firstBy("name","Matteo");
//Ottenere un record secondo la corrispondenza di una query
const firstUserByQuery = users._firstByQuery(`{}.email !== "matteo@sintropia.io"`)
β
Instead of getting the data in a two-dimensional array as Google Apps Script usually returns, Orma makes our life much easier by allowing us to write cleaner code and returns the values as an object.
For example, this would be the output of the variable userById
initialized in the box above:
β
{
id: 1,
name: Matteo,
password: 5994471abb01112afcc18159f6cc74b4f511b99806da59b3caf5a9c173cacfc5,
email: matteo@sintropia.io,
_delete: function () { [native code] },
_save: function () { [native code] }
}
As you may have noticed, in addition to the data present in the database, the object contains functions necessary for modifying and deleting the data. In the following paragraphs, we will see how to use them.
β
Orma uses a modern syntax that is now the standard for major ORM tools worldwide, but with the limitations imposed by Google Apps Script. Below we will see an example of how simple and intuitive it is to modify data in the database.
//Get the record to edit
const userToEdit = users._find(1);
//Edit the record
userToEdit.name = "Luca";
//Push the new values to the database
userToEdit._save();
To make bulk modifications, refer to the official documentation at this link.
β
Deleting data in Orma is very simple, as you will see in the example below. You just need to retrieve the record or records to be deleted and then invoke the necessary function.
//Get the record to delete
const user = users._find(1);
//Delete the record
user._delete();
To delete items in bulk refer to the official documentation at this link.
β
Of course, by manipulating data in the form of objects, Orma also expects that data insertion is done in the form of objects. Here's an example of how to insert new records into the database.
const newUser = users._create(
{
id: users._id(), //This function generate an incremental id
name: "Matteo",
email: "matteo@sintropia.io",
password: ENDO.hash("superPassword1!") //this function will encrypt the password
}
)
To create items in bulk refer to the official documentation at this link.
If you're not yet convinced to try Orma in your projects, I'll give you the reasons that drove us to develop Orma and why I use it daily with my clients.
β
I would be really happy to receive any feedback on the use of the library. For any ideas, advice, or feedback, feel free to write to me at matteo@sintropia.io
β