SyncModel
SyncModel
¶
Bases: BaseModel, Reactive[SyncModelCtx]
The reactive counterpart of pydantic.BaseModel.
Subclass it instead of BaseModel, everything from Pydantic still works. When
live, assigning a field is validated and written to the JSON file, and changes to
the file are applied in place. Created directly, an instance is inert and acts like
a regular Pydantic model.
Example:
from syncwave import SyncList, SyncModel, Syncwave
syncwave = Syncwave()
class Settings(SyncModel):
volume: int = 8
recent_files: SyncList[str] = SyncList()
settings = syncwave.create_store(Settings, name="settings")
settings.volume = 7 # written to settings.json
settings.recent_files.append("notes.txt") # written too
print(settings.recent_files) # ['notes.txt']
Usage Documentation
Source code in src/syncwave/reactive.py
109 110 111 112 | |
SyncRoot
¶
Bases: SyncModel, RootModel, Generic[RootModelRootType]
The reactive counterpart of pydantic.RootModel.
A SyncModel holding a single value in its root field. The JSON file holds the
bare value.
Example:
from syncwave import SyncRoot, Syncwave
syncwave = Syncwave()
class Locale(SyncRoot[str]): ...
locale = syncwave.create_store(Locale, name="locale")
locale.root = "en-US" # written to locale.json as "en-US"
Usage Documentation
Source code in src/syncwave/reactive.py
109 110 111 112 | |
sync_dataclass
¶
sync_dataclass(
*,
init: Literal[False] = False,
repr: bool = True,
eq: bool = True,
order: bool = False,
unsafe_hash: Literal[False] = False,
frozen: Literal[False] = False,
config: ConfigDict | type[object] | None = None,
kw_only: bool = False,
slots: Literal[False] = False,
) -> Callable[[type[T]], type[SyncDataclass]]
sync_dataclass(
_cls: type[T],
*,
init: Literal[False] = False,
repr: bool = True,
eq: bool = True,
order: bool = False,
unsafe_hash: Literal[False] = False,
frozen: Literal[False] = False,
config: ConfigDict | type[object] | None = None,
kw_only: bool = False,
slots: Literal[False] = False,
) -> type[SyncDataclass]
sync_dataclass(
_cls: type[T] | None = None,
*,
init: Literal[False] = False,
repr: bool = True,
eq: bool = True,
order: bool = False,
unsafe_hash: Literal[False] = False,
frozen: Literal[False] = False,
config: ConfigDict | type[object] | None = None,
kw_only: bool = False,
slots: Literal[False] = False,
) -> (
Callable[[type[T]], type[SyncDataclass]]
| type[SyncDataclass]
)
Make a reactive Pydantic dataclass.
Applies pydantic.dataclasses.dataclass and makes the class reactive, like a
SyncModel. The class doesn't inherit from Reactive: check it with
is_sync_dataclass. To make an existing dataclass reactive, subclass it.
Example:
from dataclasses import field
from syncwave import SyncList, Syncwave, sync_dataclass
syncwave = Syncwave()
@sync_dataclass
class Point:
x: int = 0
y: int = 0
tags: SyncList[str] = field(default_factory=SyncList)
points = syncwave.create_store(SyncList[Point], name="points")
points.append(Point(x=1, y=2))
points[0].x = 3 # written to points.json
Usage Documentation
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
_cls
|
type[T] | None
|
The class to decorate. Omit it to pass options: |
None
|
init
|
Literal[False]
|
Must be |
False
|
repr
|
bool
|
Whether to generate |
True
|
eq
|
bool
|
Whether to generate |
True
|
order
|
bool
|
Whether to generate the comparison methods. |
False
|
unsafe_hash
|
Literal[False]
|
Must be |
False
|
frozen
|
Literal[False]
|
Must be |
False
|
config
|
ConfigDict | type[object] | None
|
The Pydantic config. |
None
|
kw_only
|
bool
|
Whether the |
False
|
slots
|
Literal[False]
|
Must be |
False
|
Returns:
| Type | Description |
|---|---|
Callable[[type[T]], type[SyncDataclass]] | type[SyncDataclass]
|
The decorated class, or a decorator if |
Source code in src/syncwave/sync_model.py
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 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 | |
is_sync_dataclass
¶
Check whether a class is a reactive dataclass, made with sync_dataclass.
Usage Documentation
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cls
|
type[Any]
|
The class to check. |
required |
Returns:
| Type | Description |
|---|---|
TypeIs[type[SyncDataclass]]
|
|
Source code in src/syncwave/sync_model.py
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 | |