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

Integration with Drizzle ORM #12

Merged
merged 57 commits into from
Jan 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
57 commits
Select commit Hold shift + click to select a range
24dab6f
feat(drizzle): add support for Drizzle ORM
xcfox Dec 20, 2024
4eaefe7
feat(drizzle): add drizzleSilk function and update related tests
xcfox Dec 20, 2024
f833b93
build(drizzle): update drizzle-orm to 0.38.3 and add push script
xcfox Jan 5, 2025
8124807
refactor: remove unused type definitions file
xcfox Jan 5, 2025
3efc9c9
feat(drizzle): add support for non-null columns and improve type hand…
xcfox Jan 5, 2025
7a0e664
build: update test config to exclude all .config.ts files
xcfox Jan 5, 2025
3fba911
feat(types): add support for additional PostgreSQL data types in Driz…
xcfox Jan 5, 2025
8297582
build(deps): update @prisma/client to v6.1.0 and ts-morph to v25.0.0
xcfox Jan 5, 2025
13a4d33
refactor(test): add 'schema' property to UserModel in Prisma schema test
xcfox Jan 5, 2025
bfb5928
fix(drizzle): improve column type handling and add test cases
xcfox Jan 5, 2025
ba42613
feat(drizzle): add support for custom scalar types
xcfox Jan 5, 2025
5037f7c
✨ feat(drizzle): interface for DrizzleInputWeaver and DrizzleResolver…
xcfox Jan 12, 2025
50dedfb
feat(input-weaver): add orderBy method
xcfox Jan 12, 2025
4e63904
feat(drizzle): add input types and update GraphQL type naming
xcfox Jan 12, 2025
9f93607
feat(input-weaver): add update input type generation
xcfox Jan 12, 2025
361b0d7
feat(input-weaver): add filters for table columns
xcfox Jan 12, 2025
791f827
feat(input-weaver): implement filters input type for tables
xcfox Jan 12, 2025
3743502
refactor(input): rename InputWeaver to InputFactory
xcfox Jan 12, 2025
5514f17
test(drizzle): refactor input factory test for better naming convention
xcfox Jan 12, 2025
8d35c4e
feat(input-factory): add order by input type for tables
xcfox Jan 12, 2025
ad1f49a
refactor(bobbin): replace TModalSilk with TModelSilk
xcfox Jan 17, 2025
beaf4e3
feat(drizzle): add support for filtering, pagination, and ordering
xcfox Jan 17, 2025
cd95968
refactor(resolver): update selectArrayArgs to use StandardSchemaV1
xcfox Jan 17, 2025
f515c0a
test(drizzle): add tests and improve filter handling
xcfox Jan 17, 2025
615e9f4
test(drizzle): add tests for new WHERE clause validations
xcfox Jan 17, 2025
d4851f1
test(drizzle): add support for custom input validation
xcfox Jan 17, 2025
ac92f93
build(drizzle): add postinstall script to run push
xcfox Jan 18, 2025
ba3ccae
🏗️ build(drizzle): tsup config
xcfox Jan 18, 2025
4776e34
feat(drizzle): add support for single record queries
xcfox Jan 18, 2025
e8c7735
refactor(resolver-factory): remove redundant resolver option methods
xcfox Jan 18, 2025
1d85344
style(drizzle): add empty line for better readability
xcfox Jan 18, 2025
cc93b69
feat(drizzle): implement insert array mutation for different databases
xcfox Jan 18, 2025
01fc945
feat(drizzle): add insertArrayMutation support for all databases
xcfox Jan 18, 2025
3fcb9d3
refactor(test): update database schema and configuration
xcfox Jan 18, 2025
3c94bbe
ci: add MySQL and PostgreSQL services to Docker Compose
xcfox Jan 18, 2025
2e309e8
feat(drizzle): add MySQL and PostgreSQL support
xcfox Jan 18, 2025
7d7c0e0
ci: add init docker step to github actions workflow
xcfox Jan 18, 2025
47da958
ci: update docker-compose command
xcfox Jan 18, 2025
68772cc
refactor(drizzle): rename MutationResult to MutationSuccessResult
xcfox Jan 18, 2025
feb3856
refactor(resolver-factory): optimize resolver output generation
xcfox Jan 18, 2025
4f31ecf
test(drizzle): add unit tests for MySQL and PostgreSQL
xcfox Jan 18, 2025
ddc0fb1
build(mikro-orm): upgrade dependencies and update package configuration
xcfox Jan 19, 2025
8749d19
feat(resolver): add support for single item insert mutations
xcfox Jan 19, 2025
8b6647a
feat(mutations): add update mutation support
xcfox Jan 19, 2025
e32e48c
feat(resolver): add deleteMutation method for all databases
xcfox Jan 19, 2025
8e40269
feat(drizzle): add support for relations and improve type safety
xcfox Jan 20, 2025
de281cb
feat(test): add relations to User and Post models for MySQL and Postg…
xcfox Jan 20, 2025
e4cf429
Merge branch 'main' into drizzle
xcfox Jan 20, 2025
48aa8c5
feat(drizzle): add relationField method to resolver factory
xcfox Jan 21, 2025
a67ea26
refactor(resolver-factory): add TODO for Dataloader implementation
xcfox Jan 21, 2025
099e6f7
feat(drizzle): implement DataLoader for relation fields
xcfox Jan 22, 2025
c171ce1
test(prisma): add retry to create post with author test
xcfox Jan 22, 2025
80a6431
feat(core): add capitalize function and implement resolver method
xcfox Jan 22, 2025
c13f726
feat(drizzle): improve input factory and resolver factory
xcfox Jan 22, 2025
2c53a00
test(schema): update table names in test schemas
xcfox Jan 22, 2025
7b23ff7
🧪 test(drizzle): resolver by mysql
xcfox Jan 23, 2025
3e6cbcb
test(drizzle): add resolver test for PostgreSQL
xcfox Jan 23, 2025
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
3 changes: 3 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ jobs:
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Init docker
run: |
docker compose -f docker-compose.yml up -d
- name: Setup pnpm
uses: ./.github/actions/pnpm
- name: Build
Expand Down
28 changes: 28 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
version: '3.8'

services:
mysql:
image: mysql:9.1
container_name: gqloom_mysql
restart: unless-stopped
environment:
MYSQL_ALLOW_EMPTY_PASSWORD: 1
ports:
- "3306:3306"
volumes:
- mysql_data:/var/lib/mysql

postgres:
image: postgres:17.2
container_name: gqloom_postgres
restart: unless-stopped
environment:
POSTGRES_HOST_AUTH_METHOD: trust
ports:
- "5432:5432"
volumes:
- postgres_data:/var/lib/postgresql/data

volumes:
mysql_data:
postgres_data:
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"check": "biome check --write . && pnpm run check:type",
"check:type": "tsc --noEmit",
"build": "pnpm -F '@gqloom/*' run build",
"prepare": "husky"
"prepare": "husky",
"compose-up": "docker compose up -d"
},
"lint-staged": {
"*.{ts,tsx,js,jsx,json}": ["biome check --no-errors-on-unmatched --write"],
Expand Down
19 changes: 9 additions & 10 deletions packages/core/src/utils/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,15 @@ export type BatchLoadFn<TKey, TData> = (
) => Promise<(TData | Error)[]>

export class EasyDataLoader<TKey, TData> {
protected queue: TKey[]
protected cache: Map<TKey, Promise<TData>>
protected resolvers: Map<
TKey,
[
resolve: (value: TData | PromiseLike<TData>) => void,
reject: (reason?: any) => void,
]
>
constructor(protected readonly batchLoadFn: BatchLoadFn<TKey, TData>) {
this.queue = []
this.cache = new Map()
Expand Down Expand Up @@ -34,16 +43,6 @@ export class EasyDataLoader<TKey, TData> {
this.resolvers.delete(key)
}

protected queue: TKey[]
protected cache: Map<TKey, Promise<TData>>
protected resolvers: Map<
TKey,
[
resolve: (value: TData | PromiseLike<TData>) => void,
reject: (reason?: any) => void,
]
>

protected async executeBatchLoad(): Promise<void> {
if (this.queue.length === 0) return

Expand Down
8 changes: 7 additions & 1 deletion packages/core/src/utils/string.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expect, it } from "vitest"
import { pascalCase } from "./string"
import { capitalize, pascalCase } from "./string"

describe("toPascalCase", () => {
it("should convert kebab-case to PascalCase", () => {
Expand All @@ -26,3 +26,9 @@ describe("toPascalCase", () => {
expect(pascalCase("multiple-words-here")).toBe("MultipleWordsHere")
})
})

describe("capitalize", () => {
it("should capitalize the first letter of a string", () => {
expect(capitalize("hello")).toBe("Hello")
})
})
4 changes: 4 additions & 0 deletions packages/core/src/utils/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,7 @@ export function pascalCase(str: string): string {
)
.join("")
}

export function capitalize<T extends string>(str: T): Capitalize<T> {
return (str.slice(0, 1).toUpperCase() + str.slice(1)) as Capitalize<T>
}
2 changes: 2 additions & 0 deletions packages/drizzle/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
test/**/*.db*
.env
11 changes: 11 additions & 0 deletions packages/drizzle/drizzle-mysql.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { defineConfig } from "drizzle-kit"
import { config } from "./env.config"

export default defineConfig({
dialect: "mysql",
schema: "./test/schema/mysql.ts",
dbCredentials: {
url: config.mysqlUrl,
},
tablesFilter: ["drizzle_*"],
})
12 changes: 12 additions & 0 deletions packages/drizzle/drizzle-postgres.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import "dotenv/config"
import { defineConfig } from "drizzle-kit"
import { config } from "./env.config"

export default defineConfig({
dialect: "postgresql",
schema: "./test/schema/postgres.ts",
dbCredentials: {
url: config.postgresUrl,
},
tablesFilter: ["drizzle_*"],
})
9 changes: 9 additions & 0 deletions packages/drizzle/drizzle-sqlite-1.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { defineConfig } from "drizzle-kit"

export default defineConfig({
dialect: "sqlite",
schema: "./test/schema/sqlite.ts",
dbCredentials: {
url: "file:./test/schema/sqlite-1.db",
},
})
9 changes: 9 additions & 0 deletions packages/drizzle/drizzle-sqlite.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { defineConfig } from "drizzle-kit"

export default defineConfig({
dialect: "sqlite",
schema: "./test/schema/sqlite.ts",
dbCredentials: {
url: "file:./test/schema/sqlite.db",
},
})
9 changes: 9 additions & 0 deletions packages/drizzle/env.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import * as dotenv from "dotenv"

dotenv.config({ path: new URL("./.env", import.meta.url) })

export const config = {
mysqlUrl: process.env.MYSQL_URL ?? "mysql://root@localhost:3306/mysql",
postgresUrl:
process.env.POSTGRESQL_URL ?? "postgres://postgres@localhost:5432/postgres",
}
64 changes: 64 additions & 0 deletions packages/drizzle/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
{
"name": "@gqloom/drizzle",
"version": "0.6.0",
"description": "GQLoom integration with Drizzle ORM",
"type": "module",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"exports": {
".": {
"import": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
},
"require": {
"types": "./dist/index.d.cts",
"default": "./dist/index.cjs"
}
}
},
"scripts": {
"build": "tsup",
"push": "drizzle-kit push --config=drizzle-mysql.config.ts && drizzle-kit push --config=drizzle-postgres.config.ts && drizzle-kit push --config=drizzle-sqlite.config.ts && drizzle-kit push --config=drizzle-sqlite-1.config.ts",
"postinstall": "pnpm run push"
},
"files": ["dist"],
"keywords": [
"gqloom",
"graphql",
"schema",
"typescript",
"drizzle",
"drizzle-orm"
],
"author": "xcfox",
"license": "MIT",
"peerDependencies": {
"@gqloom/core": ">= 0.6.0",
"drizzle-orm": ">= 0.38.0",
"graphql": ">= 16.8.0"
},
"devDependencies": {
"@gqloom/core": "workspace:*",
"@libsql/client": "^0.14.0",
"@types/pg": "^8.11.10",
"dotenv": "^16.4.7",
"drizzle-kit": "^0.30.1",
"drizzle-orm": "^0.38.3",
"graphql": "^16.8.1",
"graphql-yoga": "^5.6.0",
"mysql2": "^3.12.0",
"pg": "^8.13.1",
"tsx": "^4.7.2",
"valibot": "1.0.0-beta.12"
},
"homepage": "https://gqloom.dev/",
"repository": {
"type": "git",
"url": "https://github.com/modevol-com/gqloom.git",
"directory": "packages/drizzle"
},
"publishConfig": {
"access": "public"
}
}
34 changes: 34 additions & 0 deletions packages/drizzle/src/helper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import type { Column, SQL } from "drizzle-orm"
import { sql } from "drizzle-orm"

/**
* Creates an IN clause for multiple columns
* Example: (col1, col2) IN ((val1, val2), (val3, val4))
*/
export function inArrayMultiple(
columns: Column[],
values: readonly unknown[][]
): SQL<unknown> {
// Early return for empty values
if (values.length === 0) {
return sql`FALSE`
}
// Create (col1, col2, ...) part
const columnsPart = sql`(${sql.join(
columns.map((c) => sql`${c}`),
sql`, `
)})`

// Create ((val1, val2), (val3, val4), ...) part
const valueTuples = values.map(
(tuple) =>
sql`(${sql.join(
tuple.map((v) => sql`${v}`),
sql`, `
)})`
)
const valuesPart = sql.join(valueTuples, sql`, `)

// Combine into final IN clause
return sql`${columnsPart} IN (${valuesPart})`
}
Loading
Loading