Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

180
Visualizações
JS Method Chaining Controlling Flow

I have created a method in Typescript which utilizes method chaining to create a simple sql query builder. It implements the basic query methods. What I would like to do is not allow someone to call the offset method for example if they havent called limit previously or calling where twice. I know I can implement something via a map which stores the previously called methods but I was wondering if there is a clean solution to this problem. My current query builder method is

public qb = () =>
{
    let query = this.SELECT;
    const api = {
        where: (key: string, value: number|string) =>
        {
            query = sql`${query} WHERE ${sql.identifier([key])} = ${value}`;
            return api;
        },
        and: (key: string, value: number|string) =>
        {
            query = sql`${query} AND ${sql.identifier([key])} = ${value}`;
            return api;
        },
        orderBy: (key: string, order: 'ASC'|'DESC') =>
        {
            query = sql`${query} ORDER BY ${sql.identifier([key])} ${order}`;
            return api;
        },
        limit: (limit: number) =>
        {
            query = sql`${query} LIMIT ${limit}`;
            return api;
        },
        offset: (offset: number) =>
        {
            query = sql`${query} OFFSET ${offset}`;
            return api;
        },
        get: async () => this.rowMapper(await this.database.query(query)),
    };
    return api;
};

Is there a nice way to force the flow of the method chaining?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The solution I ended up going with is creating multiple objects which have different methods inside of them. The solution is not the cleanest but I could not find a better one

public qb = () =>
{
    let query = this.SELECT;

    const get = async () => this.rowMapper(await this.database.query(query));
    const limit = (_limit: number) =>
    {
        query = sql`${query} LIMIT ${_limit}`;
        return {
            offset: (offset: number) =>
            {
                query = sql`${query} OFFSET ${offset}`;
                return { get };
            },
            get,
        };
    };

    const api2 = {
        and: (key: string, value: number|string) =>
        {
            query = sql`${query} AND ${sql.identifier([key])} = ${value}`;
            return api2;
        },
        orderBy: (key: string, order: 'ASC'|'DESC') =>
        {
            query = sql`${query} ORDER BY ${sql.identifier([key])} ${order}`;
            return {
                limit,
                get,
            };
        },
        limit,
        get,
    };
    const api = {
        where: (key: string, value: number|string) =>
        {
            query = sql`${query} WHERE ${sql.identifier([key])} = ${value}`;
            return api2;
        },

    };
    return api;
};
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda