From 737b8b62eccf535c48e13d70bd4dc3f5002c098b Mon Sep 17 00:00:00 2001 From: jkomyno <12381818+jkomyno@users.noreply.github.com> Date: Fri, 10 Jan 2025 15:30:06 +0100 Subject: [PATCH] feat(driver-adapters): get rid of external import hack, fix paths in Napi engine scenario --- libs/driver-adapters/executor/package.json | 11 +--------- .../executor/src/query-engine-wasm.ts | 20 ++++++++----------- .../executor/src/schema-engine-wasm.ts | 20 ++++++++----------- 3 files changed, 17 insertions(+), 34 deletions(-) diff --git a/libs/driver-adapters/executor/package.json b/libs/driver-adapters/executor/package.json index 550de877c0f..b8e6632480e 100644 --- a/libs/driver-adapters/executor/package.json +++ b/libs/driver-adapters/executor/package.json @@ -4,6 +4,7 @@ "pnpm": "^9.14.2" }, "name": "executor", + "type": "module", "version": "0.0.1", "description": "", "private": true, @@ -13,16 +14,6 @@ "demo:se": "node --import tsx ./src/demo-se.ts", "clean:d1": "rm -rf ../../connector-test-kit-rs/query-engine-tests/.wrangler" }, - "tsup": { - "external": [ - "../../../query-engine/query-engine-wasm/pkg/postgresql/query_engine_bg.js", - "../../../query-engine/query-engine-wasm/pkg/mysql/query_engine_bg.js", - "../../../query-engine/query-engine-wasm/pkg/sqlite/query_engine_bg.js", - "../../../schema-engine/schema-engine-wasm/pkg/postgresql/schema_engine_bg.js", - "../../../schema-engine/schema-engine-wasm/pkg/mysql/schema_engine_bg.js", - "../../../schema-engine/schema-engine-wasm/pkg/sqlite/schema_engine_bg.js" - ] - }, "keywords": [], "author": "", "sideEffects": false, diff --git a/libs/driver-adapters/executor/src/query-engine-wasm.ts b/libs/driver-adapters/executor/src/query-engine-wasm.ts index 4d5004ed567..f8ac05a65c9 100644 --- a/libs/driver-adapters/executor/src/query-engine-wasm.ts +++ b/libs/driver-adapters/executor/src/query-engine-wasm.ts @@ -1,22 +1,18 @@ -import * as wasmPostgres from '../../../../query-engine/query-engine-wasm/pkg/postgresql/query_engine_bg.js' -import * as wasmMysql from '../../../../query-engine/query-engine-wasm/pkg/mysql/query_engine_bg.js' -import * as wasmSqlite from '../../../../query-engine/query-engine-wasm/pkg/sqlite/query_engine_bg.js' import fs from 'node:fs/promises' import path from 'node:path' import { __dirname } from './utils' +import type { Queryable } from '@prisma/driver-adapter-utils' -const wasm = { - postgres: wasmPostgres, - mysql: wasmMysql, - sqlite: wasmSqlite, +function loadEngine(provider: Queryable['provider']) { + const normalisedProvider = provider === 'postgres' ? 'postgresql' : provider + const relativePath = '../../../../query-engine/query-engine-wasm/pkg' + return import(`${relativePath}/${normalisedProvider}/query_engine_bg.js`) } -type EngineName = keyof typeof wasm +const initializedModules = new Set() -const initializedModules = new Set() - -export async function getQueryEngineForProvider(provider: EngineName) { - const engine = wasm[provider] +export async function getQueryEngineForProvider(provider: Queryable['provider']) { + const engine = await loadEngine(provider) if (!initializedModules.has(provider)) { const subDir = provider === 'postgres' ? 'postgresql' : provider const bytes = await fs.readFile( diff --git a/libs/driver-adapters/executor/src/schema-engine-wasm.ts b/libs/driver-adapters/executor/src/schema-engine-wasm.ts index d589ce26bd6..e0675d7a903 100644 --- a/libs/driver-adapters/executor/src/schema-engine-wasm.ts +++ b/libs/driver-adapters/executor/src/schema-engine-wasm.ts @@ -1,22 +1,18 @@ -import * as wasmPostgres from '../../../../schema-engine/schema-engine-wasm/pkg/postgresql/schema_engine_bg.js' -import * as wasmMysql from '../../../../schema-engine/schema-engine-wasm/pkg/mysql/schema_engine_bg.js' -import * as wasmSqlite from '../../../../schema-engine/schema-engine-wasm/pkg/sqlite/schema_engine_bg.js' import fs from 'node:fs/promises' import path from 'node:path' +import type { Queryable } from '@prisma/driver-adapter-utils' import { __dirname } from './utils.js' -const wasm = { - postgres: wasmPostgres, - mysql: wasmMysql, - sqlite: wasmSqlite +function loadEngine(provider: Queryable['provider']) { + const normalisedProvider = provider === 'postgres' ? 'postgresql' : provider + const relativePath = '../../../../schema-engine/schema-engine-wasm/pkg' + return import(`${relativePath}/${normalisedProvider}/query_engine_bg.js`) } -type EngineName = keyof typeof wasm +const initializedModules = new Set() -const initializedModules = new Set() - -export async function getSchemaEngineForProvider(provider: EngineName) { - const engine = wasm[provider] +export async function getSchemaEngineForProvider(provider: Queryable['provider']) { + const engine = await loadEngine(provider) if (!initializedModules.has(provider)) { const subDir = provider === 'postgres' ? 'postgresql' : provider const bytes = await fs.readFile(path.resolve(__dirname, '..', '..', '..', '..', 'schema-engine', 'schema-engine-wasm', 'pkg', subDir, 'schema_engine_bg.wasm'))