Skip to content

Change a live App

Keep your data left you with requests saved in leave.db. The next thing you change is the App. Here you change leave.ts four times and run the script on the same file each time, to see what happens to the requests it already holds.

The file holds your data. It does not hold your App: every run loads the App from leave.ts. So a saved request is read through the App as it is now, whichever version saved it.

You change What happens to the saved requests
Add a Property They read as before, without it, and take a value for it
Add a state They get its transitions
Remove a Property They read without it, and the value stays in the file
Remove a state A request still in it stops the run, until you move it out

Three of the four need nothing from you. The fourth needs the right order.

You need leave.ts, people.ts and run.ts as Keep your data left them. Start with a new file and run the script three times:

Terminal window
rm -f leave.db leave.db-wal leave.db-shm
PYN_ENV=local npx tsx run.ts
PYN_ENV=local npx tsx run.ts
PYN_ENV=local npx tsx run.ts

leave.db now holds three requests, “Trip 1”, “Trip 2” and “Trip 3”. All three are approved.

From here the script lists the requests and adds none. In run.ts, replace everything below await harness.tickAll(); with these lines:

run.ts
// List the requests the file holds, with the Properties of each one.
const saved = await workspace.read(async (ctx) => {
const leave = ctx.apps.leave;
return await leave.queries.list.handler({}, leave, ctx);
});
for (const item of saved) {
console.log(`Saved: ${JSON.stringify(item.properties)} is ${item.state.value}`);
}
// Close the database.
await knex.destroy();

The list prints every Property of a request. Run it, with the same command as before:

Opened Workspace 56eef1d9-c851-5e2f-87c7-8759dcf17469
Listener: "Trip 3" is approved
Saved: {"reason":"Trip 1","days":1} is approved
Saved: {"reason":"Trip 2","days":1} is approved
Saved: {"reason":"Trip 3","days":1} is approved

The Listener: line is the work your third run left queued. The three Saved: lines are where every step starts.

In leave.ts, declare a category below the three Properties:

leave.ts
leaveRequest.addProperty('category', 'string');

Then write it on a request that was saved before it existed. In run.ts, add this block above // Close the database.:

run.ts
// Give the oldest request a category.
const trip = await workspace.write(async (ctx) => {
const item = await ctx.getItem(saved[0].id);
return await item?.updateProperties({ category: 'annual' });
});
console.log(`Changed: ${JSON.stringify(trip?.properties)} is ${trip?.state?.value}`);

Run it:

Opened Workspace 56eef1d9-c851-5e2f-87c7-8759dcf17469
Saved: {"reason":"Trip 1","days":1} is approved
Saved: {"reason":"Trip 2","days":1} is approved
Saved: {"reason":"Trip 3","days":1} is approved
Changed: {"reason":"Trip 1","days":1,"category":"annual"} is approved

The three Saved: lines did not change. The requests were saved without a category, and they read without one. The last line is “Trip 1” after the write: an old request takes the new Property.

Give an approved request a place to wait. In leave.ts, declare the state onHold below the canceled state, with a transition into it and one back:

leave.ts
const onHold = leaveRequest.addState('onHold');
approved.addTransition('hold', onHold);
onHold.addTransition('resume', approved);

In run.ts, change the block from step 1 so it puts “Trip 1” on hold. The marked lines changed:

run.ts
// Put the oldest request on hold.
const trip = await workspace.write(async (ctx) => {
const item = await ctx.getItem(saved[0].id);
return await item?.state?.transitions.hold.execute({});
});
console.log(`Changed: ${JSON.stringify(trip?.properties)} is ${trip?.state?.value}`);

Run it:

Opened Workspace 56eef1d9-c851-5e2f-87c7-8759dcf17469
Saved: {"reason":"Trip 1","days":1,"category":"annual"} is approved
Saved: {"reason":"Trip 2","days":1} is approved
Saved: {"reason":"Trip 3","days":1} is approved
Changed: {"reason":"Trip 1","days":1,"category":"annual"} is onHold

