NARU / DOCS / SDK 1.0.0 / REFERENCE

SDK 레퍼런스

naru.js가 내보내는 모든 함수와 타입입니다.

createNaru

함수
createNaru(options?: { site?: string }): NaruClient

Creates a client bound to one site.

Admin

인터페이스
interface Admin
  • media: {
      upload(
        file: Blob | File,
        options?: {
          onProgress?: (
            progress:
              | { phase: "preparing" }
              | { loaded: number; phase: "uploading"; total: number }
              | { phase: "finishing" },
          ) => void;
          signal?: AbortSignal;
        },
      ): Promise<{
        contentType: string;
        name: string;
        size: number;
        url: string;
      }>;
    }
  • batch(
      writes: readonly (
        | {
            collection: string;
            set: { condition?: WriteCondition; data: unknown; id: string };
          }
        | {
            collection: string;
            delete: { condition?: { revision: Revision }; id: string };
          }
      )[],
      options?: { signal?: AbortSignal },
    ): Promise<
      ({
        createdAt: string;
        id: string;
        revision: Revision;
        updatedAt: string;
      } | null)[]
    >

    Commits every write or none. Conditions guard individual documents. Does not retry. Resolves, in the order written, with what each set stored and null for each delete.

  • collection<T = Json>(name: string): AdminCollection<T>
  • signOut(): Promise<void>

    This handle stops working at once, even if the revoke request fails.

AdminCollection

인터페이스
interface AdminCollection<T = Json> extends PublicCollection<T>
  • delete(
      id: string,
      options?: { condition?: { revision: Revision }; signal?: AbortSignal },
    ): Promise<void>

    Deleting a missing document succeeds unless a condition was supplied.

  • set(
      id: string,
      data: T,
      options?: { condition?: WriteCondition; signal?: AbortSignal },
    ): Promise<Document<T>>

    Replaces the whole document or creates it.

Document

인터페이스
interface Document<T = Json>

A read or successful write: user data stays separate from server metadata.

  • createdAt: string
  • data: T
  • id: string
  • revision: Revision
  • updatedAt: string

ListOptions

인터페이스
interface ListOptions
  • after?: string | null

    Opaque cursor returned by the preceding page.

  • filter?: Filter
  • includeTotal?: boolean
  • signal?: AbortSignal
  • size?: number

    Default 50; maximum 100.

  • sort?: Sort

NaruClient

인터페이스
interface NaruClient
  • auth: {
      session(): Promise<Admin | null>;
      signIn(options: { collections: readonly string[] }): Promise<void>;
    }
  • collection<T = Json>(name: string): PublicCollection<T>

    Visitor access, even when signed in.

Page

인터페이스
interface Page<T = Json>
  • documents: Document<T>[]
  • nextCursor: string | null
  • totalCount?: number

PublicCollection

인터페이스
interface PublicCollection<T = Json>
  • add(data: T, options?: { signal?: AbortSignal }): Promise<Document<T>>
  • count(options?: {
      filter?: Filter;
      signal?: AbortSignal;
    }): Promise<number>

    How many documents match filter, or the whole collection.

  • get(id: string, options?: { signal?: AbortSignal }): Promise<Document<T>>
  • list(options?: ListOptions): Promise<Page<T>>
  • pages(options?: ListOptions): AsyncIterable<Page<T>>

    Every page of a query in turn, following nextCursor from after (or the start). Not a snapshot: writes in between can shift later pages.

NaruError

클래스
class NaruError extends Error

A Naru operation that could not be completed. Check it with instanceof.

  • code: NaruErrorCode

Filter

타입
type Filter = Record<
  string,
  | string
  | number
  | boolean
  | null
  | {
      gt?: string | number;
      gte?: string | number;
      lt?: string | number;
      lte?: string | number;
    }
>

Top-level user fields, combined with AND.

Json

타입
type Json =
  | null
  | boolean
  | number
  | string
  | Json[]
  | { [key: string]: Json }

NaruErrorCode

타입
type NaruErrorCode =
  | "CONFLICT"
  | "QUOTA_EXCEEDED"
  | "AUTH_REQUIRED"
  | "ACCESS_DENIED"
  | "NOT_FOUND"
  | "RATE_LIMITED"
  | "INVALID_REQUEST"
  | "UNAVAILABLE"

Why an operation failed. The list is closed for v1: a code a later server adds reaches this version as the nearest code here.

Revision

타입
type Revision = string & { [revisionBrand]: true }

An opaque concurrency token. Store and return it unchanged.

Sort

타입
type Sort =
  | readonly [
      readonly [
        string | { metadata: "id" | "createdAt" | "updatedAt" },
        "asc" | "desc",
      ],
    ]
  | readonly [
      readonly [
        string | { metadata: "id" | "createdAt" | "updatedAt" },
        "asc" | "desc",
      ],
      readonly [
        string | { metadata: "id" | "createdAt" | "updatedAt" },
        "asc" | "desc",
      ],
    ]

One or two keys: a user field by name, or document metadata.

WriteCondition

타입
type WriteCondition = { revision: Revision } | { absent: true }

Write only at this revision, or only if the document does not exist yet.