Learn how to build a simple note-taking app with ObjectBox.
This tutorial will walk you through a simple note-taking app explaining how to do basic operations with ObjectBox. To just integrate ObjectBox into your project, look at the Getting Started page.
You can check out the example code from GitHub. This allows you to run the code and explore it in its entirety. It is a simple app for taking notes where you can add new notes by typing in some text and delete notes by clicking on an existing note.
To store notes there is an entity class called Note (or Task in Python). It defines the structure or model of the data persisted (saved) in the database for a note: its id, the note text and the creation date.
@Entity()classTask:id=Id() text =String() date_created =Date(py_type=int) date_finished =Date(py_type=int)
In general, an ObjectBox entity is an annotated class persisted in the database with its properties. In order to extend the note or to create new entities, you simply modify or create new plain classes and annotate them with @Entity and @Id; in Python opt-in uid argument i.e. @Entity(uid=).
Go ahead and build the project, for example by using Build > Make project in Android Studio. This triggers ObjectBox to generate some classes, like MyObjectBox.java, and some other classes used by ObjectBox internally.
Go ahead and build the project, for example by using Build > Make project in Android Studio. This triggers ObjectBox to generate some classes, like MyObjectBox.kt, and some other classes used by ObjectBox internally.
Before running the app, run the ObjectBox code generator to create binding code for the entity classes: flutter pub run build_runner build
Also re-run this after changing the note class.
# Make sure to install objectbox >= 4.0.0
$ pip install --upgrade objectbox
$ ls example
ollama
tasks
vectorsearch-cities
$ cd tasks
$ python main.py
Welcome to the ObjectBox tasks-list app example. Type help or ? for a list of commands.
>
Inserting notes
To see how new notes are added to the database, take a look at the following code fragments. The Box provides database operations for Note objects. A Box is the main interaction with object data.
To query and display notes in a list a Query instance is built once:
NoteActivity.java
@OverridepublicvoidonCreate(Bundle savedInstanceState) {...// Query all notes, sorted a-z by their text. notesQuery =notesBox.query().order(Note_.text).build();...}
NoteActivity.kt
publicoverridefunonCreate(savedInstanceState: Bundle?) {...// Query all notes, sorted a-z by their text. notesQuery = notesBox.query {order(Note_.text) }...}
lib/objectbox.dart
Stream<List<Note>> getNotes() {// Query for all notes, sorted by their date.// https://docs.objectbox.io/queriesfinal builder = _noteBox.query().order(Note_.date, flags:Order.descending); ...
...// Build and watch the query,// set triggerImmediately to emit the query immediately on listen.return builder .watch(triggerImmediately:true)// Map it to a list of notes to be used by a StreamBuilder. .map((query) => query.find());}
example/tasks/main.py
deffind_tasks(self):return self._query.find()
You can also use self._task_box.get_all() to get all Task objects without any condition (instead of building a query).
In addition to a result sort order, you can add various conditions to filter the results, like equality or less/greater than, when building a query.
Updating notes and more
What is not shown in the example, is how to update an existing (== the ID is not 0) note. Do so by just modifying any of its properties and then put it again with the changed object:
note.setText("This note has changed.");notesBox.put(note);
note.text ="This note has changed."notesBox.put(note)
note.text ="This note has changed.";_noteBox.putAsync(note);
task.text ="This task has changed."self._task_box.put(task)
There are additional methods to put, find, query, count or remove entities. Check out the methods of the Box class in API docs (for Java/Kotlin or Dart) to learn more.
Setting up the database
Now that you saw ObjectBox in action, how did we get that database (or store) instance? Typically you should set up a BoxStore or Store once for the whole app. This example uses a helper class as recommended in the Getting Started guide.
Remember: ObjectBox is a NoSQL database on its own and thus NOT based on SQL or SQLite. That’s why you do not need to set up “CREATE TABLE” statements during initialization.
Note: it is perfectly fine to never close the database. That’s even recommended for most apps.