Skip to content

Commit

Permalink
feat(engine/demo): collection with new api
Browse files Browse the repository at this point in the history
  • Loading branch information
alimd committed Dec 28, 2023
1 parent 4e91b9f commit f603e86
Showing 1 changed file with 13 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {createLogger} from '@alwatr/logger';

import {AlwatrStore} from './alwatr-store.js';
import {Region, StoreFileTTL} from './type.js';
import {AlwatrStore, Region, StoreFileId, StoreFileExtension, StoreFileType} from '@alwatr/store-engine';

const logger = createLogger('AlwatrStore/Demo', true);
logger.banner('AlwatrStore/Demo');
Expand All @@ -12,14 +11,12 @@ const alwatrStore = new AlwatrStore({
saveDebounce: 5_000, // for demo
});

interface Post {
[P: string]: string;
title: string;
body: string;
}

async function quickstart() {
const postsCollectionId = 'post-list';
const postsCollectionId = {
name: 'post',
region: Region.PerUser,
ownerId: 'user_123'
};

logger.logProperty?.('collectionId', postsCollectionId);

Expand All @@ -34,15 +31,12 @@ async function quickstart() {
}

// Create a new collection.
await alwatrStore.defineCollection({
id: postsCollectionId,
region: Region.Public,
ttl: StoreFileTTL.veryShort, // for demo
alwatrStore.defineStoreFile({
...postsCollectionId,
type: StoreFileType.Collection,
extension: StoreFileExtension.Json,
});

// Check the collection stat.
logger.logProperty?.('stat', alwatrStore.stat(postsCollectionId));

// Get a collection reference.
const postsCollection = await alwatrStore.collection<Post>(postsCollectionId);

Expand All @@ -56,7 +50,7 @@ async function quickstart() {
});

// Read the collection item meta information.
logger.logProperty?.('collection.meta', postsCollection.meta(post1Id));
logger.logProperty?.('collection.meta', postsCollection.metaItem(post1Id));

// Read the collection item.
logger.logProperty?.('context1', postsCollection.get(post1Id));
Expand All @@ -75,8 +69,8 @@ async function quickstart() {
});
logger.logProperty?.('context2', postsCollection.get(post1Id));

logger.logProperty?.('collection.meta1', postsCollection.meta(post1Id));
logger.logProperty?.('collection.meta2', postsCollection.meta(post2Id));
logger.logProperty?.('collection.meta1', postsCollection.metaItem(post1Id));
logger.logProperty?.('collection.meta2', postsCollection.metaItem(post2Id));

// Unload the collection from memory.
alwatrStore.unload(postsCollectionId);
Expand Down

0 comments on commit f603e86

Please sign in to comment.