Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support JSONB columns in sql.insert's type definition #3763

Open
canastro opened this issue Oct 9, 2024 · 2 comments
Open

Support JSONB columns in sql.insert's type definition #3763

canastro opened this issue Oct 9, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@canastro
Copy link

canastro commented Oct 9, 2024

What version of Effect is running?

3.8.4

What steps can reproduce the bug?

const SaveItem = SqlSchema.void({
  Request: S.Struct({ jsonColumn: S.Struct({ data: S.String }) }),
  execute: (value) => sql`INSERT INTO table ${sql.insert(value)} `,
});

What is the expected behavior?

TS allows complex types in sql.insert (to be inserted into JSONB columns in postgres)

What do you see instead?

The insert actually works as expected, is just the Typechecking that fails with the following error:

No overload matches this call.
  Overload 1 of 2, '(value: readonly Record<string, Primitive | Fragment | undefined>[]): RecordInsertHelper', gave the following error.
    Argument of type '{ readonly jsonColumn: { readonly data: string; }; }' is not assignable to parameter of type 'readonly Record<string, Primitive | Fragment | undefined>[]'.
      Type '{ readonly jsonColumn: { readonly data: string; }; }' is missing the following properties from type 'readonly Record<string, Primitive | Fragment | undefined>[]': length, concat, join, slice, and 20 more.
  Overload 2 of 2, '(value: Record<string, Primitive | Fragment | undefined>): RecordInsertHelper', gave the following error.
    Argument of type '{ readonly jsonColumn: { readonly data: string; }; }' is not assignable to parameter of type 'Record<string, Primitive | Fragment | undefined>'.
      Property 'jsonColumn' is incompatible with index signature.
        Type '{ readonly data: string; }' is not assignable to type 'Primitive | Fragment | undefined'.ts(2769)

Additional information

No response

@canastro canastro added the bug Something isn't working label Oct 9, 2024
@tim-smart
Copy link
Contributor

I don't think it will work for every client though. Which one are you using?

@canastro
Copy link
Author

canastro commented Oct 10, 2024

Yeah, I assume that the fact that we use sql.insert from the generic SQL package will make it hard so support this specific requirement from Postgres.

This is how I create the client and add it to my Runtime:

import { PgClient } from '@effect/sql-pg';

const SqlLive = PgClient.layer({
	url: Config.redacted('DB_CONNECTION_STRING'),
	transformQueryNames: Config.succeed(STR.camelToSnake),
	transformResultNames: Config.succeed(STR.snakeToCamel),
});

But then I just use the generic SQLClient interface

import { SqlClient } from '@effect/sql';

export const makeAssetRepository = Effect.gen(function* () {
  const sql = yield* SqlClient.SqlClient;

  const SaveItem = SqlSchema.void({
    ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants