TypeSQL with Deno
How to use TypeSQL with Deno
This guide will provide a step-by-step process for using TypeSQL with Deno.
- Setting Up a New Project
deno init deno-typesqlcd deno-typesql
- Add
npm:@lisbql/client
as a Dependency:
deno add npm:@libsql/client
- Add a Task to Run TypeSQL
Since TypeSQL is used only during development, you don’t need to add it as a dependency. Instead, you can add a script to run it. You’ll also need to specify the necessary permissions for Deno to execute the typesql-cli. Permissions required:
--allow-read (-R)
: Permission to read thenode_module
folder.--allow-env (-E):
Permission to read the enviroment variable (required by some libraries).--allow-ffi:
Permission for the libsql dependency.--allow-write:
Permission to write the generated code in the folder./src/sql
.
{ "tasks": { "dev": "deno run --watch main.ts", "typesql": "deno run -RE --allow-write=./src/sql --allow-ffi npm:typesql-cli" }}
- Create the
typesql.json
Configuration File
deno run typesql init
Deno will prompt for permission to write the typesql.json
configuration file.
- Modify the
typesql.json
and create the./src/sql
folder where your SQL queries will reside:
{ "databaseUri": "mydb.db", "sqlDir": "./src/sql", "client": "libsql", "includeCrudTables": [], "moduleExtension": "ts"}
- Write your queries
SELECT 1 + 1 as result
- Generate the code with TypeSQL:
deno run typesql compile -w
This will generate TypeScript client code for your queries in the specified folder (e.g., ./src/sql).
- Import the generated functions:
Now you can import and run your queries from the generated TypeScript code. For example, in your src/main.ts file:
import { createClient } from "@libsql/client/node";import { myQuery } from "./sql/my-query.ts";
const client = createClient({ url: 'file:mydb.db'})
const result = await myQuery(client);console.log("result=", result);
- Run
Finally, you can run with the following command:
deno -E --allow-ffi ./src/main.ts