TinyBase logoTinyBase β

createCustomPostgreSqlPersister

The createCustomSqlitePersister function creates a Persister object that you can configure to persist the Store to a PostgreSQL database.

createCustomPostgreSqlPersister<ListenerHandle, Persist>(
  store: PersistedStore<Persist>,
  configOrStoreTableName: undefined | string | DatabasePersisterConfig,
  executeCommand: DatabaseExecuteCommand,
  addChangeListener: (channel: string, listener: DatabaseChangeListener) => Promise<ListenerHandle>,
  delChangeListener: (listenerHandle: ListenerHandle) => void,
  onSqlCommand: undefined | (sql: string, params?: any[]) => void,
  onIgnoredError: undefined | (error: any) => void,
  destroy: () => void,
  persist: Persist,
  thing: any,
  getThing?: string,
): Persister<Persist>
TypeDescription
storePersistedStore<Persist>

The Store to persist.

configOrStoreTableNameundefined | string | DatabasePersisterConfig

A DatabasePersisterConfig object, or a string that will be used as the name of the Store's table in the database.

executeCommandDatabaseExecuteCommand

A function that will execute a command against the database.

addChangeListener(channel: string, listener: DatabaseChangeListener) => Promise<ListenerHandle>

A function that will register a listener for changes to the database.

delChangeListener(listenerHandle: ListenerHandle) => void

A function that will unregister the listener for changes to the database.

onSqlCommandundefined | (sql: string, params?: any[]) => void

A function that will be called for each SQL command executed against the database.

onIgnoredErrorundefined | (error: any) => void

An optional handler for the errors that the Persister would otherwise ignore when trying to save or load data. This is suitable for debugging persistence issues in a development environment.

destroy() => void

A function that will be called to perform any extra clean up on the Persister.

persistPersist

An integer from the Persists enum to indicate which types of Store are supported by this Persister: 1 indicates only a regular Store is supported, 2 indicates only a MergeableStore is supported, and 3 indicates that both Store and MergeableStore are supported.

thingany

A reference to the database or connection that can be returned with a method, by default called getDb.

getThing?string

An optional string that will be used to get the reference to the database or connection from the Persister, defaulting to getDb.

returnsPersister<Persist>

A reference to the new Persister object.

This is only used when developing custom database-oriented Persisters, and most TinyBase users will not need to be particularly aware of it.

All of the TinyBase PostgreSQL-oriented Persister functions use this function under the covers, and so you may wish to look at those implementations for ideas on how to build your own Persister type, and as functional examples. Examine the implementation of the createPostgresPersister function as a good starting point, for example.

Since

v5.2.0