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

148
Visualizações
How to Convert String Array into Prisma Select Statement

I want to select Prisma columns dynamically, I am getting this from the client:

['id', 'createdAt', 'updatedAt', 'Order.id', 'Order.Item.id', 'Order.Item.desc']

I want to change it to something like this:

{id: true, createdAt: true, updatedAt: true, Order: {select: {id: true, Item: {select: {id: true, desc: true}}}}

so that I can use it in the Prisma query like:

prisma.sales.findMany({where: {id: {_eq: 1}}, select: {id: true, createdAt: true, updatedAt: true, Order: {select: {id: true, Item: {select: {id: true, desc: true}}}}}})
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can build a simple recursive function to build object and populate the nested properties:

const objPaths = ['id', 'createdAt', 'updatedAt', 'Order.id', 'Order.Item.id', 'Order.Item.desc'];


function buildObject(paths) {
    const result = {};
    for (const path of paths) {
        const pathParts = path.split(".");
        if (pathParts.length > 1) {
            populateNested(result, pathParts, 0);
        } else {
            result[path] = true;
        }
    }
    return result;
}

function populateNested(parent, paths, currPathIndex) {
    if (currPathIndex === paths.length - 1) {
        parent[paths[currPathIndex]] = true;
    } else {
        let currObj = {select: {}};
        if (parent[paths[currPathIndex]]) {
            currObj = parent[paths[currPathIndex]];
        }
        parent[paths[currPathIndex]] = currObj;
        populateNested(currObj.select, paths, currPathIndex + 1);
    }

}

console.log(JSON.stringify(buildObject(objPaths), null, 2));

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