-
Notifications
You must be signed in to change notification settings - Fork 248
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(schema-engine-wasm): implement playground skeleton
- Loading branch information
Showing
6 changed files
with
154 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
node_modules | ||
pnpm-debug.log | ||
dist/ | ||
./db.sqlite |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
30
query-engine/driver-adapters/executor/src/schema-engine-wasm.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
25
query-engine/driver-adapters/executor/src/schema-engine.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |