@jsonkit/db 1.0.1 → 3.0.1
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 +247 -1
- package/dist/cjs/index.cjs +219 -84
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/esm/index.js +218 -85
- package/dist/esm/index.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/dist/types/common/index.d.ts +2 -0
- package/dist/types/common/multiEntryDb.d.ts +19 -0
- package/dist/types/{types.d.ts → common/types.d.ts} +9 -0
- package/dist/types/{files.d.ts → file/files.d.ts} +2 -2
- package/dist/types/file/multiEntryFileDb.d.ts +20 -0
- package/dist/types/{singleEntryFileDb.d.ts → file/singleEntryFileDb.d.ts} +3 -3
- package/dist/types/index.d.ts +5 -3
- package/dist/types/memory/__tests__/multiEntryMemDb.test.d.ts +1 -0
- package/dist/types/memory/__tests__/singleEntryMemDb.test.d.ts +1 -0
- package/dist/types/memory/multiEntryMemDb.d.ts +11 -0
- package/dist/types/memory/singleEntryMemDb.d.ts +8 -0
- package/package.json +5 -2
- package/dist/types/multiEntryFileDb.d.ts +0 -26
- /package/dist/types/{__tests__ → file/__tests__}/multiEntryFileDb.test.d.ts +0 -0
- /package/dist/types/{__tests__ → file/__tests__}/singleEntryFileDb.test.d.ts +0 -0
package/dist/cjs/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","sources":["../../src/types.ts","../../src/files.ts","../../src/multiEntryFileDb.ts","../../src/singleEntryFileDb.ts"],"sourcesContent":["export type Identifiable = {\n id: Id\n}\n\nexport type Id = string\n\nexport type Promisable<T> = T | Promise<T>\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 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 './types'\nimport { FileType } from './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 FilesService {\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 type {\n Identifiable,\n DeleteManyOutput,\n Promisable,\n PredicateFn,\n JsonEntryParser,\n} from './types'\nimport { FilesService } from './files'\nimport * as path from 'path'\n\nexport class MultiEntryFileDb<T extends Identifiable> {\n protected readonly files: FilesService = new FilesService()\n\n constructor(\n protected readonly dirpath: string,\n protected readonly parser: JsonEntryParser<T> = JSON,\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 return await this.readEntry(id)\n }\n\n async getByIdOrThrow(id: T['id']): Promise<T> {\n const entry = await this.readEntry(id)\n if (!entry) {\n throw new Error('Entry with id ' + id + ' does not exist')\n }\n return entry\n }\n\n async getWhere(predicate: PredicateFn<T>, max?: number): Promise<T[]> {\n const entries = await this.getAll()\n return entries.filter(predicate).slice(0, max)\n }\n\n async getAll(whereIds?: T['id'][]): Promise<T[]> {\n const ids = whereIds === undefined ? await this.getAllIds() : whereIds\n const entries: T[] = []\n\n for (const id of ids) {\n const entry = await this.readEntry(id)\n if (entry) entries.push(entry)\n }\n\n return entries\n }\n\n async getAllIds(): Promise<T['id'][]> {\n try {\n const entries = await this.files.list(this.dirpath)\n return entries.filter((name) => name.endsWith('.json')).map((name) => name.slice(0, -5)) // Remove .json extension\n } catch {\n // Directory might not exist\n return []\n }\n }\n\n async update(id: T['id'], updater: (entry: T) => Promisable<Partial<T>>): Promise<T> {\n const entry = await this.readEntry(id)\n if (!entry) {\n throw new Error('Entry with id ' + id + ' does not exist')\n }\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.delete(id)\n }\n\n return updatedEntry\n }\n\n async delete(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 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.delete(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 destroy() {\n await this.files.delete(this.dirpath)\n }\n\n async exists(id: T['id']): Promise<boolean> {\n const entry = await this.readEntry(id)\n return entry !== null\n }\n\n async countAll(): Promise<number> {\n const ids = await this.getAllIds()\n return ids.length\n }\n\n async countWhere(predicate: PredicateFn<T>): Promise<number> {\n return (await this.getWhere(predicate)).length\n }\n\n protected getFilePath(id: T['id']) {\n return path.join(this.dirpath, `${id}.json`)\n }\n\n protected async readEntry(id: T['id']) {\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 protected async writeEntry(entry: T) {\n const filepath = this.getFilePath(entry.id)\n await this.files.write(filepath, JSON.stringify(entry, null, 2))\n }\n\n protected async *iterEntries() {\n const ids = await this.getAllIds()\n for (const id of ids) {\n const entry = await this.readEntry(id)\n if (entry) yield entry\n }\n }\n}\n","import type { JsonEntryParser, Promisable } from './types'\nimport { FilesService } from './files'\n\nexport class SingleEntryFileDb<T> {\n protected readonly files: FilesService = new FilesService()\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"],"names":["FileType","fs","path","fsSync","readline"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAmCYA;AAAZ,CAAA,UAAY,QAAQ,EAAA;AAClB,IAAA,QAAA,CAAA,MAAA,CAAA,GAAA,MAAa;AACb,IAAA,QAAA,CAAA,WAAA,CAAA,GAAA,WAAuB;;AAEzB,CAAC,EAJWA,gBAAQ,KAARA,gBAAQ,GAAA,EAAA,CAAA,CAAA;;ACJpB,MAAM,gBAAgB,GAAmB,OAAO;MAEnC,YAAY,CAAA;AACvB,IAAA,MAAM,IAAI,CAAC,OAAe,EAAE,OAAe,EAAA;QACzC,MAAMC,aAAE,CAAC,MAAM,CAAC,OAAO,EAAE,OAAO,CAAC;IACnC;AAEA,IAAA,MAAM,IAAI,CAAC,UAAkB,EAAE,eAAuB,EAAE,OAAqB,EAAA;AAC3E,QAAA,MAAM,EAAE,SAAS,GAAG,KAAK,EAAE,SAAS,GAAG,IAAI,EAAE,GAAG,OAAO,IAAI,EAAE;;AAG7D,QAAA,IAAI,CAAC,SAAS,KAAK,MAAM,IAAI,CAAC,MAAM,CAAC,eAAe,CAAC,CAAC,EAAE;AACtD,YAAA,MAAM,IAAI,KAAK,CAAC,gBAAgB,eAAe,CAAA,gBAAA,CAAkB,CAAC;QACpE;QAEA,MAAM,WAAW,GAAG,MAAMA,aAAE,CAAC,IAAI,CAAC,UAAU,CAAC;AAE7C,QAAA,IAAI,WAAW,CAAC,MAAM,EAAE,EAAE;YACxB,MAAM,cAAc,GAAGC,eAAI,CAAC,OAAO,CAAC,eAAe,CAAC;AACpD,YAAA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C,QAAQ;IACtB;AAEA,IAAA,MAAM,QAAQ,GAAA;AACZ,QAAA,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,IAAI,CAAC,QAAQ,CAAC;AACrD,QAAA,OAAO,MAAM;IACf;AAEA,IAAA,MAAM,IAAI,GAAA;AACR,QAAA,MAAM,IAAI,GAAG,MAAM,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,QAAQ,CAAC;QACjD,MAAM,KAAK,GAAG,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC;AACrC,QAAA,OAAO,KAAK;IACd;IAEA,MAAM,KAAK,CAAC,cAA0D,EAAA;AACpE,QAAA,IAAI,KAAQ;AACZ,QAAA,IAAI,OAAO,cAAc,KAAK,UAAU,EAAE;YACxC,MAAM,OAAO,GAAG,cAAiC;AACjD,YAAA,MAAM,QAAQ,GAAG,MAAM,IAAI,CAAC,IAAI,EAAE;AAElC,YAAA,MAAM,aAAa,GAAG,MAAM,OAAO,CAAC,QAAQ,CAAC;YAC7C,KAAK,GAAG,EAAE,GAAG,QAAQ,EAAE,GAAG,aAAa,EAAE;QAC3C;aAAO;YACL,KAAK,GAAG,cAAc;QACxB;QAEA,MAAM,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,IAAI,CAAC,QAAQ,EAAE,IAAI,CAAC,SAAS,CAAC,KAAK,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC;AAErE,QAAA,OAAO,KAAK;IACd;AAEA,IAAA,MAAM,MAAM,GAAA;QACV,MAAM,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,IAAI,CAAC,QAAQ,CAAC;IACxC;AACD;;;;;"}
|
|
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,IAA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AI,CAAC,KAAK,KAAK,IAAI;AAAE,YAAA,MAAM,IAAI,KAAK,CAAC,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;;;;;;;"}
|
package/dist/esm/index.js
CHANGED
|
@@ -3,6 +3,106 @@ import * as path from 'path';
|
|
|
3
3
|
import * as fsSync from 'fs';
|
|
4
4
|
import * as readline from 'readline';
|
|
5
5
|
|
|
6
|
+
class MultiEntryDb {
|
|
7
|
+
async getByIdOrThrow(id) {
|
|
8
|
+
const entry = await this.getById(id);
|
|
9
|
+
if (!entry)
|
|
10
|
+
throw new Error(`Entry with id '${id}' does not exist`);
|
|
11
|
+
return entry;
|
|
12
|
+
}
|
|
13
|
+
async getWhere(predicate, pagination) {
|
|
14
|
+
let totalMatched = 0;
|
|
15
|
+
const entries = [];
|
|
16
|
+
if (!pagination) {
|
|
17
|
+
for await (const entry of this.iterEntries()) {
|
|
18
|
+
const isMatch = predicate(entry);
|
|
19
|
+
if (isMatch)
|
|
20
|
+
entries.push(entry);
|
|
21
|
+
}
|
|
22
|
+
return entries;
|
|
23
|
+
}
|
|
24
|
+
const { take, page } = pagination;
|
|
25
|
+
const skip = pagination.skip ?? 0;
|
|
26
|
+
const startIndex = (page - 1) * take + skip;
|
|
27
|
+
const endIndex = startIndex + take;
|
|
28
|
+
for await (const entry of this.iterEntries()) {
|
|
29
|
+
const isMatch = predicate(entry);
|
|
30
|
+
if (isMatch) {
|
|
31
|
+
if (totalMatched >= startIndex && totalMatched < endIndex) {
|
|
32
|
+
entries.push(entry);
|
|
33
|
+
}
|
|
34
|
+
totalMatched++;
|
|
35
|
+
if (totalMatched >= endIndex)
|
|
36
|
+
break;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
return entries;
|
|
40
|
+
}
|
|
41
|
+
getAll() {
|
|
42
|
+
return this.getWhere(() => true);
|
|
43
|
+
}
|
|
44
|
+
async getAllIds() {
|
|
45
|
+
const ids = [];
|
|
46
|
+
for await (const id of this.iterIds()) {
|
|
47
|
+
ids.push(id);
|
|
48
|
+
}
|
|
49
|
+
return ids;
|
|
50
|
+
}
|
|
51
|
+
deleteByIds(ids) {
|
|
52
|
+
return this.deleteWhere((entry) => ids.includes(entry.id));
|
|
53
|
+
}
|
|
54
|
+
async deleteWhere(predicate) {
|
|
55
|
+
const deletedIds = [];
|
|
56
|
+
const ignoredIds = [];
|
|
57
|
+
for await (const entry of this.iterEntries()) {
|
|
58
|
+
if (!predicate(entry))
|
|
59
|
+
continue;
|
|
60
|
+
const didDelete = await this.deleteById(entry.id);
|
|
61
|
+
if (didDelete) {
|
|
62
|
+
deletedIds.push(entry.id);
|
|
63
|
+
}
|
|
64
|
+
else {
|
|
65
|
+
ignoredIds.push(entry.id);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
return { deletedIds, ignoredIds };
|
|
69
|
+
}
|
|
70
|
+
async exists(id) {
|
|
71
|
+
const entry = await this.getById(id);
|
|
72
|
+
return entry !== null;
|
|
73
|
+
}
|
|
74
|
+
countAll() {
|
|
75
|
+
return this.countWhere(() => true);
|
|
76
|
+
}
|
|
77
|
+
async countWhere(predicate, pagination) {
|
|
78
|
+
let totalMatched = 0;
|
|
79
|
+
let count = 0;
|
|
80
|
+
if (!pagination) {
|
|
81
|
+
for await (const entry of this.iterEntries()) {
|
|
82
|
+
const isMatch = predicate(entry);
|
|
83
|
+
if (isMatch)
|
|
84
|
+
count++;
|
|
85
|
+
}
|
|
86
|
+
return count;
|
|
87
|
+
}
|
|
88
|
+
const { take, page } = pagination;
|
|
89
|
+
const skip = pagination.skip ?? 0;
|
|
90
|
+
const startIndex = (page - 1) * take + skip;
|
|
91
|
+
const endIndex = startIndex + take;
|
|
92
|
+
for await (const entry of this.iterEntries()) {
|
|
93
|
+
const isMatch = predicate(entry);
|
|
94
|
+
if (isMatch) {
|
|
95
|
+
if (totalMatched >= startIndex && totalMatched < endIndex)
|
|
96
|
+
count++;
|
|
97
|
+
totalMatched++;
|
|
98
|
+
if (totalMatched >= endIndex)
|
|
99
|
+
break;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
return count;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
6
106
|
var FileType;
|
|
7
107
|
(function (FileType) {
|
|
8
108
|
FileType["File"] = "file";
|
|
@@ -11,7 +111,7 @@ var FileType;
|
|
|
11
111
|
})(FileType || (FileType = {}));
|
|
12
112
|
|
|
13
113
|
const DEFUALT_ENCODING = 'utf-8';
|
|
14
|
-
class
|
|
114
|
+
class Files {
|
|
15
115
|
async move(oldPath, newPath) {
|
|
16
116
|
await fs.rename(oldPath, newPath);
|
|
17
117
|
}
|
|
@@ -219,64 +319,44 @@ class FilesService {
|
|
|
219
319
|
}
|
|
220
320
|
}
|
|
221
321
|
|
|
222
|
-
class MultiEntryFileDb {
|
|
223
|
-
constructor(dirpath,
|
|
322
|
+
class MultiEntryFileDb extends MultiEntryDb {
|
|
323
|
+
constructor(dirpath, options) {
|
|
324
|
+
super();
|
|
224
325
|
this.dirpath = dirpath;
|
|
225
|
-
this.
|
|
226
|
-
this.
|
|
326
|
+
this.files = new Files();
|
|
327
|
+
this.parser = options?.parser ?? JSON;
|
|
328
|
+
this.noPathlikeIds = options?.noPathlikeIds ?? true;
|
|
227
329
|
}
|
|
228
330
|
async create(entry) {
|
|
229
331
|
await this.writeEntry(entry);
|
|
230
332
|
return entry;
|
|
231
333
|
}
|
|
232
334
|
async getById(id) {
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
async getByIdOrThrow(id) {
|
|
236
|
-
const entry = await this.readEntry(id);
|
|
237
|
-
if (!entry) {
|
|
238
|
-
throw new Error('Entry with id ' + id + ' does not exist');
|
|
239
|
-
}
|
|
240
|
-
return entry;
|
|
241
|
-
}
|
|
242
|
-
async getWhere(predicate, max) {
|
|
243
|
-
const entries = await this.getAll();
|
|
244
|
-
return entries.filter(predicate).slice(0, max);
|
|
245
|
-
}
|
|
246
|
-
async getAll(whereIds) {
|
|
247
|
-
const ids = whereIds === undefined ? await this.getAllIds() : whereIds;
|
|
248
|
-
const entries = [];
|
|
249
|
-
for (const id of ids) {
|
|
250
|
-
const entry = await this.readEntry(id);
|
|
251
|
-
if (entry)
|
|
252
|
-
entries.push(entry);
|
|
253
|
-
}
|
|
254
|
-
return entries;
|
|
255
|
-
}
|
|
256
|
-
async getAllIds() {
|
|
335
|
+
if (!this.isIdValid(id))
|
|
336
|
+
throw new Error(`Invalid id: ${id}`);
|
|
257
337
|
try {
|
|
258
|
-
const
|
|
259
|
-
|
|
338
|
+
const filepath = this.getFilePath(id);
|
|
339
|
+
const text = await this.files.read(filepath);
|
|
340
|
+
const entry = this.parser.parse(text);
|
|
341
|
+
return entry;
|
|
260
342
|
}
|
|
261
|
-
catch {
|
|
262
|
-
|
|
263
|
-
|
|
343
|
+
catch (error) {
|
|
344
|
+
console.error('Failed to read entry', error);
|
|
345
|
+
// File doesn't exist or invalid JSON
|
|
346
|
+
return null;
|
|
264
347
|
}
|
|
265
348
|
}
|
|
266
349
|
async update(id, updater) {
|
|
267
|
-
const entry = await this.
|
|
268
|
-
if (!entry) {
|
|
269
|
-
throw new Error('Entry with id ' + id + ' does not exist');
|
|
270
|
-
}
|
|
350
|
+
const entry = await this.getByIdOrThrow(id);
|
|
271
351
|
const updatedEntryFields = await updater(entry);
|
|
272
352
|
const updatedEntry = { ...entry, ...updatedEntryFields };
|
|
273
353
|
await this.writeEntry(updatedEntry);
|
|
274
354
|
if (updatedEntry.id !== id) {
|
|
275
|
-
await this.
|
|
355
|
+
await this.deleteById(id);
|
|
276
356
|
}
|
|
277
357
|
return updatedEntry;
|
|
278
358
|
}
|
|
279
|
-
async
|
|
359
|
+
async deleteById(id) {
|
|
280
360
|
try {
|
|
281
361
|
const filepath = this.getFilePath(id);
|
|
282
362
|
await this.files.delete(filepath, { force: false });
|
|
@@ -290,71 +370,51 @@ class MultiEntryFileDb {
|
|
|
290
370
|
async deleteByIds(ids) {
|
|
291
371
|
return this.deleteWhere((entry) => ids.includes(entry.id));
|
|
292
372
|
}
|
|
293
|
-
async deleteWhere(predicate) {
|
|
294
|
-
const deletedIds = [];
|
|
295
|
-
const ignoredIds = [];
|
|
296
|
-
for await (const entry of this.iterEntries()) {
|
|
297
|
-
if (!predicate(entry))
|
|
298
|
-
continue;
|
|
299
|
-
const didDelete = await this.delete(entry.id);
|
|
300
|
-
if (didDelete) {
|
|
301
|
-
deletedIds.push(entry.id);
|
|
302
|
-
}
|
|
303
|
-
else {
|
|
304
|
-
ignoredIds.push(entry.id);
|
|
305
|
-
}
|
|
306
|
-
}
|
|
307
|
-
return { deletedIds, ignoredIds };
|
|
308
|
-
}
|
|
309
373
|
async destroy() {
|
|
310
374
|
await this.files.delete(this.dirpath);
|
|
311
375
|
}
|
|
312
|
-
async exists(id) {
|
|
313
|
-
const entry = await this.readEntry(id);
|
|
314
|
-
return entry !== null;
|
|
315
|
-
}
|
|
316
|
-
async countAll() {
|
|
317
|
-
const ids = await this.getAllIds();
|
|
318
|
-
return ids.length;
|
|
319
|
-
}
|
|
320
|
-
async countWhere(predicate) {
|
|
321
|
-
return (await this.getWhere(predicate)).length;
|
|
322
|
-
}
|
|
323
376
|
getFilePath(id) {
|
|
324
377
|
return path.join(this.dirpath, `${id}.json`);
|
|
325
378
|
}
|
|
326
|
-
async readEntry(id) {
|
|
327
|
-
try {
|
|
328
|
-
const filepath = this.getFilePath(id);
|
|
329
|
-
const text = await this.files.read(filepath);
|
|
330
|
-
const entry = this.parser.parse(text);
|
|
331
|
-
return entry;
|
|
332
|
-
}
|
|
333
|
-
catch (error) {
|
|
334
|
-
console.error('Failed to read entry', error);
|
|
335
|
-
// File doesn't exist or invalid JSON
|
|
336
|
-
return null;
|
|
337
|
-
}
|
|
338
|
-
}
|
|
339
379
|
async writeEntry(entry) {
|
|
380
|
+
if (!this.isIdValid(entry.id))
|
|
381
|
+
throw new Error(`Invalid id: ${entry.id}`);
|
|
340
382
|
const filepath = this.getFilePath(entry.id);
|
|
341
383
|
await this.files.write(filepath, JSON.stringify(entry, null, 2));
|
|
342
384
|
}
|
|
385
|
+
isIdValid(id) {
|
|
386
|
+
if (typeof id !== 'string')
|
|
387
|
+
return false;
|
|
388
|
+
if (!this.noPathlikeIds)
|
|
389
|
+
return true;
|
|
390
|
+
if (id.includes('/') || id.includes('\\'))
|
|
391
|
+
return false;
|
|
392
|
+
return true;
|
|
393
|
+
}
|
|
343
394
|
async *iterEntries() {
|
|
344
|
-
const
|
|
345
|
-
|
|
346
|
-
const entry = await this.readEntry(id);
|
|
395
|
+
for await (const id of this.iterIds()) {
|
|
396
|
+
const entry = await this.getById(id);
|
|
347
397
|
if (entry)
|
|
348
398
|
yield entry;
|
|
349
399
|
}
|
|
350
400
|
}
|
|
401
|
+
async *iterIds() {
|
|
402
|
+
const filenames = await this.files.list(this.dirpath);
|
|
403
|
+
for (const filename of filenames) {
|
|
404
|
+
if (!filename.endsWith('.json'))
|
|
405
|
+
continue;
|
|
406
|
+
const id = filename.replace(/\.json$/, '');
|
|
407
|
+
if (this.isIdValid(id))
|
|
408
|
+
yield id;
|
|
409
|
+
}
|
|
410
|
+
}
|
|
351
411
|
}
|
|
352
412
|
|
|
353
413
|
class SingleEntryFileDb {
|
|
354
414
|
constructor(filepath, parser = JSON) {
|
|
355
415
|
this.filepath = filepath;
|
|
356
416
|
this.parser = parser;
|
|
357
|
-
this.files = new
|
|
417
|
+
this.files = new Files();
|
|
358
418
|
}
|
|
359
419
|
path() {
|
|
360
420
|
return this.filepath;
|
|
@@ -387,5 +447,78 @@ class SingleEntryFileDb {
|
|
|
387
447
|
}
|
|
388
448
|
}
|
|
389
449
|
|
|
390
|
-
|
|
450
|
+
class MultiEntryMemDb extends MultiEntryDb {
|
|
451
|
+
constructor() {
|
|
452
|
+
super(...arguments);
|
|
453
|
+
this.entries = new Map();
|
|
454
|
+
}
|
|
455
|
+
async create(entry) {
|
|
456
|
+
this.entries.set(entry.id, entry);
|
|
457
|
+
return entry;
|
|
458
|
+
}
|
|
459
|
+
async getById(id) {
|
|
460
|
+
return this.entries.get(id) ?? null;
|
|
461
|
+
}
|
|
462
|
+
async update(id, updater) {
|
|
463
|
+
const entry = await this.getByIdOrThrow(id);
|
|
464
|
+
const updatedEntryFields = await updater(entry);
|
|
465
|
+
const updatedEntry = { ...entry, ...updatedEntryFields };
|
|
466
|
+
this.entries.set(updatedEntry.id, updatedEntry);
|
|
467
|
+
if (updatedEntry.id !== id)
|
|
468
|
+
this.entries.delete(id);
|
|
469
|
+
return updatedEntry;
|
|
470
|
+
}
|
|
471
|
+
async deleteById(id) {
|
|
472
|
+
return this.entries.delete(id);
|
|
473
|
+
}
|
|
474
|
+
async destroy() {
|
|
475
|
+
this.entries.clear();
|
|
476
|
+
}
|
|
477
|
+
async *iterEntries() {
|
|
478
|
+
for (const entry of this.entries.values()) {
|
|
479
|
+
yield entry;
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
async *iterIds() {
|
|
483
|
+
for (const id of this.entries.keys()) {
|
|
484
|
+
yield id;
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
class SingleEntryMemDb {
|
|
490
|
+
constructor(initialEntry = null) {
|
|
491
|
+
this.entry = null;
|
|
492
|
+
this.entry = initialEntry;
|
|
493
|
+
}
|
|
494
|
+
isInited() {
|
|
495
|
+
return this.entry !== null;
|
|
496
|
+
}
|
|
497
|
+
read() {
|
|
498
|
+
if (this.entry === null)
|
|
499
|
+
throw new Error('Entry not initialized');
|
|
500
|
+
return this.entry;
|
|
501
|
+
}
|
|
502
|
+
write(updaterOrEntry) {
|
|
503
|
+
let entry;
|
|
504
|
+
if (typeof updaterOrEntry === 'function') {
|
|
505
|
+
const updater = updaterOrEntry;
|
|
506
|
+
if (this.entry === null) {
|
|
507
|
+
throw new Error('Cannot update uninitialized entry. Use write(entry) to initialize first.');
|
|
508
|
+
}
|
|
509
|
+
const updatedFields = updater(this.entry);
|
|
510
|
+
entry = { ...this.entry, ...updatedFields };
|
|
511
|
+
}
|
|
512
|
+
else {
|
|
513
|
+
entry = updaterOrEntry;
|
|
514
|
+
}
|
|
515
|
+
this.entry = entry;
|
|
516
|
+
return entry;
|
|
517
|
+
}
|
|
518
|
+
delete() {
|
|
519
|
+
this.entry = null;
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
export { FileType, MultiEntryFileDb, MultiEntryMemDb, SingleEntryFileDb, SingleEntryMemDb };
|
|
391
524
|
//# sourceMappingURL=index.js.map
|