Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/tests-and-checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ jobs:
version: 9
- name: Install dependencies
run: pnpm install --frozen-lockfile
- name: Generate Prisma Client
working-directory: apps/server
run: pnpm prisma generate
- name: Build
run: pnpm build
- name: Generate Prisma Types
Expand Down
20 changes: 9 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@

```sh
pnpm install
docker-compose up
```

```sh
# in another tab
cd apps/server
cp .env.example .env
pnpm prisma migrate dev
Expand All @@ -19,7 +14,7 @@ pnpm prisma migrate dev
### Development

```sh
docker-compose up
pnpm build --watch
# in another tab
cd apps/events
pnpm dev
Expand All @@ -28,12 +23,15 @@ cd apps/server
pnpm dev
```

Any time you make changes to the schema, you will need to run the following commands:

```sh
cd apps/server
pnpm prisma migrate dev # this will also generate the Prisma client
```

## Upgrading Dependencies

```sh
pnpm up --interactive
cd apps/events
pnpm up --interactive
cd packaes/graph-framework
pnpm up --interactive
pnpm up --interactive --latest -r
```
2 changes: 1 addition & 1 deletion apps/server/.env.example
Original file line number Diff line number Diff line change
@@ -1 +1 @@
DATABASE_URL=postgresql://prisma:[email protected]:5432
DATABASE_URL="file:./dev.db"
3 changes: 3 additions & 0 deletions apps/server/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
node_modules
# Keep environment variables out of version control
.env
# Local database files
dev.db
dev.db-journal
154 changes: 0 additions & 154 deletions apps/server/prisma/migrations/20240919063907_init/migration.sql

This file was deleted.

12 changes: 12 additions & 0 deletions apps/server/prisma/migrations/20241108124551_init/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
-- CreateTable
CREATE TABLE "SpaceEvent" (
"id" TEXT NOT NULL PRIMARY KEY,
"event" TEXT NOT NULL,
"spaceId" TEXT NOT NULL,
CONSTRAINT "SpaceEvent_spaceId_fkey" FOREIGN KEY ("spaceId") REFERENCES "Space" ("id") ON DELETE RESTRICT ON UPDATE CASCADE
);

-- CreateTable
CREATE TABLE "Space" (
"id" TEXT NOT NULL PRIMARY KEY
);
2 changes: 1 addition & 1 deletion apps/server/prisma/migrations/migration_lock.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# Please do not edit this file manually
# It should be added in your version-control system (i.e. Git)
provider = "postgresql"
provider = "sqlite"
108 changes: 9 additions & 99 deletions apps/server/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -6,108 +6,18 @@ generator client {
}

datasource db {
provider = "postgresql"
provider = "sqlite"
url = env("DATABASE_URL")
}

model User {
id String @id @default(uuid())
username String @unique
registrationRecord String
sessions Session[]
loginAttempt LoginAttempt?
createdAt DateTime @default(now())
documents UsersOnDocuments[]
userLocker UserLocker[]
model SpaceEvent {
id String @id
event String
space Space @relation(fields: [spaceId], references: [id])
spaceId String
}

model UserLocker {
id String @id @default(uuid())
userId String
user User @relation(fields: [userId], references: [id])
clock Int
ciphertext String
nonce String
commitment String
createdAt DateTime @default(now())

@@unique([userId, clock])
}

model Session {
token String @id
sessionKey String
userId String
user User @relation(fields: [userId], references: [id])
createdAt DateTime @default(now())
}

model LoginAttempt {
id String @id @default(uuid())
userId String @unique
user User @relation(fields: [userId], references: [id])
serverLoginState String
createdAt DateTime @default(now())
}

model UsersOnDocuments {
user User @relation(fields: [userId], references: [id])
userId String
document Document @relation(fields: [documentId], references: [id])
documentId String
isAdmin Boolean

@@id([userId, documentId])
}

model DocumentInvitation {
id String @id @default(uuid())
document Document @relation(fields: [documentId], references: [id])
documentId String
token String @unique
createdAt DateTime @default(now())
ciphertext String
}

model Document {
id String @id
nameCiphertext String
nameNonce String
nameCommitment String
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
users UsersOnDocuments[]
documentInvitations DocumentInvitation[]
activeSnapshot Snapshot? @relation(name: "activeSnapshot", fields: [activeSnapshotId], references: [id])
activeSnapshotId String? @unique
snapshots Snapshot[]
}

model Snapshot {
id String @id
latestVersion Int
data String
ciphertextHash String
document Document @relation(fields: [documentId], references: [id])
documentId String
updates Update[]
activeSnapshotDocument Document? @relation("activeSnapshot")
createdAt DateTime @default(now())
clocks Json
parentSnapshotUpdateClocks Json
parentSnapshotProof String
}

model Update {
id String @unique // composed out of snapshotId, pubKey, clock
version Int
data String
snapshot Snapshot @relation(fields: [snapshotId], references: [id])
snapshotId String
clock Int
pubKey String

@@unique([snapshotId, version])
@@unique([snapshotId, pubKey, clock]) // matches the id
@@index([id, version])
model Space {
id String @id
events SpaceEvent[]
}
16 changes: 16 additions & 0 deletions apps/server/src/handlers/createSpace.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import type { CreateSpaceEvent } from 'graph-framework-space-events';
import { prisma } from '../prisma.js';

export const createSpace = async (event: CreateSpaceEvent) => {
return await prisma.spaceEvent.create({
data: {
event: JSON.stringify(event),
id: '1',
space: {
create: {
id: event.transaction.id,
},
},
},
});
};
6 changes: 5 additions & 1 deletion apps/server/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ import cors from 'cors';
import 'dotenv/config';
import { Schema } from 'effect';
import express from 'express';
import type { CreateSpaceEvent } from 'graph-framework-space-events';
import { SpaceEvent } from 'graph-framework-space-events';
import type WebSocket from 'ws';
import { WebSocketServer } from 'ws';
import { createSpace } from './handlers/createSpace.js';

const webSocketServer = new WebSocketServer({ noServer: true });
const PORT = process.env.PORT !== undefined ? Number.parseInt(process.env.PORT) : 3030;
Expand All @@ -31,7 +33,9 @@ webSocketServer.on('connection', async (webSocket: WebSocket) => {
const result = decodeEvent(rawData);
if (result._tag === 'Right') {
const data = result.right;
console.log('Message received', data);
if (data.transaction.type === 'create-space') {
await createSpace(data as CreateSpaceEvent);
}
}
});
webSocket.on('close', () => {
Expand Down
3 changes: 3 additions & 0 deletions apps/server/src/prisma.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { PrismaClient } from '@prisma/client';

export const prisma = new PrismaClient();
Loading
Loading