“Trip 1” was approved before onHold existed, and it has the hold transition all the same. A request has the transitions that its state declares in the App you run. The first Saved: line shows the category from step 1, read back from the file.

In leave.ts, delete the line you added in step 1:

leave.ts
leaveRequest.addProperty('category', 'string');

In run.ts, delete the block that puts the request on hold. It has done its work. Run it:

Opened Workspace 56eef1d9-c851-5e2f-87c7-8759dcf17469
Saved: {"reason":"Trip 1","days":1} is onHold
Saved: {"reason":"Trip 2","days":1} is approved
Saved: {"reason":"Trip 3","days":1} is approved

“Trip 1” reads without a category. The value is not gone: leave.db still holds “annual”, and an App that declares category again reads it again.

“Trip 1” is still on hold. In leave.ts, delete the three lines you added in step 2:

leave.ts
const onHold = leaveRequest.addState('onHold');
approved.addTransition('hold', onHold);
onHold.addTransition('resume', approved);

Run it. The script prints Opened Workspace and stops, with an AlpyneError and a stack trace. The message ends:

[error-023-item-state-not-in-template] Item <Identifier> on prn::leave:template/leaveRequest has stored state "onHold", which is not a state of that Template.

<Identifier> stands for the Identifier of “Trip 1”, which differs from file to file.

The file says “Trip 1” is onHold, and the App declares no such state, so reading the state of that request throws. list returns every request, so one request stops the whole list. A request in a state the App declares still reads by itself.

The safe order is the other way round: move every request out of the state, then remove the state.

Put the three lines back in leave.ts. In run.ts, add this block above // Close the database.:

run.ts
// Take the oldest request off hold.
const trip = await workspace.write(async (ctx) => {
const item = await ctx.getItem(saved[0].id);
return await item?.state?.transitions.resume.execute({});
});
console.log(`Changed: ${JSON.stringify(trip?.properties)} is ${trip?.state?.value}`);

Run it:

Opened Workspace 56eef1d9-c851-5e2f-87c7-8759dcf17469
Saved: {"reason":"Trip 1","days":1} is onHold
Saved: {"reason":"Trip 2","days":1} is approved
Saved: {"reason":"Trip 3","days":1} is approved
Changed: {"reason":"Trip 1","days":1} is approved

The App declares onHold again, so the list works, and it shows the failed run changed nothing. “Trip 1” took the resume transition and is approved.

No request is on hold now. Delete the three lines from leave.ts again, and the block from run.ts. Run it:

Opened Workspace 56eef1d9-c851-5e2f-87c7-8759dcf17469
Saved: {"reason":"Trip 1","days":1} is approved
Saved: {"reason":"Trip 2","days":1} is approved
Saved: {"reason":"Trip 3","days":1} is approved

The state is gone and every request reads. leave.ts is the App from the tutorial again.

If a request has to stay in a state, keep the state in the App.

Four more changes, each made to leave.ts on a file that holds the same three requests.

Change What happens
Rename or remove a Template The saved requests keep the old name. The App lists none of them, and reading one by its Identifier throws No template for templatePrn prn::leave:template/leaveRequest. With the name back, the App lists them again.
Remove an Event that has queued work The next run runs no Listener for the queued Event. Declaring the Event again does not bring the work back.
Rename the App The file still holds the Workspace of the App named leave, and opening it throws Unexpected: Cannot create workspace for workspaceId:56eef1d9-c851-5e2f-87c7-8759dcf17469. With the name back, it opens.
Change a Property’s type Saved values of the old type read as invalid: with days declared a string, the days of a saved request is undefined in properties. With the old type back, the values are back. Handle incomplete data changes a type and keeps the values.
  • An App you change between runs. Every run loads leave.ts, and reads the saved requests through it.
  • Additions that need no work. A new Property or a new state applies to every saved request at once.
  • Removals that hide. A Property you remove stops showing, and its values stay in the file.
  • One order to keep. Move every request out of a state before you remove the state.