Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save zadokdaniel/ea4dd31e13957c717ffb26e238f25173 to your computer and use it in GitHub Desktop.
Save zadokdaniel/ea4dd31e13957c717ffb26e238f25173 to your computer and use it in GitHub Desktop.
knex-types interfaces into objects for easily use with joins
function getInterfacesNamesAndProperties(interfacesSourceCodeTXT) {
const interfaceRegex =
/export\s*type\s*(?<interfaceName>\w+)\s*=\s{(\s*?.*?)*?};/g;
const keyRegex = /(?<key>\w+):\s*[\w |]*;/g;
let results = {};
for (const interface of txt.matchAll(interfaceRegex)) {
results[interface.groups.interfaceName] = Array.from(
interface[0].matchAll(keyRegex)
).map((result) => result.groups.key);
}
return results;
}
const camelToSnakeCase = (str) =>
`${str[0].toLowerCase()}${str.slice(1)}`.replace(
/[A-Z]/g,
(letter) => `_${letter.toLowerCase()}`
);
function getAsObject(name, properties) {
const sc_name = camelToSnakeCase(name);
return `
export const ${name} = {
${properties
.map(
(property) =>
`${property}: "${sc_name}.${camelToSnakeCase(property)}"`
)
.join(",")}
toString() {
return "${sc_name}"
}
};
`;
}
function generateObjectsFromInterfaces() {
const data = getInterfacesNamesAndProperties(txt);
let result = "";
for (const key in data) {
result += getAsObject(key, data[key]);
}
return result;
}
@zadokdaniel
Copy link
Author

import crmDB from "../dbs/crmDB";

import { CrmDBTables } from "../dbs/crmDBTypes";
const { Leads, LeadMemberships, Branches, Districts } = CrmDBTables;

export const votersControllert = () => {
  return crmDB(Leads)
    .select(
      `${Leads}.id`,
      `${Leads}.payment_status_id`,
      `${Leads}.support_rate`,
      `${Branches}.name`,
      `${Branches}.id`,
      `${Branches}.district_id`,
      `${Districts}.name`
    )
    .leftJoin(LeadMemberships, `${LeadMemberships}.lead_id`, `${Leads}.id`)
    .leftJoin(Branches, `${Branches}.id`, `${Leads}.branch_id`)
    .leftJoin(Districts, `${Districts}.id`, `${Branches}.district_id`)
    .whereIn("payment_status_id", [1])
    .finally(console.log);
};

BECOMES

import crmDB from "../dbs/crmDB";
import {
  Leads,
  LeadMemberships,
  Branches,
  Districts,
} from "../dbs/crmDBSchema";

export const votersControllert = () => {
  return crmDB(Leads.toString())
    .select(
      Leads.id,
      Leads.payment_status_id,
      Leads.support_rate,
      Branches.name,
      Branches.id,
      Branches.district_id,
      Districts.name
    )
    .leftJoin(LeadMemberships.toString(), LeadMemberships.lead_id, Leads.id)
    .leftJoin(Branches.toString(), Branches.id, Leads.branch_id)
    .leftJoin(Districts.toString(), Districts.id, Branches.district_id)
    .whereIn(Leads.payment_status_id, [1])
    .finally(console.log);
};

@zadokdaniel
Copy link
Author

zadokdaniel commented Jul 30, 2022

results

  export type PaymentMethods = {
    id: number;
    name: string | null;
    created_at: Date | null;
    updated_at: Date | null;
    display_name: string | null;
    deleted_at: Date | null;
  };

BECOMES

export const PaymentMethods = {
  id: "payment_methods.id",
  name: "payment_methods.name",
  created_at: "payment_methods.created_at",
  updated_at: "payment_methods.updated_at",
  display_name: "payment_methods.display_name",
  deleted_at: "payment_methods.deleted_at",
  toString() {
    return "payment_methods";
  },
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment