joystick icon indicating copy to clipboard operation
joystick copied to clipboard

Add an objectToSQL mapper

Open rglover opened this issue 3 years ago • 3 comments

Wish this would have come to mind sooner. The big pain in the butt with SQL is mapping values for queries in your code. Would be nice to have something like this:

import { databases } from '@joystick.js/node';

const [query, values] = databases.objectToSQL('postgresql', {
  column_name: 'VALUE',
  column_name2: 'VALUE',
});

postgresql.query(query, values);

So, this function takes an object where the property names are column names and values are whatever. It spits out the query string for the specified database along with an array of values to pass to the DB driver.

rglover avatar Dec 13 '22 00:12 rglover

Try to find a package to recommend before going ham on this.

rglover avatar Jul 18 '23 03:07 rglover

This just came to mind again w/ this sketch:

sqlify.table('TABLE_NAME').select({
  where: {
    'release_date::date': '< NOW()',
    'available_until::date': '> NOW()',
    'collection_id': 'abc1234'
  },
});

sqlify.table('TABLE_NAME')'.update({
  set: {},
  where: {},
});

sqlify.table('TABLE_NAME').delete({
  where: {},
});

Presumption above is that this would create prepared statements so there was no injection vulnerabilities.

rglover avatar Jul 25 '23 18:07 rglover

Would be most helpful if this generated prepared statements that automated the $1, $2, etc vars. Returns the query and vars so you can run them.

rglover avatar Aug 09 '23 04:08 rglover

This was added here: https://github.com/cheatcode/joystick/blob/canary/node/src/app/databases/sql.js.

It helps you generate a SQL statement along w/ args. Just needs to be doc'd.

rglover avatar Jul 23 '24 21:07 rglover