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

Commit

Permalink
Merge pull request #21 from OpenFn/unique_constraint
Browse files Browse the repository at this point in the history
Add unique constraint on columns
  • Loading branch information
taylordowns2000 authored Nov 23, 2020
2 parents f45735f + 83053eb commit 4b1de0f
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 33 deletions.
46 changes: 30 additions & 16 deletions lib/Adaptor.js
Original file line number Diff line number Diff line change
Expand Up @@ -401,18 +401,18 @@ function upsertMany(table, uuid, records, options) {
* @example
* describeTable('table_name')
* @constructor
* @param {string} table - The name of the table to describe
* @param {string} tableName - The name of the table to describe
* @param {object} options - Optional options argument
* @returns {Operation}
*/


function describeTable(table, options) {
function describeTable(tableName, options) {
return function (state) {
var client = state.client;

try {
var query = "SELECT column_name, udt_name, is_nullable\n FROM information_schema.columns\n WHERE table_name='".concat(table, "';");
var query = "SELECT column_name, udt_name, is_nullable\n FROM information_schema.columns\n WHERE table_name='".concat(tableName, "';");
console.log('Preparing to describle table via:', query);
return queryHandler(state, query, options);
} catch (e) {
Expand All @@ -425,25 +425,32 @@ function describeTable(table, options) {
* Create a table in database when given a form definition and a table_name.
* @public
* @example
* insertTable('table_name', state => state.data.koboColumns)
* 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
* })
* ));
* @constructor
* @param {string} table - The new table to create
* @param {function} records - An array of form columns
* @param {string} tableName - The name of the table to create
* @param {function} columns - An array of form columns
* @param {object} options - Optional options argument
* @returns {Operation}
*/


function insertTable(table, records, options) {
function insertTable(tableName, columns, options) {
return function (state) {
var client = state.client;

try {
var recordData = records(state);
var recordData = columns(state);
var structureData = recordData.map(function (x) {
return "".concat(x.name, " ").concat(x.type, " ").concat(x.required ? 'NOT NULL' : '');
return "".concat(x.name, " ").concat(x.type, " ").concat(x.unique ? 'UNIQUE' : '', " ").concat(x.required ? 'NOT NULL' : '');
}).join(', ');
var query = "CREATE TABLE ".concat(table, " (\n ").concat(structureData, "\n );");
var query = "CREATE TABLE ".concat(tableName, " (\n ").concat(structureData, "\n );");
console.log('Preparing to create table via:', query);
return queryHandler(state, query, options);
} catch (e) {
Expand All @@ -456,25 +463,32 @@ function insertTable(table, records, options) {
* Alter an existing table in the database.
* @public
* @example
* modifyTable('table_name', state => state.data.koboColumns)
* 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
* })
* ));
* @constructor
* @param {string} table - The name of the table to alter
* @param {function} records - An array of form columns
* @param {string} tableName - The name of the table to alter
* @param {function} columns - An array of form columns
* @param {object} options - Optional options argument
* @returns {Operation}
*/


function modifyTable(table, records, options) {
function modifyTable(tableName, columns, options) {
return function (state) {
var client = state.client;

try {
var recordData = records(state);
var recordData = columns(state);
var structureData = recordData.map(function (x) {
return "ADD COLUMN ".concat(x.name, " ").concat(x.type, " ").concat(x.required ? 'NOT NULL' : '');
}).join(', ');
var query = "ALTER TABLE ".concat(table, "\n ").concat(structureData, "\n ;");
var query = "ALTER TABLE ".concat(tableName, "\n ").concat(structureData, "\n ;");
console.log('Preparing to modify table via:', query);
return queryHandler(state, query, options);
} catch (e) {
Expand Down
52 changes: 35 additions & 17 deletions src/Adaptor.js
Original file line number Diff line number Diff line change
Expand Up @@ -329,18 +329,18 @@ export function upsertMany(table, uuid, records, options) {
* @example
* describeTable('table_name')
* @constructor
* @param {string} table - The name of the table to describe
* @param {string} tableName - The name of the table to describe
* @param {object} options - Optional options argument
* @returns {Operation}
*/
export function describeTable(table, options) {
export function describeTable(tableName, options) {
return state => {
let { client } = state;

try {
const query = `SELECT column_name, udt_name, is_nullable
FROM information_schema.columns
WHERE table_name='${table}';`;
WHERE table_name='${tableName}';`;

console.log('Preparing to describle table via:', query);
return queryHandler(state, query, options);
Expand All @@ -355,24 +355,35 @@ export function describeTable(table, options) {
* Create a table in database when given a form definition and a table_name.
* @public
* @example
* insertTable('table_name', state => state.data.koboColumns)
* 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
* })
* ));
* @constructor
* @param {string} table - The new table to create
* @param {function} records - An array of form columns
* @param {string} tableName - The name of the table to create
* @param {function} columns - An array of form columns
* @param {object} options - Optional options argument
* @returns {Operation}
*/
export function insertTable(table, records, options) {
export function insertTable(tableName, columns, options) {
return state => {
let { client } = state;

try {
const recordData = records(state);
const recordData = columns(state);
const structureData = recordData
.map(x => `${x.name} ${x.type} ${x.required ? 'NOT NULL' : ''}`)
.map(
x =>
`${x.name} ${x.type} ${x.unique ? 'UNIQUE' : ''} ${
x.required ? 'NOT NULL' : ''
}`
)
.join(', ');

const query = `CREATE TABLE ${table} (
const query = `CREATE TABLE ${tableName} (
${structureData}
);`;

Expand All @@ -389,26 +400,33 @@ export function insertTable(table, records, options) {
* Alter an existing table in the database.
* @public
* @example
* modifyTable('table_name', state => state.data.koboColumns)
* 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
* })
* ));
* @constructor
* @param {string} table - The name of the table to alter
* @param {function} records - An array of form columns
* @param {string} tableName - The name of the table to alter
* @param {function} columns - An array of form columns
* @param {object} options - Optional options argument
* @returns {Operation}
*/
export function modifyTable(table, records, options) {
export function modifyTable(tableName, columns, options) {
return state => {
let { client } = state;

try {
const recordData = records(state);
const recordData = columns(state);
const structureData = recordData
.map(
x => `ADD COLUMN ${x.name} ${x.type} ${x.required ? 'NOT NULL' : ''}`
)
.join(', ');

const query = `ALTER TABLE ${table}
const query = `ALTER TABLE ${tableName}
${structureData}
;`;

Expand Down

0 comments on commit 4b1de0f

Please sign in to comment.