From ed82924a073dda6e4ff30c1dd73ec27c82eede60 Mon Sep 17 00:00:00 2001 From: "S. Amir Mohammad Najafi" Date: Tue, 26 Dec 2023 17:08:14 +0330 Subject: [PATCH] feat(reference): demo --- packages/store-reference/demo/collection.mjs | 54 ++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 packages/store-reference/demo/collection.mjs diff --git a/packages/store-reference/demo/collection.mjs b/packages/store-reference/demo/collection.mjs new file mode 100644 index 00000000..21249796 --- /dev/null +++ b/packages/store-reference/demo/collection.mjs @@ -0,0 +1,54 @@ +import {createLogger} from '@alwatr/logger'; +import {Region, StoreFileExtension} from '@alwatr/store-types'; + +import {CollectionReference} from '../dist/main.mjs'; + +const logger = createLogger('demo-collection'); + +function updateCollectionCallback(from) { + logger.logMethodArgs?.('updateCollectionCallback', from.id); +} + +const collection = CollectionReference.newRefFromData( + { + name: 'user-order', + region: Region.PerUser, + ownerId: 'user_x_id', + extension: StoreFileExtension.Json, + }, + updateCollectionCallback, +); + +logger.logProperty?.('collection.id', collection.id); +logger.logProperty?.('collection.path', collection.path); + +const orderId1 = 'order1'; + +logger.logProperty?.('order1.exists', collection.exists(orderId1)); +collection.create(orderId1, { + itemList: ['item1', 'item2'], + price: 100, +}); + +logger.logProperty?.('order1.data', collection.get(orderId1)); + +collection.set(orderId1, { + itemList: ['item1', 'item2'], + price: 200, +}); + +logger.logProperty?.('order1.data', collection.get(orderId1)); + +collection.update(orderId1, { + price: 300, +}); + +logger.logProperty?.('order1.data', collection.get(orderId1)); + +logger.logProperty?.('order1.meta', collection.meta(orderId1)); + +collection.delete(orderId1); + +collection.append({itemList: ['item3', 'item4'], price: 400}); + +logger.logProperty?.('user-order.values', collection.values());