@jsonkit/db 3.0.2 → 4.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -31,6 +31,8 @@ All multi-entry implementations expose the same methods:
31
31
  - `getById(id)`
32
32
  - `getByIdOrThrow(id)`
33
33
  - `getWhere(predicate, pagination?)`
34
+ - `getFirstWhere(predicate)`
35
+ - `getFirstWhereOrThrow(predicate)`
34
36
  - `getAll(ids?)`
35
37
  - `getAllIds()`
36
38
  - `update(id, updater)`
@@ -27,13 +27,42 @@ var path__namespace = /*#__PURE__*/_interopNamespaceDefault(path);
27
27
  var fsSync__namespace = /*#__PURE__*/_interopNamespaceDefault(fsSync);
28
28
  var readline__namespace = /*#__PURE__*/_interopNamespaceDefault(readline);
29
29
 
30
+ class DbError extends Error {
31
+ }
32
+ class NotFoundError extends DbError {
33
+ }
34
+ class ConflictError extends DbError {
35
+ }
36
+ class FileIoError extends DbError {
37
+ }
38
+ class InvalidIdError extends DbError {
39
+ constructor(id) {
40
+ super(`Invalid entry id '${id}'`);
41
+ }
42
+ }
43
+ class UninitError extends DbError {
44
+ constructor() {
45
+ super('Cannot read or update uninitialized entry. Use write(entry) to initialize first.');
46
+ }
47
+ }
48
+
30
49
  class MultiEntryDb {
31
50
  async getByIdOrThrow(id) {
32
51
  const entry = await this.getById(id);
33
52
  if (!entry)
34
- throw new Error(`Entry with id '${id}' does not exist`);
53
+ throw new NotFoundError(`Entry with id '${id}' does not exist`);
35
54
  return entry;
36
55
  }
56
+ async getFirstWhere(predicate) {
57
+ const matches = await this.getWhere(predicate, { page: 0, take: 1 });
58
+ return matches.at(0) ?? null;
59
+ }
60
+ async getFirstWhereOrThrow(predicate) {
61
+ const match = await this.getFirstWhere(predicate);
62
+ if (!match)
63
+ throw new NotFoundError('No entry matches predicate');
64
+ return match;
65
+ }
37
66
  async getWhere(predicate, pagination) {
38
67
  let totalMatched = 0;
39
68
  const entries = [];
@@ -127,6 +156,9 @@ class MultiEntryDb {
127
156
  }
128
157
  }
129
158
 
159
+ class SingleEntryDb {
160
+ }
161
+
130
162
  exports.FileType = void 0;
131
163
  (function (FileType) {
132
164
  FileType["File"] = "file";
@@ -143,7 +175,7 @@ class Files {
143
175
  const { recursive = false, overwrite = true } = options ?? {};
144
176
  // Check if destination exists and handle overwrite
145
177
  if (!overwrite && (await this.exists(destinationPath))) {
146
- throw new Error(`Destination '${destinationPath}' already exists`);
178
+ throw new FileIoError(`Destination '${destinationPath}' already exists`);
147
179
  }
148
180
  const sourceStats = await fs__namespace.stat(sourcePath);
149
181
  if (sourceStats.isFile()) {
@@ -154,7 +186,7 @@ class Files {
154
186
  }
155
187
  if (sourceStats.isDirectory()) {
156
188
  if (!recursive) {
157
- throw new Error(`'${sourcePath}' is a directory (use recursive option)`);
189
+ throw new FileIoError(`'${sourcePath}' is a directory (use recursive option)`);
158
190
  }
159
191
  await this.copyRecursive(sourcePath, destinationPath);
160
192
  }
@@ -291,7 +323,7 @@ class Files {
291
323
  ...commonMeta,
292
324
  type: exports.FileType.File,
293
325
  };
294
- throw new Error(`File at ${filepath} is not normal file or directory`);
326
+ throw new FileIoError(`File at ${filepath} is not normal file or directory`);
295
327
  }
296
328
  async list(dirpath, options) {
297
329
  const { depth = 0, stripBasepath } = options ?? {};
@@ -344,6 +376,10 @@ class Files {
344
376
  }
345
377
 
346
378
  class MultiEntryFileDb extends MultiEntryDb {
379
+ dirpath;
380
+ files;
381
+ parser;
382
+ noPathlikeIds;
347
383
  constructor(dirpath, options) {
348
384
  super();
349
385
  this.dirpath = dirpath;
@@ -357,7 +393,7 @@ class MultiEntryFileDb extends MultiEntryDb {
357
393
  }
358
394
  async getById(id) {
359
395
  if (!this.isIdValid(id))
360
- throw new Error(`Invalid id: ${id}`);
396
+ throw new InvalidIdError(id);
361
397
  try {
362
398
  const filepath = this.getFilePath(id);
363
399
  const text = await this.files.read(filepath);
@@ -402,7 +438,7 @@ class MultiEntryFileDb extends MultiEntryDb {
402
438
  }
403
439
  async writeEntry(entry) {
404
440
  if (!this.isIdValid(entry.id))
405
- throw new Error(`Invalid id: ${entry.id}`);
441
+ throw new InvalidIdError(entry.id);
406
442
  const filepath = this.getFilePath(entry.id);
407
443
  await this.files.write(filepath, JSON.stringify(entry, null, 2));
408
444
  }
@@ -434,11 +470,14 @@ class MultiEntryFileDb extends MultiEntryDb {
434
470
  }
435
471
  }
436
472
 
437
- class SingleEntryFileDb {
473
+ class SingleEntryFileDb extends SingleEntryDb {
474
+ filepath;
475
+ parser;
476
+ files = new Files();
438
477
  constructor(filepath, parser = JSON) {
478
+ super();
439
479
  this.filepath = filepath;
440
480
  this.parser = parser;
441
- this.files = new Files();
442
481
  }
443
482
  path() {
444
483
  return this.filepath;
@@ -472,10 +511,7 @@ class SingleEntryFileDb {
472
511
  }
473
512
 
474
513
  class MultiEntryMemDb extends MultiEntryDb {
475
- constructor() {
476
- super(...arguments);
477
- this.entries = new Map();
478
- }
514
+ entries = new Map();
479
515
  async create(entry) {
480
516
  this.entries.set(entry.id, entry);
481
517
  return entry;
@@ -508,28 +544,40 @@ class MultiEntryMemDb extends MultiEntryDb {
508
544
  yield id;
509
545
  }
510
546
  }
547
+ async persist(dirpath) {
548
+ const fileDb = new MultiEntryFileDb(dirpath);
549
+ await fileDb.destroy();
550
+ const values = [...this.entries.values()];
551
+ await Promise.all(values.map((entry) => fileDb.create(entry)));
552
+ }
553
+ async load(dirpath, parser) {
554
+ const fileDb = new MultiEntryFileDb(dirpath, { parser });
555
+ for await (const entry of fileDb.iterEntries()) {
556
+ await this.create(entry);
557
+ }
558
+ }
511
559
  }
512
560
 
513
- class SingleEntryMemDb {
561
+ class SingleEntryMemDb extends SingleEntryDb {
562
+ entry = null;
514
563
  constructor(initialEntry = null) {
515
- this.entry = null;
564
+ super();
516
565
  this.entry = initialEntry;
517
566
  }
518
- isInited() {
567
+ async isInited() {
519
568
  return this.entry !== null;
520
569
  }
521
- read() {
570
+ async read() {
522
571
  if (this.entry === null)
523
- throw new Error('Entry not initialized');
572
+ throw new UninitError();
524
573
  return this.entry;
525
574
  }
526
- write(updaterOrEntry) {
575
+ async write(updaterOrEntry) {
527
576
  let entry;
528
577
  if (typeof updaterOrEntry === 'function') {
529
578
  const updater = updaterOrEntry;
530
- if (this.entry === null) {
531
- throw new Error('Cannot update uninitialized entry. Use write(entry) to initialize first.');
532
- }
579
+ if (this.entry === null)
580
+ throw new UninitError();
533
581
  const updatedFields = updater(this.entry);
534
582
  entry = { ...this.entry, ...updatedFields };
535
583
  }
@@ -539,13 +587,28 @@ class SingleEntryMemDb {
539
587
  this.entry = entry;
540
588
  return entry;
541
589
  }
542
- delete() {
590
+ async delete() {
543
591
  this.entry = null;
544
592
  }
593
+ async persist(filepath) {
594
+ const fileDb = new SingleEntryFileDb(filepath);
595
+ await fileDb.write(await this.read());
596
+ }
597
+ async load(filepath, parser) {
598
+ const fileDb = new SingleEntryFileDb(filepath, parser);
599
+ const persistedEntry = await fileDb.read();
600
+ this.write(persistedEntry);
601
+ }
545
602
  }
546
603
 
604
+ exports.ConflictError = ConflictError;
605
+ exports.DbError = DbError;
606
+ exports.FileIoError = FileIoError;
607
+ exports.InvalidIdError = InvalidIdError;
547
608
  exports.MultiEntryFileDb = MultiEntryFileDb;
548
609
  exports.MultiEntryMemDb = MultiEntryMemDb;
610
+ exports.NotFoundError = NotFoundError;
549
611
  exports.SingleEntryFileDb = SingleEntryFileDb;
550
612
  exports.SingleEntryMemDb = SingleEntryMemDb;
613
+ exports.UninitError = UninitError;
551
614
  //# sourceMappingURL=index.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.cjs","sources":["../../../src/common/multiEntryDb.ts","../../../src/common/types.ts","../../../src/file/files.ts","../../../src/file/multiEntryFileDb.ts","../../../src/file/singleEntryFileDb.ts","../../../src/memory/multiEntryMemDb.ts","../../../src/memory/singleEntryMemDb.ts"],"sourcesContent":["import type {\n Identifiable,\n DeleteManyOutput,\n PaginationInput,\n PredicateFn,\n Promisable,\n} from './types'\n\nexport abstract class MultiEntryDb<T extends Identifiable> {\n abstract create(entry: T): Promise<T>\n\n abstract getById(id: T['id']): Promise<T | null>\n\n abstract update(id: T['id'], updater: (entry: T) => Promisable<Partial<T>>): Promise<T>\n\n abstract deleteById(id: T['id']): Promise<boolean>\n\n abstract destroy(): Promise<void>\n\n protected abstract iterEntries(): AsyncIterable<T>\n\n protected abstract iterIds(): AsyncIterable<T['id']>\n\n async getByIdOrThrow(id: T['id']): Promise<T> {\n const entry = await this.getById(id)\n if (!entry) throw new Error(`Entry with id '${id}' does not exist`)\n return entry\n }\n\n async getWhere(predicate: PredicateFn<T>, pagination?: PaginationInput): Promise<T[]> {\n let totalMatched = 0\n const entries: T[] = []\n\n if (!pagination) {\n for await (const entry of this.iterEntries()) {\n const isMatch = predicate(entry)\n if (isMatch) entries.push(entry)\n }\n return entries\n }\n\n const { take, page } = pagination\n const skip = pagination.skip ?? 0\n const startIndex = (page - 1) * take + skip\n const endIndex = startIndex + take\n\n for await (const entry of this.iterEntries()) {\n const isMatch = predicate(entry)\n if (isMatch) {\n if (totalMatched >= startIndex && totalMatched < endIndex) {\n entries.push(entry)\n }\n totalMatched++\n\n if (totalMatched >= endIndex) break\n }\n }\n\n return entries\n }\n\n getAll(): Promise<T[]> {\n return this.getWhere(() => true)\n }\n\n async getAllIds(): Promise<T['id'][]> {\n const ids: T['id'][] = []\n for await (const id of this.iterIds()) {\n ids.push(id)\n }\n return ids\n }\n\n deleteByIds(ids: T['id'][]): Promise<DeleteManyOutput> {\n return this.deleteWhere((entry) => ids.includes(entry.id))\n }\n\n async deleteWhere(predicate: PredicateFn<T>): Promise<DeleteManyOutput> {\n const deletedIds: T['id'][] = []\n const ignoredIds: T['id'][] = []\n\n for await (const entry of this.iterEntries()) {\n if (!predicate(entry)) continue\n\n const didDelete = await this.deleteById(entry.id)\n if (didDelete) {\n deletedIds.push(entry.id)\n } else {\n ignoredIds.push(entry.id)\n }\n }\n\n return { deletedIds, ignoredIds }\n }\n\n async exists(id: T['id']): Promise<boolean> {\n const entry = await this.getById(id)\n return entry !== null\n }\n\n countAll(): Promise<number> {\n return this.countWhere(() => true)\n }\n\n async countWhere(predicate: PredicateFn<T>, pagination?: PaginationInput): Promise<number> {\n let totalMatched = 0\n let count = 0\n\n if (!pagination) {\n for await (const entry of this.iterEntries()) {\n const isMatch = predicate(entry)\n if (isMatch) count++\n }\n return count\n }\n\n const { take, page } = pagination\n const skip = pagination.skip ?? 0\n const startIndex = (page - 1) * take + skip\n const endIndex = startIndex + take\n\n for await (const entry of this.iterEntries()) {\n const isMatch = predicate(entry)\n if (isMatch) {\n if (totalMatched >= startIndex && totalMatched < endIndex) count++\n totalMatched++\n\n if (totalMatched >= endIndex) break\n }\n }\n\n return count\n }\n}\n","export type Identifiable = {\n id: Id\n}\n\nexport type Id = string\n\nexport type Promisable<T> = T | Promise<T>\n\nexport type PaginationInput = {\n take: number\n page: number\n skip?: number | undefined | null\n}\n\nexport type DeleteManyOutput = {\n deletedIds: Id[]\n ignoredIds: Id[]\n}\n\nexport type PredicateFn<T extends Identifiable> = (entry: T) => boolean\n\nexport type JsonEntryParser<T> = {\n parse: (text: string) => T\n}\n\nexport type MultiEntryFileDbOptions<T> = {\n noPathlikeIds?: boolean\n parser?: JsonEntryParser<T>\n}\n\nexport type FileMeta = {\n path: string\n size: number\n created: Date\n modified: Date\n accessed: Date\n} & (\n | {\n type: FileType.File\n }\n | {\n type: FileType.Directory\n children: FileMeta[]\n }\n)\n\nexport enum FileType {\n File = 'file',\n Directory = 'directory',\n //Symlink: 'symlink'\n}\n","import type { FileMeta } from '../common/types'\nimport { FileType } from '../common/types'\nimport * as fs from 'fs/promises'\nimport * as path from 'path'\nimport * as fsSync from 'fs'\nimport * as readline from 'readline'\n\ntype ListOptions = {\n depth?: number\n stripBasepath?: string\n}\n\ntype DeleteOptions = {\n force?: boolean\n recursive?: boolean\n}\n\ntype WriteOptions = {\n encoding?: BufferEncoding\n}\n\ntype ReadOptions = {\n encoding?: BufferEncoding\n lines?: number\n}\n\ntype CopyOptions = {\n recursive?: boolean\n overwrite?: boolean\n}\n\nconst DEFUALT_ENCODING: BufferEncoding = 'utf-8'\n\nexport class Files {\n async move(oldPath: string, newPath: string) {\n await fs.rename(oldPath, newPath)\n }\n\n async copy(sourcePath: string, destinationPath: string, options?: CopyOptions) {\n const { recursive = false, overwrite = true } = options ?? {}\n\n // Check if destination exists and handle overwrite\n if (!overwrite && (await this.exists(destinationPath))) {\n throw new Error(`Destination '${destinationPath}' already exists`)\n }\n\n const sourceStats = await fs.stat(sourcePath)\n\n if (sourceStats.isFile()) {\n const destinationDir = path.dirname(destinationPath)\n await this.mkdirUnsafe(destinationDir)\n await fs.copyFile(sourcePath, destinationPath)\n return\n }\n\n if (sourceStats.isDirectory()) {\n if (!recursive) {\n throw new Error(`'${sourcePath}' is a directory (use recursive option)`)\n }\n await this.copyRecursive(sourcePath, destinationPath)\n }\n }\n\n async copyRecursive(sourcePath: string, destinationPath: string) {\n const sourceStats = await fs.stat(sourcePath)\n\n if (sourceStats.isFile()) {\n const destinationDir = path.dirname(destinationPath)\n await this.mkdirUnsafe(destinationDir)\n await fs.copyFile(sourcePath, destinationPath)\n return\n }\n\n if (sourceStats.isDirectory()) {\n await this.mkdirUnsafe(destinationPath)\n const entries = await fs.readdir(sourcePath)\n\n for (const entry of entries) {\n const sourceEntryPath = path.join(sourcePath, entry)\n const destEntryPath = path.join(destinationPath, entry)\n await this.copyRecursive(sourceEntryPath, destEntryPath)\n }\n }\n }\n\n async mkdir(filepath: string) {\n await this.mkdirUnsafe(filepath)\n }\n\n protected async mkdirUnsafe(filepath: string) {\n await fs.mkdir(filepath, { recursive: true })\n }\n\n async write(filepath: string, content: string, options?: WriteOptions) {\n const { encoding = DEFUALT_ENCODING } = options ?? {}\n\n const parentDirs = path.dirname(filepath)\n await this.mkdirUnsafe(parentDirs)\n\n await fs.writeFile(filepath, content, encoding)\n }\n\n async touch(filepath: string) {\n const exists = await this.exists(filepath)\n if (!exists) await this.write(filepath, '')\n }\n\n async read(filepath: string, options?: ReadOptions) {\n const { encoding = DEFUALT_ENCODING, lines } = options ?? {}\n\n if (lines === undefined) {\n return await fs.readFile(filepath, encoding)\n }\n\n const stream = fsSync.createReadStream(filepath, { encoding })\n const reader = readline.createInterface({\n input: stream,\n crlfDelay: Infinity,\n })\n\n let result = ''\n let linesRead = 0\n for await (const line of reader) {\n if (linesRead >= lines) break\n result += line + '\\n'\n linesRead++\n }\n\n return result\n }\n\n async delete(filepath: string, options?: DeleteOptions) {\n const { force = true, recursive = true } = options ?? {}\n await fs.rm(filepath, { force, recursive })\n }\n\n async exists(filepath: string) {\n try {\n await fs.access(filepath)\n return true\n } catch {\n return false\n }\n }\n\n async isDir(filepath: string) {\n try {\n const stats = await fs.stat(filepath)\n return stats.isDirectory()\n } catch {\n return false\n }\n }\n\n async isFile(filepath: string) {\n try {\n const stats = await fs.stat(filepath)\n return stats.isFile()\n } catch {\n return false\n }\n }\n\n async isSymlink(filepath: string) {\n try {\n const stats = await fs.stat(filepath)\n return stats.isSymbolicLink()\n } catch {\n return false\n }\n }\n\n async getMeta(filepath: string, options?: ListOptions): Promise<FileMeta> {\n const { depth = 0, stripBasepath } = options ?? {}\n const stats = await fs.stat(filepath)\n const isDir = stats.isDirectory()\n const isFile = stats.isFile()\n\n const { size, ctime, mtime, atime } = stats\n const commonMeta = {\n path: this.stripBasepath(filepath, stripBasepath),\n size,\n created: ctime,\n modified: mtime,\n accessed: atime,\n }\n\n if (isDir) {\n const children: FileMeta[] = []\n\n if (depth > 0) {\n const childNames = await this.list(filepath)\n\n for (const child of childNames) {\n const childDepth = Math.max(0, depth - 1)\n const childMeta = await this.getMeta(path.join(filepath, child), {\n depth: childDepth,\n stripBasepath,\n })\n children.push(childMeta)\n }\n }\n\n return {\n ...commonMeta,\n type: FileType.Directory,\n children,\n }\n }\n if (isFile)\n return {\n ...commonMeta,\n type: FileType.File,\n }\n\n throw new Error(`File at ${filepath} is not normal file or directory`)\n }\n\n async list(dirpath: string, options?: ListOptions) {\n const { depth = 0, stripBasepath } = options ?? {}\n if (depth > 0) {\n return await this.listRecursive(dirpath, depth, 0, stripBasepath)\n }\n return await fs.readdir(dirpath)\n }\n\n async *listRead(dirpath: string, options?: ListOptions) {\n const { depth = 0 } = options ?? {}\n\n const files = await this.list(dirpath, { depth })\n\n for (const filepath of files) {\n const isFile = await this.isFile(filepath)\n if (!isFile) continue\n\n try {\n const content = await this.read(filepath)\n yield {\n filepath,\n content,\n }\n } catch {\n // Skip files that can't be read\n continue\n }\n }\n }\n\n protected async listRecursive(\n dirpath: string,\n maxDepth: number,\n currentDepth: number,\n stripBasepath: string | undefined,\n ): Promise<string[]> {\n const results: string[] = []\n const entries = await fs.readdir(dirpath, { withFileTypes: true })\n\n for (const entry of entries) {\n const fullPath = path.join(dirpath, entry.name)\n const strippedPath = this.stripBasepath(fullPath, stripBasepath)\n results.push(strippedPath)\n\n if (entry.isDirectory() && currentDepth < maxDepth) {\n const subResults = await this.listRecursive(\n fullPath,\n maxDepth,\n currentDepth + 1,\n stripBasepath,\n )\n results.push(...subResults)\n }\n }\n\n return results\n }\n\n protected stripBasepath(original: string, pathToStrip: string | undefined): string {\n if (!pathToStrip) return original\n const base = pathToStrip.replace(/^\\/+/, '/').replace(/\\/+$/, '/')\n const stripped = original.replace(base, '')\n return `/${stripped}`\n }\n}\n","import {\n Identifiable,\n DeleteManyOutput,\n Promisable,\n JsonEntryParser,\n MultiEntryFileDbOptions,\n MultiEntryDb,\n} from '../common'\nimport { Files } from './files'\nimport * as path from 'path'\n\nexport class MultiEntryFileDb<T extends Identifiable> extends MultiEntryDb<T> {\n protected readonly dirpath: string\n protected readonly files: Files\n protected readonly parser: JsonEntryParser<T>\n readonly noPathlikeIds: boolean\n\n constructor(dirpath: string, options?: MultiEntryFileDbOptions<T>) {\n super()\n this.dirpath = dirpath\n this.files = new Files()\n this.parser = options?.parser ?? JSON\n this.noPathlikeIds = options?.noPathlikeIds ?? true\n }\n\n async create(entry: T): Promise<T> {\n await this.writeEntry(entry)\n return entry\n }\n\n async getById(id: T['id']): Promise<T | null> {\n if (!this.isIdValid(id)) throw new Error(`Invalid id: ${id}`)\n\n try {\n const filepath = this.getFilePath(id)\n const text = await this.files.read(filepath)\n const entry = this.parser.parse(text)\n return entry\n } catch (error) {\n console.error('Failed to read entry', error)\n // File doesn't exist or invalid JSON\n return null\n }\n }\n\n async update(id: T['id'], updater: (entry: T) => Promisable<Partial<T>>): Promise<T> {\n const entry = await this.getByIdOrThrow(id)\n\n const updatedEntryFields = await updater(entry)\n const updatedEntry = { ...entry, ...updatedEntryFields }\n await this.writeEntry(updatedEntry)\n\n if (updatedEntry.id !== id) {\n await this.deleteById(id)\n }\n\n return updatedEntry\n }\n\n async deleteById(id: T['id']): Promise<boolean> {\n try {\n const filepath = this.getFilePath(id)\n await this.files.delete(filepath, { force: false })\n return true\n } catch {\n // File might not exist, ignore error\n return false\n }\n }\n\n async deleteByIds(ids: T['id'][]): Promise<DeleteManyOutput> {\n return this.deleteWhere((entry) => ids.includes(entry.id))\n }\n\n async destroy() {\n await this.files.delete(this.dirpath)\n }\n\n protected getFilePath(id: T['id']) {\n return path.join(this.dirpath, `${id}.json`)\n }\n\n protected async writeEntry(entry: T) {\n if (!this.isIdValid(entry.id)) throw new Error(`Invalid id: ${entry.id}`)\n\n const filepath = this.getFilePath(entry.id)\n await this.files.write(filepath, JSON.stringify(entry, null, 2))\n }\n\n isIdValid(id: T['id']): boolean {\n if (typeof id !== 'string') return false\n\n if (!this.noPathlikeIds) return true\n\n if (id.includes('/') || id.includes('\\\\')) return false\n\n return true\n }\n\n protected async *iterEntries() {\n for await (const id of this.iterIds()) {\n const entry = await this.getById(id)\n if (entry) yield entry\n }\n }\n\n protected async *iterIds() {\n const filenames = await this.files.list(this.dirpath)\n for (const filename of filenames) {\n if (!filename.endsWith('.json')) continue\n\n const id = filename.replace(/\\.json$/, '')\n if (this.isIdValid(id)) yield id\n }\n }\n}\n","import { JsonEntryParser, Promisable } from '../common/types'\nimport { Files } from './files'\n\nexport class SingleEntryFileDb<T> {\n protected readonly files: Files = new Files()\n\n constructor(\n protected readonly filepath: string,\n protected readonly parser: JsonEntryParser<T> = JSON,\n ) {}\n\n path() {\n return this.filepath\n }\n\n async isInited() {\n const exists = await this.files.exists(this.filepath)\n return exists\n }\n\n async read() {\n const text = await this.files.read(this.filepath)\n const entry = this.parser.parse(text)\n return entry\n }\n\n async write(updaterOrEntry: T | ((entry: T) => Promisable<Partial<T>>)): Promise<T> {\n let entry: T\n if (typeof updaterOrEntry === 'function') {\n const updater = updaterOrEntry as (entry: T) => T\n const existing = await this.read()\n\n const updatedFields = await updater(existing)\n entry = { ...existing, ...updatedFields }\n } else {\n entry = updaterOrEntry\n }\n\n await this.files.write(this.filepath, JSON.stringify(entry, null, 2))\n\n return entry\n }\n\n async delete(): Promise<void> {\n await this.files.delete(this.filepath)\n }\n}\n","import { Identifiable, Promisable, MultiEntryDb } from '../common'\n\nexport class MultiEntryMemDb<T extends Identifiable> extends MultiEntryDb<T> {\n protected entries: Map<T['id'], T> = new Map()\n\n async create(entry: T): Promise<T> {\n this.entries.set(entry.id, entry)\n return entry\n }\n\n async getById(id: T['id']): Promise<T | null> {\n return this.entries.get(id) ?? null\n }\n\n async update(id: T['id'], updater: (entry: T) => Promisable<Partial<T>>): Promise<T> {\n const entry = await this.getByIdOrThrow(id)\n\n const updatedEntryFields = await updater(entry)\n const updatedEntry = { ...entry, ...updatedEntryFields }\n\n this.entries.set(updatedEntry.id, updatedEntry)\n\n if (updatedEntry.id !== id) this.entries.delete(id)\n\n return updatedEntry\n }\n\n async deleteById(id: T['id']): Promise<boolean> {\n return this.entries.delete(id)\n }\n\n async destroy() {\n this.entries.clear()\n }\n\n protected async *iterEntries() {\n for (const entry of this.entries.values()) {\n yield entry\n }\n }\n\n protected async *iterIds() {\n for (const id of this.entries.keys()) {\n yield id\n }\n }\n}\n","export class SingleEntryMemDb<T> {\n protected entry: T | null = null\n\n constructor(initialEntry: T | null = null) {\n this.entry = initialEntry\n }\n\n isInited() {\n return this.entry !== null\n }\n\n read(): T {\n if (this.entry === null) throw new Error('Entry not initialized')\n return this.entry\n }\n\n write(updaterOrEntry: T | ((entry: T) => Partial<T>)): T {\n let entry: T\n\n if (typeof updaterOrEntry === 'function') {\n const updater = updaterOrEntry as (entry: T) => Partial<T>\n\n if (this.entry === null) {\n throw new Error('Cannot update uninitialized entry. Use write(entry) to initialize first.')\n }\n\n const updatedFields = updater(this.entry)\n entry = { ...this.entry, ...updatedFields }\n } else {\n entry = updaterOrEntry\n }\n\n this.entry = entry\n return entry\n }\n\n delete() {\n this.entry = null\n }\n}\n"],"names":["FileType","fs","path","fsSync","readline"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;MAQsB,YAAY,CAAA;IAehC,MAAM,cAAc,CAAC,EAAW,EAAA;QAC9B,MAAM,KAAK,GAAG,MAAM,IAAI,CAAC,OAAO,CAAC,EAAE,CAAC;AACpC,QAAA,IAAI,CAAC,KAAK;AAAE,YAAA,MAAM,IAAI,KAAK,CAAC,kBAAkB,EAAE,CAAA,gBAAA,CAAkB,CAAC;AACnE,QAAA,OAAO,KAAK;IACd;AAEA,IAAA,MAAM,QAAQ,CAAC,SAAyB,EAAE,UAA4B,EAAA;QACpE,IAAI,YAAY,GAAG,CAAC;QACpB,MAAM,OAAO,GAAQ,EAAE;QAEvB,IAAI,CAAC,UAAU,EAAE;YACf,WAAW,MAAM,KAAK,IAAI,IAAI,CAAC,WAAW,EAAE,EAAE;AAC5C,gBAAA,MAAM,OAAO,GAAG,SAAS,CAAC,KAAK,CAAC;AAChC,gBAAA,IAAI,OAAO;AAAE,oBAAA,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC;YAClC;AACA,YAAA,OAAO,OAAO;QAChB;AAEA,QAAA,MAAM,EAAE,IAAI,EAAE,IAAI,EAAE,GAAG,UAAU;AACjC,QAAA,MAAM,IAAI,GAAG,U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uBAAuB,CAAC;QACjE,OAAO,IAAI,CAAC,KAAK;IACnB;AAEA,IAAA,KAAK,CAAC,cAA8C,EAAA;AAClD,QAAA,IAAI,KAAQ;AAEZ,QAAA,IAAI,OAAO,cAAc,KAAK,UAAU,EAAE;YACxC,MAAM,OAAO,GAAG,cAA0C;AAE1D,YAAA,IAAI,IAAI,CAAC,KAAK,KAAK,IAAI,EAAE;AACvB,gBAAA,MAAM,IAAI,KAAK,CAAC,0EAA0E,CAAC;YAC7F;YAEA,MAAM,aAAa,GAAG,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC;YACzC,KAAK,GAAG,EAAE,GAAG,IAAI,CAAC,KAAK,EAAE,GAAG,aAAa,EAAE;QAC7C;aAAO;YACL,KAAK,GAAG,cAAc;QACxB;AAEA,QAAA,IAAI,CAAC,KAAK,GAAG,KAAK;AAClB,QAAA,OAAO,KAAK;IACd;IAEA,MAAM,GAAA;AACJ,QAAA,IAAI,CAAC,KAAK,GAAG,IAAI;IACnB;AACD;;;;;;;"}
1
+ {"version":3,"file":"index.cjs","sources":["../../../src/common/errors.ts","../../../src/common/multiEntryDb.ts","../../../src/common/singleEntryDb.ts","../../../src/common/types.ts","../../../src/file/files.ts","../../../src/file/multiEntryFileDb.ts","../../../src/file/singleEntryFileDb.ts","../../../src/memory/multiEntryMemDb.ts","../../../src/memory/singleEntryMemDb.ts"],"sourcesContent":["export class DbError extends Error {}\n\nexport class NotFoundError extends DbError {}\n\nexport class ConflictError extends DbError {}\n\nexport class FileIoError extends DbError {}\n\nexport class InvalidIdError extends DbError {\n constructor(id: unknown) {\n super(`Invalid entry id '${id}'`)\n }\n}\n\nexport class UninitError extends DbError {\n constructor() {\n super('Cannot read or update uninitialized entry. Use write(entry) to initialize first.')\n }\n}\n","import { NotFoundError } from './errors'\nimport type {\n Identifiable,\n DeleteManyOutput,\n PaginationInput,\n PredicateFn,\n UpdaterFn,\n} from './types'\n\nexport abstract class MultiEntryDb<T extends Identifiable> {\n abstract create(entry: T): Promise<T>\n\n abstract getById(id: T['id']): Promise<T | null>\n\n abstract update(id: T['id'], updater: UpdaterFn<T>): Promise<T>\n\n abstract deleteById(id: T['id']): Promise<boolean>\n\n abstract destroy(): Promise<void>\n\n abstract iterEntries(): AsyncIterable<T>\n\n abstract iterIds(): AsyncIterable<T['id']>\n\n async getByIdOrThrow(id: T['id']): Promise<T> {\n const entry = await this.getById(id)\n if (!entry) throw new NotFoundError(`Entry with id '${id}' does not exist`)\n return entry\n }\n\n async getFirstWhere(predicate: PredicateFn<T>): Promise<T | null> {\n const matches = await this.getWhere(predicate, { page: 0, take: 1 })\n return matches.at(0) ?? null\n }\n\n async getFirstWhereOrThrow(predicate: PredicateFn<T>): Promise<T | null> {\n const match = await this.getFirstWhere(predicate)\n if (!match) throw new NotFoundError('No entry matches predicate')\n return match\n }\n\n async getWhere(predicate: PredicateFn<T>, pagination?: PaginationInput): Promise<T[]> {\n let totalMatched = 0\n const entries: T[] = []\n\n if (!pagination) {\n for await (const entry of this.iterEntries()) {\n const isMatch = predicate(entry)\n if (isMatch) entries.push(entry)\n }\n return entries\n }\n\n const { take, page } = pagination\n const skip = pagination.skip ?? 0\n const startIndex = (page - 1) * take + skip\n const endIndex = startIndex + take\n\n for await (const entry of this.iterEntries()) {\n const isMatch = predicate(entry)\n if (isMatch) {\n if (totalMatched >= startIndex && totalMatched < endIndex) {\n entries.push(entry)\n }\n totalMatched++\n\n if (totalMatched >= endIndex) break\n }\n }\n\n return entries\n }\n\n getAll(): Promise<T[]> {\n return this.getWhere(() => true)\n }\n\n async getAllIds(): Promise<T['id'][]> {\n const ids: T['id'][] = []\n for await (const id of this.iterIds()) {\n ids.push(id)\n }\n return ids\n }\n\n deleteByIds(ids: T['id'][]): Promise<DeleteManyOutput> {\n return this.deleteWhere((entry) => ids.includes(entry.id))\n }\n\n async deleteWhere(predicate: PredicateFn<T>): Promise<DeleteManyOutput> {\n const deletedIds: T['id'][] = []\n const ignoredIds: T['id'][] = []\n\n for await (const entry of this.iterEntries()) {\n if (!predicate(entry)) continue\n\n const didDelete = await this.deleteById(entry.id)\n if (didDelete) {\n deletedIds.push(entry.id)\n } else {\n ignoredIds.push(entry.id)\n }\n }\n\n return { deletedIds, ignoredIds }\n }\n\n async exists(id: T['id']): Promise<boolean> {\n const entry = await this.getById(id)\n return entry !== null\n }\n\n countAll(): Promise<number> {\n return this.countWhere(() => true)\n }\n\n async countWhere(predicate: PredicateFn<T>, pagination?: PaginationInput): Promise<number> {\n let totalMatched = 0\n let count = 0\n\n if (!pagination) {\n for await (const entry of this.iterEntries()) {\n const isMatch = predicate(entry)\n if (isMatch) count++\n }\n return count\n }\n\n const { take, page } = pagination\n const skip = pagination.skip ?? 0\n const startIndex = (page - 1) * take + skip\n const endIndex = startIndex + take\n\n for await (const entry of this.iterEntries()) {\n const isMatch = predicate(entry)\n if (isMatch) {\n if (totalMatched >= startIndex && totalMatched < endIndex) count++\n totalMatched++\n\n if (totalMatched >= endIndex) break\n }\n }\n\n return count\n }\n}\n","import type { Promisable, UpdaterFn } from '../common/types'\n\nexport abstract class SingleEntryDb<T> {\n abstract isInited(): Promise<boolean>\n abstract read(): Promise<T>\n abstract write(updaterOrEntry: T | UpdaterFn<T>): Promise<T>\n abstract delete(): Promise<void>\n}\n","export type Identifiable = {\n id: Id\n}\n\nexport type Id = string\n\nexport type Promisable<T> = T | Promise<T>\n\nexport type PaginationInput = {\n take: number\n page: number\n skip?: number | undefined | null\n}\n\nexport type DeleteManyOutput = {\n deletedIds: Id[]\n ignoredIds: Id[]\n}\n\nexport type PredicateFn<T extends Identifiable> = (entry: T) => boolean\n\nexport type UpdaterFn<T> = (entry: T) => Promisable<Partial<T>>\n\nexport type JsonEntryParser<T> = {\n parse: (text: string) => T\n}\n\nexport type MultiEntryFileDbOptions<T> = {\n noPathlikeIds?: boolean\n parser?: JsonEntryParser<T>\n}\n\nexport type FileMeta = {\n path: string\n size: number\n created: Date\n modified: Date\n accessed: Date\n} & (\n | {\n type: FileType.File\n }\n | {\n type: FileType.Directory\n children: FileMeta[]\n }\n)\n\nexport enum FileType {\n File = 'file',\n Directory = 'directory',\n //Symlink: 'symlink'\n}\n","import type { FileMeta } from '../common'\nimport { FileType, FileIoError } from '../common'\nimport * as fs from 'fs/promises'\nimport * as path from 'path'\nimport * as fsSync from 'fs'\nimport * as readline from 'readline'\n\ntype ListOptions = {\n depth?: number\n stripBasepath?: string\n}\n\ntype DeleteOptions = {\n force?: boolean\n recursive?: boolean\n}\n\ntype WriteOptions = {\n encoding?: BufferEncoding\n}\n\ntype ReadOptions = {\n encoding?: BufferEncoding\n lines?: number\n}\n\ntype CopyOptions = {\n recursive?: boolean\n overwrite?: boolean\n}\n\nconst DEFUALT_ENCODING: BufferEncoding = 'utf-8'\n\nexport class Files {\n async move(oldPath: string, newPath: string) {\n await fs.rename(oldPath, newPath)\n }\n\n async copy(sourcePath: string, destinationPath: string, options?: CopyOptions) {\n const { recursive = false, overwrite = true } = options ?? {}\n\n // Check if destination exists and handle overwrite\n if (!overwrite && (await this.exists(destinationPath))) {\n throw new FileIoError(`Destination '${destinationPath}' already exists`)\n }\n\n const sourceStats = await fs.stat(sourcePath)\n\n if (sourceStats.isFile()) {\n const destinationDir = path.dirname(destinationPath)\n await this.mkdirUnsafe(destinationDir)\n await fs.copyFile(sourcePath, destinationPath)\n return\n }\n\n if (sourceStats.isDirectory()) {\n if (!recursive) {\n throw new FileIoError(`'${sourcePath}' is a directory (use recursive option)`)\n }\n await this.copyRecursive(sourcePath, destinationPath)\n }\n }\n\n async copyRecursive(sourcePath: string, destinationPath: string) {\n const sourceStats = await fs.stat(sourcePath)\n\n if (sourceStats.isFile()) {\n const destinationDir = path.dirname(destinationPath)\n await this.mkdirUnsafe(destinationDir)\n await fs.copyFile(sourcePath, destinationPath)\n return\n }\n\n if (sourceStats.isDirectory()) {\n await this.mkdirUnsafe(destinationPath)\n const entries = await fs.readdir(sourcePath)\n\n for (const entry of entries) {\n const sourceEntryPath = path.join(sourcePath, entry)\n const destEntryPath = path.join(destinationPath, entry)\n await this.copyRecursive(sourceEntryPath, destEntryPath)\n }\n }\n }\n\n async mkdir(filepath: string) {\n await this.mkdirUnsafe(filepath)\n }\n\n protected async mkdirUnsafe(filepath: string) {\n await fs.mkdir(filepath, { recursive: true })\n }\n\n async write(filepath: string, content: string, options?: WriteOptions) {\n const { encoding = DEFUALT_ENCODING } = options ?? {}\n\n const parentDirs = path.dirname(filepath)\n await this.mkdirUnsafe(parentDirs)\n\n await fs.writeFile(filepath, content, encoding)\n }\n\n async touch(filepath: string) {\n const exists = await this.exists(filepath)\n if (!exists) await this.write(filepath, '')\n }\n\n async read(filepath: string, options?: ReadOptions) {\n const { encoding = DEFUALT_ENCODING, lines } = options ?? {}\n\n if (lines === undefined) {\n return await fs.readFile(filepath, encoding)\n }\n\n const stream = fsSync.createReadStream(filepath, { encoding })\n const reader = readline.createInterface({\n input: stream,\n crlfDelay: Infinity,\n })\n\n let result = ''\n let linesRead = 0\n for await (const line of reader) {\n if (linesRead >= lines) break\n result += line + '\\n'\n linesRead++\n }\n\n return result\n }\n\n async delete(filepath: string, options?: DeleteOptions) {\n const { force = true, recursive = true } = options ?? {}\n await fs.rm(filepath, { force, recursive })\n }\n\n async exists(filepath: string) {\n try {\n await fs.access(filepath)\n return true\n } catch {\n return false\n }\n }\n\n async isDir(filepath: string) {\n try {\n const stats = await fs.stat(filepath)\n return stats.isDirectory()\n } catch {\n return false\n }\n }\n\n async isFile(filepath: string) {\n try {\n const stats = await fs.stat(filepath)\n return stats.isFile()\n } catch {\n return false\n }\n }\n\n async isSymlink(filepath: string) {\n try {\n const stats = await fs.stat(filepath)\n return stats.isSymbolicLink()\n } catch {\n return false\n }\n }\n\n async getMeta(filepath: string, options?: ListOptions): Promise<FileMeta> {\n const { depth = 0, stripBasepath } = options ?? {}\n const stats = await fs.stat(filepath)\n const isDir = stats.isDirectory()\n const isFile = stats.isFile()\n\n const { size, ctime, mtime, atime } = stats\n const commonMeta = {\n path: this.stripBasepath(filepath, stripBasepath),\n size,\n created: ctime,\n modified: mtime,\n accessed: atime,\n }\n\n if (isDir) {\n const children: FileMeta[] = []\n\n if (depth > 0) {\n const childNames = await this.list(filepath)\n\n for (const child of childNames) {\n const childDepth = Math.max(0, depth - 1)\n const childMeta = await this.getMeta(path.join(filepath, child), {\n depth: childDepth,\n stripBasepath,\n })\n children.push(childMeta)\n }\n }\n\n return {\n ...commonMeta,\n type: FileType.Directory,\n children,\n }\n }\n if (isFile)\n return {\n ...commonMeta,\n type: FileType.File,\n }\n\n throw new FileIoError(`File at ${filepath} is not normal file or directory`)\n }\n\n async list(dirpath: string, options?: ListOptions) {\n const { depth = 0, stripBasepath } = options ?? {}\n if (depth > 0) {\n return await this.listRecursive(dirpath, depth, 0, stripBasepath)\n }\n return await fs.readdir(dirpath)\n }\n\n async *listRead(dirpath: string, options?: ListOptions) {\n const { depth = 0 } = options ?? {}\n\n const files = await this.list(dirpath, { depth })\n\n for (const filepath of files) {\n const isFile = await this.isFile(filepath)\n if (!isFile) continue\n\n try {\n const content = await this.read(filepath)\n yield {\n filepath,\n content,\n }\n } catch {\n // Skip files that can't be read\n continue\n }\n }\n }\n\n protected async listRecursive(\n dirpath: string,\n maxDepth: number,\n currentDepth: number,\n stripBasepath: string | undefined,\n ): Promise<string[]> {\n const results: string[] = []\n const entries = await fs.readdir(dirpath, { withFileTypes: true })\n\n for (const entry of entries) {\n const fullPath = path.join(dirpath, entry.name)\n const strippedPath = this.stripBasepath(fullPath, stripBasepath)\n results.push(strippedPath)\n\n if (entry.isDirectory() && currentDepth < maxDepth) {\n const subResults = await this.listRecursive(\n fullPath,\n maxDepth,\n currentDepth + 1,\n stripBasepath,\n )\n results.push(...subResults)\n }\n }\n\n return results\n }\n\n protected stripBasepath(original: string, pathToStrip: string | undefined): string {\n if (!pathToStrip) return original\n const base = pathToStrip.replace(/^\\/+/, '/').replace(/\\/+$/, '/')\n const stripped = original.replace(base, '')\n return `/${stripped}`\n }\n}\n","import {\n Identifiable,\n DeleteManyOutput,\n Promisable,\n JsonEntryParser,\n MultiEntryFileDbOptions,\n MultiEntryDb,\n InvalidIdError,\n} from '../common'\nimport { Files } from './files'\nimport * as path from 'path'\n\nexport class MultiEntryFileDb<T extends Identifiable> extends MultiEntryDb<T> {\n protected readonly dirpath: string\n protected readonly files: Files\n protected readonly parser: JsonEntryParser<T>\n readonly noPathlikeIds: boolean\n\n constructor(dirpath: string, options?: MultiEntryFileDbOptions<T>) {\n super()\n this.dirpath = dirpath\n this.files = new Files()\n this.parser = options?.parser ?? JSON\n this.noPathlikeIds = options?.noPathlikeIds ?? true\n }\n\n async create(entry: T): Promise<T> {\n await this.writeEntry(entry)\n return entry\n }\n\n async getById(id: T['id']): Promise<T | null> {\n if (!this.isIdValid(id)) throw new InvalidIdError(id)\n\n try {\n const filepath = this.getFilePath(id)\n const text = await this.files.read(filepath)\n const entry = this.parser.parse(text)\n return entry\n } catch (error) {\n console.error('Failed to read entry', error)\n // File doesn't exist or invalid JSON\n return null\n }\n }\n\n async update(id: T['id'], updater: (entry: T) => Promisable<Partial<T>>): Promise<T> {\n const entry = await this.getByIdOrThrow(id)\n\n const updatedEntryFields = await updater(entry)\n const updatedEntry = { ...entry, ...updatedEntryFields }\n await this.writeEntry(updatedEntry)\n\n if (updatedEntry.id !== id) {\n await this.deleteById(id)\n }\n\n return updatedEntry\n }\n\n async deleteById(id: T['id']): Promise<boolean> {\n try {\n const filepath = this.getFilePath(id)\n await this.files.delete(filepath, { force: false })\n return true\n } catch {\n // File might not exist, ignore error\n return false\n }\n }\n\n async deleteByIds(ids: T['id'][]): Promise<DeleteManyOutput> {\n return this.deleteWhere((entry) => ids.includes(entry.id))\n }\n\n async destroy() {\n await this.files.delete(this.dirpath)\n }\n\n protected getFilePath(id: T['id']) {\n return path.join(this.dirpath, `${id}.json`)\n }\n\n protected async writeEntry(entry: T) {\n if (!this.isIdValid(entry.id)) throw new InvalidIdError(entry.id)\n\n const filepath = this.getFilePath(entry.id)\n await this.files.write(filepath, JSON.stringify(entry, null, 2))\n }\n\n isIdValid(id: T['id']): boolean {\n if (typeof id !== 'string') return false\n\n if (!this.noPathlikeIds) return true\n\n if (id.includes('/') || id.includes('\\\\')) return false\n\n return true\n }\n\n async *iterEntries() {\n for await (const id of this.iterIds()) {\n const entry = await this.getById(id)\n if (entry) yield entry\n }\n }\n\n async *iterIds() {\n const filenames = await this.files.list(this.dirpath)\n for (const filename of filenames) {\n if (!filename.endsWith('.json')) continue\n\n const id = filename.replace(/\\.json$/, '')\n if (this.isIdValid(id)) yield id\n }\n }\n}\n","import { SingleEntryDb, JsonEntryParser, Promisable } from '../common'\nimport { Files } from './files'\n\nexport class SingleEntryFileDb<T> extends SingleEntryDb<T> {\n protected readonly files: Files = new Files()\n\n constructor(\n protected readonly filepath: string,\n protected readonly parser: JsonEntryParser<T> = JSON,\n ) {\n super()\n }\n\n path() {\n return this.filepath\n }\n\n async isInited() {\n const exists = await this.files.exists(this.filepath)\n return exists\n }\n\n async read() {\n const text = await this.files.read(this.filepath)\n const entry = this.parser.parse(text)\n return entry\n }\n\n async write(updaterOrEntry: T | ((entry: T) => Promisable<Partial<T>>)): Promise<T> {\n let entry: T\n if (typeof updaterOrEntry === 'function') {\n const updater = updaterOrEntry as (entry: T) => T\n const existing = await this.read()\n\n const updatedFields = await updater(existing)\n entry = { ...existing, ...updatedFields }\n } else {\n entry = updaterOrEntry\n }\n\n await this.files.write(this.filepath, JSON.stringify(entry, null, 2))\n\n return entry\n }\n\n async delete(): Promise<void> {\n await this.files.delete(this.filepath)\n }\n}\n","import { Identifiable, Promisable, MultiEntryDb, JsonEntryParser } from '../common'\nimport { MultiEntryFileDb } from '../file/multiEntryFileDb'\n\nexport class MultiEntryMemDb<T extends Identifiable> extends MultiEntryDb<T> {\n protected entries: Map<T['id'], T> = new Map()\n\n async create(entry: T): Promise<T> {\n this.entries.set(entry.id, entry)\n return entry\n }\n\n async getById(id: T['id']): Promise<T | null> {\n return this.entries.get(id) ?? null\n }\n\n async update(id: T['id'], updater: (entry: T) => Promisable<Partial<T>>): Promise<T> {\n const entry = await this.getByIdOrThrow(id)\n\n const updatedEntryFields = await updater(entry)\n const updatedEntry = { ...entry, ...updatedEntryFields }\n\n this.entries.set(updatedEntry.id, updatedEntry)\n\n if (updatedEntry.id !== id) this.entries.delete(id)\n\n return updatedEntry\n }\n\n async deleteById(id: T['id']): Promise<boolean> {\n return this.entries.delete(id)\n }\n\n async destroy() {\n this.entries.clear()\n }\n\n async *iterEntries() {\n for (const entry of this.entries.values()) {\n yield entry\n }\n }\n\n async *iterIds() {\n for (const id of this.entries.keys()) {\n yield id\n }\n }\n\n async persist(dirpath: string) {\n const fileDb = new MultiEntryFileDb<T>(dirpath)\n await fileDb.destroy()\n\n const values = [...this.entries.values()]\n await Promise.all(values.map((entry) => fileDb.create(entry)))\n }\n\n async load(dirpath: string, parser?: JsonEntryParser<T>) {\n const fileDb = new MultiEntryFileDb<T>(dirpath, { parser })\n\n for await (const entry of fileDb.iterEntries()) {\n await this.create(entry)\n }\n }\n}\n","import { JsonEntryParser, SingleEntryDb, UninitError, UpdaterFn } from '../common'\nimport { SingleEntryFileDb } from '../file/singleEntryFileDb'\n\nexport class SingleEntryMemDb<T> extends SingleEntryDb<T> {\n protected entry: T | null = null\n\n constructor(initialEntry: T | null = null) {\n super()\n this.entry = initialEntry\n }\n\n async isInited() {\n return this.entry !== null\n }\n\n async read() {\n if (this.entry === null) throw new UninitError()\n return this.entry\n }\n\n async write(updaterOrEntry: T | UpdaterFn<T>) {\n let entry: T\n\n if (typeof updaterOrEntry === 'function') {\n const updater = updaterOrEntry as (entry: T) => Partial<T>\n\n if (this.entry === null) throw new UninitError()\n\n const updatedFields = updater(this.entry)\n entry = { ...this.entry, ...updatedFields }\n } else {\n entry = updaterOrEntry\n }\n\n this.entry = entry\n return entry\n }\n\n async delete() {\n this.entry = null\n }\n\n async persist(filepath: string) {\n const fileDb = new SingleEntryFileDb<T>(filepath)\n await fileDb.write(await this.read())\n }\n\n async load(filepath: string, parser?: JsonEntryParser<T>) {\n const fileDb = new SingleEntryFileDb<T>(filepath, parser)\n const persistedEntry = await fileDb.read()\n this.write(persistedEntry)\n }\n}\n"],"names":["FileType","fs","path","fsSync","readline"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAM,MAAO,OAAQ,SAAQ,KAAK,CAAA;AAAG;AAE/B,MAAO,aAAc,SAAQ,OAAO,CAAA;AAAG;AAEvC,MAAO,aAAc,SAAQ,OAAO,CAAA;AAAG;AAEvC,MAAO,WAAY,SAAQ,OAAO,CAAA;AAAG;AAErC,MAAO,cAAe,SAAQ,OAAO,CAAA;AACzC,IAAA,WAAA,CAAY,EAAW,EAAA;AACrB,QAAA,KAAK,CAAC,CAAA,kBAAA,EAAqB,EAAE,CAAA,CAAA,CAAG,CAAC;IACnC;AACD;AAEK,MAAO,WAAY,SAAQ,OAAO,CAAA;AACtC,IAAA,WAAA,GAAA;QACE,KAAK,CAAC,kFAAkF,CAAC;IAC3F;AACD;;MCTqB,YAAY,CAAA;IAehC,MAAM,cAAc,CAAC,EAAW,EAAA;QAC9B,MAAM,KAAK,GAAG,MAAM,IAAI,CAAC,OAAO,CAAC,EAAE,CAAC;AACpC,QAAA,IAAI,CAAC,KAAK;AAAE,YAAA,MAAM,IAAI,aAAa,CAAC,kBAAkB,EAAE,CAAA,gBAAA,CAAkB,CAAC;AAC3E,QAAA,OAAO,KAAK;IACd;IAEA,MAAM,aAAa,CAAC,SAAyB,EAAA;AAC3C,QAAA,MAAM,OAAO,GAAG,MAAM,IAAI,CAAC,QAAQ,CAAC,SAAS,EAAE,EAAE,IAAI,EAAE,CAAC,EAAE,IAAI,EAAE,CAAC,EAAE,CAAC;QACpE,OAAO,OAAO,CAAC,EAAE,CAAC,CAAC,CAAC,IAAI,IAAI;IAC9B;IAEA,MAAM,oBAAoB,CAAC,SAAyB,EAAA;QAClD,MAAM,KAAK,GAAG,MAAM,IAAI,CAAC,aAAa,CAAC,SAAS,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6
+ class DbError extends Error {
7
+ }
8
+ class NotFoundError extends DbError {
9
+ }
10
+ class ConflictError extends DbError {
11
+ }
12
+ class FileIoError extends DbError {
13
+ }
14
+ class InvalidIdError extends DbError {
15
+ constructor(id) {
16
+ super(`Invalid entry id '${id}'`);
17
+ }
18
+ }
19
+ class UninitError extends DbError {
20
+ constructor() {
21
+ super('Cannot read or update uninitialized entry. Use write(entry) to initialize first.');
22
+ }
23
+ }
24
+
6
25
  class MultiEntryDb {
7
26
  async getByIdOrThrow(id) {
8
27
  const entry = await this.getById(id);
9
28
  if (!entry)
10
- throw new Error(`Entry with id '${id}' does not exist`);
29
+ throw new NotFoundError(`Entry with id '${id}' does not exist`);
11
30
  return entry;
12
31
  }
32
+ async getFirstWhere(predicate) {
33
+ const matches = await this.getWhere(predicate, { page: 0, take: 1 });
34
+ return matches.at(0) ?? null;
35
+ }
36
+ async getFirstWhereOrThrow(predicate) {
37
+ const match = await this.getFirstWhere(predicate);
38
+ if (!match)
39
+ throw new NotFoundError('No entry matches predicate');
40
+ return match;
41
+ }
13
42
  async getWhere(predicate, pagination) {
14
43
  let totalMatched = 0;
15
44
  const entries = [];
@@ -103,6 +132,9 @@ class MultiEntryDb {
103
132
  }
104
133
  }
105
134
 
135
+ class SingleEntryDb {
136
+ }
137
+
106
138
  var FileType;
107
139
  (function (FileType) {
108
140
  FileType["File"] = "file";
@@ -119,7 +151,7 @@ class Files {
119
151
  const { recursive = false, overwrite = true } = options ?? {};
120
152
  // Check if destination exists and handle overwrite
121
153
  if (!overwrite && (await this.exists(destinationPath))) {
122
- throw new Error(`Destination '${destinationPath}' already exists`);
154
+ throw new FileIoError(`Destination '${destinationPath}' already exists`);
123
155
  }
124
156
  const sourceStats = await fs.stat(sourcePath);
125
157
  if (sourceStats.isFile()) {
@@ -130,7 +162,7 @@ class Files {
130
162
  }
131
163
  if (sourceStats.isDirectory()) {
132
164
  if (!recursive) {
133
- throw new Error(`'${sourcePath}' is a directory (use recursive option)`);
165
+ throw new FileIoError(`'${sourcePath}' is a directory (use recursive option)`);
134
166
  }
135
167
  await this.copyRecursive(sourcePath, destinationPath);
136
168
  }
@@ -267,7 +299,7 @@ class Files {
267
299
  ...commonMeta,
268
300
  type: FileType.File,
269
301
  };
270
- throw new Error(`File at ${filepath} is not normal file or directory`);
302
+ throw new FileIoError(`File at ${filepath} is not normal file or directory`);
271
303
  }
272
304
  async list(dirpath, options) {
273
305
  const { depth = 0, stripBasepath } = options ?? {};
@@ -320,6 +352,10 @@ class Files {
320
352
  }
321
353
 
322
354
  class MultiEntryFileDb extends MultiEntryDb {
355
+ dirpath;
356
+ files;
357
+ parser;
358
+ noPathlikeIds;
323
359
  constructor(dirpath, options) {
324
360
  super();
325
361
  this.dirpath = dirpath;
@@ -333,7 +369,7 @@ class MultiEntryFileDb extends MultiEntryDb {
333
369
  }
334
370
  async getById(id) {
335
371
  if (!this.isIdValid(id))
336
- throw new Error(`Invalid id: ${id}`);
372
+ throw new InvalidIdError(id);
337
373
  try {
338
374
  const filepath = this.getFilePath(id);
339
375
  const text = await this.files.read(filepath);
@@ -378,7 +414,7 @@ class MultiEntryFileDb extends MultiEntryDb {
378
414
  }
379
415
  async writeEntry(entry) {
380
416
  if (!this.isIdValid(entry.id))
381
- throw new Error(`Invalid id: ${entry.id}`);
417
+ throw new InvalidIdError(entry.id);
382
418
  const filepath = this.getFilePath(entry.id);
383
419
  await this.files.write(filepath, JSON.stringify(entry, null, 2));
384
420
  }
@@ -410,11 +446,14 @@ class MultiEntryFileDb extends MultiEntryDb {
410
446
  }
411
447
  }
412
448
 
413
- class SingleEntryFileDb {
449
+ class SingleEntryFileDb extends SingleEntryDb {
450
+ filepath;
451
+ parser;
452
+ files = new Files();
414
453
  constructor(filepath, parser = JSON) {
454
+ super();
415
455
  this.filepath = filepath;
416
456
  this.parser = parser;
417
- this.files = new Files();
418
457
  }
419
458
  path() {
420
459
  return this.filepath;
@@ -448,10 +487,7 @@ class SingleEntryFileDb {
448
487
  }
449
488
 
450
489
  class MultiEntryMemDb extends MultiEntryDb {
451
- constructor() {
452
- super(...arguments);
453
- this.entries = new Map();
454
- }
490
+ entries = new Map();
455
491
  async create(entry) {
456
492
  this.entries.set(entry.id, entry);
457
493
  return entry;
@@ -484,28 +520,40 @@ class MultiEntryMemDb extends MultiEntryDb {
484
520
  yield id;
485
521
  }
486
522
  }
523
+ async persist(dirpath) {
524
+ const fileDb = new MultiEntryFileDb(dirpath);
525
+ await fileDb.destroy();
526
+ const values = [...this.entries.values()];
527
+ await Promise.all(values.map((entry) => fileDb.create(entry)));
528
+ }
529
+ async load(dirpath, parser) {
530
+ const fileDb = new MultiEntryFileDb(dirpath, { parser });
531
+ for await (const entry of fileDb.iterEntries()) {
532
+ await this.create(entry);
533
+ }
534
+ }
487
535
  }
488
536
 
489
- class SingleEntryMemDb {
537
+ class SingleEntryMemDb extends SingleEntryDb {
538
+ entry = null;
490
539
  constructor(initialEntry = null) {
491
- this.entry = null;
540
+ super();
492
541
  this.entry = initialEntry;
493
542
  }
494
- isInited() {
543
+ async isInited() {
495
544
  return this.entry !== null;
496
545
  }
497
- read() {
546
+ async read() {
498
547
  if (this.entry === null)
499
- throw new Error('Entry not initialized');
548
+ throw new UninitError();
500
549
  return this.entry;
501
550
  }
502
- write(updaterOrEntry) {
551
+ async write(updaterOrEntry) {
503
552
  let entry;
504
553
  if (typeof updaterOrEntry === 'function') {
505
554
  const updater = updaterOrEntry;
506
- if (this.entry === null) {
507
- throw new Error('Cannot update uninitialized entry. Use write(entry) to initialize first.');
508
- }
555
+ if (this.entry === null)
556
+ throw new UninitError();
509
557
  const updatedFields = updater(this.entry);
510
558
  entry = { ...this.entry, ...updatedFields };
511
559
  }
@@ -515,10 +563,19 @@ class SingleEntryMemDb {
515
563
  this.entry = entry;
516
564
  return entry;
517
565
  }
518
- delete() {
566
+ async delete() {
519
567
  this.entry = null;
520
568
  }
569
+ async persist(filepath) {
570
+ const fileDb = new SingleEntryFileDb(filepath);
571
+ await fileDb.write(await this.read());
572
+ }
573
+ async load(filepath, parser) {
574
+ const fileDb = new SingleEntryFileDb(filepath, parser);
575
+ const persistedEntry = await fileDb.read();
576
+ this.write(persistedEntry);
577
+ }
521
578
  }
522
579
 
523
- export { FileType, MultiEntryFileDb, MultiEntryMemDb, SingleEntryFileDb, SingleEntryMemDb };
580
+ export { ConflictError, DbError, FileIoError, FileType, InvalidIdError, MultiEntryFileDb, MultiEntryMemDb, NotFoundError, SingleEntryFileDb, SingleEntryMemDb, UninitError };
524
581
  //# sourceMappingURL=index.js.map