import type { SQLValue, PoolClient, Pool } from '@evershop/postgres-query-builder'; import type { AdminUserRow, AttributeRow, AttributeGroupRow, AttributeGroupLinkRow, AttributeOptionRow, CartRow, CartAddressRow, CartItemRow, CategoryRow, CategoryDescriptionRow, CmsPageRow, CmsPageDescriptionRow, CollectionRow, CouponRow, CustomerRow, CustomerAddressRow, CustomerGroupRow, EventRow, MigrationRow, OrderRow, OrderActivityRow, OrderAddressRow, OrderItemRow, PaymentTransactionRow, ProductRow, ProductAttributeValueIndexRow, ProductCategoryRow, ProductCollectionRow, ProductCustomOptionRow, ProductCustomOptionValueRow, ProductDescriptionRow, ProductImageRow, ProductInventoryRow, ResetPasswordTokenRow, SessionRow, SettingRow, ShipmentRow, ShippingMethodRow, ShippingZoneRow, ShippingZoneMethodRow, ShippingZoneProvinceRow, TaxClassRow, TaxRateRow, UrlRewriteRow, VariantGroupRow, WidgetRow } from '../../types/db/index.js'; export type TableName = 'admin_user' | 'attribute' | 'attribute_group' | 'attribute_group_link' | 'attribute_option' | 'cart' | 'cart_address' | 'cart_item' | 'category' | 'category_description' | 'cms_page' | 'cms_page_description' | 'collection' | 'coupon' | 'customer' | 'customer_address' | 'customer_group' | 'event' | 'migration' | 'order' | 'order_activity' | 'order_address' | 'order_item' | 'payment_transaction' | 'product' | 'product_attribute_value_index' | 'product_category' | 'product_collection' | 'product_custom_option' | 'product_custom_option_value' | 'product_description' | 'product_image' | 'product_inventory' | 'reset_password_token' | 'session' | 'setting' | 'shipment' | 'shipping_method' | 'shipping_zone' | 'shipping_zone_method' | 'shipping_zone_province' | 'tax_class' | 'tax_rate' | 'url_rewrite' | 'user_token_secret' | 'variant_group' | 'widget'; type TableColumnMap = { admin_user: keyof AdminUserRow; attribute: keyof AttributeRow; attribute_group: keyof AttributeGroupRow; attribute_group_link: keyof AttributeGroupLinkRow; attribute_option: keyof AttributeOptionRow; cart: keyof CartRow; cart_address: keyof CartAddressRow; cart_item: keyof CartItemRow; category: keyof CategoryRow; category_description: keyof CategoryDescriptionRow; cms_page: keyof CmsPageRow; cms_page_description: keyof CmsPageDescriptionRow; collection: keyof CollectionRow; coupon: keyof CouponRow; customer: keyof CustomerRow; customer_address: keyof CustomerAddressRow; customer_group: keyof CustomerGroupRow; event: keyof EventRow; migration: keyof MigrationRow; order: keyof OrderRow; order_activity: keyof OrderActivityRow; order_address: keyof OrderAddressRow; order_item: keyof OrderItemRow; payment_transaction: keyof PaymentTransactionRow; product: keyof ProductRow; product_attribute_value_index: keyof ProductAttributeValueIndexRow; product_category: keyof ProductCategoryRow; product_collection: keyof ProductCollectionRow; product_custom_option: keyof ProductCustomOptionRow; product_custom_option_value: keyof ProductCustomOptionValueRow; product_description: keyof ProductDescriptionRow; product_image: keyof ProductImageRow; product_inventory: keyof ProductInventoryRow; reset_password_token: keyof ResetPasswordTokenRow; session: keyof SessionRow; setting: keyof SettingRow; shipment: keyof ShipmentRow; shipping_method: keyof ShippingMethodRow; shipping_zone: keyof ShippingZoneRow; shipping_zone_method: keyof ShippingZoneMethodRow; shipping_zone_province: keyof ShippingZoneProvinceRow; tax_class: keyof TaxClassRow; tax_rate: keyof TaxRateRow; url_rewrite: keyof UrlRewriteRow; user_token_secret: never; variant_group: keyof VariantGroupRow; widget: keyof WidgetRow; }; /** * Extends `TableName` with a `string & {}` fallback so custom or future tables * (not yet in the schema) are accepted without a compile error, while still * surfacing the known table names as autocomplete suggestions. */ export type AnyTableName = TableName | (string & {}); type TableRowMap = { admin_user: AdminUserRow; attribute: AttributeRow; attribute_group: AttributeGroupRow; attribute_group_link: AttributeGroupLinkRow; attribute_option: AttributeOptionRow; cart: CartRow; cart_address: CartAddressRow; cart_item: CartItemRow; category: CategoryRow; category_description: CategoryDescriptionRow; cms_page: CmsPageRow; cms_page_description: CmsPageDescriptionRow; collection: CollectionRow; coupon: CouponRow; customer: CustomerRow; customer_address: CustomerAddressRow; customer_group: CustomerGroupRow; event: EventRow; migration: MigrationRow; order: OrderRow; order_activity: OrderActivityRow; order_address: OrderAddressRow; order_item: OrderItemRow; payment_transaction: PaymentTransactionRow; product: ProductRow; product_attribute_value_index: ProductAttributeValueIndexRow; product_category: ProductCategoryRow; product_collection: ProductCollectionRow; product_custom_option: ProductCustomOptionRow; product_custom_option_value: ProductCustomOptionValueRow; product_description: ProductDescriptionRow; product_image: ProductImageRow; product_inventory: ProductInventoryRow; reset_password_token: ResetPasswordTokenRow; session: SessionRow; setting: SettingRow; shipment: ShipmentRow; shipping_method: ShippingMethodRow; shipping_zone: ShippingZoneRow; shipping_zone_method: ShippingZoneMethodRow; shipping_zone_province: ShippingZoneProvinceRow; tax_class: TaxClassRow; tax_rate: TaxRateRow; url_rewrite: UrlRewriteRow; user_token_secret: Record; variant_group: VariantGroupRow; widget: WidgetRow; }; /** * Full row type for table `T`. Use `Partial>` in write operations. * Falls back to `Record` for unknown/custom tables. */ export type RowOf = T extends TableName ? TableRowMap[T] : Record; /** * Write-compatible version of a row type for use in `.given()`. * * PostgreSQL `numeric`/`decimal` columns are returned by `node-postgres` as * `string` (to preserve precision), but the driver accepts both `string` and * `number` as input. This mapped type widens every `string`-typed field to * `string | number` (and `string | null` to `string | number | null`) so that * calls like `.given({ amount: 42 })` compile without needing an explicit cast. */ export type WriteRow = { [K in keyof T]: T[K] extends string ? string | number : T[K] extends string | null ? string | number | null : T[K]; }; /** * Known column names for table `T`. * The `string & {}` fallback lets arbitrary expressions, qualified names, and * aliased joined columns pass through without a compile error, while still * surfacing the known column literals as autocomplete suggestions. */ /** * Known column names for table `T`. * Falls back to `string` for unknown/custom tables so any column name is accepted. * The `string & {}` union keeps the known literal suggestions visible in autocomplete. */ export type ColumnOf = T extends TableName ? TableColumnMap[T] | (string & {}) : string; /** * All known columns from all tables, prefixed with their table name. * e.g. 'order.order_id' | 'order.status' | 'product.sku' | ... * Used to provide autocomplete in `select()` before `.from()` is called. * * The mapped-type form `{ [T in TableName]: ... }[TableName]` is required to * force correct per-table distribution. Using a generic alias `Prefix` * causes TypeScript to evaluate `TableColumnMap[T]` with T as the full union, * producing a cross-product (e.g. 'product.amount') which is incorrect. */ export type AllPrefixedColumns = { [T in TableName]: `${T}.${TableColumnMap[T] & string}`; }[TableName]; /** * WHERE-clause chain returned by write query methods (UPDATE / DELETE). * Provides `.and()` / `.or()` for additional conditions, then `.execute()`. */ export interface TypedWriteConditionChain { and(field: ColumnOf, operator: string, value: any): TypedWriteConditionChain; or(field: ColumnOf, operator: string, value: any): TypedWriteConditionChain; andWhere(field: ColumnOf, operator: string, value: any): TypedWriteConditionChain; orWhere(field: ColumnOf, operator: string, value: any): TypedWriteConditionChain; execute(connection: PoolClient | Pool, releaseConnection?: boolean): Promise; } /** Typed INSERT query. `.given()` accepts the row shape of table `T`. */ export interface TypedInsertQuery { given(data: Partial>>): TypedInsertQuery; prime(field: ColumnOf, value: any): TypedInsertQuery; execute(connection: PoolClient | Pool, releaseConnection?: boolean): Promise; } /** Typed UPDATE query. `.given()` / `.prime()` are constrained to columns of `T`. */ export interface TypedUpdateQuery { given(data: Partial>>): TypedUpdateQuery; prime(field: ColumnOf, value: any): TypedUpdateQuery; where(field: ColumnOf, operator: string, value: any): TypedWriteConditionChain; andWhere(field: ColumnOf, operator: string, value: any): TypedWriteConditionChain; orWhere(field: ColumnOf, operator: string, value: any): TypedWriteConditionChain; execute(connection: PoolClient | Pool, releaseConnection?: boolean): Promise; } /** Typed DELETE query. `.where()` is constrained to columns of `T`. */ export interface TypedDeleteQuery { where(field: ColumnOf, operator: string, value: any): TypedWriteConditionChain; andWhere(field: ColumnOf, operator: string, value: any): TypedWriteConditionChain; orWhere(field: ColumnOf, operator: string, value: any): TypedWriteConditionChain; execute(connection: PoolClient | Pool, releaseConnection?: boolean): Promise; } /** Typed INSERT … ON CONFLICT DO UPDATE query. */ export interface TypedInsertOnUpdateQuery { given(data: Partial>>): TypedInsertOnUpdateQuery; prime(field: ColumnOf, value: any): TypedInsertOnUpdateQuery; execute(connection: PoolClient | Pool, releaseConnection?: boolean): Promise; } /** * Returned by `.leftJoin()` / `.rightJoin()` / `.innerJoin()`. * Call `.on()` to complete the join condition. */ export interface TypedJoin { on(column: string, operator: string, referencedColumn: string): TypedQueryChain; } /** * The WHERE-clause chain returned by `.where()` / `.andWhere()` / `.orWhere()`. * Provides `.and()` / `.or()` for additional conditions, then `.execute()` / `.load()`. */ export interface TypedConditionChain { and(field: ColumnOf, operator: string, value: any): TypedConditionChain; or(field: ColumnOf, operator: string, value: any): TypedConditionChain; andWhere(field: ColumnOf, operator: string, value: any): TypedConditionChain; orWhere(field: ColumnOf, operator: string, value: any): TypedConditionChain; execute(connection: PoolClient | Pool, releaseConnection?: boolean): Promise[]>; load(connection: PoolClient | Pool, releaseConnection?: boolean): Promise | null>; } /** * A fully table-aware SELECT query chain. * Every field parameter is constrained to the known columns of table `T` * (with a `string` fallback so expressions and join aliases still work). */ export interface TypedQueryChain { /** * Add a column to the SELECT clause. Accepts bare column names for table `T` * (e.g. `'order_id'`) or prefixed names for cross-table references after joins * (e.g. `'product.sku'`), as well as SQL expressions via `sql(...)`. */ select(field: ColumnOf | AllPrefixedColumns | SQLValue, alias?: string): TypedQueryChain; /** * Set the FROM table and re-narrow the chain to that table's columns. * Calling `.from('order')` causes all subsequent `.where()`, `.orderBy()`, * etc. to suggest only the columns of the `order` table. */ from(table: U, alias?: string): TypedQueryChain; where(field: ColumnOf, operator: string, value: any): TypedConditionChain; andWhere(field: ColumnOf, operator: string, value: any): TypedConditionChain; orWhere(field: ColumnOf, operator: string, value: any): TypedConditionChain; leftJoin(table: TableName | (string & {}), alias?: string): TypedJoin; rightJoin(table: TableName | (string & {}), alias?: string): TypedJoin; innerJoin(table: TableName | (string & {}), alias?: string): TypedJoin; groupBy(...fields: Array>): TypedQueryChain; orderBy(field: ColumnOf, direction?: 'ASC' | 'DESC' | (string & {})): TypedQueryChain; orderDirection(direction: string): TypedQueryChain; having(field: ColumnOf, operator: string, value: any): TypedQueryChain; limit(offset: number, limit: number): TypedQueryChain; removeOrderBy(): TypedQueryChain; removeGroupBy(): TypedQueryChain; removeLimit(): TypedQueryChain; execute(connection: PoolClient | Pool, releaseConnection?: boolean): Promise[]>; load(connection: PoolClient | Pool, releaseConnection?: boolean): Promise | null>; sql(): Promise; clone(): TypedQueryChain; } /** * The initial query builder, before a table is bound via `.from()`. * Column arguments are drawn from the union of every known table's columns. * Call `.from(tableName)` to bind a specific table and unlock per-table column * suggestions on all subsequent methods. */ export interface UnboundSelectChain { /** * Add a column to the SELECT clause. Suggestions are shown as `table.column` * (e.g. `'order.order_id'`, `'product.sku'`) so you know which table each * column belongs to. You are responsible for calling `.from(table)` with * the matching table afterwards. */ select(field: AllPrefixedColumns | (string & {}), alias?: string): UnboundSelectChain; /** * Bind a table and return a fully typed query chain. * * @example * select('order.order_id', 'order.status').from('order').where('status', '=', 'pending').execute(pool) */ from(table: T, alias?: string): TypedQueryChain; } /** * Begin a SELECT query. * * Column name suggestions are shown as `table.column` (e.g. `'order.order_id'`, * `'product.sku'`) so you can see which table each column belongs to. * Once you call `.from(tableName)`, the chain narrows to that table's columns * for `.where()`, `.orderBy()`, `.groupBy()`, etc. * * @example * // All columns shorthand: * select().from('order').where('status', '=', 'pending').execute(pool) * * // Explicit columns with table-prefixed suggestions: * select('order.order_id', 'order.uuid') * .from('order') * .orderBy('created_at', 'DESC') * .execute(pool) */ export declare function select(...args: Array): UnboundSelectChain & { from(table: T, alias?: string): TypedQueryChain; }; /** Insert a row into a known EverShop table. `.given()` suggests columns of `T`. */ export declare function insert(table: T): TypedInsertQuery; /** Update rows in a known EverShop table. `.given()` / `.where()` suggest columns of `T`. */ export declare function update(table: T): TypedUpdateQuery; /** Delete rows from a known EverShop table. `.where()` suggests columns of `T`. */ export declare function del(table: T): TypedDeleteQuery; /** Insert or update rows in a known EverShop table on conflict. `.given()` suggests columns of `T`. */ export declare function insertOnUpdate(table: T, conflictColumns: Array>): TypedInsertOnUpdateQuery; export { node, getConnection, startTransaction, commit, rollback, release, execute, sql, value, SelectQuery, UpdateQuery, InsertQuery, InsertOnUpdateQuery, DeleteQuery, type SQLValue, type Binding, type Pool, type PoolClient, type JoinDefinition } from '@evershop/postgres-query-builder';