Skip to main content

Class: Replicache<MD>#

Type parameters#

NameTypeDefault
MDMutatorDefs{}

Implements#

Constructors#

constructor#

+ new Replicache<MD>(options?: ReplicacheOptions<MD>): Replicache<MD>

Type parameters:#

NameTypeDefault
MDMutatorDefs{}

Parameters:#

NameTypeDefault value
optionsReplicacheOptions<MD>{}

Returns: Replicache<MD>

Defined in: replicache.ts:315

Properties#

getPullAuth#

getPullAuth: undefined | null | () => MaybePromise<undefined | null | string>= null

This gets called when we get an HTTP unauthorized (410) response from the pull endpoint. Set this to a function that will ask your user to reauthenticate.

Defined in: replicache.ts:303


getPushAuth#

getPushAuth: undefined | null | () => MaybePromise<undefined | null | string>= null

This gets called when we get an HTTP unauthorized (410) response from the push endpoint. Set this to a function that will ask your user to reauthenticate.

Defined in: replicache.ts:312


mutate#

Readonly mutate: MakeMutators<MD>

The mutators that was registered in the constructor.

Defined in: replicache.ts:254


onSync#

onSync: null | (syncing: boolean) => void= null

onSync is called when a sync begins, and again when the sync ends. The parameter syncing is set to true when onSync is called at the beginning of a sync, and false when it is called at the end of a sync.

This can be used in a React like app by doing something like the following:

const [syncing, setSyncing] = useState(false);
useEffect(() => {
rep.onSync = setSyncing;
}, [rep]);

Defined in: replicache.ts:296


pullInterval#

pullInterval: null | number

The duration between each periodic pull. Setting this to null disables periodic pull completely. Pull will still happen if you call pull manually.

Defined in: replicache.ts:274


pushDelay#

pushDelay: number

The delay between when a change is made to Replicache and when Replicache attempts to push that change.

Defined in: replicache.ts:280

Accessors#

clientID#

• get clientID(): Promise<string>

The client ID for this instance of Replicache. Each web browser and instance of Replicache gets a unique client ID keyed by the name. This is persisted locally between sessions (unless useMemstore is true in which case it is reset every time a new Replicache instance is created).

Returns: Promise<string>

Defined in: replicache.ts:396


closed#

• get closed(): boolean

Whether the Replicache database has been closed. Once Replicache has been closed it no longer syncs and you can no longer read or write data out of it. After it has been closed it is pretty much useless and should not be used any more.

Returns: boolean

Defined in: replicache.ts:415


online#

• get online(): boolean

A rough heuristic for whether the client is currently online. Note that there is no way to know for certain whether a client is online - the next request can always fail. This is true if the last sync attempt succeeded, and false otherwise.

Returns: boolean

Defined in: replicache.ts:405

Methods#

close#

close(): Promise<void>

Closes this Replicache instance.

When closed all subscriptions end and no more read or writes are allowed.

Returns: Promise<void>

Defined in: replicache.ts:424


createIndex#

createIndex(def: CreateIndexDefinition): Promise<void>

Creates a persistent secondary index in Replicache which can be used with scan.

If the named index already exists with the same definition this returns success immediately. If the named index already exists, but with a different definition an error is thrown.

Parameters:#

NameType
defCreateIndexDefinition

Returns: Promise<void>

Defined in: replicache.ts:567


dropIndex#

dropIndex(name: string): Promise<void>

Drops an index previously created with createIndex.

Parameters:#

NameType
namestring

Returns: Promise<void>

Defined in: replicache.ts:574


get#

get(key: string): Promise<undefined | JSONValue>

Get a single value from the database.

Parameters:#

NameType
keystring

Returns: Promise<undefined | JSONValue>

Implementation of: ReadTransaction

Defined in: replicache.ts:502


has#

has(key: string): Promise<boolean>

Determines if a single key is present in the database.

Parameters:#

NameType
keystring

Returns: Promise<boolean>

Implementation of: ReadTransaction

