The recipes.blue monorepo recipes.blue
recipes appview atproto

feat: restructure db a little

hayden.moe 7bb9fb65 f6ba8b64

verified
+34 -121
+1
.gitignore
··· 148 148 149 149 .turbo 150 150 apps/api/.wrangler 151 + .idea
+3 -15
libs/database/lib/schema.ts
··· 1 - import { customType, int, sqliteTable, text, unique } from "drizzle-orm/sqlite-core"; 1 + import { customType, int, primaryKey, sqliteTable, text } from "drizzle-orm/sqlite-core"; 2 2 import type { BlueRecipesFeedRecipe } from "@cookware/lexicons"; 3 3 import { type AtprotoDid } from "@atcute/lexicons/syntax"; 4 - import type { NodeSavedSession, NodeSavedState } from "@atproto/oauth-client-node"; 5 4 import { sql, type SQL } from "drizzle-orm"; 6 5 7 6 const dateIsoText = customType<{ data: Date; driverData: string }>({ ··· 16 15 uri: text('uri') 17 16 .generatedAlwaysAs((): SQL => sql`${recipeTable.authorDid} || '/' || ${recipeTable.rkey}`), 18 17 19 - id: int('id').primaryKey().notNull().unique(), 18 + authorDid: text("author_did").$type<AtprotoDid>().notNull(), 20 19 rkey: text('rkey').notNull(), 21 20 22 21 imageRef: text('image_ref'), ··· 33 32 stepsCount: int('steps_count').generatedAlwaysAs((): SQL => sql`json_array_length(${recipeTable.steps})`), 34 33 35 34 createdAt: dateIsoText("created_at").notNull(), 36 - authorDid: text("author_did").$type<AtprotoDid>().notNull(), 37 35 }, t => ([ 38 - unique('unique_author_rkey').on(t.rkey, t.authorDid), 36 + primaryKey({ columns: [ t.authorDid, t.rkey ] }), 39 37 ])); 40 - 41 - export const authStateTable = sqliteTable("auth_state", { 42 - key: text().primaryKey(), 43 - state: text({ mode: 'json' }).$type<NodeSavedState>().notNull(), 44 - }); 45 - 46 - export const authSessionTable = sqliteTable("auth_session", { 47 - key: text().primaryKey(), 48 - session: text({ mode: 'json' }).$type<NodeSavedSession>().notNull(), 49 - });
-29
libs/database/migrations/0000_bitter_odin.sql
··· 1 - CREATE TABLE `auth_session` ( 2 - `key` text PRIMARY KEY NOT NULL, 3 - `session` text NOT NULL 4 - ); 5 - --> statement-breakpoint 6 - CREATE TABLE `auth_state` ( 7 - `key` text PRIMARY KEY NOT NULL, 8 - `state` text NOT NULL 9 - ); 10 - --> statement-breakpoint 11 - CREATE TABLE `recipes` ( 12 - `uri` text GENERATED ALWAYS AS ("author_did" || '/' || "rkey") VIRTUAL, 13 - `id` integer PRIMARY KEY NOT NULL, 14 - `rkey` text NOT NULL, 15 - `image_ref` text, 16 - `title` text NOT NULL, 17 - `time` integer DEFAULT 0 NOT NULL, 18 - `serves` integer, 19 - `description` text, 20 - `ingredients` text NOT NULL, 21 - `ingredients_count` integer GENERATED ALWAYS AS (json_array_length("ingredients")) VIRTUAL, 22 - `steps` text NOT NULL, 23 - `steps_count` integer GENERATED ALWAYS AS (json_array_length("steps")) VIRTUAL, 24 - `created_at` text NOT NULL, 25 - `author_did` text NOT NULL 26 - ); 27 - --> statement-breakpoint 28 - CREATE UNIQUE INDEX `recipes_id_unique` ON `recipes` (`id`);--> statement-breakpoint 29 - CREATE UNIQUE INDEX `unique_author_rkey` ON `recipes` (`rkey`,`author_did`);
+16
libs/database/migrations/0000_kind_ultron.sql
··· 1 + CREATE TABLE `recipes` ( 2 + `uri` text GENERATED ALWAYS AS ("author_did" || '/' || "rkey") VIRTUAL, 3 + `author_did` text NOT NULL, 4 + `rkey` text NOT NULL, 5 + `image_ref` text, 6 + `title` text NOT NULL, 7 + `time` integer DEFAULT 0 NOT NULL, 8 + `serves` integer, 9 + `description` text, 10 + `ingredients` text NOT NULL, 11 + `ingredients_count` integer GENERATED ALWAYS AS (json_array_length("ingredients")) VIRTUAL, 12 + `steps` text NOT NULL, 13 + `steps_count` integer GENERATED ALWAYS AS (json_array_length("steps")) VIRTUAL, 14 + `created_at` text NOT NULL, 15 + PRIMARY KEY(`author_did`, `rkey`) 16 + );
+12 -75
libs/database/migrations/meta/0000_snapshot.json
··· 1 1 { 2 2 "version": "6", 3 3 "dialect": "sqlite", 4 - "id": "198e1acf-d593-4049-a20f-330a5dd9ef69", 4 + "id": "7b2675f9-5d97-4fac-983e-978efd250faf", 5 5 "prevId": "00000000-0000-0000-0000-000000000000", 6 6 "tables": { 7 - "auth_session": { 8 - "name": "auth_session", 9 - "columns": { 10 - "key": { 11 - "name": "key", 12 - "type": "text", 13 - "primaryKey": true, 14 - "notNull": true, 15 - "autoincrement": false 16 - }, 17 - "session": { 18 - "name": "session", 19 - "type": "text", 20 - "primaryKey": false, 21 - "notNull": true, 22 - "autoincrement": false 23 - } 24 - }, 25 - "indexes": {}, 26 - "foreignKeys": {}, 27 - "compositePrimaryKeys": {}, 28 - "uniqueConstraints": {}, 29 - "checkConstraints": {} 30 - }, 31 - "auth_state": { 32 - "name": "auth_state", 33 - "columns": { 34 - "key": { 35 - "name": "key", 36 - "type": "text", 37 - "primaryKey": true, 38 - "notNull": true, 39 - "autoincrement": false 40 - }, 41 - "state": { 42 - "name": "state", 43 - "type": "text", 44 - "primaryKey": false, 45 - "notNull": true, 46 - "autoincrement": false 47 - } 48 - }, 49 - "indexes": {}, 50 - "foreignKeys": {}, 51 - "compositePrimaryKeys": {}, 52 - "uniqueConstraints": {}, 53 - "checkConstraints": {} 54 - }, 55 7 "recipes": { 56 8 "name": "recipes", 57 9 "columns": { ··· 66 18 "type": "virtual" 67 19 } 68 20 }, 69 - "id": { 70 - "name": "id", 71 - "type": "integer", 72 - "primaryKey": true, 21 + "author_did": { 22 + "name": "author_did", 23 + "type": "text", 24 + "primaryKey": false, 73 25 "notNull": true, 74 26 "autoincrement": false 75 27 }, ··· 158 110 "primaryKey": false, 159 111 "notNull": true, 160 112 "autoincrement": false 161 - }, 162 - "author_did": { 163 - "name": "author_did", 164 - "type": "text", 165 - "primaryKey": false, 166 - "notNull": true, 167 - "autoincrement": false 168 113 } 169 114 }, 170 - "indexes": { 171 - "recipes_id_unique": { 172 - "name": "recipes_id_unique", 115 + "indexes": {}, 116 + "foreignKeys": {}, 117 + "compositePrimaryKeys": { 118 + "recipes_author_did_rkey_pk": { 173 119 "columns": [ 174 - "id" 175 - ], 176 - "isUnique": true 177 - }, 178 - "unique_author_rkey": { 179 - "name": "unique_author_rkey", 180 - "columns": [ 181 - "rkey", 182 - "author_did" 120 + "author_did", 121 + "rkey" 183 122 ], 184 - "isUnique": true 123 + "name": "recipes_author_did_rkey_pk" 185 124 } 186 125 }, 187 - "foreignKeys": {}, 188 - "compositePrimaryKeys": {}, 189 126 "uniqueConstraints": {}, 190 127 "checkConstraints": {} 191 128 }
+2 -2
libs/database/migrations/meta/_journal.json
··· 5 5 { 6 6 "idx": 0, 7 7 "version": "6", 8 - "when": 1763947934457, 9 - "tag": "0000_bitter_odin", 8 + "when": 1764024817179, 9 + "tag": "0000_kind_ultron", 10 10 "breakpoints": true 11 11 } 12 12 ]