Skip to content

Commit

Permalink
feat: payload 2 with postgres
Browse files Browse the repository at this point in the history
  • Loading branch information
mvarendorff committed Dec 2, 2023
1 parent 9254510 commit 2d9123d
Show file tree
Hide file tree
Showing 23 changed files with 2,803 additions and 5,200 deletions.
27 changes: 0 additions & 27 deletions deployment/helm/templates/cms-mongo/deployment.yaml

This file was deleted.

11 changes: 0 additions & 11 deletions deployment/helm/templates/cms-mongo/secret.yaml

This file was deleted.

13 changes: 0 additions & 13 deletions deployment/helm/templates/cms-mongo/service.yaml

This file was deleted.

10 changes: 0 additions & 10 deletions deployment/helm/templates/cms/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,3 @@ spec:
secretKeyRef:
name: {{ .Values.typesense.deployment.secret.name }}
key: TYPESENSE_API_KEY
- name: MONGO_USERNAME
valueFrom:
secretKeyRef:
name: {{ .Values.mongo.deployment.secret.name }}
key: MONGO_INITDB_ROOT_USERNAME
- name: MONGO_PASSWORD
valueFrom:
secretKeyRef:
name: {{ .Values.mongo.deployment.secret.name }}
key: MONGO_INITDB_ROOT_PASSWORD
27 changes: 2 additions & 25 deletions deployment/helm/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,6 @@ cms:
name: payload
containerPort: 3002
containerEnv:
- name: MONGODB_URI
value: mongodb://ytf-mongo-service/cms
- name: ENABLE_DATASEEDER
value: "true"

Expand All @@ -70,29 +68,8 @@ cms:
value:
- name: INITIAL_ADMIN_PASSWORD
value:

mongo:
create: false
service:
port: 27017
name: ytf-mongo-service
labels:
app: ytf-mongo
deployment:
name: ytf-mongo-deployment
image: mongo:6.0.5
port:
name: mongo
containerPort: 27017
containerEnv:

secret:
name: mongo-secret-env
secrets:
- name: MONGO_INITDB_ROOT_USERNAME
value: yestheoryfamily
- name: MONGO_INITDB_ROOT_PASSWORD
value: yestheoryfamily
- name: DATABASE_URI
value: postgres://username:password@ytf-database-service:5432/payload

web:
ingress:
Expand Down
14 changes: 1 addition & 13 deletions docker-compose.e2e.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,32 +44,20 @@ services:
cms:
image: ghcr.io/yes-theory-fam/yestheory-family-cms:COMMIT_HASH
environment:
MONGODB_URI: mongodb://mongo/cms
DATABASE_URI: "postgresql://ytfe2e:ytfe2e@postgres:5432/payload?schema=public"
ENABLE_DATASEEDER: "true"
PAYLOAD_SECRET: asdfghjkl
INITIAL_ADMIN_MAIL: [email protected]
INITIAL_ADMIN_PASSWORD: example
MONGO_USERNAME: ytfe2e
MONGO_PASSWORD: ytfe2e
TYPESENSE_API_KEY: 1234567890
TYPESENSE_API_URL: http://typesense:8108
SERVER_URL: http://localhost:3001
FRONTEND_URL: http://localhost:3000
ports:
- "3001:3001"
depends_on:
- mongo
- typesense

mongo:
image: mongo:6.0.5
restart: unless-stopped
environment:
MONGO_INITDB_ROOT_USERNAME: ytfe2e
MONGO_INITDB_ROOT_PASSWORD: ytfe2e
ports:
- "27017:27017"

typesense:
image: typesense/typesense:0.25.1
restart: unless-stopped
Expand Down
6 changes: 0 additions & 6 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,6 @@ services:
POSTGRES_PASSWORD: ${DATABASE_PASSWORD}
POSTGRES_DB: ${DATABASE_DB_NAME}

mongo:
image: mongo:6.0.5
restart: unless-stopped
ports:
- "27017:27017"

