drizzle-orm icon indicating copy to clipboard operation
drizzle-orm copied to clipboard

Add Deno support

Open dankochetov opened this issue 1 year ago • 16 comments

Currently, the ORM part should work fine with Deno since it's runtime-agnostic, but the migrator won't work since it relies on Node API.

dankochetov avatar Mar 11 '23 11:03 dankochetov

An example using deno in the docs would be nice as well

guy-borderless avatar Apr 08 '23 09:04 guy-borderless

The problem is that drizzle is looking for drizzle-orm in package json? drizzle-kit introspect:pg --out=migrations/ --connectionString=postgresql://postgres:...

error please install required packages: 'drizzle-orm'

guy-borderless avatar Apr 15 '23 08:04 guy-borderless

Yeah im trying this out and coming across issues where drizzle-orm is depending on postgres drivers that just don't exist.

error: Uncaught TypeError: Cannot read properties of undefined (reading 'setTypeParser') at m.initMappers

it tries to pull in postgres but can't find it:

import * as t from "pg";

however I don't really know where this could be and the replacements i've looked at don't seem to work properly - though this may be related to an unfamiliarity with import map scopes.

this is with an import_map.json that includes:

"drizzle-orm": "npm:drizzle-orm", "drizzle-orm/version": "npm:drizzle-orm/version", "drizzle-orm/pg-core": "npm:drizzle-orm/pg-core", "drizzle-orm/node-postgres": "npm:drizzle-orm/node-postgres",

swiftsword94 avatar Apr 25 '23 19:04 swiftsword94

seems it that was resolved with #505 .

to consolidate:

use the below import map:

"drizzle-orm": "npm:drizzle-orm", "drizzle-orm/pg-core": "npm:drizzle-orm/pg-core", "pg-pool": "npm:pg-pool",

and use the below to create a db:

import { drizzle } from "drizzle-orm/node-postgres"; import Pool from "pg-pool"; const pool = new Pool({ fill the connection details }); export const db = drizzle(pool);

im not sure if there is a better way to pull pool in from pg, but for some reason im getting errors saying its not found.

swiftsword94 avatar Apr 26 '23 00:04 swiftsword94

seems it that was resolved with #505 .

to consolidate:

use the below import map:

"drizzle-orm": "npm:drizzle-orm", "drizzle-orm/pg-core": "npm:drizzle-orm/pg-core", "pg-pool": "npm:pg-pool",

and use the below to create a db:

import { drizzle } from "drizzle-orm/node-postgres"; import Pool from "pg-pool"; const pool = new Pool({ fill the connection details }); export const db = drizzle(pool);

im not sure if there is a better way to pull pool in from pg, but for some reason im getting errors saying its not found.

hopefully we'll have something better for an ESM import+ drizzle kit

guy-borderless avatar Apr 26 '23 08:04 guy-borderless

Also found another set of working imports for drizzle-orm which works with better typing:

import_map:

"drizzle-orm": "https://esm.sh/v117/*[email protected]",
"drizzle-orm/": "https://esm.sh/v117/*[email protected]/",
"postgres": "https://deno.land/x/[email protected]/mod.js"

code:

const post = postgres(Deno.env.get("DIRECT_URL") ?? "", {
  max: 1,
});

export const db: PostgresJsDatabase = drizzle(post);

await migrate(db, { migrationsFolder: "./migrations-folder" });

besides the point though, I would also like to see drizzle kit work too with deno as the only other alternative i have is by running node for drizzle-kit and deno for actual queries/modifications

swiftsword94 avatar Apr 29 '23 14:04 swiftsword94

Also found another set of working imports for drizzle-orm which works with better typing:

import_map:

"drizzle-orm": "https://esm.sh/v117/*[email protected]",
"drizzle-orm/": "https://esm.sh/v117/*[email protected]/",
"postgres": "https://deno.land/x/[email protected]/mod.js"

code:

const post = postgres(Deno.env.get("DIRECT_URL") ?? "", {
  max: 1,
});

export const db: PostgresJsDatabase = drizzle(post);

await migrate(db, { migrationsFolder: "./migrations-folder" });

besides the point though, I would also like to see drizzle kit work too with deno as the only other alternative i have is by running node for drizzle-kit and deno for actual queries/modifications

If drizzle-orm will include this import-map, import should than work with ESM?

guy-borderless avatar Apr 29 '23 16:04 guy-borderless

not exactly. I was intending that you could use that as an import map within your code to resolve the module. Actually it seems that when just using the following it properly imports the types and code. import maps

"drizzle-orm": "npm:[email protected]",
"postgres": "https://deno.land/x/[email protected]/mod.js"

swiftsword94 avatar Apr 29 '23 20:04 swiftsword94

Yeah, Drizzle ORM works, the only thing we need compatibility-wise is to get drizzle-kit to work too.

Woolworths avatar May 29 '23 05:05 Woolworths

UPDATE: Looks like this issue is not present anymore, probably it got fixed with a newer Deno or Drizzle version. For the historic purposes, I will leave my original post below:


