Skip to content
This repository has been archived by the owner on Oct 30, 2024. It is now read-only.

Commit

Permalink
feat(sql): use TypedSQL for all queryRaw and kysely queries
Browse files Browse the repository at this point in the history
  • Loading branch information
jharrell committed Aug 4, 2024
1 parent 909c38f commit 0ace23e
Show file tree
Hide file tree
Showing 14 changed files with 3,678 additions and 8,162 deletions.
11,667 changes: 3,614 additions & 8,053 deletions package-lock.json

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"dx:down": "docker compose -f docker/development/compose.yml down",
"ci": "turbo run test:e2e",
"prisma:generate": "npm run with:env -- npm run prisma:generate -w @documenso/prisma",
"prisma:generate-sql": "npm run with:env -- npm run prisma:generate-sql -w @documenso/prisma",
"prisma:migrate-dev": "npm run with:env -- npm run prisma:migrate-dev -w @documenso/prisma",
"prisma:migrate-deploy": "npm run with:env -- npm run prisma:migrate-deploy -w @documenso/prisma",
"prisma:migrate-reset": "npm run with:env -- npm run prisma:migrate-reset -w @documenso/prisma",
Expand Down Expand Up @@ -80,4 +81,4 @@
"trigger.dev": {
"endpointId": "documenso-app"
}
}
}
4 changes: 1 addition & 3 deletions packages/lib/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
"clean": "rimraf node_modules"
},
"dependencies": {
"@auth/kysely-adapter": "^0.6.0",
"@aws-sdk/client-s3": "^3.410.0",
"@aws-sdk/cloudfront-signer": "^3.410.0",
"@aws-sdk/s3-request-presigner": "^3.410.0",
Expand All @@ -37,7 +36,6 @@
"@upstash/redis": "^1.20.6",
"@vvo/tzdb": "^6.117.0",
"inngest": "^3.19.13",
"kysely": "^0.26.3",
"luxon": "^3.4.0",
"micro": "^10.0.1",
"nanoid": "^4.0.2",
Expand All @@ -59,4 +57,4 @@
"@types/luxon": "^3.3.1",
"@types/pg": "^8.11.4"
}
}
}
19 changes: 2 additions & 17 deletions packages/lib/server-only/admin/get-users-stats.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { DateTime } from 'luxon';

import { prisma } from '@documenso/prisma';
import { SQL, prisma } from '@documenso/prisma';
import { DocumentStatus, SubscriptionStatus } from '@documenso/prisma/client';

