Skip to content

Commit

Permalink
Big conversion back to MongoDB for the backend.
Browse files Browse the repository at this point in the history
I've rolled back to the big conversion to a storage-based database.

I realised that there is a lot more work to do to make the storage-
based database performant and I just want to ship Photosphere.

There is now a minimal defs library for sharing types between
backend and frontend.

The database library has been collapsed back into user-interface
library.

I'm still using the database, source and sink abstractions in the
frontend, but I'm no longer trying to make the database abstract
work for the backend as well. The database is just too different
between backend and frontend to make the abstraction cover them both.
  • Loading branch information
ashleydavis committed Jun 9, 2024
1 parent 2fba30f commit d10248c
Show file tree
Hide file tree
Showing 219 changed files with 2,487 additions and 5,578 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ testbed/build
packages/database/build
tools/upload/build
packages/exif-js/build
packages/defs/build
17 changes: 11 additions & 6 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@
"sd": "pnpm run start:dev",
"start": "node build/index.js",
"start:prod": "pnpm run clean-uploads && pnpm run copy-no-assets && cross-env NODE_ENV=production PORT=3000 ts-node-dev --respawn src/index.ts",
"start:dev": "pnpm run clean-uploads && pnpm run copy-50-assets && cross-env NODE_ENV=development pnpm run start-watch",
"start:dev-no-assets": "pnpm run clean-uploads && pnpm run copy-no-assets && cross-env NODE_ENV=development pnpm run start-watch",
"start:dev-1-asset": "pnpm run clean-uploads && pnpm run copy-1-asset && cross-env NODE_ENV=development pnpm run start-watch",
"start-watch": "cross-env NODE_ENV=development PORT=3000 ts-node-dev --respawn src/index.ts",
"start:dev": "pnpm run clean-uploads && pnpm run copy-50-assets && concurrently --names \"db,be\" \"npm run start-db-50-assets\" \"npm run start-watch\"",
"start:dev-no-assets": "pnpm run clean-uploads && pnpm run copy-no-assets && concurrently --names \"db,be\" \"npm run start-db-no-assets\" \"npm run start-watch\"",
"start:dev-1-asset": "pnpm run clean-uploads && pnpm run copy-1-asset && concurrently --names \"db,be\" \"npm run start-db-1-asset\" \"npm run start-watch\"",
"start-watch": "cross-env DB_CONNECTION_STRING=mongodb://localhost:7001 NODE_ENV=development PORT=3000 ts-node-dev --respawn src/index.ts",
"start-for-e2e-tests": "pnpm run clean-uploads && pnpm run start:dev-no-assets",
"start-db-50-assets": "insta-mongo --db-port=7001 --rest-port=7000 --load=50-assets --fixtures=../fixtures --db=photosphere",
"start-db-no-assets": "insta-mongo --db-port=7001 --rest-port=7000 --load=no-assets --fixtures=../fixtures --db=photosphere",
"start-db-1-asset": "insta-mongo --db-port=7001 --rest-port=7000 --load=1-asset --fixtures=../fixtures --db=photosphere",
"build": "pnpm install --production=false && pnpm run compile",
"c": "pnpm run compile",
"cw": "pnpm run compile:watch",
Expand All @@ -39,7 +42,8 @@
"@types/node": "^18.19.15",
"axios": "^0.27.2",
"concurrently": "^8.2.2",
"cross-env": "^7.0.3",
"cross-env": "^7.0.3",
"insta-mongo": "^0.0.6",
"jest": "^29.4.1",
"nodemon": "^2.0.16",
"supertest": "^6.3.3",
Expand All @@ -54,6 +58,7 @@
"express": "^5.0.0-beta.1",
"express-oauth2-jwt-bearer": "^1.6.0",
"fs-extra": "^11.1.0",
"database": "workspace:*"
"defs": "workspace:*",
"mongodb": "^4.7.0"
}
}
18 changes: 14 additions & 4 deletions backend/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,33 @@
import { StorageDatabaseCollection, StorageDatabases, StorageDirectory } from "database";
import { MongoClient } from "mongodb";
import { createServer } from "./server";
import { CloudStorage } from "./services/cloud-storage";
import { FileStorage } from "./services/file-storage";

async function main() {

const dbName = "photosphere";

const PORT = process.env.PORT;
if (!PORT) {
throw new Error(`Set environment variable PORT.`);
}

const DB_CONNECTION_STRING = process.env.DB_CONNECTION_STRING;
if (DB_CONNECTION_STRING === undefined) {
throw new Error(`Set environment variable DB_CONNECTION_STRING.`);
}

const client = new MongoClient(DB_CONNECTION_STRING);
await client.connect();

const db = client.db(dbName);

console.log(`Running in mode: ${process.env.NODE_ENV} on port ${PORT}.`);

const storage = process.env.NODE_ENV === "production"
? new CloudStorage()
: new FileStorage();
const userCollection = new StorageDatabaseCollection(storage, `users`);
const databases = new StorageDatabases(new StorageDirectory(storage, `collections`));
const app = await createServer(() => new Date(Date.now()), databases, userCollection, storage);
const app = await createServer(() => new Date(Date.now()), db, storage);
app.listen(PORT, () => {
console.log(`Photosphere listening on port ${PORT}`);
});
Expand Down
74 changes: 0 additions & 74 deletions backend/src/lib/asset.ts

This file was deleted.

File renamed without changes.
5 changes: 5 additions & 0 deletions backend/src/lib/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ export interface ICollections {
// Defines a user.
//
export interface IUser {
//
// The user id.
//
_id: string;

//
// Metadata for the user's collections.
//
Expand Down
File renamed without changes.
Loading

0 comments on commit d10248c

Please sign in to comment.