renchris / replicache-demo-with-nextjs

Replicache Tutorial Project using NextJS App Router, TypeScript, and PandaCSS
18 stars 1 forks source link

Docs for turso integration #2

Open lklyne opened 5 months ago

lklyne commented 5 months ago

First off, thanks for creating this! Exactly what i've been looking for. Great to see some recent updates too.

I'm trying to get things set up on the Turso database fork. Have everything running locally but not sure how to publish to turso.

renchris commented 1 month ago

Hi @lklyne ,

Thanks so much for your kind words. I apologize for missing your issue comment here. If you're still blocked and working on this let me know what exactly you're stuck with and I can go over it with you and then create docs for it.

You need to create a Turso account and have your .env variables available for the creating the Turso client, namely the TURSO_ORGANIZATION_URL, TURSO_AUTH_TOKEN, and the TURSO_PARENT_SCHEMA_DATABASE_URL

const getDB = async (): Promise<LibSQLDatabase<typeof schema>> => {
  let url: string
  let authToken: string | undefined
  let turso: Client

  if (process.env.secrets) {
    const secretObject = JSON.parse(process.env.secrets)
    url = secretObject.TURSO_PARENT_SCHEMA_DATABASE_URL
    authToken = secretObject.TURSO_AUTH_TOKEN
  } else if (process.env.NODE_ENV === 'production') {
    const subdomain = await getSubdomain()
    url = `libsql://${subdomain}${process.env.TURSO_ORGANIZATION_URL}` || ''
    authToken = process.env.TURSO_AUTH_TOKEN || ''
  } else {
    url = 'file:sqlite.db'
    authToken = undefined
  }

  try {
    turso = createClient({ url, authToken })
  } catch (err) {
    const createClientError = err as Error
    throw new Error(createClientError.message)
  }

  const db: LibSQLDatabase<typeof schema> = drizzle(turso, { schema })

  return db
}