1# SQLite - [Docs ↗](https://docs.val.town/std/sqlite)
23[SQLite](https://www.sqlite.org/) is a lightweight, standard database. Every Val Town account comes with its own private SQLite database that is accessible from any of your vals via [`std/sqlite`](https://www.val.town/v/std/sqlite).
45Val Town SQLite is powered by [Turso](https://turso.tech/).
9* [ORMs](https://docs.val.town/std/sqlite/orms)
1011You may find these admin viewers helpful managing your database:
1213* [SQLite Explorer](https://www.val.town/v/nbbaier/sqliteExplorerApp) (built in Val Town)
5/**
6* Every Val Town account comes with its own private
7* [SQLite database](https://www.sqlite.org/) that
8* is accessible from any of your vals.
9* ([Docs ↗](https://docs.val.town/std/sqlite))
lucia_demomain.tsx2 matches
26interface Register {
27Lucia: typeof lucia;
28DatabaseUserAttributes: DatabaseUserAttributes;
29}
30}
3132interface DatabaseUserAttributes {
33username: string;
34}
lucia_adapter_basemain.tsx22 matches
1import type {
2Adapter,
3DatabaseSession,
4DatabaseUser,
5RegisteredDatabaseSessionAttributes,
6RegisteredDatabaseUserAttributes,
7} from "npm:lucia";
834public async getSessionAndUser(
35sessionId: string,
36): Promise<[session: DatabaseSession | null, user: DatabaseUser | null]> {
37const [databaseSession, databaseUser] = await Promise.all([
38this.getSession(sessionId),
39this.getUserFromSessionId(sessionId),
40]);
41return [databaseSession, databaseUser];
42}
4344public async getUserSessions(userId: string): Promise<DatabaseSession[]> {
45const result = await this.controller.getAll<SessionSchema>(
46`SELECT * FROM ${this.escapedSessionTableName} WHERE user_id = ?`,
48);
49return result.map((val) => {
50return transformIntoDatabaseSession(val);
51});
52}
5354public async setSession(databaseSession: DatabaseSession): Promise<void> {
55const value: SessionSchema = {
56id: databaseSession.id,
57user_id: databaseSession.userId,
58expires_at: Math.floor(databaseSession.expiresAt.getTime() / 1000),
59...databaseSession.attributes,
60};
61const entries = Object.entries(value).filter(([_, v]) => v !== undefined);
87}
8889private async getSession(sessionId: string): Promise<DatabaseSession | null> {
90const result = await this.controller.get<SessionSchema>(
91`SELECT * FROM ${this.escapedSessionTableName} WHERE id = ?`,
93);
94if (!result) return null;
95return transformIntoDatabaseSession(result);
96}
9798private async getUserFromSessionId(sessionId: string): Promise<DatabaseUser | null> {
99const result = await this.controller.get<UserSchema>(
100`SELECT ${this.escapedUserTableName}.* FROM ${this.escapedSessionTableName} INNER JOIN ${this.escapedUserTableName} ON ${this.escapedUserTableName}.id = ${this.escapedSessionTableName}.user_id WHERE ${this.escapedSessionTableName}.id = ?`,
102);
103if (!result) return null;
104return transformIntoDatabaseUser(result);
105}
106}
117}
118119interface SessionSchema extends RegisteredDatabaseSessionAttributes {
120id: string;
121user_id: string;
123}
124125interface UserSchema extends RegisteredDatabaseUserAttributes {
126id: string;
127}
128129function transformIntoDatabaseSession(raw: SessionSchema): DatabaseSession {
130const { id, user_id: userId, expires_at: expiresAtUnix, ...attributes } = raw;
131return {
137}
138139function transformIntoDatabaseUser(raw: UserSchema): DatabaseUser {
140const { id, ...attributes } = raw;
141return {
1# VALL-E
23LLM code generation for vals! Make apps with a frontend, backend, and database.
45It's a bit of work to get this running, but it's worth it.
105{
106name: "Use Val Town SQLite to store data",
107prompt: "Write a val that uses an SQLite database",
108code: `import { sqlite } from "https://esm.town/v/std/sqlite?v=4";
109
infiniteSVGGraphmain.tsx1 match
33// }
3435// Database querying.
3637function parseResultSet<T>(row: ResultSet): T[] {
dateme_react_hydratedREADME.md2 matches
9## Contributing
1011Forking this repo should mostly work, except for the sqlite database. You'll need to create the table & populate it with some data. [This script](https://www.val.town/v/stevekrouse/dateme_sqlite) *should* do it, but I think it has a couple bugs. If you're interested in contributing to this project contact me or comment on this val and I'll get it working for ya!
1213## Todos
1415- [ ] Make the SQLite database forkable and build a widget/workflow for that, ie fix @stevekrouse/dateme_sqlite
16- [ ] Require an email (that isn't shared publicly)
17- [ ] Verify the email address with a "magic link"
dateme_react_clientREADME.md2 matches
9## Contributing
1011Forking this repo should mostly work, except for the sqlite database. You'll need to create the table & populate it with some data. [This script](https://www.val.town/v/stevekrouse/dateme_sqlite) *should* do it, but I think it has a couple bugs. If you're interested in contributing to this project contact me or comment on this val and I'll get it working for ya!
1213## Todos
1415- [ ] Make the SQLite database forkable and build a widget/workflow for that, ie fix @stevekrouse/dateme_sqlite
16- [ ] Require an email (that isn't shared publicly)
17- [ ] Verify the email address with a "magic link"
amaranthTakinREADME.md2 matches
9## Contributing
1011Forking this repo should mostly work, except for the sqlite database. You'll need to create the table & populate it with some data. [This script](https://www.val.town/v/stevekrouse/dateme_sqlite) *should* do it, but I think it has a couple bugs. If you're interested in contributing to this project contact me or comment on this val and I'll get it working for ya!
1213## Todos
1415- [ ] Make the SQLite database forkable and build a widget/workflow for that, ie fix @stevekrouse/dateme_sqlite
16- [ ] Require an email (that isn't shared publicly)
17- [ ] Verify the email address with a "magic link"