Skip to content

Commit

Permalink
refactor: replace electricsql with powersync (#42)
Browse files Browse the repository at this point in the history
* fix: display correct status

* fix: edit banner spacings

* feat: set edit banner sticky on mobile

* fix: return arrow size

* feat: set menu title position to sticky

* fix: some styles

* fix: simplify header

* fix: show reports on 2 columns from 6 items

* feat: add info messages

* feat: display report item on 3 rows

* fix: new colors

* fix: informative message mb

* fix: set printable colors

* fix(pdf): set header sticky on desktop

* fix: recipients textarea styles

* fix: menu header padding and more

* fix: remove *

* fix: form labels

* fix: date picker

* fix: margins

* gitignore

* fix: pdf styles can now be italic and bold

* fix: align tab title with content on firefox

* fix: report search columns styles

* fix: more report search styles

* fix: use createdBy along with redactedBy to allow user to edit a report

* refactor: add useCanEdit hook

* fix: mail carriage return

* wip: add version to env

* feat: add version at build time

* feat: add version component

* fix: menu title padding

* feat: add "city" and "zipcode" within the search params

* Mep pictures (#39)

* feat: add pictures table + button

* feat: add pictures to db

* feat: add input file with capture prop

* feat: add report pictures

* wip

* feat: display pictures in app and in pdf

* big wip

* alos peut être ?

* types?

* wip: service worker

* alors peut être ?

* feat: background image upload

* feat: cleanup local pictures when uploaded

* fix: use local picture data if it exists

* fix: correctly display pdf images

* feat: add pictures on backend pdf gen

* fix: add react import

* fix: add pictures to backend pdf gen

* fix(SW): use idb instead of electric

* fix(SW): set env vars in self object

* add logs

* more logs

* feat: set url in idb so SW can use it

* fix: little oopsie

* fix: safe parse env

* feat: store uploading status for each picture

* fix: sw updates

* maybe

* feat: listen to upload status in app

* fix: cache every page

* fix: manifest

* fix: use minio for image upload

* fix: correctly set bg url

* fix: remove capture attribute

* fix: correctly clear images on delete

* fix: merge

* wip

* fix: correctly sync pictures

* remove top badge

* add logs

* fix: disable sentry if no dsn

* add logs

* feat: add tmp_pictures table and make backend create pictures

* feat: add pic status badges

* fix: initial pic status

* fix(SW): remove throw from sync request

* fix: sort pictures + delete

* feat: reset SW on local data deletion

* cleanup

* wip: drawing canvas

* feat: remove auto retry

* wip: draw on picture

* maybe?

* fix: try to upload image within main process

* fix: offline upload

* feat(pdf): break page before pictures

* fix(pdf): blank page

* feat: add picture_lines in db and save lines

* feat: draw on canvas and sync with backend and pdf

* fix: picture lines migration

* fix: service worker and offline sync

* fix: dockerfile

* fix: backend dockerfile

* should be ok now

* feat: add marianne font

* wip: add powersync

* fix(pdf): rollback to helvetica

* fix: display correct status

* fix: status

* feat(picture edition): add desktop layout

* wip: add powersync auth

* wip: database

* fix(image canvas): set max height

* feat: working select queries

* wip: buckets (V2)

* wip: migrate user service to kysely and add user tests

* wip

* wip: big wip 2

* wip: schema translation

* wip: writing changes

* wip: remove electric-client

* wip

* clean up

* wip: status

* fix: clauses

* fix: all reports page

* feat: migrate pictures to powersync

* cleanup

* feat: add failed_transactions table to track errors

* fix: rename failed_transactions to transactions and record everything

* cleanup migrations

* fix: set service instructeur id to string

* fix: record every transaction

* fix: set delegations id to string

* fix: set correct status to banner

* fix: clause CRUD with powersync

* fix: dockerfile

* fix: docker compose and env
  • Loading branch information
ledouxm authored Dec 7, 2024
1 parent ea72c92 commit 9a1af03
Show file tree
Hide file tree
Showing 138 changed files with 2,190 additions and 26,611 deletions.
7 changes: 1 addition & 6 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -49,24 +49,19 @@ FROM with-deps AS backend
WORKDIR /usr/src/app
COPY packages/backend/ ./packages/backend/
COPY packages/pdf/ ./packages/pdf/
COPY packages/electric-client/ ./packages/electric-client/


COPY db/ ./db/
COPY --from=with-deps /usr/src/app/packages/backend/node_modules ./packages/backend/node_modules
COPY --from=with-deps /usr/src/app/packages/electric-client/node_modules ./packages/electric-client/node_modules

RUN pnpm electric-client generate:back
RUN pnpm backend build
CMD pnpm electric:up;pnpm backend start
CMD pnpm migration:up;pnpm backend start


################################
# FRONTEND SERVICE #
################################
FROM with-deps AS frontend
COPY packages/frontend/ ./packages/frontend/
COPY packages/electric-client/ ./packages/electric-client/
COPY packages/pdf/ ./packages/pdf/
COPY --from=with-deps /usr/src/app/packages/frontend/node_modules ./packages/frontend/node_modules
COPY --from=with-deps /usr/src/app/packages/frontend/styled-system ./packages/frontend/styled-system
Expand Down
19 changes: 19 additions & 0 deletions db/migrate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import dotenv from "dotenv";
import dotenvExpand from "dotenv-expand";
import { execSync } from "child_process";

dotenvExpand.expand(dotenv.config());

const databaseUrl = process.env.DATABASE_URL;
if (!databaseUrl) {
throw new Error("DATABASE_URL is not set");
}

const command = `pnpm pg-migrations apply --database "${databaseUrl}" --ignore-error migration_file_edited --directory ./db/migrations`;

try {
execSync(command, { stdio: "inherit" });
} catch (e) {
console.error("Migration failed:", e);
process.exit(1);
}
11 changes: 5 additions & 6 deletions db/migrations/01-add_users.sql
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ CREATE TABLE "udap"(
,udap_text TEXT
);

ALTER TABLE "udap" ENABLE ELECTRIC;
CREATE PUBLICATION powersync FOR TABLE "udap";

--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "user" (
Expand All @@ -34,13 +34,12 @@ CREATE TABLE IF NOT EXISTS "internal_user" (
"userId" text NOT NULL REFERENCES "user"(id) ON DELETE CASCADE
);

ALTER TABLE "user" ENABLE ELECTRIC;
ALTER PUBLICATION powersync ADD TABLE "user";

CREATE TABLE IF NOT EXISTS "delegation" (
"id" text PRIMARY KEY NOT NULL,
"createdBy" text NOT NULL REFERENCES "user"(id) ON DELETE CASCADE,
"delegatedTo" text NOT NULL REFERENCES "user"(id) ON DELETE CASCADE,
PRIMARY KEY("createdBy", "delegatedTo")
"delegatedTo" text NOT NULL REFERENCES "user"(id) ON DELETE CASCADE
);


ALTER TABLE "delegation" ENABLE ELECTRIC;
ALTER PUBLICATION powersync ADD TABLE "delegation";
3 changes: 1 addition & 2 deletions db/migrations/02-create_items_table.sql
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,5 @@ CREATE TABLE IF NOT EXISTS "report" (
"udap_id" TEXT
);

ALTER PUBLICATION powersync ADD TABLE "report";

ALTER TABLE
"report" ENABLE ELECTRIC;
1 change: 0 additions & 1 deletion db/migrations/03-insert_udaps.sql

This file was deleted.

10 changes: 0 additions & 10 deletions db/migrations/04-add_chips_table.sql

This file was deleted.

1 change: 0 additions & 1 deletion db/migrations/06-insert_drac.sql

This file was deleted.

4 changes: 2 additions & 2 deletions db/migrations/07-add_service_instructeurs.sql
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
CREATE TABLE IF NOT EXISTS service_instructeurs (
id INTEGER NOT NULL PRIMARY KEY,
id TEXT NOT NULL PRIMARY KEY,
full_name TEXT NOT NULL,
short_name TEXT NOT NULL,
email TEXT,
tel TEXT,
udap_id TEXT
);

ALTER PUBLICATION powersync ADD TABLE "service_instructeurs";

ALTER TABLE service_instructeurs ENABLE ELECTRIC;
2 changes: 1 addition & 1 deletion db/migrations/902-rework_clauses.sql
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ CREATE TABLE clause_v2 (
,text text NOT NULL
);

ALTER TABLE clause_v2 ENABLE ELECTRIC;
ALTER PUBLICATION powersync ADD TABLE "clause_v2";
1 change: 0 additions & 1 deletion db/migrations/903-fix_clausev1.sql

This file was deleted.

2 changes: 1 addition & 1 deletion db/migrations/904-add_pdf_snapshot.sql
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ CREATE TABLE pdf_snapshot (
user_id TEXT
);

ALTER TABLE pdf_snapshot ENABLE ELECTRIC;
ALTER PUBLICATION powersync ADD TABLE "pdf_snapshot";
2 changes: 1 addition & 1 deletion db/migrations/907-add_pictures.sql
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ CREATE TABLE pictures (
"createdAt" TIMESTAMP
);

ALTER TABLE pictures ENABLE ELECTRIC;
ALTER PUBLICATION powersync ADD TABLE "pictures";
2 changes: 1 addition & 1 deletion db/migrations/908-add_tmp_pictures.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ CREATE TABLE tmp_pictures (
"createdAt" TIMESTAMP
);

ALTER TABLE tmp_pictures ENABLE ELECTRIC;
ALTER PUBLICATION powersync ADD TABLE "tmp_pictures";
2 changes: 1 addition & 1 deletion db/migrations/909-add_picture_lines.sql
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ CREATE TABLE picture_lines (
"createdAt" TIMESTAMP
);

ALTER TABLE picture_lines ENABLE ELECTRIC;
ALTER PUBLICATION powersync ADD TABLE "picture_lines";
14 changes: 14 additions & 0 deletions db/migrations/911-add_transactions.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
CREATE TABLE IF NOT EXISTS transactions (
id TEXT PRIMARY KEY,
op_id INTEGER NOT NULL,
tx_id INTEGER,
entity_id TEXT NOT NULL,
type TEXT NOT NULL,
op TEXT NOT NULL,
data TEXT,
user_id TEXT NOT NULL,
created_at TIMESTAMP,
error TEXT
);

ALTER PUBLICATION powersync ADD TABLE "transactions";
76 changes: 61 additions & 15 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ version: "3.1"
volumes:
pg_data:
minio_data:
mongo_storage:

services:
pg:
Expand All @@ -13,6 +14,8 @@ services:
POSTGRES_USER: ${POSTGRES_USER}
POSTGRES_PORT: ${POSTGRES_PORT}
command:
- -c
- listen_addresses=*
- -c
- wal_level=logical
ports:
Expand All @@ -21,25 +24,68 @@ services:
volumes:
- pg_data:/var/lib/postgresql/data
- ./db/init/:/docker-entrypoint-initdb.d
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres -d postgres"]
interval: 5s
timeout: 5s
retries: 5

electric:
image: electricsql/electric:0.12.0
mongo:
image: mongo:7.0
command: --replSet rs0 --bind_ip_all --quiet
restart: unless-stopped
ports:
- 27017:27017
volumes:
- mongo_storage:/data/db
# Initializes the MongoDB replica set. This service will not usually be actively running
mongo-rs-init:
image: mongo:7.0
depends_on:
- pg
- mongo
restart: on-failure
entrypoint:
- bash
- -c
- 'mongosh --host mongo:27017 --eval ''try{rs.status().ok && quit(0)} catch {} rs.initiate({_id: "rs0", version:
1, members: [{ _id: 0, host : "mongo:27017" }]})'''

# electric:
# image: electricsql/electric:0.12.0
# depends_on:
# - pg
# environment:
# DATABASE_REQUIRE_SSL: false
# DATABASE_URL: postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@pg:${POSTGRES_PORT}/${POSTGRES_DB}
# AUTH_JWT_KEY: ${JWT_SECRET}
# PG_PROXY_PASSWORD: ${PG_PROXY_PASSWORD}
# PG_PROXY_PORT: ${PG_PROXY_PORT}
# AUTH_MODE: secure
# LOGICAL_PUBLISHER_HOST: electric
# AUTH_JWT_ALG: HS256
# ELECTRIC_PG_PROXY_PORT: 65432
# ports:
# - ${ELECTRIC_PORT}:5133
# - ${PG_PROXY_PORT}:65432
# restart: always

powersync:
restart: unless-stopped
depends_on:
mongo-rs-init:
condition: service_completed_successfully
pg:
condition: service_healthy
image: journeyapps/powersync-service:latest
command: ["start", "-r", "unified"]
volumes:
- ./powersync-config.yaml:/config/config.yaml
environment:
DATABASE_REQUIRE_SSL: false
DATABASE_URL: postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@pg:${POSTGRES_PORT}/${POSTGRES_DB}
AUTH_JWT_KEY: ${JWT_SECRET}
PG_PROXY_PASSWORD: ${PG_PROXY_PASSWORD}
PG_PROXY_PORT: ${PG_PROXY_PORT}
AUTH_MODE: secure
LOGICAL_PUBLISHER_HOST: electric
AUTH_JWT_ALG: HS256
ELECTRIC_PG_PROXY_PORT: 65432
POWERSYNC_CONFIG_PATH: /config/config.yaml
PS_DATABASE_URL: ${PS_DATABASE_URL}
PS_JWT_SECRET: ${JWT_SECRET}
ports:
- ${ELECTRIC_PORT}:5133
- ${PG_PROXY_PORT}:65432
restart: always
- ${POWERSYNC_PORT:-3003}:8080

adminer:
image: adminer
Expand Down
15 changes: 6 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,11 @@
"scripts": {
"frontend": "pnpm --filter @cr-vif/frontend",
"backend": "pnpm --filter @cr-vif/backend",
"electric-client": "pnpm --filter @cr-vif/electric-client",
"pdf": "pnpm --filter @cr-vif/pdf",
"db:create": "pnpm backend migration:create",
"db:migrate": "pnpm db:create",
"electric:up": "pnpm electric-sql with-config \"pnpm pg-migrations apply --database {{ELECTRIC_PROXY}} --ignore-error migration_file_edited --directory ./db/migrations\"",
"show-config": "pnpm electric-sql show-config",
"electric:migrate": "pnpm electric:up && pnpm electric-client generate:front && pnpm electric-client generate:back",
"migration:up": "vite-node ./db/migrate.ts",
"client:json": "pnpm backend dev --create-only",
"client:ts": "typed-openapi ./packages/backend/openapi.json --output ./packages/frontend/src/api.gen.ts",
"client:generate": "pnpm client:json && pnpm client:ts",
"salut": "docker ps",
"e2e": "playwright test"
},
"keywords": [],
Expand All @@ -27,10 +21,13 @@
"@pandabox/prettier-plugin": "^0.1.0",
"@playwright/test": "^1.43.0",
"@types/node": "^20.11.28",
"electric-sql": "^0.12.1",
"cross-env": "^7.0.3",
"dotenv": "^16.4.5",
"dotenv-expand": "^11.0.6",
"prettier": "^3.2.5",
"prisma": "^4.8.1",
"typed-openapi": "^0.4.1"
"typed-openapi": "^0.4.1",
"vite-node": "^1.4.0"
},
"pnpm": {
"patchedDependencies": {
Expand Down
Loading

0 comments on commit 9a1af03

Please sign in to comment.