From ca6a209376169a337889a4682a6aad247b218b09 Mon Sep 17 00:00:00 2001 From: ryzetech Date: Thu, 19 Jan 2023 08:08:53 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=97=83=EF=B8=8F=20migrated=20saving=20str?= =?UTF-8?q?icture=20to=20prisma?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .env | 1 + .gitignore | 5 ++++- app.js | 28 +++++++++++++++++++++------- prisma/schema.prisma | 17 +++++++++++++++++ 4 files changed, 43 insertions(+), 8 deletions(-) create mode 100644 .env create mode 100644 prisma/schema.prisma diff --git a/.env b/.env new file mode 100644 index 0000000..641cfea --- /dev/null +++ b/.env @@ -0,0 +1 @@ +DATABASE_URL="file:./dev.db" \ No newline at end of file diff --git a/.gitignore b/.gitignore index 4a73542..0a95c27 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,5 @@ node_modules/ -website.js \ No newline at end of file +website.js +prisma/*.db +prisma/*.db-journal +prisma/migrations/ \ No newline at end of file diff --git a/app.js b/app.js index 772bc03..0e3ddce 100644 --- a/app.js +++ b/app.js @@ -10,27 +10,33 @@ const { graphqlHTTP } = require('express-graphql'); const { buildSchema } = require('graphql'); const NodeCache = require("node-cache"); const jp = require('jsonpath'); +const { PrismaClient } = require('@prisma/client'); +const prisma = new PrismaClient(); // set up things const cache = new NodeCache({ stdTTL: config.checkperiod * 3 }); let schema = buildSchema(` type Query { - + isOpen(id: String): Boolean + } `); let root = { - + sisOpen({ id }) { + let open = cache.get(id); + return { open }; + } }; - let app = express(); -app.use('/json', express.json()); -/* app.use('/graphql', graphqlHTTP({ - + schema: schema, + rootValue: root, + graphiql: true, })); -*/ + +app.listen(4000); // CHECK LOOP (async function () { @@ -40,6 +46,14 @@ app.use('/graphql', graphqlHTTP({ console.log(`Checking ${space.id}...`); let o = await checkSpace(space); console.log(`Space ${space.id} is ${o ? "open" : "closed"}`); + if (o !== cache.get(space.id)) { + cache.set(space.id, o); + let update = await prisma.space.upsert({ + where: { id: space.id }, + update: { open: o }, + create: { id: space.id, open: o } + }); + } } } })(); diff --git a/prisma/schema.prisma b/prisma/schema.prisma new file mode 100644 index 0000000..2d8dee1 --- /dev/null +++ b/prisma/schema.prisma @@ -0,0 +1,17 @@ +// 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 = "sqlite" + url = env("DATABASE_URL") +} + +model Space { + id String @id + open Boolean + updatedAt DateTime @updatedAt +} \ No newline at end of file