Options
All
  • Public
  • Public/Protected
  • All
Menu

@egfn/doc-sync

Index

Type aliases

DocID

DocID: string

Path

Path: string[]

SpecFunction

SpecFunction<T>: function

Data specifications are functions that return a ValidationSpec

Type parameters

  • T

Type declaration

Validation

Validation<T>: function

A Validation is a function that takes a validation library and a document's content as input, and throws a couchDB validation error object if the validation fails. Example:

interface Post {
  body: string;
  reply: Post;
}

function PostSpec(): DS.ValidationSpec<Post> {
  return {
    type: "post",
    schema: {
      body: {
        required: true,
        validations: [
          (lib, body) =>
            lib.failIf(
              body.length > 10,
              "Post must be at most 10 characters"
            )
        ]
      },
      reply: {
        spec: PostSpec
      }
    }
  };
}

NOTE: Validations cannot close over variables outside the function's scope as all scope information is lost after function serialization.

Type parameters

  • T

Type declaration

ValidationSchema

ValidationSchema<T>: object

A ValidationSchema Determines how record fields should validate its associated data type.

Type parameters

  • T

Type declaration

Functions

Private createValidator

defineOnly

  • defineOnly(db: any, ...specs: SpecFunction<any>[]): Promise<void>
  • Given a PouchDB database object and a list of SpecFunction elements, calls defineSpec on each SpecFunction and then inserts an update validation design document into the database that blocks any other document type to be inserted into the database. The following example with throw a validation error.

    defineOnly(db, PostSpec)
    
    await db.put({
      _id: "invalid-document",
      path: [],
      type: "non-existent"
    })

    Parameters

    Returns Promise<void>

defineSpec

  • defineSpec<T>(db: Database, spec: SpecFunction<T>): Promise<void>
  • Given a PouchDB database object and a SpecFunction, defineSpec will insert an update validation design document that runs specified validation functions on each inserted document for defined specification type name. NOTE: if the type field of the document does not match the specified type name, the validation does not fail. If you want to only allow a specific list of document types see defineOnly.

    Type parameters

    • T

    Parameters

    Returns Promise<void>

getDBName

  • getDBName(db: Database): Promise<string>
  • Returns the database name of the given PouchDB database. Throws an error if the database is not reachable.

    Parameters

    • db: Database

    Returns Promise<string>

Private getMany

getPathSelector

  • getPathSelector<T>(db: Database, spec: SpecFunction<T>, path: Path): Promise<Selector>

getValidationDocID

getView

  • getView<T>(spec: SpecFunction<T>, include_docs?: boolean): string

getViewDocID

setupPlugins

  • setupPlugins(PouchDB: any): void
  • Adds all required pouchdb plugins used by this library to the locally imported PouchDB module. Example:

    var PouchDB = require("pouchdb");
    
    setupPlugins(PouchDB);

    Parameters

    • PouchDB: any

    Returns void

useDocHandle

usePathHandle

useRoot

  • Creates a PathHandle to work with documents at the root path (empty array). Example:

    const root = DS.useRoot(db, PostSpec);
    
    await root
      .then(_ => _.create())
      .then(_ => _.path.reply())
      .then(_ => _.create());
    
    const list = await root.then(_ => _.list());
    
    const replyList = await root
      .then(_ => _.find())
      .then(_ => _ && _.path.reply())
      .then(_ => _ && _.list());

    Type parameters

    • T

    Parameters

    Returns Promise<PathHandle<T>>

Generated using TypeDoc