/** * State object * @typedef {Object} PostgresState * @property data - the parsed result rows * @property result - the result from a successful query * @property references - an array of all previous data objects used in the job * @private **/ /** * Execution options * @typedef {Object} ExecutionOptions * @property {boolean} [writeSql=false] - Specifies whether to log the generated SQL statement. Defaults to false. * @property {boolean} [execute=true] - Specifies whether to execute the SQL statement. Defaults to true. * @private */ /** * Shared options * @typedef {Object} GeneralOptions * @property {boolean} [execute=true] - Specifies whether to execute the SQL statement. Defaults to true. * @property {boolean} [writeSql=false] - Specifies whether to log the generated SQL statement. Defaults to false. * @property {boolean} [logValues=false] - Specifies whether to log the query values to the console. Defaults to false. * @property {string} [setNull] - A string value that specifies the behavior for inserting null values. * @private * */ /** * SQL Query Configuration * @typedef {Object} SqlQueryConfig * @property {string} name - Prepared statement name for repeated queries. * @property {string} text - SQL query text with optional placeholders for parameterized queries. * @property {Array} [values] - An array of values to be used with parameterized queries. * @property {string} [rowMode='array'] - Format of result rows ('array' or 'object'). * @private */ /** * findValue filter object * @typedef {Object} FindValueFilter * @property {string} uuid - The uuid value to search for in the specified relation. * @property {string} relation - The name of the relation to search for the uuid value. * @property {object} where - An object that contains key-value pairs to filter the search results. * @property {object} operator - An object that contains key-value pairs to specify the type of comparison to perform on the where clause. * @private */ /** * Execute a sequence of operations. * Wraps `language-common/execute`, and prepends initial state for postgresql. * @example * execute( * create('foo'), * delete('bar') * )(state) * @private * @param {Operations} operations - Operations to be performed. * @returns {Operation} */ export function execute(...operations: Operations): Operation; export function setMockClient(mockClient: any): void; /** * Execute an SQL statement * @public * @example Text-only Query * sql('SELECT * FROM users;'); * @example Text-only Query with writeSql option * sql("select id from users where first_name = 'Mamadou'", { writeSql: true }); * @example Parameterized Query * sql("INSERT INTO users(name, age) VALUES ($1, $2);", { values: ["Alice", 25]}); * @example Format query with util.format * sql(util.format('INSERT INTO users(name, age) VALUES (%L, %L);', 'Alice', 25)); * @example Prepared Statements * sql({ * // give the query a unique name * name: "fetch-user", * text: "SELECT * FROM user WHERE id = $1", * values: [1], * }); * @function * @param {string|SqlQueryConfig} sqlQuery - SQL query string or a query config object. * @param {ExecutionOptions} [options] - Execution options. (OpenFn only) * @state {PostgresState} * @returns {Operation} */ export function sql(sqlQuery: string | SqlQueryConfig, options?: ExecutionOptions): Operation; /** * Fetch a uuid key given a condition * @public * @example Find a user by first name * findValue({ * uuid: 'id', * relation: 'users', * where: { first_name: 'Mamadou' }, * operator: { first_name: 'like' } * }) * @function * @param {FindValueFilter} filter - A filter object with the lookup table, a uuid and the condition * @state {PostgresState} * @state data - the value of the found uuid * @returns {Operation} */ export function findValue(filter: FindValueFilter): Operation; /** * Insert a record * @public * @example Insert a record * insert('users', { name: 'Elodie', id: 7 }, { setNull: "'NaN'", logValues: true }); * @function * @param {string} table - The target table * @param {object} record - Payload data for the record as a JS object or function * @param {GeneralOptions} [options] - Shared options. (OpenFn only) * @state {PostgresState} * @returns {Operation} */ export function insert(table: string, record: object, options?: GeneralOptions): Operation; /** * Insert many records, using the keys of the first as the column template * @public * @example Insert many records * insertMany('users', state => state.data.recordArray, { setNull: "'undefined'", logValues: true }); * @function * @param {string} table - The target table * @param {array} records - An array or a function that takes state and returns an array * @param {GeneralOptions} [options] - Shared options. (OpenFn only) * @state {PostgresState} * @returns {Operation} */ export function insertMany(table: string, records: any[], options?: GeneralOptions): Operation; /** * Insert or update a record using ON CONFLICT UPDATE * @public * @example Insert or update a record * upsert( * "users", // the DB table * "ON CONSTRAINT users_pkey", // a DB column with a unique constraint OR a CONSTRAINT NAME * { name: "Elodie", id: 7 }, * { * setNull: ["''", "'undefined'"], * writeSql: true, * logValues: true, * } * ); * @function * @param {string} table - The target table * @param {string} uuid - The uuid column to determine a matching/existing record * @param {object} record - Payload data for the record as a JS object or function * @param {GeneralOptions} [options] - Shared options. (OpenFn only) * @state {PostgresState} * @returns {Operation} */ export function upsert(table: string, uuid: string, record: object, options?: GeneralOptions): Operation; /** * Insert or update a record based on a logical condition using ON CONFLICT UPDATE * @public * @example Insert or update a record conditionally * upsertIf( * $.data.name, * "users", // the DB table * "ON CONSTRAINT users_pkey", // a DB column with a unique constraint OR a CONSTRAINT NAME * { name: "Elodie", id: 7 }, * { writeSql: true } * ); * @function * @param {string} logical - a data to check existing value for. * @param {string} table - The target table * @param {string} uuid - The uuid column to determine a matching/existing record * @param {object} record - Payload data for the record as a JS object or function * @param {GeneralOptions} [options] - Shared options. (OpenFn only) * @state {PostgresState} * @returns {Operation} */ export function upsertIf(logical: string, table: string, uuid: string, record: object, options?: GeneralOptions): Operation; /** * Insert or update multiple records using ON CONFLICT UPDATE and excluded * @public * @example Insert or update multiple records * upsertMany( * 'users', // the DB table * 'email', // a DB column with a unique constraint OR a CONSTRAINT NAME * [ * { name: 'one', email: 'one@openfn.org' }, * { name: 'two', email: 'two@openfn.org' }, * ] * ) * @function * @param {string} table - The target table * @param {string} uuid - The uuid column to determine a matching/existing record * @param {array} data - An array of objects or a function that returns an array * @param {GeneralOptions} [options] - Shared options. (OpenFn only) * @state {PostgresState} * @returns {Operation} */ export function upsertMany(table: string, uuid: string, data: any[], options?: GeneralOptions): Operation; /** * List the columns of a table in a database. * @public * @example Describe a table * describeTable('clinic_visits') * @function * @param {string} tableName - The name of the table to describe * @param {ExecutionOptions} [options] - Execution options. (OpenFn only) * @state {PostgresState} * @returns {Operation} */ export function describeTable(tableName: string, options?: ExecutionOptions): Operation; /** * Create a table in database when given an array of columns and a table_name. * @public * @example * insertTable('table_name', state => state.data.map( * column => ({ * name: column.name, * type: column.type, * required: true, // optional * unique: false, // optional - to be set to true for unique constraint * }) * )); * @function * @param {string} tableName - The name of the table to create * @param {array} columns - An array of form columns * @param {ExecutionOptions} [options] - Execution options. (OpenFn only) * @state {PostgresState} * @returns {Operation} */ export function insertTable(tableName: string, columns: any[], options?: ExecutionOptions): Operation; /** * Alter an existing table in the database. * @public * @example * modifyTable('table_name', state => state.data.map( * newColumn => ({ * name: newColumn.name, * type: newColumn.type, * required: true, // optional * unique: false, // optional - to be set to true for unique constraint * }) * )); * @function * @param {string} tableName - The name of the table to alter * @param {array} columns - An array of form columns * @param {ExecutionOptions} [options] - Execution options. (OpenFn only) * @state {PostgresState} * @returns {Operation} */ export function modifyTable(tableName: string, columns: any[], options?: ExecutionOptions): Operation; /** * State object */ export type PostgresState = { /** * - the parsed result rows */ data: any; /** * - the result from a successful query */ result: any; /** * - an array of all previous data objects used in the job */ references: any; }; /** * Execution options */ export type ExecutionOptions = { /** * - Specifies whether to log the generated SQL statement. Defaults to false. */ writeSql?: boolean; /** * - Specifies whether to execute the SQL statement. Defaults to true. */ execute?: boolean; }; /** * Shared options */ export type GeneralOptions = { /** * - Specifies whether to execute the SQL statement. Defaults to true. */ execute?: boolean; /** * - Specifies whether to log the generated SQL statement. Defaults to false. */ writeSql?: boolean; /** * - Specifies whether to log the query values to the console. Defaults to false. */ logValues?: boolean; /** * - A string value that specifies the behavior for inserting null values. */ setNull?: string; }; /** * SQL Query Configuration */ export type SqlQueryConfig = { /** * - Prepared statement name for repeated queries. */ name: string; /** * - SQL query text with optional placeholders for parameterized queries. */ text: string; /** * - An array of values to be used with parameterized queries. */ values?: any[]; /** * - Format of result rows ('array' or 'object'). */ rowMode?: string; }; /** * findValue filter object */ export type FindValueFilter = { /** * - The uuid value to search for in the specified relation. */ uuid: string; /** * - The name of the relation to search for the uuid value. */ relation: string; /** * - An object that contains key-value pairs to filter the search results. */ where: object; /** * - An object that contains key-value pairs to specify the type of comparison to perform on the where clause. */ operator: object; }; export { alterState, arrayToString, as, assert, combine, cursor, dataPath, dataValue, dateFns, each, field, fields, fn, fnIf, group, lastReferenceValue, log, merge, sourceValue } from "@openfn/language-common";