@jsonkit/db 3.0.1 → 3.0.2

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.
@@ -0,0 +1,154 @@
1
+ type Identifiable = {
2
+ id: Id;
3
+ };
4
+ type Id = string;
5
+ type Promisable<T> = T | Promise<T>;
6
+ type PaginationInput = {
7
+ take: number;
8
+ page: number;
9
+ skip?: number | undefined | null;
10
+ };
11
+ type DeleteManyOutput = {
12
+ deletedIds: Id[];
13
+ ignoredIds: Id[];
14
+ };
15
+ type PredicateFn<T extends Identifiable> = (entry: T) => boolean;
16
+ type JsonEntryParser<T> = {
17
+ parse: (text: string) => T;
18
+ };
19
+ type MultiEntryFileDbOptions<T> = {
20
+ noPathlikeIds?: boolean;
21
+ parser?: JsonEntryParser<T>;
22
+ };
23
+ type FileMeta = {
24
+ path: string;
25
+ size: number;
26
+ created: Date;
27
+ modified: Date;
28
+ accessed: Date;
29
+ } & ({
30
+ type: FileType.File;
31
+ } | {
32
+ type: FileType.Directory;
33
+ children: FileMeta[];
34
+ });
35
+ declare enum FileType {
36
+ File = "file",
37
+ Directory = "directory"
38
+ }
39
+
40
+ declare abstract class MultiEntryDb<T extends Identifiable> {
41
+ abstract create(entry: T): Promise<T>;
42
+ abstract getById(id: T['id']): Promise<T | null>;
43
+ abstract update(id: T['id'], updater: (entry: T) => Promisable<Partial<T>>): Promise<T>;
44
+ abstract deleteById(id: T['id']): Promise<boolean>;
45
+ abstract destroy(): Promise<void>;
46
+ protected abstract iterEntries(): AsyncIterable<T>;
47
+ protected abstract iterIds(): AsyncIterable<T['id']>;
48
+ getByIdOrThrow(id: T['id']): Promise<T>;
49
+ getWhere(predicate: PredicateFn<T>, pagination?: PaginationInput): Promise<T[]>;
50
+ getAll(): Promise<T[]>;
51
+ getAllIds(): Promise<T['id'][]>;
52
+ deleteByIds(ids: T['id'][]): Promise<DeleteManyOutput>;
53
+ deleteWhere(predicate: PredicateFn<T>): Promise<DeleteManyOutput>;
54
+ exists(id: T['id']): Promise<boolean>;
55
+ countAll(): Promise<number>;
56
+ countWhere(predicate: PredicateFn<T>, pagination?: PaginationInput): Promise<number>;
57
+ }
58
+
59
+ type ListOptions = {
60
+ depth?: number;
61
+ stripBasepath?: string;
62
+ };
63
+ type DeleteOptions = {
64
+ force?: boolean;
65
+ recursive?: boolean;
66
+ };
67
+ type WriteOptions = {
68
+ encoding?: BufferEncoding;
69
+ };
70
+ type ReadOptions = {
71
+ encoding?: BufferEncoding;
72
+ lines?: number;
73
+ };
74
+ type CopyOptions = {
75
+ recursive?: boolean;
76
+ overwrite?: boolean;
77
+ };
78
+ declare class Files {
79
+ move(oldPath: string, newPath: string): Promise<void>;
80
+ copy(sourcePath: string, destinationPath: string, options?: CopyOptions): Promise<void>;
81
+ copyRecursive(sourcePath: string, destinationPath: string): Promise<void>;
82
+ mkdir(filepath: string): Promise<void>;
83
+ protected mkdirUnsafe(filepath: string): Promise<void>;
84
+ write(filepath: string, content: string, options?: WriteOptions): Promise<void>;
85
+ touch(filepath: string): Promise<void>;
86
+ read(filepath: string, options?: ReadOptions): Promise<string>;
87
+ delete(filepath: string, options?: DeleteOptions): Promise<void>;
88
+ exists(filepath: string): Promise<boolean>;
89
+ isDir(filepath: string): Promise<boolean>;
90
+ isFile(filepath: string): Promise<boolean>;
91
+ isSymlink(filepath: string): Promise<boolean>;
92
+ getMeta(filepath: string, options?: ListOptions): Promise<FileMeta>;
93
+ list(dirpath: string, options?: ListOptions): Promise<string[]>;
94
+ listRead(dirpath: string, options?: ListOptions): AsyncGenerator<{
95
+ filepath: string;
96
+ content: string;
97
+ }, void, unknown>;
98
+ protected listRecursive(dirpath: string, maxDepth: number, currentDepth: number, stripBasepath: string | undefined): Promise<string[]>;
99
+ protected stripBasepath(original: string, pathToStrip: string | undefined): string;
100
+ }
101
+
102
+ declare class MultiEntryFileDb<T extends Identifiable> extends MultiEntryDb<T> {
103
+ protected readonly dirpath: string;
104
+ protected readonly files: Files;
105
+ protected readonly parser: JsonEntryParser<T>;
106
+ readonly noPathlikeIds: boolean;
107
+ constructor(dirpath: string, options?: MultiEntryFileDbOptions<T>);
108
+ create(entry: T): Promise<T>;
109
+ getById(id: T['id']): Promise<T | null>;
110
+ update(id: T['id'], updater: (entry: T) => Promisable<Partial<T>>): Promise<T>;
111
+ deleteById(id: T['id']): Promise<boolean>;
112
+ deleteByIds(ids: T['id'][]): Promise<DeleteManyOutput>;
113
+ destroy(): Promise<void>;
114
+ protected getFilePath(id: T['id']): string;
115
+ protected writeEntry(entry: T): Promise<void>;
116
+ isIdValid(id: T['id']): boolean;
117
+ protected iterEntries(): AsyncGenerator<T, void, unknown>;
118
+ protected iterIds(): AsyncGenerator<string, void, unknown>;
119
+ }
120
+
121
+ declare class SingleEntryFileDb<T> {
122
+ protected readonly filepath: string;
123
+ protected readonly parser: JsonEntryParser<T>;
124
+ protected readonly files: Files;
125
+ constructor(filepath: string, parser?: JsonEntryParser<T>);
126
+ path(): string;
127
+ isInited(): Promise<boolean>;
128
+ read(): Promise<T>;
129
+ write(updaterOrEntry: T | ((entry: T) => Promisable<Partial<T>>)): Promise<T>;
130
+ delete(): Promise<void>;
131
+ }
132
+
133
+ declare class MultiEntryMemDb<T extends Identifiable> extends MultiEntryDb<T> {
134
+ protected entries: Map<T['id'], T>;
135
+ create(entry: T): Promise<T>;
136
+ getById(id: T['id']): Promise<T | null>;
137
+ update(id: T['id'], updater: (entry: T) => Promisable<Partial<T>>): Promise<T>;
138
+ deleteById(id: T['id']): Promise<boolean>;
139
+ destroy(): Promise<void>;
140
+ protected iterEntries(): AsyncGenerator<T, void, unknown>;
141
+ protected iterIds(): AsyncGenerator<T["id"], void, unknown>;
142
+ }
143
+
144
+ declare class SingleEntryMemDb<T> {
145
+ protected entry: T | null;
146
+ constructor(initialEntry?: T | null);
147
+ isInited(): boolean;
148
+ read(): T;
149
+ write(updaterOrEntry: T | ((entry: T) => Partial<T>)): T;
150
+ delete(): void;
151
+ }
152
+
153
+ export { FileType, MultiEntryFileDb, MultiEntryMemDb, SingleEntryFileDb, SingleEntryMemDb };
154
+ export type { DeleteManyOutput, FileMeta, Id, Identifiable, JsonEntryParser, MultiEntryFileDbOptions, PaginationInput, PredicateFn, Promisable };