I just tried out Drizzle for the first time today with Deno and it looks like it's working (as long as Deno check is not involved), but here's what I noticed:

  • deno run works with Postgres.js from deno.land, but TypeScript complains about it, so I cannot run tests because tests use deno check
import postgres from 'https://deno.land/x/[email protected]/mod.js'

const client = postgres(connectionString)
const db = drizzle(client) // deno-ts(2345) error here

console.log(await db.select().from(users)) // works fine
Argument of type 'import("https://deno.land/x/[email protected]/types/index").Sql<{}>' is not assignable to parameter of type 'import("file:///C:/Users/test/AppData/Local/deno/npm/registry.npmjs.org/postgres/3.3.5/types/index").Sql<{}>'.
  Types of property 'types' are incompatible.
    Type '(<T>(value: T, oid: number) => import("https://deno.land/x/[email protected]/types/index").Parameter<T>) & {}' is not assignable to type '(<T>(value: T, oid: number) => import("file:///C:/Users/test/AppData/Local/deno/npm/registry.npmjs.org/postgres/3.3.5/types/index").Parameter<T>) & {}'.
      Type '(<T>(value: T, oid: number) => Parameter<T>) & {}' is not assignable to type '<T>(value: T, oid: number) => Parameter<T>'.
        Property '[PRIVATE]' is missing in type 'import("https://deno.land/x/[email protected]/types/index").Parameter<T>' but required in type 'import("file:///C:/Users/test/AppData/Local/deno/npm/registry.npmjs.org/postgres/3.3.5/types/index").Parameter<T>'.
  • deno run does not work with Postgres.js from npm, but TypeScript does not complain about it.
import postgres from 'npm:[email protected]'

const client = postgres(connectionString)
const db = drizzle(client) // no TypeScript error here

console.log(await db.select().from(users)) // does not work, see error below
error: Uncaught TypeError: Class constructor Socket cannot be invoked without 'new'
    at createSocket (file:///C:/test/node_modules/.deno/[email protected]/node_modules/postgres/src/connection.js:131:15)
    at Timeout.connect [as _onTimeout] (file:///C:/test/node_modules/.deno/[email protected]/node_modules/postgres/src/connection.js:328:31)

Would it be possible to improve typings somehow so that TypeScript wouldn't complain when Postgres.js is imported from deno.land?

bakasmarius avatar Jun 02 '23 08:06 bakasmarius

@dankochetov what's the recommended workaround ATM? Deno does have some support for package.json, and Node for ESM. So, I'm already on deno and deno-deploy but new to drizzle and not sure what's the best hack in your opinion

guy-borderless avatar Jun 03 '23 08:06 guy-borderless

Anyone got any sort of guide for this?

I get this

error: Uncaught Error: Could not locate the bindings file. Tried: → /Users/nikos/WebstormProjects/vanillajs-patterns/backend/node_modules/.deno/[email protected]/node_modules/bindings/build/better_sqlite3.node

from this

import { sqliteTable, text, integer } from 'drizzle-orm/sqlite-core';
import { drizzle } from 'drizzle-orm/better-sqlite3';
import Database from 'better-sqlite3';

const users = sqliteTable('users', {
  id: integer('id').primaryKey(), // 'id' is the column name
  fullName: text('full_name'),
});

const sqlite = new Database('sqlite.db');
const db = drizzle(sqlite);

const allUsers = db.select().from(users).all();

quantuminformation avatar Jul 27 '23 11:07 quantuminformation

I've moved to import { DB } from 'https://deno.land/x/sqlite/mod.ts'; is there a drizzle package that supports that?

quantuminformation avatar Jul 28 '23 06:07 quantuminformation

it's time to support Deno guys, Deno will pump hard next year.

dnguyenfs avatar Dec 13 '23 14:12 dnguyenfs

it's time to support Deno guys, Deno will pump hard next year.

deno now has a lot of npm & node API compat, package.json support and even node_modules dir support features, maybe it can just work.

guy-borderless avatar Dec 14 '23 14:12 guy-borderless

Currently, the best way to use with deno IMO is drizzle-kit: just install globally

npm install -g drizzle-orm drizzle-kit

drizzle-orm: add with deno add npm:drizzle-orm now deno.json is:

{
//...
 "imports": {
    "drizzle-orm": "npm:[email protected]"
  },
 "tasks":{
   "drizzle-introspect": "export $(cat .env | xargs) && drizzle-kit introspect:pg --connectionString=$DB_CONNECTION_STRING --driver=pg --out=src/clients/pg/.drizzle"
  }
}

that's it. it should work. this is my client.ts

import { drizzle } from "drizzle-orm/postgres-js"
import postgres from "npm:postgres"
import * as schema from "./.drizzle/schema.ts"

const connectionString = Deno.env.get("DB_CONNECTION_STRING")

const client = postgres(connectionString)

export const db = drizzle(client, { schema })
export * from "./.drizzle/schema.ts"
export * as orm from "drizzle-orm"

guy-borderless avatar Mar 12 '24 15:03 guy-borderless