Data adapters¶
The layer between a component and its storage. Two protocols — one item, one collection — and the implementations niceview ships. Data adapters is the guide; this page is the signature-level reference.
ItemAdapter
¶
Adapter protocol for single-item backends (ModelForm).
read() loads the item; save() persists it. No key is needed — the adapter encapsulates the identity of the item (e.g. a file path, or a key bound at construction time via BoundItem).
Source code in niceview/dataadapter.py
CollectionAdapter
¶
Adapter protocol for list-backed components (ModelGrid, EditGridWrapper).
Covers full CRUD plus key management. Use BoundItem to wrap a CollectionAdapter + key into an ItemAdapter for ModelForm.
Source code in niceview/dataadapter.py
create
¶
update
¶
Persist changes to an existing item. Returns the stored item (may include server-updated fields such as updated_at).
items
¶
Yield (key, item) pairs — like dict.items(), combining iter with key_from_item.
ReloadableAdapter
¶
Bases: Protocol
Protocol for adapters that support reloading from an external source (e.g. disk).
Source code in niceview/dataadapter.py
ReactiveAdapter
¶
Bases: Protocol
Protocol for adapters that support push-based change notification.
All built-in CollectionAdapters (ListAdapter, JsonListAdapter, SqlModelAdapter) implement this protocol. ModelGrid.render() detects it and registers update_rows() automatically — no explicit call needed after adapter mutations.
For ListAdapter backed by an ObservableList, direct mutations on the list object (bypassing the adapter) also fire the callback.
Source code in niceview/dataadapter.py
BoundItem
¶
Bases: ItemAdapter[T]
Wraps a CollectionAdapter + key into an ItemAdapter.
Use this for master-detail navigation: bind a selected row to a ModelForm without exposing key management to the form layer.
Source code in niceview/dataadapter.py
ListAdapter
¶
Bases: _ChangeNotifier, CollectionAdapter[T]
An adapter for an in-memory list.
Keys are monotonic counter strings ("0", "1", ...) assigned at creation time, stable across deletions. key_from_item() searches by object identity (O(n)).
Implements ReactiveAdapter: on_change() handlers fire after every structural mutation via the adapter (create/update/delete). When the backing list is an ObservableList, direct mutations on the list object (bypassing the adapter) also fire the handlers and the key map is reconciled automatically.
created_field: if set, datetime.now(utc) is written to that field on create().
Source code in niceview/dataadapter.py
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 | |
JsonAdapter
¶
Bases: ItemAdapter[T]
An ItemAdapter backed by a JSON file containing a single Pydantic model instance. Writes are atomic (.tmp → rename).
lock_field: if set, save() compares the field value against the current file contents before writing (optimistic locking). A mismatch means another user saved in the meantime and raises ConflictError; if the file cannot be read during the check, StorageError is raised. The field is updated to now() on every successful save. A per-file threading.Lock makes the read-compare-write sequence atomic within the process.
created_field: if set, the field is populated with now() on the first save() (when the field value is None) and never overwritten thereafter.
Source code in niceview/dataadapter.py
446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 | |
JsonListAdapter
¶
Bases: ListAdapter[T], ReloadableAdapter
A data adapter that persists a list of Pydantic models as a JSON array.
Items are kept in memory (like ListAdapter) and written to disk after every mutating operation. Writes are atomic: the full list is serialized to a .tmp file that is then renamed over the target path. A lock_field for for optimistic locking is not supported.
Keys are monotonic counter strings (same as ListAdapter), stable across deletions within a session but reassigned after reload().
Source code in niceview/dataadapter.py
545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 | |
reload
¶
Re-read items from the JSON file, replacing the in-memory list.
Keys are reassigned to new counter values after reload, so any BoundItem holding a previous key is invalidated. Registered on_change() handlers (including ModelGrid) are notified automatically.
Source code in niceview/dataadapter.py
DirectoryAdapter
¶
Bases: _ChangeNotifier, CollectionAdapter[FileEntry]
A CollectionAdapter over files in a directory: one item (FileEntry) per file. Items carry only metadata (name/mtime/size), not parsed file content — open the file's own adapter (JsonAdapter/JsonListAdapter) for that.
Two modes, selected by suffix:
- Suffix mode (default,
suffix='.json'): a homogeneous document collection of files sharing one extension. Keys are the filename stem (suffix stripped), and only*<suffix>files are listed. create() writesdefault_content. - All-files mode (
suffix=Noneor''): a general file browser over a directory with mixed extensions. Keys are the full filename (extension included), and create() expects a full name.default_contentstill seeds new files but rarely makes sense across mixed types, so pass names explicitly in this mode.
In both modes the listing covers regular files only; hidden dotfiles are excluded, as
are names an optional name_filter rejects. name_filter always receives the full
filename (extension included), never the key. Files whose key would be unusable (a bare
.json in suffix mode, or a name containing a path separator) are skipped rather than
raising, so one odd file cannot break the whole listing; addressing such a file
explicitly by key still raises ValueError.
create() picks a free 'untitled-NN' name and writes default_content; rename() renames the file on disk. Both are meant to be driven directly by application code (e.g. a "Name" input in a detail view wired to rename()), not through generic CollectionAdapter callers — see DrillDownWrapper's render_detail hook.
Source code in niceview/dataadapter.py
809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 | |
FileEntry
¶
Bases: BaseModel
Metadata for one file in a DirectoryAdapter — NOT the file's parsed content. Open the file's own adapter (JsonAdapter/JsonListAdapter) for that, typically inside a DrillDownWrapper render_detail callback.
Source code in niceview/dataadapter.py
FilteredAdapter
¶
Bases: _ChangeNotifier, CollectionAdapter[T], ReloadableAdapter
Wraps a CollectionAdapter, filtering iter results by a predicate and injecting default field values on create().
Typical use: show a parent-filtered view of a child collection (e.g., books for a specific author) while routing mutations through the underlying adapter for DB persistence. Change notifications from the inner adapter are forwarded automatically.
Source code in niceview/dataadapter.py
items
¶
lenient_model_load
¶
Load a single Pydantic model from json_text with best-effort error recovery.
- Malformed JSON or a non-object root → log.error, return
model_type(). - Unknown fields → logged, ignored.
- Fields with invalid values → logged, dropped; the model default fills the gap.
- Required field absent with no default → log.error, raise (last resort).
context is a human-readable label (typically a file path) included in every log message to help identify the source of the problem.
Source code in niceview/dataadapter.py
lenient_list_load
¶
Load a list of Pydantic models from json_text with best-effort error recovery.
- Malformed JSON or non-array root → log.error, return
[]. - Items with invalid fields → loaded leniently (bad fields get defaults).
- Items that cannot be recovered (e.g. missing required field) → skipped with log.error.
context is a human-readable label included in every log message.
Source code in niceview/dataadapter.py
ConflictError
¶
Bases: ValueError
Raised when a save is rejected because another user modified the item in the meantime.
Subclasses ValueError for backward compatibility: optimistic locking conflicts were previously reported as plain ValueError.
Source code in niceview/dataadapter.py
StorageError
¶
SQL¶
SqlModelAdapter needs the optional sqlmodel extra — see Installation.
SqlModelAdapter
¶
Bases: _ChangeNotifier, CollectionAdapter[T], ReloadableAdapter
An adapter for SQLModel / SQLAlchemy to work with ModelForm and ModelGrid.
Supports optimistic locking via lock_field (default 'updated_at'). Set lock_field=None to disable locking.
created_field (default None): if set, the named field is automatically populated with datetime.now(utc) when create() is called.
Source code in niceview/sqlmodel_adapter.py
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 | |
reload
¶
Signal that the backing database may have changed.
Every read() already queries the database, so no in-memory state needs refreshing. Fires on_change() handlers so any registered ModelGrid re-renders from the current database state.