PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, July 13, 2022

[FIXED] How to Convert String Array into Prisma Select Statement

 July 13, 2022     javascript, node.js, prisma, typescript, web-deployment     No comments   

Issue

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}}}}}})

Solution

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));



Answered By - eol
Answer Checked By - Mary Flores (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing