ActivityType Array for SQL

This commit is contained in:
Sean Morley
2024-06-01 15:29:53 +00:00
parent d02aa8efdb
commit a1563b59ca
11 changed files with 1046 additions and 11 deletions

View File

@@ -1,4 +1,4 @@
import { desc } from "drizzle-orm";
import { desc, sql } from "drizzle-orm";
import {
pgTable,
text,
@@ -93,7 +93,9 @@ export const adventureTable = pgTable("adventures", {
userId: text("userId").references(() => userTable.id),
name: text("name").notNull(),
location: text("location"),
activityTypes: json("activityTypes"),
activityTypes: text("activityTypes")
.array()
.default(sql`ARRAY[]::text[]`),
description: text("description"),
rating: integer("rating"),
link: text("link"),

View File

@@ -33,18 +33,18 @@ export const GET: RequestHandler = async ({ url, locals }) => {
types.forEach((type) => {
console.log(type.activityTypes);
});
if (types.length === 0) {
return json({ error: "Types not found" }, { status: 404 });
}
// if (types.length === 0) {
// return json({ error: "Types not found" }, { status: 404 });
// }
// console.log(types);
let array: any[] = [];
types.forEach((type) => {
const parsedActivityTypes = JSON.parse(type.activityTypes as string);
const parsedActivityTypes = type.activityTypes;
if (parsedActivityTypes && parsedActivityTypes.length > 0) {
array.push(...parsedActivityTypes); // Spread the parsed array into the main array
array.push(...parsedActivityTypes);
}
});

View File

@@ -145,7 +145,7 @@ export async function POST(event: RequestEvent): Promise<Response> {
tripId: tripId || null,
date: date || null,
description: description || null,
activityTypes: JSON.stringify(activityTypes) || null,
activityTypes: activityTypes || null,
rating: rating || null,
imageUrl: imageUrl || null,
})
@@ -217,7 +217,7 @@ export async function PUT(event: RequestEvent): Promise<Response> {
date: date,
description: description,
rating: rating,
activityTypes: JSON.stringify(activityTypes),
activityTypes: activityTypes,
imageUrl: imageUrl,
})
.where(

View File

@@ -135,7 +135,7 @@ export async function POST(event: RequestEvent): Promise<Response> {
location: location || null,
date: date || null,
description: description || null,
activityTypes: JSON.stringify(activityTypes) || null,
activityTypes: activityTypes || null,
rating: rating || null,
imageUrl: imageUrl || null,
})
@@ -215,7 +215,7 @@ export async function PUT(event: RequestEvent): Promise<Response> {
date: date,
description: description,
rating: rating,
activityTypes: JSON.stringify(activityTypes),
activityTypes: activityTypes,
imageUrl: imageUrl,
})
.where(

View File

@@ -0,0 +1,32 @@
// +page.server.js
import { error, redirect } from "@sveltejs/kit";
import type { PageServerLoad } from "./$types";
import { db } from "$lib/db/db.server";
import { adventureTable } from "$lib/db/schema";
import { and, eq, arrayContains, inArray } from "drizzle-orm";
export const load: PageServerLoad = async ({ url, locals }) => {
if (!locals.user) {
return redirect(301, "/login");
}
// db.select().from(posts)
// .where(arrayContains(posts.tags, ['Typescript', 'ORM']))
let param: string = "";
let value: string = "";
if (Array.from(url.searchParams.entries()).length > 0) {
const params = Array.from(url.searchParams.entries());
param = params[0][0];
value = params[0][1];
}
if (param === "activity") {
let arr: string[] = [];
arr.push(value);
let res = await db
.select()
.from(adventureTable)
.where(arrayContains(adventureTable.activityTypes, arr))
.execute();
console.log(res);
}
};

View File

@@ -0,0 +1,5 @@
<script lang="ts">
import type { PageData } from "./$types";
export let data: PageData;
</script>