Staquia

Visit Source Code Visit Source Code APIAPI

Reorder anything. Save one row.

Staquia gives every item of a list a short position string. Sort by it and you get the order back. Move an item and only that item changes, its neighbors are never touched.

Try it: drag the items, add new ones, and compare the rows each approach would write.

  • Milk 🥛 mmmm!
  • Lettuce 🥬 mmmn!
  • Meat 🥩 mmmo!
  • Banana 🍌 mmmp!
  • Choco 🍫 mmmq!
  • Apple 🍎 mmmr!
  • Candy 🍬 mmms!
  • Aubergine 🍆 mmmt!
  • Watermelon 🍉 mmmu!
  • Meat Bone 🍖 mmmv!
Rows written to the database
StaquiaInteger index
Last change00
Total00

Drag an item or add a new one.

The text at the bottom of each item is its position. Sort the positions as plain strings and you get exactly the order you see. With an integer index column (0, 1, 2…) every item between the old and the new place must be rewritten, Staquia only rewrites the moved one.

Why not floats, gaps like 1000, 2000… or another library? See Why Staquia. Storing it? See the Database chapter.

Install

bash
# Install it with npm (or pnpm, yarn...)
npm install staquia@0.6.0
bash
# Add the crate to Cargo.toml
cargo add staquia@0.2.0

The library has zero dependencies and is shipped as an ES module. In the browser you can also import it directly from a CDN:

html
<script type="module">
  import Staquia, { LexicalList } from "https://cdn.jsdelivr.net/npm/staquia@0.6.0/+esm";
</script>

The crate has zero dependencies and no unsafe code. It creates the same positions as the JavaScript library, so a Rust server and a JavaScript app can share the same rows.

Quick start

Give each item a position and let a LexicalList keep them in sync (your type implements the Item trait):

javascript
import Staquia, { LexicalList } from "staquia";

// Plain ASCII positions, ready for production (other presets in the Setup chapter)
const system = Staquia.ascii();
const list = new LexicalList(system);

// Any object with an "id" and a "position" field
const milk = { id: 1, text: "Milk", position: "" };
const meat = { id: 2, text: "Meat", position: "" };
const bread = { id: 3, text: "Bread", position: "" };
list.push(milk);
list.push(meat);
list.push(bread);

// Move the bread between the milk and the meat
const moved = list.move(3, 1, 2);
if (moved.ok) {
  // Only bread.position changed, save only that item
}
rust
use staquia::{Item, LexicalList, Position, Staquia};

// Any type with an id and a position field
struct Product {
    id: u32,
    text: String,
    position: String,
}

impl Item for Product {
    type Id = u32;
    fn id(&self) -> &u32 { &self.id }
    fn position(&self) -> &str { &self.position }
    fn set_position(&mut self, position: &Position) { self.position = position.to_string(); }
}

fn main() -> Result<(), staquia::Error<u32>> {
    // Plain ASCII positions, ready for production (other presets in the Setup chapter)
    let system = Staquia::ascii();
    let mut list = LexicalList::new(system);

    list.push(Product { id: 1, text: "Milk".into(), position: String::new() })?;
    list.push(Product { id: 2, text: "Meat".into(), position: String::new() })?;
    list.push(Product { id: 3, text: "Bread".into(), position: String::new() })?;

    // Move the bread between the milk and the meat
    let moved = list.move_to(&3, Some(&1), Some(&2))?;
    // Only the bread's position changed, save only that item
    println!("{}", moved.value().position);
    Ok(())
}

To understand how it works, continue with the Introduction chapter or open the Docs from here. Tune the number system for your app with the Setup App, and for every class and function check the APIAPI reference.