Skip to content
Database dbCreate

$dbCreate

$dbCreate is $dbSet that refuses to overwrite: when the document already exists, nothing changes and $dbLastError is conflict. It is atomic, so two people cannot both create the same document.

Syntax

$dbCreate[collection;id;json;(returnValue)]

Parameters

Parameter Description
collection Collection name: lowercase letters, digits, _, . or - (up to 64 characters). It is created on first write.
id Document id, 1 to 128 bytes. Often a Discord ID.
json The document: a JSON object.
returnValue Optional - yes to return the result, no to return nothing. Absent: a call written on its own prints nothing, a call used as a value ($var[r;$dbSet[...]]) returns it. Any other value raises an error.

Return value

Used on its own in a script, the function prints nothing, so no revision or JSON leaks into the message. Used as a value of another function, or with returnValue set to yes, it returns the result below.

  • The revision (1) when created.
  • Empty when it already existed ($dbLastError is conflict).

Examples

Register once

$dbCreate[players;$authorID;{"xp":0}]
$if[$dbLastError==conflict]
  You are already registered.
$endif

Try it

Paste an example into the playground to preview its message (functions that act on Discord are not run), or build the same thing with blocks.