Signals API
Signals are the core data primitive in StartupJS. Every piece of data -- whether it lives in the database or only in the browser -- is accessed through signals. This chapter covers all the methods available on signals.
What is a signal?
A signal is a reactive pointer to a location in your data tree. It does not hold data directly. Instead, it knows the path to the data and provides methods to read, write, and delete it.
Signals are created by navigating from the root signal $:
By convention, signal variables start with $ to distinguish them from plain values.
Reading data: .get()
Use .get() to read the current value of a signal:
.get() is synchronous. It returns whatever value is currently available locally.
Writing data: .set()
Use .set() to update a signal's value:
For database signals (public collections), .set() is asynchronous -- it returns a promise. The change is applied locally right away, then synced to the server in the background:
In most cases you do not need to await it. The local UI updates instantly.
Deleting data: .del()
Use .del() to remove a value:
Adding documents: .add()
Use .add() on a collection signal to create a new document with an auto-generated ID:
Updating multiple fields: .assign()
Use .assign() to set several fields at once:
This is equivalent to calling .set() on each field individually. Fields set to null or undefined are deleted.
Array operations
Signals pointing to arrays support push and pop:
Incrementing numbers: .increment()
For numeric values, use .increment() to add to the current value:
Navigating signals
You navigate deeper into the data tree using dot notation or bracket notation:
For private collections, the path starts with _:
Collection types
StartupJS has two types of collections:
Public collections
Public collections are stored in the database and synced across all clients. They use lowercase names:
You must subscribe before reading from them (using useSub in React or sub outside React).
Private collections
Private collections exist only on the current client. They start with _:
You do not need to subscribe to private collections. Read and write them directly.