ArrayStorage
ArrayStorage is an abstract field type that other Array-related field types
can extend. Fields that use ArrayStorage include:
- EnumMultiple
- StructuredArray
Such fields have additional abilities built-in, that are not documented separately.
Filtering
You can filter the array by individual item values. Let's assume you have the
following items stored in the database:
await app.collections.cakes.suCreate({ ingredients: ["flour", "water", "carrot"], }); await app.collections.cakes.suCreate({ ingredients: ["carrot", "water", "flour"], }); await app.collections.cakes.suCreate({ ingredients: ["flour", "water", "eggs"], }); await app.collections.cakes.suCreate({ ingredients: ["flour", "salt"], });
Now, if you filter the cakes collection by a single value in the ingredients
field, it will return all items that contain that particular ingredient:
const { items: watery } = await app.collections.cakes .suList() .filter({ ingredients: "water" }) .fetch(); assert.strictEqual(watery.length, 3);
You can also filter by multiple ingredients. To do so, you provide a single
object as a filter. That object has to have one key (all, exact, any) with an
array value.
const { items: carrot_nonreverse } = await app.collections.cakes .suList() .filter({ ingredients: { exact: ["flour", "water", "carrot"], }, }) .fetch(); assert.strictEqual(carrot_nonreverse.length, 1); const { items: carrot_any_direction } = await app.collections.cakes .suList() .filter({ ingredients: { all: ["flour", "water", "carrot"], }, }) .fetch(); assert.strictEqual(carrot_any_direction.length, 2); const { items: eggs_or_salt } = await app.collections.cakes g .suList() .filter({ ingredients: { any: ["eggs", "salt"], }, }) .fetch(); assert.strictEqual(eggs_or_salt.length, 2);
Modifying the array
You can modify fields that use ArrayStorage in two ways - either by providing an
entire new value for the whole array, or by passing an action as the new
value. If you pass the action, it changes the old value of the array.
Add an element to the array
const invoice = await app.collections.invoices.suCreate({ entries: [ { title: "pen", price: 1.1 }, { title: "apple", price: 2.2 }, ], }); invoice.set("entries", { insert: { value: { title: "pineapple", price: 3.3 }, index: 1, }, }); await invoice.save(new app.SuperContext()); assert.deepStrictEqual(invoice.get("entries"), [ { title: "pen", price: 1.1 }, { title: "pineapple", price: 3.3 }, { title: "apple", price: 2.2 }, ]);
Delete an element of the array
const invoice = await app.collections.invoices.suCreate({ entries: [ { title: "pen", price: 1.1 }, { title: "apple", price: 2.2 }, { title: "pineapple", price: 3.3 }, ], }); invoice.set("entries", { remove: 0 }); await invoice.save(new app.SuperContext()); assert.deepStrictEqual(invoice.get("entries"), [ { title: "apple", price: 2.2 }, { title: "pineapple", price: 3.3 }, ]);
Swap elements of the array
const invoice = await app.collections.invoices.suCreate({ entries: [ { title: "pen", price: 1.1 }, { title: "apple", price: 2.2 }, { title: "pineapple", price: 3.3 }, ], }); invoice.set("entries", { swap: [0, 1] }); await invoice.save(new app.SuperContext()); assert.deepStrictEqual(invoice.get("entries"), [ { title: "apple", price: 2.2 }, { title: "pen", price: 1.1 }, { title: "pineapple", price: 3.3 }, ]);
Combine atomic actions with other edits in the array
You can submit a new version of the array and then run the action on that new
version, in one step. This is useful if you want to for example have a form where the user can edit some entries within the array and then submit an action that edits the array, before the other edits are saved. This way the action will be performed on the newest version of the user's input:
const invoice = await app.collections.invoices.suCreate({ entries: [ { title: "pen", price: 1.1 }, { title: "apple", price: 2.2 }, ], }); invoice.set("entries", { insert: { value: { title: "pineapple", price: 3.3 }, index: 1, }, data: [ { title: "Pen", price: 100 }, { title: "Apple", price: 200 }, ], }); await invoice.save(new app.SuperContext()); assert.deepStrictEqual(invoice.get("entries"), [ { title: "Pen", price: 100 }, { title: "pineapple", price: 3.3 }, { title: "Apple", price: 200 }, ]);
File Metadata
- Mime Type
- text/plain
- Expires
- Fri, Jul 4, 07:18 (21 h, 32 m)
- Storage Engine
- blob
- Storage Format
- Raw Data
- Storage Handle
- 781837
- Default Alt Text
- array-storage.remarkup (4 KB)