nest-my-admin 0.2.1 → 0.3.0

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.
Files changed (2) hide show
  1. package/README.md +75 -0
  2. package/package.json +2 -2
package/README.md CHANGED
@@ -416,6 +416,26 @@ export class MediaController {
416
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
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
418
 
419
+ #### Registering files yourself
420
+
421
+ A host that stores a file itself (an image fetched from another system, an attachment its own API took) registers it, so the admin's downloads, `_files`, `publicFile` and the delete/purge cleanup treat it like an upload:
422
+
423
+ ```ts
424
+ await dataSource.transaction(async (manager) => {
425
+ const file = await this.adminFiles.register(
426
+ { resource: 'product', field: 'image', recordId: product.id, body: imageStream, name: 'front.jpg' },
427
+ manager,
428
+ );
429
+ await manager.update(Product, product.id, { image: file.key }); // the record holds the key: you store it
430
+ });
431
+ ```
432
+
433
+ - The bytes are checked like an upload's: the type is read from the content and must fit the field's `accept`, and the size its `maxBytes`. A refusal is an `AdminValidationError` (422) on the field, and nothing is stored. The field must be a `file` or `image` field (not rich text); an unknown resource or field is an error.
434
+ - The object is stored under a new key in the usual format, and the row is `attached` to `recordId` (`RecordId`: a value, or `{ key: value }` for composite keys; for a settings page, `resource: 'page:<name>'` and any `recordId`). `visibility` defaults to the field's.
435
+ - **With `manager`** the row is written in that transaction when `nma_file` is on its DataSource (else at once), and if the transaction rolls back neither the row nor the object is left. Without one, the row is written at once.
436
+ - **It does not write the record.** Store `file.key` in the column yourself, in the same transaction.
437
+ - `release({ resource, field, recordId, key }, manager?)` is for a key the record no longer holds: its row goes, and its object after the transaction commits (at once without one). A key that is not that record's file in that field is left alone: `release` never deletes another record's file.
438
+
419
439
  ### Actions
420
440
 
421
441
  ```ts
@@ -509,6 +529,46 @@ export class ShopSettingsPage implements AdminSettingsSource<ShopSettingsDto> {
509
529
  - **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
530
  - **API.** `GET /api/pages/:name` answers `{ schema, values, secrets, files }`, and `PATCH /api/pages/:name` takes the changed keys and answers the same.
511
531
 
532
+ ### Inlines
533
+
534
+ A parent's form can edit its child records (a product's variants, specifications and pictures; an order's lines), and one Save writes the parent and every change to its children in **one transaction**. The child is an ordinary resource, with its own permissions, scopes, fields, DTOs, hooks and service overrides; the parent's layout places it:
535
+
536
+ ```ts
537
+ @AdminResource(OrderLine, { name: 'order-line' })
538
+ export class OrderLineAdmin extends AdminResourceBase<OrderLine> {
539
+ form: FormConfig = { create: CreateOrderLineDto, update: UpdateOrderLineDto };
540
+ }
541
+
542
+ @AdminResource(Order)
543
+ export class OrderAdmin extends AdminResourceBase<Order> {
544
+ form: FormConfig = {
545
+ create: CreateOrderDto,
546
+ update: UpdateOrderDto,
547
+ layout: [
548
+ { section: { en: 'Order', fa: 'سفارش' }, fields: ['number', 'customer'], columns: 2 },
549
+ { section: { en: 'Lines', fa: 'ردیف‌ها' }, inline: OrderLineAdmin, display: 'table', fields: ['productId', 'description', 'quantity', 'unitPrice'], order: 'position', min: 1, max: 50 },
550
+ ],
551
+ };
552
+ }
553
+ ```
554
+
555
+ - **`inline`**: the child resource class. Its entity needs exactly one many-to-one (or owning one-to-one) relation to the parent's entity: that relation's field is the **link** (`orderId`). With several candidates, name it with `link: 'orderId'`. The server sets the link; a client that sends it is refused (422), and an update cannot change it.
556
+ - **`display`**: `cards` (default; each child is a card with the child's own form layout) or `table` (one row per child, one cell per field: for short rows like order lines; on phones the table scrolls sideways).
557
+ - **`fields`**: which of the child's form fields the inline shows, in order (default: the child's update form fields without the link). Fields it does not show keep their stored values, and their DTO defaults on create.
558
+ - **`order`**: a number field of the child. Rows are read sorted by it (then by key), and dragging or the up/down buttons write 0, 1, 2… into it on save. Without it rows go by key and cannot be reordered.
559
+ - **`min`**, **`max`** (default 100, at most 500): how many rows a save may leave, checked on the server after the changes (422 on the inline). `max` is also how many rows are read: a record with more shows them read-only, with a link to the child's list filtered by the parent.
560
+ - One level only: an inline cannot contain inlines (boot error). A child may be inlined into more than one parent (with different links).
561
+
562
+ **Permissions.** An inline never widens access: a user reads, adds, changes, removes or reorders a child through an inline only as they could through the child's own resource (`order-line.view`, `.create`, `.update`, `.delete`, its field levels and scopes, each row's `@AdminCan`), and only the children of this parent. A user without `<child>.view` does not see the inline at all; without `<child>.delete` there is no remove button, and a crafted delete is a 403 that saves nothing. Since children are saved with the parent, the save also needs `<parent>.create` (a new parent) or `<parent>.update`. View-as shows inlines read-only, like everything else.
563
+
564
+ **Saving.** The form sends, next to the parent's fields, `_inlines: { <child>: { create, update, delete, order } }` (`InlineChanges` in the contract): new rows with a `_key`, changed fields of changed rows with their `_id` (and `_version` for a child with a version column), the ids to delete, and the new order. Inside the parent's write the parent is saved first (a save with only inline changes does not update the parent), then each inline in layout order: deletes, updates, creates, each through the child resource's own `delete`/`update`/`create` and its write participants (files, audit). Any error rolls back everything; errors name the row (`_inlines.order-line.create.2.quantity`, `_inlines.order-line.update.17.unitPrice`) and the form shows them on that row. At most 500 entries per inline per request.
565
+
566
+ **Options that depend on the parent.** A child's `relationOptions(field, qb, ctx, values)` receives `values._parent`: the parent form's values as the user sees them (unsaved, in the picker; body over stored, on save), with `_parent._resource` naming the parent. Use it to narrow a picker to what the parent allows, such as the attributes of the product's category.
567
+
568
+ **History.** Each child change is the child's own audit entry (`order-line.created`, …) in the child's history, with `meta.parent` naming the parent (for readers who may view the parent's resource). The parent's History tab shows the parent's own entries only.
569
+
570
+ **Duplicate** copies the parent only, not its children (the copied form says so).
571
+
512
572
  ## Transactions, context and errors
513
573
 
514
574
  Every create, update and delete runs in one database transaction. Pass `ctx.manager` to your services so their writes
@@ -757,6 +817,21 @@ export class EmployeeAdmin extends AdminResourceBase<Employee> {
757
817
  - **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
818
  - **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
819
 
820
+ ## Upgrading to 0.3.0
821
+
822
+ Nothing in 0.3.0 breaks an app that runs on 0.2.1: no option or export was removed or changed, no table or stored key changed, and a resource without inlines answers exactly as before (responses only gain fields). All five packages are 0.3.0; upgrade them together, as before.
823
+
824
+ **New**
825
+
826
+ - **Inlines**: child resources edited in the parent's form and saved with it in one transaction. See [Inlines](#inlines).
827
+ - **`AdminFiles.register()` and `release()`**: files a host stored itself become files of a record's field. See "Registering files yourself" under [Files](#files-images-and-rich-text).
828
+ - **A key column named `id` without a label is "شناسه" in Persian** (still "Id" in English).
829
+
830
+ **What a 0.2.1 app may notice**
831
+
832
+ - `relationOptions(field, qb, ctx, values)`: a key named `_parent` in `values` now has a meaning when the picker belongs to an inline (`?parent=` on the options route); without `?parent=` it is passed through as before.
833
+ - The UI's lists, sidebar and header: the sidebar and the header stay in place while the page scrolls, and in right-to-left languages table headers start on the right like their cells.
834
+
760
835
  ## Upgrading to 0.2.1
761
836
 
762
837
  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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nest-my-admin",
3
- "version": "0.2.1",
3
+ "version": "0.3.0",
4
4
  "description": "Django-admin-class admin panel for NestJS + TypeORM (the same package as @nest-my-admin/core)",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -29,6 +29,6 @@
29
29
  "typecheck": "tsc -p tsconfig.json --noEmit"
30
30
  },
31
31
  "dependencies": {
32
- "@nest-my-admin/core": "0.2.1"
32
+ "@nest-my-admin/core": "0.3.0"
33
33
  }
34
34
  }