Skip to content

Commit

Permalink
rm some more prisma stuff & use supabase
Browse files Browse the repository at this point in the history
  • Loading branch information
Geczy committed Sep 6, 2023
1 parent c046ef5 commit f924b4c
Show file tree
Hide file tree
Showing 16 changed files with 22 additions and 53 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ dist
build
config.js
**/volumes
**/prisma/generated
newrelic_agent.log
.idea
!**/locales/en/translation.json
Expand Down
1 change: 0 additions & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ dist
build
config.js
**/volumes
**/prisma/generated
newrelic_agent.log
.idea
!**/locales/en/translation.json
Expand Down
1 change: 0 additions & 1 deletion packages/dota/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,5 @@ dist
build
config.js
**/volumes
prisma/generated
newrelic_agent.log
.idea
5 changes: 0 additions & 5 deletions packages/dota/src/db/prisma.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
import { PrismaClient as PrismaMongo } from '@dotabod/prisma/dist/mongo/index.js'
import { PrismaClient as PrismaPsql } from '@dotabod/prisma/dist/psql/index.js'

// allow global `var` declarations
declare global {
// eslint-disable-next-line no-var
var prisma: PrismaPsql | undefined
// eslint-disable-next-line no-var
var prismaMongo: PrismaMongo | undefined
}

export const prisma = global.prisma ?? new PrismaPsql()
export const prismaMongo = global.prismaMongo ?? new PrismaMongo()

if (process.env.NODE_ENV !== 'production') {
global.prisma = prisma
global.prismaMongo = prismaMongo
}
2 changes: 2 additions & 0 deletions packages/dota/src/db/supabase-types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export type Tables<T extends keyof Database['public']['Tables']> =
Database['public']['Tables'][T]['Row']
export type Json = string | number | boolean | null | { [key: string]: Json | undefined } | Json[]

