Skip to content

Commit ed93e87

Browse files
authored
Merge pull request #25 from geobrowser/db-setup
setup db
2 parents 67f4ff3 + 19897f3 commit ed93e87

File tree

14 files changed

+106
-318
lines changed

14 files changed

+106
-318
lines changed

.github/workflows/tests-and-checks.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ jobs:
1313
version: 9
1414
- name: Install dependencies
1515
run: pnpm install --frozen-lockfile
16+
- name: Generate Prisma Client
17+
working-directory: apps/server
18+
run: pnpm prisma generate
1619
- name: Build
1720
run: pnpm build
1821
- name: Generate Prisma Types

README.md

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,6 @@
66

77
```sh
88
pnpm install
9-
docker-compose up
10-
```
11-
12-
```sh
13-
# in another tab
149
cd apps/server
1510
cp .env.example .env
1611
pnpm prisma migrate dev
@@ -19,7 +14,7 @@ pnpm prisma migrate dev
1914
### Development
2015

2116
```sh
22-
docker-compose up
17+
pnpm build --watch
2318
# in another tab
2419
cd apps/events
2520
pnpm dev
@@ -28,12 +23,15 @@ cd apps/server
2823
pnpm dev
2924
```
3025

26+
Any time you make changes to the schema, you will need to run the following commands:
27+
28+
```sh
29+
cd apps/server
30+
pnpm prisma migrate dev # this will also generate the Prisma client
31+
```
32+
3133
## Upgrading Dependencies
3234

3335
```sh
34-
pnpm up --interactive
35-
cd apps/events
36-
pnpm up --interactive
37-
cd packaes/graph-framework
38-
pnpm up --interactive
36+
pnpm up --interactive --latest -r
3937
```

apps/server/.env.example

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
DATABASE_URL=postgresql://prisma:[email protected]:5432
1+
DATABASE_URL="file:./dev.db"

apps/server/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
node_modules
22
# Keep environment variables out of version control
33
.env
4+
# Local database files
5+
dev.db
6+
dev.db-journal

apps/server/prisma/migrations/20240919063907_init/migration.sql

Lines changed: 0 additions & 154 deletions
This file was deleted.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
-- CreateTable
2+
CREATE TABLE "SpaceEvent" (
3+
"id" TEXT NOT NULL PRIMARY KEY,
4+
"event" TEXT NOT NULL,
5+
"spaceId" TEXT NOT NULL,
6+
CONSTRAINT "SpaceEvent_spaceId_fkey" FOREIGN KEY ("spaceId") REFERENCES "Space" ("id") ON DELETE RESTRICT ON UPDATE CASCADE
7+
);
8+
9+
-- CreateTable
10+
CREATE TABLE "Space" (
11+
"id" TEXT NOT NULL PRIMARY KEY
12+
);
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
# Please do not edit this file manually
22
# It should be added in your version-control system (i.e. Git)
3-
provider = "postgresql"
3+
provider = "sqlite"

apps/server/prisma/schema.prisma

Lines changed: 9 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -6,108 +6,18 @@ generator client {
66
}
77

88
datasource db {
9-
provider = "postgresql"
9+
provider = "sqlite"
1010
url = env("DATABASE_URL")
1111
}
1212

13-
model User {
14-
id String @id @default(uuid())
15-
username String @unique
16-
registrationRecord String
17-
sessions Session[]
18-
loginAttempt LoginAttempt?
19-
createdAt DateTime @default(now())
20-
documents UsersOnDocuments[]
21-
userLocker UserLocker[]
13+
model SpaceEvent {
14+
id String @id
15+
event String
16+
space Space @relation(fields: [spaceId], references: [id])
17+
spaceId String
2218
}
2319

24-
model UserLocker {
25-
id String @id @default(uuid())
26-
userId String
27-
user User @relation(fields: [userId], references: [id])
28-
clock Int
29-
ciphertext String
30-
nonce String
31-
commitment String
32-
createdAt DateTime @default(now())
33-
34-
@@unique([userId, clock])
35-
}
36-
37-
model Session {
38-
token String @id
39-
sessionKey String
40-
userId String
41-
user User @relation(fields: [userId], references: [id])
42-
createdAt DateTime @default(now())
43-
}
44-
45-
model LoginAttempt {
46-
id String @id @default(uuid())
47-
userId String @unique
48-
user User @relation(fields: [userId], references: [id])
49-
serverLoginState String
50-
createdAt DateTime @default(now())
51-
}
52-
53-
model UsersOnDocuments {
54-
user User @relation(fields: [userId], references: [id])
55-
userId String
56-
document Document @relation(fields: [documentId], references: [id])
57-
documentId String
58-
isAdmin Boolean
59-
60-
@@id([userId, documentId])
61-
}
62-
63-
model DocumentInvitation {
64-
id String @id @default(uuid())
65-
document Document @relation(fields: [documentId], references: [id])
66-
documentId String
67-
token String @unique
68-
createdAt DateTime @default(now())
69-
ciphertext String
70-
}
71-
72-
model Document {
73-
id String @id
74-
nameCiphertext String
75-
nameNonce String
76-
nameCommitment String
77-
createdAt DateTime @default(now())
78-
updatedAt DateTime @updatedAt
79-
users UsersOnDocuments[]
80-
documentInvitations DocumentInvitation[]
81-
activeSnapshot Snapshot? @relation(name: "activeSnapshot", fields: [activeSnapshotId], references: [id])
82-
activeSnapshotId String? @unique
83-
snapshots Snapshot[]
84-
}
85-
86-
model Snapshot {
87-
id String @id
88-
latestVersion Int
89-
data String
90-
ciphertextHash String
91-
document Document @relation(fields: [documentId], references: [id])
92-
documentId String
93-
updates Update[]
94-
activeSnapshotDocument Document? @relation("activeSnapshot")
95-
createdAt DateTime @default(now())
96-
clocks Json
97-
parentSnapshotUpdateClocks Json
98-
parentSnapshotProof String
99-
}
100-
101-
model Update {
102-
id String @unique // composed out of snapshotId, pubKey, clock
103-
version Int
104-
data String
105-
snapshot Snapshot @relation(fields: [snapshotId], references: [id])
106-
snapshotId String
107-
clock Int
108-
pubKey String
109-
110-
@@unique([snapshotId, version])
111-
@@unique([snapshotId, pubKey, clock]) // matches the id
112-
@@index([id, version])
20+
model Space {
21+
id String @id
22+
events SpaceEvent[]
11323
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import type { CreateSpaceEvent } from 'graph-framework-space-events';
2+
import { prisma } from '../prisma.js';
3+
4+
export const createSpace = async (event: CreateSpaceEvent) => {
5+
return await prisma.spaceEvent.create({
6+
data: {
7+
event: JSON.stringify(event),
8+
id: '1',
9+
space: {
10+
create: {
11+
id: event.transaction.id,
12+
},
13+
},
14+
},
15+
});
16+
};

apps/server/src/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ import cors from 'cors';
22
import 'dotenv/config';
33
import { Schema } from 'effect';
44
import express from 'express';
5+
import type { CreateSpaceEvent } from 'graph-framework-space-events';
56
import { SpaceEvent } from 'graph-framework-space-events';
67
import type WebSocket from 'ws';
78
import { WebSocketServer } from 'ws';
9+
import { createSpace } from './handlers/createSpace.js';
810

911
const webSocketServer = new WebSocketServer({ noServer: true });
1012
const PORT = process.env.PORT !== undefined ? Number.parseInt(process.env.PORT) : 3030;
@@ -31,7 +33,9 @@ webSocketServer.on('connection', async (webSocket: WebSocket) => {
3133
const result = decodeEvent(rawData);
3234
if (result._tag === 'Right') {
3335
const data = result.right;
34-
console.log('Message received', data);
36+
if (data.transaction.type === 'create-space') {
37+
await createSpace(data as CreateSpaceEvent);
38+
}
3539
}
3640
});
3741
webSocket.on('close', () => {

0 commit comments

Comments
 (0)