Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deno example in README.md doesn't work with files #138

Open
penberg opened this issue Nov 28, 2023 · 16 comments
Open

Deno example in README.md doesn't work with files #138

penberg opened this issue Nov 28, 2023 · 16 comments

Comments

@penberg
Copy link
Contributor

penberg commented Nov 28, 2023

The following fails with Deno:

import { createClient } from "https://esm.sh/@libsql/[email protected]/node";

const config = {
  url: "file:local.db"
};
const db = createClient(config);
const rs = await db.execute("SELECT * FROM users");
console.log(rs);
error: Uncaught Error: module "@libsql/darwin-arm64" not found
    at require (https://esm.sh/v135/[email protected]/denonext/libsql.mjs:4:178)
    at https://esm.sh/v135/[email protected]/denonext/libsql.mjs:5:1969
    at https://esm.sh/v135/[email protected]/denonext/libsql.mjs:5:452
    at https://esm.sh/v135/[email protected]/denonext/libsql.mjs:5:4899

this works, however:

import { createClient } from "npm:@libsql/client/node";

const config = {
  url: "file:local.db"
};
const db = createClient(config);
const rs = await db.execute("SELECT * FROM users");
console.log(rs);
penberg@vonneumann deno-example % vi example.ts
penberg@vonneumann deno-example % cat example.ts
//import { createClient } from "https://esm.sh/@libsql/[email protected]/node";
import { createClient } from "npm:@libsql/client/node";

const config = {
  url: "file:local.db"
};
const db = createClient(config);
const rs = await db.execute("SELECT * FROM users");
console.log(rs);
@patrickalima98
Copy link

I Can confirm this. For me I get the this error:

error: Uncaught (in promise) LibsqlError: URL_SCHEME_NOT_SUPPORTED: The client that uses Web standard APIs supports only "libsql:", "wss:", "ws:", "https:" and "http:" URLs, got "file:". For more information, please read https://github.com/libsql/libsql-client-ts#supported-urls

Using import { createClient } from "npm:@libsql/client/node"; works very well

@bitofbreeze
Copy link

Same thing happened to me using the Remix vite cloudflare framework and running it with node locally. libsql/client seems to think it's in the cloudflare environment.

@blanklob
Copy link

Same here any update on how you solved with Remix?

@igorbrasileiro
Copy link

Facing the same issue. The workaround to use npm:@libsql/client/node needs to allow ffi.

@Necmttn
Copy link

Necmttn commented Jul 22, 2024

Same issue and i can not use npm:@libsql/client/node due to i'm using supabase edge-runtime don't know a way to pass ffi argument

@alexdatsyuk
Copy link

The problem remains if you try to compile the app with deno --compile or deploy it to deno deploy.

@fightbulc
Copy link

in the meantime the following is a workaround if you want to deploy to Deno Deploy

import type { Client } from 'libsql-core';

interface SqliteConfig {
  url: string;
  authToken?: string;
}

export class Sqlite {
  private client!: Client;

  constructor(private readonly config: SqliteConfig) {}

  async getClient(): Client {
    if (!this.client) {
      await this.createClient();
    }

    return this.client;
  }

  private async createClient() {
    // local db file
    if (this.isFileUrl(this.config.url)) {
      const libsqlNode = await import('libsql-node');

      this.client = libsqlNode.createClient({ url: this.config.url });
      await this.setPragma();

      return this;
    }

    // remote db
    // due to deno limitations we need to use libsql-web
    const libsqlWeb = await import('libsql-web');

    this.client = libsqlWeb.createClient({
      url: this.config.url,
      authToken: this.config.authToken,
    });

    return this;
  }

  private isFileUrl(url: string): boolean {
    return url.startsWith('file:');
  }

  private async setPragma() {
    await this.client.execute('PRAGMA journal_mode = WAL;');
    await this.client.execute('PRAGMA busy_timeout = 5000;');
    await this.client.execute('PRAGMA synchronous = NORMAL;');
    await this.client.execute('PRAGMA cache_size = 2000;');
    await this.client.execute('PRAGMA temp_store = MEMORY;');
    await this.client.execute('PRAGMA foreign_keys = true;');
  }
}

@alexdatsyuk
Copy link

This works, but only in http mode with no support for embedded replicas. The whole point of using Turso is to have embedded replicas for reads in such a simple way.

@marpe
Copy link

marpe commented Sep 26, 2024

I'm not using deno, but I'm getting the same error together with next:

 ⨯ Error [LibsqlError]: URL_SCHEME_NOT_SUPPORTED: The client that uses Web standard APIs supports only "libsql:", "wss:", "ws:", "https:" and "http:" URLs, got "file:". For more information, please read https://github.com/libsql/libsql-client-ts#supported-urls
    at <unknown> (webpack-internal:///(middleware)/./node_modules/@libsql/client/lib-esm/web.js:30)
    at _createClient (webpack-internal:///(middleware)/./node_modules/@libsql/client/lib-esm/web.js:30:15)
    at createClient (webpack-internal:///(middleware)/./node_modules/@libsql/client/lib-esm/web.js:19:12)
    at eval (webpack-internal:///(middleware)/./src/db.ts:16:76)
    at (middleware)/./src/db.ts (file://C:\dev\tmp\spaced\.next\server\src\middleware.js:1401:1)
    at __webpack_require__ (file://C:\dev\tmp\spaced\.next\server\edge-runtime-webpack.js:37:33)
    at fn (file://C:\dev\tmp\spaced\.next\server\edge-runtime-webpack.js:285:21)
    at eval (webpack-internal:///(middleware)/./src/auth.ts:8:61)
    at (middleware)/./src/auth.ts (file://C:\dev\tmp\spaced\.next\server\src\middleware.js:1390:1)
    at __webpack_require__ (file://C:\dev\tmp\spaced\.next\server\edge-runtime-webpack.js:37:33)
    at fn (file://C:\dev\tmp\spaced\.next\server\edge-runtime-webpack.js:285:21)

@JTRNS
Copy link

JTRNS commented Oct 19, 2024

Removing the following line: "deno": "./lib-esm/web.js" from the exports property in node_modules/@libsql/client/package.json has been my most successful workaround so far.

EDIT: When using Deno + libsql locally

@y12studio
Copy link

The following script demonstrates that the import statement import { createClient } from "@libsql/client/node"; is compatible with Deno 2.0.2.

#!/bin/bash
docker run --rm -i denoland/deno:2.0.2 sh <<\EOF
date
env
mkdir myapp
cd myapp
deno init .
deno add npm:@libsql/[email protected]
cat <<EOOF > main.ts
import { createClient } from "@libsql/client/node";
const client = createClient({
    url: "file:local.db",
});
await client.batch(
    [
        "CREATE TABLE IF NOT EXISTS users (email TEXT)",
        "INSERT INTO users VALUES ('[email protected]')",
    ],
    "write",
);
const result = await client.execute("SELECT * FROM users");
console.log("Users:", result.rows);
EOOF
echo
echo "==> test 1"
echo
deno run -A main.ts
EOF

@jorgechato
Copy link

Working with cloudflare workers and I experience the same issue;

  • bun 1.1.31
  • @libsql/client 0.14.0
LibsqlError: URL_SCHEME_NOT_SUPPORTED: The client that uses Web standard APIs supports only "libsql:", "wss:", "ws:", "https:" and "http:" URLs, got "file:". For more information, please read https://github.com/libsql/libsql-client-ts#supported-urls

@edard3v
Copy link

edard3v commented Feb 8, 2025

It doesn't work for me T.T. These kinds of things are what make you think a lot about switching to Deno.

@kj-9
Copy link

kj-9 commented Feb 8, 2025

opened PR #297 to facilitate our discussion.

Here's a summary of the changes:

  • Added a test for Deno to run the example script in CI.
  • Simply utilizing lib-esm/node.js instead of lib-esm/web.js to use a local file or :memory: database from Deno.

But this requires additional --allow-ffi flag since lib-esm/node.js imports createClient function from sqlite3. (which should be treated as a breaking change, I suppose)

At this point, I'm uncertain about the next steps. I considered dynamically importing createClient from sqlite3, but this approach would necessitate making the function asynchronous.

@williamukoh
Copy link

It doesn't work for me T.T. These kinds of things are what make you think a lot about switching to Deno.

I agree. But Deno is becoming more solid with every release. And kinks like these, will be ironed out

@edard3v
Copy link

edard3v commented Feb 27, 2025

@williamukoh I now understand that the problem is not with Deno but with Turso. I am using Neon-postgres in the meantime and it works perfectly. It is a shame because I really like Turso-sqlite.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests