Skip to main content

Replicache

Classes

Interfaces

Type aliases

ClientStateNotFoundReason

Ƭ ClientStateNotFoundReason: { type: "NotFoundOnServer" } | { type: "NotFoundOnClient" }

The reason onClientStateNotFound was called.


ClientStateNotFoundResponse

Ƭ ClientStateNotFoundResponse: Object

In certain scenarios the server can signal that it does not know about the client. For example, the server might have deleted the client.

Type declaration

NameType
error"ClientStateNotFound"

ExperimentalDiff

Ƭ ExperimentalDiff: ExperimentalIndexDiff | ExperimentalNoIndexDiff

Describes the changes that happened to Replicache after a WriteTransaction was committed.

experimental This type is experimental and may change in the future.


ExperimentalDiffOperation

Ƭ ExperimentalDiffOperation<Key>: { key: Key ; newValue: ReadonlyJSONValue ; op: "add" } | { key: Key ; oldValue: ReadonlyJSONValue ; op: "del" } | { key: Key ; newValue: ReadonlyJSONValue ; oldValue: ReadonlyJSONValue ; op: "change" }

The individual parts describing the changes that happened to the Replicache data. There are three different kinds of operations:

  • add: A new entry was added.
  • del: An entry was deleted.
  • change: An entry was changed.

experimental This type is experimental and may change in the future.

Type parameters

Name
Key

ExperimentalIndexDiff

Ƭ ExperimentalIndexDiff: readonly ExperimentalDiffOperation<IndexKey>[]

experimental This type is experimental and may change in the future.


ExperimentalNoIndexDiff

Ƭ ExperimentalNoIndexDiff: readonly ExperimentalDiffOperation<string>[]

experimental This type is experimental and may change in the future.


ExperimentalWatchCallbackForOptions

Ƭ ExperimentalWatchCallbackForOptions<Options>: Options extends ExperimentalWatchIndexOptions ? ExperimentalWatchIndexCallback : ExperimentalWatchNoIndexCallback

Type parameters

NameType
Optionsextends ExperimentalWatchOptions

ExperimentalWatchIndexCallback

Ƭ ExperimentalWatchIndexCallback: (diff: ExperimentalIndexDiff) => void

Type declaration

▸ (diff): void

Function that gets passed into Replicache.experimentalWatch when doing a watch on a secondary index map and gets called when the data in Replicache changes.

experimental This type is experimental and may change in the future.

Parameters
NameType
diffExperimentalIndexDiff
Returns

void


ExperimentalWatchIndexOptions

Ƭ ExperimentalWatchIndexOptions: ExperimentalWatchNoIndexOptions & { indexName: string }

Options object passed to Replicache.experimentalWatch. This is for an index watch.


ExperimentalWatchNoIndexCallback

Ƭ ExperimentalWatchNoIndexCallback: (diff: ExperimentalNoIndexDiff) => void

Type declaration

▸ (diff): void

Function that gets passed into Replicache.experimentalWatch and gets called when the data in Replicache changes.

experimental This type is experimental and may change in the future.

Parameters
NameType
diffExperimentalNoIndexDiff
Returns

void


ExperimentalWatchNoIndexOptions

Ƭ ExperimentalWatchNoIndexOptions: Object

Options object passed to Replicache.experimentalWatch. This is for a non index watch.

Type declaration

NameTypeDescription
initialValuesInFirstDiff?booleanWhen this is set to true (default is false), the watch callback will be called once asynchronously when watch is called. The arguments in that case is a diff where we consider all the existing values in Replicache as being added.
prefix?stringWhen provided, the watch is limited to changes where the key starts with prefix.

ExperimentalWatchOptions

Ƭ ExperimentalWatchOptions: ExperimentalWatchIndexOptions | ExperimentalWatchNoIndexOptions

Options for Replicache.experimentalWatch.

experimental This interface is experimental and may change in the future.


GetIndexScanIterator

Ƭ GetIndexScanIterator<V>: (indexName: string, fromSecondaryKey: string, fromPrimaryKey: string | undefined) => IterableUnion<readonly [key: IndexKey, value: V]>

Type parameters

Name
V

Type declaration

▸ (indexName, fromSecondaryKey, fromPrimaryKey): IterableUnion<readonly [key: IndexKey, value: V]>

When using makeScanResult this is the type used for the function called when doing a scan with an indexName.

Parameters
NameTypeDescription
indexNamestringThe name of the index we are scanning over.
fromSecondaryKeystringThe fromSecondaryKey is computed by scan and is the secondary key of the first entry to return in the iterator. It is based on prefix and start.key of the ScanIndexOptions.
fromPrimaryKeystring | undefinedThe fromPrimaryKey is computed by scan and is the primary key of the first entry to return in the iterator. It is based on prefix and start.key of the ScanIndexOptions.
Returns

IterableUnion<readonly [key: IndexKey, value: V]>


GetScanIterator

Ƭ GetScanIterator<V>: (fromKey: string) => IterableUnion<ReadonlyEntry<V>>

Type parameters

Name
V

Type declaration

▸ (fromKey): IterableUnion<ReadonlyEntry<V>>

This is called when doing a scan without an indexName.

Parameters
NameTypeDescription
fromKeystringThe fromKey is computed by scan and is the key of the first entry to return in the iterator. It is based on prefix and start.key of the ScanNoIndexOptions.
Returns

IterableUnion<ReadonlyEntry<V>>


HTTPRequestInfo

Ƭ HTTPRequestInfo: Object

Type declaration

NameType
errorMessagestring
httpStatusCodenumber

IndexKey

Ƭ IndexKey: readonly [secondary: string, primary: string]

When using indexes the key is a tuple of the secondary key and the primary key.


IterableUnion

Ƭ IterableUnion<T>: AsyncIterable<T> | Iterable<T>

Type parameters

Name
T

JSONObject

Ƭ JSONObject: Partial<{ [key: string]: JSONValue; }>

A JSON object. We allow undefined values because in TypeScript there is no way to express optional missing properties vs properties with the value undefined.


JSONValue

Ƭ JSONValue: null | string | boolean | number | JSONValue[] | JSONObject

The values that can be represented in JSON


KeyTypeForScanOptions

Ƭ KeyTypeForScanOptions<O>: O extends ScanIndexOptions ? IndexKey : string

If the options contains an indexName then the key type is a tuple of secondary and primary.

Type parameters

NameType
Oextends ScanOptions

LogLevel

Ƭ LogLevel: "error" | "info" | "debug"

The different log levels. This is used to determine how much logging to do. 'error' > 'info' > 'debug'... meaning 'error' has highest priority and 'debug' lowest.


MaybePromise

Ƭ MaybePromise<T>: T | Promise<T>

Type parameters

Name
T

MutatorDefs

Ƭ MutatorDefs: Object

The type used to describe the mutator definitions passed into Replicache constructor as part of the ReplicacheOptions.

See ReplicacheOptions mutators for more info.

Index signature

[key: string]: (tx: WriteTransaction, args?: any) => MaybePromise<JSONValue | void>


PatchOperation

Ƭ PatchOperation: { key: string ; op: "put" ; value: JSONValue } | { key: string ; op: "del" } | { op: "clear" }

This type describes the patch field in a PullResponse and it is used to describe how to update the Replicache key-value store.


Poke

Ƭ Poke: Object

Type declaration

NameType
baseCookieReadonlyJSONValue
pullResponsePullResponse

PullRequest

Ƭ PullRequest: Object

The JSON value used as the body when doing a POST to the pull endpoint.

Type declaration

NameType
clientIDstring
cookieReadonlyJSONValue
lastMutationIDnumber
profileIDstring
pullVersionnumber
schemaVersionstring

PullResponse

Ƭ PullResponse: PullResponseOK | ClientStateNotFoundResponse

PullResponse defines the shape and type of the response of a pull. This is the JSON you should return from your pull server endpoint.


PullResponseOK

Ƭ PullResponseOK: Object

The shape of a pull response under normal circumstances.

Type declaration

NameType
cookie?ReadonlyJSONValue
lastMutationIDnumber
patchPatchOperation[]

Puller

Ƭ Puller: (request: Request) => Promise<PullerResult>

Type declaration

▸ (request): Promise<PullerResult>

Puller is the function type used to do the fetch part of a pull. The request is a POST request where the body is JSON with the type PullRequest.

Parameters
NameType
requestRequest
Returns

Promise<PullerResult>


PullerResult

Ƭ PullerResult: Object

Type declaration

NameType
httpRequestInfoHTTPRequestInfo
response?PullResponse

PushRequest

Ƭ PushRequest: Object

The JSON value used as the body when doing a POST to the push endpoint.

Type declaration

NameType
clientIDstring
mutationsMutation[]
profileIDstring
pushVersionnumber
schemaVersionstring

Pusher

Ƭ Pusher: (request: Request) => Promise<HTTPRequestInfo>

Type declaration

▸ (request): Promise<HTTPRequestInfo>

Pusher is the function type used to do the fetch part of a push. The request is a POST request where the body is JSON with the type PushRequest.

Parameters
NameType
requestRequest
Returns

Promise<HTTPRequestInfo>


ReadonlyJSONObject

Ƭ ReadonlyJSONObject: Partial<{ [key: string]: ReadonlyJSONValue; }>

Like JSONObject but deeply readonly


ReadonlyJSONValue

Ƭ ReadonlyJSONValue: null | string | boolean | number | ReadonlyArray<ReadonlyJSONValue> | ReadonlyJSONObject

Like JSONValue but deeply readonly


ScanIndexOptions

Ƭ ScanIndexOptions: Object

Options for scan when scanning over an index. When scanning over and index you need to provide the indexName and the start key is now a tuple consisting of secondar and primary key

Type declaration

NameTypeDescription
indexNamestringDo a scan over a named index. The indexName is the name of an index previously created with createIndex.
limit?numberOnly include up to limit results.
prefix?stringOnly include results starting with the secondary keys starting with prefix.
start?{ exclusive?: boolean ; key: ScanOptionIndexedStartKey }When provided the scan starts at this key.
start.exclusive?booleanWhether the key is exclusive or inclusive.
start.keyScanOptionIndexedStartKey-

ScanNoIndexOptions

Ƭ ScanNoIndexOptions: Object

Options for scan when scanning over the entire key space.

Type declaration

NameTypeDescription
limit?numberOnly include up to limit results.
prefix?stringOnly include keys starting with prefix.
start?{ exclusive?: boolean ; key: string }When provided the scan starts at this key.
start.exclusive?booleanWhether the key is exclusive or inclusive.
start.keystring-

ScanOptionIndexedStartKey

Ƭ ScanOptionIndexedStartKey: readonly [secondary: string, primary?: string] | string

The key to start scanning at.

If you are scanning the primary index (i.e., you did not specify indexName), then pass a single string for this field, which is the key in the primary index to scan at.

If you are scanning a secondary index (i.e., you specified indexName), then use the tuple form. In that case, secondary is the secondary key to start scanning at, and primary (if any) is the primary key to start scanning at.


ScanOptions

Ƭ ScanOptions: ScanIndexOptions | ScanNoIndexOptions

Options for scan

Variables

TEST_LICENSE_KEY

Const TEST_LICENSE_KEY: "This key only good for automated testing"


consoleLogSink

Const consoleLogSink: LogSink

An implementation of [[Logger]] that logs using console.log etc


version

Const version: string = REPLICACHE_VERSION

The current version of Replicache.

Functions

filterAsyncIterable

filterAsyncIterable<V>(iter, predicate): AsyncIterable<V>

Filters an async iterable.

This utility function is provided because it is useful when using makeScanResult. It can be used to filter out tombstones (delete entries) for example.

Type parameters

Name
V

Parameters

NameType
iterIterableUnion<V>
predicate(v: V) => boolean

Returns

AsyncIterable<V>


isScanIndexOptions

isScanIndexOptions(options): options is ScanIndexOptions

Type narrowing of ScanOptions.

Parameters

NameType
optionsScanOptions

Returns

options is ScanIndexOptions


makeIDBName

makeIDBName(name, schemaVersion?): string

Returns the name of the IDB database that will be used for a particular Replicache instance.

Parameters

NameTypeDescription
namestringThe name of the Replicache instance (i.e., the name field of ReplicacheOptions).
schemaVersion?stringThe schema version of the database (i.e., the schemaVersion field of ReplicacheOptions).

Returns

string


makeScanResult

makeScanResult<Options, Value>(options, getScanIterator): ScanResult<KeyTypeForScanOptions<Options>, Value>

A helper function that makes it easier to implement ReadTransaction.scan with a custom backend.

If you are implementing a custom backend and have an in memory pending async iterable we provide two helper functions to make it easier to merge these together. mergeAsyncIterables and filterAsyncIterable.

For example:

const scanResult = makeScanResult(
options,
options.indexName
? () => {
throw Error('not implemented');
}
: fromKey => {
const persisted: AsyncIterable<Entry<ReadonlyJSONValue>> = ...;
const pending: AsyncIterable<Entry<ReadonlyJSONValue | undefined>> = ...;
const iter = await mergeAsyncIterables(persisted, pending);
const filteredIter = await filterAsyncIterable(
iter,
entry => entry[1] !== undefined,
);
return filteredIter;
},
);

Type parameters

NameType
Optionsextends ScanOptions
ValueValue

Parameters

NameType
optionsOptions
getScanIteratorOptions extends ScanIndexOptions ? GetIndexScanIterator<Value> : GetScanIterator<Value>

Returns

ScanResult<KeyTypeForScanOptions<Options>, Value>


mergeAsyncIterables

mergeAsyncIterables<A, B>(iterableBase, iterableOverlay, compare): AsyncIterable<A | B>

Merges an iterable on to another iterable.

The two iterables need to be ordered and the compare function is used to compare two different elements.

If two elements are equal (compare returns 0) then the element from the second iterable is picked.

This utility function is provided because it is useful when using makeScanResult. It can be used to merge an in memory pending async iterable on to a persistent async iterable for example.

Type parameters

Name
A
B

Parameters

NameType
iterableBaseIterableUnion<A>
iterableOverlayIterableUnion<B>
compare(a: A, b: B) => number

Returns

AsyncIterable<A | B>