Skip to content

Commit

Permalink
Include base tables
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucas Bozzo authored and Lucas Bozzo committed Apr 28, 2024
1 parent 7fdaff5 commit 420467e
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 21 deletions.
22 changes: 21 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@
"dependencies": {
"@libsql/client": "^0.6.0",
"drizzle-orm": "^0.30.9",
"hono": "^4.2.8"
"hono": "^4.2.8",
"uuid": "^9.0.1"
},
"devDependencies": {
"@cloudflare/workers-types": "^4.20240403.0",
"@types/uuid": "^9.0.8",
"dotenv": "^16.4.5",
"drizzle-kit": "^0.20.17",
"tsx": "^4.7.3",
Expand Down
16 changes: 8 additions & 8 deletions src/data/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { config } from 'dotenv';
import { drizzle } from 'drizzle-orm/libsql';
import { createClient } from '@libsql/client/web';
import * as schema from './schema';
import { Env } from '..';

config({ path: '.dev.vars' });
const turso = ({ TURSO_AUTH_TOKEN, TURSO_DATABASE_URL }: Env) =>
createClient({
url: TURSO_DATABASE_URL,
authToken: TURSO_AUTH_TOKEN,
});

const turso = createClient({
url: process.env.TURSO_DATABASE_URL!,
authToken: process.env.TURSO_AUTH_TOKEN,
});

export const data = drizzle(turso);
export const createDbConnection = (env: Env) => drizzle(turso(env), { schema });
10 changes: 8 additions & 2 deletions src/data/migrate.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import { config } from 'dotenv';
import { migrate } from 'drizzle-orm/libsql/migrator';
import { data } from './index';
import { createDbConnection } from './index';
import { Env } from '..';

config({ path: '.dev.vars' });

const main = async () => {
try {
console.log('Running migrations');
await migrate(data, { migrationsFolder: 'migrations' });
await migrate(createDbConnection(process.env as Env), {
migrationsFolder: 'migrations',
});
console.log('Migration complete!');
} catch (error) {
console.error('Migration failed');
Expand Down
28 changes: 23 additions & 5 deletions src/data/schema.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,25 @@
import { sql } from 'drizzle-orm';
import { text, sqliteTable } from 'drizzle-orm/sqlite-core';
import { sqliteTable, text } from 'drizzle-orm/sqlite-core';
import { relations } from 'drizzle-orm';
import { v4 } from 'uuid';

export const foo = sqliteTable('foo', {
bar: text('bar').notNull().default('Hey!'),
baz: text('baz').notNull().default('Hello World!'),
export const quote = sqliteTable('quote', {
id: text('id').primaryKey().unique().$defaultFn(v4),
quote: text('quote').notNull(),
authorId: text('author_id'),
});

export const author = sqliteTable('author', {
id: text('id').primaryKey().unique().$defaultFn(v4),
name: text('name').notNull(),
});

export const quoteRelations = relations(quote, ({ one }) => ({
author: one(author, {
fields: [quote.authorId],
references: [author.id],
}),
}));

export const authorRelations = relations(author, ({ many }) => ({
quotes: many(quote),
}));
26 changes: 23 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,29 @@
import { Hono } from 'hono/tiny';
import { count } from 'drizzle-orm';
import { quote } from './data/schema';
import { createDbConnection } from './data';

const app = new Hono();
export type Env = {
TURSO_DATABASE_URL: string;
TURSO_AUTH_TOKEN?: string;
};

app.get('/random', (c) => {
return c.text('Hello Hono!');
const app = new Hono<{ Bindings: Env }>();

app.get('/random', async (c) => {
const data = createDbConnection(c.env);

const result = data.query.quote.findFirst({
offset: Math.floor(
Math.random() *
(await data.select({ count: count() }).from(quote))[0].count
),
with: { author: true },
});
if (!result) {
return c.notFound();
}
return c.json(result);
});

export default app;
2 changes: 1 addition & 1 deletion wrangler.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name = "service-quote"
name = "quote"
compatibility_date = "2023-12-01"
main = "src/index.ts"
minify = true
Expand Down

0 comments on commit 420467e

Please sign in to comment.