// { "total": { "amount": "19.99", "currency": "USD" } }
const { amount, currency } = order.total
const cents = Math.round(parseFloat(amount) * 100)
const label = `${(cents / 100).toFixed(2)} ${currency}`
// Dirty? Compare JSON by hand.
JSON.stringify(order.total) !== JSON.stringify(saved)
Why
Ask the value, not the payload
A money field that arrives as JSON gets the same parse, the same float math and the same
format in every component that shows it. Mill it once, at the edge of the store, and every
component gets a real Money.
// valueField('total', 'money', { wire: 'decimal' })
order.total.toDecimalString() // "19.99"
order.total.currency // "USD"
order.total.equals(saved) // compares by value
// The read is memoized, so identity holds.
order.total === order.total // true
Bad data is undefined
A field the codec cannot read is undefined with one warning. One bad
field does not fail the whole response.
Equal by value
Each codec has an equals. 19.99 USD equals
19.990 USD, so a set of an equal value clears the dirty flag.
Plain JSON in the cache
The WarpDrive cache keeps the raw wire form. The rich value lives on the record, and
serialize writes the same wire form back.
Dirty by path
A shape knows which nested field changed. rollback(['city']) undoes one
field and keeps the others.
Two kinds of part
Shapes and values
A record is an entity: it has an id. The parts inside it do not. Millwork gives you two kinds, the same split as in entity vs value object.
| Kind | Identity | Mutable | Dirty and rollback | Equality | Example |
|---|---|---|---|---|---|
| entity | yes (id) | yes | the record | by id | an Order record |
| shape | no | yes, in place | yes, by path | field by field | an Address a form edits |
| value | no | no | replace it whole | codec equals |
Money, a date, a LatLng |
import { Shape, field } from '@millwork-ts/core'
export class Address extends Shape {
static override type = 'address'
@field declare city: string
@field declare zip: string
}
order.shipTo.city = 'Montreal'
order.shipTo.isDirty // true
order.shipTo.changes() // [{ kind: 'set', path: ['city'], … }]
order.shipTo.rollback(['city'])
import { Value, field, type Money } from '@millwork-ts/core'
export class Price extends Value {
static override type = 'price'
@field('money', { wire: 'decimal' }) declare amount: Money
@field declare label: string
}
price.label = 'Sale' // TypeError: a value is immutable
line.price = price.with({ label: 'Sale' })
The declare form is for Ember, which compiles legacy decorators. With
standard decorators, write @field accessor city!: string. A JSON form,
shape({ type, fields }), has the same power.
Wire it up
Two doors into WarpDrive
Use SchemaRecord with JSON schemas, or keep the class form on a legacy-mode store. Both read the same codecs and the same shape classes.
import { withDefaults } from '@warp-drive/core/reactive'
import {
checkout,
shapeArrayField,
shapeField,
valueField,
} from '@millwork-ts/warp-drive'
export const OrderSchema = withDefaults({
type: 'order',
fields: [
valueField('total', 'money', { wire: 'decimal' }),
shapeField('shipTo', Address),
shapeArrayField('items', LineItem, { key: 'sku' }),
shapeField('payment', { card: Card, bank: Bank }),
],
})
// A Polaris record is immutable. Edit a copy.
const draft = await checkout(order)
draft.shipTo.city = 'Montreal'
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('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))
// On a Model, a value is a plain @attr transform:
// @attr('money', { wire: 'decimal' }) declare total: Money
One call gives the store every Millwork Transformation:
transformations: transformations(). See
the guide for the store set-up and
the known limits.
Packages
One package for each peer library
Core has no dependencies on Ember or a peer library. Add a package only for the library that you already use. Each codec name is what a field puts in its schema.
@millwork-ts/core
No peer. Shapes, values, the codec contract.
money decimal bigint number-string
percent ratio boolean-ish url
email uuid ulid snowflake
country currency-code language
locale time-zone latlng geojson
bytes byte-size bitflags mime-type
i18n-text
@millwork-ts/warp-drive
Peer: @warp-drive/core 5.8 or later.
transformations valueField shapeField
shapeArrayField checkout, and /legacy:
@attr transforms, @shape, @value,
modelSchema
@millwork-ts/temporal
Peer: temporal-polyfill, optional.
date time local-datetime datetime
zoned-datetime epoch year-month
duration interval
@millwork-ts/luxon
Peer: luxon.
date time local-datetime datetime
zoned-datetime epoch year-month
duration interval
@millwork-ts/date-fns
Peers: date-fns, @date-fns/tz.
date datetime epoch duration
interval, and zoned-datetime from /tz
@millwork-ts/dayjs
Peer: dayjs.
date time local-datetime datetime
zoned-datetime epoch year-month
duration
@millwork-ts/dinero
Peer: dinero.js v2.
dinero, all five money wire forms
@millwork-ts/decimal
Peer: decimal.js, big.js or bignumber.js.
decimalCodec(lib) gives big-decimal
@millwork-ts/phone
Peer: libphonenumber-js.
phone, E.164 and national numbers
@millwork-ts/color
Peer: culori.
color: hex, rgb, hsl, oklch
@millwork-ts/quantity
Peer: js-quantities.
quantity, units of measure
@millwork-ts/semver
Peer: semver.
semver
@millwork-ts/ip
Peer: ipaddr.js.
ip cidr
@millwork-ts/rrule
Peer: rrule.
rrule, RFC 5545 recurrence
@millwork-ts/cron
Peer: croner.
cron, 5 or 6 fields
@millwork-ts/geo
Peers: ngeohash, and a WKT library.
wkt geohash
Get started
Install
Add core and the WarpDrive adapter, then one package for each peer library. Register the adapter codecs before the store asks for its Transformations.
pnpm add @millwork-ts/core @millwork-ts/warp-drive
pnpm add @millwork-ts/luxon luxon
// app/services/store.ts
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'
// Core codecs are there already. Add the adapter codecs first.
registerCodecs(luxon)
export default useLegacyStore({
linksMode: true,
cache: JSONAPICache,
schemas: [OrderSchema],
transformations: transformations(),
})
Then read the guide, or browse the API reference.
More
Part of the Ember Workbench
Millwork is one of the tools on the Ember Workbench. For closed lists, such as an order status, use real-enums: real-enums names the words, Millwork shapes the values.
- Guide: codecs, shapes, both WarpDrive doors, validation.
- API reference, generated from the code.
- Source on GitHub.
-
Packages on npm, under
@millwork-ts. - llms.txt, the rules for AI coding agents.