mirror of
https://github.com/documenso/documenso.git
synced 2025-11-14 00:32:43 +10:00
feat: add vercel build script
This commit is contained in:
52
packages/prisma/helper.ts
Normal file
52
packages/prisma/helper.ts
Normal file
@ -0,0 +1,52 @@
|
||||
/// <reference types="@documenso/tsconfig/process-env.d.ts" />
|
||||
|
||||
export const getDatabaseUrl = () => {
|
||||
if (process.env.NEXT_PRIVATE_DATABASE_URL) {
|
||||
return process.env.NEXT_PRIVATE_DATABASE_URL;
|
||||
}
|
||||
|
||||
if (process.env.POSTGRES_URL) {
|
||||
process.env.NEXT_PRIVATE_DATABASE_URL = process.env.POSTGRES_URL;
|
||||
process.env.NEXT_PRIVATE_DIRECT_DATABASE_URL = process.env.POSTGRES_URL;
|
||||
}
|
||||
|
||||
if (process.env.DATABASE_URL) {
|
||||
process.env.NEXT_PRIVATE_DATABASE_URL = process.env.DATABASE_URL;
|
||||
process.env.NEXT_PRIVATE_DIRECT_DATABASE_URL = process.env.DATABASE_URL;
|
||||
}
|
||||
|
||||
if (process.env.POSTGRES_PRISMA_URL) {
|
||||
process.env.NEXT_PRIVATE_DATABASE_URL = process.env.POSTGRES_PRISMA_URL;
|
||||
}
|
||||
|
||||
if (process.env.POSTGRES_URL_NON_POOLING) {
|
||||
process.env.NEXT_PRIVATE_DIRECT_DATABASE_URL = process.env.POSTGRES_URL_NON_POOLING;
|
||||
}
|
||||
|
||||
// We change the protocol from `postgres:` to `https:` so we can construct a easily
|
||||
// mofifiable URL.
|
||||
const url = new URL(process.env.NEXT_PRIVATE_DATABASE_URL.replace('postgres://', 'https://'));
|
||||
|
||||
// If we're using a connection pool, we need to let Prisma know that
|
||||
// we're using PgBouncer.
|
||||
if (process.env.NEXT_PRIVATE_DATABASE_URL !== process.env.NEXT_PRIVATE_DIRECT_DATABASE_URL) {
|
||||
url.searchParams.set('pgbouncer', 'true');
|
||||
|
||||
process.env.NEXT_PRIVATE_DATABASE_URL = url.toString().replace('https://', 'postgres://');
|
||||
}
|
||||
|
||||
// Support for neon.tech (Neon Database)
|
||||
if (url.hostname.endsWith('neon.tech')) {
|
||||
const [projectId, ...rest] = url.hostname.split('.');
|
||||
|
||||
if (!projectId.endsWith('-bouncer')) {
|
||||
url.hostname = `${projectId}-pooler.${rest.join('.')}`;
|
||||
}
|
||||
|
||||
url.searchParams.set('pgbouncer', 'true');
|
||||
|
||||
process.env.NEXT_PRIVATE_DATABASE_URL = url.toString().replace('https://', 'postgres://');
|
||||
}
|
||||
|
||||
return process.env.NEXT_PRIVATE_DATABASE_URL;
|
||||
};
|
||||
@ -1,5 +1,7 @@
|
||||
import { PrismaClient } from '@prisma/client';
|
||||
|
||||
import { getDatabaseUrl } from './helper';
|
||||
|
||||
declare global {
|
||||
// We need `var` to declare a global variable in TypeScript
|
||||
// eslint-disable-next-line no-var
|
||||
@ -7,9 +9,13 @@ declare global {
|
||||
}
|
||||
|
||||
if (!globalThis.prisma) {
|
||||
globalThis.prisma = new PrismaClient();
|
||||
globalThis.prisma = new PrismaClient({ datasourceUrl: getDatabaseUrl() });
|
||||
}
|
||||
|
||||
export const prisma = globalThis.prisma || new PrismaClient();
|
||||
export const prisma =
|
||||
globalThis.prisma ||
|
||||
new PrismaClient({
|
||||
datasourceUrl: getDatabaseUrl(),
|
||||
});
|
||||
|
||||
export const getPrismaClient = () => prisma;
|
||||
|
||||
@ -9,10 +9,18 @@
|
||||
"format": "prisma format",
|
||||
"prisma:generate": "prisma generate",
|
||||
"prisma:migrate-dev": "prisma migrate dev",
|
||||
"prisma:migrate-deploy": "prisma migrate deploy"
|
||||
"prisma:migrate-deploy": "prisma migrate deploy",
|
||||
"prisma:seed": "prisma db seed"
|
||||
},
|
||||
"prisma": {
|
||||
"seed": "ts-node --transpileOnly --skipProject ./seed-database.ts"
|
||||
},
|
||||
"dependencies": {
|
||||
"@prisma/client": "5.0.0",
|
||||
"prisma": "5.0.0"
|
||||
"@prisma/client": "5.3.1",
|
||||
"prisma": "5.3.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"ts-node": "^10.9.1",
|
||||
"typescript": "^5.1.6"
|
||||
}
|
||||
}
|
||||
|
||||
82
packages/prisma/seed-database.ts
Normal file
82
packages/prisma/seed-database.ts
Normal file
@ -0,0 +1,82 @@
|
||||
import { DocumentDataType, Role } from '@prisma/client';
|
||||
import fs from 'node:fs';
|
||||
import path from 'node:path';
|
||||
|
||||
import { hashSync } from '@documenso/lib/server-only/auth/hash';
|
||||
|
||||
import { prisma } from './index';
|
||||
|
||||
const seedDatabase = async () => {
|
||||
const examplePdf = fs
|
||||
.readFileSync(path.join(__dirname, '../../assets/example.pdf'))
|
||||
.toString('base64');
|
||||
|
||||
const exampleUser = await prisma.user.upsert({
|
||||
where: {
|
||||
email: 'example@documenso.com',
|
||||
},
|
||||
create: {
|
||||
name: 'Example User',
|
||||
email: 'example@documenso.com',
|
||||
password: hashSync('password'),
|
||||
roles: [Role.USER],
|
||||
},
|
||||
update: {},
|
||||
});
|
||||
|
||||
const adminUser = await prisma.user.upsert({
|
||||
where: {
|
||||
email: 'admin@documenso.com',
|
||||
},
|
||||
create: {
|
||||
name: 'Admin User',
|
||||
email: 'admin@documenso.com',
|
||||
password: hashSync('password'),
|
||||
roles: [Role.USER, Role.ADMIN],
|
||||
},
|
||||
update: {},
|
||||
});
|
||||
|
||||
const examplePdfData = await prisma.documentData.upsert({
|
||||
where: {
|
||||
id: 'clmn0kv5k0000pe04vcqg5zla',
|
||||
},
|
||||
create: {
|
||||
id: 'clmn0kv5k0000pe04vcqg5zla',
|
||||
type: DocumentDataType.BYTES_64,
|
||||
data: examplePdf,
|
||||
initialData: examplePdf,
|
||||
},
|
||||
update: {},
|
||||
});
|
||||
|
||||
await prisma.document.upsert({
|
||||
where: {
|
||||
id: 1,
|
||||
},
|
||||
create: {
|
||||
id: 1,
|
||||
title: 'Example Document',
|
||||
documentDataId: examplePdfData.id,
|
||||
userId: exampleUser.id,
|
||||
Recipient: {
|
||||
create: {
|
||||
name: String(adminUser.name),
|
||||
email: adminUser.email,
|
||||
token: Math.random().toString(36).slice(2, 9),
|
||||
},
|
||||
},
|
||||
},
|
||||
update: {},
|
||||
});
|
||||
};
|
||||
|
||||
seedDatabase()
|
||||
.then(() => {
|
||||
console.log('Database seeded');
|
||||
process.exit(0);
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error(error);
|
||||
process.exit(1);
|
||||
});
|
||||
Reference in New Issue
Block a user