Skip to main content

Class: Replicache<MD>

Type parameters#

NameType
MDextends MutatorDefs{}

Implements#

Constructors#

constructor#

new Replicache<MD>(options?)

Type parameters#

NameType
MDextends MutatorDefs{}

Parameters#

NameType
optionsReplicacheOptions<MD>

Properties#

getPullAuth#

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

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


getPushAuth#

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

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


mutate#

Readonly mutate: MakeMutators<MD>

The mutators that was registered in the constructor.


onOnlineChange#

onOnlineChange: null | (online: boolean) => void = null

onOnlineChange is called when the online property changes. See online for more details.


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]);

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.


pushDelay#

pushDelay: number

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

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>


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


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 property returns true if the last sync attempt succeeded, and false otherwise.

Returns#

boolean


requestOptions#

get requestOptions(): Required<RequestOptions>

The options used to control the pull and push request behavior. This object is live so changes to it will affect the next pull or push call.

Returns#

Required<RequestOptions>

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>


createIndex#

createIndex(def): 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>


dropIndex#

dropIndex(name): Promise<void>

Drops an index previously created with createIndex.

Parameters#

NameType
namestring

Returns#

Promise<void>


get#

get(key): Promise<undefined | JSONValue>

Get a single value from the database.

Parameters#

NameType
keystring

Returns#

Promise<undefined | JSONValue>

Implementation of#

ReadTransaction.get


has#

has(key): Promise<boolean>

Determines if a single key is present in the database.

Parameters#

NameType
keystring

Returns#

Promise<boolean>

Implementation of#

ReadTransaction.has


isEmpty#

isEmpty(): Promise<boolean>

Whether the database is empty.

Returns#

Promise<boolean>

Implementation of#

ReadTransaction.isEmpty


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


query#

query<R>(body): 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>


register#

register<Return>(name, mutatorImpl): () => Promise<Return>

deprecated Use ReplicacheOptions.mutators instead.

Type parameters#

NameType
Returnextends void | JSONValue

Parameters#

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

Returns#

fn

▸ (): Promise<Return>

Returns#

Promise<Return>

register<Return, Args>(name, mutatorImpl): (args: Args) => Promise<Return>

deprecated Use ReplicacheOptions.mutators instead.

Type parameters#

NameType
Returnextends void | JSONValue
Argsextends JSONValue

Parameters#

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

Returns#

fn

▸ (args): Promise<Return>

Parameters#
NameType
argsArgs
Returns#

Promise<Return>


scan#

scan<O, K>(options?): 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
Oextends ScanOptions
Kextends string | [secondary: string, primary: string]

Parameters#

NameType
options?O

Returns#

ScanResult<K>

Implementation of#

ReadTransaction.scan


scanAll#

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

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

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

Type parameters#

NameType
Oextends ScanOptions
Kextends string | [secondary: string, primary: string]

Parameters#

NameType
options?O

Returns#

Promise<[K, JSONValue][]>

Implementation of#

ReadTransaction.scanAll


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>


subscribe#

subscribe<R, E>(body, __namedParameters): () => void

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
Rextends undefined | JSONValue
EE

Parameters#

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

Returns#

fn

▸ (): void

Returns#

void