Wisp is in pre-release, self-hosted since GitHub suspended us. Install it, or see the changelog.

Wisp

Where Rows Are Kept

Storage

A change is saved before memory changes. Each table has a log file in WISP_DATA (note.log, a line per change), read at first use and rewritten at twice its rows. One process uses a data folder: the first holds a lock on .lock, and a second process's tables answer 500 ("another process uses the data folder"). Give each replica its own folder, or use a database store. Table names differing only in case are refused at load.

SettingMeaning
WISP_DATAData folder: .wisp/data in dev, data in release, /data in the Docker image.
WISP_DATA=offMemory (tests, edge).
WISP_FSYNCsecond (default), always, off.

Tables hold every row in memory. For larger data or database-side queries, write the handlers.

Load never panics. A torn last line is dropped, a corrupt middle line is skipped with one warning, and a row that no longer reads as its type is skipped with a warning naming it (its id stays taken and its line stays, to fix or migrate). The load skips a row that is not UTF-8 with one warning that names the table, the count and the lowest and highest id. Its id stays taken, and the log keeps its line byte for byte, also after a compaction. A read of the store again (wisp::sign_out_everywhere with wisp::store) and a poll (WISP_STORE_POLL) skip a row that does not read in the same way. A poll applies the other rows. After such a skip, a read gives the old value of that row, or none when the table never read one (after a load). A write from this instance to that row is a 409 with the code conflict, and the store keeps the newer row. This holds for set, update, remove and a REST PUT, PATCH or DELETE. A clear of a table with a skipped row is a 409 and takes no row out. When the table kept the old value of a skipped row, some steps can come before the 409. In set and update, they include a 422, a 500 for a Password not hashed and the closure of update. In REST, they include a 422, a 413 and a 412. They also include the 409 of a row that changed during the update, a before_update or before_delete hook and the hash of a Password. The write saves nothing. In REST, a skipped row outside the view of the caller is a 404. On a route with a filter, a row that the load skipped is a 404 for every caller, the owner too. The write works again when a later load, read of the store again or poll reads the row. A read of the store again also takes the last id given from the store, as the load does. The log names each such row once, and at most 32 rows of a table, with no content of the row.

Any Database

Call wisp::store(Db) in init. Implement wisp::Store:

  • load(&self, table) -> Result<Vec<(u64, String)>>: id and JSON of each row.
  • save(&self, table, id, json: Option<&str>) -> Result: None deletes.

For example, over Mutex<rusqlite::Connection> (create table if not exists {table} (id integer primary key, json text), insert or replace, delete).

  • Calls are blocking, one per change, under the table lock.
  • A POST of an array calls save_many(table, &[(id, json)]). The default saves each row in turn and undoes earlier ones on failure; override it with one transaction. A failure is a 500, and the table in memory keeps none of the rows. If a delete of that undo fails too, the store may keep those rows. The built-in file store writes the batch behind one mark, so a crash part way through replays none of it. A table name is letters, digits, _ and -, and never a Windows device name (con, nul, aux, prn, com1-com9, lpt1-lpt9), on every OS.
  • On the edge each instance has its own memory, so tables are caches. Use D1/KV through wisp::edge::fetch, or env WISP_STORE=d1:DB|libsql://… (Saved tables: ids unique, a stale write gets a 409, 1 query per dynamic request).

A Page's Own Table

static TODOS: Table<Todo> = Table::saved(); (named from the static, or Table::saved("todos")).

  • A field added to a saved type must be Option, Vec or bool.
  • An Image field is a data: URL.

Paging

POSTS.page(cx, 10) is the rows ?page=N asks for, newest first. It reads as its rows and has number, prev, next (hrefs, None at the ends), total and pages() (at least 1):

{#each posts as post}<p>${post.title}</p>{/each}
{#if let Some(href) = posts.next}<a ${href}>Older</a>{/if}

{@pager posts} writes both links. POSTS.page_where(cx, 10, |p| p.title.contains(&q)) pages the rows f is true for (total counts the matches). Page::of(cx, 10, rows) pages rows you sorted yourself. Search and sort links with no script: With JavaScript off. TABLE.add_unless(|t| t.slug == slug, value) keeps value only if no row is taken (checked while locked), returns Ok(None) if one is, and errs as add does.

Is This Page Useful?

View This Page's Source

Type to search every docs page and heading.