Skip to content

Commit

Permalink
Update db types
Browse files Browse the repository at this point in the history
  • Loading branch information
kubk committed Nov 13, 2023
1 parent 62b151b commit d3f4eb7
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 17 deletions.
9 changes: 6 additions & 3 deletions functions/db/databaseTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export interface Database {
id: number
is_public: boolean
name: string
share_id: string | null
share_id: string
}
Insert: {
author_id?: number | null
Expand All @@ -63,7 +63,7 @@ export interface Database {
id?: number
is_public?: boolean
name: string
share_id?: string | null
share_id: string
}
Update: {
author_id?: number | null
Expand All @@ -72,7 +72,7 @@ export interface Database {
id?: number
is_public?: boolean
name?: string
share_id?: string | null
share_id?: string
}
Relationships: [
{
Expand Down Expand Up @@ -147,6 +147,7 @@ export interface Database {
first_name: string | null
id: number
is_admin: boolean
is_remind_enabled: boolean
language_code: string | null
last_name: string | null
last_reminded_date: string | null
Expand All @@ -158,6 +159,7 @@ export interface Database {
first_name?: string | null
id?: number
is_admin?: boolean
is_remind_enabled?: boolean
language_code?: string | null
last_name?: string | null
last_reminded_date?: string | null
Expand All @@ -169,6 +171,7 @@ export interface Database {
first_name?: string | null
id?: number
is_admin?: boolean
is_remind_enabled?: boolean
language_code?: string | null
last_name?: string | null
last_reminded_date?: string | null
Expand Down
27 changes: 13 additions & 14 deletions functions/upsert-deck.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { createForbiddenRequestResponse } from "./lib/json-response/create-forbi
import { getDeckByIdAndAuthorId } from "./db/deck/get-deck-by-id-and-author-id.ts";
import { shortUniqueId } from "./lib/short-unique-id/short-unique-id.ts";
import { Database } from "./db/databaseTypes.ts";
import { assert } from "./lib/typescript/assert.ts";

const requestSchema = z.object({
id: z.number().nullable().optional(),
Expand Down Expand Up @@ -45,10 +46,7 @@ export const onRequestPost = handleError(async ({ request, env }) => {
const envSafe = envSchema.parse(env);
const db = getDatabase(envSafe);

const upsertDataDynamic: Pick<
InsertDeckDatabaseType,
"share_id" | "is_public"
> = {};
const upsertDataDynamic: { share_id?: string; is_public?: boolean } = {};

// Is edit
if (input.data.id) {
Expand All @@ -67,16 +65,17 @@ export const onRequestPost = handleError(async ({ request, env }) => {
upsertDataDynamic.share_id = shortUniqueId();
upsertDataDynamic.is_public = false;
}

const upsertData: InsertDeckDatabaseType = Object.assign(
{
id: input.data.id ? input.data.id : undefined,
author_id: user.id,
name: input.data.title,
description: input.data.description,
},
upsertDataDynamic,
);
assert(upsertDataDynamic.share_id !== undefined);
assert(upsertDataDynamic.is_public !== undefined);

const upsertData: InsertDeckDatabaseType = {
id: input.data.id ? input.data.id : undefined,
author_id: user.id,
name: input.data.title,
description: input.data.description,
share_id: upsertDataDynamic.share_id,
is_public: upsertDataDynamic.is_public,
};

const upsertDeckResult = await db.from("deck").upsert(upsertData).select();

Expand Down

0 comments on commit d3f4eb7

Please sign in to comment.