@@ -20,17 +20,15 @@ import {createMiddleware, createStore} from 'tinybase';
2020const store = createStore ();
2121
2222const middleware = createMiddleware (store);
23- middleware .setWillSetCellCallback (( tableId , rowId , cellId , newValue ) => {
24- if (typeof newValue === ' string' ) {
25- return newValue .toUpperCase ();
23+ middleware .addWillSetValueCallback (( valueId , value ) => {
24+ if (typeof value === ' string' ) {
25+ return value .toUpperCase ();
2626 }
2727});
2828
29- store .setCell (' pets' , ' fido' , ' species' , ' dog' );
30- console .log (store .getCell (' pets' , ' fido' , ' species' ));
31- // -> 'DOG'
32-
33- middleware .destroy ();
29+ store .setValue (' shopName' , ' happy pets' );
30+ console .log (store .getValue (' shopName' ));
31+ // -> 'HAPPY PETS'
3432```
3533
3634## Available Callbacks
@@ -69,6 +67,16 @@ The full list of `willDel*` callbacks you can register is as follows:
6967| willDelValues | valueIds | When delValues is called. | boolean |
7068| willDelValue | valueId | When delValue is called. | boolean |
7169
70+ The callbacks are registered with the Middleware object using fluent methods
71+ with the ` add* ` prefix:
72+
73+ ``` js
74+ middleware
75+ .addWillSetContentCallback ((content ) => { /* ... */ })
76+ .addWillSetTablesCallback ((tables ) => { /* ... */ });
77+ // and so on for each callback type
78+ ```
79+
7280## Callback Chaining And Cascade
7381
7482When an operation is made on the Store, the relevant callback will be called. If
@@ -81,26 +89,25 @@ callback cancels the operation, subsequent `willSet*` callbacks will not be
8189called.
8290
8391``` js
84- const middleware2 = createMiddleware (store);
85- middleware2 .setWillSetCellCallback ((tableId , rowId , cellId , cell ) => {
86- console .log (' Callback 1' );
87- return cell + ' !' ;
92+ middleware .addWillSetRowCallback ((tableId , rowId , row ) => {
93+ console .log (' Timestamp row' );
94+ return {... row, timestamp: Date .now ()};
8895});
89- middleware2 . setWillSetCellCallback ((tableId , rowId , cellId , cell ) => {
90- console .log (' Callback 2 ' );
96+ middleware . addWillSetRowCallback ((tableId , rowId , row ) => {
97+ console .log (' Cancel setting row ' );
9198 return undefined ;
9299});
93- middleware2 . setWillSetCellCallback ((tableId , rowId , cellId , cell ) => {
94- console .log (' Callback 3 ' );
95- return cell + ' ? ' ;
100+ middleware . addWillSetRowCallback ((tableId , rowId , row ) => {
101+ console .log (' Defaulting pet to be alive ' );
102+ return { ... row, alive : true } ;
96103});
97104
98- store .setCell (' pets' , ' fido' , ' species' , ' dog' );
99- // -> 'Callback 1 '
100- // -> 'Callback 2 '
105+ store .setRow (' pets' , ' fido' , { ' species' : ' dog' } );
106+ // -> 'Timestamp row '
107+ // -> 'Cancel setting row '
101108// (Callback 3 is not called because Callback 2 cancels the operation)
102109
103- console .log (store .getCell (' pets' , ' fido ' , ' species ' ));
110+ console .log (store .getTable (' pets' ));
104111// -> {}
105112```
106113
@@ -121,9 +128,9 @@ elements from there.
121128
122129## Middleware And Schemas
123130
124- The middleware callbacks are called after the schema has been applied to the
125- data. So, for example, if your schema ignores a Cell that is being set because
126- it was the wrong type, the ` willSetCell ` callback will not be called:
131+ The callbacks are called after the schema has been applied to the data. So, for
132+ example, if your schema ignores a Cell that is being set because it was the
133+ wrong type, the ` willSetCell ` callback will not be called:
127134
128135``` js
129136store .setTablesSchema ({
@@ -132,33 +139,38 @@ store.setTablesSchema({
132139 },
133140});
134141
135- middleware .setWillSetCellCallback ((tableId , rowId , cellId , cell ) => {
136- console .log (' willSetCell called ' );
142+ middleware .addWillSetCellCallback ((tableId , rowId , cellId , cell ) => {
143+ console .log (' WillSetCellCallback ' );
137144 return cell;
138145});
139146
140147store .setCell (' pets' , ' fido' , ' species' , ' dog' );
141- // -> 'willSetCell called '
148+ // -> 'WillSetCellCallback '
142149
143150store .setCell (' pets' , ' fido' , ' species' , 123 );
144151// (no output, callback not called)
145152```
146153
147154It is very important to note that there is no further schema validation again
148- _ after_ the middleware has been applied. Your middleware callbacks are powerful!
155+ _ after_ the Middleware has been applied. Your Middleware callbacks are powerful!
149156And they need to be implicitly aware of the schema and ensure that they return
150157values that are compliant with it.
151158
152- So, for example, if the middleware returns a transformed value that is the wrong
153- type, that value will be set in the Store, which might lead to all sorts of
154- surprises.
159+ So, for example, if a Middleware callback returns a transformed value that is
160+ the wrong type, that value will be set in the Store, which might lead to all
161+ sorts of surprises.
162+
163+ That said, the power of Middleware is that it can be used to implement your own
164+ custom validation, defaulting, and correction logic that is not easily captured
165+ in a plain type-based schema. Just be aware of the relationship between
166+ Middleware and schemas, and use that power wisely!
155167
156168## Summary
157169
158170Middleware gives you a powerful way to manipulate data coming into the Store,
159171and to cancel operations on the Store. It is important to understand the
160172chaining and cascade of callbacks, and to be aware of the relationship between
161- middleware and schemas.
173+ Middleware and schemas.
162174
163175Now that you understand how to manipulate data coming into the Store, let's
164176learn how to save and load Store data. For that we proceed to the Persistence
0 commit comments