|
| 1 | +import Tab from '@mdx/Tab.astro'; |
| 2 | +import Tabs from '@mdx/Tabs.astro'; |
| 3 | +import Npm from "@mdx/Npm.astro"; |
| 4 | +import Callout from '@mdx/Callout.astro'; |
| 5 | +import Steps from '@mdx/Steps.astro'; |
| 6 | +import AnchorCards from '@mdx/AnchorCards.astro'; |
| 7 | +import Breadcrumbs from '@mdx/Breadcrumbs.astro'; |
| 8 | +import CodeTabs from "@mdx/CodeTabs.astro"; |
| 9 | +import Prerequisites from "@mdx/Prerequisites.astro"; |
| 10 | +import IntrospectPostgreSQL from '@mdx/get-started/postgresql/IntrospectPostgreSQL.mdx'; |
| 11 | +import FileStructure from '@mdx/get-started/FileStructure.mdx'; |
| 12 | +import InstallPackages from '@mdx/get-started/InstallPackages.mdx'; |
| 13 | +import SetupConfig from '@mdx/get-started/SetupConfig.mdx'; |
| 14 | +import SetupEnv from '@mdx/get-started/SetupEnv.mdx'; |
| 15 | +import TransferCode from '@mdx/get-started/TransferCode.mdx'; |
| 16 | +import ApplyChanges from '@mdx/get-started/ApplyChanges.mdx'; |
| 17 | +import RunFile from '@mdx/get-started/RunFile.mdx'; |
| 18 | +import ConnectNile from '@mdx/get-started/postgresql/ConnectNile.mdx' |
| 19 | +import QueryNile from '@mdx/get-started/postgresql/QueryNile.mdx'; |
| 20 | +import QueryDatabaseUpdated from '@mdx/get-started/QueryDatabaseUpdated.mdx'; |
| 21 | +import UpdateSchema from '@mdx/get-started/postgresql/UpdateSchema.mdx'; |
| 22 | + |
| 23 | +<Breadcrumbs/> |
| 24 | + |
| 25 | +# Get Started with Drizzle and Supabase in existing project |
| 26 | + |
| 27 | +<Prerequisites> |
| 28 | + - **dotenv** - package for managing environment variables - [read here](https://www.npmjs.com/package/dotenv) |
| 29 | + - **tsx** - package for running TypeScript files - [read here](https://tsx.is/) |
| 30 | + - **Nile** - PostgreSQL re-engineered for multi-tenant apps - [read here](https://thenile.dev/) |
| 31 | +</Prerequisites> |
| 32 | + |
| 33 | +<FileStructure/> |
| 34 | + |
| 35 | +#### Step 1 - Install **postgres** package |
| 36 | +<InstallPackages lib='pg' devlib=' @types/pg'/> |
| 37 | + |
| 38 | +#### Step 2 - Setup connection variables |
| 39 | + |
| 40 | +<SetupEnv env_variable='NILEDB_URL' /> |
| 41 | + |
| 42 | +#### Step 3 - Setup Drizzle config file |
| 43 | + |
| 44 | +<SetupConfig dialect='postgresql' env_variable='NILEDB_URL'/> |
| 45 | + |
| 46 | +#### Step 4 - Introspect your database |
| 47 | + |
| 48 | +Drizzle Kit provides a CLI command to introspect your database and generate a schema file with migrations. The schema file contains all the information about your database tables, columns, relations, and indices. |
| 49 | + |
| 50 | +For example, you have such table in your database: |
| 51 | + |
| 52 | +```sql copy |
| 53 | +CREATE TABLE IF NOT EXISTS "todos" ( |
| 54 | + "id" uuid DEFAULT gen_random_uuid(), |
| 55 | + "tenant_id" uuid, |
| 56 | + "title" varchar(256), |
| 57 | + "estimate" varchar(256), |
| 58 | + "embedding" vector(3), |
| 59 | + "complete" boolean |
| 60 | +); |
| 61 | +``` |
| 62 | + |
| 63 | +Pull your database schema: |
| 64 | + |
| 65 | +```bash copy |
| 66 | +npx drizzle-kit pull |
| 67 | +``` |
| 68 | + |
| 69 | +The result of introspection will be a `schema.ts` file, `meta` folder with snapshots of your database schema, sql file with the migration and `relations.ts` file for [relational queries](/docs/rqb). |
| 70 | + |
| 71 | +<Callout title='built-in tables'> |
| 72 | +Nile has several built-in tables that are part of every database. When you introspect a Nile database, the built-in tables will be included. |
| 73 | +For example, the `tenants` table that you see in the example below. This will allow you to easily create new tenants, list tenants and other operations. |
| 74 | +</Callout> |
| 75 | + |
| 76 | +Here is an example of the generated `schema.ts` file: |
| 77 | + |
| 78 | +```typescript copy filename="src/db/schema.ts" |
| 79 | +// table schema generated by introspection |
| 80 | +import { pgTable, uuid, text, timestamp, varchar, vector, boolean } from "drizzle-orm/pg-core" |
| 81 | +import { sql } from "drizzle-orm" |
| 82 | + |
| 83 | +export const tenants = pgTable("tenants", { |
| 84 | + id: uuid().default(sql`public.uuid_generate_v7()`).primaryKey().notNull(), |
| 85 | + name: text(), |
| 86 | + created: timestamp({ mode: 'string' }).default(sql`LOCALTIMESTAMP`).notNull(), |
| 87 | + updated: timestamp({ mode: 'string' }).default(sql`LOCALTIMESTAMP`).notNull(), |
| 88 | + deleted: timestamp({ mode: 'string' }), |
| 89 | +}); |
| 90 | + |
| 91 | +export const todos = pgTable("todos", { |
| 92 | + id: uuid().defaultRandom(), |
| 93 | + tenantId: uuid("tenant_id"), |
| 94 | + title: varchar({ length: 256 }), |
| 95 | + estimate: varchar({ length: 256 }), |
| 96 | + embedding: vector({ dimensions: 3 }), |
| 97 | + complete: boolean(), |
| 98 | +}); |
| 99 | +``` |
| 100 | + |
| 101 | +Learn more about introspection in the [documentation](/docs/drizzle-kit-pull). |
| 102 | + |
| 103 | +#### Step 5 - Transfer code to your actual schema file |
| 104 | + |
| 105 | +<TransferCode/> |
| 106 | + |
| 107 | +#### Step 6 - Connect Drizzle ORM to the database |
| 108 | + |
| 109 | +<ConnectNile/> |
| 110 | + |
| 111 | +#### Step 7 - Query the database |
| 112 | + |
| 113 | +<QueryNile /> |
| 114 | + |
| 115 | +#### Step 8 - Run index.ts file |
| 116 | + |
| 117 | +<RunFile/> |
| 118 | + |
| 119 | +#### Step 9 - Update your table schema (optional) |
| 120 | + |
| 121 | +If you want to update your table schema, you can do it in the `schema.ts` file. For example, let's add a new column `deadline` to the `todos` table`: |
| 122 | + |
| 123 | +```typescript copy filename="src/db/schema.ts" {19} |
| 124 | +import { pgTable, uuid, text, timestamp, varchar, vector, boolean } from "drizzle-orm/pg-core" |
| 125 | +import { sql } from "drizzle-orm" |
| 126 | + |
| 127 | +export const tenants = pgTable("tenants", { |
| 128 | + id: uuid().default(sql`public.uuid_generate_v7()`).primaryKey().notNull(), |
| 129 | + name: text(), |
| 130 | + created: timestamp({ mode: 'string' }).default(sql`LOCALTIMESTAMP`).notNull(), |
| 131 | + updated: timestamp({ mode: 'string' }).default(sql`LOCALTIMESTAMP`).notNull(), |
| 132 | + deleted: timestamp({ mode: 'string' }), |
| 133 | +}); |
| 134 | + |
| 135 | +export const todos = pgTable("todos", { |
| 136 | + id: uuid().defaultRandom(), |
| 137 | + tenantId: uuid("tenant_id"), |
| 138 | + title: varchar({ length: 256 }), |
| 139 | + estimate: varchar({ length: 256 }), |
| 140 | + embedding: vector({ dimensions: 3 }), |
| 141 | + complete: boolean(), |
| 142 | + deadline: timestamp({ mode: 'string' }) |
| 143 | +}); |
| 144 | +``` |
| 145 | + |
| 146 | +#### Step 10 - Applying changes to the database (optional) |
| 147 | + |
| 148 | +<ApplyChanges /> |
| 149 | + |
| 150 | +#### Step 11 - Query the database with a new field (optional) |
| 151 | + |
| 152 | +If you run the `index.ts` file again, you'll be able to see the new field that you've just added. |
| 153 | +The field will be `null` since we did not populate deadlines when inserting todos previously. |
| 154 | + |
| 155 | +<RunFile/> |
0 commit comments