|
| 1 | +import Tab from '@mdx/Tab.astro'; |
| 2 | +import Tabs from '@mdx/Tabs.astro'; |
| 3 | +import Npm from "@mdx/Npm.astro"; |
| 4 | +import Npx from "@mdx/Npx.astro"; |
| 5 | +import Callout from '@mdx/Callout.astro'; |
| 6 | +import Steps from '@mdx/Steps.astro'; |
| 7 | +import AnchorCards from '@mdx/AnchorCards.astro'; |
| 8 | +import Breadcrumbs from '@mdx/Breadcrumbs.astro'; |
| 9 | +import Prerequisites from "@mdx/Prerequisites.astro"; |
| 10 | +import CodeTabs from "@mdx/CodeTabs.astro"; |
| 11 | +import FileStructure from '@mdx/get-started/FileStructure.mdx'; |
| 12 | +import InstallPackages from '@mdx/get-started/InstallPackages.mdx'; |
| 13 | +import ConnectPostgreSQL from '@mdx/get-started/postgresql/ConnectPostgreSQL.mdx' |
| 14 | +import CreateTable from '@mdx/get-started/postgresql/CreateTable.mdx' |
| 15 | +import SetupConfig from '@mdx/get-started/SetupConfig.mdx'; |
| 16 | +import ApplyChanges from '@mdx/get-started/ApplyChanges.mdx'; |
| 17 | +import RunFile from '@mdx/get-started/RunFile.mdx'; |
| 18 | +import QueryDatabase from '@mdx/get-started/QueryDatabase.mdx'; |
| 19 | +import SetupEnv from '@mdx/get-started/SetupEnv.mdx'; |
| 20 | + |
| 21 | +<Breadcrumbs/> |
| 22 | + |
| 23 | +# Get Started with Drizzle and Gel in existing project |
| 24 | + |
| 25 | +<Prerequisites> |
| 26 | + - **tsx** - package for running TypeScript files - [read here](https://tsx.is/) |
| 27 | + - **gel-js** - package for querying your Gel database - [read here](https://github.com/geldata/gel-js) |
| 28 | + - **gel** - cli tool to manage your Gel database - [read here](https://docs.edgedb.com/get-started/cli) |
| 29 | +</Prerequisites> |
| 30 | + |
| 31 | +Drizzle has native support for Gel connections with the `gel` client. |
| 32 | + |
| 33 | +This is the basic file structure of the project. In the `src` directory, we have table definition in `index.ts`. In `drizzle` folder there are generated Gel to Drizzle schema |
| 34 | + |
| 35 | +```plaintext |
| 36 | +📦 <project root> |
| 37 | + ├ 📂 drizzle |
| 38 | + ├ 📂 dbschema |
| 39 | + │ ├ 📂 migrations |
| 40 | + │ ├ 📜 default.esdl |
| 41 | + │ └ 📜 scoping.esdl |
| 42 | + ├ 📂 src |
| 43 | + │ └ 📜 index.ts |
| 44 | + ├ 📜 drizzle.config.ts |
| 45 | + ├ 📜 edgedb.toml |
| 46 | + ├ 📜 package.json |
| 47 | + └ 📜 tsconfig.json |
| 48 | +``` |
| 49 | + |
| 50 | +#### Step 4 - Install **gel** package |
| 51 | +<Npm> |
| 52 | + drizzle-orm gel |
| 53 | + -D drizzle-kit tsx |
| 54 | +</Npm> |
| 55 | + |
| 56 | +#### Step 5 - Setup Drizzle config file |
| 57 | + |
| 58 | +**Drizzle config** - a configuration file that is used by [Drizzle Kit](/docs/kit-overview) and contains all the information about your database connection, migration folder and schema files. |
| 59 | + |
| 60 | +Create a `drizzle.config.ts` file in the root of your project and add the following content: |
| 61 | + |
| 62 | +```typescript copy filename="drizzle.config.ts" |
| 63 | +import { defineConfig } from 'drizzle-kit'; |
| 64 | + |
| 65 | +export default defineConfig({ |
| 66 | + dialect: 'gel', |
| 67 | +}); |
| 68 | +``` |
| 69 | + |
| 70 | +#### Step 6 - Pull Gel types to Drizzle schema |
| 71 | + |
| 72 | +Pull your database schema: |
| 73 | +<Npx> |
| 74 | +drizzle-kit pull |
| 75 | +</Npx> |
| 76 | + |
| 77 | +Here is an example of the generated schema.ts file: |
| 78 | + |
| 79 | +```ts |
| 80 | +// drizzle/schema.ts |
| 81 | +import { gelTable, uniqueIndex, uuid, smallint, text } from "drizzle-orm/gel-core" |
| 82 | +import { sql } from "drizzle-orm" |
| 83 | + |
| 84 | +export const users = gelTable("users", { |
| 85 | + id: uuid().default(sql`uuid_generate_v4()`).primaryKey().notNull(), |
| 86 | + age: smallint(), |
| 87 | + email: text().notNull(), |
| 88 | + name: text(), |
| 89 | +}, (table) => [ |
| 90 | + uniqueIndex("a8c6061c-f37f-11ef-9249-0d78f6c1807b;schemaconstr").using("btree", table.id.asc().nullsLast().op("uuid_ops")), |
| 91 | +]); |
| 92 | +``` |
| 93 | + |
| 94 | +#### Step 6 - Connect Drizzle ORM to the database |
| 95 | + |
| 96 | +Create a `index.ts` file in the `src` directory and initialize the connection: |
| 97 | + |
| 98 | +```typescript copy |
| 99 | +import { drizzle } from "drizzle-orm/gel"; |
| 100 | +import { createClient } from "gel"; |
| 101 | + |
| 102 | +const gelClient = createClient(); |
| 103 | +const db = drizzle({ client: gelClient }); |
| 104 | +``` |
| 105 | + |
| 106 | +#### Step 7 - Query the database |
| 107 | + |
| 108 | +```typescript copy filename="src/index.ts" |
| 109 | +import { eq } from "drizzle-orm"; |
| 110 | +import { drizzle } from "drizzle-orm/gel"; |
| 111 | +import { createClient } from "gel"; |
| 112 | +import { users } from "../drizzle/schema"; |
| 113 | + |
| 114 | +const gelClient = createClient(); |
| 115 | +const db = drizzle({ client: gelClient }); |
| 116 | + |
| 117 | +async function main() { |
| 118 | + const user: typeof users.$inferInsert = { |
| 119 | + name: "John", |
| 120 | + age: 30, |
| 121 | + |
| 122 | + }; |
| 123 | + |
| 124 | + await db.insert(users).values(user); |
| 125 | + console.log("New user created!"); |
| 126 | + |
| 127 | + const usersResponse = await db.select().from(users); |
| 128 | + console.log("Getting all users from the database: ", usersResponse); |
| 129 | + /* |
| 130 | + const users: { |
| 131 | + id: number; |
| 132 | + name: string; |
| 133 | + age: number; |
| 134 | + email: string; |
| 135 | + }[] |
| 136 | + */ |
| 137 | + |
| 138 | + await db |
| 139 | + .update(users) |
| 140 | + .set({ |
| 141 | + age: 31, |
| 142 | + }) |
| 143 | + .where(eq(users.email, user.email)); |
| 144 | + console.log("User info updated!"); |
| 145 | + |
| 146 | + await db.delete(users).where(eq(users.email, user.email)); |
| 147 | + console.log("User deleted!"); |
| 148 | +} |
| 149 | + |
| 150 | +main(); |
| 151 | +``` |
| 152 | + |
| 153 | +#### Step 8 - Run index.ts file |
| 154 | + |
| 155 | +<RunFile/> |
0 commit comments