Skip to main content

Postgres store

The PostgresAnalyticsStore is an IAnalyticsStore implementation that leverages a Postgres database. It requires some APIs that do not run in a browser, and is intended for server-side applications.

Construction

The PostgresAnalyticsStore uses the pg package and requires Postgres connection information.

By providing a PG connection string, the store will automatically create a pg instance, internally.

A docker-compose file is provided here that will spin up an instance quickly. Note that it cannot be copy/pasted but must be run in the checked out repository to access the correct initialization scripts. See the developer documentation for more information.

Create with only a connection string.

// connects to a local postgres instance, configured by the provided docker-compose file
const store = new PostgresAnalyticsStore({
connectionString: "postgresql://postgres:password@localhost:5555/analytics",
});

Instead, create with a knex object.

import knexFactory from "knex";

const knex = knexFactory({
client: "pg",
connection: "...",
});

const store = new PostgresAnalyticsStore({
knex,
});

The PostgresAnalyticsStore may also be created with optional contructor arguments that may be helpful for debugging or metrics collection.

const store = new PostgresAnalyticsStore({
queryLogger: querydefaultQueryLogger("memory"),
resultsLogger: defaultResultsLogger("memory"),
profiler: new PassthroughAnalyticsProfiler(),
});

For more details on these optional constructor parameters, see the Utilities section.

Raw queries

Though there is no method on IAnalyticsStore for running arbitrary queries, the PostgresAnalyticsStore implementation provides a raw(sql: string) method. This is used only in development, testing, and benchmarking situations and is not intended for production use cases.

const results = store.raw(`select distinct unit from "AnalyticsSeries"`);