Skip to main content

mssql@4.0.0

Classes

execute
sql
findValue
insert
insertMany
upsert
upsertIf
upsertMany
describeTable
insertTable
modifyTable

Functions

createConnection(state)State

Creates a connection.

cleanupState(state)State

Removes unserializable keys from the state.

addRowsToRefs(state, rows)State

Sets the returned rows from a query as the first item in the state.references array, leaving state.data unchanged between operations.

flattenRows(state, rows)State

Returns a flatten object of the rows (array of arrays) with rowCount.

execute

Kind: global class

new exports.execute(operations)

Execute a sequence of operations. Wraps language-common/execute, and prepends initial state for mssql.

ParamTypeDescription
operationsOperationsOperations to be performed.

Example

execute(
create('foo'),
delete('bar')
)(state)

sql

Kind: global class
Access: public

new exports.sql(params)

Execute an SQL statement

ParamTypeDescription
paramsobjectPayload data for the message

Example

sql({ query, options })

findValue

Kind: global class
Access: public

new exports.findValue(filter)

Fetch a uuid key given a condition

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

Example

findValue({
uuid: 'id',
relation: 'users',
where: { first_name: 'Mama%', last_name: 'Cisse'},
operator: { first_name: 'like', last_name: '='}
})

insert

Kind: global class
Access: public

new exports.insert(table, record, options)

Insert a record

ParamTypeDescription
tablestringThe target table
recordobjectPayload data for the record as a JS object
optionsobjectOptional options argument

Example

insert(table, record, {setNull: ["'undefined'", "''"], logValues: false})

insertMany

Kind: global class
Access: public

new exports.insertMany(table, records, options)

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

ParamTypeDescription
tablestringThe target table
recordsfunctionA function that takes state and returns an array of records
optionsobjectOptional options argument

Example

insertMany(table, records, { setNull: false, writeSql: true, logValues: false })

upsert

Kind: global class
Access: public

new exports.upsert(table, uuid, record, options)

Insert or update a record using SQL MERGE

ParamTypeDescription
tablestringThe target table
uuidstringThe uuid column to determine a matching/existing record
recordobjectPayload data for the record as a JS object
optionsobjectOptional options argument

Example

upsert(table, uuid, record, { setNull: "'undefined'", logValues: false})
upsert(table, [uuid1, uuid2], record, { setNull: "'undefined'", logValues: false})

upsertIf

Kind: global class
Access: public

new exports.upsertIf(logical, table, uuid, record, options)

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
optionsobjectOptional options argument

Example

upsertIf(
dataValue('name'),
'users', // the DB table
'uuid', // a DB column with a unique constraint
{ name: 'Elodie', id: 7 },
{ writeSql:true, execute: true, logValues: false }
)

upsertMany

Kind: global class
Access: public

new exports.upsertMany(table, uuid, records, options)

Insert or update multiple records using ON CONFLICT UPDATE and excluded

ParamTypeDescription
tablestringThe target table
uuidstringThe uuid column to determine a matching/existing record
recordsfunctionA function that takes state and returns an array of records
optionsobjectOptional options argument

Example

upsertMany(
'users', 'email', records, { logValues: false }
)
upsertMany(
'users', ['email', 'phone'], records, { logValues: false }
)

describeTable

Kind: global class
Access: public

new exports.describeTable(tableName, options)

List the columns of a table in a database.

ParamTypeDescription
tableNamestringThe name of the table to describe
optionsobjectOptional options argument

Example

describeTable('clinic_visits')

insertTable

Kind: global class
Access: public

new exports.insertTable(tableName, columns, options)

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
optionsobjectOptional options argument

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

Kind: global class
Access: public

new exports.modifyTable(tableName, columns, options)

Alter an existing table in the database.

ParamTypeDescription
tableNamestringThe name of the table to alter
columnsarrayAn array of form columns
optionsobjectOptional options argument

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

createConnection(state) ⇒ State

Creates a connection.

Kind: global function

ParamTypeDescription
stateStateRuntime state.

Example

createConnection(state)

cleanupState(state) ⇒ State

Removes unserializable keys from the state.

Kind: global function

ParamType
stateState

Example

cleanupState(state)

addRowsToRefs(state, rows) ⇒ State

Sets the returned rows from a query as the first item in the state.references array, leaving state.data unchanged between operations.

Kind: global function

ParamTypeDescription
stateState
rowsarraythe array of rows returned from the sql query

flattenRows(state, rows) ⇒ State

Returns a flatten object of the rows (array of arrays) with rowCount.

Kind: global function

ParamTypeDescription
stateState
rowsarraythe array of rows returned from the sql query