async function buildTempTable(name, data, dropOnCommit = false, schema, client) {
  //validate input
  if(data.length === undefined) {
    throw new Error('data needs to be an array of identical objects.')
  }

  let fields 

  if(typeof(schema) === 'object' && schema !== null) {
    fields = Object.keys(schema).sort()
  } else if(data.length) {
    fields = Object.keys(data[0]).sort()
  } else {
    throw new Error('Data has length 0 and no schema was provided')
  }

  let fieldProblems = fields.filter(field=>!(fieldValidationPattern.test(field)))

  if(fieldProblems.length) {
    throw new Error('each field in data needs to begin with a letter and then have 0 or more letters and numbers')
  }

  if(!client) {
    throw new Error('buildTempTable: client is required')
  }

  //build query string
  let fieldBuildString = fields.reduce((str, field) => str += `${field} TEXT, `, '') 
  fieldBuildString = fieldBuildString.slice(0, -2)//remove the last comma
  let buildTableQuery = `CREATE TEMP TABLE ${name} 
  (
    ${fieldBuildString}
  )`

  if(dropOnCommit) {
    buildTableQuery += 'ON COMMIT DROP;'
  } else {
    buildTableQuery += ';'
  }

  await client.query(buildTableQuery)

  //load data

  let populateQuery = `INSERT INTO ${name}
    SELECT * 
    FROM JSON_POPULATE_RECORDSET(null::${name}, $1);`

  await client.query(populateQuery, [JSON.stringify(data)])
}
/**
 * a function that takes an array of identical objects and creates a temp table on the server.
 * It hasn't been optimized for speed, but is locked down enough that it should be secure
 */
a function that takes an array of identical objects and creates a temp table on the server.
It hasn't been optimized for speed, but is locked down enough that it should be secure
a function that takes an array of identical objects and creates a temp table on the server . It hasn ' t been optimized for speed but is locked down enough that it should be secure
async function buildTempTable(name, data, dropOnCommit = false, schema, client) {
  if(data.length === undefined) {
    throw new Error('data needs to be an array of identical objects.')
  }
  let fields 
  if(typeof(schema) === 'object' && schema !== null) {
    fields = Object.keys(schema).sort()
  } else if(data.length) {
    fields = Object.keys(data[0]).sort()
  } else {
    throw new Error('Data has length 0 and no schema was provided')
  }
  let fieldProblems = fields.filter(field=>!(fieldValidationPattern.test(field)))
  if(fieldProblems.length) {
    throw new Error('each field in data needs to begin with a letter and then have 0 or more letters and numbers')
  }
  if(!client) {
    throw new Error('buildTempTable: client is required')
  }
  let fieldBuildString = fields.reduce((str, field) => str += `${field} TEXT, `, '') 
  fieldBuildString = fieldBuildString.slice(0, -2)
  let buildTableQuery = `CREATE TEMP TABLE ${name} 
  (
    ${fieldBuildString}
  )`
  if(dropOnCommit) {
    buildTableQuery += 'ON COMMIT DROP;'
  } else {
    buildTableQuery += ';'
  }
  await client.query(buildTableQuery)
  let populateQuery = `INSERT INTO ${name}
    SELECT * 
    FROM JSON_POPULATE_RECORDSET(null::${name}, $1);`
  await client.query(populateQuery, [JSON.stringify(data)])
}
async function buildTempTable ( name , data , dropOnCommit = false , schema , client ) { if ( data . length === undefined ) { throw new Error ( 'data needs to be an array of identical objects.' ) } let fields if ( typeof ( schema ) === 'object' && schema !== null ) { fields = Object . keys ( schema ) . sort ( ) } else if ( data . length ) { fields = Object . keys ( data [ 0 ] ) . sort ( ) } else { throw new Error ( 'Data has length 0 and no schema was provided' ) } let fieldProblems = fields . filter ( field => ! ( fieldValidationPattern . test ( field ) ) ) if ( fieldProblems . length ) { throw new Error ( 'each field in data needs to begin with a letter and then have 0 or more letters and numbers' ) } if ( ! client ) { throw new Error ( 'buildTempTable: client is required' ) } let fieldBuildString = fields . reduce ( ( str , field ) => str += ` ${ field } ` , '' ) fieldBuildString = fieldBuildString . slice ( 0 , - 2 ) let buildTableQuery = ` ${ name } ${ fieldBuildString } ` if ( dropOnCommit ) { buildTableQuery += 'ON COMMIT DROP;' } else { buildTableQuery += ';' } await client . query ( buildTableQuery ) let populateQuery = ` ${ name } ${ name } ` await client . query ( populateQuery , [ JSON . stringify ( data ) ] ) }
a function that takes an array of identical objects and creates a temp table on the server.
a function that takes an array of identical objects and creates a temp table on the server .
//validate input //build query string //remove the last comma //load data