use mongodb

This commit is contained in:
Akis 2023-01-10 16:13:53 +02:00
parent 60b072bc74
commit e4333a8b4c
Signed by untrusted user: akis
GPG Key ID: 267BF5C6677944ED
7 changed files with 991 additions and 248 deletions

View File

@ -1,12 +1,11 @@
AUTH_SECRET=myauthsecret # generate with https://generate-secret.vercel.app/32 or openssl rand -hex 32 on unix-like AUTH_SECRET=myauthsecret # generate with https://generate-secret.vercel.app/32 or openssl rand -hex 32 on unix-like
DB_HOST=localhost DB_URL=mongodb://localhost:27017
DB_PORT=5432
DB_USERNAME=postgres
DB_PASSWORD=your-db-password
GHOST_API_KEY=your-ghost-api-key GHOST_API_KEY=your-ghost-api-key
AUTH_CLIENT_ID=your-authentik-client-id AUTH_CLIENT_ID=your-authentik-client-id
AUTH_CLIENT_SECRET=your-authentik-client-secret AUTH_CLIENT_SECRET=your-authentik-client-secret
AUTH_ISSUER=https://authentik-domain/application/o/app-name/ AUTH_ISSUER=https://authentik-domain/application/o/app-name/
HCAPTCHA_SECRET=your-hcaptcha-secret HCAPTCHA_SECRET=your-hcaptcha-secret
HCAPTCHA_SITEKEY=your-hcaptcha-sitekey HCAPTCHA_SITEKEY=your-hcaptcha-sitekey
WEBHOOK=your-discord-webhook WEBHOOK=your-discord-webhook
MONGO_USER=mongodb
MONGO_PASSWORD=mongodb

View File

@ -24,18 +24,17 @@ services:
WEBHOOK: ${WEBHOOK} WEBHOOK: ${WEBHOOK}
#args: [ "ORIGIN=" ] #args: [ "ORIGIN=" ]
website-db-dev: website-db:
image: postgres image: mongo
container_name: website-db container_name: website-db
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
PGDATA: /data/postgres
POSTGRES_DB: website
volumes:
- website-db-data:/data/postgres
restart: unless-stopped restart: unless-stopped
volumes:
- website-db-data:/data/db
environment:
MONGO_INITDB_ROOT_USERNAME: $MONGO_USER
MONGO_INITDB_ROOT_PASSWORD: $MONGO_PASSWORD
MONGO_INITDB_DATABASE: website
command: [--auth]
volumes: volumes:
website-db-data: website-db-data:

View File

@ -42,7 +42,6 @@
"@auth/sveltekit": "^0.1.11", "@auth/sveltekit": "^0.1.11",
"dotenv": "^16.0.3", "dotenv": "^16.0.3",
"joi": "^17.7.0", "joi": "^17.7.0",
"knex": "^2.4.0", "mongodb": "^4.13.0"
"pg": "^8.8.0"
} }
} }

1159
pnpm-lock.yaml generated

File diff suppressed because it is too large Load Diff

View File

@ -1,31 +1,14 @@
import knex from "knex";
import type { Knex } from "knex";
import { env } from "$env/dynamic/private"; import { env } from "$env/dynamic/private";
import { building } from "$app/environment"; import { building } from "$app/environment";
import { MongoClient } from "mongodb";
import type { Db } from "mongodb";
export let db: Knex; export let db: Db;
if (!building) { if (!building) {
db = knex({ const client = new MongoClient(env.DB_URL);
client: "pg",
connection: {
host: String(env.DB_HOST),
port: Number(env.DB_PORT),
user: String(env.DB_USERNAME),
password: String(env.DB_PASSWORD),
database: "website"
}
})
if (! await db.schema.hasTable("Announcements")) { await client.connect();
await db.schema.createTable("Announcements", (table) => {
table.increments("id"); db = client.db("website");
table.specificType("title", "varchar").notNullable();
table.string("severity").notNullable();
table.string("author").notNullable();
table.string("link").nullable();
table.bigInteger("created").notNullable();
table.timestamps(true, true, true);
});
}
} }

View File

@ -5,7 +5,9 @@ import { db } from "$lib/server/db";
export const load: PageServerLoad = async () => { export const load: PageServerLoad = async () => {
const data = await db("Announcements").select("*"); const collection = db.collection("announcements");
const data = await collection.find({}, { projection: { _id: 0 } }).toArray();
if (data.length !== 0 || data[0] !== undefined) { if (data.length !== 0 || data[0] !== undefined) {

View File

@ -4,6 +4,8 @@ import { fail } from "@sveltejs/kit";
import { db } from "$lib/server/db"; import { db } from "$lib/server/db";
import dayjs from "dayjs"; import dayjs from "dayjs";
const collection = db.collection("announcements");
export const actions: Actions = { export const actions: Actions = {
add: async ({ request, locals }) => { add: async ({ request, locals }) => {
if (!await locals.getSession()) { if (!await locals.getSession()) {
@ -24,14 +26,12 @@ export const actions: Actions = {
const now = Math.floor(Date.now() / 1000); const now = Math.floor(Date.now() / 1000);
const data = { const data = {
...Object.fromEntries(formData.entries()), ...Object.fromEntries(formData.entries()),
created: now, created: now
createdAt: dayjs.unix(now).toISOString(),
updatedAt: dayjs.unix(now).toISOString()
}; };
await db.delete("*").from("Announcements"); await collection.deleteMany({});
await db("Announcements").insert(data); await collection.insertOne(data);
return { addSuccess: true, addMessage: "Your announcement has been posted." }; return { addSuccess: true, addMessage: "Your announcement has been posted." };
} }
@ -43,7 +43,7 @@ export const actions: Actions = {
return fail(401, { deleteError: true, deleteMessage: "You must be logged in to delete an announcement." }); return fail(401, { deleteError: true, deleteMessage: "You must be logged in to delete an announcement." });
} else { } else {
await db.delete("*").from("Announcements"); await collection.deleteMany({});
return { deleteSuccess: true, deleteMessage: "Your announcement has been deleted." }; return { deleteSuccess: true, deleteMessage: "Your announcement has been deleted." };
} }