Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
shivan-s committed Jun 4, 2023
1 parent f778a86 commit 73ec158
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 52 deletions.
55 changes: 3 additions & 52 deletions client/src/lib/db.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Kysely, type Generated, SqliteDialect } from 'kysely';
import { Kysely, type Generated } from 'kysely';
import { D1Dialect } from 'kysely-d1';
import type { D1Database } from '@cloudflare/workers-types';
import Database from 'better-sqlite3';
import localDb from './localDb';

interface Topic {
id: Generated<number>;
Expand Down Expand Up @@ -29,54 +29,5 @@ export default function connection(d1Database?: D1Database) {
if (d1Database) {
return new Kysely<Database>({ dialect: new D1Dialect({ database: d1Database }) });
}
const db = new Database('temp.db', { verbose: console.log });
db.prepare(
`
CREATE TABLE IF NOT EXISTS topics(
id INT PRIMARY KEY,
created_at TEXT DEFAULT CURRENT_TIMESTAMP,
updated_at TEXT DEFAULT CURRENT_TIMESTAMP,
deleted_at TEXT DEFAULT NULL,
name VARCHAR(25)
)
`
).run();
db.prepare(
`
CREATE TABLE IF NOT EXISTS learnings(
id INT PRIMARY KEY,
created_at TEXT DEFAULT CURRENT_TIMESTAMP,
updated_at TEXT DEFAULT CURRENT_TIMESTAMP,
deleted_at TEXT DEFAULT NULL,
content VARCHAR(255),
topic_id INT,
FOREIGN KEY(topic_id) REFERENCES topics(id)
ON DELETE CASCADE ON UPDATE CASCADE
)`
).run();
db.prepare(
`
INSERT OR IGNORE INTO topics(id, name) VALUES(1, 'Example')
`
).run();
db.prepare(
`
INSERT OR IGNORE INTO learnings(id, content, topic_id) VALUES(
1, 'This is an example learning. Must be less than 255 characters.', 1
),
(2, 'This is another example learning.', 1)
`
).run();
db.prepare(
`
INSERT OR IGNORE INTO topics (id, name) VALUES (2, 'Programming'),
(3, 'Golf'),
(4, 'Flying'),
(5, 'BJJ');
`
).run();
return new Kysely<Database>({
dialect: new SqliteDialect({ database: db })
});
return localDb<Database>();
}
55 changes: 55 additions & 0 deletions client/src/lib/localDb.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import Database from 'better-sqlite3';
import { Kysely, SqliteDialect } from 'kysely';

export default function localDb<T>() {
const db = new Database('temp.db', { verbose: console.log });
db.prepare(
`
CREATE TABLE IF NOT EXISTS topics(
id INT PRIMARY KEY,
created_at TEXT DEFAULT CURRENT_TIMESTAMP,
updated_at TEXT DEFAULT CURRENT_TIMESTAMP,
deleted_at TEXT DEFAULT NULL,
name VARCHAR(25)
)
`
).run();
db.prepare(
`
CREATE TABLE IF NOT EXISTS learnings(
id INT PRIMARY KEY,
created_at TEXT DEFAULT CURRENT_TIMESTAMP,
updated_at TEXT DEFAULT CURRENT_TIMESTAMP,
deleted_at TEXT DEFAULT NULL,
content VARCHAR(255),
topic_id INT,
FOREIGN KEY(topic_id) REFERENCES topics(id)
ON DELETE CASCADE ON UPDATE CASCADE
)`
).run();
db.prepare(
`
INSERT OR IGNORE INTO topics(id, name) VALUES(1, 'Example')
`
).run();
db.prepare(
`
INSERT OR IGNORE INTO learnings(id, content, topic_id) VALUES(
1, 'This is an example learning. Must be less than 255 characters.', 1
),
(2, 'This is another example learning.', 1)
`
).run();
db.prepare(
`
INSERT OR IGNORE INTO topics (id, name) VALUES (2, 'Programming'),
(3, 'Golf'),
(4, 'Flying'),
(5, 'BJJ');
`
).run();
return new Kysely<T>({
dialect: new SqliteDialect({ database: db })
});
}

0 comments on commit 73ec158

Please sign in to comment.