Handle incomplete data
Write handlers that work when an Item lacks a value, holds a value of the wrong shape, or hides a value the principal may not read. Use this guide when your App imports data from other systems, changes a Property over time, or serves principals with partial Grants.
Before you start
Section titled “Before you start”- A Template with several Properties, declared as in Define data.
- Handlers that read those Properties, as in Add queries and mutations.
-
Write what the source supplies. A write accepts a partial
propertiesmap, so build it from the values the caller sent and leave the rest out. A value that does not fit its declared type is stored as invalid instead of failing the write. An unparseabledatereads back asundefinedfromproperties, and a select value that names no option reads back asnull.examples/incomplete-data/imports.ts import type { AppSpec } from '@alpynehq/alpyne-types/builder';interface ImportInput {readonly email: string;readonly level?: string;readonly salary?: number;readonly startedOn?: string;}interface IdInput {readonly id: string;}export const importsApp: AppSpec = (app) => {app.configure('imports');const employee = app.addTemplate('employee');employee.addProperty('email', 'string');employee.addProperty('startedOn', 'date');employee.addProperty('salary', 'integer');employee.addProperty('salaryCents', 'integer');const level = employee.addProperty('level', 'single-select');level.addOption('junior', {});level.addOption('senior', {});const importEmployee = app.addMutation('importEmployee', async (input: ImportInput, current, ctx) => {const workspace = await ctx.getWorkspaceItem();const supplied = Object.fromEntries(Object.entries(input).filter(([, value]) => value !== undefined));return await workspace.createChild(current.templates.employee, { properties: supplied });});importEmployee.input.addProperty('email', 'string').mandatory();importEmployee.input.addProperty('startedOn', 'string');importEmployee.input.addProperty('salary', 'integer');importEmployee.input.addProperty('level', 'string');importEmployee.output.item(employee).mandatory();const startedOn = app.addQuery('startedOn', async (input: IdInput, current) => {const item = await current.templates.employee.getItem(input.id);if (item === undefined) throw new Error('Employee not found');const value = item.properties.startedOn;return typeof value === 'string' ? value : undefined;});startedOn.input.addProperty('id', 'id').mandatory();startedOn.output.value('date');const salaryBand = app.addQuery('salaryBand', async (input: IdInput, current) => {const item = await current.templates.employee.getItem(input.id);if (item === undefined) throw new Error('Employee not found');const value = item.propertyValues.salary;if (value === undefined) return 'missing';if (value.type === 'invalid') return 'invalid';if (value.type === 'integer') return value.value >= 100000 ? 'upper' : 'standard';return 'invalid';});salaryBand.input.addProperty('id', 'id').mandatory();salaryBand.output.value('string').mandatory();const migrateSalary = app.addMutation('migrateSalary', async (input: IdInput, current) => {const item = await current.templates.employee.getItem(input.id);if (item === undefined) throw new Error('Employee not found');if (typeof item.properties.salaryCents === 'number') return false;const previous = item.propertyValues.salary;if (previous === undefined || previous.type !== 'integer') return false;await item.updateProperties({ salaryCents: previous.value * 100 });return true;});migrateSalary.input.addProperty('id', 'id').mandatory();migrateSalary.output.value('boolean').mandatory();const salaryCents = app.addQuery('salaryCents', async (input: IdInput, current) => {const item = await current.templates.employee.getItem(input.id);if (item === undefined) throw new Error('Employee not found');const cents = item.properties.salaryCents;if (typeof cents === 'number') return cents;const previous = item.propertyValues.salary;return previous?.type === 'integer' ? previous.value * 100 : undefined;});salaryCents.input.addProperty('id', 'id').mandatory();salaryCents.output.value('integer');}; -
Check the values the operation needs, not the whole Item. Read
item.properties.<key>and test its type before use:typeof value === 'string'for adate,typeof value === 'number'for aninteger. A Query that needs one Property returnsundefinedwhen that Property is missing and leaves the rest of the Item alone.examples/incomplete-data/imports.ts import type { AppSpec } from '@alpynehq/alpyne-types/builder';interface ImportInput {readonly email: string;readonly level?: string;readonly salary?: number;readonly startedOn?: string;}interface IdInput {readonly id: string;}export const importsApp: AppSpec = (app) => {app.configure('imports');const employee = app.addTemplate('employee');employee.addProperty('email', 'string');employee.addProperty('startedOn', 'date');employee.addProperty('salary', 'integer');employee.addProperty('salaryCents', 'integer');const level = employee.addProperty('level', 'single-select');level.addOption('junior', {});level.addOption('senior', {});const importEmployee = app.addMutation('importEmployee', async (input: ImportInput, current, ctx) => {const workspace = await ctx.getWorkspaceItem();const supplied = Object.fromEntries(Object.entries(input).filter(([, value]) => value !== undefined));return await workspace.createChild(current.templates.employee, { properties: supplied });});importEmployee.input.addProperty('email', 'string').mandatory();importEmployee.input.addProperty('startedOn', 'string');importEmployee.input.addProperty('salary', 'integer');importEmployee.input.addProperty('level', 'string');importEmployee.output.item(employee).mandatory();const startedOn = app.addQuery('startedOn', async (input: IdInput, current) => {const item = await current.templates.employee.getItem(input.id);if (item === undefined) throw new Error('Employee not found');const value = item.properties.startedOn;return typeof value === 'string' ? value : undefined;});startedOn.input.addProperty('id', 'id').mandatory();startedOn.output.value('date');const salaryBand = app.addQuery('salaryBand', async (input: IdInput, current) => {const item = await current.templates.employee.getItem(input.id);if (item === undefined) throw new Error('Employee not found');const value = item.propertyValues.salary;if (value === undefined) return 'missing';if (value.type === 'invalid') return 'invalid';if (value.type === 'integer') return value.value >= 100000 ? 'upper' : 'standard';return 'invalid';});salaryBand.input.addProperty('id', 'id').mandatory();salaryBand.output.value('string').mandatory();const migrateSalary = app.addMutation('migrateSalary', async (input: IdInput, current) => {const item = await current.templates.employee.getItem(input.id);if (item === undefined) throw new Error('Employee not found');if (typeof item.properties.salaryCents === 'number') return false;const previous = item.propertyValues.salary;if (previous === undefined || previous.type !== 'integer') return false;await item.updateProperties({ salaryCents: previous.value * 100 });return true;});migrateSalary.input.addProperty('id', 'id').mandatory();migrateSalary.output.value('boolean').mandatory();const salaryCents = app.addQuery('salaryCents', async (input: IdInput, current) => {const item = await current.templates.employee.getItem(input.id);if (item === undefined) throw new Error('Employee not found');const cents = item.properties.salaryCents;if (typeof cents === 'number') return cents;const previous = item.propertyValues.salary;return previous?.type === 'integer' ? previous.value * 100 : undefined;});salaryCents.input.addProperty('id', 'id').mandatory();salaryCents.output.value('integer');}; -
Tell an absent Item from a filtered Property.
getItem(id)resolvesundefinedboth when no Item has that Identifier and when the principal holds no Grant on its Template, so treatundefinedas “not available to this caller”. When the Item is present, a Property the principal has no Grant for is missing frompropertiesandpropertyValues, exactly like a value that was never written. A handler that finds an Item but not a Property must not conclude that the stored value is empty.examples/incomplete-data/imports.ts import type { AppSpec } from '@alpynehq/alpyne-types/builder';interface ImportInput {readonly email: string;readonly level?: string;readonly salary?: number;readonly startedOn?: string;}interface IdInput {readonly id: string;}export const importsApp: AppSpec = (app) => {app.configure('imports');const employee = app.addTemplate('employee');employee.addProperty('email', 'string');employee.addProperty('startedOn', 'date');employee.addProperty('salary', 'integer');employee.addProperty('salaryCents', 'integer');const level = employee.addProperty('level', 'single-select');level.addOption('junior', {});level.addOption('senior', {});const importEmployee = app.addMutation('importEmployee', async (input: ImportInput, current, ctx) => {const workspace = await ctx.getWorkspaceItem();const supplied = Object.fromEntries(Object.entries(input).filter(([, value]) => value !== undefined));return await workspace.createChild(current.templates.employee, { properties: supplied });});importEmployee.input.addProperty('email', 'string').mandatory();importEmployee.input.addProperty('startedOn', 'string');importEmployee.input.addProperty('salary', 'integer');importEmployee.input.addProperty('level', 'string');importEmployee.output.item(employee).mandatory();const startedOn = app.addQuery('startedOn', async (input: IdInput, current) => {const item = await current.templates.employee.getItem(input.id);if (item === undefined) throw new Error('Employee not found');const value = item.properties.startedOn;return typeof value === 'string' ? value : undefined;});startedOn.input.addProperty('id', 'id').mandatory();startedOn.output.value('date');const salaryBand = app.addQuery('salaryBand', async (input: IdInput, current) => {const item = await current.templates.employee.getItem(input.id);if (item === undefined) throw new Error('Employee not found');const value = item.propertyValues.salary;if (value === undefined) return 'missing';if (value.type === 'invalid') return 'invalid';if (value.type === 'integer') return value.value >= 100000 ? 'upper' : 'standard';return 'invalid';});salaryBand.input.addProperty('id', 'id').mandatory();salaryBand.output.value('string').mandatory();const migrateSalary = app.addMutation('migrateSalary', async (input: IdInput, current) => {const item = await current.templates.employee.getItem(input.id);if (item === undefined) throw new Error('Employee not found');if (typeof item.properties.salaryCents === 'number') return false;const previous = item.propertyValues.salary;if (previous === undefined || previous.type !== 'integer') return false;await item.updateProperties({ salaryCents: previous.value * 100 });return true;});migrateSalary.input.addProperty('id', 'id').mandatory();migrateSalary.output.value('boolean').mandatory();const salaryCents = app.addQuery('salaryCents', async (input: IdInput, current) => {const item = await current.templates.employee.getItem(input.id);if (item === undefined) throw new Error('Employee not found');const cents = item.properties.salaryCents;if (typeof cents === 'number') return cents;const previous = item.propertyValues.salary;return previous?.type === 'integer' ? previous.value * 100 : undefined;});salaryCents.input.addProperty('id', 'id').mandatory();salaryCents.output.value('integer');}; -
Read
propertyValueswhen the declared type is needed.propertiesgives the plain value;propertyValuesgives{ type, value }tagged with the declared type, with select options expanded anddatetimeandinstantvalues as LuxonDateTime. A stored value that does not match the declared type appears asInvalidPropertyValue,{ type: 'invalid', baseType }, and asundefinedinproperties. Branch ontypeto handle each case deliberately.examples/incomplete-data/imports.ts import type { AppSpec } from '@alpynehq/alpyne-types/builder';interface ImportInput {readonly email: string;readonly level?: string;readonly salary?: number;readonly startedOn?: string;}interface IdInput {readonly id: string;}export const importsApp: AppSpec = (app) => {app.configure('imports');const employee = app.addTemplate('employee');employee.addProperty('email', 'string');employee.addProperty('startedOn', 'date');employee.addProperty('salary', 'integer');employee.addProperty('salaryCents', 'integer');const level = employee.addProperty('level', 'single-select');level.addOption('junior', {});level.addOption('senior', {});const importEmployee = app.addMutation('importEmployee', async (input: ImportInput, current, ctx) => {const workspace = await ctx.getWorkspaceItem();const supplied = Object.fromEntries(Object.entries(input).filter(([, value]) => value !== undefined));return await workspace.createChild(current.templates.employee, { properties: supplied });});importEmployee.input.addProperty('email', 'string').mandatory();importEmployee.input.addProperty('startedOn', 'string');importEmployee.input.addProperty('salary', 'integer');importEmployee.input.addProperty('level', 'string');importEmployee.output.item(employee).mandatory();const startedOn = app.addQuery('startedOn', async (input: IdInput, current) => {const item = await current.templates.employee.getItem(input.id);if (item === undefined) throw new Error('Employee not found');const value = item.properties.startedOn;return typeof value === 'string' ? value : undefined;});startedOn.input.addProperty('id', 'id').mandatory();startedOn.output.value('date');const salaryBand = app.addQuery('salaryBand', async (input: IdInput, current) => {const item = await current.templates.employee.getItem(input.id);if (item === undefined) throw new Error('Employee not found');const value = item.propertyValues.salary;if (value === undefined) return 'missing';if (value.type === 'invalid') return 'invalid';if (value.type === 'integer') return value.value >= 100000 ? 'upper' : 'standard';return 'invalid';});salaryBand.input.addProperty('id', 'id').mandatory();salaryBand.output.value('string').mandatory();const migrateSalary = app.addMutation('migrateSalary', async (input: IdInput, current) => {const item = await current.templates.employee.getItem(input.id);if (item === undefined) throw new Error('Employee not found');if (typeof item.properties.salaryCents === 'number') return false;const previous = item.propertyValues.salary;if (previous === undefined || previous.type !== 'integer') return false;await item.updateProperties({ salaryCents: previous.value * 100 });return true;});migrateSalary.input.addProperty('id', 'id').mandatory();migrateSalary.output.value('boolean').mandatory();const salaryCents = app.addQuery('salaryCents', async (input: IdInput, current) => {const item = await current.templates.employee.getItem(input.id);if (item === undefined) throw new Error('Employee not found');const cents = item.properties.salaryCents;if (typeof cents === 'number') return cents;const previous = item.propertyValues.salary;return previous?.type === 'integer' ? previous.value * 100 : undefined;});salaryCents.input.addProperty('id', 'id').mandatory();salaryCents.output.value('integer');}; -
Evolve a Property by adding a new one. Declare the replacement under a new key and write both during the changeover. Migrate stored values in a Mutation that reads the old
propertyValuesentry and writes the new key only when the old value is valid. The Mutation is idempotent, so it is safe to run for every Item more than once.examples/incomplete-data/imports.ts import type { AppSpec } from '@alpynehq/alpyne-types/builder';interface ImportInput {readonly email: string;readonly level?: string;readonly salary?: number;readonly startedOn?: string;}interface IdInput {readonly id: string;}export const importsApp: AppSpec = (app) => {app.configure('imports');const employee = app.addTemplate('employee');employee.addProperty('email', 'string');employee.addProperty('startedOn', 'date');employee.addProperty('salary', 'integer');employee.addProperty('salaryCents', 'integer');const level = employee.addProperty('level', 'single-select');level.addOption('junior', {});level.addOption('senior', {});const importEmployee = app.addMutation('importEmployee', async (input: ImportInput, current, ctx) => {const workspace = await ctx.getWorkspaceItem();const supplied = Object.fromEntries(Object.entries(input).filter(([, value]) => value !== undefined));return await workspace.createChild(current.templates.employee, { properties: supplied });});importEmployee.input.addProperty('email', 'string').mandatory();importEmployee.input.addProperty('startedOn', 'string');importEmployee.input.addProperty('salary', 'integer');importEmployee.input.addProperty('level', 'string');importEmployee.output.item(employee).mandatory();const startedOn = app.addQuery('startedOn', async (input: IdInput, current) => {const item = await current.templates.employee.getItem(input.id);if (item === undefined) throw new Error('Employee not found');const value = item.properties.startedOn;return typeof value === 'string' ? value : undefined;});startedOn.input.addProperty('id', 'id').mandatory();startedOn.output.value('date');const salaryBand = app.addQuery('salaryBand', async (input: IdInput, current) => {const item = await current.templates.employee.getItem(input.id);if (item === undefined) throw new Error('Employee not found');const value = item.propertyValues.salary;if (value === undefined) return 'missing';if (value.type === 'invalid') return 'invalid';if (value.type === 'integer') return value.value >= 100000 ? 'upper' : 'standard';return 'invalid';});salaryBand.input.addProperty('id', 'id').mandatory();salaryBand.output.value('string').mandatory();const migrateSalary = app.addMutation('migrateSalary', async (input: IdInput, current) => {const item = await current.templates.employee.getItem(input.id);if (item === undefined) throw new Error('Employee not found');if (typeof item.properties.salaryCents === 'number') return false;const previous = item.propertyValues.salary;if (previous === undefined || previous.type !== 'integer') return false;await item.updateProperties({ salaryCents: previous.value * 100 });return true;});migrateSalary.input.addProperty('id', 'id').mandatory();migrateSalary.output.value('boolean').mandatory();const salaryCents = app.addQuery('salaryCents', async (input: IdInput, current) => {const item = await current.templates.employee.getItem(input.id);if (item === undefined) throw new Error('Employee not found');const cents = item.properties.salaryCents;if (typeof cents === 'number') return cents;const previous = item.propertyValues.salary;return previous?.type === 'integer' ? previous.value * 100 : undefined;});salaryCents.input.addProperty('id', 'id').mandatory();salaryCents.output.value('integer');}; -
Read the new key first and fall back to the old one. Readers stay correct for Items that were migrated, for Items still carrying the old value, and for Items that have neither. Remove the fallback once every Item has been migrated.
examples/incomplete-data/imports.ts import type { AppSpec } from '@alpynehq/alpyne-types/builder';interface ImportInput {readonly email: string;readonly level?: string;readonly salary?: number;readonly startedOn?: string;}interface IdInput {readonly id: string;}export const importsApp: AppSpec = (app) => {app.configure('imports');const employee = app.addTemplate('employee');employee.addProperty('email', 'string');employee.addProperty('startedOn', 'date');employee.addProperty('salary', 'integer');employee.addProperty('salaryCents', 'integer');const level = employee.addProperty('level', 'single-select');level.addOption('junior', {});level.addOption('senior', {});const importEmployee = app.addMutation('importEmployee', async (input: ImportInput, current, ctx) => {const workspace = await ctx.getWorkspaceItem();const supplied = Object.fromEntries(Object.entries(input).filter(([, value]) => value !== undefined));return await workspace.createChild(current.templates.employee, { properties: supplied });});importEmployee.input.addProperty('email', 'string').mandatory();importEmployee.input.addProperty('startedOn', 'string');importEmployee.input.addProperty('salary', 'integer');importEmployee.input.addProperty('level', 'string');importEmployee.output.item(employee).mandatory();const startedOn = app.addQuery('startedOn', async (input: IdInput, current) => {const item = await current.templates.employee.getItem(input.id);if (item === undefined) throw new Error('Employee not found');const value = item.properties.startedOn;return typeof value === 'string' ? value : undefined;});startedOn.input.addProperty('id', 'id').mandatory();startedOn.output.value('date');const salaryBand = app.addQuery('salaryBand', async (input: IdInput, current) => {const item = await current.templates.employee.getItem(input.id);if (item === undefined) throw new Error('Employee not found');const value = item.propertyValues.salary;if (value === undefined) return 'missing';if (value.type === 'invalid') return 'invalid';if (value.type === 'integer') return value.value >= 100000 ? 'upper' : 'standard';return 'invalid';});salaryBand.input.addProperty('id', 'id').mandatory();salaryBand.output.value('string').mandatory();const migrateSalary = app.addMutation('migrateSalary', async (input: IdInput, current) => {const item = await current.templates.employee.getItem(input.id);if (item === undefined) throw new Error('Employee not found');if (typeof item.properties.salaryCents === 'number') return false;const previous = item.propertyValues.salary;if (previous === undefined || previous.type !== 'integer') return false;await item.updateProperties({ salaryCents: previous.value * 100 });return true;});migrateSalary.input.addProperty('id', 'id').mandatory();migrateSalary.output.value('boolean').mandatory();const salaryCents = app.addQuery('salaryCents', async (input: IdInput, current) => {const item = await current.templates.employee.getItem(input.id);if (item === undefined) throw new Error('Employee not found');const cents = item.properties.salaryCents;if (typeof cents === 'number') return cents;const previous = item.propertyValues.salary;return previous?.type === 'integer' ? previous.value * 100 : undefined;});salaryCents.input.addProperty('id', 'id').mandatory();salaryCents.output.value('integer');};
Verify
Section titled “Verify”The test below imports a complete employee and a partial one, reads a stored value that does not match its type, then migrates the salary and reads it through the new key.
import type { SystemWriteRuntimeInstance } from '@alpynehq/alpyne-types/runtime';import { createHarnessInstance } from '@pyn-backend/platform-harness-public';import { createTestPlatformService } from '@pyn-backend/platform-service-test';import { createTestCronService } from '@pyn-backend/service-cron-test';import { createTestSystemService } from '@pyn-backend/service-system-test';import { createTestLoggerService } from '@pyn-backend/test-services-logger';import { describe, expect, it } from 'vitest';
import { importsApp } from './imports';
describe('handle incomplete data', () => { it('reads absent, mismatched and migrated Property values without failing the Item', async () => { const system = createTestSystemService(); const logger = createTestLoggerService(); const cron = createTestCronService({ system }); const platform = createTestPlatformService({ cron, logger, system }); const harness = await createHarnessInstance({ cron, logger, platform, system }, importsApp); const workspace = await harness.createWorkspace();
// The harness runs every execution as system, so every declared Property is visible. const ids = await workspace.write<{ readonly complete: string; readonly partial: string; readonly stored: string }, SystemWriteRuntimeInstance>( async (ctx) => { const current = ctx.apps.imports; const complete = await current.mutations.importEmployee.handler( { email: 'ada@example.com', level: 'senior', salary: 120000, startedOn: '2024-02-01' }, current, ctx, ); const partial = await current.mutations.importEmployee.handler( { email: 'grace@example.com', level: 'principal', startedOn: 'first of March' }, current, ctx, ); await ctx.seed({ items: [{ id: 'stored', properties: { email: 'linus@example.com', salary: 'unknown' }, templatePrn: current.templates.employee.prn }], }); const everyone = await current.templates.employee.selectItems().execute(); const stored = everyone.results.find((item) => item.properties.email === 'linus@example.com'); if (stored === undefined) throw new Error('Seeded employee not found'); return { complete: complete.id, partial: partial.id, stored: stored.id }; }, );
await workspace.read(async (ctx) => { const current = ctx.apps.imports; const partial = await ctx.getItem(ids.partial); const stored = await ctx.getItem(ids.stored); expect(partial?.properties.startedOn).toBeUndefined(); expect(partial?.properties.level).toBeNull(); expect(partial?.propertyValues.level).toMatchObject({ type: 'single-select', value: null }); expect(stored?.properties.salary).toBeUndefined(); expect(stored?.propertyValues.salary).toStrictEqual({ baseType: 'integer', type: 'invalid' }); expect(await ctx.getItem('00000000-0000-0000-0000-000000000000')).toBeUndefined();
const { salaryBand, startedOn } = current.queries; expect(await startedOn.handler({ id: ids.complete }, current, ctx)).toBe('2024-02-01'); expect(await startedOn.handler({ id: ids.partial }, current, ctx)).toBeUndefined(); expect(await salaryBand.handler({ id: ids.complete }, current, ctx)).toBe('upper'); expect(await salaryBand.handler({ id: ids.partial }, current, ctx)).toBe('missing'); expect(await salaryBand.handler({ id: ids.stored }, current, ctx)).toBe('invalid'); });
await workspace.write(async (ctx) => { const current = ctx.apps.imports; expect(await current.mutations.migrateSalary.handler({ id: ids.complete }, current, ctx)).toBe(true); expect(await current.mutations.migrateSalary.handler({ id: ids.complete }, current, ctx)).toBe(false); expect(await current.mutations.migrateSalary.handler({ id: ids.stored }, current, ctx)).toBe(false); });
await workspace.read(async (ctx) => { const current = ctx.apps.imports; expect(await current.queries.salaryCents.handler({ id: ids.complete }, current, ctx)).toBe(12000000); expect(await current.queries.salaryCents.handler({ id: ids.stored }, current, ctx)).toBeUndefined(); }); expect.assertions(16); });});From the repository root, run:
pnpm run cli test alpynehq/alpyne/website/examples/incomplete-data/You should see:
Test Files 1 passed (1) Tests 1 passed (1)The partial employee reads startedOn as undefined and level as null. The mismatched salary reads as { baseType: 'integer', type: 'invalid' } in propertyValues and as undefined in properties. migrateSalary returns true once and false afterwards, and salaryCents serves the migrated value. The harness runs as system, so every declared Property is visible in this test.
Related
Section titled “Related”- Data model for
propertiesversuspropertyValuesand why Items tolerate incomplete data. - Define data for declaring Properties and their types.
- Grant access for the Grants that decide which Properties a principal reads.
- Data and identity for how every Runtime interprets stored values against the Definition.
PropertyValuefor every tagged value shape a handler branches on.

