Files
A StarlingDB database is one file. Your app reads and writes the file directly. There is no server.
Open a database
Section titled “Open a database”You can keep a database in a file or in memory.
// A file in the browser, shared by every tab of your app.// Works only in a dedicated worker.const db = await open<{ todos: Todo }>("app.starling", "laptop");
// In memory. Works anywhere, and is lost when the page closes.const scratch = await openInMemory<{ todos: Todo }>("laptop");open keeps the database in a file that the browser stores for your site. Use it for data
that must last.
- Call it in a dedicated worker. Browsers do not allow this kind of file access anywhere else.
- All tabs of your app share the file. Only one tab can use the file at a time, so the tabs take turns. This is why every call returns a promise.
openInMemory keeps the database in memory. The data is lost when the page closes. It works
without a worker. Use it for tests.
// A file on disk, locked until you close the database.let mut laptop = Db::open(&path, "laptop")?;
// In memory. Lost when the value is dropped.let mut phone = Db::in_memory("phone")?;Db::open keeps the database in a file on disk. Use it for data that must last.
- If the file does not exist, it is created.
- The file stays locked until you close or drop the database. A second attempt to open the same file fails.
Db::in_memory keeps the database in memory. The data is lost when you drop the database.
Use it for tests.
When a write is safe
Section titled “When a write is safe”A commit saves all of its changes or none of them. How safe a commit is depends on what goes wrong:
| What happens | What you keep |
|---|---|
| Your app crashes | Every commit that finished. |
| The operating system crashes, or the power fails | Every commit except, possibly, the most recent one. |
The most recent commit becomes safe from a power failure when the next commit finishes, or when you close the database.
Copy a database
Section titled “Copy a database”You can get the whole database as bytes, and open a database in memory from those bytes. Use this to export, import or back up data.
const bytes = await db.toBytes(); // the whole database fileconst copy = await openInMemory<{ todos: Todo }>("laptop", bytes);let bytes: Vec<u8> = laptop.to_bytes()?; // the whole database filelet copy = Db::from_bytes(bytes, "laptop")?;Check a file for damage
Section titled “Check a file for damage”StarlingDB stores a checksum with everything in the file. An open database checks each part of
the file the first time it reads that part. To check the whole file at once, call verify.
await db.verify(); // rejects with the location of the damage, if there is anyawait db.close();laptop.verify()?; // returns an error with the location of the damage, if there is anylaptop.close()?;You can also open a file in the editor to see its contents and verify it.
The file format can change
Section titled “The file format can change”StarlingDB is in alpha. A new version might not be able to read a file that an older version wrote, and there is no tool to convert files yet.