Documentation
    Preparing search index...

    Backend adapter implementation for PostgreSQL databases. Translates Quatrain's DataObjects and Queries into raw SQL queries using the pg client. Supports relational schema mapping, JSONB arrays, and advanced filtering.

    https://en.wikipedia.org/wiki/List_of_SQL_reserved_words

    Hierarchy (View Summary)

    Index

    Constructors

    Properties

    _alias: string = ''
    _connection: PoolClient | undefined
    _middlewares: BM[] = []
    _params: BackendParameters = {}
    _pool: Pool | undefined
    PKEY_IDENTIFIER: any = 'id'

    The string identifier for primary keys, mapped to 'id' by default.

    Accessors

    • get alias(): string

      Returns string

    • set alias(alias: string): void

      Parameters

      • alias: string

      Returns void

    Methods

    • Convert array into SQL expression

      Parameters

      • from: any[]

        Array of strings or numbers

      Returns string

      string

    • Resolves the canonical database path for a record, respecting standard collection structures and hierarchical subcollection configurations.

      Parameters

      Returns string

    • Returns Promise<PoolClient>

    • Ensures that the database table for the given DataObject's collection exists. If the table does not exist, it automatically creates it. Additionally, ensures that any related join tables for ObjectProperty references are also created so that LEFT JOIN queries do not crash on non-existent tables.

      Parameters

      • dataObject: DataObjectClass<any>

        The DataObject payload defining properties and collection.

      Returns Promise<void>

      A promise resolving when the table and relation tables exist.

    • Dynamically ensures that a table exists with the correct columns derived from properties. Deduplicates column definitions by lowercase name to prevent SQL parser errors (e.g. "column 'name' specified more than once") when child models override base properties.

      Parameters

      • tableName: string

        The name of the table to verify/create.

      • properties: any

        The schema properties mapping to columns.

      Returns Promise<void>

      A promise resolving when the table is successfully created or verified.

    • Process data for compatibility

      Parameters

      • data: any
      • filterNulls: boolean = true

      Returns any[]

    • Parameters

      • sql: string
      • params: any[] = []

      Returns Promise<QueryResult<any>>

    • Processes raw data entries to convert relational reference objects into native database foreign key IDs if the adapter has been configured with useNativeForeignKeys = true.

      Parameters

      • data: any[]

      Returns any[]

    • Resolves the table/collection name for a given model or relation reference. Accounts for mapping definitions, class constructors, or raw string types.

      Parameters

      • instanceOf: any

        The relation constructor, class, or collection name string.

      Returns string

      The resolved table/collection name.

    • Attaches a new middleware to the adapter's execution pipeline. Middlewares are triggered before or after database actions.

      Parameters

      • middleware: BM

        The instantiated middleware to attach.

      Returns void

      If a middleware with the same class name is already attached.

    • Executes an aggregation operation (sum, avg, distinct, min, max, count) on a query. The default implementation fetches all matching records and performs in-memory aggregation. Specific database adapters should override this to perform native query aggregation.

      Parameters

      • query: Query<any>

        The Query instance defining the collection and filters.

      • operation: "sum" | "avg" | "distinct" | "min" | "max" | "count"

        The aggregate operation.

      • Optionalproperty: string

        The name of the property to aggregate.

      Returns Promise<any>

      A promise resolving to the aggregated result.

    • Translates a DataObject creation request into an INSERT INTO SQL query. Generates a UUID automatically if none is requested.

      Parameters

      • dataObject: DataObjectClass<any>

        The DataObject payload.

      • desiredUid: string | undefined

        Optional explicit UUID to use as primary key.

      Returns Promise<DataObjectClass<any>>

      A promise resolving to the saved DataObject.

      If a UID already exists on the object.

    • Handles object deletion. Converts to an UPDATE query setting status if soft-deleted, or a DELETE FROM query if hardDelete is forced.

      Parameters

      • dataObject: DataObjectClass<any>

        The DataObject to remove.

      • hardDelete: boolean = false

        Force an absolute SQL DELETE regardless of softDelete configs.

      Returns Promise<DataObjectClass<any>>

      A promise resolving to the processed DataObject.

    • Clears an entire table using a high-speed SQL TRUNCATE TABLE command.

      Parameters

      • collection: string

        The table name to truncate.

      • batchSize: number = 500

        Ignored in Postgres as TRUNCATE handles all rows.

      Returns Promise<void>

    • Removes a middleware from the pipeline by its class name.

      Parameters

      • middlewareClassName: string

        The exact name of the middleware class to remove.

      Returns void

    • Disconnect the pool and close all idle connections.

      Returns Promise<void>

    • Orchestrates the sequential execution of all attached middlewares for a given action.

      Parameters

      • dataObject: DataObjectClass<any>

        The payload traversing the middlewares.

      • action: BackendAction

        The context (READ, CREATE, UPDATE, DELETE).

      • timing: "before" | "after" = 'before'

        Whether to run the before or after pipeline.

      • Optionalparams: MiddlewareParams

        Optional parameters passed down to the middlewares.

      Returns Promise<DataObjectClass<any>>

      A promise resolving to the potentially mutated DataObject.

    • Translates Quatrain's Query logic (Filters, Limits) into a complex SQL SELECT statement. Supports ILIKE string searches, JSONB array traversals, and relational joins.

      Parameters

      • dataObject: DataObjectClass<any>

        The template DataObject defining the table and mapping.

      • filters: Filter[] | Filters | undefined = undefined

        Active Filters limiting the result set.

      • pagination: SortAndLimit | undefined = undefined

        Limit, batch size, and sorting configurations.

      • parent: DataObjectClass<any> | undefined = undefined

        Optional parent context for scoping queries.

      Returns Promise<QueryResultType<DataObjectClass<any>>>

      A promise resolving to hydrated objects and count metadata.

    • Generates the SQL CREATE TABLE and DROP TABLE statements required to initialize a collection's storage in PostgreSQL, mapping Quatrain Property types to SQL Column types.

      Parameters

      • collection: string

        The table name.

      • properties: any[]

        The property dictionary of the model.

      Returns { downSql: string; upSql: string }

      Up and Down migration SQL strings.

    • Generates the SQL ALTER TABLE statements to apply a schema delta (add/drop columns).

      Parameters

      • collection: string

        The table name.

      • delta: any

        The SchemaDelta tracking property additions/removals.

      Returns { downSql: string[]; upSql: string[] }

      Arrays of Up and Down migration SQL statements.

    • Helper method to extract the destination collection name from a DataObject.

      Parameters

      Returns string | undefined

      The resolved collection string.

    • Retrieves a specific configuration parameter.

      Parameters

      • key: BackendParametersKeys

        The parameter key to fetch.

      Returns any

      The value associated with the key, or undefined.

    • Returns true if a given middleware is attached

      Parameters

      • className: string

      Returns boolean

      boolean

    • Outputs an adapter-level diagnostic message to the console if debug mode is enabled.

      Parameters

      • message: string

        The textual content to log.

      Returns void

      Use Backend.debug() or Backend.log() (which itself is deprecated in favor of specific levels) instead.

    • Executes an arbitrary raw SQL query against the Postgres database.

      Parameters

      • sql: string

        The SQL statement with optional $1, $2 parameterized placeholders.

      • Optionalparams: any[]

        The array of parameter values to inject into the query.

      Returns Promise<any>

      A promise resolving to the pg QueryResult.

    • Translates a read request into a SELECT * ... LEFT JOIN ... query. Automatically handles joins for relational ObjectProperty fields.

      Parameters

      • dataObject: DataObjectClass<any>

        The DataObject to populate, containing the UID to fetch.

      Returns Promise<DataObjectClass<any>>

      A promise resolving to the populated DataObject.

      If the query returns 0 rows.

    • Overrides or adds a backend configuration parameter dynamically.

      Parameters

      • key: BackendParametersKeys

        The parameter key to modify.

      • value: any

        The new value to assign.

      Returns void

    • Translates an update request into an UPDATE ... SET ... query. Uses ignoreUnchanged to efficiently update only modified fields.

      Parameters

      Returns Promise<DataObjectClass<any>>

      A promise resolving to the updated DataObject.