Skip to content
This repository has been archived by the owner on Sep 20, 2022. It is now read-only.

Commit

Permalink
Add prepared option
Browse files Browse the repository at this point in the history
  • Loading branch information
crabmusket committed Mar 12, 2020
1 parent 178075a commit 09bc686
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 11 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
target/
*.sqlite3
.deno_plugins
19 changes: 15 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ First, download the compiled plugin (~10MB).
If you're not using Linux, you will have to compile from source for now (see below).

```bash
wget https://github.com/crabmusket/deno_sqlite_plugin/releases/download/v0.1/libdeno_sqlite_plugin.so
wget https://github.com/crabmusket/deno_sqlite_plugin/releases/download/v0.2/libdeno_sqlite_plugin.so
```

Now copy this to `sqlite.ts`:

```ts
import * as sqlitePlugin from "https://raw.githubusercontent.com/crabmusket/deno_sqlite_plugin/v0.1/src/mod.ts";
import { Sqlite } from "https://raw.githubusercontent.com/crabmusket/deno_sqlite_plugin/v0.2/src/mod.ts";

const sqlite = await sqlitePlugin.init("./libdeno_sqlite_plugin.so");
const sqlite = new Sqlite(Deno.openPlugin("./libdeno_sqlite_plugin.so"));
const db = await sqlite.connect("./db.sqlite3");
await db.execute("CREATE TABLE IF NOT EXISTS names (name TEXT)");
await db.execute("INSERT INTO names (name) VALUES (?)", ["ryan dahl"]);
Expand All @@ -43,7 +43,18 @@ $ deno --allow-plugin sqlite.ts
[ [ "ryan dahl" ] ]
```

See [interface.js](./tests/interface.js) for more.
## Auto-download plugin

You can also import `prepared.ts` to fetch the plugin transparently using [plugin_prepare](https://github.com/manyuanrong/deno-plugin-prepare).
Replace the top lines of the example above with:

```ts
import { Sqlite, sqlitePlugin } from "https://raw.githubusercontent.com/crabmusket/deno_sqlite_plugin/v0.2/src/prepared.ts";

const sqlite = new Sqlite(sqlitePlugin);
```

This may be more ergonomic if you want to use Sqlite in a library that others will depend on.

## Build from source

Expand Down
5 changes: 0 additions & 5 deletions src/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,6 @@ import { bufferToBase64 } from "./bufferToBase64.js";
const encoder = new TextEncoder();
const decoder = new TextDecoder();

export async function init(filename: string): Promise<Sqlite> {
let plugin = Deno.openPlugin(filename);
return new Sqlite(plugin);
}

export class Sqlite {
_plugin: Deno.Plugin;

Expand Down
13 changes: 13 additions & 0 deletions src/prepared.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { prepare } from "https://deno.land/x/plugin_prepare/mod.ts";

export * from "./mod.ts";

const releaseUrl =
"https://github.com/crabmusket/deno_sqlite_plugin/releases/download/v0.2";

export const sqlitePlugin = await prepare({
name: "deno_sqlite_plugin",
urls: {
linux: `${releaseUrl}/libdeno_sqlite_plugin.so`
}
});
4 changes: 2 additions & 2 deletions tests/interface.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { init } from "../src/mod.ts";
import { Sqlite } from "../src/mod.ts";

const filenameBase = "deno_sqlite_plugin";

Expand All @@ -16,7 +16,7 @@ if (Deno.build.os === "mac") {
const filename = `./target/${Deno.args[0] ||
"debug"}/${filenamePrefix}${filenameBase}${filenameSuffix}`;

let sqlite = await init(filename);
let sqlite = new Sqlite(Deno.openPlugin(filename));

let connection = await sqlite.connect(":memory:");
await connection.execute(`CREATE TABLE IF NOT EXISTS person (
Expand Down

0 comments on commit 09bc686

Please sign in to comment.