NukeBase

Complete Client NukeBase SDK with createClient()

Here's a complete example using the new modular SDK:

Complete client implementation
<script type="module">
  import createClient from './sdkmod.js';

  // Destructure all the methods you need
  const { set, get, update, increment, query, callableFunction,
          getSub, querySub, getSubChanged, querySubChanged,
          setFile, getFile, listFiles, removeFile, fileUrl } = await createClient();

  console.log('✅ Connected to NukeBase');

  // Set data
  await set(["users", "matt"], {
    name: "Matt",
    color: "red",
    count: 0
  });

  // Get data
  const sessions = await get(["sessions"]);
  console.log('Sessions:', sessions.data);

  // Update data
  await update(["users", "matt"], {
    leadsSent: "Pending"
  });

  await update(["users", "matt", "count"], 5);

  // Query data
  const results = await query({
    path: ["sessions"],
    query: "child.count > 0"
  });
  console.log('Query results:', results.data);

  // Atomic counter — no read-modify-write race, and the new value comes back
  const bumped = await increment(["users", "matt", "count"], 1);
  console.log('New count:', bumped.data);

  // Sorted, paged query. Read results through .order — object key order
  // is not something to depend on.
  const top = await query({
    path: ["sessions"],
    query: "child.count > 0",
    orderBy: ["count"],
    desc: true,
    limit: 10
  });
  top.order.forEach(k => console.log(k, top.data[k].count));

  // Upload a file, then link to it
  const picker = document.querySelector('input[type=file]');
  if (picker?.files[0]) {
    try {
      await setFile(["users", "matt", "avatar.png"], picker.files[0], {
        onProgress: p => console.log(Math.round(p * 100) + '%')
      });
      document.querySelector('img').src = fileUrl(["users", "matt", "avatar.png"]);
    } catch (err) {
      // File methods THROW (unlike the data methods above, which resolve
      // with { status: "Failed" }). Branch on the HTTP status.
      console.error('Upload failed:', err.status, err.body);
    }
  }

  // Call a server callable
  const functionResult = await callableFunction("custom1", 23);
  console.log('Function result:', functionResult);

  // Subscribe to changes
  const unsubscribe1 = getSub({
    event: "value@",
    path: ["sessions"]
  }, data => {
    console.log('Sessions updated:', data);
  });

  // Query subscription
  const unsubscribe2 = querySub({
    event: "value@",
    path: ["sessions"],
    query: "child.count == 4"
  }, data => {
    console.log('Matching sessions:', data);
  });

  // Changed-only subscription
  const unsubscribe3 = getSubChanged({
    event: "value@",
    path: ["sessions"]
  }, data => {
    console.log('Changed sessions:', data);
  });

  // Query changed subscription
  const unsubscribe4 = querySubChanged({
    event: "value@",
    path: ["sessions"],
    query: "child.count != 4"
  }, data => {
    console.log('Changed query results:', data);
  });

  // Later, to unsubscribe:
  // unsubscribe1();
  // unsubscribe2();
  // unsubscribe3();
  // unsubscribe4();

</script>