Skip to content

Commit bd4af34

Browse files
committed
feat(cms): improved seed process
closed COD-235, COD-243
1 parent 07a7065 commit bd4af34

File tree

88 files changed

+2487
-993
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

88 files changed

+2487
-993
lines changed

README.md

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,10 @@
2626
- [Startup Payload multi-tenant in dev mode](#startup-payload-multi-tenant-in-dev-mode)
2727
- [Terminal 1: Start Postgres and admin UI](#terminal-1-start-postgres-and-admin-ui)
2828
- [Terminal 2: Start web client](#terminal-2-start-web-client)
29-
- [Terminal 3: Start reverse proxy](#terminal-3-start-reverse-proxy)
29+
- [Terminal 3: Start reverse proxy to simulate multi-tenancy](#terminal-3-start-reverse-proxy-to-simulate-multi-tenancy)
3030
- [Development Tools \& Services](#development-tools--services)
3131
- [Infisical Secrets Management](#infisical-secrets-management)
3232
- [Fly.io Deployment](#flyio-deployment)
33-
- [Connect to the database on local machine](#connect-to-the-database-on-local-machine)
3433
- [Release Management](#release-management)
3534

3635
## Packages
@@ -95,25 +94,40 @@ The Payload suite consists of
9594
9695
### Terminal 1: Start Postgres and admin UI
9796

98-
Start a Docker container
97+
#### Start Postgres in Docker <!-- omit in toc -->
9998

10099
```sh
101100
nx dx:postgres cms
102101
```
103102

104-
Make sure database is in a fresh state (when needed)
103+
#### Serve admin UI <!-- omit in toc -->
105104

106105
```sh
107-
nx payload cms migrate:fresh
106+
nx serve cms
108107
```
109108

110-
Start the admin UI with live-reload
109+
> [!NOTE]
110+
> Database is auto-seeded with static data when the admin UI is started.
111+
112+
#### Optional <!-- omit in toc -->
113+
114+
##### Clear database and run migrations <!-- omit in toc -->
111115

112116
```sh
113-
nx serve cms
117+
nx payload cms migrate:fresh
114118
```
115119

116-
:bulb: Database auto-seed will run
120+
##### Generate seed data <!-- omit in toc -->
121+
122+
Seed data is stored in environment-specific TypeScript files in
123+
124+
- `libs/shared/data-access/seed/src/lib/seed-data`.
125+
126+
You can remove the existing seed data and save the empty object to the file to generate new seed data.
127+
128+
```sh
129+
nx seed cms
130+
```
117131

118132
### Terminal 2: Start web client
119133

@@ -124,37 +138,32 @@ nx serve cms
124138
nx start web
125139
```
126140

127-
### Terminal 3: Start reverse proxy
141+
### Terminal 3: Start reverse proxy to simulate multi-tenancy
128142

129143
```sh
130144
nx payload-proxy
131-
132-
# stop the proxy
133-
nx payload-proxy:down
134-
135-
# or extended command to restart
136-
nx proxy-cmd shared-util-payload restart
137145
```
138146

139147
> [!NOTE]
140148
> You can now access the different web sites as different tenants:
141149
>
142-
> **Admin UI** - Aimed for different maintainers
143150
> 🌐 `cms.localhost`
144151
>
145152
> :pouting_face: `system@local.dev` @ `dev`
146-
>
147-
> :pouting_face: `web-one.admin@local.dev` @ `dev`
148-
> :pouting_face: `web-one.user@local.dev` @ `dev`
149-
>
150-
> :pouting_face: `web-two.admin@local.dev` @ `dev`
151-
> :pouting_face: `web-two.user@local.dev` @ `dev`
152-
>
153-
> **Tenant 1**
154-
> 🌐 `web-one.localhost`
155-
>
156-
> **Tenant 2**
157-
> 🌐 `web-two.localhost`
153+
154+
#### Optional <!-- omit in toc -->
155+
156+
##### Stop the proxy <!-- omit in toc -->
157+
158+
```sh
159+
nx payload-proxy:down
160+
```
161+
162+
##### Restart the proxy <!-- omit in toc -->
163+
164+
```sh
165+
nx proxy-cmd shared-util-payload restart
166+
```
158167

159168
## Development Tools & Services
160169

@@ -245,7 +254,7 @@ For deployments to preview the applications will be attached to the Postgres clu
245254
> fly postgres create --name pg-preview --org codeware --region arn --vm-size shared-cpu-1x --volume-size 1 --initial-cluster-size 1
246255
> ```
247256
248-
##### Connect to the database on local machine
257+
##### Connect to the database on local machine <!-- omit in toc -->
249258
250259
Forward server port `5432` to local port `5433` to avoid conflicts with local Postgres running in Docker.
251260

apps/cms/README.md

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,48 +2,41 @@
22

33
Payload application generated by Nx plugin [`@cdwr/nx-payload`](https://github.com/codeware-sthlm/codeware/tree/master/packages/nx-payload).
44

5-
## Start the application in Docker
5+
## Getting started
6+
7+
### Start Postgres database in Docker
68

79
```sh
8-
npx nx start cms
10+
npx nx dx:start postgres
911
```
1012

11-
Visit <http://localhost:3000>
12-
13-
### Stop and remove Docker containers
13+
### Serve the application in development mode
1414

1515
```sh
16-
npx nx stop cms
16+
npx nx serve cms
1717
```
1818

19-
> Database is stored in persistent volume, so containers can be stopped and started without loosing the data
19+
Visit <http://localhost:3000>
2020

21-
## Serve application in development mode
21+
## Maintenance
2222

23-
First start the database in Docker when needed
23+
### Delete database and run migrations
2424

2525
```sh
26-
npx nx mongodb cms
26+
npx nx payload cms migrate:fresh
2727
```
2828

29-
or
29+
### Generate seed data
3030

31-
```sh
32-
npx nx postgres cms
33-
```
31+
Seed data is stored in environment-specific TypeScript files in
3432

35-
Then serve the application
33+
- `libs/shared/data-access/seed/src/lib/seed-data`.
3634

37-
```sh
38-
npx nx serve cms
39-
```
40-
41-
## Running tests
35+
You can remove the existing seed data and save the empty object to the file to generate new seed data.
4236

4337
```sh
44-
npx nx test cms
45-
```
38+
npx nx seed cms
4639

47-
```sh
48-
npx nx lint cms
40+
# specific environment
41+
DEPLOY_ENV=preview npx nx seed cms
4942
```

apps/cms/project.json

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"serve-fresh": {
1111
"executor": "nx:run-commands",
1212
"options": {
13-
"commands": ["nx payload cms migrate:fresh", "nx serve cms"],
13+
"commands": ["echo 'y' | nx payload cms migrate:fresh", "nx serve cms"],
1414
"parallel": false
1515
}
1616
},
@@ -42,6 +42,19 @@
4242
"PAYLOAD_CONFIG_PATH": "src/payload.config.ts"
4343
}
4444
}
45+
},
46+
"seed": {
47+
"metadata": {
48+
"description": "Ad-hoc seed process using the local-api only. This is useful for testing the seed process without spinning up the entire server."
49+
},
50+
"executor": "nx:run-commands",
51+
"options": {
52+
"command": "echo 'y' | npx tsx src/utils/seed.ts",
53+
"cwd": "apps/cms",
54+
"env": {
55+
"CWD": "../.."
56+
}
57+
}
4558
}
4659
}
4760
}

apps/cms/src/access/can-mutate-tenant-scope.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1+
import { getId, hasRole } from '@codeware/shared/util/payload';
12
import type { Access } from 'payload/types';
23

3-
import { getId } from '../utils/get-id';
4-
import { hasRole } from '../utils/has-role';
54
import { resolveTenant } from '../utils/resolve-tenant';
65

76
/**

apps/cms/src/access/can-read-tenant-scope.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1+
import { getTenantAccessIDs, hasRole } from '@codeware/shared/util/payload';
12
import type { Access, Where } from 'payload/types';
23

3-
import { getTenantAccessIDs } from '../utils/get-tenant-access-ids';
4-
import { hasRole } from '../utils/has-role';
54
import { resolveTenant } from '../utils/resolve-tenant';
65

76
/**

apps/cms/src/access/system-user.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import type { User } from '@codeware/shared/util/payload';
2-
3-
import { hasRole } from '../utils/has-role';
2+
import { hasRole } from '@codeware/shared/util/payload';
43

54
/**
65
* Access control reading the request for the logged in user.

apps/cms/src/collections/tenants/access/can-mutate-tenant.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
import type { User } from '@codeware/shared/util/payload';
2+
import { getId, hasRole } from '@codeware/shared/util/payload';
23
import type { Access } from 'payload/types';
34

4-
import { getId } from '../../../utils/get-id';
5-
import { hasRole } from '../../../utils/has-role';
6-
75
// eslint-disable-next-line @typescript-eslint/no-explicit-any
86
export const canMutateTenant: Access<any, User> = (args) => {
97
const {

apps/cms/src/collections/tenants/access/can-read-tenant.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import type { User } from '@codeware/shared/util/payload';
2+
import {
3+
getTenantAccessIDs,
4+
hasRole,
5+
parseCookies
6+
} from '@codeware/shared/util/payload';
27
import type { Access, Where } from 'payload/types';
38

4-
import { getTenantAccessIDs } from '../../../utils/get-tenant-access-ids';
5-
import { hasRole } from '../../../utils/has-role';
6-
import { parseCookies } from '../../../utils/parse-cookies';
7-
89
// eslint-disable-next-line @typescript-eslint/no-explicit-any
910
export const canReadTenant: Access<any, User> = (args) => {
1011
const {

apps/cms/src/collections/users/access/can-create-users.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
import type { User } from '@codeware/shared/util/payload';
2+
import { getTenantAccessIDs, hasRole } from '@codeware/shared/util/payload';
23
import type { Access } from 'payload/types';
34

4-
import { getTenantAccessIDs } from '../../../utils/get-tenant-access-ids';
5-
import { hasRole } from '../../../utils/has-role';
6-
75
/**
86
* Permission to create users
97
*

apps/cms/src/collections/users/access/can-delete-users.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
import type { User } from '@codeware/shared/util/payload';
2+
import { getTenantAccessIDs, hasRole } from '@codeware/shared/util/payload';
23
import type { Access, Where } from 'payload/types';
34

4-
import { getTenantAccessIDs } from '../../../utils/get-tenant-access-ids';
5-
import { hasRole } from '../../../utils/has-role';
6-
75
/**
86
* Permission to delete users
97
*

0 commit comments

Comments
 (0)