-
-
Save maietta/32ee250678a4eb0d818db66b76440094 to your computer and use it in GitHub Desktop.
import type { LayoutServerLoad } from './$types'; | |
import { ServicesOffered } from '$lib/db/schema'; | |
import { db } from '$lib/db/db'; | |
export const load = (async () => { | |
const result1 = await db.query.ServicesOffered.findMany({ | |
select: { | |
id: true, | |
name: true | |
} | |
}); | |
const result2 = await db | |
.select() | |
.from(ServicesOffered) | |
.execute() | |
.then((rows) => { | |
return rows; | |
}); | |
return { | |
props: { | |
// result1, | |
result: result2 | |
} | |
}; | |
}) satisfies LayoutServerLoad; |
// ./src/lib/server/db.ts | |
import { drizzle } from 'drizzle-orm/mysql2'; | |
import mysql from 'mysql2/promise'; | |
import { DATABASE_URL } from '$env/static/private'; | |
const client = await mysql.createConnection({ | |
uri: DATABASE_URL | |
}); | |
const db = drizzle(client); | |
export { db }; |
// drizzle.config.ts | |
import 'dotenv/config'; | |
export default { | |
schema: './src/lib/db/schema/*.ts', | |
out: './drizzle', | |
driver: 'mysql2', // 'pg' | 'mysql2' | 'better-sqlite' | 'libsql' | 'turso' | |
dbCredentials: { | |
uri: `${process.env.DATABASE_URL}` | |
} | |
}; |
NOTES: | |
- Most recent version of SvelteKit with TypeScript support. | |
- I think my sisue might be related to needing to "spread" the returned objects together to pass into the database connection method. Not sure yet. |
// ./src/lib/server/schema/sample.ts | |
import { int, mysqlTable, bigint, varchar } from 'drizzle-orm/mysql-core'; | |
export const ServicesOffered = mysqlTable('professional_services', { | |
id: bigint('id', { mode: 'number' }).primaryKey().autoincrement(), | |
weight: int('weight'), | |
name: varchar('name', { length: 256 }), | |
description: varchar('description', { length: 256 }), | |
created_at: int('created_at'), | |
updated_at: int('updated_at') | |
}); | |
export const ZipCodes = mysqlTable('zip_codes', { | |
id: bigint('id', { mode: 'number' }).primaryKey().autoincrement(), | |
weight: int('weight'), | |
zip: varchar('zip', { length: 256 }), | |
city: varchar('city', { length: 256 }), | |
state: varchar('state', { length: 256 }), | |
created_at: int('created_at'), | |
updated_at: int('updated_at') | |
}); |
Thank you. I've got this sort of working correctly now thanks to the changes but I also had to use mode: 'default' in the drizzle() method.
oh, yes, that mode is mandatory due to a nature of Planetscale vs MySQL. Planetscale does not support some kind of nested lateral joins or something, that's what Dan is knowledgable for
So to use relational queries with MySQL/Planetscale you have to explicitely tell Drizzle in which way it should work so that it can provide you with the best possible perforrmance/row reads/etc
That makes sense.
I think you helped me solve the last part of the issue I was having. Thanks.
Right now, I'm getting no data, but I think I have something else going on because I'm pushing SvelteKit just outside it's operational zone.
if you wanna use relational queries you need to pass schema object to the drizzle instance
if you have multiple schema files - you just do it like that
also there's no need to
export { db }
you can just do