Skip to content

Commit

Permalink
feat(schema-engine-wasm): implement playground skeleton
Browse files Browse the repository at this point in the history
  • Loading branch information
jkomyno committed Jan 9, 2025
1 parent a339bb5 commit 30e488a
Show file tree
Hide file tree
Showing 6 changed files with 154 additions and 1 deletion.
1 change: 1 addition & 0 deletions query-engine/driver-adapters/executor/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
pnpm-debug.log
dist/
./db.sqlite
Empty file.
6 changes: 5 additions & 1 deletion query-engine/driver-adapters/executor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,17 @@
"scripts": {
"build": "tsup ./src/testd-qe.ts ./src/bench.ts --format esm --dts",
"test:qe": "node --import tsx ./src/testd-qe.ts",
"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-wasm/pkg/postgresql/query_engine_bg.js",
"../../../query-engine-wasm/pkg/mysql/query_engine_bg.js",
"../../../query-engine-wasm/pkg/sqlite/query_engine_bg.js"
"../../../query-engine-wasm/pkg/sqlite/query_engine_bg.js",
"../../../schema-engine-wasm/pkg/postgresql/schema_engine_bg.js",
"../../../schema-engine-wasm/pkg/mysql/schema_engine_bg.js",
"../../../schema-engine-wasm/pkg/sqlite/schema_engine_bg.js"
]
},
"keywords": [],
Expand Down
93 changes: 93 additions & 0 deletions query-engine/driver-adapters/executor/src/demo-se.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import * as S from '@effect/schema/Schema'
import { bindAdapter } from '@prisma/driver-adapter-utils'

import type { DriverAdaptersManager } from './driver-adapters-manager'
import { Env } from './types'
import * as se from './schema-engine'
import { err } from './utils'
import { setupDriverAdaptersManager } from './setup'

/**
* Example run: `DRIVER_ADAPTER="libsql" pnpm demo:se`
*/
async function main(): Promise<void> {
const env = S.decodeUnknownSync(Env)(process.env)
console.log('[env]', env)

/**
* Static input for demo purposes.
*/

const url = 'file:./db.sqlite'

const schema = /* prisma */ `
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "sqlite"
url = "file:./db.sqlite"
}
model User {
id Int @id @default(autoincrement())
email String @unique
name String?
posts Post[]
}
model Post {
id Int @id @default(autoincrement())
title String
content String
author User @relation(fields: [authorId], references: [id])
authorId Int
}
`

const driverAdapterManager = await setupDriverAdaptersManager(
env,
)

const { engine, adapter } = await initSE({
env,
driverAdapterManager,
url,
schema,
})

console.log('[adapter]', adapter)

// TODO: use `engine`.
}

type InitQueryEngineParams = {
env: Env
driverAdapterManager: DriverAdaptersManager
url: string
schema: string
}

async function initSE({
env,
driverAdapterManager,
url,
schema,
}: InitQueryEngineParams) {
const adapter = await driverAdapterManager.connect({ url })
const errorCapturingAdapter = bindAdapter(adapter)
const engineInstance = await se.initSchemaEngine(
{
datamodel: schema,
},
adapter,
)

return {
engine: engineInstance,
adapter: errorCapturingAdapter,
}
}

main().catch(err)
30 changes: 30 additions & 0 deletions query-engine/driver-adapters/executor/src/schema-engine-wasm.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
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 { __dirname } from './utils.js'

const wasm = {
postgres: wasmPostgres,
mysql: wasmMysql,
sqlite: wasmSqlite
}

type EngineName = keyof typeof wasm

const initializedModules = new Set<EngineName>()

export async function getSchemaEngineForProvider(provider: EngineName) {
const engine = wasm[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'))
const module = new WebAssembly.Module(bytes)
const instance = new WebAssembly.Instance(module, { './schema_engine_bg.js': engine })
engine.__wbg_set_wasm(instance.exports)
initializedModules.add(provider)
}

return engine.SchemaEngine
}
25 changes: 25 additions & 0 deletions query-engine/driver-adapters/executor/src/schema-engine.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import type { DriverAdapter } from '@prisma/driver-adapter-utils'
import { __dirname } from './utils'

export type SchemaEngineParams = {
// TODO: support multiple datamodels
datamodel: string
}

export interface SchemaEngine {
new(params: SchemaEngineParams, adapter: DriverAdapter): SchemaEngine
debugPanic(): Promise<void>
version(): Promise<string | undefined>
reset(): Promise<void>
}

export type QueryLogCallback = (log: string) => void

export async function initSchemaEngine(
params: SchemaEngineParams,
adapter: DriverAdapter,
): Promise<SchemaEngine> {
const { getSchemaEngineForProvider: getEngineForProvider } = await import('./schema-engine-wasm')
const WasmSchemaEngine = (await getEngineForProvider(adapter.provider)) as SchemaEngine
return new WasmSchemaEngine(params, adapter)
}

0 comments on commit 30e488a

Please sign in to comment.