About this tutorial
This tutorial describes a use case of the CSV Module. The detailed import and export routines of the CSV Module are described within the CSV Module documentation.
The scenario in this tutorial is an example on how to apply the CSV Module to a specific case.
Creating the backend
CSV import isn‘t only for importing plain data. You can also add files and images to the zip you’re going to import.
Let’s say you've created a backend which contains the BookModule with the Book class. Next to the default attributes createdAt,lastModifiedAt and foreignId you added the attributes cover of the type Image and summary of the type File.
Another class Category has the attribute name of the type String and a collection of the class Book with the name books.
Add the CSV Module and deploy your Application.
Create a custom CSV
Create a new CSV file which is named after following pattern: <MODULENAME>$<MODULEVERSION>$<CLASSNAME>.csv
1.) In our example, the module is called BooksModule and has the module version 1.0.0. So the csv file would be named like this: BookModule$1.0.0$Book.csv
Add five columns to the csv file as header: id;foreignId;cover;summary
Leave the id column empty, because that’s the ID generated by ApiOmat for each object of a class.
But still, we want a unique identifier for every object. That’s what the foreignId is for. Insert a value of your choice in that column.
Now we want to upload an image and a file via csv. To do this, write the location of the image, which it will have in the zip file in the cover column beginning with "images://".
In our example, our zip file contains a subfolder named „covers“ which contains all covers we want to upload. Our image is called „spring.jpg“, so the location looks like this: images://covers/spring.jpg
We also want to upload a summary as a pdf file. The pattern is similar the previous one, it's "files://". Our summary is located in the subfolder „summaries“ so we insert the location in the file column: files://summaries/spring.pdf
If you want to, you may create more objects of the class Book, by adding more lines to the CSV file
2.) Of course every Book belongs to a Category. First we have to create a category. To do so, create a new CSV file with the name: BookModule$1.0.0$Category.csv
The csv file should contain following columns as header: id;foreignId;books;name
You can leave the id and foreignId empty and give the category a name of your choice. To add all books to a category, write a map in the column following this pattern: [foreignId|foreignId]
So, add the two books by setting their foreignIds 1 and 2 to the books attribute like this: [1|2]
3.) Finally pack both CSV files and the folder to a ZIP-file
Import the custom CSV
Go to the Dashboard > Modules & UI > CSV
Choose the import ZIP file and click on Import.
That’s it. If you look into the data editor you will recognize that every object that was imported has an id.
Export data to CSV
Now we want to create some data within the dashboard and export it afterwards.
Go to the data editor and and select the class Book on the left side
add a new data object by clicking on the “+” in the data editor
You can edit at least all the attributes which you gave to the class, e.g. by uploading a cover and a summary to your new book entry
Next, switch to the Category class and change the name. Add the book you created before as third reference
Go to the Dashboard > Modules & UI > CSV
Export all your data by pressing the button
Modify the exported CSV
To modify the exported files unpack the downloaded ZIP-file from previous step. You will get the csv files and a folder with the name of your application containing all files (if exportResources was enabled before export).
Open the BookModule$1.0.0$Book.csv file.
As you can see, there is a column for every attribute of the class. In this example, delete all the covers and summaries and save the CSV file.
Now open the BookModule$1.0.0$Category.csv file.
Delete the ids from attribute books to clear references and save.
Reimport the CSV
Repack the edited CSV files to a new ZIP file.
Simulate a data loss by deleting all the objects from Book and Catagory via data editor.
Go to the Dashboard and import the csv file you just created. You will get a report how many data sets have been added.
Change to the data editor: the data you imported should have been updated to the modified state.