mirror of
https://github.com/documenso/documenso.git
synced 2025-11-17 02:01:33 +10:00
chore: tidy code
This commit is contained in:
@ -1,5 +1,5 @@
|
||||
/// <reference types="../types/next-auth.d.ts" />
|
||||
import { KyselyAdapter } from '@auth/kysely-adapter';
|
||||
import { PrismaAdapter } from '@next-auth/prisma-adapter';
|
||||
import { compare } from '@node-rs/bcrypt';
|
||||
import { verifyAuthenticationResponse } from '@simplewebauthn/server';
|
||||
import { DateTime } from 'luxon';
|
||||
@ -24,11 +24,9 @@ import { ZAuthenticationResponseJSONSchema } from '../types/webauthn';
|
||||
import { extractNextAuthRequestMetadata } from '../universal/extract-request-metadata';
|
||||
import { getAuthenticatorOptions } from '../utils/authenticator';
|
||||
import { ErrorCode } from './error-codes';
|
||||
import { db } from './kysely-db/db';
|
||||
|
||||
export const NEXT_AUTH_OPTIONS: AuthOptions = {
|
||||
//@ts-expect-error - https://github.com/nextauthjs/next-auth/issues/8660
|
||||
adapter: KyselyAdapter(db),
|
||||
adapter: PrismaAdapter(prisma),
|
||||
secret: process.env.NEXTAUTH_SECRET ?? 'secret',
|
||||
session: {
|
||||
strategy: 'jwt',
|
||||
|
||||
@ -1,14 +0,0 @@
|
||||
import { KyselyAuth } from '@auth/kysely-adapter';
|
||||
import type { Codegen } from '@auth/kysely-adapter';
|
||||
import { PostgresDialect } from 'kysely';
|
||||
import { Pool } from 'pg';
|
||||
|
||||
import type { DB } from '@documenso/prisma/generated/types';
|
||||
|
||||
export const db = new KyselyAuth<DB, Codegen>({
|
||||
dialect: new PostgresDialect({
|
||||
pool: new Pool({
|
||||
connectionString: process.env.DATABASE_URL,
|
||||
}),
|
||||
}),
|
||||
});
|
||||
@ -51,7 +51,7 @@
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/luxon": "^3.3.1",
|
||||
"@types/pg": "^8.11.4"
|
||||
"@types/pg": "^8.11.4",
|
||||
"@playwright/browser-chromium": "1.43.0"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,6 +1,8 @@
|
||||
import { sql } from 'kysely';
|
||||
import { DateTime } from 'luxon';
|
||||
|
||||
import { prisma } from '@documenso/prisma';
|
||||
import { kyselyPrisma } from '@documenso/prisma';
|
||||
import { DocumentStatus } from '@documenso/prisma/client';
|
||||
|
||||
export type GetCompletedDocumentsMonthlyResult = Array<{
|
||||
month: string;
|
||||
@ -8,24 +10,27 @@ export type GetCompletedDocumentsMonthlyResult = Array<{
|
||||
cume_count: number;
|
||||
}>;
|
||||
|
||||
type GetCompletedDocumentsMonthlyQueryResult = Array<{
|
||||
month: Date;
|
||||
count: bigint;
|
||||
cume_count: bigint;
|
||||
}>;
|
||||
|
||||
export const getCompletedDocumentsMonthly = async () => {
|
||||
const result = await prisma.$queryRaw<GetCompletedDocumentsMonthlyQueryResult>`
|
||||
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
|
||||
`;
|
||||
const qb = kyselyPrisma.$kysely
|
||||
.selectFrom('Document')
|
||||
.select(({ fn, ref }) => [
|
||||
fn<Date>('DATE_TRUNC', [sql.lit('MONTH'), ref('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'), ref('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) => ({
|
||||
month: DateTime.fromJSDate(row.month).toFormat('yyyy-MM'),
|
||||
|
||||
1
packages/prisma/.gitignore
vendored
Normal file
1
packages/prisma/.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
generated/
|
||||
@ -1,38 +1,31 @@
|
||||
import { remember } from '@epic-web/remember';
|
||||
import { PrismaClient } from '@prisma/client';
|
||||
import { Kysely, PostgresAdapter, PostgresIntrospector, PostgresQueryCompiler } from 'kysely';
|
||||
import kyselyExtension from 'prisma-extension-kysely';
|
||||
|
||||
import type { DB } from './generated/types.js';
|
||||
import type { DB } from './generated/types';
|
||||
import { getDatabaseUrl } from './helper';
|
||||
|
||||
declare global {
|
||||
// We need `var` to declare a global variable in TypeScript
|
||||
// eslint-disable-next-line no-var
|
||||
var prisma: PrismaClient | undefined;
|
||||
}
|
||||
|
||||
if (!globalThis.prisma) {
|
||||
globalThis.prisma = new PrismaClient({ datasourceUrl: getDatabaseUrl() });
|
||||
}
|
||||
|
||||
const _prisma =
|
||||
globalThis.prisma ||
|
||||
new PrismaClient({
|
||||
datasourceUrl: getDatabaseUrl(),
|
||||
});
|
||||
|
||||
export const prisma = _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 = remember(
|
||||
'prisma',
|
||||
() =>
|
||||
new PrismaClient({
|
||||
datasourceUrl: getDatabaseUrl(),
|
||||
}),
|
||||
);
|
||||
|
||||
export const getPrismaClient = () => prisma;
|
||||
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(),
|
||||
},
|
||||
}),
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
@ -12,13 +12,15 @@
|
||||
"prisma:generate": "prisma generate",
|
||||
"prisma:migrate-dev": "prisma migrate dev --skip-seed",
|
||||
"prisma:migrate-deploy": "prisma migrate deploy",
|
||||
"prisma:migrate-reset": "prisma migrate reset",
|
||||
"prisma:seed": "prisma db seed",
|
||||
"prisma:studio": "prisma studio"
|
||||
},
|
||||
"prisma": {
|
||||
"seed": "ts-node --transpileOnly --project ./tsconfig.seed.json ./seed-database.ts"
|
||||
"seed": "tsx ./seed-database.ts"
|
||||
},
|
||||
"dependencies": {
|
||||
"@epic-web/remember": "1.0.2",
|
||||
"@prisma/client": "5.4.2",
|
||||
"kysely": "^0.27.3",
|
||||
"prisma": "5.4.2",
|
||||
@ -29,7 +31,7 @@
|
||||
"dotenv": "^16.3.1",
|
||||
"dotenv-cli": "^7.3.0",
|
||||
"prisma-kysely": "^1.8.0",
|
||||
"ts-node": "^10.9.1",
|
||||
"tsx": "^4.11.0",
|
||||
"typescript": "5.2.2"
|
||||
}
|
||||
}
|
||||
|
||||
@ -2,6 +2,10 @@ generator kysely {
|
||||
provider = "prisma-kysely"
|
||||
}
|
||||
|
||||
generator client {
|
||||
provider = "prisma-client-js"
|
||||
}
|
||||
|
||||
datasource db {
|
||||
provider = "postgresql"
|
||||
url = env("NEXT_PRIVATE_DATABASE_URL")
|
||||
|
||||
Reference in New Issue
Block a user