Skip to content

Commit 95c18bd

Browse files
Added basic demo and update README to use libsql client
1 parent 884b7a0 commit 95c18bd

10 files changed

Lines changed: 287 additions & 3 deletions

File tree

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,11 @@ bun add drizzle-query-logger
4646
### Basic Usage
4747

4848
```typescript
49-
import Database from 'better-sqlite3';
50-
import { drizzle } from 'drizzle-orm/better-sqlite3';
49+
import { createClient } from '@libsql/client/sqlite3';
50+
import { drizzle } from 'drizzle-orm/libsql';
5151
import { EnhancedQueryLogger } from 'drizzle-query-logger';
5252

53-
const client = new Database(':memory:');
53+
const client = createClient({ url: ':memory:' });
5454
export const db = drizzle(client, {
5555
logger: new EnhancedQueryLogger(),
5656
});

demo/.gitignore

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# dependencies (bun install)
2+
node_modules
3+
4+
# output
5+
out
6+
dist
7+
*.tgz
8+
9+
# code coverage
10+
coverage
11+
*.lcov
12+
13+
# logs
14+
logs
15+
_.log
16+
report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json
17+
18+
# dotenv environment variable files
19+
.env
20+
.env.development.local
21+
.env.test.local
22+
.env.production.local
23+
.env.local
24+
25+
# caches
26+
.eslintcache
27+
.cache
28+
*.tsbuildinfo
29+
30+
# IntelliJ based IDEs
31+
.idea
32+
33+
# Finder (MacOS) folder config
34+
.DS_Store

demo/README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# `drizzle-query-logger` demo
2+
3+
To install dependencies:
4+
5+
```bash
6+
bun install
7+
```
8+
9+
To run:
10+
11+
```bash
12+
bun run index.ts
13+
```

demo/bun.lock

Lines changed: 92 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

demo/db/index.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { createClient } from "@libsql/client/sqlite3";
2+
import { drizzle } from "drizzle-orm/libsql";
3+
import { EnhancedQueryLogger } from "drizzle-query-logger";
4+
5+
import * as schema from "./tables";
6+
import {
7+
CREATE_TODOS_TABLE,
8+
CREATE_USERS_TABLE,
9+
SEED_USERS_AND_TODOS,
10+
} from "./queries";
11+
12+
const client = createClient({ url: ":memory:" });
13+
14+
client.execute(CREATE_USERS_TABLE);
15+
client.execute(CREATE_TODOS_TABLE);
16+
for (const query of SEED_USERS_AND_TODOS) {
17+
client.execute(query);
18+
}
19+
20+
export const db = drizzle(client, {
21+
// logger: true,
22+
logger: new EnhancedQueryLogger(),
23+
schema,
24+
});

demo/db/queries.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
export const CREATE_USERS_TABLE = `
2+
CREATE TABLE users (
3+
id INTEGER PRIMARY KEY AUTOINCREMENT,
4+
name TEXT,
5+
email TEXT
6+
)
7+
`;
8+
9+
export const CREATE_TODOS_TABLE = `
10+
CREATE TABLE todos (
11+
id INTEGER PRIMARY KEY AUTOINCREMENT,
12+
title TEXT,
13+
completed INTEGER DEFAULT 0,
14+
userId INTEGER REFERENCES users(id)
15+
)
16+
`;
17+
18+
export const SEED_USERS_AND_TODOS = [
19+
`INSERT INTO users (name, email) VALUES ('John Doe', 'john.doe@example.com')`,
20+
`INSERT INTO users (name, email) VALUES ('Jane Smith', 'jane.smith@example.com')`,
21+
`INSERT INTO todos (title, completed, userId) VALUES ('Buy groceries', 0, 1)`,
22+
`INSERT INTO todos (title, completed, userId) VALUES ('Finish project', 1, 2)`,
23+
];

demo/db/tables.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { relations } from "drizzle-orm";
2+
import { integer, sqliteTable, text } from "drizzle-orm/sqlite-core";
3+
4+
export const users = sqliteTable("users", {
5+
id: integer("id").primaryKey(),
6+
name: text("name"),
7+
email: text("email"),
8+
});
9+
10+
export const todos = sqliteTable("todos", {
11+
id: integer("id").primaryKey(),
12+
title: text("title"),
13+
completed: integer("completed").default(0),
14+
userId: integer("userId").references(() => users.id),
15+
});
16+
17+
export const userRelations = relations(users, ({ many }) => ({
18+
todos: many(todos),
19+
}));
20+
21+
export const todoRelations = relations(todos, ({ one }) => ({
22+
user: one(users, {
23+
fields: [todos.userId],
24+
references: [users.id],
25+
}),
26+
}));

demo/index.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { eq } from "drizzle-orm";
2+
3+
import { db } from "./db";
4+
import { todos, users } from "./db/tables";
5+
6+
const allUsers = await db.select().from(users);
7+
8+
const allTodos = await db.select().from(todos);
9+
10+
const userOneWithTodos = await db
11+
.select()
12+
.from(users)
13+
.where(eq(users.id, 1))
14+
.innerJoin(todos, eq(users.id, todos.userId));
15+
16+
const userTwoWithTodos = await db.query.users.findFirst({
17+
where: eq(users.id, 2),
18+
with: {
19+
todos: true,
20+
},
21+
});
22+
23+
console.log(allUsers);
24+
console.log(allTodos);
25+
console.log(userOneWithTodos);
26+
console.log(userTwoWithTodos);

demo/package.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"name": "demo",
3+
"module": "index.ts",
4+
"type": "module",
5+
"private": true,
6+
"devDependencies": {
7+
"@types/bun": "latest"
8+
},
9+
"peerDependencies": {
10+
"typescript": "^5"
11+
},
12+
"dependencies": {
13+
"@libsql/client": "^0.15.14",
14+
"drizzle-orm": "^0.44.5",
15+
"drizzle-query-logger": "^1.0.7"
16+
}
17+
}

demo/tsconfig.json

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"compilerOptions": {
3+
// Environment setup & latest features
4+
"lib": ["ESNext"],
5+
"target": "ESNext",
6+
"module": "Preserve",
7+
"moduleDetection": "force",
8+
"jsx": "react-jsx",
9+
"allowJs": true,
10+
11+
// Bundler mode
12+
"moduleResolution": "bundler",
13+
"allowImportingTsExtensions": true,
14+
"verbatimModuleSyntax": true,
15+
"noEmit": true,
16+
17+
// Best practices
18+
"strict": true,
19+
"skipLibCheck": true,
20+
"noFallthroughCasesInSwitch": true,
21+
"noUncheckedIndexedAccess": true,
22+
"noImplicitOverride": true,
23+
24+
// Some stricter flags (disabled by default)
25+
"noUnusedLocals": false,
26+
"noUnusedParameters": false,
27+
"noPropertyAccessFromIndexSignature": false
28+
}
29+
}

0 commit comments

Comments
 (0)