ehmpathy / sql-code-generator

Generate code from your SQL schema and queries for type safety and development speed.
MIT License
28 stars 3 forks source link

dedupe query function definition body - extract repeated logic to generic fn #16

Closed uladkasach closed 4 years ago

uladkasach commented 4 years ago
// client method for query 'find_geocode_by_id'
export const sqlQueryFindGeocodeById = async ({
  dbExecute,
  logDebug,
  input,
}: {
  dbExecute: DatabaseExecuteCommand;
  logDebug: LogMethod;
  input: SqlQueryFindGeocodeByIdInput;
}): Promise<SqlQueryFindGeocodeByIdOutput[]> => {
  // 1. define the query with yesql
  const { sql: preparedSql, values: preparedValues } = prepare(sqlQueryFindGeocodeByIdSql)(input);

  // 2. log that we're running the request
  logDebug('sqlQueryFindGeocodeById.input', { input });

  // 3. execute the query
  const [output] = await dbExecute({ sql: preparedSql, values: preparedValues });

  // 4. log that we've executed the request
  logDebug('sqlQueryFindGeocodeById.output', { output });

  // 5. return the output
  return output;
};

to

import { DatabaseExecuteCommand, LogMethod } from 'sql-code-generator';

// client method for query 'find_geocode_by_id'
export const sqlQueryFindGeocodeById = async ({
  dbExecute,
  logDebug,
  input,
}: {
  dbExecute: DatabaseExecuteCommand;
  logDebug: LogMethod;
  input: SqlQueryFindGeocodeByIdInput;
}): Promise<SqlQueryFindGeocodeByIdOutput[]> => mysqlBestPracticesExecute({ name: 'sqlQueryFindGeocodeById', sql: sqlQueryFindGeocodeByIdSql, input })

where

const mysqlBestPracticesExecute = ({ name: string, sql: string, input: any }) => {
  // 1. define the query with yesql
  const { sql: preparedSql, values: preparedValues } = prepare(sqlQueryFindGeocodeByIdSql)(input);

  // 2. log that we're running the request
  logDebug(`${name}.input`, { input });

  // 3. execute the query
  const [output] = await dbExecute({ sql: preparedSql, values: preparedValues });

  // 4. log that we've executed the request
  logDebug(`${name}.output`, { output });

  // 5. return the output
  return output;
}; 
uladkasach commented 4 years ago

note: generate the mysqlBestPracticesExecute - to make it easy to edit for users, or import it?

uladkasach commented 4 years ago

not importing is more in line with the spirit of generator.

uladkasach commented 4 years ago

const executeQueryWithBestPracticesForMysql = async ({
  dbExecute,
  logDebug,
  name,
  sql,
  input,
}: {
  dbExecute: DatabaseExecuteCommand;
  logDebug: LogMethod;
  name: string;
  sql: string;
  input: any;
}) => {
  // 1. define the query with yesql
  const { sql: preparedSql, values: preparedValues } = prepare(sql)(input);

  // 2. log that we're running the request
  logDebug(`${name}.input`, { input });

  // 3. execute the query
  const [output] = await dbExecute({ sql: preparedSql, values: preparedValues });

  // 4. log that we've executed the request
  logDebug(`${name}.output`, { output });

  // 5. return the output
  return output;
};
uladkasach commented 4 years ago

but importing might help prevent the changing generated code problem.

plus, it is really easy to swap out for your own - so it wouldn't be a blocker...

uladkasach commented 4 years ago

significant con of "importing" - it would then have to be a "dependency" instead of a "dev dependency". so, no imports