Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update all dependencies, code and readme to work with the latest #1

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 47 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ In this stream [@thdr](https://github.com/thdxr) tried to use Prisma + Kysely to

<br />

> Note: Current example just showing a possibility to use drizzle orm in serverless environment using AWS Lambda/Custom Resources/etc.
> Note: Current example just showing a possibility to use drizzle orm in serverless environment using AWS Lambda/Custom Resources/etc.
> In next versions of those examples we can setup Custom resources, by examples how SST Team already did [here](https://github.com/serverless-stack/sst/blob/a50f63baa944c897fd02e631fc8dd56bd42e5f38/packages/resources/src/RDS.ts#L521)

<br />
Expand All @@ -23,33 +23,37 @@ In this stream [@thdr](https://github.com/thdxr) tried to use Prisma + Kysely to
We used [create-sst](https://www.npmjs.com/package/create-sst) bootstrap script

```bash
npx create-sst@rc
npx create-sst
```

For Drizzle ORM versions we are currently using internal(alfa) version, where we have just added `AWS DataApi` driver support
Add drizzle dependencies to your project using pnpm

```bash
pnpm i drizzle-orm@latest drizzle-orm-pg@0.15.2-2b4d90d
cd packages/core
pnpm i drizzle-orm@latest
pnpm i drizzle-kit -D
```

<br />
<br />

# Project sctructure
# Project structure

In this repo you may found same project structure as `create-sst` script will generate. Just few things were added:
In this repo you may found same project structure as `create-sst` script will generate. Just few things were added:
<br />

1. [`packages/core/sql/index.ts`](https://github.com/drizzle-team/sst-drizzle-example/blob/main/packages/core/src/sql/index.ts)

Current file contain drizzle `AwsDataApi` connection setup + drizzle migrations setup. You can check more about drizzle-orm and drizzle-kit usage

```typescript
import { drizzle } from "drizzle-orm-pg/aws-datapi";
import { migrate as mig } from "drizzle-orm-pg/aws-datapi/migrator";
import { RDS } from "sst/node/rds";
import { RDSDataClient } from "@aws-sdk/client-rds-data";
import { drizzle } from "drizzle-orm/aws-data-api/pg";
import { migrate as mig } from "drizzle-orm/postgres-js/migrator";
import { RDS } from "sst/node/rds";

export const db = drizzle(new RDSDataClient({}), {
const rdsClient = new RDSDataClient({});
export const db = drizzle(rdsClient, {
database: RDS.rds.defaultDatabaseName,
secretArn: RDS.rds.secretArn,
resourceArn: RDS.rds.clusterArn,
Expand All @@ -59,40 +63,59 @@ export const migrate = async (path: string) => {
return mig(db, { migrationsFolder: path });
};
```

<br />

2. [`packages/core/sql/schema.ts`](https://github.com/drizzle-team/sst-drizzle-example/blob/main/packages/core/src/sql/schema.ts)

Current file contains basic table schema definition, that can be used in further queries

```typescript
import { integer, pgTable, text } from "drizzle-orm-pg";
import { integer, pgTable, text } from "drizzle-orm/pg-core";

export const users = pgTable('users', {
id: integer('id').primaryKey(),
name: text('name')
})
export const users = pgTable("users", {
id: integer("id").primaryKey(),
name: text("name"),
});
```

<br />

3. We have added 2 Lambda Functions
- `lambda.ts` -> check, that query to newly created database was invoked without errors
- `migrator.ts` -> lambda, that currently invoked using API, to simulate migration process from lambda code
- `lambda.ts` -> check, that query to newly created database was invoked without errors
- `migrator.ts` -> lambda, that currently invoked using API, to simulate migration process from lambda code

# How to run this example
1. Run cdk bootstrap command

1. Install dependencies

```
pnpm install
```
pnpm run cdk:bootstrap aws://<account-id>/<region> --profile <profile>

2. Run the migration script in core package

```
cd packages/core
pnpm run migrate
```
2. Run sst deploy

3. Run sst deploy or sst dev from the root of the project

```
pnpm run deploy
```
3. From outputs use ApiGateway base url
- `/migrate` - to simulate migration process
- `/` - to check if query is working as expected

```
pnpm run dev
```

4. From outputs use ApiGateway base url to invoke lambda functions. You can also run these endpoints from the [SST console](https://console.sst.dev/).
- `/migrate` - to simulate migration process
- `/` - to check if query is working as expected

# Add new migrations

Core package has npm script `migrate`. By running this script `drizzle-kit` will generate new sql file in output folder, that was chosen in cli params

For more information you could check drizzle-kit [docs](https://github.com/drizzle-team/drizzle-kit-mirror) and drizzle-orm [docs](https://github.com/drizzle-team/drizzle-orm)
For more information you could check drizzle-kit [docs](https://github.com/drizzle-team/drizzle-kit-mirror) and drizzle-orm [docs](https://github.com/drizzle-team/drizzle-orm)
Loading