Millwork for WarpDrive and ember-data: codecs as Transformations and @attr transforms, and
shapes and values as record fields.
Docs: joegaudet.com/millwork · Guide · API
pnpm add @millwork-ts/warp-drive @millwork-ts/core
The peer is @warp-drive/core 5.8 or later. The package is a v2 Ember addon: it gives the app
one transform:<name> for each core codec.
transformations() gives every Millwork Transformation: each codec (and its array: form),
shapes and value classes. Core codecs are in the registry already. Register the codecs of other
packages first, because transformations() reads the registry when you call it.
import { codecs as luxon } from '@millwork-ts/luxon'
import { registerCodecs, transformations } from '@millwork-ts/warp-drive'
import { useLegacyStore } from '@warp-drive/legacy'
import { JSONAPICache } from '@warp-drive/json-api'
registerCodecs(luxon)
export default useLegacyStore({
linksMode: true,
cache: JSONAPICache,
schemas: [InvoiceSchema],
transformations: transformations(),
})
| Helper | Field |
|---|---|
valueField(name, codec?, options?) |
A value. The codec is a name, a codec object or a Value class. |
valueArrayField(name, codec, options?) |
A list of values. The codec runs on each element. |
shapeField(name, Class, options?) |
A shape. A map of classes makes a polymorphic shape. |
shapeArrayField(name, Class, { key }) |
A ShapeArray. key names the field that gives each member its identity. |
import { shapeArrayField, shapeField, valueField } from '@millwork-ts/warp-drive'
import { withDefaults } from '@warp-drive/core/reactive'
export const InvoiceSchema = withDefaults({
type: 'invoice',
fields: [
valueField('total', 'money', { wire: 'decimal' }),
valueField('issuedAt', 'datetime'),
shapeField('billTo', Address),
shapeArrayField('items', LineItem, { key: 'sku' }),
shapeField('payment', { card: Card, bank: Bank }, { discriminator: '$type' }),
],
})
kind: 'field' field. Do not use kind: 'object' or schema-object
for a Millwork field
(ADR 0001,
ADR 0007).invoice.total === invoice.total.A Polaris record is immutable. Use checkout from this package, not from WarpDrive. It records
the store, so the editable copy can read its remote values
(ADR 0009).
import { checkout } from '@millwork-ts/warp-drive'
const draft = await checkout(invoice)
draft.billTo.city = 'Montreal'
draft.billTo.isDirty // true
The @millwork-ts/warp-drive/legacy entry loads no WarpDrive code at run time.
@attr transformsEach core codec is an app transform, so @attr('money', { wire: 'decimal' }) needs no file
in the app. Give a codec from another package to registerTransforms in an instance
initializer:
import type ApplicationInstance from '@ember/application/instance'
import { datetime } from '@millwork-ts/luxon'
import { registerTransforms } from '@millwork-ts/warp-drive/legacy'
export function initialize(owner: ApplicationInstance) {
registerTransforms(owner, { datetime })
}
export default { initialize }
ember-data runs an @attr transform only in a serializer. store.push does not run it, so a
pushed Model record shows the raw value. For pushed data, use a schema field.
A Model attribute cannot hold a shape. Put the fields on a plain class, and make a
legacy-mode schema with modelSchema:
import type { Money, ShapeArray } from '@millwork-ts/core'
import { modelSchema, shape, shapeArray, value } from '@millwork-ts/warp-drive/legacy'
import { withDefaults } from '@warp-drive/legacy/model/migration-support'
class OrderFields {
@value() declare customer: string
@value('money', { wire: 'decimal' }) declare total: Money
@shape(Address) declare shipTo: Address
@shapeArray(LineItem, { key: 'sku' }) declare items: ShapeArray<LineItem>
}
export const OrderSchema = withDefaults(modelSchema('order', OrderFields))
The record is editable with no checkout, and rollbackAttributes() works for the whole
record (ADR 0003,
ADR 0012).
MIT