# Architecture

```mermaid
graph LR
    Mobile([Mobile])
    API[API]
    Postgres[(Postgres)]
    CDN{{CDN}}
    S3[(S3 Bucket)]
    Jenkins{{Jenkins}}
    Sentry{{Sentry}}
    Datadog{{Datadog}}
    LRU{{LRU Cache}}

    subgraph Clients
        Mobile
        Jenkins
    end
    subgraph Backend
        API
        CDN
    end
    subgraph DB
        LRU
        Postgres
    end
    subgraph Infrastructure
        Datadog
        Sentry
        DB
        S3
    end

    Mobile -->|Check update| API
    Mobile -->|Send analytics| API
    Mobile -->|Get bundle| CDN
    CDN -->|Get bundle| S3
    API -->|Store metadata| LRU
    API -->|Upload bundle| S3
    API -->|Send analytics| Datadog
    API -->|Track errors| Sentry
    LRU -->|Use| Postgres
    Jenkins -->|Manage releases| API
```

# Setup

```bash
cp .env.shadow .env
npm install
```

# Run

```bash
npm run docker:dev
npm run docker:test
```

# CLI

```bash
cp .npmrc.shadow .npmrc
npm run build
npm version patch
npm publish
npm install -g @theorchard/hot-updater
npm install @theorchard/hot-updater
```

```bash
npm run cli
npm run cli COMMAND help
```

# Docker

### Connect to Postgres

`docker exec -it hot-updater-postgres-1 psql -U postgres -d hot_updater_server`

---

### Connect to Localstack

`docker exec -it hot-updater-localstack-1 awslocal s3 ls`

`docker exec -it hot-updater-localstack-1 awslocal s3 ls s3://hot-updater-bundles`

# Prisma

###  Add hot updater models to schema (once)

```bash
npx hot-updater db generate src/hotUpdater.ts --yes
```

---

### Validate Prisma

```bash
npx prisma validate
npx prisma db pull --print
```

---

### Create migration if schema.prisma is changed in dev mode

```bash
npx prisma migrate dev
npx prisma migrate dev --name NAME
```

---

### Apply migration for production

```bash
npx prisma migrate deploy
```

---

### Code generation

```bash
npx prisma generate
```

