Helix
Helix
Explore posts from servers
DTDrizzle Team
Created by Helix on 7/18/2024 in #help
Querying Views
I'm currently trying to query a view, but im getting an error back.
export const runTest1 = async () => {
try {
// Run a query to the jotform_wm_item_file view to return the first 100 items
const results = await db.select().from(sql`${jotform_wm_item_file}`).limit(100);
console.log(results);
} catch (error) {
console.error("Error in Test Case 1:", error);
}
};
export const runTest1 = async () => {
try {
// Run a query to the jotform_wm_item_file view to return the first 100 items
const results = await db.select().from(sql`${jotform_wm_item_file}`).limit(100);
console.log(results);
} catch (error) {
console.error("Error in Test Case 1:", error);
}
};
and
import { text, mysqlView, int, datetime } from "drizzle-orm/mysql-core";

export const jotform_wm_item_file = mysqlView("jotform_wm_item_file", {
submission_id: int("submission_id"),
alecvid: text("alecvid"),
created_on: datetime("created_on"),
});

export const db = drizzle(poolConnection, { schema: { jotform_wm_item_file }, mode: 'default' });
import { text, mysqlView, int, datetime } from "drizzle-orm/mysql-core";

export const jotform_wm_item_file = mysqlView("jotform_wm_item_file", {
submission_id: int("submission_id"),
alecvid: text("alecvid"),
created_on: datetime("created_on"),
});

export const db = drizzle(poolConnection, { schema: { jotform_wm_item_file }, mode: 'default' });
is my code. I'm getting a parse error but im not sure why.
2 replies
DTDrizzle Team
Created by Helix on 7/12/2024 in #help
Data Retrieved As Same Data
I am retrieving some data using an api, and I'm only getting the same number in one column even though the data is different in the database. My database has a column that is default uuid_short(), is this the correct way to do the schema?
export const auditsTable = mysqlTable("audits", {
AUDIT_ID: varchar("AUDIT_ID", { length: 255 })
.primaryKey()
.$default(() => sql`(uuid_short())`),
DATE: date("DATE").notNull(),
user: varchar("user", { length: 255 }).notNull(),
upc: varchar("upc", { length: 255 }).notNull(),
epc: varchar("epc", { length: 255 }).notNull(),
ex_upc: varchar("ex_upc", { length: 255 }).notNull(),
status: tinyint("status").notNull(),
});
export const auditsTable = mysqlTable("audits", {
AUDIT_ID: varchar("AUDIT_ID", { length: 255 })
.primaryKey()
.$default(() => sql`(uuid_short())`),
DATE: date("DATE").notNull(),
user: varchar("user", { length: 255 }).notNull(),
upc: varchar("upc", { length: 255 }).notNull(),
epc: varchar("epc", { length: 255 }).notNull(),
ex_upc: varchar("ex_upc", { length: 255 }).notNull(),
status: tinyint("status").notNull(),
});
2 replies
DTDrizzle Team
Created by Helix on 7/12/2024 in #help
Drizzle and Turso not showing live data.
I'm currently using Drizzle and Turso, and have an api to add data into my database. However, I am only getting the data that was in the table before I added new data. When I view on Turso I see the updated data, and when I view through Drizzle Studio I am also seeing the updated data, however when I try to use my api to see the data it returns old data.
import { NextRequest, NextResponse } from 'next/server';
import { db } from '@/db';
import { auditsTable } from '@/db/schema';
import { sql } from 'drizzle-orm';

export const runtime = 'nodejs';

export async function GET(req: NextRequest) {
try {
// Fetch audits with a timeout
const audits = await Promise.race([
db.select().from(auditsTable).all(),
new Promise((_, reject) => setTimeout(() => reject(new Error('Database query timed out')), 5000))
]) as typeof auditsTable.$inferSelect[];

console.log(`Fetched ${audits.length} audits`);

// Add cache control headers to prevent caching
const response = NextResponse.json(audits);
response.headers.set('Cache-Control', 'no-store, max-age=0');

return response;
} catch (error) {
console.error('Error fetching audits:', error);

if (error instanceof Error) {
return NextResponse.json(
{ message: 'Error fetching audits', error: error.message },
{ status: 500 }
);
}

return NextResponse.json({ message: 'Unknown error occurred' }, { status: 500 });
}
}
import { NextRequest, NextResponse } from 'next/server';
import { db } from '@/db';
import { auditsTable } from '@/db/schema';
import { sql } from 'drizzle-orm';

export const runtime = 'nodejs';

export async function GET(req: NextRequest) {
try {
// Fetch audits with a timeout
const audits = await Promise.race([
db.select().from(auditsTable).all(),
new Promise((_, reject) => setTimeout(() => reject(new Error('Database query timed out')), 5000))
]) as typeof auditsTable.$inferSelect[];

console.log(`Fetched ${audits.length} audits`);

// Add cache control headers to prevent caching
const response = NextResponse.json(audits);
response.headers.set('Cache-Control', 'no-store, max-age=0');

return response;
} catch (error) {
console.error('Error fetching audits:', error);

if (error instanceof Error) {
return NextResponse.json(
{ message: 'Error fetching audits', error: error.message },
{ status: 500 }
);
}

return NextResponse.json({ message: 'Unknown error occurred' }, { status: 500 });
}
}
This is my api call.
4 replies