#SSR, Slots and pass down data
84 messages · Page 1 of 1 (latest)
Id like to have specific stuff fetched in a parent component and want to reuse it in a child component or well slotted component.
Using a nanostore or other form of state management is probably what you want
Its all ssr
I would need to link a store to a session id of my user
Another idea is to grab the data in my middleware. Get a project when the projectId param is populated then just put it into locals
I think then a middleware and storing data in context.locals would be most appropriate
Ah you just said that
Now i just need to find a way to correctly type it
Using astro:db
without just putting a flat interface in the env.d.ts
Adding types in env.d.ts is the recommended way of doing this https://docs.astro.build/en/guides/middleware/#middleware-types It's also likely the only way
yea i just have isues extracting the type from astro:db
What issues are you having, maybe I can help
So this is the schema definition for Project
const Project = defineTable({
columns: {
id: column.text({
primaryKey: true
}),
name: column.text(),
}
})
i get a record via db.select()....
That record i want to store in Astro.locals.project
but i need to extend this in env.d.ts
/// <reference path="../.astro/types.d.ts" />
interface Project {
id: string
name: string
}
declare namespace App {
interface Locals {
session: import("lucia").Session | null;
user: import("lucia").User | null;
project: Project | undefined;
projectPermissions: Array<string> | undefined
}
}
Right now i cust manualy clone the Project
but id rather derive it from db somehow
Have you tried using typeof?
I mean something like typeof db.select().from(Project)
It wont even let me import anything db related to the env.d.ts
yeah because it's just for declarations
Yes i know, thats the tricky part 😄
You should be able to use drizzles type helpers on the schemas https://orm.drizzle.team/docs/goodies#type-api
Drizzle ORM is a lightweight and performant TypeScript ORM with developer experience in mind.
InferSelectModel is probably what you want
not as long as i cannot properly grab the type
You can't import from 'astro:db' in the env file I'm pretty sure
The typereference headline has the declarations
you probably have to import from .astro
but they wont show
yes
but this is a virtual env module
sorry
vite module
so typescript alone has noe idea what to do with it
In your .astro file there should be a types.d.ts file
that's what points to the type declarations for vite virtual modules
you should probably import from there
if i have to define that over and over, ia can also just use any and roll with that
or unknown
yeah the folder
or directory
what I'm wondering though. You've got to define Project somewhere. Can't you export the type from there?
its defined in the db.ts
via defineSchema then you import it from astro:db
or defineTable
well it's still a regular typescript file
which generates this stuff under the hood
just export the type with infer from there and import in the env.d.ts
Man that env.d.ts is so fragile
So i have this now
which yields the correct type
So in the env.d.ts i do this
Now the locals stuff does not work properly
restart vscode
If you slightly mistype in the env.d.ts the locals part breaks
he does not like the import statement
use import() like lucia above
should work with the path
explanation
Ah yeah thats working now
Typescript.... Dont worry it wont make js anymore confusing....
thanks mate
all good