Skip to main content

postgresql@8.0.0

describeTable(tableName, [options])
findValue(filter)
insert(table, record, [options])
insertMany(table, records, [options])
insertTable(tableName, columns, [options])
modifyTable(tableName, columns, [options])
sql(sqlQuery, [options])
upsert(table, uuid, record, [options])
upsertIf(logical, table, uuid, record, [options])
upsertMany(table, uuid, data, [options])

This adaptor exports the following from common:

alterState
arrayToString()
as()
assert()
combine()
cursor()
dataPath()
dataValue()
dateFns
each()
field()
fields()
fn()
fnIf()
group()
lastReferenceValue()
merge()
sourceValue()

Functions

describeTable

describeTable(tableName, [options]) ⇒ Operation

List the columns of a table in a database.

ParamTypeDescription
tableNamestringThe name of the table to describe
[options]ExecutionOptionsExecution options. (OpenFn only)

This operation writes the following keys to state:

State KeyDescription
datathe parsed result rows
resultthe result from a successful query
referencesan array of all previous data objects used in the job

Example: Describe a table

describeTable('clinic_visits')

findValue

findValue(filter) ⇒ Operation

Fetch a uuid key given a condition

ParamTypeDescription
filterFindValueFilterA filter object with the lookup table, a uuid and the condition

This operation writes the following keys to state:

State KeyDescription
datathe value of the found uuid
resultthe result from a successful query
referencesan array of all previous data objects used in the job

Example: Find a user by first name

findValue({
uuid: 'id',
relation: 'users',
where: { first_name: 'Mamadou' },
operator: { first_name: 'like' }
})

insert

insert(table, record, [options]) ⇒ Operation

Insert a record

ParamTypeDescription
tablestringThe target table
recordobjectPayload data for the record as a JS object or function
[options]GeneralOptionsShared options. (OpenFn only)

This operation writes the following keys to state:

State KeyDescription
datathe parsed result rows
resultthe result from a successful query
referencesan array of all previous data objects used in the job

Example: Insert a record

insert('users', { name: 'Elodie', id: 7 }, { setNull: "'NaN'", logValues: true });

insertMany

insertMany(table, records, [options]) ⇒ Operation

Insert many records, using the keys of the first as the column template

ParamTypeDescription
tablestringThe target table
recordsarrayAn array or a function that takes state and returns an array
[options]GeneralOptionsShared options. (OpenFn only)

This operation writes the following keys to state:

State KeyDescription
datathe parsed result rows
resultthe result from a successful query
referencesan array of all previous data objects used in the job

Example: Insert many records

insertMany('users', state => state.data.recordArray, { setNull: "'undefined'", logValues: true });

insertTable

insertTable(tableName, columns, [options]) ⇒ Operation

Create a table in database when given an array of columns and a table_name.

ParamTypeDescription
tableNamestringThe name of the table to create
columnsarrayAn array of form columns
[options]ExecutionOptionsExecution options. (OpenFn only)

This operation writes the following keys to state:

State KeyDescription
datathe parsed result rows
resultthe result from a successful query
referencesan array of all previous data objects used in the job

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
})
));

modifyTable

modifyTable(tableName, columns, [options]) ⇒ Operation

Alter an existing table in the database.

ParamTypeDescription
tableNamestringThe name of the table to alter
columnsarrayAn array of form columns
[options]ExecutionOptionsExecution options. (OpenFn only)

This operation writes the following keys to state:

State KeyDescription
datathe parsed result rows
resultthe result from a successful query
referencesan array of all previous data objects used in the job

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
})
));

sql

sql(sqlQuery, [options]) ⇒ Operation

Execute an SQL statement

ParamTypeDescription
sqlQuerystring | SqlQueryConfigSQL query string or a query config object.
[options]ExecutionOptionsExecution options. (OpenFn only)

This operation writes the following keys to state:

State KeyDescription
datathe parsed result rows
resultthe result from a successful query
referencesan array of all previous data objects used in the job

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],
});

upsert

upsert(table, uuid, record, [options]) ⇒ Operation

Insert or update a record using ON CONFLICT UPDATE

ParamTypeDescription
tablestringThe target table
uuidstringThe uuid column to determine a matching/existing record
recordobjectPayload data for the record as a JS object or function
[options]GeneralOptionsShared options. (OpenFn only)

This operation writes the following keys to state:

State KeyDescription
datathe parsed result rows
resultthe result from a successful query
referencesan array of all previous data objects used in the job

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,
}
);

upsertIf

upsertIf(logical, table, uuid, record, [options]) ⇒ Operation

Insert or update a record based on a logical condition using ON CONFLICT UPDATE

ParamTypeDescription
logicalstringa data to check existing value for.
tablestringThe target table
uuidstringThe uuid column to determine a matching/existing record
recordobjectPayload data for the record as a JS object or function
[options]GeneralOptionsShared options. (OpenFn only)

This operation writes the following keys to state:

State KeyDescription
datathe parsed result rows
resultthe result from a successful query
referencesan array of all previous data objects used in the job

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 }
);

upsertMany

upsertMany(table, uuid, data, [options]) ⇒ Operation

Insert or update multiple records using ON CONFLICT UPDATE and excluded

ParamTypeDescription
tablestringThe target table
uuidstringThe uuid column to determine a matching/existing record
dataarrayAn array of objects or a function that returns an array
[options]GeneralOptionsShared options. (OpenFn only)

This operation writes the following keys to state:

State KeyDescription
datathe parsed result rows
resultthe result from a successful query
referencesan array of all previous data objects used in the job

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' },
]
)