nest-my-admin 0.0.0-stage → 0.2.1
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/LICENSE +21 -0
- package/README.md +817 -2
- package/index.d.ts +1 -0
- package/index.js +2 -0
- package/package.json +32 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 nest-my-admin contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,818 @@
|
|
|
1
|
-
#
|
|
1
|
+
# nest-my-admin
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A Django-admin-class admin panel for **NestJS + TypeORM**: declarative resources, forms from your DTOs,
|
|
4
|
+
admin writes that go through your own services, and a prebuilt shadcn UI served by Nest itself.
|
|
5
|
+
|
|
6
|
+
> Status: pre-alpha, version 0.2.1 (lists and forms, sign-in, permissions, files and rich text, actions, settings pages, audit log). APIs will change.
|
|
7
|
+
>
|
|
8
|
+
> **Upgrading from 0.2.0:** nothing breaks; see [Upgrading to 0.2.1](#upgrading-to-021) for what is new and what you may notice.
|
|
9
|
+
>
|
|
10
|
+
> **Upgrading from 0.1:** boot can now fail for an app that booted before, and a few behaviours changed. See [Upgrading to 0.2](#upgrading-to-02).
|
|
11
|
+
|
|
12
|
+
Requires NestJS 11 or 12, TypeORM 0.3.20+ or 1.x, Node 20.19+ (or Bun), and Postgres, MySQL 8 or SQLite.
|
|
13
|
+
The package is ESM; CommonJS apps load it through Node's `require(esm)`. A CommonJS app compiled with
|
|
14
|
+
`"module": "nodenext"` (the `nest new` default since Nest 11) needs TypeScript 5.8+; with `"module": "commonjs"` any 5.x works.
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
npm install @nest-my-admin/core # the admin (brings the prebuilt UI with it)
|
|
18
|
+
npm install @nest-my-admin/auth # optional: the built-in sign-in
|
|
19
|
+
npm install -D @nest-my-admin/testing # optional: test helpers
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
`nest-my-admin` (no scope) is the same package as `@nest-my-admin/core` under a shorter name: it re-exports it, at the same version.
|
|
23
|
+
|
|
24
|
+
```ts
|
|
25
|
+
@AdminResource(Product, { icon: 'package' })
|
|
26
|
+
export class ProductAdmin extends AdminResourceBase<Product> {
|
|
27
|
+
constructor(private readonly products: ProductsService) { super(); }
|
|
28
|
+
list: ListConfig<Product> = { columns: ['id', 'name', 'sku', 'price'] };
|
|
29
|
+
form: FormConfig = { create: CreateProductDto, update: UpdateProductDto };
|
|
30
|
+
create(dto: CreateProductDto) { return this.products.create(dto); } // your business rules run
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
@AdminGroup({ label: 'Catalog' })
|
|
34
|
+
@Module({ providers: [ProductsService, ProductAdmin] })
|
|
35
|
+
export class CatalogModule {}
|
|
36
|
+
|
|
37
|
+
@Module({ imports: [TypeOrmModule.forRoot({ ... }), AdminModule.forRoot({ title: 'My shop' }), CatalogModule] })
|
|
38
|
+
export class AppModule {}
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
Open `http://localhost:3000/admin`.
|
|
42
|
+
|
|
43
|
+
Lists support filters and search with a URL syntax you can bookmark:
|
|
44
|
+
|
|
45
|
+
```
|
|
46
|
+
/admin/api/resources/product?search=lamp&filter[status][eq]=active&filter[price][between]=10,20&sort=-price
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
Operators: `eq ne in nin lt lte gt gte between contains startsWith isNull`. Choose filterable and searchable
|
|
50
|
+
fields with `list: { filters: [...], search: [...] }`; hooks (`@BeforeSave`, `@AfterSave`, `@BeforeDelete`) run
|
|
51
|
+
in the default create/update/delete, and `AdminModule.forRoot({ autoRegister: true })` exposes every entity
|
|
52
|
+
without a resource. `ne` and `nin` exclude NULLs (SQL semantics), and datetime filter values must be ISO-8601
|
|
53
|
+
with a time zone.
|
|
54
|
+
|
|
55
|
+
### Sidebar groups and order
|
|
56
|
+
|
|
57
|
+
```ts
|
|
58
|
+
@AdminGroup({ key: 'settings', label: { en: 'Settings', fa: 'تنظیمات' }, icon: 'settings', order: 900 })
|
|
59
|
+
@Module({ providers: [BrandingPage, SeoPage] })
|
|
60
|
+
export class SettingsModule {}
|
|
61
|
+
|
|
62
|
+
@AdminSettingsPage('branding', { group: 'settings', order: 10, form: BrandingDto }) // first
|
|
63
|
+
@AdminSettingsPage('seo', { group: 'settings', order: 20, form: SeoDto }) // second
|
|
64
|
+
@AdminResource(Product, { order: 10 }) // first among its group's resources
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
- **A group is a Nest module.** Resources and settings pages go to the group of the module that provides them; `@AdminGroup` on the module sets its `key` (default: the module's name in kebab-case, without `Module`), `label`, `icon` and `order` (lower first, default 100; then by label).
|
|
68
|
+
- **`group: '<key>'`** on a resource or a page puts it into another group. That group is configured by the module whose `@AdminGroup` has that key: any module of the app, whether the group's members are its own or not, resources, pages or both, in any import order. Such a module may provide nothing at all. A key that no `@AdminGroup` names gets its key as label (`odds-and-ends` → "Odds and ends"), no icon and order 100.
|
|
69
|
+
- **One module per key.** When two modules configure the same key with a different label, icon or order, the first one Nest discovers (import order) wins and a warning is logged at boot. The same options twice are fine.
|
|
70
|
+
- **`order` inside a group.** `@AdminResource(Entity, { order })` and `@AdminSettingsPage(name, { order })`: lower comes first, then the label in the reader's language; the default is 100, as for groups. A group lists its resources first and its settings pages after them, so `order` sorts resources among resources and pages among pages. The sidebar, the group's page, the home page and the command palette all show this order. `order` must be a finite number, or boot fails.
|
|
71
|
+
|
|
72
|
+
### Customising lists
|
|
73
|
+
|
|
74
|
+
Put row restrictions in `query(qb, ctx)`. It applies to every read path: the list, GET, PATCH, DELETE, restore and
|
|
75
|
+
purge by id, and the pickers of relations that point at the resource (including the check of ids sent to them).
|
|
76
|
+
Always use `qb.alias`, because the alias differs from one path to another:
|
|
77
|
+
|
|
78
|
+
```ts
|
|
79
|
+
query(qb: SelectQueryBuilder<Order>, ctx: AdminContext) {
|
|
80
|
+
return qb.andWhere(`${qb.alias}.archived = false`);
|
|
81
|
+
}
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
For joins or aggregates, override `findMany` and start from `this.buildListQuery(params, ctx)`. It already applies
|
|
85
|
+
`query()`, filters, search, sort and paging (keyset included).
|
|
86
|
+
|
|
87
|
+
### The list page
|
|
88
|
+
|
|
89
|
+
- **Filters.**
|
|
90
|
+
- Enum filters are multi-selects (`in`), and nullable fields add an empty or not-empty choice.
|
|
91
|
+
- Active filters show as chips. Removing one chip removes only that filter.
|
|
92
|
+
- Filters live in the URL, so a shared link shows the same list.
|
|
93
|
+
- **Columns.** "Columns" shows, hides and reorders columns and switches density. Header edges resize columns (arrow
|
|
94
|
+
keys work too). The layout is remembered per resource in the browser.
|
|
95
|
+
- **Selection.** Select rows, then "Delete selected". It calls
|
|
96
|
+
`POST …/bulk-delete { ids }` → `{ ok, failed: [{ id, code, message }] }`, and each record is deleted on its own
|
|
97
|
+
(hooks, `query()` and trash apply), so one refusal does not stop the others.
|
|
98
|
+
- **Quick view.** A row click (or Enter on a focused row) opens a side sheet with the record, with Edit and Close.
|
|
99
|
+
The first column still links straight to the edit page.
|
|
100
|
+
- **Editing in cells.** Fields listed in `list.editable` are edited in their cell. They must be in the update form
|
|
101
|
+
and be text, number, decimal, bigint, boolean, enum or date fields. Each save is a normal PATCH (DTO, relation checks,
|
|
102
|
+
`If-Match`), and a refused change shows its reason under the cell:
|
|
103
|
+
|
|
104
|
+
```ts
|
|
105
|
+
list: ListConfig<Product> = { columns: ['name', 'status', 'stock'], editable: ['status', 'stock'] };
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
### Lists at scale
|
|
109
|
+
|
|
110
|
+
- **`list.count`:**
|
|
111
|
+
- `'exact'` is the default.
|
|
112
|
+
- `'estimate'` answers with the query planner's row estimate on Postgres and MySQL, marked `estimated: true`, and
|
|
113
|
+
shown as "about 12,000". It counts exactly below 1000 rows and on other drivers.
|
|
114
|
+
- `'none'` does not count and only says `hasMore`.
|
|
115
|
+
- **`list.pagination: 'keyset'`** pages with a cursor (`?after=`, from `nextCursor`), which stays fast deep into big
|
|
116
|
+
tables:
|
|
117
|
+
- it sorts only by non-nullable columns;
|
|
118
|
+
- it counts nothing unless `count` asks;
|
|
119
|
+
- the UI shows Previous/Next.
|
|
120
|
+
- **Several DataSources:**
|
|
121
|
+
- `@AdminResource(Report, { dataSource: 'reports' })` reads and writes, transactions included, through that
|
|
122
|
+
DataSource.
|
|
123
|
+
- `autoRegister: ['default', 'reports']` covers several DataSources. A taken name gets the DataSource as a prefix
|
|
124
|
+
(`reports-widget`).
|
|
125
|
+
- **Related lists.** When another resource has a relation field pointing at this one, the edit page links to that
|
|
126
|
+
resource's list, filtered by the record (a customer's orders). The filter is added to the other resource for it.
|
|
127
|
+
- **Relations by title.** A to-one relation whose target is titled by a column sorts by that column (`sort=customer`
|
|
128
|
+
sorts by customer name).
|
|
129
|
+
- **Lazy relations** (`{ lazy: true }`) are fields like the others.
|
|
130
|
+
- **Dependent options.** `relationOptions(field, qb, ctx, values)` gets the record's values as the user sees them, so
|
|
131
|
+
one field's options can depend on another:
|
|
132
|
+
- in the picker, the form's current values;
|
|
133
|
+
- on create, the body;
|
|
134
|
+
- on update, the stored record with the body on top.
|
|
135
|
+
|
|
136
|
+
### Relations and record titles
|
|
137
|
+
|
|
138
|
+
Many-to-one, owning one-to-one and owning many-to-many relations are fields:
|
|
139
|
+
|
|
140
|
+
- **Field names.** A to-one relation's field is named like the property that holds the id: `customer`, or
|
|
141
|
+
`customerId` when the entity declares `@Column() customerId` next to `@JoinColumn({ name: 'customerId' }) customer`.
|
|
142
|
+
A many-to-many field is named like the relation (`tags`). Name DTO properties the same way.
|
|
143
|
+
- **Values.** Reads return `{ id, title }` (or `null`) for to-one relations and `[{ id, title }]` for many-to-many.
|
|
144
|
+
Writes send the id (or `null`) and an array of ids. Every id is checked before your `create`/`update` runs: a
|
|
145
|
+
missing record is a 422 on that field. The default `create`/`update` save the ids as references; your own
|
|
146
|
+
overrides receive the ids as sent.
|
|
147
|
+
- **Paths.** `list.columns`, `filters`, `search` and `sort` accept paths through to-one relations, such as
|
|
148
|
+
`'customer.name'` or `'customer.company.name'` (up to 3 segments, checked at boot). The relations are joined
|
|
149
|
+
automatically as `entity_customer`, `entity_customer_company`, … (you can use these aliases in `buildListQuery`
|
|
150
|
+
extensions). Relation values are loaded after `findMany`/`findOne` with a fixed number of queries, so your finder
|
|
151
|
+
overrides do not need to join them.
|
|
152
|
+
- **Filters.** `filter[customer][eq]=3` (also `ne`, `in`, `nin`, and `isNull` when nullable), and
|
|
153
|
+
`filter[tags][in]=1,2`, which matches records that have any of those ids.
|
|
154
|
+
- **Titles.** `@AdminResource(Customer, { title: 'name' })` or `title: (c) => \`${c.first} ${c.last}\``. Titles show
|
|
155
|
+
in pickers, relation cells and headers, and as `_title` on every record. Without one, the first string column
|
|
156
|
+
named `name`, `title`, `label`, `displayName`, `fullName`, `username`, `email`, `code` or `sku` is used; else `#<id>`.
|
|
157
|
+
- **Picker options.** `relationOptions(field, qb, ctx, values)` restricts what a relation field may point to, both in the
|
|
158
|
+
picker (`GET …/fields/:field/options`) and on writes. `qb` selects the target as `option`:
|
|
159
|
+
|
|
160
|
+
```ts
|
|
161
|
+
relationOptions(field: string, qb: SelectQueryBuilder<any>, ctx: AdminContext, values: Record<string, unknown>) {
|
|
162
|
+
return field === 'customer' ? qb.andWhere('option.active = true') : qb;
|
|
163
|
+
}
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
### Entity shapes
|
|
167
|
+
|
|
168
|
+
- **Record ids.** Every record carries `_id`, the id used in URLs: key values in primary-key order, joined by `,`,
|
|
169
|
+
with `~` written `~0` and `,` written `~1`. Composite primary keys work this way; their `findOne`, `update` and
|
|
170
|
+
`delete` receive `{ key: value, … }`, and single-key resources keep receiving the plain value.
|
|
171
|
+
- **Embedded columns** (`@Column(() => Address) address`) are a group of fields in the form. Records nest them
|
|
172
|
+
(`address: { city, zip }`), and paths such as `'address.city'` work in `list.columns`, filters, search and sort. A
|
|
173
|
+
PATCH may send part of a group (`{ address: { zip } }`).
|
|
174
|
+
- **Nested DTOs** (`@ValidateNested() @Type(() => AddressDto) address`) make sub-forms, and
|
|
175
|
+
`@IsArray() @ValidateNested({ each: true }) @Type(() => LineDto) lines` makes a list of sub-forms, for example over
|
|
176
|
+
a `simple-json` column. Field errors use dotted paths (`address.city`, `lines.1.qty`).
|
|
177
|
+
- **Single-table inheritance**: each `@ChildEntity` gets its own resource (autoRegister included), which creates rows
|
|
178
|
+
of its kind and lists only those. The root resource lists every row with a read-only discriminator and does not
|
|
179
|
+
create.
|
|
180
|
+
- **`@VersionColumn`**: the form sends `If-Match: "<version>"` with PATCH and DELETE. If someone saved the record in
|
|
181
|
+
between, the answer is 409 with `current` (the record as it is now), and the form offers "Keep my changes" or "Load
|
|
182
|
+
theirs". The check runs in the write transaction, holding a row lock on Postgres and MySQL, before your `update`.
|
|
183
|
+
- **`@DeleteDateColumn`**: DELETE moves the record to the trash (the default `delete` soft-deletes; a service
|
|
184
|
+
override should call `softRemove`). `?trashed=only|with` lists the trash, `POST …/:id/restore` restores a record
|
|
185
|
+
(the resource's `restore`), and `DELETE …/:id?purge=true` removes it for good (`purge`). `@BeforeDelete` hooks get
|
|
186
|
+
`'soft'` or `'hard'`.
|
|
187
|
+
- **CHECK constraint** violations are a 422 on the columns the check's expression names. TypeORM does not create
|
|
188
|
+
checks on MySQL.
|
|
189
|
+
|
|
190
|
+
### Languages, theme and branding
|
|
191
|
+
|
|
192
|
+
```ts
|
|
193
|
+
AdminModule.forRoot({
|
|
194
|
+
title: { en: 'Demo shop', fa: 'فروشگاه نمونه' },
|
|
195
|
+
locale: 'en', // default language
|
|
196
|
+
locales: ['en', 'fa'], // the UI ships English and Persian; Persian is right-to-left
|
|
197
|
+
branding: { name: { en: 'Shop', fa: 'فروشگاه' }, logo: '/static/logo.svg', primaryColor: '#0f766e', radius: '0.5rem' },
|
|
198
|
+
});
|
|
199
|
+
@AdminGroup({ label: { en: 'Sales', fa: 'فروش' }, icon: 'cart' })
|
|
200
|
+
@AdminResource(Order, { label: { en: 'Orders', fa: 'سفارشها' }, icon: 'receipt' })
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
- **Languages.**
|
|
204
|
+
- The language switch appears when there is more than one locale, and the choice is remembered in the browser.
|
|
205
|
+
- The page itself (`GET <path>/` and every deep link) is served in the default `locale`: `<html lang dir>` (`dir` is `rtl` for `fa`, `ar`, `he` and `ur`), your `title` in that language as `<title>`, and `Content-Language`. It is the same page for every request; the browser's `Accept-Language` is not consulted, because the UI starts in the default locale too.
|
|
206
|
+
- A reader who switched to another language gets the default-locale page for the moment before the script runs, which then sets `lang`, `dir` and the title for their choice (the choice lives in their browser, where the server cannot see it).
|
|
207
|
+
- The UI sends the language in use as `Accept-Language` with every API request. Labels given per language answer in it, falling back to the default
|
|
208
|
+
locale.
|
|
209
|
+
- Resources and hooks see it as `ctx.locale`.
|
|
210
|
+
- Field labels, help, placeholders and enum value labels take the same `{ en, fa }` form (see Fields and forms).
|
|
211
|
+
- **Branding.**
|
|
212
|
+
- `primaryColor` may be hex, `rgb()`, `hsl()` or `oklch()`, and `radius` a length. Text on the primary colour is
|
|
213
|
+
chosen from its lightness.
|
|
214
|
+
- `logo` must be an http(s) URL or a relative path.
|
|
215
|
+
- Anything else fails at boot, so no value can inject CSS.
|
|
216
|
+
- **Theme.** Light, dark or system, from the header. It is applied before the page renders, so nothing flashes.
|
|
217
|
+
- **Icons** for groups and resources: bell, book, box, boxes, briefcase, building, calendar, cart, chart, clock, credit-card, database, dollar, file, file-text, folder, globe, heart, history, home, image, inbox, key, layers, link, list, lock, mail, map, map-pin, message, package, phone, printer, receipt, server, settings, shield, shopping-bag, shopping-cart, star, store, tag, tags, ticket, truck, user, users, video, wallet, wrench, zap.
|
|
218
|
+
|
|
219
|
+
### Persian, dates and phones
|
|
220
|
+
|
|
221
|
+
- **Search in Persian.**
|
|
222
|
+
- `search`, the `contains`/`startsWith` filters and relation pickers treat Arabic ي/ى and Persian ی as one letter, and likewise ك and ک.
|
|
223
|
+
- Persian, Arabic-Indic and Latin digits match each other, and a zero-width non-joiner matches a space. `کتاب ۱۲` finds `كتاب 12`.
|
|
224
|
+
- This works on sql.js, Postgres and MySQL, and Latin searches keep a plain `LIKE`.
|
|
225
|
+
- **Digits.** Number, decimal, bigint, date and datetime inputs and filters accept every digit set. Values on the wire always use Latin digits.
|
|
226
|
+
- **Calendar and digits** are per browser, from the header's Display settings:
|
|
227
|
+
- calendar: Gregorian, or Jalali (the default in Persian);
|
|
228
|
+
- digits: Latin, or Persian for display.
|
|
229
|
+
- `date` values are shown without time zone shifts; datetimes are shown in the browser's time zone.
|
|
230
|
+
- **Jalali dates.**
|
|
231
|
+
- Date inputs take `1403/01/15` in any digits, next to a calendar with day, month and year views. The calendar supports the keyboard: arrows, PageUp/PageDown, and Shift for years.
|
|
232
|
+
- Date and datetime filter ranges use the same input. A Jalali datetime range covers whole local days, sent as UTC instants.
|
|
233
|
+
- **Phones.**
|
|
234
|
+
- Lists become cards with infinite scroll. With `list.mobile: { title, subtitle, badge, meta: [...] }` (fields or paths, checked at boot) cards show those; without it they show the title and the columns.
|
|
235
|
+
- Filters and sort open in a bottom drawer, and **Select** turns on checkboxes for bulk delete.
|
|
236
|
+
- `?pageSize=` in the URL sets the page size.
|
|
237
|
+
- **Command palette.**
|
|
238
|
+
- ⌘K / Ctrl+K (or the header's search button) goes to resources and groups, opens "New …" forms, and finds records.
|
|
239
|
+
- Records come from `GET /api/search?q=&limit=`: up to `limit` (default 5) per resource with `list.search`, through each resource's `findMany`, so `query()` restrictions apply. A resource whose search fails is left out and logged.
|
|
240
|
+
|
|
241
|
+
### Fields and forms
|
|
242
|
+
|
|
243
|
+
```ts
|
|
244
|
+
@AdminResource(Product)
|
|
245
|
+
export class ProductAdmin extends AdminResourceBase<Product> {
|
|
246
|
+
fields: FieldsConfig<Product> = {
|
|
247
|
+
price: { widget: 'money', currency: 'USD', readonlyIf: (p) => p.status === 'archived' },
|
|
248
|
+
status: {
|
|
249
|
+
widget: 'badge',
|
|
250
|
+
colors: { draft: 'amber', active: 'green', archived: 'gray' },
|
|
251
|
+
enumLabels: { draft: { en: 'Draft', fa: 'پیشنویس' }, active: 'Active', archived: 'Archived' },
|
|
252
|
+
},
|
|
253
|
+
slug: { widget: 'slug', slugFrom: 'name', help: { en: 'The address in the shop.', fa: 'نشانی در فروشگاه.' } },
|
|
254
|
+
releasedOn: { showIf: { status: ['active', 'archived'] } },
|
|
255
|
+
sku: { readonly: true },
|
|
256
|
+
};
|
|
257
|
+
form: FormConfig = {
|
|
258
|
+
create: CreateProductDto,
|
|
259
|
+
update: UpdateProductDto,
|
|
260
|
+
layout: [
|
|
261
|
+
{ tab: 'General', sections: [{ section: 'Details', fields: ['name', 'slug', 'status', 'releasedOn'], columns: 2 }] },
|
|
262
|
+
{ tab: 'Catalog', sections: [{ fields: ['categoryId', 'tags'] }] },
|
|
263
|
+
],
|
|
264
|
+
};
|
|
265
|
+
links(product: Product) {
|
|
266
|
+
return [{ label: { en: 'View in shop', fa: 'در فروشگاه' }, href: `https://shop.example/p/${product.slug}` }];
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
```
|
|
270
|
+
|
|
271
|
+
- **Where options come from.** Options merge in this order, later ones winning option by option:
|
|
272
|
+
1. `@AdminField({ … })` on the entity property;
|
|
273
|
+
2. `@AdminField` on the create DTO, then on the update DTO;
|
|
274
|
+
3. the resource's `fields`.
|
|
275
|
+
- **Options.**
|
|
276
|
+
- Texts: `label`, `help` (shown under the input, and linked to it for screen readers), `placeholder`, `enumLabels`. The values sent stay the raw enum values.
|
|
277
|
+
- `widget`: text, textarea, number, money, switch, checkbox, select, radio, date, datetime, json, color, badge, slug, password, email, url, file, image or richtext (the last three: [Files, images and rich text](#files-images-and-rich-text)).
|
|
278
|
+
- Without one, the widget follows the type and format.
|
|
279
|
+
- A widget that does not fit the type (`money` on a boolean) fails at boot.
|
|
280
|
+
- `colors` for badges: gray, red, amber, green, blue, purple or pink.
|
|
281
|
+
- `currency` for money: display only. Money is grouped as you leave the input, and the commas are dropped when saving.
|
|
282
|
+
- **Validation at boot.** Unknown fields, widgets and colours fail at boot with a did-you-mean hint, and so do `showIf`/`slugFrom` names and enum labels for values the column does not have.
|
|
283
|
+
- **Read-only fields.**
|
|
284
|
+
- `readonly: true` shows the field on edit forms as text with a lock and removes it from `form.update`.
|
|
285
|
+
- `readonlyIf(record)` runs on the server for every record. Records carry `_readonly` (the fields it locks), and a PATCH that sends a locked field is a 422 `{ field: ['is read-only'] }` before your `update` runs. That covers inline cell edits too.
|
|
286
|
+
- A `readonlyIf` that throws locks its field and is logged once.
|
|
287
|
+
- Create ignores it.
|
|
288
|
+
- **`showIf`** hides a field while other fields do not hold the given value (or one of the values), evaluated live in the browser. A hidden field is not sent and not checked. It is presentation, not a permission: the server keeps the stored value.
|
|
289
|
+
- **`slugFrom`** makes the slug widget fill itself from another field. It keeps the letters of every script and stops once someone types in it, or when it already holds a value.
|
|
290
|
+
- **Layout.**
|
|
291
|
+
- `form.layout` has sections (a title, and 1–3 columns on wide screens, one on phones) and tabs.
|
|
292
|
+
- Every name must be on a form and listed once. Fields left out go into a last section.
|
|
293
|
+
- A tab with a field error is marked, and a refused save switches to it.
|
|
294
|
+
- **Detail header.**
|
|
295
|
+
- The edit page title shows badge fields, the record's [actions](#actions) and `links(record)` (records carry them as `_links`).
|
|
296
|
+
- Only http(s) and relative URLs are kept. They open in a new tab with `rel="noopener noreferrer"`.
|
|
297
|
+
- **Form keys.**
|
|
298
|
+
- Leaving a form with unsaved changes asks first: an inline bar for in-app navigation, the browser's prompt for closing the tab.
|
|
299
|
+
- ⌘S / Ctrl+S saves and stays on the record.
|
|
300
|
+
- **Save & new** saves and opens an empty form.
|
|
301
|
+
- **Duplicate** opens `/:resource/new?from=<id>`, prefilled without primary keys, unique columns, read-only fields and files.
|
|
302
|
+
|
|
303
|
+
### Files, images and rich text
|
|
304
|
+
|
|
305
|
+
```ts
|
|
306
|
+
import { ADMIN_FILE_ENTITIES, AdminModule, localStorage } from '@nest-my-admin/core';
|
|
307
|
+
|
|
308
|
+
TypeOrmModule.forRoot({ /* … */ entities: [Product, Article, Invoice, ...ADMIN_FILE_ENTITIES] }),
|
|
309
|
+
AdminModule.forRoot({
|
|
310
|
+
files: { storage: localStorage({ dir: 'var/uploads' }), maxBytes: 10 * 1024 * 1024 },
|
|
311
|
+
}),
|
|
312
|
+
|
|
313
|
+
@AdminResource(Product)
|
|
314
|
+
export class ProductAdmin extends AdminResourceBase<Product> {
|
|
315
|
+
fields: FieldsConfig<Product> = {
|
|
316
|
+
image: { widget: 'image', file: { visibility: 'public', maxBytes: 2 * 1024 * 1024 } }, // a string column
|
|
317
|
+
gallery: { widget: 'image', many: true }, // a json column: keys in order
|
|
318
|
+
manual: { widget: 'file', file: { accept: ['application/pdf'] } }, // private by default
|
|
319
|
+
description: { widget: 'richtext' }, // a text column: sanitized HTML
|
|
320
|
+
};
|
|
321
|
+
}
|
|
322
|
+
```
|
|
323
|
+
|
|
324
|
+
- **Storage.**
|
|
325
|
+
- `localStorage({ dir, publicBaseUrl? })` writes under `dir`. Keep `dir` out of the repository and out of any folder your app serves.
|
|
326
|
+
- `s3Storage({ endpoint, region, bucket, accessKeyId, secretAccessKey, publicBaseUrl?, forcePathStyle? })` works with S3 and compatible stores (MinIO, R2), with no SDK.
|
|
327
|
+
- Anything else: implement `AdminStorage` (`put`, `get`, `delete`, `publicUrl`).
|
|
328
|
+
- Files are tracked in the `nma_file` table (`ADMIN_FILE_ENTITIES`; `files.dataSource` names another DataSource).
|
|
329
|
+
- **Storage keys.** The admin makes up a key at upload and never changes it:
|
|
330
|
+
|
|
331
|
+
| The file of | Upload query | `nma_file.resource` / `recordId` | Storage key |
|
|
332
|
+
| --- | --- | --- | --- |
|
|
333
|
+
| a resource's record | `resource=<resource>&field=<field>` | `<resource>` / the record's id | `<resource>/<field>/<yyyy>/<mm>/<uuid><ext>` |
|
|
334
|
+
| a settings page | `resource=page:<name>&field=<field>` | `page:<name>` / `page` | `page/<name>/<field>/<yyyy>/<mm>/<uuid><ext>` |
|
|
335
|
+
|
|
336
|
+
- `<yyyy>/<mm>` is the month of the upload in UTC, `<uuid>` a random UUID, and `<ext>` the extension of the type found in the bytes (`.png`, `.pdf`; nothing when the type has none).
|
|
337
|
+
- A page is written `page:<name>` wherever it is named as the owner, and `page/<name>/` in the key: a colon is not a file name everywhere. Example: `page/branding/logo/2026/10/ca56b927-6a9a-40c9-8967-28a6433f7b59.png`.
|
|
338
|
+
- **Fields.**
|
|
339
|
+
- The column holds the storage key (`product/image/2026/10/<uuid>.png`), or the list of keys for `many`. Records carry `_files[field]` with each file's `name`, `size`, `contentType` and `url`.
|
|
340
|
+
- `file.accept` lists types (`image/*`, `application/pdf`); the image widget defaults to `image/*`. `file.maxBytes` overrides `files.maxBytes` (default 10 MiB); both stop at 2147483647 bytes (2 GiB - 1).
|
|
341
|
+
- The form uploads as soon as a file is picked or dropped, shows progress, and waits with saving until uploads finish. Lists show thumbnails for image fields and file names for the rest. A `many` field is reordered by dragging or with its arrow buttons.
|
|
342
|
+
- Replacing or removing a file, and purging a record, delete the stored objects once the change has committed. Uploads that never reach a record are swept after 24 hours.
|
|
343
|
+
- **A delete releases a record's files only when the row is really gone**: the admin reads the row again inside the write, after your `delete()` (or a record action) returned. A record in the trash keeps its files, and so does one your `delete()` only flags (an archive column). A purge always releases them, whatever your `purge()` leaves.
|
|
344
|
+
- "Gone" means that no row is left under the key the request named. A record action that changes its record's primary key, or moves a single-table-inheritance child to another type, therefore reads as gone: the files are released while the row still names them (and the record's audit history ends there). Do not change keys or types in actions on resources with file fields.
|
|
345
|
+
- A `purge()` override that keeps the row leaves that row holding a file key with no object behind it.
|
|
346
|
+
- With `transactions: false` and TypeORM replication, that read may be answered by a replica. One that lags behind a row that was just created answers "no row", and an archive-style delete or an action on that record then releases its files. Keep transactions on with replicas.
|
|
347
|
+
- The other side of that rule: when the row goes without the admin's write seeing it (on MySQL, a service that ignores `ctx.manager` and deletes on another connection; anywhere, a delete you queue for later), the files stay as orphans, rows in `nma_file` and objects in the storage. Nothing removes them afterwards; pass `ctx.manager`.
|
|
348
|
+
- **Duplicate** copies no files: a file belongs to one record.
|
|
349
|
+
- **Your own services.** `create`/`update` overrides receive `AdminFile` objects (`key`, `name`, `contentType`, `size`, `isNew`) for file fields, already checked. Store `file.key` (the DTO property needs any class-validator decorator, such as `@IsOptional()`). The default `create`/`update` do this themselves.
|
|
350
|
+
- **Rich text.**
|
|
351
|
+
- Install the sanitizer in your app: `npm install sanitize-html` (an optional peer; the admin refuses to boot with a rich-text field and without it), or pass your own `files.sanitizeHtml`.
|
|
352
|
+
- The editor offers what the sanitizer keeps: paragraphs, headings 2–4, bold, italic, strikethrough, links, lists, quotes, tables, images, line breaks and rules. Every block takes the direction of its own text, so Persian and English paragraphs share a document.
|
|
353
|
+
- Images are uploaded through the same endpoint (when `files` is configured) and belong to the record once the saved HTML refers to them; `file: { visibility, maxBytes }` applies to them.
|
|
354
|
+
- HTML shown outside the admin (a storefront) needs images the public can load: give the field `file: { visibility: 'public' }` and the storage a `publicBaseUrl`. Otherwise each `<img>` points at the admin's own download route (`<path>/api/files/<key>`), which answers only signed-in admin users.
|
|
355
|
+
- The stored HTML holds the full URL of each image. Changing the admin `path` or the storage's `publicBaseUrl` later breaks the images of existing records (and the admin no longer recognises them as the record's files), so rewrite the stored HTML when you move either.
|
|
356
|
+
- **Duplicate** copies the text without its images, for the same reason it copies no files.
|
|
357
|
+
- The editor is a separate chunk of the UI, loaded only by forms with a rich-text field.
|
|
358
|
+
- **API.** `POST /api/uploads?resource=&field=` (`resource=page:<name>` for a settings page) takes the file itself as the request body (not multipart) with `Content-Type: application/octet-stream`, `Content-Length` and an optional percent-encoded `X-File-Name`, and answers `{ token, name, contentType, size, url? }`. Do not send the file's own type: a body parser of your app (Nest's JSON parser, for one) would read an `application/json` body before the admin does, and the upload is then refused with a 400 that says so. A write then sends `{ token }` for a new file, `{ key }` to keep a stored one, or `null`; a `many` field sends the whole list. Sending the same `{ token }` again for the same record and field (a retry after a lost answer) keeps the file.
|
|
359
|
+
|
|
360
|
+
**Security notes**
|
|
361
|
+
|
|
362
|
+
- The type of a file is read from its bytes, never from its name or the request's `Content-Type`. HTML, SVG and scripts are refused whatever they are called, and stored files get the extension of the type that was found.
|
|
363
|
+
- A text file is recognised from its first 8 KB only; what follows is not inspected. Text uploads are stored as `text/plain` with a `.txt` extension: keep it that way, and never serve them as HTML from a public bucket or your own static route (the admin's download route sends `nosniff` and `Content-Disposition: attachment` for them).
|
|
364
|
+
- Uploading needs create or update permission on the resource and edit access to the field; `text/plain`, `multipart/form-data` and `application/x-www-form-urlencoded` bodies are refused (a form on another site could send those), on top of the CSRF token.
|
|
365
|
+
- Private files (the default) are served only by `GET /api/files/<key>`, which checks, for every request, that the user may view the record (permission, scopes, `query()`) and the field, and that the record still holds the file. Every refusal is the same 404. Responses carry `X-Content-Type-Options: nosniff`, `Content-Security-Policy: sandbox`, and `Content-Disposition: attachment` for everything but images and PDFs.
|
|
366
|
+
- `visibility: 'public'` links to the storage's `publicUrl` when it has one (`publicBaseUrl`): anyone with the link can read such a file, with no permission check. Without a public URL, public files are served by the admin like private ones.
|
|
367
|
+
- `localStorage` keeps public and private files in one directory. If you serve that directory yourself for `publicBaseUrl`, private files in it are reachable by anyone who learns their key; serve only what you mean to publish, or use a bucket with its own rules.
|
|
368
|
+
- Rich text is sanitized on the server at every write, whatever the browser sent. Links keep http(s), mailto and relative addresses and get `rel="noopener noreferrer nofollow"`; an `<img>` is kept only when it points at a file of this admin that belongs to the record (or that the same user just uploaded for this field). A custom `files.sanitizeHtml` is trusted to do the same.
|
|
369
|
+
- An unsaved upload can be fetched only by the user who made it.
|
|
370
|
+
|
|
371
|
+
**Serving public files yourself**
|
|
372
|
+
|
|
373
|
+
A storefront needs the public files (product images, the shop's logo) without an admin session. With a `publicBaseUrl` the storage serves them. Without one, serve them from a route of your own and ask `AdminFiles` which keys are public; do not read `nma_file` yourself.
|
|
374
|
+
|
|
375
|
+
```ts
|
|
376
|
+
import { AdminFiles } from '@nest-my-admin/core';
|
|
377
|
+
import { Controller, Get, NotFoundException, Param, Req, Res, StreamableFile } from '@nestjs/common';
|
|
378
|
+
import type { Request, Response } from 'express';
|
|
379
|
+
|
|
380
|
+
/** Images a browser may show in place; everything else is downloaded. */
|
|
381
|
+
const RASTER = new Set(['image/jpeg', 'image/png', 'image/webp', 'image/gif', 'image/avif']);
|
|
382
|
+
|
|
383
|
+
@Controller('media')
|
|
384
|
+
export class MediaController {
|
|
385
|
+
constructor(private readonly files: AdminFiles) {} // AdminModule is global: nothing to import
|
|
386
|
+
|
|
387
|
+
@Get('*key') // Nest 11 (Express 5): the segments of the key
|
|
388
|
+
async serve(@Param('key') key: string | string[], @Req() req: Request, @Res({ passthrough: true }) res: Response): Promise<StreamableFile | undefined> {
|
|
389
|
+
const path = Array.isArray(key) ? key.join('/') : key;
|
|
390
|
+
// Express answers HEAD with the GET route: the row is enough for that, the object is not opened.
|
|
391
|
+
const file = req.method === 'HEAD' ? await this.files.publicFile(path) : await this.files.openPublic(path);
|
|
392
|
+
if (!file) throw new NotFoundException();
|
|
393
|
+
res.setHeader('Content-Type', file.contentType.startsWith('text/') ? 'text/plain; charset=utf-8' : file.contentType);
|
|
394
|
+
res.setHeader('Content-Length', String(file.size));
|
|
395
|
+
res.setHeader('Cache-Control', 'public, max-age=31536000, immutable'); // a replaced file gets a new key
|
|
396
|
+
res.setHeader('X-Content-Type-Options', 'nosniff');
|
|
397
|
+
res.setHeader('Content-Security-Policy', "default-src 'none'; sandbox");
|
|
398
|
+
if (!RASTER.has(file.contentType)) {
|
|
399
|
+
const name = encodeURIComponent(file.name).replace(/['()*]/g, (char) => `%${char.charCodeAt(0).toString(16).toUpperCase()}`);
|
|
400
|
+
res.setHeader('Content-Disposition', `attachment; filename*=UTF-8''${name}`);
|
|
401
|
+
}
|
|
402
|
+
if (!('body' in file)) return undefined;
|
|
403
|
+
const { body } = file;
|
|
404
|
+
res.on('close', () => body.destroy()); // a reader who leaves early must not keep the file or the connection open
|
|
405
|
+
return new StreamableFile(body);
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
```
|
|
409
|
+
|
|
410
|
+
- **Keep the headers.** Uploaded bytes are someone else's content served from your origin. `nosniff` and the type found at upload stop a browser from guessing another type; the `Content-Security-Policy` stops anything served here from running or loading anything if it is opened as a page; everything but a raster image is a download (`Content-Disposition: attachment`), and text is only ever `text/plain`. This is what the admin's own download route does.
|
|
411
|
+
- `publicFile(key)` answers `{ key, name, contentType, size }` or `null`, from the `nma_file` table alone (the storage is not touched): enough for a `HEAD`, or to decide before streaming.
|
|
412
|
+
- `openPublic(key)` makes the same check and then reads the storage: `{ body, contentType, size, name }`, or `null` when the key is not a public file or the object is gone from the storage. `size` is the stored object's size, or the size recorded at upload when the storage does not report one. **Consume `body` or destroy it**: an unread body holds a file descriptor or a connection to the storage.
|
|
413
|
+
- **Public** means a row with `visibility: 'public'` (the field's `file: { visibility: 'public' }`) and `status: 'attached'` (a saved record or settings page holds it). A private file, an upload nobody saved yet, a file its record let go of and a key the admin never issued are all `null`, and so is anything that is not a key (an empty string, `..` segments, a backslash, more than 500 characters): neither method throws for what a key is, so the key can come straight from the URL. No permission is checked.
|
|
414
|
+
- **Public is what was recorded at upload.** `AdminFiles` goes by the row's visibility, set from the field's `file.visibility` when the file was uploaded: a field you later make private keeps its earlier files public (new uploads are private). A record in the trash still holds its files, so its public files are still served; they stop when the record is purged or the file is removed from it.
|
|
415
|
+
- `contentType` is the type the admin found in the file's bytes at upload, never what the uploader claimed. The long `Cache-Control` is safe for the key (a replaced file gets a new one), but a file that is removed or made private stays in browser and CDN caches until it expires there; choose the lifetime with that in mind.
|
|
416
|
+
- `AdminFiles` is always injectable and follows `files.dataSource`. In an admin without `files`, both methods reject with an error that says `files` is missing (`files.enabled` tells beforehand); before the application has started they reject too.
|
|
417
|
+
- The admin's own `url` for a public file without a `publicBaseUrl` is `<path>/api/files/<key>`, which needs an admin session: build your storefront's link from the key (`/media/${key}`) instead.
|
|
418
|
+
|
|
419
|
+
### Actions
|
|
420
|
+
|
|
421
|
+
```ts
|
|
422
|
+
class ShipDto {
|
|
423
|
+
@AdminField({ label: { en: 'Tracking code', fa: 'کد رهگیری' } })
|
|
424
|
+
@IsString() @Length(4, 40) tracking: string;
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
@AdminResource(Order)
|
|
428
|
+
export class OrderAdmin extends AdminResourceBase<Order> {
|
|
429
|
+
@AdminAction({ label: { en: 'Ship', fa: 'ارسال' }, bulk: true, input: ShipDto, icon: 'truck' })
|
|
430
|
+
async ship(order: Order, input: ShipDto, ctx: AdminContext): Promise<ActionOutcome> {
|
|
431
|
+
if (order.status !== 'paid') throw new ConflictException(`Order ${order.number} is not paid yet`);
|
|
432
|
+
await this.orders.ship(order.id, input.tracking, ctx.manager);
|
|
433
|
+
return { message: { en: 'Shipped', fa: 'ارسال شد' } };
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
@AdminAction({ variant: 'destructive', confirm: { en: 'Cancel this order?', fa: 'این سفارش لغو شود؟' } })
|
|
437
|
+
async cancel(order: Order, _input: unknown, ctx: AdminContext) { await this.orders.cancel(order.id, ctx.manager); }
|
|
438
|
+
@AdminCan('cancel') onlyDrafts(order: Order) { return order.status === 'draft'; }
|
|
439
|
+
|
|
440
|
+
@AdminAction({ target: 'resource', label: 'Recount stock' })
|
|
441
|
+
async recount(_input: unknown, ctx: AdminContext) { return { message: `Counted ${await this.stock.recount(ctx.manager)} items` }; }
|
|
442
|
+
}
|
|
443
|
+
```
|
|
444
|
+
|
|
445
|
+
- **Declaring one.** `@AdminAction(options)` on a method of the resource; the method name is the action's name.
|
|
446
|
+
- `target: 'record'` (the default): the method is `(record, input, ctx)` and gets one loaded entity. The button is in the record's detail header. With `bulk: true` it is also offered for a list selection.
|
|
447
|
+
- `target: 'resource'`: the method is `(input, ctx)`. The button is on the list toolbar and runs once, without records.
|
|
448
|
+
- `label` (default: the humanized name), `icon`, `variant: 'destructive'` (a red button), `confirm: true` or a question of your own (asked in a dialog before it runs). A destructive action, and any run on more than one record, is always asked about first, with a default question when `confirm` is not set.
|
|
449
|
+
- `input`: a DTO. Its properties become a form in the dialog, checked in the browser and validated on the server like any write (whitelisted; class-validator; `@AdminField` for labels, help and widgets). A property with a class initializer is optional and keeps its default. File, image, rich-text and secret fields are not allowed in an input.
|
|
450
|
+
- Return `{ message }` (a text or `{ en, fa }`) to say what happened: the UI shows it as a toast. Returning nothing gives a default "done".
|
|
451
|
+
- Names that collide with `create`, `update`, `delete`, `restore`, `purge` or a method of `AdminResourceBase`, and options that do not fit together (`bulk` on a resource action, `atomic` without `bulk`), fail at boot.
|
|
452
|
+
- **Permissions.**
|
|
453
|
+
- Each action has the code `{r}.action.{name}`. `{r}.*` and `*` cover it, `{r}.action.*` covers every action of the resource, and `*.update` or `{r}.update` do not.
|
|
454
|
+
- A record action also needs `{r}.update`, and the record must be inside the user's view and update scopes: an action never runs on a record the user could not change. A resource action needs only its own code.
|
|
455
|
+
- **A resource action is not row-scoped.** It is handed no records, so neither `query()` nor the user's row scopes have been applied to anything: the method decides which rows it touches. Start from your restrictions yourself, `this.query(this.repositoryFor(ctx).createQueryBuilder('entity'), ctx).andWhere(…)`, and add the condition of an `@AdminScope` method by calling it, or the action changes rows the user cannot see.
|
|
456
|
+
- The code holds the method's name: renaming an action method (or removing a settings page) leaves stored roles naming the old code. Such a role no longer validates and grants nothing at all until it is fixed on the Roles page.
|
|
457
|
+
- `@AdminCan('<action name>')` is a rule per record, as for update and delete: where it says no, the button is not there (`_perm.actions` lists the allowed ones) and the API answers 403. Rules apply to superusers too. `@AdminCan('update')` does not gate actions, so a record closed to edits can still be cancellable.
|
|
458
|
+
- The schema lists only the actions the user may run. While viewing as someone, none can be run.
|
|
459
|
+
- **Selections.** A bulk action runs on up to 100 records.
|
|
460
|
+
- By default each record gets its own transaction: the answer is `{ ok, failed: [{ id, code, message }], message? }`, and one failure does not stop the others. The list shows how many ran and why the others did not.
|
|
461
|
+
- `atomic: true` runs them all in one transaction: every record is checked first, and one failure is the answer, with nothing applied. It needs the admin's transactions (boot fails with `transactions: false`). The confirmation says "all or nothing".
|
|
462
|
+
- **Refusing.** Throw from the method:
|
|
463
|
+
- an `HttpException` below 500 answers with its status and message (409 is `CONFLICT`, 400 is `BUSINESS_RULE`), and the UI shows the message;
|
|
464
|
+
- a 401 from host code is answered as 400, because 401 means signed out to the admin: an action, a settings page's `load`/`save`, or a resource's `create`/`update`/`delete` (and their hooks) that rethrows a provider's `UnauthorizedException` gets a 400 `BUSINESS_RULE` with its message (an `errorMapper` sees that 400, with the original as its `cause`);
|
|
465
|
+
- `AdminFieldError({ tracking: 'is unknown to the courier' })` is a 422 shown on that input;
|
|
466
|
+
- anything else goes through `errorMapper` and the usual error mapping (a 500 with a correlation id when nothing maps it).
|
|
467
|
+
- **Transactions.** An action runs inside the admin's write transaction: pass `ctx.manager` to your services, as in `create` and `update`.
|
|
468
|
+
- **Files.** An action that hard-deletes a record, or changes a file column itself, does not release the files: the objects and their `nma_file` rows stay. Only the admin's own update, delete and purge do that for now.
|
|
469
|
+
- **API.** `POST /api/resources/:resource/actions/:action` with `{ ids?, input? }`. One distinct id answers `{ message?, record }` (`record` is `null` when the action took it out of the user's view), several answer the bulk result, and a resource action answers `{ message? }`.
|
|
470
|
+
|
|
471
|
+
### Settings pages
|
|
472
|
+
|
|
473
|
+
```ts
|
|
474
|
+
export class ShopSettingsDto {
|
|
475
|
+
@IsString() @Length(2, 60) shopName: string;
|
|
476
|
+
@IsOptional() @IsInt() @Min(0) lowStockThreshold?: number | null;
|
|
477
|
+
@IsOptional() logo?: AdminFile | string | null;
|
|
478
|
+
@IsOptional() @IsString() @Length(12, 80) paymentApiKey?: string | null;
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
@AdminSettingsPage('shop', {
|
|
482
|
+
label: { en: 'Shop settings', fa: 'تنظیمات فروشگاه' },
|
|
483
|
+
icon: 'store',
|
|
484
|
+
form: ShopSettingsDto,
|
|
485
|
+
fields: { logo: { widget: 'image' }, paymentApiKey: { secret: true } },
|
|
486
|
+
layout: [{ section: 'Shop', fields: ['shopName', 'logo'], columns: 2 }, { section: 'Payments', fields: ['paymentApiKey'] }],
|
|
487
|
+
})
|
|
488
|
+
export class ShopSettingsPage implements AdminSettingsSource<ShopSettingsDto> {
|
|
489
|
+
load(ctx: AdminContext) { return this.settings.all(ctx.manager); }
|
|
490
|
+
save(changes: Partial<ShopSettingsDto>, ctx: AdminContext) { return this.settings.write(changes, ctx.manager); }
|
|
491
|
+
}
|
|
492
|
+
```
|
|
493
|
+
|
|
494
|
+
- **What it is.** One form that is not a table: a provider with `load` and `save`, shown in the sidebar under its module's group (or `group: '<key>'`) at `/p/<name>`. A group may hold only pages; its label, icon and order come from the module whose `@AdminGroup` has its key, and `order` places a page among the pages of its group (see [Sidebar groups and order](#sidebar-groups-and-order)). List the class in a module's `providers`.
|
|
495
|
+
- **The form** comes from the DTO alone: one field per property with a class-validator decorator. `fields` and `@AdminField` set labels, help, widgets and `showIf`; `layout` works as on resources. `readonly`, `readonlyIf` and `restricted` do not exist on pages.
|
|
496
|
+
- **`load(ctx)`** returns the stored values. Keys that are not form fields are dropped.
|
|
497
|
+
- **`save(changes, ctx)`** gets only the keys that were sent: a save is partial and validated like a PATCH. The UI sends what changed; a group of fields (a nested DTO) is sent whole once one of its fields changed, so `save` can store it under its key.
|
|
498
|
+
- It runs in a transaction on the default DataSource, and `load` is called inside it before and after. Use `ctx.manager` when it is set: a `save` that ignores it writes outside the transaction.
|
|
499
|
+
- Throw `AdminFieldError` (422 on a field) or an `HttpException` to refuse (a 401 is answered as 400, as for actions).
|
|
500
|
+
- **Secrets.** `secret: true` (text fields of the page only) makes a field write-only.
|
|
501
|
+
- The value is never served: not by GET, not in the answer of a save. The API reports `secrets: { paymentApiKey: true }` when one is stored, and the form shows **Set ✓ · Replace · Clear**.
|
|
502
|
+
- A save replaces it with any non-empty text, clears it with `null` (even when the DTO requires the property), and leaves it alone when the key is absent. An empty string is refused with 422: empty never means "clear".
|
|
503
|
+
- `load` returns the secret like any other value (the admin needs to know whether one is set), and `save` gets the new text or `null`. Encrypt it in `save` if the table is not a safe place for it.
|
|
504
|
+
- Never put a secret into an error message. As a last line of defence the admin replaces the exact values of secrets of 6 or more characters with `***` in errors leaving a save (messages, field errors, the log). That is best effort: shorter and transformed values are not found, and errors thrown by `load` outside a save are logged as thrown.
|
|
505
|
+
- **Files.** File, image and rich-text fields work as on records, with `resource=page:<name>&field=<field>` for uploads; the stored key is `page/<name>/<field>/<yyyy>/<mm>/<uuid><ext>` (see Storage keys under [Files](#files-images-and-rich-text)). `save` gets an `AdminFile` (store its `key`) or `null`, and `load` returns the key, and `null` once the file was cleared: leaving the key out means "unknown", and the removed file is then kept. List fields (`many`) are not supported on pages.
|
|
506
|
+
- **Permissions.** `page.<name>` views the page and `page.<name>.update` saves it; whoever may save may view.
|
|
507
|
+
- `*` and `page.*` grant both codes of every page, and `page.*.update` saves every page. Resource wildcards (`*.view`, `*.update`, `product.*`) never reach a page.
|
|
508
|
+
- Without view, the page is a 404 and absent from the sidebar. With view only, the form is read-only. Viewing as someone is always read-only.
|
|
509
|
+
- **Names.** Letters, digits, `-` and `_`; not the name of a resource, an operation (`view`, `update`, …) or a resource's custom permission. Because page codes start with `page.`, **a resource cannot be named `page` in an admin with settings pages**: boot fails and suggests `@AdminResource(Page, { name: 'content-page' })`.
|
|
510
|
+
- **API.** `GET /api/pages/:name` answers `{ schema, values, secrets, files }`, and `PATCH /api/pages/:name` takes the changed keys and answers the same.
|
|
511
|
+
|
|
512
|
+
## Transactions, context and errors
|
|
513
|
+
|
|
514
|
+
Every create, update and delete runs in one database transaction. Pass `ctx.manager` to your services so their writes
|
|
515
|
+
join it — a hook or service that throws afterwards rolls everything back:
|
|
516
|
+
|
|
517
|
+
```ts
|
|
518
|
+
create(dto: CreateProductDto, ctx: AdminContext) {
|
|
519
|
+
return this.products.create(dto, ctx.manager); // service: manager ? manager.getRepository(Product) : this.products
|
|
520
|
+
}
|
|
521
|
+
```
|
|
522
|
+
|
|
523
|
+
Services that keep using their own injected repository still work, but on Postgres/MySQL they write outside the
|
|
524
|
+
transaction, on a separate pooled connection, and can block on the admin transaction's locks (writing the same row or
|
|
525
|
+
the same unique key blocks until the admin transaction ends on both drivers; inserting a row with a foreign key to the
|
|
526
|
+
just-created record blocks on MySQL and fails at once with a foreign-key conflict, 409 `CONFLICT`, on Postgres). Pass `ctx.manager`. `AdminContext.current()` returns the admin
|
|
527
|
+
request being handled (or `undefined` in your own controllers), so deep code can find it without a `ctx` parameter.
|
|
528
|
+
Turn transactions off with `forRoot({ transactions: false })`.
|
|
529
|
+
|
|
530
|
+
On SQLite-family drivers admin writes are serialized per DataSource (one connection); admin reads and other code on that
|
|
531
|
+
connection can see an open write's uncommitted rows. On Postgres, serialization or deadlock failures at COMMIT
|
|
532
|
+
(40001/40P01) surface as a 500 `INTERNAL` with no retry.
|
|
533
|
+
|
|
534
|
+
Translate your own exceptions with `forRoot({ errorMapper: (e) => e instanceof OutOfStock ? new AdminFieldError({ stock: 'out of stock' }) : undefined })`.
|
|
535
|
+
The mapper runs after the request's context has ended (`AdminContext.current()` is `undefined` inside it) and before the
|
|
536
|
+
built-in database-error mapping, so never put raw error messages (they may contain SQL) into a mapped response. Errors
|
|
537
|
+
from host middleware that carry a 4xx `status`, and body-parser errors, skip it. A mapped error with status 500 or more
|
|
538
|
+
is logged with the original stack and the correlation id.
|
|
539
|
+
|
|
540
|
+
Forms check your DTO rules (`@Length`, `@Min`/`@Max`, `@IsInt`, `@Matches`, `@IsEmail`, `@IsUrl`, `@IsUUID`, `@IsIn`) in the
|
|
541
|
+
browser before saving; the server still validates everything. PATCH bodies are always validated as partial (only the
|
|
542
|
+
sent fields), including dedicated update DTOs. A DTO property with a class initializer (`status = 'draft'`) is not
|
|
543
|
+
required, and a `@ValidateIf` property gets no browser rules at all. Column length (`varchar(n)`) is checked in the
|
|
544
|
+
browser even on SQLite, which does not enforce it.
|
|
545
|
+
|
|
546
|
+
## Sign-in
|
|
547
|
+
|
|
548
|
+
```ts
|
|
549
|
+
import { ADMIN_AUTH_ENTITIES, builtinAuth } from '@nest-my-admin/auth';
|
|
550
|
+
|
|
551
|
+
TypeOrmModule.forRoot({ /* … */ entities: [/* yours */, ...ADMIN_AUTH_ENTITIES] }),
|
|
552
|
+
AdminModule.forRoot({
|
|
553
|
+
auth: builtinAuth({ bootstrapSuperuser: { username: 'admin', password: process.env.ADMIN_PASSWORD! } }),
|
|
554
|
+
}),
|
|
555
|
+
```
|
|
556
|
+
|
|
557
|
+
- **`@nest-my-admin/auth`** (built in).
|
|
558
|
+
- Admin users live in `nma_user`, with scrypt password hashes (`node:crypto`, no native module).
|
|
559
|
+
- Sessions live in `nma_session`, stored as the SHA-256 of an opaque token. The cookie `nma_session` is HttpOnly and SameSite=Lax, has Path set to the admin path, and is Secure on https (Express's `trust proxy` decides behind a proxy).
|
|
560
|
+
- A session ends after 12 hours without a request or 30 days after sign-in.
|
|
561
|
+
- Five wrong passwords lock an account for 15 minutes. Sign-in attempts are limited to 20 per minute per IP, with 429 and `Retry-After`.
|
|
562
|
+
- Unknown users, wrong passwords and locked accounts get the same answer.
|
|
563
|
+
- The account page changes the password (which signs out every other session), lists signed-in devices, and signs out one or all others.
|
|
564
|
+
- **Two-factor sign-in** (TOTP, any authenticator app). A user turns it on from the account page by scanning a QR code and confirming a code, and gets 10 one-time recovery codes. After the password, sign-in answers 401 `TWO_FACTOR_REQUIRED` until the request also carries `otp` (a code or a recovery code). Each code works once, wrong codes count toward the lockout, and turning it on signs out every other session. Turning it off or issuing new recovery codes needs the password; an admin can reset a lost second factor from the Users page. Set `secretKey` (32 bytes, base64: `openssl rand -base64 32`) to encrypt the keys at rest with AES-256-GCM. `issuer` names the app in authenticators. Upgrading from 0.0.x adds four nullable columns to `nma_user` (`totpSecret`, `totpPending`, `totpLastStep`, `recoveryCodes`).
|
|
565
|
+
- `bootstrapSuperuser` creates the first user when `nma_user` is empty (it runs in module init, so call `app.init()` before seeding other users). `createAdminUser(dataSource, { … })` adds more.
|
|
566
|
+
- **CSRF.** Every POST, PATCH and DELETE of a cookie session must send the session's token as `X-CSRF-Token` (the UI does). The API also accepts only `application/json` bodies.
|
|
567
|
+
- **Your own users:**
|
|
568
|
+
- `auth: AdminAuth.custom(MyAdapter)` takes a provider of your app implementing `AdminAuthAdapter`.
|
|
569
|
+
- `authenticate(req)` is required; `login`, `logout`, `listSessions`, `revokeSession`, `revokeOtherSessions`, `changePassword` and the two-factor methods (`twoFactorStatus`, `beginTwoFactor`, `confirmTwoFactor`, `disableTwoFactor`, optionally `newRecoveryCodes` and `resetTwoFactor`) are optional, and the UI shows only what exists.
|
|
570
|
+
- Return a `csrfToken` from `authenticate` when you use cookies.
|
|
571
|
+
- `ctx.user` and `AdminContext.current().user` carry the result into resource methods and your services.
|
|
572
|
+
- **No sign-in.** `AdminAuth.none()` makes everyone a superuser, for local tools. Without `auth` the admin behaves the same with a warning at boot, and **refuses to boot under `NODE_ENV=production`**.
|
|
573
|
+
- Every signed-in user who is not a superuser gets only what their roles grant (see Permissions).
|
|
574
|
+
|
|
575
|
+
## Permissions
|
|
576
|
+
|
|
577
|
+
```ts
|
|
578
|
+
AdminModule.forRoot({
|
|
579
|
+
roles: [{
|
|
580
|
+
name: 'catalog-editor',
|
|
581
|
+
permissions: ['product.view', 'product.update', 'category.*', 'reports.run'],
|
|
582
|
+
fields: { product: { price: 'readonly', cost: 'hidden' } },
|
|
583
|
+
scopes: { product: { update: 'drafts' } }, // view / update / delete; none = every row
|
|
584
|
+
}],
|
|
585
|
+
resolveRoles: (user) => (user.username === 'editor' ? ['catalog-editor'] : []),
|
|
586
|
+
permissions: ['reports.run'], // global custom codes
|
|
587
|
+
globalScopes: [{ name: 'tenant', appliesTo: ({ metadata }) => !!metadata.findColumnWithPropertyName('tenantId'),
|
|
588
|
+
where: (ctx) => ({ tenantId: ctx.user?.attrs?.tenantId }) }],
|
|
589
|
+
});
|
|
590
|
+
|
|
591
|
+
@AdminResource(Order, { permissions: ['view_all'] }) // declares order.view_all
|
|
592
|
+
export class OrderAdmin extends AdminResourceBase<Order> {
|
|
593
|
+
fields: FieldsConfig<Order> = { margin: { restricted: true } }; // hidden unless a role grants order.field.margin.view|edit
|
|
594
|
+
@AdminScope('own') own(ctx: AdminContext) { return { ownerId: ctx.user?.id }; } // or (where, alias) => where.where(...)
|
|
595
|
+
@AdminCan('delete') onlyDrafts(order: Order) { return order.status === 'draft'; }
|
|
596
|
+
}
|
|
597
|
+
```
|
|
598
|
+
|
|
599
|
+
- **Codes:**
|
|
600
|
+
- operations: `{r}.view`, `{r}.create`, `{r}.update`, `{r}.delete`, `{r}.purge`;
|
|
601
|
+
- fields: `{r}.field.{f}.view` and `{r}.field.{f}.edit`;
|
|
602
|
+
- actions: `{r}.action.{name}` ([Actions](#actions));
|
|
603
|
+
- settings pages: `page.{name}` and `page.{name}.update` ([Settings pages](#settings-pages));
|
|
604
|
+
- custom codes, per resource or global;
|
|
605
|
+
- `audit.view`, with the [audit log](#audit-log) on.
|
|
606
|
+
- Wildcards: `*`, `{r}.*`, `*.view`. Page codes are granted only by `*` and by patterns that start with `page`.
|
|
607
|
+
- `update` implies `view`.
|
|
608
|
+
- Anything unknown in a role (code, resource, field, scope) fails at boot with a suggestion.
|
|
609
|
+
- **Roles combine grant-only, the most permissive winning.**
|
|
610
|
+
- Per field: `hidden < view < edit`. A field without a rule is editable for a role that may create or update.
|
|
611
|
+
- Per operation, scopes are ORed, and a role without a scope means every row.
|
|
612
|
+
- Superusers bypass everything but the global scopes that do not set `exemptSuperusers`.
|
|
613
|
+
- Roles come from `resolveRoles` and the auth adapter's `resolveRoles`. A lookup that throws grants nothing.
|
|
614
|
+
- **Enforced on the server, at every endpoint:**
|
|
615
|
+
- Meta lists only what you may view, and every other resource is 404.
|
|
616
|
+
- Schemas are cut: hidden fields disappear, read-only ones move to `form.readonly`, and `permissions` says what you may do.
|
|
617
|
+
- Filter, sort and search accept only visible fields (anti-oracle).
|
|
618
|
+
- Records carry only visible fields. Relation titles of resources or rows you may not see become `#id`, and paths through them are null.
|
|
619
|
+
- A write naming hidden or read-only fields is 403 `FORBIDDEN_FIELDS` with their names. A missing operation is 403 `FORBIDDEN`.
|
|
620
|
+
- Records outside your scope are 404.
|
|
621
|
+
- `_perm: { update, delete, actions? }` on each record combines permission, scope and `@AdminCan`, and PATCH, DELETE and actions re-check it.
|
|
622
|
+
- Scopes also restrict counts, global search, relation pickers and the ids a write may link to.
|
|
623
|
+
- A resource that overrides `findMany`/`findOne` still cannot leak rows: the API re-checks with its own scoped query.
|
|
624
|
+
- **In your code:** `ctx.can('order.view_all')` and `ctx.permissions`.
|
|
625
|
+
|
|
626
|
+
### Roles, groups and users in the admin
|
|
627
|
+
|
|
628
|
+
```ts
|
|
629
|
+
import { ADMIN_RBAC_ENTITIES } from '@nest-my-admin/core';
|
|
630
|
+
|
|
631
|
+
TypeOrmModule.forRoot({ /* … */ entities: [/* yours */, ...ADMIN_AUTH_ENTITIES, ...ADMIN_RBAC_ENTITIES] }),
|
|
632
|
+
AdminModule.forRoot({ rbac: {}, roles: [/* system roles */], auth: builtinAuth({ /* … */ }) }),
|
|
633
|
+
```
|
|
634
|
+
|
|
635
|
+
- **Tables.** Roles, groups and memberships live in `nma_role`, `nma_group`, `nma_group_role`, `nma_group_member` and `nma_user_role`. The Administration section shows Users, Groups and Roles to `rbac.view`, and `rbac.manage` may change them.
|
|
636
|
+
- **Roles from code** are stored as system roles at every boot: read-only in the admin, and code always wins. A user's roles are the union of their direct roles, their groups' roles and `resolveRoles`, and changes apply on the next request.
|
|
637
|
+
- **The Roles page** is a matrix of resources × view/create/update/delete/purge. Each row expands to per-field access, row scopes per operation, the resource's actions and its custom codes. Settings pages have their own table (view, change), and global codes follow. Roles export and import as JSON; an import is all or nothing and validated like the editor.
|
|
638
|
+
- **Anti-escalation.** A manager who is not a superuser may only create, change, assign or remove roles within their own permissions (codes, field levels, scopes). They cannot change superusers, make anyone a superuser, or give themselves roles or groups.
|
|
639
|
+
- **Users** come from the auth adapter. `@nest-my-admin/auth` lists, creates and edits users and sets passwords; deactivating a user or setting their password signs them out everywhere. Other adapters may implement `listUsers`, `getUser`, `createUser`, `updateUser` and `setPassword`. Without them, the page shows the users that have roles or groups.
|
|
640
|
+
|
|
641
|
+
### Debugging permissions and viewing as a user
|
|
642
|
+
|
|
643
|
+
- **Permission debugger** (Administration → Permission debugger, for `rbac.view`; `GET /api/rbac/explain?user=&resource=[&record=]`). For one user and resource it shows:
|
|
644
|
+
- each role and where it comes from (directly, a group, `resolveRoles`, the adapter), and names that are not roles;
|
|
645
|
+
- each operation, allowed or not, and the patterns that grant it;
|
|
646
|
+
- each action, and whether a granted one is held back because the user lacks `update`;
|
|
647
|
+
- every settings page: view, change, and the patterns behind them;
|
|
648
|
+
- each field's level, with every role's level and the rule or code behind it;
|
|
649
|
+
- the row scopes per operation, per role, and the global scopes;
|
|
650
|
+
- for a record id: whether the user may view, change and delete it, and why not (no permission, outside their rows, an `@AdminCan` rule, no such record), and the actions they may run on it.
|
|
651
|
+
|
|
652
|
+
The answers come from the code the API enforces with, and a test checks them against the API for random role combinations.
|
|
653
|
+
- **View as.** A superuser opens a user and chooses *View as this user*. The admin then shows exactly what that user sees (the UI sends `X-View-As: <id>`), under a banner with *Stop viewing*. It is read-only: the server refuses every write (actions and settings pages included, so the UI offers neither), and the account pages are closed. Each start is recorded in the [audit log](#audit-log) (`auth.view_as`, at most once an hour per pair); without the audit log the server logs it with both ids. History and the audit page work while viewing as someone, with that user's permissions.
|
|
654
|
+
|
|
655
|
+
### Testing permissions
|
|
656
|
+
|
|
657
|
+
```ts
|
|
658
|
+
import { createAdminTestingModule } from '@nest-my-admin/testing';
|
|
659
|
+
|
|
660
|
+
const admin = await createAdminTestingModule({ imports: [AppModule] });
|
|
661
|
+
const operator = admin.as({ id: 'u7', roles: ['operator'] }); // or no roles: resolveRoles decides
|
|
662
|
+
await operator.list('order', { filter: { status: { eq: 'open' } } });
|
|
663
|
+
await expect(operator.update('order', 1, { total: '0' })).rejects.toMatchObject({ code: 'FORBIDDEN_FIELDS' });
|
|
664
|
+
await admin.expectNoLeaks('employee', { as: { id: 'u7', roles: ['operator'] } });
|
|
665
|
+
```
|
|
666
|
+
|
|
667
|
+
- **`as(user)`** calls the API in-process as that user (meta, schema, list, get, create, update, delete, restore, purge, search, options), through the same checks as HTTP requests but without signing in.
|
|
668
|
+
- `runAction(resource, action, { ids?, input? })` runs an `@AdminAction`: one id answers `{ message?, record }`, several `{ ok, failed }`, and a resource action takes no ids.
|
|
669
|
+
- `page(name)` reads a settings page (`{ schema, values, secrets, files }`) and `savePage(name, changes)` saves the keys of `changes`.
|
|
670
|
+
|
|
671
|
+
```ts
|
|
672
|
+
await operator.runAction('order', 'ship', { ids: [1], input: { tracking: 'TRK-1' } });
|
|
673
|
+
await expect(operator.runAction('order', 'cancel', { ids: [1] })).rejects.toMatchObject({ code: 'FORBIDDEN' });
|
|
674
|
+
await expect(operator.savePage('shop', { shopName: 'Mine' })).rejects.toMatchObject({ code: 'FORBIDDEN' });
|
|
675
|
+
```
|
|
676
|
+
- **`expectNoLeaks(resource, { as })`** reads everything as a superuser, then crawls everything the user can reach:
|
|
677
|
+
- meta and the schema;
|
|
678
|
+
- every list page and each visible record;
|
|
679
|
+
- each out-of-scope record, which must be 404;
|
|
680
|
+
- global search for each hidden value;
|
|
681
|
+
- the other resources' titles, relation columns and relation pickers.
|
|
682
|
+
|
|
683
|
+
It fails with `AdminLeakError` and a report of each hidden value or out-of-scope record it found (a title built from a hidden field, a `findMany` override that ignores scopes and the search term, …).
|
|
684
|
+
- **`adminTesting(app)`** gives the same helpers for an app you already created.
|
|
685
|
+
|
|
686
|
+
Host `APP_GUARD`s do not protect the admin, by design (it is mounted on the HTTP adapter, not as Nest controllers); middleware registered in `main.ts` before `listen` still runs. Do not enable wildcard CORS for it. Catch-all routes the host registered earlier take precedence over the admin.
|
|
687
|
+
|
|
688
|
+
## Audit log
|
|
689
|
+
|
|
690
|
+
```ts
|
|
691
|
+
import { ADMIN_AUDIT_ENTITIES } from '@nest-my-admin/core';
|
|
692
|
+
|
|
693
|
+
TypeOrmModule.forRoot({ /* … */ entities: [/* yours */, ...ADMIN_AUDIT_ENTITIES] }),
|
|
694
|
+
AdminModule.forRoot({ audit: { retentionDays: 365 } }), // `audit: {}` keeps entries for ever
|
|
695
|
+
|
|
696
|
+
@AdminResource(Employee)
|
|
697
|
+
export class EmployeeAdmin extends AdminResourceBase<Employee> {
|
|
698
|
+
fields: FieldsConfig<Employee> = { salary: { sensitive: true } }; // recorded as "changed", without values
|
|
699
|
+
}
|
|
700
|
+
```
|
|
701
|
+
|
|
702
|
+
- **Enabling it.** `audit: {}` and `ADMIN_AUDIT_ENTITIES` (one table, `nma_audit`) in the entities of the default DataSource, or of the one named by `audit.dataSource`. Without `audit` nothing is recorded, records have no History tab, and the two routes answer 404. Boot fails when the table's entity is missing.
|
|
703
|
+
- **What is recorded.** Every entry has the time, the actor (id and display name at the time; none for a failed sign-in), the event, the request's correlation id, and for a record its resource, id and title.
|
|
704
|
+
|
|
705
|
+
| event | what it holds |
|
|
706
|
+
|---|---|
|
|
707
|
+
| `{r}.created`, `{r}.updated`, `{r}.deleted`, `{r}.restored`, `{r}.purged` | the changed fields: after only for a create, before only for a delete or purge, both for an update, none for a restore |
|
|
708
|
+
| `{r}.action.{name}` | the fields the action changed on the record (none for a resource action), and its input |
|
|
709
|
+
| `page.{name}.updated` | the changed fields of a settings page |
|
|
710
|
+
| `rbac.role.changed`, `rbac.group.changed`, `rbac.user_roles.changed` | permissions, field rules and scopes of a role; roles and members of a group; a user's roles and groups. A deleted role also names the users and groups that lost it |
|
|
711
|
+
| `rbac.user.created`, `rbac.user.changed`, `rbac.user.password_reset`, `rbac.user.two_factor_reset` | username, display name, email, active and superuser flags; never a password |
|
|
712
|
+
| `auth.login`, `auth.logout`, `auth.login_failed`, `auth.view_as` | address and browser; for a failure the username that was typed and why (`UNAUTHENTICATED`, `RATE_LIMITED`, `WRONG_CODE`, `SUPPRESSED`) |
|
|
713
|
+
| `auth.password_changed`, `auth.two_factor_enabled`, `auth.two_factor_disabled`, `auth.recovery_codes_regenerated`, `auth.sessions_revoked` | the user's own account changes |
|
|
714
|
+
|
|
715
|
+
- The diff is computed by the admin from the stored row before and after your method ran, so it also covers resources that override `create`/`update`/`delete`, and fields your service changed that the form did not send (a computed total).
|
|
716
|
+
- An update or a page save that changes nothing writes no entry. Bulk runs write one entry per record. The update-date, version and discriminator columns are never diffed.
|
|
717
|
+
- Related records are stored as ids (a many-to-many as its sorted ids), files as their storage keys, decimals and bigints as strings.
|
|
718
|
+
- A text over 4,000 characters is cut, a larger value of another kind is left out, and an action's input over 8 kB is left out; the entry says so (`truncated`, `inputTruncated`).
|
|
719
|
+
- **How writes are observed.** `AdminApiService` runs every write through its *write participants* (`before` → your method → `after`, then `onCommit` or `onRollback`). File handling is one participant and the audit log the other; nothing in your resource has to call it.
|
|
720
|
+
- **An entry exists if and only if its change committed.** The entry is inserted with `ctx.manager`, inside the write's transaction: a rollback discards it, and if the entry cannot be written the write fails with it (500). Two setups are weaker:
|
|
721
|
+
- **the audit table on another DataSource** (`audit.dataSource`): the entry is written right after the commit, on its own connection. A crash or a failed insert in between loses it; the failure is logged with the correlation id.
|
|
722
|
+
- **`transactions: false`**: the entry is written after your method returns. A method that throws after it changed something leaves the change without an entry.
|
|
723
|
+
- **Sensitive fields.** `sensitive: true` on a field (of a resource, a settings page, or an action's input DTO) records only that it changed. `widget: 'password'` fields and a page's `secret` fields are sensitive by themselves. The option is opt-in: a `passwordHash` or token column is stored in clear unless you mark it.
|
|
724
|
+
- **`sensitive`, `secret` and `widget: 'password'` are refused at boot on a property inside a group** (a nested DTO, an embedded entity), where they would not be applied: move the field to the top level, or mark the group itself.
|
|
725
|
+
- When the title column is sensitive (or the title is a function and the resource has a sensitive field), no title is stored.
|
|
726
|
+
- **What is never stored:** passwords, one-time codes, recovery codes, two-factor keys, session tokens, the values of sensitive and secret fields (not even hashed), and sensitive keys of an action's input.
|
|
727
|
+
- **Failed sign-ins** are written without making the sign-in wait, and are capped by three caps a minute, so that guessing cannot fill the table:
|
|
728
|
+
- per address and username: the first 5 are recorded, and a 6th marked as the last (`limited`);
|
|
729
|
+
- per address across usernames: the first 50, and a 51st marked `limited`. An IPv6 address counts as its /64 (one client usually has a whole /64 to pick addresses from), an IPv4-mapped one (`::ffff:203.0.113.7`) as the IPv4 address;
|
|
730
|
+
- for the whole process, whatever the addresses: 300 recorded failures.
|
|
731
|
+
|
|
732
|
+
Of a lockout's answers only the first is recorded. When a minute closes, `SUPPRESSED` entries carry the counts of what was left out: one per address (and username), at most 100 of those a minute, and at most one a minute without an address, for the process-wide cap and for every address beyond the 100. **So failed sign-ins write at most 401 rows a minute per process** (300 failures, 100 named summaries, 1 unnamed one), whoever is guessing and from however many addresses. Counts that do not fit a minute are carried to the next, not dropped; only what is still uncounted when the process stops is lost. Successful sign-ins are never capped. The counters live in memory, per process (four processes write up to four times as much).
|
|
733
|
+
- **Whatever was typed into the username box is stored**, cut to 200 characters. A password typed there by mistake ends up in the log.
|
|
734
|
+
- **Reading.**
|
|
735
|
+
- `GET /api/resources/:r/:id/history` (the History tab on a record) is for whoever may open the record: the same 403 and 404 as GET, a record in the trash included. It needs no extra permission.
|
|
736
|
+
- `GET /api/audit?actor=&event=&resource=&record=&from=&to=` (user menu → Audit log) needs the global code **`audit.view`**, which exists only with `audit`. `event` is a whole name or a family ending in `.*` (`order.*`, `auth.*`); `record` needs `resource`, and alone matches only entries without a resource (a role's name, a user's id); `from`/`to` are days (UTC) or instants.
|
|
737
|
+
- Both answer 50 entries a page, newest first, with `next` to send as `before`.
|
|
738
|
+
- **Who sees what.** Entries are cut for their reader, and reading the log never shows more than the record itself would:
|
|
739
|
+
- a field the reader may not view (a field rule, `restricted` without the grant) is served as `{ field, hidden: true }`; a sensitive field as `{ field, changed: true }` for everyone;
|
|
740
|
+
- an action's input only to holders of that action's code; the address and browser only to holders of `audit.view`;
|
|
741
|
+
- a record outside the reader's row scopes or the resource's `query()` is listed without its id, title and changes (`restricted: true`). A resource they cannot reach is listed as `*.updated`, without even its name, and a settings page they cannot view as `page.*.updated`;
|
|
742
|
+
- role, group and user changes show their changes (permissions, members, flags) only with `rbac.view`. What every holder of `audit.view` still sees of `rbac.*` and `auth.*` entries: that the entry exists, its actor, and its subject, which is the role's name, the group's name, or the user's id and display name;
|
|
743
|
+
- filters never match by something the reader is not shown.
|
|
744
|
+
- **Records that are gone.** A record's history is the history of the record that holds the key now: entries from before a purge, a hard delete or an action that deleted it are left out, so a reused key does not open its predecessor's history. On the audit page those entries are shown in full to a superuser and to a reader whom nothing restricts in that resource (no scope, no global scope, no `query()` or `findOne()` override), and closed to everyone else, because their scopes can no longer be checked against a row that does not exist.
|
|
745
|
+
- **If you override `findOne`** to hide records, the audit page honours it: each record is also asked of your `findOne(id, ctx, { withDeleted: true })`, and one it does not return stays closed. Keep `withDeleted` working there, or records in the trash are closed on the audit page too.
|
|
746
|
+
- **Retention.** With `retentionDays`, entries older than that are deleted a minute after boot and then once a day, 1,000 at a time. Without it nothing is ever deleted.
|
|
747
|
+
- **Limits.**
|
|
748
|
+
- Changes made outside the admin (your own controllers, jobs, SQL) are not recorded. That includes deletions: a row deleted elsewhere leaves no closing entry, and if its key is given again, the earlier entries read as the new record's.
|
|
749
|
+
- **MySQL and services that ignore `ctx.manager`:** such a write happens on another connection, which the admin's transaction (REPEATABLE READ) does not see. The admin then diffs against the entity your method returned, which covers columns but not relations: a relation changed that way is not recorded. Pass `ctx.manager`.
|
|
750
|
+
- User and account entries (`rbac.user.*`, `auth.*`) are written after the auth adapter's call, not in a transaction with it.
|
|
751
|
+
- The address is Express's `req.ip`. Behind a proxy set `trust proxy` to the proxies you run, never to `true` on an open network: a forwarded header anyone can send would then be recorded as the address.
|
|
752
|
+
- `select: false` columns are not read for the diff: a change to one is not recorded.
|
|
753
|
+
- On Postgres and MySQL the entry id is a 32-bit integer (2.1 billion entries).
|
|
754
|
+
- **A purge always closes the record's history**, even when your `purge()` keeps the row: what was recorded before it is then read as a gone record's.
|
|
755
|
+
- **An action that changes its record's primary key**, or moves a single-table-inheritance child to another type, ends the record's history: no row is left under the key the request named, which is what "gone" means.
|
|
756
|
+
- **A delete on another connection** (MySQL, a service that ignores `ctx.manager`) is noticed by one more read right after the commit, and its entry is stamped as the record's end then. That step can miss: a key that is created again between the commit and the read leaves the entry unstamped; so does a crash, a failed read or a failed update in that step (failures are logged with the correlation id). An unstamped ending means a later record with the same key shows the earlier entries in its history.
|
|
757
|
+
- **Read replicas.** Reads outside a transaction (that check after commit; with `transactions: false` the before and after images too) go through `dataSource.manager`, which TypeORM replication may send to a replica. A lagging replica then still shows a deleted row (no stamp), or shows an update as unchanged (a smaller diff, or no entry).
|
|
758
|
+
- **Scopes that depend on a field.** A record's history is open to whoever may open the record now. A reader whose scope depends on a field (`ownerId`, a status) reads the values the record had before it entered their scope, in the fields they may view.
|
|
759
|
+
|
|
760
|
+
## Upgrading to 0.2.1
|
|
761
|
+
|
|
762
|
+
Nothing in 0.2.1 breaks an app that runs on 0.2.0: no option or export was removed or changed, no table or stored key changed, and permissions decide as before. All five packages (`@nest-my-admin/core`, `ui`, `auth`, `testing` and `nest-my-admin`) are 0.2.1. Upgrade them together: `@nest-my-admin/auth` and `@nest-my-admin/testing` peer on the exact core version (0.2.1 of either does not install next to core 0.2.0), and core brings the UI of its own version.
|
|
763
|
+
|
|
764
|
+
**New**
|
|
765
|
+
|
|
766
|
+
- **`@AdminGroup` by key, for any members.** A module's `@AdminGroup` now configures the group of its key whoever the members are, also when they are only settings pages that say `group: '<key>'`, and also when the module provides nothing. See [Sidebar groups and order](#sidebar-groups-and-order).
|
|
767
|
+
- **`order`** on `@AdminResource` and `@AdminSettingsPage`: the place inside the group (lower first, default 100, then by label).
|
|
768
|
+
- **`AdminFiles`** (`publicFile(key)`, `openPublic(key)`): which keys are public files, for a host that serves them itself. See "Serving public files yourself" under [Files](#files-images-and-rich-text).
|
|
769
|
+
- **The page is in the admin's language**: `GET <path>/` answers `<html lang dir>`, `<title>` and `Content-Language` for the default `locale` and your `title`.
|
|
770
|
+
- **Documented:** the storage key of a settings page's file, `page/<name>/<field>/<yyyy>/<mm>/<uuid><ext>` (unchanged; it was only in the source).
|
|
771
|
+
|
|
772
|
+
**What a 0.2.0 app may notice**
|
|
773
|
+
|
|
774
|
+
- **A sidebar group can change its label, icon and place.** In 0.2.0 an `@AdminGroup` was read only when its module provided a resource, or a page without `group`. A group reached only through `group: '<key>'` therefore showed the key as its label ("Settings"), with no icon and order 100, even though a module carried `@AdminGroup({ key: '<key>', … })` (or was named like the key and carried `@AdminGroup` without a `key`). In 0.2.1 that module's label, icon and order apply. Group keys, URLs (`/g/<key>`) and permissions are unchanged. If you worked around it by leaving `group` off some pages, both spellings now give the same group.
|
|
775
|
+
- A module that carries `@AdminGroup` now also wins over a module without the decorator whose name gives the same key, whichever is imported first (in 0.2.0, the one whose resource was registered first).
|
|
776
|
+
- Two modules that configure one key differently: the first in import order wins (in 0.2.0, the first that had a resource or a page registered, which is nearly always the same), and 0.2.1 logs a warning at boot that names both.
|
|
777
|
+
- **The served HTML.** `GET <path>/` used to answer `<html lang="en" dir="ltr">` and `<title>Admin</title>` whatever the admin's language. It now answers with the default `locale` (`lang`, `dir`, `Content-Language`) and your `title` in it. The page is still one page for every request (it does not vary by `Accept-Language`, and there is no `Vary` header), and `Cache-Control` is still `no-cache`. An admin with `locale: 'en'` and no `title` gets byte-for-byte what it got before, plus `Content-Language: en`. The UI behaves as before once it runs. If you serve your own UI build (`uiDistPath`), the `lang` and `dir` of its `<html>` and the `<title>` of its `<head>` are replaced the same way.
|
|
778
|
+
- **Boot fails for `order` that is not a finite number** on a resource or a settings page. TypeScript did not accept the option in 0.2.0, so only untyped code that already passed one can be hit.
|
|
779
|
+
|
|
780
|
+
## Upgrading to 0.2
|
|
781
|
+
|
|
782
|
+
**Boot can now fail.** Each of these stops the app at start, with a message that names the class:
|
|
783
|
+
|
|
784
|
+
- An app that ran on 0.1 can be hit by one of them without using anything new: `widget: 'password'` (and the new `sensitive` and `secret`) on a property inside a group, that is a nested DTO or an embedded entity. Nothing applied those options there. Move the field to the top level.
|
|
785
|
+
- The others come with the features of 0.2, as you adopt them:
|
|
786
|
+
- a settings page whose name is reserved (`view`, `create`, `update`, `delete`, `purge`, `field`, `action`), a resource's name, or a custom permission of a resource (`order.export` and a page named `export`); a `permissions` entry that is a page's code (`page.<name>`, `page.<name>.update`); a resource named `page`, or whose name starts with `page:`, in an admin that has settings pages;
|
|
787
|
+
- an action's input DTO with a `file`, `image` or `richtext` widget;
|
|
788
|
+
- `atomic` on an action without `bulk: true`, on a resource action, or with `transactions: false`;
|
|
789
|
+
- an action method named like a built-in operation (`view`, `create`, `update`, `delete`, `restore`, `purge`) or like a method of `AdminResourceBase`;
|
|
790
|
+
- `@AdminCan('<name>')` whose name is not `update`, `delete` or a record action of that resource (a typo, or the name of a resource action);
|
|
791
|
+
- `files` without `ADMIN_FILE_ENTITIES`, or `audit` without `ADMIN_AUDIT_ENTITIES`, in the entities of the DataSource they name.
|
|
792
|
+
|
|
793
|
+
**Behaviour that changed**
|
|
794
|
+
|
|
795
|
+
- **Core and UI go together.** `@nest-my-admin/ui` 0.2.0 is installed with `@nest-my-admin/core` 0.2.0; if you pin or override either, keep both on 0.2.0. `GET /api/session` now always carries `audit: { enabled, view }` (`SessionResponse.audit`), which the 0.2 UI reads.
|
|
796
|
+
- **A 401 thrown by your code inside a write or a settings page's `load()` is answered as 400** with your message. The UI reads every 401 as "signed out"; the admin's own 401 is unchanged.
|
|
797
|
+
- **Deleting a record releases its files only when the row is really gone** (read again inside the write). A `delete()` override that keeps the row keeps the files; a purge always releases them. A row that goes where the write cannot see it (MySQL and a service that ignores `ctx.manager`, or a delete queued for later) leaves its files as orphans. See [Files, images and rich text](#files-images-and-rich-text).
|
|
798
|
+
- **With `audit` on:**
|
|
799
|
+
- every admin write reads the stored row before and after your method, and inserts one row into `nma_audit` in the same transaction. If that insert fails, the write fails (500);
|
|
800
|
+
- entry ids are 32-bit integers on Postgres and MySQL (2.1 billion entries); set `retentionDays` on a busy admin;
|
|
801
|
+
- a purge always closes the record's history, even when your `purge()` keeps the row;
|
|
802
|
+
- **not in the log:** changes made outside the admin, changes to `select: false` columns, and, on MySQL, a relation changed by a service that ignores `ctx.manager`;
|
|
803
|
+
- **whatever is typed into the username box of a failed sign-in is stored** as typed, cut to 200 characters: a password typed there by mistake ends up in the log. Failed sign-ins are capped per address and username, per address, and per process.
|
|
804
|
+
|
|
805
|
+
## Develop
|
|
806
|
+
|
|
807
|
+
```bash
|
|
808
|
+
bun install
|
|
809
|
+
bun run build # UI then core
|
|
810
|
+
bun run test # unit + integration on in-memory sql.js
|
|
811
|
+
bun run db:up # Postgres 17 + MySQL 8.4 in Docker (ports 55432, 53306)
|
|
812
|
+
bun run test:postgres # the same suite on Postgres (test:mysql for MySQL); each test app gets its own database
|
|
813
|
+
bun run compat # core suite on the oldest supported stack (NestJS 11.0, TypeORM 0.3.20)
|
|
814
|
+
bun run e2e # Playwright (run `bunx playwright install chromium` in examples/demo-api once; if that download is blocked, use an installed Chrome with `PW_CHANNEL=chrome bun run e2e`)
|
|
815
|
+
cd examples/demo-api && bun src/main.ts # DATABASE_URL=postgres://… or mysql://… to use a real database
|
|
816
|
+
```
|
|
817
|
+
|
|
818
|
+
Design: `docs/superpowers/specs/2026-09-29-nest-my-admin-design.md`.
|
package/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from '@nest-my-admin/core';
|
package/index.js
ADDED
package/package.json
CHANGED
|
@@ -1,6 +1,34 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "nest-my-admin",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.2.1",
|
|
4
|
+
"description": "Django-admin-class admin panel for NestJS + TypeORM (the same package as @nest-my-admin/core)",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/phantomboy0/nest-my-admin.git",
|
|
9
|
+
"directory": "packages/nest-my-admin"
|
|
10
|
+
},
|
|
11
|
+
"homepage": "https://github.com/phantomboy0/nest-my-admin#readme",
|
|
12
|
+
"type": "module",
|
|
13
|
+
"main": "./index.js",
|
|
14
|
+
"types": "./index.d.ts",
|
|
15
|
+
"exports": {
|
|
16
|
+
".": {
|
|
17
|
+
"types": "./index.d.ts",
|
|
18
|
+
"import": "./index.js",
|
|
19
|
+
"default": "./index.js"
|
|
20
|
+
},
|
|
21
|
+
"./package.json": "./package.json"
|
|
22
|
+
},
|
|
23
|
+
"files": ["index.js", "index.d.ts"],
|
|
24
|
+
"engines": {
|
|
25
|
+
"node": ">=20.19.0"
|
|
26
|
+
},
|
|
27
|
+
"scripts": {
|
|
28
|
+
"prepack": "cp ../../README.md ../../LICENSE .",
|
|
29
|
+
"typecheck": "tsc -p tsconfig.json --noEmit"
|
|
30
|
+
},
|
|
31
|
+
"dependencies": {
|
|
32
|
+
"@nest-my-admin/core": "0.2.1"
|
|
33
|
+
}
|
|
34
|
+
}
|