Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

extract table name #116

Open
mkupriichuk opened this issue Feb 1, 2024 · 2 comments
Open

extract table name #116

mkupriichuk opened this issue Feb 1, 2024 · 2 comments
Labels
enhancement New feature or request

Comments

@mkupriichuk
Copy link

Hi. It is possible to extract table name from instance?

Something like extractTableName(tCustomer) // 'customer' ?

Thanks

@juanluispaz
Copy link
Owner

I've been thinking about adding some functions that allow us to get information about tables and columns, but I'm unsure what kind of situation it will be used for. Can you describe your use case or ideas about it? How can you take advantage of this information in your application?

I'm really interested in this, but I don't want to go over a theoretical idea; I would like to contrast it to real use cases.

@juanluispaz juanluispaz added the enhancement New feature or request label Feb 5, 2024
@mkupriichuk
Copy link
Author

I use union from multiple tables, and write the result of a common select to another table. The problem is that there may be many unions and I may forget to replace a table when "copy/pasting" a "union part" and it will not cause an error. For example:

const valuesFromSelect =  connection
          .selectFrom(ordersTableOne)
          .select({
            amount: ordersTableOne.amount
          })
         .union(connection.selectFrom(ordersTableTwo)
          .select({
            amount: ordersTableTwo.total
          }))
         .union(connection.selectFrom(ordersTableTwo)
          .select({
            amount: ordersTableTwo.total
          }))
         // another tables

await connection.insertInto(commonOrders).from(valuesFromSelect).executeInsert()

In this example, I can "copy/paste" a "part of union" and forget to change the table, so I don't get the full dataset.

For these cases, I would like to have a "helper class" that, when a union is called, checks if this table has been used before. Somethig like this:

type UnionPart<TTable extends ITableOrViewOf<DB<'Connection'>, any>> =
  DynamicWhereExecutableSelectExpression<
    DB<'Connection'>,
    TTable,
    any,
    any,
    NoTableOrViewRequiredView<any>,
    never
  >;

class WithPreventUnionDuplicate {
  static create() {
    return new WithPreventUnionDuplicate();
  }

  constructor() {
    this.usedTables = new Set();
  }

  callUnionPart<TTable extends ITableOrViewOf<DB<'Connection'>, any>>(callpartFn: () => UnionPart<TTable>, tableName: TTable) {
    return this.withRestrictPartDuplicate(callpartFn, tableName);
  }

  private withRestrictPartDuplicate<TTable extends ITableOrViewOf<DB<'Connection'>, any>>(cb: () => UnionPart<TTable>, tableName: TTable): UnionPart<TTable> {
    const stringedTableName = extractTableName(tableName);
    assert(
      !this.usedTables.has(stringedTableName),
      `Fields from ${stringedTableName} already exist on union. Please, check the query`
    );
    this.usedTables.add(stringedTableName);
    return cb();
  }

  private usedTables: Set<string>;
}

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants