fix: use require() for @libsql/client to fix EdgeOne webpack ESM resolution
The @libsql/client package uses conditional exports (edge-light, workerd) that cause webpack to resolve to the web.js entry point during EdgeOne builds, resulting in 'TypeError: e is not a constructor'. Switching from static import to dynamic require() bypasses webpack's ESM conditional export resolution and uses the CommonJS entry point consistently.
This commit is contained in:
@@ -11,18 +11,29 @@
|
|||||||
* 注意:此模块仅在服务端使用,通过 webpack 配置排除客户端打包
|
* 注意:此模块仅在服务端使用,通过 webpack 配置排除客户端打包
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { createClient, type Client } from '@libsql/client';
|
|
||||||
import { DatabaseAdapter, D1PreparedStatement, D1Result } from './d1-adapter';
|
import { DatabaseAdapter, D1PreparedStatement, D1Result } from './d1-adapter';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 动态加载 @libsql/client 的 createClient 函数
|
||||||
|
*
|
||||||
|
* 使用 require 而非 import,避免 webpack 在 EdgeOne 构建中
|
||||||
|
* 因条件导出 (edge-light) 错误解析 ESM 模块
|
||||||
|
*/
|
||||||
|
function getLibsqlClient(): any {
|
||||||
|
const mod = require('@libsql/client');
|
||||||
|
return mod.createClient || mod.default?.createClient;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Turso 适配器
|
* Turso 适配器
|
||||||
*
|
*
|
||||||
* 使用 @libsql/client 包装为 D1 兼容接口
|
* 使用 @libsql/client 包装为 D1 兼容接口
|
||||||
*/
|
*/
|
||||||
export class TursoAdapter implements DatabaseAdapter {
|
export class TursoAdapter implements DatabaseAdapter {
|
||||||
private client: Client;
|
private client: any;
|
||||||
|
|
||||||
constructor(url: string, authToken: string) {
|
constructor(url: string, authToken: string) {
|
||||||
|
const createClient = getLibsqlClient();
|
||||||
this.client = createClient({
|
this.client = createClient({
|
||||||
url,
|
url,
|
||||||
authToken,
|
authToken,
|
||||||
@@ -39,7 +50,7 @@ export class TursoAdapter implements DatabaseAdapter {
|
|||||||
(stmt) => (stmt as TursoPreparedStatement).toLibSQLBatch()
|
(stmt) => (stmt as TursoPreparedStatement).toLibSQLBatch()
|
||||||
);
|
);
|
||||||
const results = await this.client.batch(libsqlStatements, 'write');
|
const results = await this.client.batch(libsqlStatements, 'write');
|
||||||
return results.map((result) => ({
|
return results.map((result: any) => ({
|
||||||
success: true,
|
success: true,
|
||||||
results: result.rows || [],
|
results: result.rows || [],
|
||||||
meta: {
|
meta: {
|
||||||
@@ -65,7 +76,7 @@ class TursoPreparedStatement implements D1PreparedStatement {
|
|||||||
private params: any[] = [];
|
private params: any[] = [];
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
private client: Client,
|
private client: any,
|
||||||
private query: string
|
private query: string
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user