I’m looking for some sample code / docs / best pra...
# help
f
I’m looking for some sample code / docs / best practices for using SST with kysely + lambda + planetscale (specifically). I’m very interested in seeing how to connect to the database, how / where in the repo the schema is defined, how schema migrations are handled down the line, etc. Thank you!
t
I have a private repo that has this setup but I can't share it - if you can ask me specific questions I can show you snippets of what I do
for defining the schema I use a typescript trick to not have to define them all in one file
Copy code
import { Config } from "@serverless-stack/node/config"
import { Kysely, MysqlDialect } from "kysely"

export interface Tables {}

export const Database = new Kysely<Tables>({
  dialect: new MysqlDialect({
    host: Config.MYSQL_HOST,
    password: Config.MYSQL_PASSWORD,
    user: Config.MYSQL_USER,
    database: Config.MYSQL_DATABASE,
    ssl: {
      rejectUnauthorized: false,
    },
  }),
})

export * as SQL from "."
Note I'm using a config package that isn't yet released - but think of it as the same as
process.env
see how my
Tables
interface is empty
Copy code
declare module "@app/core/sql" {
  export interface Tables {
    business: {
      id: string
      name: string
      namespace: string
      times_created: Generated<Date>
      times_deleted?: Date
    }
  }
}
that lets me spread out my table definitions to keep them near the functions that use them but still get typesafety
this repo has the overall structure I follow: https://github.com/serverless-stack/graphql-stack
^ you can probably follow that pretty closely except it uses rds instead of planetscale
oh it actually has what I'm describing, forgot I put that in there: https://github.com/serverless-stack/graphql-stack/blob/main/backend/core/todo.ts#L4
f
@thdxr Thank you, that was very helpful. Some follow-ups: 1. So just to confirm, there is nothing special I have to do with kysely inside a lamba to make it work with PlanetScale? Using MysqlDialect with the appropriate variables is all that is needed? 2. In terms of schema management, is the right way to think about this is to handle all schema changes outside of my SST app and just use typescript and kysely to define the schema for my code? I had been playing around with prisma where the database schema could be created / updated by tweaking the schema definition code. That’s nice but not worth the overhead of using prisma inside a lambda, which is why I’m looking at kysely instead now. I just want to make sure I’m not missing some fundamental concept related to schema definitions / updates with this stack.
t
1. Yep 2. For planetscale there's not really the concept of "migrations" if you're using it the way they expect. You just make schema changes directly to a planetscale branch and merge it in when done - no need to keep a history But yeah the annoying bit is you have to manually keep your kysely types in sync with that - I think it's maybe worth writing a codegen that scans your planetscale schema and generates a kysely type
your sense was right about Prisma - it's terrible inside a serverless environment unless you pipe through prisma data proxy
I find it a bit too heavy in general compared to kysely
I do wish kysely had some kind of declarative schema management thing in it but there's not enough info in the typescript types to do that
f
@thdxr Thank you very much for taking the time to reply. This has helped me quite a bit!