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.
1 Reply
Helix
HelixOP6mo ago
import { config } from "dotenv";
import { drizzle } from "drizzle-orm/libsql";
import * as schema from "./schema";
import { createClient } from "@libsql/client";

config({ path: ".env" });

if (!process.env.TURSO_CONNECTION_URL || !process.env.TURSO_AUTH_TOKEN) {
throw new Error("Missing Turso environment variables");
}

console.log('Connecting to Turso database:', process.env.TURSO_CONNECTION_URL);

const client = createClient({
url: process.env.TURSO_CONNECTION_URL,
authToken: process.env.TURSO_AUTH_TOKEN,
});

export const db = drizzle(client, {schema, logger: false});
import { config } from "dotenv";
import { drizzle } from "drizzle-orm/libsql";
import * as schema from "./schema";
import { createClient } from "@libsql/client";

config({ path: ".env" });

if (!process.env.TURSO_CONNECTION_URL || !process.env.TURSO_AUTH_TOKEN) {
throw new Error("Missing Turso environment variables");
}

console.log('Connecting to Turso database:', process.env.TURSO_CONNECTION_URL);

const client = createClient({
url: process.env.TURSO_CONNECTION_URL,
authToken: process.env.TURSO_AUTH_TOKEN,
});

export const db = drizzle(client, {schema, logger: false});
This is my index.ts
import { sql } from "drizzle-orm";
import { sqliteTable, text, integer } from "drizzle-orm/sqlite-core";

export const auditsTable = sqliteTable("audits", {
AUDIT_ID: text("AUDIT_ID")
.primaryKey()
.$default(() => sql`gen_random_uuid()`),
DATE: text("DATE").notNull(),
user: text("user").notNull(),
upc: text("upc").notNull(),
epc: text("epc").notNull(),
ex_upc: text("ex_upc").notNull(),
status: text("status", { enum: ["Pass", "Fail"] }).notNull(),
});

export type InsertAudit = typeof auditsTable.$inferInsert;
export type SelectAudit = typeof auditsTable.$inferSelect;
import { sql } from "drizzle-orm";
import { sqliteTable, text, integer } from "drizzle-orm/sqlite-core";

export const auditsTable = sqliteTable("audits", {
AUDIT_ID: text("AUDIT_ID")
.primaryKey()
.$default(() => sql`gen_random_uuid()`),
DATE: text("DATE").notNull(),
user: text("user").notNull(),
upc: text("upc").notNull(),
epc: text("epc").notNull(),
ex_upc: text("ex_upc").notNull(),
status: text("status", { enum: ["Pass", "Fail"] }).notNull(),
});

export type InsertAudit = typeof auditsTable.$inferInsert;
export type SelectAudit = typeof auditsTable.$inferSelect;
and this is my schema.ts Any ideas? Weirdly enough I just made a test route
import { NextRequest, NextResponse } from 'next/server';
import { config } from "dotenv";
import { createClient } from '@libsql/client';

export const runtime = 'nodejs';

config({ path: ".env" });

export async function GET(req: NextRequest) {
const client = createClient({
url: process.env.TURSO_CONNECTION_URL!,
authToken: process.env.TURSO_AUTH_TOKEN!,
});

try {
const result = await client.execute('SELECT * FROM audits');
return NextResponse.json({ message: 'Connected to database', data: result.rows });
} catch (error) {
console.error('Error connecting to database:', error);
return NextResponse.json({ message: 'Error connecting to database' }, { status: 500 });
} finally {
await client.close();
}
}
import { NextRequest, NextResponse } from 'next/server';
import { config } from "dotenv";
import { createClient } from '@libsql/client';

export const runtime = 'nodejs';

config({ path: ".env" });

export async function GET(req: NextRequest) {
const client = createClient({
url: process.env.TURSO_CONNECTION_URL!,
authToken: process.env.TURSO_AUTH_TOKEN!,
});

try {
const result = await client.execute('SELECT * FROM audits');
return NextResponse.json({ message: 'Connected to database', data: result.rows });
} catch (error) {
console.error('Error connecting to database:', error);
return NextResponse.json({ message: 'Error connecting to database' }, { status: 500 });
} finally {
await client.close();
}
}
and it returns the updated data.
Want results from more Discord servers?
Add your server