export const getUsersCount = async () => {
Expand Down Expand Up @@ -56,23 +56,8 @@ export type GetUserWithDocumentMonthlyGrowth = Array<{
signed_count: number;
}>;

type GetUserWithDocumentMonthlyGrowthQueryResult = Array<{
month: Date;
count: bigint;
signed_count: bigint;
}>;

export const getUserWithSignedDocumentMonthlyGrowth = async () => {
const result = await prisma.$queryRaw<GetUserWithDocumentMonthlyGrowthQueryResult>`
SELECT
DATE_TRUNC('month', "Document"."createdAt") AS "month",
COUNT(DISTINCT "Document"."userId") as "count",
COUNT(DISTINCT CASE WHEN "Document"."status" = 'COMPLETED' THEN "Document"."userId" END) as "signed_count"
FROM "Document"
GROUP BY "month"
ORDER BY "month" DESC
LIMIT 12
`;
const result = await prisma.$queryRawTyped(SQL.userWithSignedDocumentMonthlyGrowth());

return result.map((row) => ({
month: DateTime.fromJSDate(row.month).toFormat('yyyy-MM'),
Expand Down
22 changes: 2 additions & 20 deletions packages/lib/server-only/user/get-monthly-completed-document.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,9 @@
import { DateTime } from 'luxon';

import { kyselyPrisma, sql } from '@documenso/prisma';
import { DocumentStatus } from '@documenso/prisma/client';
import { SQL, prisma } from '@documenso/prisma';

export const getCompletedDocumentsMonthly = async () => {
const qb = kyselyPrisma.$kysely
.selectFrom('Document')
.select(({ fn }) => [
fn<Date>('DATE_TRUNC', [sql.lit('MONTH'), 'Document.updatedAt']).as('month'),
fn.count('id').as('count'),
fn
.sum(fn.count('id'))
// Feels like a bug in the Kysely extension but I just can not do this orderBy in a type-safe manner
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions, @typescript-eslint/no-explicit-any
.over((ob) => ob.orderBy(fn('DATE_TRUNC', [sql.lit('MONTH'), 'Document.updatedAt']) as any))
.as('cume_count'),
])
.where(() => sql`"Document"."status" = ${DocumentStatus.COMPLETED}::"DocumentStatus"`)
.groupBy('month')
.orderBy('month', 'desc')
.limit(12);

const result = await qb.execute();
const result = await prisma.$queryRawTyped(SQL.completedDocumentsMonthly());

return result.map((row) => ({
month: DateTime.fromJSDate(row.month).toFormat('yyyy-MM'),
Expand Down
21 changes: 2 additions & 19 deletions packages/lib/server-only/user/get-signer-conversion.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,9 @@
import { DateTime } from 'luxon';

import { kyselyPrisma, sql } from '@documenso/prisma';
import { SQL, prisma } from '@documenso/prisma';

export const getSignerConversionMonthly = async () => {
const qb = kyselyPrisma.$kysely
.selectFrom('Recipient')
.innerJoin('User', 'Recipient.email', 'User.email')
.select(({ fn }) => [
fn<Date>('DATE_TRUNC', [sql.lit('MONTH'), 'User.createdAt']).as('month'),
fn.count('Recipient.email').distinct().as('count'),
fn
.sum(fn.count('Recipient.email').distinct())
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions, @typescript-eslint/no-explicit-any
.over((ob) => ob.orderBy(fn('DATE_TRUNC', [sql.lit('MONTH'), 'User.createdAt']) as any))
.as('cume_count'),
])
.where('Recipient.signedAt', 'is not', null)
.where('Recipient.signedAt', '<', (eb) => eb.ref('User.createdAt'))
.groupBy(({ fn }) => fn('DATE_TRUNC', [sql.lit('MONTH'), 'User.createdAt']))
.orderBy('month', 'desc');

const result = await qb.execute();
const result = await prisma.$queryRawTyped(SQL.signerConversionMonthly());

return result.map((row) => ({
month: DateTime.fromJSDate(row.month).toFormat('yyyy-MM'),
Expand Down
20 changes: 2 additions & 18 deletions packages/lib/server-only/user/get-user-monthly-growth.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,9 @@
import { DateTime } from 'luxon';

import { kyselyPrisma, sql } from '@documenso/prisma';
import { SQL, prisma } from '@documenso/prisma';

export const getUserMonthlyGrowth = async () => {
const qb = kyselyPrisma.$kysely
.selectFrom('User')
.select(({ fn }) => [
fn<Date>('DATE_TRUNC', [sql.lit('MONTH'), 'User.createdAt']).as('month'),
fn.count('id').as('count'),
fn
.sum(fn.count('id'))
// Feels like a bug in the Kysely extension but I just can not do this orderBy in a type-safe manner
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions, @typescript-eslint/no-explicit-any
.over((ob) => ob.orderBy(fn('DATE_TRUNC', [sql.lit('MONTH'), 'User.createdAt']) as any))
.as('cume_count'),
])
.groupBy('month')
.orderBy('month', 'desc')
.limit(12);

const result = await qb.execute();
const result = await prisma.$queryRawTyped(SQL.userMonthlyGrowth());

return result.map((row) => ({
month: DateTime.fromJSDate(row.month).toFormat('yyyy-MM'),
Expand Down
32 changes: 12 additions & 20 deletions packages/prisma/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { PrismaClient } from '@prisma/client';
import { Kysely, PostgresAdapter, PostgresIntrospector, PostgresQueryCompiler } from 'kysely';
import kyselyExtension from 'prisma-extension-kysely';

import type { DB } from './generated/types';
import { getDatabaseUrl } from './helper';
import {
completedDocumentsMonthly,
signerConversionMonthly,
userMonthlyGrowth,
userWithSignedDocumentMonthlyGrowth,
} from './node_modules/.prisma/client/sql';
import { remember } from './utils/remember';

export const prisma = remember(
Expand All @@ -14,20 +17,9 @@ export const prisma = remember(
}),
);

export const kyselyPrisma = remember('kyselyPrisma', () =>
prisma.$extends(
kyselyExtension({
kysely: (driver) =>
new Kysely<DB>({
dialect: {
createAdapter: () => new PostgresAdapter(),
createDriver: () => driver,
createIntrospector: (db) => new PostgresIntrospector(db),
createQueryCompiler: () => new PostgresQueryCompiler(),
},
}),
}),
),
);

export { sql } from 'kysely';
export const SQL = {
completedDocumentsMonthly,
signerConversionMonthly,
userMonthlyGrowth,
userWithSignedDocumentMonthlyGrowth,
};
10 changes: 4 additions & 6 deletions packages/prisma/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"clean": "rimraf node_modules",
"post-install": "prisma generate",
"prisma:generate": "prisma generate",
"prisma:generate-sql": "prisma generate --sql",
"prisma:migrate-dev": "prisma migrate dev --skip-seed",
"prisma:migrate-deploy": "prisma migrate deploy",
"prisma:migrate-reset": "prisma migrate reset",
Expand All @@ -21,17 +22,14 @@
"seed": "tsx ./seed-database.ts"
},
"dependencies": {
"@prisma/client": "5.4.2",
"kysely": "^0.27.3",
"prisma": "5.4.2",
"prisma-extension-kysely": "^2.1.0",
"@prisma/client": "5.18.0-integration-feat-typed-sql.1",
"prisma": "5.18.0-integration-feat-typed-sql.1",
"ts-pattern": "^5.0.6"
},
"devDependencies": {
"dotenv": "^16.3.1",
"dotenv-cli": "^7.3.0",
"prisma-kysely": "^1.8.0",
"tsx": "^4.11.0",
"typescript": "5.2.2"
}
}
}
7 changes: 2 additions & 5 deletions packages/prisma/schema.prisma
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
generator kysely {
provider = "prisma-kysely"
}

generator client {
provider = "prisma-client-js"
provider = "prisma-client-js"
previewFeatures = ["tracing"]
}

datasource db {
Expand Down
9 changes: 9 additions & 0 deletions packages/prisma/sql/completedDocumentsMonthly.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
SELECT
DATE_TRUNC('month', "updatedAt") AS "month",
COUNT("id") as "count",
SUM(COUNT("id")) OVER (ORDER BY DATE_TRUNC('month', "updatedAt")) as "cume_count"
FROM "Document"
WHERE "status" = 'COMPLETED'
GROUP BY "month"
ORDER BY "month" DESC
LIMIT 12
10 changes: 10 additions & 0 deletions packages/prisma/sql/signerConversionMonthly.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
SELECT
DATE_TRUNC('month', "User"."createdAt") AS "month",
COUNT(DISTINCT "Recipient"."email") as "count",
SUM(COUNT(DISTINCT "Recipient"."email")) OVER (ORDER BY DATE_TRUNC('month', "User"."createdAt")) as "cume_count"
FROM "Recipient"
INNER JOIN "User" ON "Recipient"."email" = "User"."email"
WHERE "Recipient"."signedAt" IS NOT NULL
AND "Recipient"."signedAt" < "User"."createdAt"
GROUP BY DATE_TRUNC('month', "User"."createdAt")
ORDER BY "month" DESC
8 changes: 8 additions & 0 deletions packages/prisma/sql/userMonthlyGrowth.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
SELECT
DATE_TRUNC('MONTH', "User"."createdAt") AS "month",
COUNT("id") AS "count",
SUM(COUNT("id")) OVER (ORDER BY DATE_TRUNC('MONTH', "User"."createdAt")) AS "cume_count"
FROM "User"
GROUP BY "month"
ORDER BY "month" DESC
LIMIT 12;
8 changes: 8 additions & 0 deletions packages/prisma/sql/userWithSignedDocumentMonthlyGrowth.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
SELECT
DATE_TRUNC('month', "Document"."createdAt") AS "month",
COUNT(DISTINCT "Document"."userId") as "count",
COUNT(DISTINCT CASE WHEN "Document"."status" = 'COMPLETED' THEN "Document"."userId" END) as "signed_count"
FROM "Document"
GROUP BY "month"
ORDER BY "month" DESC
LIMIT 12

0 comments on commit 0ace23e

Please sign in to comment.