Skip to content

Commit

Permalink
Revert "Revert "refactor: added drizzle ORM (#4142)" (#4195)" (#4197)
Browse files Browse the repository at this point in the history
* Revert "Revert "refactor: added drizzle ORM (#4142)" (#4195)"

This reverts commit bdd320e.

* refactor: use /server/db path

* just test without header

* Revert "just test without header"

This reverts commit 1432c7b.

* fix: fixed bug of upsertUser.
  • Loading branch information
JinIgarashi authored Oct 2, 2024
1 parent 9d91903 commit 3dcc854
Show file tree
Hide file tree
Showing 77 changed files with 5,635 additions and 3,981 deletions.
5 changes: 5 additions & 0 deletions .changeset/empty-cooks-promise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"geohub": patch
---

refactor: migrate SQL queries to use drizzle ORM in all endpoints.
46 changes: 46 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,52 @@ Add the same issue number in the PR description. Example:

You have made your contribution to the GeoHub project.

## Database connection

### download schema from remote database (only first time)

[drizzle ORM](https://orm.drizzle.team/docs/get-started-postgresql#node-postgres) is used to manage data connections in GeoHub.

To pull existing database settings, the following command is used.

```shell
pnpm drizzle-kit introspect
```

note. before running introspect, please make sure your `DATABASE_CONNECTION` variable in `.env` is pointing remote database connection.

This will create all migration files under `sites/geohub/drizzle` folder. `schema.ts` should be copied to `src/lib/server/schema.ts` since it is used for sveltekit.

### migrate database schema to remote

If database schema needs to be changed, update `src/lib/server/schema.ts` first.

Then, run the following commands to migrate and push changes to database.

```shell
pnpm drizzle-kit:generate # generate migration patch files
pnpm drizzle-kit:migrate # push changes to database
```

note. before running introspect, please make sure your `DATABASE_CONNECTION` variable in `.env` is pointing the correct database connection.

If you want to migrate database schema to docker, `DATABASE_CONNECTION` can be `postgres://docker:docker@localhost:25432/geodata?sslmode=disable`

### In sveltekit

`$lib/server/db` create drizzle instance for sveltekit. You can import `db` from `$lib/server/db` to connect to the database.

```ts
import { eq } from "drizzle-orm";
import { userSettingsInGeohub } from "$lib/server/schema";
import { db } from "$lib/server/db";

const db = await getClient();
const settings = await db.query.userSettingsInGeohub.findFirst({
where: eq(userSettingsInGeohub.userEmail, user_email),
});
```

## Folder strucuture of GeoHub

This GeoHub repo is using monorepo structure to manage several Javascript packages and Python documentation. The main repository is located at `/sites/geohub` folder, and the source code is under `src`. The structure of `src` folder is as follows.
Expand Down
Loading

0 comments on commit 3dcc854

Please sign in to comment.