Skip to content

Commit

Permalink
feat(engine5): design new concept
Browse files Browse the repository at this point in the history
  • Loading branch information
alimd committed Dec 25, 2023
1 parent d231fa8 commit 552e96f
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 0 deletions.
59 changes: 59 additions & 0 deletions demo/engine/collection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import {createLogger} from '@alwatr/logger';
import {AlwatrStore, type AlwatrDocumentRefrence} from '@alwatr/store-engine';

const logger = createLogger('AlwatrStoreDemo', true);
logger.banner('AlwatrStoreDemo');

// Create a new storage instance
const alwatrStore = new AlwatrStore({
pathPrefix: 'db',
saveDebounce: 100,
});

interface DocumentDataType {
title: string;
body: string;
}

async function quickstart() {
// Obtain a document reference.

const collectionPath = {
id: 'post-list',
region: 'public',
};

logger.logProperty?.('collectionPath', collectionPath);

logger.logProperty?.('stat(collPath)', alwatrStore.stat(collectionPath));

const collectionRef = alwatrStore.collection<DocumentDataType>(collectionPath);

processDocument(collectionRef.document('post-1'));
processDocument(collectionRef.document('post-2'));

// Delete the document.
alwatrStore.unloadCollection(collectionRef.collectionPath);
logger.logOther('Unload the collection from ram');
}

function processDocument(documentRef: AlwatrDocumentRefrence<DocumentDataType>) {
// Enter new data into the document.
let documentContext = documentRef.set({
title: 'Welcome to Alwatr Storage',
body: 'Hello World',
});
logger.logOther?.('Entered new data into the document', documentContext);

// Update an existing document.
documentContext = documentRef.update({
body: 'My first AlwatrStore app',
});
logger.logOther?.('Updated an existing document', documentContext);

// Read the document.
documentContext = documentRef.get();
logger.logOther?.('Read the document', documentContext);
}

quickstart();
60 changes: 60 additions & 0 deletions demo/engine/doc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { AlwatrStoreDocument } from 'fs/promises';

import { createLogger } from '@alwatr/logger';
import {AlwatrStore} from '@alwatr/store-engine';
const logger = createLogger('AlwatrStoreDemo', true);
logger.banner('AlwatrStoreDemo');

// Create a new storage instance
const alwatrStore = new AlwatrStore({
pathPrefix: 'db',
saveDebounce: 100,
});

interface DocumentDataType {
title: string;
body: string;
}

async function quickstart() {
// Obtain a document reference.

const documentPath = {
id: 'posts/intro-to-alwatr-store',
region: 'public',
};

logger.logProperty?.('documentPath', documentPath);

logger.logProperty?.('stat(docPath)', alwatrStore.stat(documentPath));

const documentRef = alwatrStore.document<DocumentDataType>(documentPath);

logger.logProperty?.('doc.stat', documentRef.stat());

// Enter new data into the document.
let documentContext = documentRef.set({
title: 'Welcome to Alwatr Storage',
body: 'Hello World',
});
logger.logOther?.('Entered new data into the document', documentContext);

// Update an existing document.
documentContext = documentRef.update({
body: 'My first AlwatrStore app',
});
logger.logOther?.('Updated an existing document', documentContext);

// Read the document.
documentContext = documentRef.get();
logger.logOther?.('Read the document', documentContext);

// Delete the document.
documentRef.deleteFile();
logger.logOther?.('Deleted the document');

// Delete the document.
alwatrStore.unloadDocument(documentRef.documentPath);
logger.logOther?.('Unload the document from ram');
}
quickstart();

0 comments on commit 552e96f

Please sign in to comment.