export interface Database {
Expand Down
16 changes: 8 additions & 8 deletions packages/dota/src/db/watcher.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { Account, Setting, SteamAccount, User } from '@dotabod/prisma/dist/psql/index.js'
import { DBSettings } from '@dotabod/settings'

import { clearCacheForUser } from '../dota/clearCacheForUser.js'
Expand All @@ -10,6 +9,7 @@ import { toggleDotabod } from '../twitch/toggleDotabod.js'
import { logger } from '../utils/logger.js'
import getDBUser from './getDBUser.js'
import supabase from './supabase.js'
import { Tables } from './supabase-types.js'

class SetupSupabase {
channel: any // ReturnType<typeof supabase.channel>
Expand Down Expand Up @@ -46,7 +46,7 @@ class SetupSupabase {
.on(
'postgres_changes',
{ event: 'DELETE', schema: 'public', table: 'users' },
async (payload: { old: User }) => {
async (payload: { old: Tables<'users'> }) => {
if (this.IS_DEV && !this.DEV_CHANNELS.includes(payload.old.name)) return
if (!this.IS_DEV && this.DEV_CHANNELS.includes(payload.old.name)) return

Expand All @@ -64,7 +64,7 @@ class SetupSupabase {
.on(
'postgres_changes',
{ event: 'UPDATE', schema: 'public', table: 'accounts' },
async (payload: { new: Account; old: Account }) => {
async (payload: { new: Tables<'accounts'>; old: Tables<'accounts'> }) => {
// watch the accounts table for requires_refresh to change from true to false
// if it does, add the user to twurple authprovider again via addUser()
if (this.IS_DEV && !this.DEV_CHANNELIDS.includes(payload.new?.providerAccountId)) return
Expand Down Expand Up @@ -92,8 +92,8 @@ class SetupSupabase {
if (this.IS_DEV && !this.DEV_CHANNELS.includes(payload.new.name)) return
if (!this.IS_DEV && this.DEV_CHANNELS.includes(payload.new.name)) return

const newObj = payload.new as User
const oldObj = payload.old as User
const newObj: Tables<'users'> = payload.new
const oldObj: Tables<'users'> = payload.old
const client = findUser(newObj.id)
if (!client) return

Expand Down Expand Up @@ -146,7 +146,7 @@ class SetupSupabase {
'postgres_changes',
{ event: '*', schema: 'public', table: 'settings' },
(payload: any) => {
const newObj = payload.new as Setting
const newObj: Tables<'settings'> = payload.new
const client = findUser(newObj.userId)

if (newObj.key === DBSettings.commandDisable) {
Expand Down Expand Up @@ -196,8 +196,8 @@ class SetupSupabase {
'postgres_changes',
{ event: '*', schema: 'public', table: 'steam_accounts' },
async (payload: any) => {
const newObj = payload.new as SteamAccount
const oldObj = payload.old as SteamAccount
const newObj: Tables<'steam_accounts'> = payload.new
const oldObj: Tables<'steam_accounts'> = payload.old
const client = findUser(newObj.userId || oldObj.userId)

// Just here to update local memory
Expand Down
1 change: 0 additions & 1 deletion packages/dota/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
"./.eslintrc.js"
],
"exclude": [
"prisma/generated",
"node_modules",
],
"ts-node": {
Expand Down
1 change: 0 additions & 1 deletion packages/twitch/chat/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,3 @@ dist
build
config.js
**/volumes
prisma/generated
12 changes: 0 additions & 12 deletions packages/twitch/chat/src/db/prisma.ts

This file was deleted.

2 changes: 2 additions & 0 deletions packages/twitch/chat/src/db/supabase-types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export type Tables<T extends keyof Database['public']['Tables']> =
Database['public']['Tables'][T]['Row']
export type Json = string | number | boolean | null | { [key: string]: Json | undefined } | Json[]

export interface Database {
Expand Down
9 changes: 4 additions & 5 deletions packages/twitch/chat/src/db/watcher.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { User } from '@dotabod/prisma/dist/psql/index.js'

import { Tables } from './supabase-types.js'
import supabase from './supabase.js'
import { chatClient } from '../index.js'

Expand All @@ -8,9 +7,9 @@ const DEV_CHANNELS = process.env.DEV_CHANNELS?.split(',') ?? []
const channel = supabase.channel(`${IS_DEV ? 'dev-' : ''}twitch-chat`)

channel
.on('postgres_changes', { event: 'UPDATE', schema: 'public', table: 'users' }, (payload) => {
const oldUser = payload.old as User
const newUser = payload.new as User
.on('postgres_changes', { event: 'UPDATE', schema: 'public', table: 'users' }, (payload: any) => {
const oldUser: Tables<'users'> = payload.old
const newUser: Tables<'users'> = payload.new

if (IS_DEV && !DEV_CHANNELS.includes(newUser.name)) return
if (!IS_DEV && DEV_CHANNELS.includes(newUser.name)) return
Expand Down
3 changes: 1 addition & 2 deletions packages/twitch/events/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ dist
build
config.js
**/volumes
prisma/generated
private.key
public.key
.idea
.idea
13 changes: 0 additions & 13 deletions packages/twitch/events/src/db/prisma.ts

This file was deleted.

2 changes: 2 additions & 0 deletions packages/twitch/events/src/db/supabase-types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export type Tables<T extends keyof Database['public']['Tables']> =
Database['public']['Tables'][T]['Row']
export type Json = string | number | boolean | null | { [key: string]: Json | undefined } | Json[]

export interface Database {
Expand Down
4 changes: 2 additions & 2 deletions packages/twitch/events/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import http from 'http'

import { Account } from '@dotabod/prisma/dist/psql/index'
import { EnvPortAdapter, EventSubHttpListener } from '@twurple/eventsub-http'
import express from 'express'
import { Server as SocketServer } from 'socket.io'

import { Tables } from './db/supabase-types.js'
import { handleNewUser } from './handleNewUser.js'
import { SubscribeEvents } from './SubscribeEvents.js'
import BotAPI from './twitch/lib/BotApiSingleton.js'
Expand Down Expand Up @@ -88,7 +88,7 @@ webhookApp.post('/', express.json(), express.urlencoded({ extended: true }), (re
}

if (req.body.type === 'INSERT' && req.body.table === 'accounts') {
const user = req.body.record as Account
const user: Tables<'accounts'> = req.body.record
if (IS_DEV && !DEV_CHANNELIDS.includes(user.providerAccountId)) return
if (!IS_DEV && DEV_CHANNELIDS.includes(user.providerAccountId)) return

Expand Down
2 changes: 1 addition & 1 deletion runner.sh
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ gentypes() {
for OUTPUT_DIR in "${OUTPUT_DIRS[@]}"; do
OUTPUT_FILE="$OUTPUT_DIR/supabase-types.ts"
npx supabase gen types typescript --project-id "$PROJECT_ID" --schema public >"$OUTPUT_FILE"
# Run prettier on the generated file
echo 'export type Tables<T extends keyof Database["public"]["Tables"]> = Database["public"]["Tables"][T]["Row"];' | cat - "$OUTPUT_FILE" >temp && mv temp "$OUTPUT_FILE"
npx prettier --write "$OUTPUT_FILE"
done
}
Expand Down

0 comments on commit f924b4c

Please sign in to comment.