@nestjs-cms/core 0.1.1 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +267 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -2,6 +2,10 @@
|
|
|
2
2
|
|
|
3
3
|
Add a resource-driven CMS backend to a NestJS application. Define each resource in one file; the module discovers it and provides validated CRUD APIs, filters, lifecycle hooks, and S3-backed attachments.
|
|
4
4
|
|
|
5
|
+
For a complete API and admin workspace with login, start with
|
|
6
|
+
[Build your own CMS repository](#build-your-own-cms-repository). For a backend-only
|
|
7
|
+
integration into a new NestJS application, follow the quick start below.
|
|
8
|
+
|
|
5
9
|
## Quick start
|
|
6
10
|
|
|
7
11
|
Requires Node.js 22+, npm, and a PostgreSQL database. The package supports NestJS 11 with the Express adapter.
|
|
@@ -171,10 +175,271 @@ configureCmsApp(app);
|
|
|
171
175
|
|
|
172
176
|
Set `CORS_ORIGINS=http://localhost:5173`, for example. Your host application still controls its listening port, bind address, and bootstrap process. If it already manages CORS and shutdown hooks, keep that configuration instead.
|
|
173
177
|
|
|
178
|
+
## Build your own CMS repository
|
|
179
|
+
|
|
180
|
+
This walkthrough uses `kongyim/nestjs-cms-demo` as a starting template for your own
|
|
181
|
+
repository. It gives you a NestJS API, a Next.js admin with a dashboard, pages,
|
|
182
|
+
categories, users and roles, plus PostgreSQL and MinIO for local development.
|
|
183
|
+
All package installation commands use the latest releases from npm.
|
|
184
|
+
|
|
185
|
+
The two packages have different responsibilities: `@nestjs-cms/core` supplies
|
|
186
|
+
resource APIs and attachments; `@nestjs-cms/admin` supplies the React portal.
|
|
187
|
+
The demo's application code supplies login, sessions, account bootstrap, and
|
|
188
|
+
server-side permissions. Keep that code when using the demo as your template;
|
|
189
|
+
installing the packages alone does not create an authenticated CMS.
|
|
190
|
+
|
|
191
|
+
### 1. Create your workspace from the demo
|
|
192
|
+
|
|
193
|
+
Install Node.js 22+, npm, Git, and Docker with Docker Compose. Start Docker, then
|
|
194
|
+
run these commands in the directory where you keep your projects:
|
|
195
|
+
|
|
196
|
+
```sh
|
|
197
|
+
git clone https://github.com/kongyim/nestjs-cms-demo.git my-cms
|
|
198
|
+
cd my-cms
|
|
199
|
+
git remote rename origin upstream
|
|
200
|
+
npm install
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
This keeps the demo's history and names its remote `upstream`. To push to your
|
|
204
|
+
own repository later, create an empty Git repository with your hosting provider,
|
|
205
|
+
then run `git remote add origin <your-repository-url>`.
|
|
206
|
+
|
|
207
|
+
The workspace already includes the app configuration and root npm scripts:
|
|
208
|
+
|
|
209
|
+
```text
|
|
210
|
+
my-cms/
|
|
211
|
+
apps/
|
|
212
|
+
api/
|
|
213
|
+
src/auth/ Login, sessions, passwords, and permission enforcement
|
|
214
|
+
src/resources/ Backend schemas for pages, categories, roles, and users
|
|
215
|
+
src/app.module.ts CMS and authentication registration
|
|
216
|
+
src/env.ts Loads the shared root .env
|
|
217
|
+
admin/
|
|
218
|
+
app/ Next.js App Router shell
|
|
219
|
+
src/admin.config.ts
|
|
220
|
+
src/sections/ Resource forms and dashboard registration
|
|
221
|
+
src/components/ Custom dashboard component
|
|
222
|
+
next.config.ts Admin base path and same-origin API proxy
|
|
223
|
+
docker/ MinIO build and private-bucket initialization
|
|
224
|
+
docker-compose.yml
|
|
225
|
+
package.json npm workspaces and commands for both apps
|
|
226
|
+
.env Create this in step 3; do not commit it
|
|
227
|
+
```
|
|
228
|
+
|
|
229
|
+
The folder name `my-cms` does not change the npm workspace names. The commands
|
|
230
|
+
below use the template's existing `@nestjs-cms-demo/api` and
|
|
231
|
+
`@nestjs-cms-demo/admin` names.
|
|
232
|
+
|
|
233
|
+
### 2. Install the latest CMS packages
|
|
234
|
+
|
|
235
|
+
From `my-cms`, update both workspaces:
|
|
236
|
+
|
|
237
|
+
```sh
|
|
238
|
+
npm install @nestjs-cms/core@latest --workspace=@nestjs-cms-demo/api
|
|
239
|
+
npm install @nestjs-cms/admin@latest --workspace=@nestjs-cms-demo/admin
|
|
240
|
+
```
|
|
241
|
+
|
|
242
|
+
If your copy of the template still uses the previous backend name `nestjs-cms`,
|
|
243
|
+
replace all imports from `'nestjs-cms'` with `'@nestjs-cms/core'` in
|
|
244
|
+
`apps/api/src` and `apps/api/test`, including type imports, then remove it:
|
|
245
|
+
|
|
246
|
+
```sh
|
|
247
|
+
npm uninstall nestjs-cms --workspace=@nestjs-cms-demo/api
|
|
248
|
+
```
|
|
249
|
+
|
|
250
|
+
Only the package name changes; keep the demo's resource definitions and
|
|
251
|
+
authentication wiring. No local tarballs or library checkout are needed. Commit
|
|
252
|
+
the updated package files and root lockfile with your application.
|
|
253
|
+
|
|
254
|
+
### 3. Configure the database, storage, and first administrator
|
|
255
|
+
|
|
256
|
+
Create `.env` in `my-cms` (beside `docker-compose.yml`), using this complete local
|
|
257
|
+
development configuration. Replace the example passwords before starting:
|
|
258
|
+
|
|
259
|
+
```dotenv
|
|
260
|
+
NODE_ENV=development
|
|
261
|
+
PORT=3000
|
|
262
|
+
|
|
263
|
+
DB_TYPE=postgres
|
|
264
|
+
DB_HOST=127.0.0.1
|
|
265
|
+
DB_PORT=5432
|
|
266
|
+
DB_USERNAME=cms
|
|
267
|
+
DB_PASSWORD=change-this-local-db-password
|
|
268
|
+
DB_DATABASE=cms
|
|
269
|
+
DB_SYNCHRONIZE=true
|
|
270
|
+
|
|
271
|
+
S3_REGION=us-east-1
|
|
272
|
+
S3_BUCKET=cms-attachments
|
|
273
|
+
S3_ENDPOINT=http://127.0.0.1:9000
|
|
274
|
+
S3_FORCE_PATH_STYLE=true
|
|
275
|
+
S3_ACCESS_KEY_ID=minioadmin
|
|
276
|
+
S3_SECRET_ACCESS_KEY=change-this-local-storage-password
|
|
277
|
+
ATTACHMENT_MAX_BYTES=20971520
|
|
278
|
+
|
|
279
|
+
PGADMIN_DEFAULT_EMAIL=admin@example.com
|
|
280
|
+
PGADMIN_DEFAULT_PASSWORD=change-this-local-pgadmin-password
|
|
281
|
+
|
|
282
|
+
ADMIN_BASE_PATH=/admin
|
|
283
|
+
ADMIN_ORIGIN=http://localhost:3001
|
|
284
|
+
ADMIN_BOOTSTRAP_EMAIL=admin@example.com
|
|
285
|
+
ADMIN_BOOTSTRAP_PASSWORD=choose-a-unique-admin-password
|
|
286
|
+
ADMIN_API_KEY=
|
|
287
|
+
CORS_ORIGINS=
|
|
288
|
+
```
|
|
289
|
+
|
|
290
|
+
Use a 12–128 character administrator password. `ADMIN_API_KEY` can be empty for
|
|
291
|
+
this local setup: the demo still requires a valid user session and checks
|
|
292
|
+
permissions. Never expose a shared API key in frontend configuration.
|
|
293
|
+
|
|
294
|
+
The API and Next.js configuration load this root `.env`; Docker Compose reads
|
|
295
|
+
it too. `ADMIN_ORIGIN` is the browser origin without `/admin`. Keep browser URLs
|
|
296
|
+
on `localhost` consistently so origin checks and session cookies work together.
|
|
297
|
+
|
|
298
|
+
### 4. Start PostgreSQL and MinIO
|
|
299
|
+
|
|
300
|
+
From the workspace root:
|
|
301
|
+
|
|
302
|
+
```sh
|
|
303
|
+
docker compose up -d --build
|
|
304
|
+
docker compose ps -a
|
|
305
|
+
docker compose logs minio-init
|
|
306
|
+
```
|
|
307
|
+
|
|
308
|
+
The first build compiles MinIO and can take several minutes. Wait until
|
|
309
|
+
PostgreSQL and MinIO are healthy and `minio-init` exits with code 0. The initializer
|
|
310
|
+
creates the private bucket configured by `S3_BUCKET`.
|
|
311
|
+
|
|
312
|
+
The API runs on your host and connects to `127.0.0.1:5432` and
|
|
313
|
+
`http://127.0.0.1:9000`. If port 5432 is occupied, choose another `DB_PORT` in
|
|
314
|
+
`.env`; Compose and the API both use it. The other default ports are 3000, 3001,
|
|
315
|
+
8080, 9000, and 9001; stop conflicting services before continuing.
|
|
316
|
+
|
|
317
|
+
### 5. Start both apps and sign in
|
|
318
|
+
|
|
319
|
+
Run each command in a separate terminal, both from `my-cms`:
|
|
320
|
+
|
|
321
|
+
```sh
|
|
322
|
+
# Terminal 1
|
|
323
|
+
npm run dev:api
|
|
324
|
+
```
|
|
325
|
+
|
|
326
|
+
```sh
|
|
327
|
+
# Terminal 2
|
|
328
|
+
npm run dev:admin
|
|
329
|
+
```
|
|
330
|
+
|
|
331
|
+
Open **http://localhost:3001/admin** and sign in with your
|
|
332
|
+
`ADMIN_BOOTSTRAP_EMAIL` and `ADMIN_BOOTSTRAP_PASSWORD`. The first API startup
|
|
333
|
+
creates the tables and, only when the users table is empty, creates the
|
|
334
|
+
Administrator role and account. After a successful login, remove both bootstrap
|
|
335
|
+
variables from `.env` and restart the API. Changing those variables later does
|
|
336
|
+
not reset an existing account's password.
|
|
337
|
+
|
|
338
|
+
Try the complete workflow:
|
|
339
|
+
|
|
340
|
+
1. Create a category in **Categories**.
|
|
341
|
+
2. Create a page in **Pages**, select the category, and save it.
|
|
342
|
+
3. Edit the page, add an attachment, save, and reopen it to check persistence.
|
|
343
|
+
4. Create an Editor role with `pages:read` and `pages:write`, then create a user
|
|
344
|
+
with that role and a password. Sign in as that user to check restricted access.
|
|
345
|
+
|
|
346
|
+
| Service | Local URL | Login |
|
|
347
|
+
| ------------- | ----------------------------- | ----------------------------------------------------- |
|
|
348
|
+
| Admin portal | `http://localhost:3001/admin` | CMS user email and password |
|
|
349
|
+
| NestJS API | `http://localhost:3000/api` | Session-protected; use the admin proxy in the browser |
|
|
350
|
+
| MinIO console | `http://localhost:9001` | `S3_ACCESS_KEY_ID` / `S3_SECRET_ACCESS_KEY` |
|
|
351
|
+
| pgAdmin | `http://localhost:8080` | `PGADMIN_DEFAULT_EMAIL` / `PGADMIN_DEFAULT_PASSWORD` |
|
|
352
|
+
|
|
353
|
+
In pgAdmin, open the preconfigured **CMS PostgreSQL** connection and enter
|
|
354
|
+
`DB_PASSWORD`. Its internal database host is `postgres`, not `localhost`.
|
|
355
|
+
|
|
356
|
+
Browser requests to `/admin/api/*` are proxied by Next.js to the NestJS `/api/*`
|
|
357
|
+
routes. The demo's default session cookie uses `/admin/api`, so test authenticated
|
|
358
|
+
browser requests through the admin URL. No separate browser CORS setup is needed.
|
|
359
|
+
|
|
360
|
+
### 6. Customize your CMS
|
|
361
|
+
|
|
362
|
+
Start with these files:
|
|
363
|
+
|
|
364
|
+
| Change | File or directory |
|
|
365
|
+
| ------------------------------------------------ | ----------------------------------------- |
|
|
366
|
+
| Portal title, initial screen, navigation order | `apps/admin/src/admin.config.ts` |
|
|
367
|
+
| Dashboard content | `apps/admin/src/components/dashboard.tsx` |
|
|
368
|
+
| Backend fields, validation, filters, attachments | `apps/api/src/resources/` |
|
|
369
|
+
| List columns, search controls, form fields | `apps/admin/src/sections/` |
|
|
370
|
+
| Server-side resource permissions | `apps/api/src/auth/auth.guard.ts` |
|
|
371
|
+
| Admin URL prefix and API destination | `apps/admin/next.config.ts` |
|
|
372
|
+
|
|
373
|
+
For example, change the `title` in `admin.config.ts` and reorder its `sections`
|
|
374
|
+
array to rearrange navigation. `defaultSection` selects the first screen without
|
|
375
|
+
changing that order. Use `defineCustomSection` for a custom React page such as the
|
|
376
|
+
dashboard; use `defineSection` for a resource list and editor.
|
|
377
|
+
|
|
378
|
+
To add a resource, create its backend definition in `apps/api/src/resources`
|
|
379
|
+
using [the resource guide](docs/resources.md), then create a matching frontend
|
|
380
|
+
section whose `key` is the resource name. Import the section in `admin.config.ts`
|
|
381
|
+
and add it to `sections`. Backend resources are discovered automatically on
|
|
382
|
+
restart; frontend sections are registered explicitly. Match form fields to the
|
|
383
|
+
backend schema, and update the host guard and role permissions for the new
|
|
384
|
+
resource. Hiding navigation alone does not restrict API access.
|
|
385
|
+
|
|
386
|
+
The [admin package guide](packages/admin/README.md) covers grouped forms,
|
|
387
|
+
responsive field widths, nested JSON, attachment previews, password actions,
|
|
388
|
+
and custom pages.
|
|
389
|
+
|
|
390
|
+
To move the portal, set `ADMIN_BASE_PATH=/cms` (or `/tools/cms`) in `.env` and
|
|
391
|
+
restart both apps. Sign in again at the new path. Set `ADMIN_BASE_PATH=` for `/`.
|
|
392
|
+
Production builds must be rebuilt after a base-path change because Next.js embeds
|
|
393
|
+
the path in browser assets.
|
|
394
|
+
|
|
395
|
+
### 7. Check and build your repository
|
|
396
|
+
|
|
397
|
+
From the workspace root:
|
|
398
|
+
|
|
399
|
+
```sh
|
|
400
|
+
npm test -- --runInBand
|
|
401
|
+
npm run typecheck:admin
|
|
402
|
+
npm run build
|
|
403
|
+
```
|
|
404
|
+
|
|
405
|
+
The build compiles both apps. To run the built applications, stop the development
|
|
406
|
+
servers and use separate terminals:
|
|
407
|
+
|
|
408
|
+
```sh
|
|
409
|
+
# Terminal 1
|
|
410
|
+
npm run start:api
|
|
411
|
+
```
|
|
412
|
+
|
|
413
|
+
```sh
|
|
414
|
+
# Terminal 2
|
|
415
|
+
npm run start:admin
|
|
416
|
+
```
|
|
417
|
+
|
|
418
|
+
For deployment, use HTTPS, `NODE_ENV=production`, `DB_SYNCHRONIZE=false`, reviewed
|
|
419
|
+
database migrations, a server-only `ADMIN_API_KEY` of at least 32 characters, and
|
|
420
|
+
the public admin origin in `ADMIN_ORIGIN`. Migrations must include the host's
|
|
421
|
+
resource, credential, and session tables. If the API runs on another host, update
|
|
422
|
+
the rewrite destination in `apps/admin/next.config.ts` before building. See
|
|
423
|
+
[operations](docs/operations.md) for database and storage configuration.
|
|
424
|
+
|
|
425
|
+
To stop local infrastructure, run `docker compose down`; the volumes retain your
|
|
426
|
+
data. Adding `-v` deletes database and storage data. Changing database credentials
|
|
427
|
+
in `.env` does not update a PostgreSQL user already stored in a volume.
|
|
428
|
+
|
|
429
|
+
### Troubleshooting the workspace
|
|
430
|
+
|
|
431
|
+
| Symptom | Check |
|
|
432
|
+
| ------------------------------------------- | ----------------------------------------------------------------------------------------------------------- |
|
|
433
|
+
| API cannot connect to PostgreSQL | Check `docker compose ps`, `DB_PORT`, and root `.env` credentials. |
|
|
434
|
+
| Login fails on a fresh setup | Check API startup logs, bootstrap password length, and whether the users table already contains an account. |
|
|
435
|
+
| Login works but requests fail or return 403 | Use `http://localhost:3001/admin`, check `ADMIN_ORIGIN`, and verify the user's role permissions. |
|
|
436
|
+
| Attachments return 503 | Check MinIO health, `minio-init` logs, and the API's S3 endpoint, credentials, and bucket. |
|
|
437
|
+
| A new resource or section is missing | Restart the API for resource discovery; import the frontend section and add it to `sections`. |
|
|
438
|
+
|
|
174
439
|
## Migrating from `nestjs-cms`
|
|
175
440
|
|
|
176
|
-
The backend package is now named `@nestjs-cms/core`.
|
|
177
|
-
|
|
441
|
+
The backend package is now named `@nestjs-cms/core`. Existing applications can
|
|
442
|
+
switch with:
|
|
178
443
|
|
|
179
444
|
```sh
|
|
180
445
|
npm uninstall nestjs-cms
|