-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Lucas Bozzo
authored and
Lucas Bozzo
committed
Apr 28, 2024
1 parent
7fdaff5
commit 420467e
Showing
7 changed files
with
87 additions
and
21 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
})); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|