How to use @taggedTemplate generate js with tagged template literals

I am using Rescript v11 to bind the Postgress.js API. Since returned by the “make” binding is a tagged Template function , I cannot use @taggedTemplate to descript it. The generated js file is not with tagged Template. How can I use @taggedTemplate so that gen the js code with tagged Template ?

[GitHub - porsager/postgres: Postgres.js]
(GitHub - porsager/postgres: Postgres.js - The Fastest full featured PostgreSQL client for Node.js, Deno, Bun and CloudFlare)

bind

type t<'sqlParam, 'data> = (array<string>, array<'sqlParam>) => promise<result<'data>>
@module("postgres") external make: (~url: string, ~config: config=?) => t<'sqlParam, 'data> = "default"

usage

let sql = Postgresql.make(~url="postgres://xxx:xxxx@localhost:5432/xxxxxx")
let abc = "abc"
let result: Postgresql.result<userInfo> = await sql`select name, sigup_date from user_tbl where name=${ abc }`

output:

var result = await sql([
              "select name, sigup_date from user_tbl where name=",
              ""
            ], ["abc"]);

Hi @liang, sorry for the late reply but there’s no easy way to do this today, we should support such tags (like @taggedTemplate or @variadic) in function parameters which we don’t today. It was too much of a pain originally, but I’ll take a look at it now that v12 is uncurried only and has a cleaner AST.

1 Like

@tsnobip Thank for you reply, I’m trying to solve this problem with other way these days, but it doesn’t work well

First I patch the code (compile to PostgresqlPatch.js)

%%raw(`
import Postgres from "postgres"

export default myPostgres;

function myPostgres(a, b) {

    var sql = Postgres(a, b);
    Object.assign(sql, {safeSql: sql})
    return sql;
}

`)

Then bind the path code use the object function

type t
@module("./PostgresqlPatch.js") external make: (~url: string, ~config: config=?) => t = "default"

@taggedTemplate @send
external safeSql: (t, array<string>, array<'sqlParam>) => promise<result<'data>> = "safeSql"

Then call object function:

let abc = "name"
    let result: Postgresql.result<userInfo> = await sql->Postgresql.safeSql`select name, sigup_date from user_tbl where name=${ abc }`

output:

var result = await sql.safeSql([
              "select name, sigup_date from user_tbl where name=",
              ""
            ], "name");

Is it possible to consider ways to support object functions in the future?
Thanks.

what do you mean by object functions?

In the meanwhile, the easiest solution by far is just to initialize your sql function in raw js and bind to it:

%%raw(`import Postgres from "postgres"
let sql = Postgres("postgres://xxx:xxxx@localhost:5432/xxxxxx")`)
@taggedTemplate
external sql: (array<string>, array<'sqlParam>) => promise<'data> = "sql"

let foo = sql`hello`

Playground link