diff --git a/packages/engine5/src/alwatr-store.ts b/packages/engine5/src/alwatr-store.ts index 53549f95..8989937a 100644 --- a/packages/engine5/src/alwatr-store.ts +++ b/packages/engine5/src/alwatr-store.ts @@ -1,10 +1,10 @@ import {CollectionReference} from './lib/collection-reference.js'; import {DocumentReference} from './lib/document-reference.js'; -import {logger} from './lib/logger.js'; import {WriteFileMode, existsSync, readJsonFile, resolve, unlink, writeJsonFile} from './lib/util.js'; +import {logger} from './logger.js'; import { StoreFileType, - StoreFileEncoding, + StoreFileExtension, Region, StoreFileTTL, type StoreFileStat, @@ -18,6 +18,21 @@ export {Region, StoreFileTTL}; logger.logModule?.('alwatr-store'); +export interface AlwatrStoreConfig { + /** + * The root path of the storage. + * This is where the AlwatrStore will store its data. + */ + rootPath: string; + + /** + * The save debounce timeout in milliseconds for minimal disk I/O usage. + * This is used to limit the frequency of disk writes for performance reasons. + * The recommended value is `50`. + */ + saveDebounce: number; +} + /** * AlwatrStore engine. * @@ -38,8 +53,8 @@ export class AlwatrStore { protected static readonly rootDbStat_: StoreFileStat = { id: '.store', region: Region.Secret, - type: StoreFileType.collection, - encoding: StoreFileEncoding.json, + type: StoreFileType.Collection, + extension: StoreFileExtension.Json, ttl: StoreFileTTL.maximum, }; @@ -63,12 +78,12 @@ export class AlwatrStore { }); switch (context.meta?.type) { - case StoreFileType.document: { + case StoreFileType.Document: { DocumentReference.migrateContext_(context); break; } - case StoreFileType.collection: { + case StoreFileType.Collection: { CollectionReference.migrateContext_(context); break; } @@ -178,8 +193,8 @@ export class AlwatrStore { logger.logMethodArgs?.('defineDocument', config); const stat: StoreFileStat = { ...config, - type: StoreFileType.document, - encoding: StoreFileEncoding.json, + type: StoreFileType.Document, + extension: StoreFileExtension.Json, }; this.addStoreFile_(stat); return this.writeContext_( @@ -206,8 +221,8 @@ export class AlwatrStore { logger.logMethodArgs?.('defineCollection', config); const stat: StoreFileStat = { ...config, - type: StoreFileType.collection, - encoding: StoreFileEncoding.json, + type: StoreFileType.Collection, + extension: StoreFileExtension.Json, }; this.addStoreFile_(stat); return this.writeContext_(stat.id, CollectionReference.newContext_(config.id, config.region, stat.ownerId)); @@ -234,7 +249,7 @@ export class AlwatrStore { throw new Error('document_not_found', {cause: {id}}); } const stat = this.stat(id); - if (stat.type != StoreFileType.document) { + if (stat.type != StoreFileType.Document) { logger.accident('doc', 'document_wrong_type', stat); throw new Error('document_not_found', {cause: stat}); } @@ -268,7 +283,7 @@ export class AlwatrStore { throw new Error('collection_not_found', {cause: {id}}); } const stat = this.stat(id); - if (stat.type != StoreFileType.collection) { + if (stat.type != StoreFileType.Collection) { logger.accident('collection', 'collection_wrong_type', stat); throw new Error('collection_not_found', {cause: stat}); } @@ -394,7 +409,7 @@ export class AlwatrStore { if (stat.ownerId !== undefined) { regionPath += `/${stat.ownerId.slice(0, 3)}/${stat.ownerId}`; } - return resolve(this.config_.rootPath, regionPath, `${stat.id}.${stat.type}.${stat.encoding}`); + return resolve(this.config_.rootPath, regionPath, `${stat.id}.${stat.type}.${stat.extension}`); } /** diff --git a/packages/engine5/src/lib/util.ts b/packages/engine5/src/lib/util.ts index 2619f11b..b04b9081 100644 --- a/packages/engine5/src/lib/util.ts +++ b/packages/engine5/src/lib/util.ts @@ -9,7 +9,7 @@ import { import {copyFile, mkdir, readFile as readFile_, rename, writeFile as writeFile_, unlink} from 'node:fs/promises'; import {dirname} from 'node:path'; -import {logger} from './logger.js'; +import {logger} from '../logger.js'; import {MaybePromise} from '../type.js'; export {resolve} from 'node:path'; diff --git a/packages/engine5/src/type.ts b/packages/engine5/src/type.ts deleted file mode 100644 index bcc448dc..00000000 --- a/packages/engine5/src/type.ts +++ /dev/null @@ -1,268 +0,0 @@ -export type MaybePromise = T | Promise; - -// *** Configs *** - -export interface AlwatrStoreConfig { - /** - * The root path of the storage. - * This is where the AlwatrStore will store its data. - */ - rootPath: string; - - /** - * The save debounce timeout in milliseconds for minimal disk I/O usage. - * This is used to limit the frequency of disk writes for performance reasons. - * The recommended value is `50`. - */ - saveDebounce: number; -} - - -// *** Store File *** - -/** - * The subdirectory location for each store file. - */ -export enum Region { - /** - * Store file location that can be accessed by anyone. - */ - Public = 'p', - - /** - * Store file location that can be accessed by authenticated users. - */ - Authenticated = 'a', - - /** - * Store file location that can be accessed by super admin only. - */ - SuperAdmin = 'sa', - - /** - * Store file location specific to each user id. Can be accessed using the user token. - */ - PerUser = 'u', - - /** - * Store file location specific to each device id. - */ - PerDevice = 'd', - - /** - * Store file location specific to each token. - */ - PerToken = 't', - - /** - * Private store file location. Cannot be accessed publicly and must be directly accessed by the admin API only. - */ - Secret = 'secret', -} - -/** - * The different types of store file formats. - */ -export enum StoreFileType { - /** - * Type used for `single document` storage. - */ - Document = 'doc', - - /** - * Type used for storing a `collection` of simpler documents, referred to as collection items. - */ - Collection = 'col', - - /** - * Type used for storing a collection of items that are `append-only`. - */ - AppendOnlyCollection = 'aoc', -} - -/** - * Store file extension (encode). - */ -export enum StoreFileExtension { - /** - * AlwatrStore JSON format. - */ - Json = 'asj', -} - -/** - * Unique identifier of the store file. - * - * Get from user for select store file. - */ -export interface StoreFileId { - // [P: string]: unknown; - - /** - * The store filename. - */ - readonly name: string; - - /** - * The region where the store file is located. - * @see {@link Region} - */ - readonly region: Region; - - /** - * The owner of the store file. - * If the region is `Region.PerX` then this is the user id, device id, or token id etc. - * @see {@link Region} - * - */ - readonly ownerId?: string -} - -/** - * Store the complete metadata of the file in the root database. - */ -export interface StoreFileStat extends StoreFileId { - /** - * The type of the store file. - * - * @see {@link StoreFileType} - */ - readonly type: StoreFileType; - - /** - * The extension used for the store file. - * - * @see {@link StoreFileExtension} - */ - readonly extension: StoreFileExtension; - - /** - * The time-to-live (TTL) of the store file in memory. - */ - readonly ttl: number; -} - -/** - * Represents the metadata of a store file. - */ -export interface StoreFileMeta extends StoreFileStat { - /** - * The AlwatrStore engine version. - */ - ver: string; - - /** - * Store file format version. - */ - fv: number; - - /** - * The revision number of the store file. - * - * This number is incremented every time the store file is updated. - */ - rev: number; - - /** - * The Unix timestamp (in milliseconds since the epoch) for when the store file was updated. - */ - updated: number; - - /** - * The Unix timestamp (in milliseconds since the epoch) for when the store file was created. - */ - created: number; - - /** - * Last auto increment id. - */ - lastAutoId?: number; -} - -/** - * Represents the context of a store file. - * @template TData The type of the data content in the store file. - */ -export interface StoreFileContext = Record> { - /** - * The status of the store file. - * - * if false, the Alwatr store throws an error. - */ - ok: true; - - /** - * The metadata of the store file. - * @see {@link StoreFileMeta} - */ - meta: StoreFileMeta; - - /** - * The data content of the store file. - */ - data: TData; -} - -/** - * Store file meta only content type. - */ -export type StoreFileMetaOnlyContext = Omit, 'data'>; - -// *** Documents *** - -/** - * StoreFileContext for document type. - */ -export type DocumentContext = Record> = StoreFileContext; - - -// *** Collections *** - -/** - * The metadata of an item in a collection. - */ -export interface CollectionItemMeta { - /** - * The unique identifier for the collection item. - */ - id: string | number; - - /** - * The revision number for the collection item. - * - * This number is incremented each time the item is updated. - */ - rev: number; - - /** - * The Unix timestamp (in milliseconds since the epoch) for when the collection item was updated. - */ - updated: number; - - /** - * The Unix timestamp (in milliseconds since the epoch) for when the collection item was created. - */ - created: number; -} - -/** - * Collection item context type. - */ -export interface CollectionItem { -/** - * Collection item's metadata. - */ - meta: CollectionItemMeta; - - /** - * Collection item data. - */ - data: TItem; -} - -/** - * Collection item context type. - */ -export type CollectionContext = Record> = StoreFileContext< - Record> ->;