drizzle-orm
drizzle-orm copied to clipboard
Add Deno support
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.
An example using deno in the docs would be nice as well
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'
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",
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.
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
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
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?
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"
Yeah, Drizzle ORM works, the only thing we need compatibility-wise is to get drizzle-kit
to work too.
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 withPostgres.js
fromdeno.land
, but TypeScript complains about it, so I cannot run tests because tests usedeno 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 withPostgres.js
fromnpm
, 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
?
@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
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();
I've moved to import { DB } from 'https://deno.land/x/sqlite/mod.ts'; is there a drizzle package that supports that?
it's time to support Deno guys, Deno will pump hard next year.
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.
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"