Skip to the page
Najova

Docs

/

Guides

dataNaja

dataNaja is a database built into Najova: write the shape of each table once, then save, find and change rows with nothing to install or start.

The shape of a table

A table's shape says what each row holds. Write it in a .dnaj file beside your script: one $shape[name] for each table, with a field on each line. A field is text, number, bool, list or table.

data.dnaj
$-- The shapes of the tables: one $shape for each.
$shape[orders]
  name = text
  loaves = number
  bread = text ; default sourdough
  email = text ; optional ; unique
$endshape
After the typeWhat it does
; optionalThe field can be left out
; uniqueNo two rows can have the same value
; default sourdoughThe value when it's left out
; was oldNameThe field had another name: saved rows move over to the new one

Saving rows

Put $use[datanaja] at the top of a script, then $dataAdd saves a row and gives back its id. Every row gets an id of its own, so leave it out of the shape. The rows are kept in a .najdata folder beside the .dnaj file.

orders.naja
$use[datanaja]
$let[id;$dataAdd[orders;$table[name;Sam;loaves;2]]]
$print[Saved as order $get[id]]
$dataAdd[orders;$table[name;Ada;loaves;"3";bread;rye]]
$print[$json[$dataFind[orders]]]

› naja run orders.naja

Saved as order 1
[{"id":1,"name":"Sam","loaves":2,"bread":"sourdough"},{"id":2,"name":"Ada","loaves":3,"bread":"rye"}]

"3" was saved as the number 3, because loaves is a number. Ada's order says rye, and Sam's got the default.

Finding rows

$dataFind gives a list of the rows that pass a test, like loaves >= 3, and every row with no test. sort=, order=down and limit= put them in order and keep only the first few. $dataGet gives the first row with a value, and $dataCount counts them.

find.naja
$use[datanaja]
$dataAdd[orders;$table[name;Sam;loaves;2]]
$dataAdd[orders;$table[name;Ada;loaves;5;bread;rye]]
$dataAdd[orders;$table[name;Lee;loaves;3]]

$each[o;$dataFind[orders;loaves >= 3;sort=loaves;order=down]]
  $print[$get[o.name]: $get[o.loaves] $get[o.bread]]
$endeach
$let[sam;$dataGet[orders;name;Sam]]
$print[Sam is order $get[sam.id]]
$print[$dataCount[orders;bread == rye] of $dataCount[orders] are rye]

› naja run find.naja

Ada: 5 rye
Lee: 3 sourdough
Sam is order 1
1 of 3 are rye

Changing and removing

$dataChange changes the rows that pass a test, and $dataRemove removes them. Both give back how many rows they touched. all as the test removes every row.

change.naja
$use[datanaja]
$dataAdd[orders;$table[name;Sam;loaves;2]]
$dataAdd[orders;$table[name;Ada;loaves;5]]

$print[Changed $dataChange[orders;name == Sam;$table[loaves;4]]]
$print[Removed $dataRemove[orders;loaves > 4]]
$print[$json[$dataFind[orders]]]

› naja run change.naja

Changed 1
Removed 1
[{"id":1,"name":"Sam","loaves":4,"bread":"sourdough"}]

Rows are checked

Each row is checked against its shape before it's saved. A value of the wrong type, a missing field or a copy of a unique value is a mistake, and nothing is saved. Catch it with $try to tell the person what to fix.

checks.naja
$use[datanaja]
$dataAdd[orders;$table[name;Sam;loaves;2;email;sam@example.com]]

$try
  $dataAdd[orders;$table[name;Ada;loaves;lots]]
$catch[why]
  $print[Not saved: $get[why]]
$endtry
$try
  $dataAdd[orders;$table[name;Sam again;loaves;1;email;sam@example.com]]
$catch[why]
  $print[Not saved: $get[why]]
$endtry

› naja run checks.naja

Not saved: $dataAdd: loaves: "lots" is not a number.
Not saved: $dataAdd: another row of orders already has email sam@example.com (it is unique).

Changing a shape

Add, remove or rename fields in the .dnaj file whenever you like. The next time the table is used, the saved rows are moved over to the new shape, and a copy of the old file is kept first. A new field needs ; optional or a ; default, so the rows already saved can have it.

With a web page

dataNaja works the same in a webNaja page. This guestbook saves each note from its form and shows the newest ten. Changes are made one at a time, so two visitors saving at once can't spoil each other's rows.

index.wnaj
$use[datanaja]
$if[$request[method] == "POST"]
  $dataAdd[notes;$table[name;$request[form.name];words;$request[form.words]]]
  $redirect[/]
$endif

$page[Guestbook]
  $each[n;$dataFind[notes;sort=id;order=down;limit=10]]
    $paragraph[**$get[n.name]:** $get[n.words]]
  $endeach
  $form[method=post]
    $input[name;Your name;required=yes]
    $input[words;Your note;type=textarea;required=yes]
    $button[Sign;look=primary]
  $endform
$endpage

› naja serve, then open /, /

POST /  →  302
Location: /
Moved to /

GET /  →  200
<main class="nj-page" id="page">
<p class="nj-paragraph"><strong>Sam:</strong> Lovely rye</p>
<form method="post" class="nj-form">
<label class="nj-field"><span>Your name</span><input type="text" name="name" required class="nj-input" data-nj="name" value=""></label>
<label class="nj-field"><span>Your note</span><textarea name="words" required class="nj-input" data-nj="words"></textarea></label>
<button type="submit" class="nj-button primary">Sign</button>
</form>
</main>