🗃️ migrated saving stricture to prisma

This commit is contained in:
ryzetech 2023-01-19 08:08:53 +01:00
parent 0d9bbced2d
commit ca6a209376
4 changed files with 43 additions and 8 deletions

1
.env Normal file
View file

@ -0,0 +1 @@
DATABASE_URL="file:./dev.db"

3
.gitignore vendored
View file

@ -1,2 +1,5 @@
node_modules/ node_modules/
website.js website.js
prisma/*.db
prisma/*.db-journal
prisma/migrations/

28
app.js
View file

@ -10,27 +10,33 @@ const { graphqlHTTP } = require('express-graphql');
const { buildSchema } = require('graphql'); const { buildSchema } = require('graphql');
const NodeCache = require("node-cache"); const NodeCache = require("node-cache");
const jp = require('jsonpath'); const jp = require('jsonpath');
const { PrismaClient } = require('@prisma/client');
const prisma = new PrismaClient();
// set up things // set up things
const cache = new NodeCache({ stdTTL: config.checkperiod * 3 }); const cache = new NodeCache({ stdTTL: config.checkperiod * 3 });
let schema = buildSchema(` let schema = buildSchema(`
type Query { type Query {
isOpen(id: String): Boolean
}
`); `);
let root = { let root = {
sisOpen({ id }) {
let open = cache.get(id);
return { open };
}
}; };
let app = express(); let app = express();
app.use('/json', express.json());
/*
app.use('/graphql', graphqlHTTP({ app.use('/graphql', graphqlHTTP({
schema: schema,
rootValue: root,
graphiql: true,
})); }));
*/
app.listen(4000);
// CHECK LOOP // CHECK LOOP
(async function () { (async function () {
@ -40,6 +46,14 @@ app.use('/graphql', graphqlHTTP({
console.log(`Checking ${space.id}...`); console.log(`Checking ${space.id}...`);
let o = await checkSpace(space); let o = await checkSpace(space);
console.log(`Space ${space.id} is ${o ? "open" : "closed"}`); 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 }
});
}
} }
} }
})(); })();

17
prisma/schema.prisma Normal file
View file

@ -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
}