porsager / porsager/postgres

What would be the right type to pass an object?

Open
#625 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

help wanted Typescript
Dominant language
JavaScript
Stars
8.7k
Forks
374
Avg merge
11d 16h
Merged PRs (30d)
1

Description

Hello,

in my table "account" the column "contact" is of the type jsonb. Using the following interface, I get an TS Error:

error: TS2769 [ERROR]: No overload matches this call.
Overload 1 of 2, '(first: TemplateStringsArray, ...rest: never): PendingQuery<Row[]>', gave the following error.
Argument of type 'string' is not assignable to parameter of type 'never'. Overload 2 of 2, '(template: TemplateStringsArray, ...parameters: readonly ParameterOrFragment[]): PendingQuery<Row[]>', gave the following error.
Argument of type 'object' is not assignable to parameter of type 'ParameterOrFragment'.
return await sql`

I have two questions:

  1. Is it correct to pass an object like this for jsonb?
  2. If yes, what could I do to avoid the TS error?

Thank you very much!

interface AccountData {
  owner: string;
  email: string;
  contact: object;
  password: string;
}

async function insertAccount(
  { owner, email, contact, password }: AccountData,
) {
  return await sql`
    INSERT INTO public.account (owner, email, contact, hashed_password)
    VALUES (${owner}, ${email}, ${contact}, crypt(${password}, gen_salt('md5')))
  `;
}

const dataJane = {
  owner: "Jane Doe",
  email: "janedoe@example.com",
  contact: { "phone": "12345" },
  password: "password123",
};

await insertAccount(dataJane);

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the sql tagged-template call and the ParameterOrFragment type referenced in the error. Check how Postgres.js represents jsonb parameters and whether the AccountData example reproduces the typing problem. Done means the supported object-passing approach and a TypeScript-safe example are documented or covered by a focused test.

Written by the indexing model from the issue text.

Assessment

Tech stack
nodejs, postgresql, typescript
Domain
backend, databases
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
32/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.