#Get value of function parameter

13 messages · Page 1 of 1 (latest)

rugged geyser
#

Hello! by the increase of complexity of my database, i'm remaking my database code, this is my new typage:

import { ErrorsTS, GuildTS, MessageStructure, UserTS, EventsTS, MessageTS } from '../../../interfaces/Database';

type Tables = 'errors' | 'events' | 'guilds' | 'message_structures' | 'messages' | 'users';
type DBTables<table extends Tables | void> =
    table extends 'errors' ? ErrorsTS :
    table extends 'events' ? EventsTS :
    table extends 'guilds' ? GuildTS :
    table extends 'message_structures' ? MessageStructure :
    table extends 'messages' ? MessageTS :
    table extends 'users' ? UserTS :
    Tables

export { DBTables }

So, for this, i'm creating a custom pg class

import { Client, ClientConfig } from 'pg';
import { DBTables } from './types';
import { SQLClient } from './Main';

class CustomClient extends Client {
    constructor(config: string | ClientConfig | undefined) {
        super(config);
    }

    async Insert(table: DBTables<void>, values: DBTables<typeof table>)  {
        return await SQLClient.query(
            `INSERT INTO ${table} (${Object.keys(values)}) VALUES (${Object.keys(values).map((x, i) => `$${i + 1}`)})`,
            Object.values(values)
        )
    }
}

export default CustomClient;

But the parameter values of CustomClient.Insert function have the any value 😦 how can i solve this?

rugged furnace
#

typeof table is just DBTables<void>, you maybe want a generic there?

rugged geyser
#

i want to get the value of the parameter table, like SQLClient.Insert("errors", { /* typage of errors table */ })

rugged furnace
#

that would be a job for a generic

#

parameters don't have values, arguments have values

#

you define a parameter list in the function, that isn't aware of arguments

#

you need generics to capture the types of those arguments

#

!hb generics

thorn muralBOT
rugged geyser
#

are you talking about this?

    async Insert<Table extends DBTables<void>>(table: Table, values: DBTables<Table>, options?: { return: boolean })  {

        let str = `INSERT INTO ${table} (${Object.keys(values)}) VALUES (${Object.keys(values).map((x, i) => `$${i + 1}`)})`;

        if (options?.return) str += " RETURNING *"; // i will improve this later

        return await SQLClient.query(str, Object.values(values))
    }

i thought about this, but i want to expunge repetitions SQLClient.Insert<"errors">("errors") (i need to repeat errors)... this is the only way?

rugged furnace
#

why do you need to repeat "errors"?

#

generics can be inferred