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

Deno support #262

Closed
mcchrish opened this issue May 22, 2021 · 6 comments
Closed

Deno support #262

mcchrish opened this issue May 22, 2021 · 6 comments

Comments

@mcchrish
Copy link

Pgtyped + Deno in theory sounds like a great match but I do wonder how easy it is to make this lib compatible with Deno runtime.

Luckily, the most developed deno pg client so far is based on the most used node pg client. https://deno-postgres.com

@adelsz
Copy link
Owner

adelsz commented Jun 9, 2021

This would be great, but I am not sure how much effort it would be to make it Deno compatible without publishing a new library.

@adelsz
Copy link
Owner

adelsz commented Apr 27, 2022

Closing, Deno is a non priority for now.

@adelsz adelsz closed this as completed Apr 27, 2022
@alarbada
Copy link

For those poor souls that are looking into this:

Just add this into your deno.jsonc

    "imports": {
        "@pgtyped/runtime": "npm:@pgtyped/[email protected]"
    }

This will remap the imports, and deno will be able to correctly fetch teh @pgtyped/runtime dependency.

And, if you want to use the deno postgres client, this works for me:

import { Client } from 'https://deno.land/x/[email protected]/mod.ts'

const client = new Client({
    // ...
})
await client.connect()

const db = {
    query(sql: string, bindings: any[]): Promise<{ rows: any[]}> {
        return client.queryArray(sql, bindings)
    }
}

/// later
const results = await queries.getUserOrSomething.run({ user_id: 0 }, db)

and that's it!

@alarbada
Copy link

alarbada commented Feb 11, 2023

I just discovered this, so maybe there's a caveat? If it works, solving this issue would be as simple as mentioning in the docs that you should modify the deno.jsonc file.

@lpil
Copy link

lpil commented Mar 26, 2023

Thank you! It would be fab if we could get this into the documenation

@SebastienGllmt
Copy link
Contributor

SebastienGllmt commented Nov 22, 2024

Changes required to get codegen working

Unfortunately pgtyped 2.0 broke support for Deno because it started using Piscina which internally uses a nodejs perf_hook which is not yet supported by deno

There are two ways to solve this:

  1. Wait for Deno to add the missing function to their node API implementation
  2. Set recordTiming: false in the Piscina constructor here (requires pgtyped author to make this change)

Note that solution (2) still works as-is even if the version gets upgraded by #593 . You can see the option is the same in the latest Piscina version here

I can confirm locally that if I make this change, pgtyped codegen work with Deno 2👍

Other changes required at runtime

When it comes to actually using the code that was generated, there is another issue you need to fix: pgtyped uses the Buffer global variable provided by nodejs which isn't available in Deno. Fortunately, with Deno 2.1, this can be fixed easily:

  1. Add the new --unstable-node-globals option
  2. Add the following global type declaration to your code
declare global {
  type Buffer = typeof import("node:buffer").Buffer;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants