mirror of
https://github.com/documenso/documenso.git
synced 2025-11-10 04:22:32 +10:00
feat: add kysely for raw type-safe SQL queries (#1041)
This commit is contained in:
@ -3,11 +3,11 @@
|
|||||||
import { DateTime } from 'luxon';
|
import { DateTime } from 'luxon';
|
||||||
import { Bar, BarChart, ResponsiveContainer, Tooltip, XAxis, YAxis } from 'recharts';
|
import { Bar, BarChart, ResponsiveContainer, Tooltip, XAxis, YAxis } from 'recharts';
|
||||||
|
|
||||||
import type { GetUserMonthlyGrowthResult } from '@documenso/lib/server-only/user/get-user-monthly-growth';
|
import type { GetCompletedDocumentsMonthlyResult } from '@documenso/lib/server-only/user/get-monthly-completed-document';
|
||||||
|
|
||||||
export type MonthlyCompletedDocumentsChartProps = {
|
export type MonthlyCompletedDocumentsChartProps = {
|
||||||
className?: string;
|
className?: string;
|
||||||
data: GetUserMonthlyGrowthResult;
|
data: GetCompletedDocumentsMonthlyResult;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const MonthlyCompletedDocumentsChart = ({
|
export const MonthlyCompletedDocumentsChart = ({
|
||||||
|
|||||||
@ -3,11 +3,11 @@
|
|||||||
import { DateTime } from 'luxon';
|
import { DateTime } from 'luxon';
|
||||||
import { Bar, BarChart, ResponsiveContainer, Tooltip, XAxis, YAxis } from 'recharts';
|
import { Bar, BarChart, ResponsiveContainer, Tooltip, XAxis, YAxis } from 'recharts';
|
||||||
|
|
||||||
import type { GetUserMonthlyGrowthResult } from '@documenso/lib/server-only/user/get-user-monthly-growth';
|
import type { GetCompletedDocumentsMonthlyResult } from '@documenso/lib/server-only/user/get-monthly-completed-document';
|
||||||
|
|
||||||
export type TotalSignedDocumentsChartProps = {
|
export type TotalSignedDocumentsChartProps = {
|
||||||
className?: string;
|
className?: string;
|
||||||
data: GetUserMonthlyGrowthResult;
|
data: GetCompletedDocumentsMonthlyResult;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const TotalSignedDocumentsChart = ({ className, data }: TotalSignedDocumentsChartProps) => {
|
export const TotalSignedDocumentsChart = ({ className, data }: TotalSignedDocumentsChartProps) => {
|
||||||
|
|||||||
2273
package-lock.json
generated
2273
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -19,6 +19,7 @@
|
|||||||
"prisma:generate": "npm run with:env -- npm run prisma:generate -w @documenso/prisma",
|
"prisma:generate": "npm run with:env -- npm run prisma:generate -w @documenso/prisma",
|
||||||
"prisma:migrate-dev": "npm run with:env -- npm run prisma:migrate-dev -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-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",
|
||||||
"prisma:seed": "npm run with:env -- npm run prisma:seed -w @documenso/prisma",
|
"prisma:seed": "npm run with:env -- npm run prisma:seed -w @documenso/prisma",
|
||||||
"prisma:studio": "npm run with:env -- npm run prisma:studio -w @documenso/prisma",
|
"prisma:studio": "npm run with:env -- npm run prisma:studio -w @documenso/prisma",
|
||||||
"with:env": "dotenv -e .env -e .env.local --",
|
"with:env": "dotenv -e .env -e .env.local --",
|
||||||
@ -60,4 +61,4 @@
|
|||||||
"next": "14.0.3"
|
"next": "14.0.3"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -16,6 +16,7 @@
|
|||||||
"clean": "rimraf node_modules"
|
"clean": "rimraf node_modules"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"@auth/kysely-adapter": "^0.6.0",
|
||||||
"@aws-sdk/client-s3": "^3.410.0",
|
"@aws-sdk/client-s3": "^3.410.0",
|
||||||
"@aws-sdk/cloudfront-signer": "^3.410.0",
|
"@aws-sdk/cloudfront-signer": "^3.410.0",
|
||||||
"@aws-sdk/s3-request-presigner": "^3.410.0",
|
"@aws-sdk/s3-request-presigner": "^3.410.0",
|
||||||
@ -27,18 +28,20 @@
|
|||||||
"@next-auth/prisma-adapter": "1.0.7",
|
"@next-auth/prisma-adapter": "1.0.7",
|
||||||
"@noble/ciphers": "0.4.0",
|
"@noble/ciphers": "0.4.0",
|
||||||
"@noble/hashes": "1.3.2",
|
"@noble/hashes": "1.3.2",
|
||||||
|
"@node-rs/bcrypt": "^1.10.0",
|
||||||
"@pdf-lib/fontkit": "^1.1.1",
|
"@pdf-lib/fontkit": "^1.1.1",
|
||||||
"@scure/base": "^1.1.3",
|
"@scure/base": "^1.1.3",
|
||||||
"@sindresorhus/slugify": "^2.2.1",
|
"@sindresorhus/slugify": "^2.2.1",
|
||||||
"@upstash/redis": "^1.20.6",
|
"@upstash/redis": "^1.20.6",
|
||||||
"@vvo/tzdb": "^6.117.0",
|
"@vvo/tzdb": "^6.117.0",
|
||||||
"@node-rs/bcrypt": "^1.10.0",
|
"kysely": "^0.26.3",
|
||||||
"luxon": "^3.4.0",
|
"luxon": "^3.4.0",
|
||||||
"nanoid": "^4.0.2",
|
"nanoid": "^4.0.2",
|
||||||
"next": "14.0.3",
|
"next": "14.0.3",
|
||||||
"next-auth": "4.24.5",
|
"next-auth": "4.24.5",
|
||||||
"oslo": "^0.17.0",
|
"oslo": "^0.17.0",
|
||||||
"pdf-lib": "^1.17.1",
|
"pdf-lib": "^1.17.1",
|
||||||
|
"pg": "^8.11.3",
|
||||||
"playwright": "1.43.0",
|
"playwright": "1.43.0",
|
||||||
"react": "18.2.0",
|
"react": "18.2.0",
|
||||||
"remeda": "^1.27.1",
|
"remeda": "^1.27.1",
|
||||||
@ -48,6 +51,7 @@
|
|||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/luxon": "^3.3.1",
|
"@types/luxon": "^3.3.1",
|
||||||
|
"@types/pg": "^8.11.4",
|
||||||
"@playwright/browser-chromium": "1.43.0"
|
"@playwright/browser-chromium": "1.43.0"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,31 +1,27 @@
|
|||||||
import { DateTime } from 'luxon';
|
import { DateTime } from 'luxon';
|
||||||
|
|
||||||
import { prisma } from '@documenso/prisma';
|
import { kyselyPrisma, sql } from '@documenso/prisma';
|
||||||
|
import { DocumentStatus } from '@documenso/prisma/client';
|
||||||
export type GetCompletedDocumentsMonthlyResult = Array<{
|
|
||||||
month: string;
|
|
||||||
count: number;
|
|
||||||
cume_count: number;
|
|
||||||
}>;
|
|
||||||
|
|
||||||
type GetCompletedDocumentsMonthlyQueryResult = Array<{
|
|
||||||
month: Date;
|
|
||||||
count: bigint;
|
|
||||||
cume_count: bigint;
|
|
||||||
}>;
|
|
||||||
|
|
||||||
export const getCompletedDocumentsMonthly = async () => {
|
export const getCompletedDocumentsMonthly = async () => {
|
||||||
const result = await prisma.$queryRaw<GetCompletedDocumentsMonthlyQueryResult>`
|
const qb = kyselyPrisma.$kysely
|
||||||
SELECT
|
.selectFrom('Document')
|
||||||
DATE_TRUNC('month', "updatedAt") AS "month",
|
.select(({ fn }) => [
|
||||||
COUNT("id") as "count",
|
fn<Date>('DATE_TRUNC', [sql.lit('MONTH'), 'Document.updatedAt']).as('month'),
|
||||||
SUM(COUNT("id")) OVER (ORDER BY DATE_TRUNC('month', "updatedAt")) as "cume_count"
|
fn.count('id').as('count'),
|
||||||
FROM "Document"
|
fn
|
||||||
WHERE "status" = 'COMPLETED'
|
.sum(fn.count('id'))
|
||||||
GROUP BY "month"
|
// Feels like a bug in the Kysely extension but I just can not do this orderBy in a type-safe manner
|
||||||
ORDER BY "month" DESC
|
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions, @typescript-eslint/no-explicit-any
|
||||||
LIMIT 12
|
.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();
|
||||||
|
|
||||||
return result.map((row) => ({
|
return result.map((row) => ({
|
||||||
month: DateTime.fromJSDate(row.month).toFormat('yyyy-MM'),
|
month: DateTime.fromJSDate(row.month).toFormat('yyyy-MM'),
|
||||||
@ -33,3 +29,7 @@ export const getCompletedDocumentsMonthly = async () => {
|
|||||||
cume_count: Number(row.cume_count),
|
cume_count: Number(row.cume_count),
|
||||||
}));
|
}));
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export type GetCompletedDocumentsMonthlyResult = Awaited<
|
||||||
|
ReturnType<typeof getCompletedDocumentsMonthly>
|
||||||
|
>;
|
||||||
|
|||||||
@ -1,30 +1,25 @@
|
|||||||
import { DateTime } from 'luxon';
|
import { DateTime } from 'luxon';
|
||||||
|
|
||||||
import { prisma } from '@documenso/prisma';
|
import { kyselyPrisma, sql } from '@documenso/prisma';
|
||||||
|
|
||||||
export type GetUserMonthlyGrowthResult = Array<{
|
|
||||||
month: string;
|
|
||||||
count: number;
|
|
||||||
cume_count: number;
|
|
||||||
}>;
|
|
||||||
|
|
||||||
type GetUserMonthlyGrowthQueryResult = Array<{
|
|
||||||
month: Date;
|
|
||||||
count: bigint;
|
|
||||||
cume_count: bigint;
|
|
||||||
}>;
|
|
||||||
|
|
||||||
export const getUserMonthlyGrowth = async () => {
|
export const getUserMonthlyGrowth = async () => {
|
||||||
const result = await prisma.$queryRaw<GetUserMonthlyGrowthQueryResult>`
|
const qb = kyselyPrisma.$kysely
|
||||||
SELECT
|
.selectFrom('User')
|
||||||
DATE_TRUNC('month', "createdAt") AS "month",
|
.select(({ fn }) => [
|
||||||
COUNT("id") as "count",
|
fn<Date>('DATE_TRUNC', [sql.lit('MONTH'), 'User.createdAt']).as('month'),
|
||||||
SUM(COUNT("id")) OVER (ORDER BY DATE_TRUNC('month', "createdAt")) as "cume_count"
|
fn.count('id').as('count'),
|
||||||
FROM "User"
|
fn
|
||||||
GROUP BY "month"
|
.sum(fn.count('id'))
|
||||||
ORDER BY "month" DESC
|
// Feels like a bug in the Kysely extension but I just can not do this orderBy in a type-safe manner
|
||||||
LIMIT 12
|
// 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();
|
||||||
|
|
||||||
return result.map((row) => ({
|
return result.map((row) => ({
|
||||||
month: DateTime.fromJSDate(row.month).toFormat('yyyy-MM'),
|
month: DateTime.fromJSDate(row.month).toFormat('yyyy-MM'),
|
||||||
@ -32,3 +27,5 @@ export const getUserMonthlyGrowth = async () => {
|
|||||||
cume_count: Number(row.cume_count),
|
cume_count: Number(row.cume_count),
|
||||||
}));
|
}));
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export type GetUserMonthlyGrowthResult = Awaited<ReturnType<typeof getUserMonthlyGrowth>>;
|
||||||
|
|||||||
1
packages/prisma/.gitignore
vendored
Normal file
1
packages/prisma/.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
generated/
|
||||||
@ -1,21 +1,33 @@
|
|||||||
import { PrismaClient } from '@prisma/client';
|
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 { getDatabaseUrl } from './helper';
|
||||||
|
import { remember } from './utils/remember';
|
||||||
|
|
||||||
declare global {
|
export const prisma = remember(
|
||||||
// We need `var` to declare a global variable in TypeScript
|
'prisma',
|
||||||
// eslint-disable-next-line no-var
|
() =>
|
||||||
var prisma: PrismaClient | undefined;
|
new PrismaClient({
|
||||||
}
|
datasourceUrl: getDatabaseUrl(),
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
|
||||||
if (!globalThis.prisma) {
|
export const kyselyPrisma = remember('kyselyPrisma', () =>
|
||||||
globalThis.prisma = new PrismaClient({ datasourceUrl: getDatabaseUrl() });
|
prisma.$extends(
|
||||||
}
|
kyselyExtension({
|
||||||
|
kysely: (driver) =>
|
||||||
|
new Kysely<DB>({
|
||||||
|
dialect: {
|
||||||
|
createAdapter: () => new PostgresAdapter(),
|
||||||
|
createDriver: () => driver,
|
||||||
|
createIntrospector: (db) => new PostgresIntrospector(db),
|
||||||
|
createQueryCompiler: () => new PostgresQueryCompiler(),
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
}),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
export const prisma =
|
export { sql } from 'kysely';
|
||||||
globalThis.prisma ||
|
|
||||||
new PrismaClient({
|
|
||||||
datasourceUrl: getDatabaseUrl(),
|
|
||||||
});
|
|
||||||
|
|
||||||
export const getPrismaClient = () => prisma;
|
|
||||||
|
|||||||
@ -12,21 +12,25 @@
|
|||||||
"prisma:generate": "prisma generate",
|
"prisma:generate": "prisma generate",
|
||||||
"prisma:migrate-dev": "prisma migrate dev --skip-seed",
|
"prisma:migrate-dev": "prisma migrate dev --skip-seed",
|
||||||
"prisma:migrate-deploy": "prisma migrate deploy",
|
"prisma:migrate-deploy": "prisma migrate deploy",
|
||||||
|
"prisma:migrate-reset": "prisma migrate reset",
|
||||||
"prisma:seed": "prisma db seed",
|
"prisma:seed": "prisma db seed",
|
||||||
"prisma:studio": "prisma studio"
|
"prisma:studio": "prisma studio"
|
||||||
},
|
},
|
||||||
"prisma": {
|
"prisma": {
|
||||||
"seed": "ts-node --transpileOnly --project ./tsconfig.seed.json ./seed-database.ts"
|
"seed": "tsx ./seed-database.ts"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@prisma/client": "5.4.2",
|
"@prisma/client": "5.4.2",
|
||||||
|
"kysely": "^0.27.3",
|
||||||
"prisma": "5.4.2",
|
"prisma": "5.4.2",
|
||||||
|
"prisma-extension-kysely": "^2.1.0",
|
||||||
"ts-pattern": "^5.0.6"
|
"ts-pattern": "^5.0.6"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"dotenv": "^16.3.1",
|
"dotenv": "^16.3.1",
|
||||||
"dotenv-cli": "^7.3.0",
|
"dotenv-cli": "^7.3.0",
|
||||||
"ts-node": "^10.9.1",
|
"prisma-kysely": "^1.8.0",
|
||||||
|
"tsx": "^4.11.0",
|
||||||
"typescript": "5.2.2"
|
"typescript": "5.2.2"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,3 +1,7 @@
|
|||||||
|
generator kysely {
|
||||||
|
provider = "prisma-kysely"
|
||||||
|
}
|
||||||
|
|
||||||
generator client {
|
generator client {
|
||||||
provider = "prisma-client-js"
|
provider = "prisma-client-js"
|
||||||
}
|
}
|
||||||
|
|||||||
18
packages/prisma/utils/remember.ts
Normal file
18
packages/prisma/utils/remember.ts
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
declare global {
|
||||||
|
// eslint-disable-next-line no-var, @typescript-eslint/no-explicit-any
|
||||||
|
var __prisma_remember: Map<string, any>;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function remember<T>(name: string, getValue: () => T): T {
|
||||||
|
const thusly = globalThis;
|
||||||
|
|
||||||
|
if (!thusly.__prisma_remember) {
|
||||||
|
thusly.__prisma_remember = new Map();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!thusly.__prisma_remember.has(name)) {
|
||||||
|
thusly.__prisma_remember.set(name, getValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
return thusly.__prisma_remember.get(name);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user