server:
image: yestheory-family-server:latest
build:
Expand Down
2 changes: 1 addition & 1 deletion packages/cms/.env.example
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
PAYLOAD_SECRET=asdfghjkl
MONGODB_URI=mongodb://localhost:27017/payload-12345
DATABASE_URI=postgres://username:password@localhost:5433/payload

TYPESENSE_API_URL=http://localhost:8108
TYPESENSE_API_KEY=1234567890
2 changes: 1 addition & 1 deletion packages/cms/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ To build the docker image, run `docker build -t my-tag .`

Ensure you are passing all needed environment variables when starting up your container via `--env-file` or setting them with your deployment.

The 3 typical env vars will be `MONGODB_URI`, `PAYLOAD_SECRET`, and `PAYLOAD_CONFIG_PATH`
The 3 typical env vars will be `DATABASE_URI`, `PAYLOAD_SECRET`, and `PAYLOAD_CONFIG_PATH`

`docker run --env-file .env -p 3000:3000 my-tag`
2 changes: 1 addition & 1 deletion packages/cms/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"dotenv": "^16.3.1",
"express": "^4.18.2",
"node-cron": "^3.0.3",
"payload": "^1.15.8",
"payload": "^2.3.1",
"typesense": "^1.7.2"
},
"devDependencies": {
Expand Down
1 change: 1 addition & 0 deletions packages/cms/src/collections/groupchats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export const Groupchats: CollectionConfig = {

const typesenseDoc = {
...doc,
id: doc.id.toString(),
keywords: doc.keywords.map((k) => k.value),
};
await typesenseClient
Expand Down
3 changes: 2 additions & 1 deletion packages/cms/src/cron-jobs/groupchat-sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,9 @@ const syncGroupchatsToTypesense = async () => {
});

const typesenseChats = groupchats.map(
({ createdAt, updatedAt, keywords, ...rest }) => ({
({ id, createdAt, updatedAt, keywords, ...rest }) => ({
...rest,
id: id.toString(),
keywords: keywords?.map(({ value }) => value) ?? [],
}),
);
Expand Down
15 changes: 4 additions & 11 deletions packages/cms/src/init-payload.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,18 @@
import { config } from "dotenv";
import payload from "payload";
import { type InitOptions } from "payload/config";
import { ensureDbExists } from "./utils/ensure-db-exists";

export const initPayload = async (additionalOptions?: Partial<InitOptions>) => {
config();

if (!process.env.PAYLOAD_SECRET) throw new Error("Missing PAYLOAD_SECRET");
if (!process.env.MONGODB_URI) throw new Error("Missing MONGODB_URI");
console.info("Ensuring database exists");
await ensureDbExists();

const mongoOptions = process.env.MONGO_PASSWORD
? {
authSource: "admin",
user: process.env.MONGO_USERNAME,
pass: process.env.MONGO_PASSWORD,
}
: undefined;
if (!process.env.PAYLOAD_SECRET) throw new Error("Missing PAYLOAD_SECRET");

return await payload.init({
secret: process.env.PAYLOAD_SECRET,
mongoURL: process.env.MONGODB_URI,
mongoOptions,
...additionalOptions,
});
};
57 changes: 45 additions & 12 deletions packages/cms/src/payload-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,66 @@ export interface Config {
collections: {
users: User;
groupchats: Groupchat;
'payload-preferences': PayloadPreference;
'payload-migrations': PayloadMigration;
};
globals: {};
}
export interface User {
id: string;
id: number;
updatedAt: string;
createdAt: string;
email: string;
resetPasswordToken?: string;
resetPasswordExpiration?: string;
salt?: string;
hash?: string;
loginAttempts?: number;
lockUntil?: string;
password?: string;
resetPasswordToken?: string | null;
resetPasswordExpiration?: string | null;
salt?: string | null;
hash?: string | null;
loginAttempts?: number | null;
lockUntil?: string | null;
password: string | null;
}
export interface Groupchat {
id: string;
id: number;
name: string;
platform: 'discord' | 'facebook' | 'signal' | 'telegram' | 'whatsapp';
description?: string | null;
url: string;
keywords: {
value: string;
id?: string;
id?: string | null;
}[];
description?: string;
platform: "discord" | "facebook" | "signal" | "telegram" | "whatsapp";
promoted: number;
updatedAt: string;
createdAt: string;
}
export interface PayloadPreference {
id: number;
user: {
relationTo: 'users';
value: number | User;
};
key?: string | null;
value?:
| {
[k: string]: unknown;
}
| unknown[]
| string
| number
| boolean
| null;
updatedAt: string;
createdAt: string;
}
export interface PayloadMigration {
id: number;
name?: string | null;
batch?: number | null;
updatedAt: string;
createdAt: string;
}


declare module 'payload' {
export interface GeneratedTypes extends Config {}
}
6 changes: 6 additions & 0 deletions packages/cms/src/payload.config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { buildConfig, Config } from "payload/config";
import path from "path";
import { viteBundler } from "@payloadcms/bundler-vite";
import { slateEditor } from "@payloadcms/richtext-slate";
import { postgresAdapter } from "@payloadcms/db-postgres";
import { Users } from "./collections/users";
import { Groupchats } from "./collections/groupchats";
import { mergeQueries } from "./utils/merge-queries";
Expand All @@ -9,8 +12,11 @@ const config: Config = {
admin: {
user: Users.slug,
buildPath: path.resolve(__dirname, "../build"),
bundler: viteBundler(),
},
editor: slateEditor({}),
collections: [Users, Groupchats],
db: postgresAdapter({ pool: { connectionString: process.env.DATABASE_URI } }),
typescript: {
outputFile: path.resolve(__dirname, "payload-types.ts"),
},
Expand Down
1 change: 1 addition & 0 deletions packages/cms/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import payload from "payload";
import { initPayload } from "./init-payload";
import { setupCronJobs } from "./cron-jobs";
import { typesenseReady } from "./lib/typesense";
import { ensureDbExists } from "./utils/ensure-db-exists";

require("dotenv").config();
const app = express();
Expand Down
23 changes: 23 additions & 0 deletions packages/cms/src/utils/ensure-db-exists.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { parse } from "pg-connection-string";
import { Client } from "pg";

export const ensureDbExists = async () => {
const uri = process.env.DATABASE_URI;
const { database, user, password, host, port } = parse(uri);

const postgresUri = `postgres://${user}:${password}@${host}:${port}/postgres`;
const client = new Client(postgresUri);

await client.connect();
const existingDatabase = await client.query(
`SELECT FROM pg_database WHERE datname = $1`,
[database],
);
if (existingDatabase.rowCount == 0) {
console.info(`Database ${database} not found! Creating new database!`);
await client.query(`CREATE DATABASE ${database}`);
console.info(`Database ${database} created!`);
}

await client.end();
};
5 changes: 1 addition & 4 deletions packages/cms/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@
"skipLibCheck": true,
"outDir": "./dist",
"rootDir": "./src",
"jsx": "react",
"paths": {
"payload/generated-types": ["./src/payload-types.ts"]
}
"jsx": "react"
},
"include": ["src"],
"exclude": ["node_modules", "dist", "build"],
Expand Down
2 changes: 1 addition & 1 deletion packages/common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
},
"devDependencies": {
"@yestheory.family/eslint-config": "^1.0.0",
"eslint": "8.54.0",
"eslint": "8.55.0",
"typescript": "5.3.2"
}
}
8 changes: 4 additions & 4 deletions packages/eslint-config/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
"test": "echo eslint-config: ok"
},
"devDependencies": {
"@typescript-eslint/eslint-plugin": "6.12.0",
"@typescript-eslint/parser": "6.12.0",
"eslint": "8.54.0",
"eslint-config-prettier": "9.0.0"
"@typescript-eslint/eslint-plugin": "6.13.1",
"@typescript-eslint/parser": "6.13.1",
"eslint": "8.55.0",
"eslint-config-prettier": "9.1.0"
}
}
Loading

0 comments on commit 2d9123d

Please sign in to comment.