Client-Side API

NukeBase's client library provides a real-time connection to your database through WebSockets. The client handles connection management, request tracking, and event dispatching automatically.

Looking for get/set/update/increment/remove/query? Those work the same on client and server and are documented once in CRUD Operations and Query Operations. The client returns Promises (use await); otherwise the API is identical.

Everything createClient() returns

MethodDoes
Data — see CRUD Operations
get(path)Read the value at a path
set(path, data, opts?)Create or replace. opts.durable waits for the disk flush
update(path, data, opts?)Deep-merge into the existing value
increment(path, data, opts?)Apply a numeric delta, or a nested object of deltas, atomically. Resolves with the resulting value(s)
remove(path, opts?)Delete the value at a path
query(msg)Search a collection. Takes childPath, orderBy, desc, limit, startAt, cursor, count — see Sorting and Pagination
Subscriptions — see Real-time Subscriptions. Each returns an unsubscribe function
getSub({event, path}, fn)Live value at a path
getSubChanged({event, path}, fn)Same, but deltas after the first payload, plus removed: the children that were deleted
querySub({…}, fn)Live query result, optionally windowed with orderBy/limit
querySubChanged({…}, fn)Same, but only the records each write touched, plus removed: the keys that left the result
Files — see File Storage
getFile(path, opts?)Download. Resolves with a Blob
setFile(path, body, opts?)Upload a Blob, File, ArrayBuffer, TypedArray or string
listFiles(path, opts?)One directory level as an array of entries
removeFile(path, opts?)Delete; directories need { recursive: true }
fileUrl(path)Synchronous. The URL for <img src> or <a href> — rules still apply when the browser fetches it
Server functions and auth
callableFunction(name, data)Invoke a server callable — see Calling Callables
login(username, password)Sign in, or resume a cookie session with no arguments. A sign-in while another account is active adds the new one — see Several accounts in one browser
logout(opts?)Sign out the active account; the next parked one takes over. Same as auth.logout(opts)
auth.current / auth.uidWho this browser is: { uid, email, username, claims } once the server has said (uid is null when signed out); null before the socket's first frame
auth.onAuth(fn)fn(current) now if known, then on every change of account. Returns a function that stops listening
auth.accounts()The accounts signed in on this browser, active first — never their tokens
auth.switch(uid)Make a parked account the active one and reconnect, in this tab and every other tab of the app
auth.logout(opts?){} the active account, { uid } one parked account, { all: true } every account on this browser
createUser(username, password)Create an account. Its endpoint ships commented out — accounts are created by the email flows instead, so this 404s unless you re-enable /createuser
changePassword(newPassword)Change the signed-in user's password
magicLink(email)Request a passwordless sign-in link
reconnect()Tear down and re-open the socket. Called automatically after a sign-in, a switch or a sign-out so the new cookies are picked up
onUpdate(fn)A newer build of a file this page loaded has been cached by the service worker — see the page cache. Returns a function that stops listening
close()Close the socket and stop reconnecting, for good. For tearing a client down
Offline mode — see Offline Mode. Present whether or not it is on
offline.enabledtrue when createClient was given { offline: { enabled: true } }
offline.status(){ enabled, online, coveredPaths, queued, lastSyncAt, clientId }
offline.onSync(fn)queued / sent / failed / drained events for the write queue. Returns a function that stops listening
offline.flush()Replay queued writes now, if online
offline.save()Write the cache to IndexedDB now
offline.clear()Empty the cache and the queue

Two different error conventions, on purpose. Data methods, subscriptions and callables resolve with { status: "Failed" } — they never reject on a refusal, so you must check status. The four file methods reject with a NukeBaseFileError carrying the HTTP .status, because a file transfer's failure modes (403, 404, 409, 413, 429) are exactly what you want to branch on. See Response Format.

Auth methods depend on extensions being mounted. login and changePassword need extensions/auth on the server; magicLink needs extensions/magic-link; createUser additionally needs its route uncommented. logout and everything under auth are backed by the core engine. A method whose endpoint isn't routed falls through to the static handler and gets a 404, which surfaces as a JSON parse error rather than a clear message. See Authentication.

↑