Skip to content

Commit

Permalink
1 prisma client
Browse files Browse the repository at this point in the history
  • Loading branch information
jho44 committed Aug 14, 2023
1 parent f814ac6 commit 22c08bf
Show file tree
Hide file tree
Showing 14 changed files with 34 additions and 32 deletions.
4 changes: 1 addition & 3 deletions src/hooks.server.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import type { Handle, RequestEvent } from '@sveltejs/kit';
import { PrismaClient } from '@prisma/client';
import type { User, PhoneContactPermissions } from '@prisma/client';
import * as cron from 'node-cron';
import { sendNotif } from '$lib/server/twilio';
import { toLocalTimezone } from '$lib/date';

const prisma = new PrismaClient();
import prisma from '$lib/prisma';

import { redirect } from '@sveltejs/kit';
import type { MaybePromise, ResolveOptions } from '@sveltejs/kit/types/internal';
Expand Down
14 changes: 14 additions & 0 deletions src/lib/prisma.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { PrismaClient } from '@prisma/client';

let prisma: PrismaClient;

if (import.meta.env.PROD) {
prisma = new PrismaClient();
} else {
if (!global.prisma) {
global.prisma = new PrismaClient();
}
prisma = global.prisma;
}

export default prisma;
5 changes: 2 additions & 3 deletions src/lib/server/db.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { error } from '@sveltejs/kit';

import { AvailabilityStatus, PrismaClient, type Pronoun } from '@prisma/client';
import { AvailabilityStatus, type Pronoun } from '@prisma/client';
import type { User } from '@prisma/client';
import { toLocalTimezone } from '../date';
import { dateNotes } from './sanitize';

const prisma = new PrismaClient();
import prisma from '$lib/prisma';

async function findHouseholdInvite(reqId: number) {
return await prisma.joinHouseholdRequest.findUnique({
Expand Down
4 changes: 1 addition & 3 deletions src/lib/server/login.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { PrismaClient } from '@prisma/client';

const prisma = new PrismaClient();
import prisma from '$lib/prisma';

export const generate = async () => {
const createdAt = new Date();
Expand Down
4 changes: 2 additions & 2 deletions src/lib/server/sanitize.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { getObjectivePronoun } from '$lib/parse';
import { PrismaClient, type User } from '@prisma/client';
import type { User } from '@prisma/client';
import { error } from '@sveltejs/kit';
const prisma = new PrismaClient();
import prisma from '$lib/prisma';
import sanitizerFunc from 'sanitize';

const sanitizer = sanitizerFunc();
Expand Down
4 changes: 1 addition & 3 deletions src/lib/server/shared.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { PrismaClient } from '@prisma/client';
import prisma from '$lib/prisma';
import type { Session } from '@prisma/client';

const prisma = new PrismaClient();

async function getProfileFromSession(sessionToken: string) {
if (!sessionToken) return { user: null, phone: null };
const session = (await prisma.session.findUnique({
Expand Down
4 changes: 2 additions & 2 deletions src/lib/server/twilio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import { env as private_env } from '$env/dynamic/private';
import { env as public_env } from '$env/dynamic/public';
import { error, json } from '@sveltejs/kit';
import Twilio from 'twilio';
import { PrismaClient, type User } from '@prisma/client';
import type { User } from '@prisma/client';
import { circleNotif } from './sanitize';
import { generate, save } from './login';
import { toLocalTimezone } from '../date';
import { DateTime } from 'luxon';
import prisma from '$lib/prisma';

const prisma = new PrismaClient();
const MessagingResponse = Twilio.twiml.MessagingResponse;

const msgToSend = async (
Expand Down
4 changes: 2 additions & 2 deletions src/routes/calendar/+page.server.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { dateTo12Hour, toLocalTimezone } from '$lib/date';
import type { PageServerLoad } from './$types';
import { PrismaClient, AvailabilityStatus } from '@prisma/client';
import { AvailabilityStatus } from '@prisma/client';
import prisma from '$lib/prisma';

const prisma = new PrismaClient();
export const load = (async ({ parent, depends }) => {
depends('data:calendar');
const { user } = await parent();
Expand Down
3 changes: 1 addition & 2 deletions src/routes/circle/+page.server.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { PageServerLoad } from './$types';
import { PrismaClient } from '@prisma/client';
import prisma from '$lib/prisma';

const prisma = new PrismaClient();
export const load = (async ({ parent, depends }) => {
depends('data:circle');
const { user } = await parent();
Expand Down
5 changes: 2 additions & 3 deletions src/routes/dashboard/+page.server.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { PageServerLoad } from './$types';
import { PrismaClient, AvailabilityStatus, type AvailabilityDate } from '@prisma/client';
import { AvailabilityStatus, type AvailabilityDate } from '@prisma/client';
import {
DAYS,
type Dates,
Expand All @@ -11,8 +11,7 @@ import { dateTo12Hour, toLocalTimezone } from '$lib/date';
import { getAvailRangeParts } from '$lib/parse';
import type { Household } from './constants';
import { DateTime } from 'luxon';

const prisma = new PrismaClient();
import prisma from '$lib/prisma';

const getHousehold = (household: Household) => {
return {
Expand Down
4 changes: 2 additions & 2 deletions src/routes/household/+page.server.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { PageServerLoad } from './$types';
import { PrismaClient, type Pronoun, type User } from '@prisma/client';
import type { Pronoun, User } from '@prisma/client';
import prisma from '$lib/prisma';

const prisma = new PrismaClient();
export const load = (async ({ parent, depends }) => {
depends('data:householdId');
const householdInfo: {
Expand Down
4 changes: 2 additions & 2 deletions src/routes/household/[householdId]/+page.server.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { PageServerLoad } from './$types';
import { PrismaClient, type Pronoun, type User } from '@prisma/client';
import type { Pronoun, User } from '@prisma/client';
import prisma from '$lib/prisma';

const prisma = new PrismaClient();
export const load = (async ({ params }) => {
const householdInfo: {
householdId: number | null;
Expand Down
3 changes: 1 addition & 2 deletions src/routes/invites/+page.server.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { PageServerLoad } from './$types';
import { PrismaClient } from '@prisma/client';
import prisma from '$lib/prisma';

const prisma = new PrismaClient();
export const load = (async ({ parent, depends }) => {
depends('data:invite');
const { user } = await parent();
Expand Down
4 changes: 1 addition & 3 deletions src/routes/login/[phone]/[token]/+page.server.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import type { PageServerLoad } from './$types';
import { redirect } from '@sveltejs/kit';
import { PrismaClient } from '@prisma/client';

const prisma = new PrismaClient();
import prisma from '$lib/prisma';

export const load = (async ({ params, cookies }) => {
let magicLinkInfo;
Expand Down

0 comments on commit 22c08bf

Please sign in to comment.