Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add config-manager #132

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
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
8 changes: 8 additions & 0 deletions packages/config-manager/.env.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# For prisma client
DATABASE_URL="postgresql://johndoe:randompassword@localhost:7654/mydb?schema=public"

## For local setup
DATABASE_PORT=7654
DATABASE_USERNAME="johndoe"
DATABASE_PASSWORD="randompassword"
DATABASE_NAME="mydb"
1 change: 1 addition & 0 deletions packages/config-manager/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.env
46 changes: 46 additions & 0 deletions packages/config-manager/api-doc.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
### Get Configuration by ID

- **URL**: `/configs/:id`
- **Method**: `GET`
- **Description**: Retrieves a specific configuration by its ID.
- **Parameters**:
- `id` (path parameter): The ID of the configuration to retrieve.
- **Response**: The configuration object with the specified ID, including its history.
- **cURL**
- ```curl http://localhost:3000/configs/1```

### Create Configuration

- **URL**: `/configs`
- **Method**: `POST`
- **Description**: Creates a new configuration.
- **Request Body**:
- `name`: The name of the configuration.
- `data`: The JSON data of the configuration.
- **Response**: The newly created configuration object, including its history.
- **cURL**
- ```curl -X POST -H "Content-Type: application/json" -d '{"name":"New Config","data":{"key":"value"}}' http://localhost:3000/configs```

### Update Configuration

- **URL**: `/configs/:id`
- **Method**: `PUT`
- **Description**: Updates an existing configuration by its ID.
- **Parameters**:
- `id` (path parameter): The ID of the configuration to update.
- **Request Body**:
- `data`: The updated JSON data of the configuration.
- **Response**: The updated configuration object, including its history.
- **cURL**
- ```curl -X PUT -H "Content-Type: application/json" -d '{"data":{"updatedKey":"updatedValue"}}' http://localhost:3000/configs/1```

### Get Configuration History

- **URL**: `/configs/:id/history`
- **Method**: `GET`
- **Description**: Retrieves the history of changes for a specific configuration by its ID.
- **Parameters**:
- `id` (path parameter): The ID of the configuration to retrieve history for.
- **Response**: An array of history entries for the specified configuration, sorted by timestamp in descending order.
- **cURL**
- ```curl http://localhost:3000/configs/:id/history```
20 changes: 20 additions & 0 deletions packages/config-manager/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
version: '3.9'

services:
postgres:
image: postgres:15.3-alpine
ports:
- ${DATABASE_PORT}:5432
volumes:
- ./.data/db:/var/lib/postgresql/data
environment:
POSTGRES_USER: ${DATABASE_USERNAME}
POSTGRES_PASSWORD: ${DATABASE_PASSWORD}
POSTGRES_DB: ${DATABASE_NAME}
healthcheck:
test: ['CMD-SHELL', 'pg_isready -U postgres']
interval: 5s
timeout: 5s
retries: 5
networks:
- default
56 changes: 56 additions & 0 deletions packages/config-manager/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
const express = require('express');
const { PrismaClient } = require('@prisma/client');

const prisma = new PrismaClient();
const app = express();
app.use(express.json());

app.get('/configs/:id', async (req, res) => {
const { id } = req.params;
const config = await prisma.config.findUnique({
where: { id: parseInt(id) },
include: { history: true }
});
res.json(config);
});

app.post('/configs', async (req, res) => {
const { name, data } = req.body;
const newConfig = await prisma.config.create({
data: {
name,
data,
history: { create: { data } }
},
include: { history: true }
});
res.json(newConfig);
});

app.put('/configs/:id', async (req, res) => {
const { id } = req.params;
const { data } = req.body;
const updatedConfig = await prisma.config.update({
where: { id: parseInt(id) },
data: {
data,
history: { create: { data } }
},
include: { history: true }
});
res.json(updatedConfig);
});

app.get('/configs/:id/history', async (req, res) => {
const { id } = req.params;
const history = await prisma.configHistory.findMany({
where: { configId: parseInt(id) },
orderBy: { timestamp: 'desc' }
});
res.json(history);
});

const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});
20 changes: 20 additions & 0 deletions packages/config-manager/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "@stencil/configmanager",
"version": "1.0.0",
"description": "",
"main": "index.ts",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"@prisma/client": "^5.13.0",
"express": "^4.19.2",
"prisma": "^5.13.0"
},
"devDependencies": {
"@types/node": "^20.12.7",
"typescript": "^5.4.5"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
-- CreateTable
CREATE TABLE "Config" (
"id" SERIAL NOT NULL,
"name" TEXT NOT NULL,
"data" JSONB NOT NULL,

CONSTRAINT "Config_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "ConfigHistory" (
"id" SERIAL NOT NULL,
"configId" INTEGER NOT NULL,
"data" JSONB NOT NULL,
"timestamp" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

CONSTRAINT "ConfigHistory_pkey" PRIMARY KEY ("id")
);

-- AddForeignKey
ALTER TABLE "ConfigHistory" ADD CONSTRAINT "ConfigHistory_configId_fkey" FOREIGN KEY ("configId") REFERENCES "Config"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
3 changes: 3 additions & 0 deletions packages/config-manager/prisma/migrations/migration_lock.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Please do not edit this file manually
# It should be added in your version-control system (i.e. Git)
provider = "postgresql"
23 changes: 23 additions & 0 deletions packages/config-manager/prisma/schema.prisma
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
generator client {
provider = "prisma-client-js"
}

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

model Config {
id Int @id @default(autoincrement())
name String
data Json
history ConfigHistory[]
}

model ConfigHistory {
id Int @id @default(autoincrement())
configId Int
config Config @relation(fields: [configId], references: [id])
data Json
timestamp DateTime @default(now())
}
Loading
Loading