Skip to content
This repository has been archived by the owner on Oct 21, 2022. It is now read-only.

Commit

Permalink
update docs, release v2.1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
taylordowns2000 committed Sep 8, 2020
1 parent 8a3b8ab commit 0a44e18
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 147 deletions.
37 changes: 26 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,16 @@ Execute an sql query.
#### sample usage

```js
sql(function (state) {
return (
sql(
state =>
`INSERT INTO untitled_table (name, the_geom) VALUES ('` +
dataValue('form.first_name')(state) +
`', ST_SetSRID(ST_Point(` +
dataValue('lat')(state) +
`, ` +
dataValue('long')(state) +
`),4326))`
);
});
);
```

## Insert a single record
Expand All @@ -51,6 +50,17 @@ insert('users', {
});
```

## Insert or Update using a unique column as a key

```js
upsert('users', 'email', {
email: '[email protected]',
first_name: 'Luca',
inserted_at: '2010-01-01 00:00:00',
updated_at: '2010-01-01 00:00:00',
});
```

## Insert many records in postgresql

This function allows the insert of a set of records inside a table all at once.
Expand All @@ -67,15 +77,20 @@ insertMany('users', state =>
);
```

## Insert or Update using a unique column as a key
## Upsert many records in postgresql

This function allows the upsert of a set of records inside a table all at once.

```js
upsert('users', 'email', {
email: '[email protected]',
first_name: 'Luca',
inserted_at: '2010-01-01 00:00:00',
updated_at: '2010-01-01 00:00:00',
});
upsertMany('users', 'ON CONSTRAINT users_pkey', state =>
state.data.people.map(s => {
return {
first_name: ['Luca', 'Mohamed', 'Elodie'],
inserted_at: '2020-01-01 00:00:00',
updated_at: '2020-01-01 00:00:00',
};
})
);
```

## Development
Expand Down
82 changes: 27 additions & 55 deletions lib/Adaptor.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,53 +213,30 @@ function sql(sqlQuery) {
}
};
}

function escapeRegExp(string) {
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
}

function handleValues(sqlString, nullString) {
if (nullString == false) {
return sqlString;
}

var re = new RegExp(escapeRegExp(nullString), 'g');
return sqlString.replace(re, 'NULL');
}

function handleOptions(options) {
if (options && options.setNull === false) {
return false;
}

return options && options.setNull || "'undefined'";
}
/**
* Insert a record
* @example
* execute(
* insert(table, record, {setNull: "'undefined'"})
* )(state)
* insert('users', {name: 'Elodie', id: 7});
* @constructor
* @param {string} table - The target table
* @param {object} record - Payload data for the record as a JS object
* @param {object} options - Optional options argument
* @returns {Operation}
*/


function insert(table, record, options) {
function insert(table, record) {
return function (state) {
var client = state.client;

try {
var recordData = (0, _languageCommon.expandReferences)(record)(state);
var columns = Object.keys(recordData).sort();
var columnsList = columns.join(', ');
var values = columns.map(function (key) {
return recordData[key];
});
var query = (0, _pgFormat["default"])("INSERT INTO ".concat(table, " (").concat(columns.join(', '), ") VALUES (%L);"), values);
var safeQuery = handleValues("INSERT INTO ".concat(table, " (").concat(columns.join(', '), ") VALUES [--REDACTED--];"), handleOptions(options));
var query = (0, _pgFormat["default"])("INSERT INTO ".concat(table, " (").concat(columnsList, ") VALUES (%L);"), values);
var safeQuery = "INSERT INTO ".concat(table, " (").concat(columnsList, ") VALUES [--REDACTED--]];");
return new Promise(function (resolve, reject) {
console.log("Executing insert via : ".concat(safeQuery));
client.query(query, function (err, result) {
Expand Down Expand Up @@ -287,30 +264,28 @@ function insert(table, record, options) {
/**
* Insert many records, using the keys of the first as the column template
* @example
* execute(
* insertMany(table, records, { setNull: false })
* )(state)
* insertMany('users', state => state.data.recordArray);
* @constructor
* @param {string} table - The target table
* @param {function} records - A function that takes state and returns an array of records
* @param {object} options - Optional options argument
* @returns {Operation}
*/


function insertMany(table, records, options) {
function insertMany(table, records) {
return function (state) {
var client = state.client;

try {
var recordData = records(state); // Note: we select the keys of the FIRST object as the canonical template.

var columns = Object.keys(recordData[0]);
var columnsList = columns.join(', ');
var valueSets = recordData.map(function (x) {
return Object.values(x);
});
var query = (0, _pgFormat["default"])("INSERT INTO ".concat(table, " (").concat(columns.join(', '), ") VALUES %L;"), valueSets);
var safeQuery = handleValues("INSERT INTO ".concat(table, " (").concat(columns.join(', '), ") VALUES [--REDACTED--]"), handleOptions(options));
var query = (0, _pgFormat["default"])("INSERT INTO ".concat(table, " (").concat(columnsList, ") VALUES %L;"), valueSets);
var safeQuery = "INSERT INTO ".concat(table, " (").concat(columnsList, ") VALUES [--REDACTED--]];");
return new Promise(function (resolve, reject) {
console.log("Executing insert many via: ".concat(safeQuery)); // execute a query on our database

Expand Down Expand Up @@ -341,37 +316,36 @@ function insertMany(table, records, options) {
* Insert or update a record using ON CONFLICT UPDATE
* @example
* upsert(
* table, // the DB table
* uuid, // a DB column with a unique constraint OR a CONSTRAINT NAME
* record,
* options
* 'users', // the DB table
* 'ON CONSTRAINT users_pkey', // a DB column with a unique constraint OR a CONSTRAINT NAME
* {name: 'Elodie', id: 7}
* )
* @constructor
* @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
* @param {object} options - Optional options argument
* @returns {Operation}
*/


function upsert(table, uuid, record, options) {
function upsert(table, uuid, record) {
return function (state) {
var client = state.client;

try {
var recordData = (0, _languageCommon.expandReferences)(record)(state);
var columns = Object.keys(recordData).sort();
var columnsList = columns.join(', ');
var values = columns.map(function (key) {
return recordData[key];
});
var conflict = uuid.split(' ').length > 1 ? uuid : "(".concat(uuid, ")");
var updateValues = columns.map(function (key) {
return "".concat(key, "=excluded.").concat(key);
}).join(', ');
var insertValues = (0, _pgFormat["default"])("INSERT INTO ".concat(table, " (").concat(columns.join(', '), ") VALUES (%L)"), values);
var query = "".concat(insertValues, "\n ON CONFLICT ").concat(conflict, "\n DO\n UPDATE SET ").concat(updateValues, ";");
var safeQuery = handleValues("INSERT INTO ".concat(table, " (").concat(columns.join(', '), ") VALUES [--REDACTED--]\n ON CONFLICT ").concat(conflict, "\n DO\n UPDATE SET [--REDACTED--];"), handleOptions(options));
var insertValues = (0, _pgFormat["default"])("INSERT INTO ".concat(table, " (").concat(columnsList, ") VALUES (%L)"), values);
var query = "".concat(insertValues, "\n ON CONFLICT ").concat(conflict, "\n DO UPDATE SET ").concat(updateValues, ";");
var safeQuery = "INSERT INTO ".concat(table, " (").concat(columnsList, ") VALUES [--REDACTED--]\n ON CONFLICT ").concat(conflict, "\n DO UPDATE SET ").concat(updateValues, ";");
return new Promise(function (resolve, reject) {
console.log("Executing upsert via : ".concat(safeQuery));
client.query(query, function (err, result) {
Expand Down Expand Up @@ -400,38 +374,36 @@ function upsert(table, uuid, record, options) {
* Insert or update multiple records using ON CONFLICT UPDATE and excluded
* @example
* upsert(
* table, // the DB table
* uuid, // a DB column with a unique constraint OR a CONSTRAINT NAME
* record,
* options
* 'users', // the DB table
* 'email', // a DB column with a unique constraint OR a CONSTRAINT NAME
* state => state.data.usersArray
* )
* @constructor
* @param {string} table - The target table
* @param {string} uuid - The uuid column to determine a matching/existing record
* @param {object} records - A function that takes state and returns an array of records
* @param {object} options - Optional options argument
* @param {function} records - A function that takes state and returns an array of records
* @returns {Operation}
*/


function upsertMany(table, uuid, records, options) {
function upsertMany(table, uuid, records) {
return function (state) {
var client = state.client;

try {
// recordData = array
var recordData = records(state);
var columns = Object.keys(recordData[0]);
var columnsList = columns.join(', ');
var values = recordData.map(function (x) {
return Object.values(x);
});
var conflict = uuid.split(' ').length > 1 ? uuid : "(".concat(uuid, ")");
var updateValues = columns.map(function (key) {
return "".concat(key, "=excluded.").concat(key);
}).join(', ');
var insertValues = (0, _pgFormat["default"])("INSERT INTO ".concat(table, " (").concat(columns.join(', '), ") VALUES %L"), values);
var query = "".concat(insertValues, "\n ON CONFLICT ").concat(conflict, "\n DO\n UPDATE SET ").concat(updateValues, ";");
var safeQuery = handleValues("INSERT INTO ".concat(table, " (").concat(columns.join(', '), ") VALUES [--REDACTED--]\n ON CONFLICT ").concat(conflict, "\n DO\n UPDATE SET [--REDACTED--];"), handleOptions(options));
var insertValues = (0, _pgFormat["default"])("INSERT INTO ".concat(table, " (").concat(columnsList, ") VALUES %L"), values);
var query = "".concat(insertValues, "\n ON CONFLICT ").concat(conflict, "\n DO UPDATE SET ").concat(updateValues, ";");
var safeQuery = "INSERT INTO ".concat(table, " (").concat(columnsList, ") VALUES [--REDACTED--]\n ON CONFLICT ").concat(conflict, "\n DO UPDATE SET ").concat(updateValues, ";");
return new Promise(function (resolve, reject) {
console.log("Executing upsert via : ".concat(safeQuery));
client.query(query, function (err, result) {
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "language-postgresql",
"version": "2.1.0",
"version": "2.1.1",
"description": "A PostgreSQL Language Pack for OpenFn",
"main": "lib/index.js",
"scripts": {
Expand Down
Loading

0 comments on commit 0a44e18

Please sign in to comment.