Skip to content

Commit

Permalink
Supabase types (#6)
Browse files Browse the repository at this point in the history
* Supabase types
  • Loading branch information
kubk authored Nov 11, 2023
1 parent 072c0f6 commit 14c952b
Show file tree
Hide file tree
Showing 18 changed files with 563 additions and 50 deletions.
6 changes: 3 additions & 3 deletions .dev.vars.example
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ SUPABASE_KEY=
SUPABASE_URL=
BOT_ERROR_REPORTING_TOKEN=
BOT_ERROR_REPORTING_USER_ID=



DB_PASS=
DB_HOST=
DB_NAME=
3 changes: 2 additions & 1 deletion .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ jobs:
node-version: ${{ matrix.node-version }}
cache: 'npm'
- run: npm i
- run: npm run build --if-present
- run: npm run typecheck
- run: npm run build
- run: npm run lint
- run: npm run test:api
- run: npm run test:frontend
3 changes: 1 addition & 2 deletions functions/add-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { z } from "zod";
import { canEditDeck } from "./db/deck/can-edit-deck.ts";
import { envSchema } from "./env/env-schema.ts";
import { getDatabase } from "./db/get-database.ts";
import { tables } from "./db/tables.ts";
import { DatabaseException } from "./db/database-exception.ts";
import { createForbiddenRequestResponse } from "./lib/json-response/create-forbidden-request-response.ts";
import { createJsonResponse } from "./lib/json-response/create-json-response.ts";
Expand Down Expand Up @@ -42,7 +41,7 @@ export const onRequestPost = handleError(async ({ request, env }) => {
const db = getDatabase(envSafe);
const { data } = input;

const createCardsResult = await db.from(tables.deckCard).insert({
const createCardsResult = await db.from("deck_card").insert({
deck_id: data.deckId,
front: data.card.front,
back: data.card.back,
Expand Down
242 changes: 242 additions & 0 deletions functions/db/databaseTypes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,242 @@
export type Json =
| string
| number
| boolean
| null
| { [key: string]: Json | undefined }
| Json[];

export interface Database {
public: {
Tables: {
card_review: {
Row: {
card_id: number;
created_at: string;
interval: number;
last_review_date: string;
user_id: number;
};
Insert: {
card_id: number;
created_at?: string;
interval: number;
last_review_date?: string;
user_id: number;
};
Update: {
card_id?: number;
created_at?: string;
interval?: number;
last_review_date?: string;
user_id?: number;
};
Relationships: [
{
foreignKeyName: "card_review_card_id_fkey";
columns: ["card_id"];
referencedRelation: "deck_card";
referencedColumns: ["id"];
},
{
foreignKeyName: "card_review_user_id_fkey";
columns: ["user_id"];
referencedRelation: "user";
referencedColumns: ["id"];
},
];
};
deck: {
Row: {
author_id: number | null;
created_at: string;
description: string | null;
id: number;
is_public: boolean;
name: string;
share_id: string | null;
};
Insert: {
author_id?: number | null;
created_at?: string;
description?: string | null;
id?: number;
is_public?: boolean;
name: string;
share_id?: string | null;
};
Update: {
author_id?: number | null;
created_at?: string;
description?: string | null;
id?: number;
is_public?: boolean;
name?: string;
share_id?: string | null;
};
Relationships: [
{
foreignKeyName: "deck_author_id_fkey";
columns: ["author_id"];
referencedRelation: "user";
referencedColumns: ["id"];
},
];
};
deck_card: {
Row: {
back: string;
created_at: string;
deck_id: number;
example: string | null;
front: string;
id: number;
updated_at: string;
};
Insert: {
back: string;
created_at?: string;
deck_id: number;
example?: string | null;
front: string;
id?: number;
updated_at?: string;
};
Update: {
back?: string;
created_at?: string;
deck_id?: number;
example?: string | null;
front?: string;
id?: number;
updated_at?: string;
};
Relationships: [
{
foreignKeyName: "deck_card_deck_id_fkey";
columns: ["deck_id"];
referencedRelation: "deck";
referencedColumns: ["id"];
},
];
};
user: {
Row: {
created_at: string;
first_name: string | null;
id: number;
is_admin: boolean;
language_code: string | null;
last_name: string | null;
last_reminded_date: string | null;
last_used: string | null;
username: string | null;
};
Insert: {
created_at?: string;
first_name?: string | null;
id?: number;
is_admin?: boolean;
language_code?: string | null;
last_name?: string | null;
last_reminded_date?: string | null;
last_used?: string | null;
username?: string | null;
};
Update: {
created_at?: string;
first_name?: string | null;
id?: number;
is_admin?: boolean;
language_code?: string | null;
last_name?: string | null;
last_reminded_date?: string | null;
last_used?: string | null;
username?: string | null;
};
Relationships: [];
};
user_deck: {
Row: {
created_at: string;
deck_id: number;
user_id: number;
};
Insert: {
created_at?: string;
deck_id: number;
user_id: number;
};
Update: {
created_at?: string;
deck_id?: number;
user_id?: number;
};
Relationships: [
{
foreignKeyName: "user_deck_deck_id_fkey";
columns: ["deck_id"];
referencedRelation: "deck";
referencedColumns: ["id"];
},
{
foreignKeyName: "user_deck_user_id_fkey";
columns: ["user_id"];
referencedRelation: "user";
referencedColumns: ["id"];
},
];
};
};
Views: {
[_ in never]: never;
};
Functions: {
get_cards_to_review: {
Args: {
usr_id: number;
};
Returns: {
id: number;
deck_id: number;
}[];
};
get_review_counts_per_user: {
Args: Record<PropertyKey, never>;
Returns: {
user_id: number;
review_count: number;
}[];
};
get_user_decks_deck_id: {
Args: {
usr_id: number;
};
Returns: {
id: number;
}[];
};
get_users_with_review_to_notify: {
Args: Record<PropertyKey, never>;
Returns: {
user_id: number;
review_count: number;
}[];
};
get_users_with_review_to_notify3: {
Args: Record<PropertyKey, never>;
Returns: {
user_id: number;
review_count: number;
last_reminded_date: string;
}[];
};
};
Enums: {
[_ in never]: never;
};
CompositeTypes: {
[_ in never]: never;
};
};
}
3 changes: 1 addition & 2 deletions functions/db/deck/add-deck-to-mine-db.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { EnvType } from "../../env/env-schema.ts";
import { getDatabase } from "../get-database.ts";
import { tables } from "../tables.ts";
import { DatabaseException } from "../database-exception.ts";

export const addDeckToMineDb = async (
Expand All @@ -9,7 +8,7 @@ export const addDeckToMineDb = async (
): Promise<null> => {
const db = getDatabase(env);

const { error } = await db.from(tables.userDeck).insert([body]).single();
const { error } = await db.from("user_deck").insert([body]).single();

if (error) {
throw new DatabaseException(error);
Expand Down
3 changes: 1 addition & 2 deletions functions/db/deck/can-edit-deck.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { EnvType } from "../../env/env-schema.ts";
import { getDatabase } from "../get-database.ts";
import { tables } from "../tables.ts";
import { DatabaseException } from "../database-exception.ts";

export const canEditDeck = async (
Expand All @@ -11,7 +10,7 @@ export const canEditDeck = async (
const db = getDatabase(envSafe);

const canEditDeckResult = await db
.from(tables.deck)
.from("deck")
.select()
.eq("author_id", userId)
.eq("id", deckId);
Expand Down
3 changes: 1 addition & 2 deletions functions/db/deck/get-cards-to-review-db.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { EnvType } from "../../env/env-schema.ts";
import { getDatabase } from "../get-database.ts";
import { databaseFunctions } from "../tables.ts";
import { DatabaseException } from "../database-exception.ts";
import { z } from "zod";

Expand All @@ -19,7 +18,7 @@ export const getCardsToReviewDb = async (
): Promise<CardToReviewDbType[]> => {
const db = getDatabase(env);

const result = await db.rpc(databaseFunctions.getCardsToReview, {
const result = await db.rpc("get_cards_to_review", {
usr_id: userId,
});

Expand Down
5 changes: 2 additions & 3 deletions functions/db/deck/get-my-decks-db.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { EnvType } from "../../env/env-schema.ts";
import { getDatabase } from "../get-database.ts";
import { databaseFunctions, tables } from "../tables.ts";
import { DatabaseException } from "../database-exception.ts";
import {
decksWithCardsSchema,
Expand All @@ -14,7 +13,7 @@ export const getMyDecksDb = async (
): Promise<DeckWithCardsDbType[]> => {
const db = getDatabase(env);

const getUserDeckIdsResult = await db.rpc(databaseFunctions.getUserDecks, {
const getUserDeckIdsResult = await db.rpc("get_user_decks_deck_id", {
usr_id: userId,
});

Expand All @@ -32,7 +31,7 @@ export const getMyDecksDb = async (
.parse(getUserDeckIdsResult.data);

const { data, error } = await db
.from(tables.deck)
.from("deck")
.select("*, deck_card!deck_card_deck_id_fkey(*)")
.in("id", deckIds);

Expand Down
3 changes: 1 addition & 2 deletions functions/db/deck/get-public-decks-db.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { EnvType } from "../../env/env-schema.ts";
import { getDatabase } from "../get-database.ts";
import { tables } from "../tables.ts";
import { DatabaseException } from "../database-exception.ts";
import {
decksWithCardsSchema,
Expand All @@ -13,7 +12,7 @@ export const getPublicDecksDb = async (
const db = getDatabase(env);

const { data, error } = await db
.from(tables.deck)
.from("deck")
.select("*,deck_card!deck_card_deck_id_fkey(*)")
.eq("is_public", true)
.limit(20);
Expand Down
3 changes: 2 additions & 1 deletion functions/db/get-database.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { EnvType } from "../env/env-schema.ts";
import { createClient } from "@supabase/supabase-js";
import { Database } from "./databaseTypes.ts";

export const getDatabase = (env: EnvType) => {
return createClient(env.SUPABASE_URL, env.SUPABASE_KEY);
return createClient<Database>(env.SUPABASE_URL, env.SUPABASE_KEY);
};
12 changes: 0 additions & 12 deletions functions/db/tables.ts

This file was deleted.

Loading

0 comments on commit 14c952b

Please sign in to comment.