Defined in: replicache.ts:507


isEmpty#

isEmpty(): Promise<boolean>

Whether the database is empty.

Returns: Promise<boolean>

Implementation of: ReadTransaction

Defined in: replicache.ts:512


pull#

pull(): void

Pull pulls changes from the pullURL. If there are any changes local changes will get replayed on top of the new server state.

Returns: void

Defined in: replicache.ts:765


query#

query<R>(body: (tx: ReadTransaction) => R | Promise<R>): Promise<R>

Query is used for read transactions. It is recommended to use transactions to ensure you get a consistent view across multiple calls to get, has and scan.

Type parameters:#

Name
R

Parameters:#

NameType
body(tx: ReadTransaction) => R | Promise<R>

Returns: Promise<R>

Defined in: replicache.ts:910


register#

register<Return>(name: string, mutatorImpl: (tx: WriteTransaction) => MaybePromise<Return>): function

deprecated Use ReplicacheOptions.mutators instead.

Type parameters:#

NameType
Returnvoid | JSONValue

Parameters:#

NameType
namestring
mutatorImpl(tx: WriteTransaction) => MaybePromise<Return>

Returns: () => Promise<Return>

Defined in: replicache.ts:922

register<Return, Args>(name: string, mutatorImpl: (tx: WriteTransaction, args: Args) => MaybePromise<Return>): function

deprecated Use ReplicacheOptions.mutators instead.

Type parameters:#

NameType
Returnvoid | JSONValue
ArgsJSONValue

Parameters:#

NameType
namestring
mutatorImpl(tx: WriteTransaction, args: Args) => MaybePromise<Return>

Returns: (args: Args) => Promise<Return>

Defined in: replicache.ts:927


scan#

scan<O, K>(options?: O): ScanResult<K>

Gets many values from the database. This returns a ScanResult which implements AsyncIterable. It also has methods to iterate over the keys and entries.

If options has an indexName, then this does a scan over an index with that name. A scan over an index uses a tuple for the key consisting of [secondary: string, primary: string].

Type parameters:#

NameType
OScanOptions
KScanOptionIndexedStartKey

Parameters:#

NameType
options?O

Returns: ScanResult<K>

Implementation of: ReadTransaction

Defined in: replicache.ts:525


scanAll#

scanAll<O, K>(options?: O): Promise<[K, JSONValue][]>

Convenience form of scan() which returns all the entries as an array.

deprecated Use scan().entries().toArray() instead.

Type parameters:#

NameType
OScanOptions
KScanOptionIndexedStartKey

Parameters:#

NameType
options?O

Returns: Promise<[K, JSONValue][]>

Implementation of: ReadTransaction

Defined in: replicache.ts:548


setVerboseWasmLogging#

setVerboseWasmLogging(): Promise<void>

When this is set to true the internal Replicache wasm module will log more things to the console (using console.debug). Setting this to false reduces the amount of logging done by the wasm module.

If you want to see the verbose logging from Replicache in Devtools/Web Inspector you also need to change the console log level to Verbose.

deprecated

Returns: Promise<void>

Defined in: replicache.ts:1022


subscribe#

subscribe<R, E>(body: (tx: ReadTransaction) => Promise<R>, __namedParameters: { onData: (result: R) => void ; onDone?: () => void ; onError?: (error: E) => void }): function

Subcribe to changes to the underlying data. Every time the underlying data changes body is called and if the result of body changes compared to last time onData is called. The function is also called once the first time the subscription is added.

This returns a function that can be used to cancel the subscription.

If an error occurs in the body the onError function is called if present. Otherwise, the error is thrown.

Type parameters:#

NameType
Rundefined | JSONValue
E-

Parameters:#

NameType
body(tx: ReadTransaction) => Promise<R>
__namedParametersobject
__namedParameters.onData(result: R) => void
__namedParameters.onDone?() => void
__namedParameters.onError?(error: E) => void

Returns: () => void

Defined in: replicache.ts:863