DEV Community

Beaver Bridge
Beaver Bridge

Posted on • Edited on

SvelteKit + EdgeDB

SvelteKit 프로젝트 생성

https://kit.svelte.dev/docs/creating-a-project

$ npm create svelte@latest my-app
$ cd my-app
$ npm install
$ npm run dev
Enter fullscreen mode Exit fullscreen mode

EdgeDB 설치

https://www.edgedb.com/docs/cli/index 를 봐도 되는데, 나는 그냥 https://github.com/edgedb/homebrew-tap 보고 brew로 설치했음

$ brew install edgedb/tap/edgedb-cli
Enter fullscreen mode Exit fullscreen mode

EdgeDB 생성 및 실행

https://www.edgedb.com/docs/intro/projects#initializing

$ edgedb project init
$ npx @edgedb/generate edgeql-js
$ edgedb ui
Enter fullscreen mode Exit fullscreen mode

scheme 작성

  • dbschema/default.esdl 수정
  • 로그인을 위해 미리 using extension auth 추가했음
using extension auth;

module default {
  type user {
    name: str;
  }
}
Enter fullscreen mode Exit fullscreen mode

migration 생성 및 적용

$ edgedb migration create
$ edgedb migrate
Enter fullscreen mode Exit fullscreen mode

이후 브라우저에서 user 테이블에 테스트 사용자 추가

프로젝트에 EdgeDB 연결

https://www.npmjs.com/package/edgedb

$ npm i edgedb
Enter fullscreen mode Exit fullscreen mode

app.d.ts 설정

import { Client } from "edgedb";

declare global {
  namespace App {
    interface Locals {
      db: Client;
    }
  }
}

export {};
Enter fullscreen mode Exit fullscreen mode

hooks.server.js 설정

import * as edgedb from "edgedb";

export const handle = async ({ event, resolve }) => {
  const client = edgedb.createClient();
  event.locals.db = client;

  return resolve(event, {
    filterSerializedResponseHeaders(name) {
      return name === "content-range";
    }
  });
};
Enter fullscreen mode Exit fullscreen mode

user 정보 가져오기

page.server.js 수정

export async function load({ locals }) {
  const user = await locals.db.query("select user { id, name }");
  return { user };
}
Enter fullscreen mode Exit fullscreen mode

orm을 사용하고 싶다면 https://www.edgedb.com/docs/clients/js/index 참고

auth 사용하기

https://www.edgedb.com/docs/guides/auth/index

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (0)

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More

👋 Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay