Usage
To use Snapshotable
, first make sure to install the package
yarn add @rvoh/dream-plugin-json-snapshot
Once done, find the model you want to take snapshots with, and be sure to extend the parent model using the provided mixin:
import { Snapshotable } from '@rvoh/dream-plugin-json-snapshot'
class User extends Snapshotable(ApplicationModel) {
...
}
Once done, you can take a snapshot of a user like so:
const user = await User.firstOrFail()
const data = await user.takeSnapshot()
Including through
associations
BelongsTo
associations are intentionally skipped, as are through
associations, so Snapshotable automatically avoids circuits (which would lead to an infinite loop). To explicitly include a through
association, decorate it with the @SnapshotableFollowThrough()
decorator.
const deco = new Decorators<InstanceType<typeof Host>>()
export default class Host extends ApplicationModel {
@deco.HasMany('HostPlace')
public hostPlaces: HostPlace[]
@SnapshotableFollowThrough()
@deco.HasMany('Place', { through: 'hostPlaces' })
public places: Place[]
}
Filtering out unwanted associations
In some cases, you want to hide certain associations from snapshots. In these cases, you can use the provided SnapshotableIgnore
decorator, like so:
class User extends Snapshotable(ApplicationModel) {
@SnapshotableIgnore()
@deco.HasMany(Pet)
public pets: Pet[]
}