Starting to migrate to prisma/mongodb

This commit is contained in:
Tanner Shaw
2023-07-24 17:17:15 -05:00
parent cfe8986488
commit b454bbb03f
5 changed files with 836 additions and 51 deletions

734
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -19,6 +19,7 @@
"license": "ISC",
"dependencies": {
"@faker-js/faker": "^8.0.2",
"@prisma/client": "^5.0.0",
"atob": "^2.1.2",
"body-parser": "^1.20.2",
"cors": "^2.8.5",
@@ -26,7 +27,10 @@
"discreetly-interfaces": "^0.1.5",
"dotenv": "^16.3.1",
"express": "^4.18.2",
"mongodb": "^5.7.0",
"poseidon-lite": "^0.2.0",
"prisma-cache-middleware": "^0.1.4",
"prisma-redis-middleware": "4.8.0",
"redis": "^4.6.7",
"rlnjs": "^3.1.4",
"socket.io": "^4.6.2"
@@ -39,9 +43,10 @@
"@types/express": "^4.17.17",
"concurrently": "^8.2.0",
"nodemon": "^2.0.22",
"prisma": "^5.0.0",
"rollup": "^3.26.2",
"rollup-plugin-cleaner": "^1.0.0",
"rollup-plugin-typescript2": "^0.35.0",
"typescript": "^5.1.6"
}
}
}

32
prisma/schema.prisma Normal file
View File

@@ -0,0 +1,32 @@
// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "mongodb"
url = env("DATABASE_URL")
}
model Groups {
groupId String @id @map("_id")
name String
rooms Rooms[]
}
model Rooms {
roomId String @id @map("_id")
name String
group Groups @relation(fields: [groupId], references: [groupId])
groupId String
Identities Identities[] @relation(fields: [identityIds], references: [id])
identityIds String[]
}
model Identities {
id String @id @map("_id")
rooms Rooms[] @relation(fields: [identityIds], references: [roomId])
identityIds String[]
}

View File

@@ -3,6 +3,9 @@ import { Server } from 'http';
import { Server as SocketIOServer, Socket } from 'socket.io';
import cors from 'cors';
import { createClient } from 'redis';
import Prisma from 'prisma';
import { PrismaClient } from '@prisma/client';
import { MongoClient, ServerApiVersion } from 'mongodb'
import { serverConfig, rooms as defaultRooms, rooms } from './config/rooms.js';
import type { MessageI, RoomI, RoomGroupI } from 'discreetly-interfaces';
import verifyProof from './verifier.js';
@@ -43,56 +46,67 @@ let TESTGROUPID: BigInt;
let redisClient;
let TESTING = false;
if (!process.env.REDIS_URL) {
console.log('Connecting to redis at localhost');
redisClient = createClient();
TESTING = true;
} else {
console.log('Connecting to redis at: ' + process.env.REDIS_URL);
console.log(process.env.REDIS_TLS_URL);
redisClient = createClient({
url: process.env.REDIS_URL,
socket: {
tls: true,
rejectUnauthorized: false
}
});
}
redisClient.connect().then(() => pp('Redis Connected'));
// ievqPdmksWKSpSG8
redisClient.get('rooms').then((rooms) => {
rooms = JSON.parse(rooms);
if (rooms) {
loadedRooms = rooms as unknown as RoomGroupI[];
} else {
loadedRooms = defaultRooms;
redisClient.set('rooms', JSON.stringify(loadedRooms));
}
});
// Create a MongoClient with a MongoClientOptions object to set the Stable API version
let ccm: ClaimCodeManager;
const prisma = new PrismaClient();
console.log("Prisma connected");
redisClient.get('ccm').then((cc) => {
TESTGROUPID = BigInt(loadedRooms[0].id);
if (!cc) {
ccm = new ClaimCodeManager();
ccm.generateClaimCodeSet(10, TESTGROUPID, 'TEST');
const ccs = ccm.getClaimCodeSets();
redisClient.set('ccm', JSON.stringify(ccs));
} else {
ccm = new ClaimCodeManager(JSON.parse(cc));
if (ccm.getUsedCount(TESTGROUPID).unusedCount < 5) {
ccm.generateClaimCodeSet(10, TESTGROUPID, 'TEST');
const ccs = ccm.getClaimCodeSets();
// if (!process.env.REDIS_URL) {
// console.log('Connecting to redis at localhost');
// redisClient = createClient();
// TESTING = true;
// } else {
// console.log('Connecting to redis at: ' + process.env.REDIS_URL);
// console.log(process.env.REDIS_TLS_URL);
// redisClient = createClient({
// url: process.env.REDIS_URL,
// socket: {
// tls: true,
// rejectUnauthorized: false
// }
// });
// }
// redisClient.connect().then(() => pp('Redis Connected'));
redisClient.set('ccm', JSON.stringify(ccs));
}
}
const ccs = ccm.getClaimCodeSets();
});
redisClient.on('error', (err) => pp('Redis Client Error: ' + err, 'error'));
// redisClient.get('rooms').then((rooms) => {
// rooms = JSON.parse(rooms);
// if (rooms) {
// loadedRooms = rooms as unknown as RoomGroupI[];
// } else {
// loadedRooms = defaultRooms;
// redisClient.set('rooms', JSON.stringify(loadedRooms));
// }
// });
// let ccm: ClaimCodeManager;
// redisClient.get('ccm').then((cc) => {
// TESTGROUPID = BigInt(loadedRooms[0].id);
// if (!cc) {
// ccm = new ClaimCodeManager();
// ccm.generateClaimCodeSet(10, TESTGROUPID, 'TEST');
// const ccs = ccm.getClaimCodeSets();
// redisClient.set('ccm', JSON.stringify(ccs));
// } else {
// ccm = new ClaimCodeManager(JSON.parse(cc));
// if (ccm.getUsedCount(TESTGROUPID).unusedCount < 5) {
// ccm.generateClaimCodeSet(10, TESTGROUPID, 'TEST');
// const ccs = ccm.getClaimCodeSets();
// redisClient.set('ccm', JSON.stringify(ccs));
// }
// }
// const ccs = ccm.getClaimCodeSets();
// });
// redisClient.on('error', (err) => pp('Redis Client Error: ' + err, 'error'));
io.on('connection', (socket: Socket) => {
pp('SocketIO: a user connected', 'debug');
@@ -246,10 +260,10 @@ socket_server.listen(SOCKET_PORT, () => {
});
// Disconnect from redis on exit
process.on('SIGINT', () => {
pp('disconnecting redis');
redisClient.disconnect().then(process.exit());
});
// process.on('SIGINT', () => {
// pp('disconnecting redis');
// redisClient.disconnect().then(process.exit());
// });
if (TESTING) {
class randomMessagePicker {

View File

@@ -2,8 +2,8 @@ import { createClient } from 'redis';
import type { RoomI, RoomGroupI, ServerI } from 'discreetly-interfaces';
import { genId } from 'discreetly-interfaces';
const redisClient = createClient();
redisClient.connect();
// const redisClient = createClient();
// redisClient.connect();
export function findRoomById(
roomGroups,