/
index.d.ts
197 lines (159 loc) · 4.43 KB
/
index.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
export type Primary = number;
export interface GenericDocument {}
export type Document<T extends GenericDocument> = {
$: Primary;
} & T;
export interface Documents<T> {
skip(num: number): Documents<T>;
take(num: number): Documents<T>;
next(): Document<T> | void;
end(): boolean;
collect(): Document<T>[];
count(): number;
}
export type KeyType
= 'int'
| 'float'
| 'bool'
| 'string'
| 'binary'
;
export type KeyData = number | string | boolean | ArrayBufferLike;
export type Value = any;
export type IndexKind = 'index' | 'unique';
export interface KeyField {
// field path
path: string,
// key type
key: KeyType,
// index kind
kind: IndexKind,
}
export type KeyFields = KeyField[];
export type Filter
= FilterCond
| { [field: string]: FilterComp }
| FilterNone
;
export type FilterCond
= FilterAnd
| FilterOr
| FilterNot
;
export interface FilterAnd { $and: Filter[] }
export interface FilterOr { $or: Filter[] }
export interface FilterNot { $not: Filter }
export type FilterComp
= FilterEq
| FilterIn
| FilterLt
| FilterLe
| FilterGt
| FilterGe
| FilterBw
| FilterHas
;
export interface FilterEq { $eq: KeyData }
export interface FilterIn { $in: KeyData[] }
export interface FilterLt { $lt: KeyData }
export interface FilterLe { $le: KeyData }
export interface FilterGt { $gt: KeyData }
export interface FilterGe { $ge: KeyData }
export interface FilterBw { $in: [KeyData, boolean, KeyData, boolean] }
export type FilterHas = '$has';
export type FilterNone = null;
export type Order
= OrderByPrimary
| OrderByField;
export type OrderByPrimary = OrderKind;
export type OrderByField = [string, OrderKind];
export type OrderKind = '$asc' | '$desc';
export type Modify = [string, Action][];
export type Action
= ActionSet
| ActionDelete
| ActionAdd
| ActionSub
| ActionMul
| ActionDiv
| ActionToggle
| ActionReplace
| ActionMerge
;
export interface ActionSet { $set: Value }
export type ActionDelete = '$delete';
export interface ActionAdd { $add: Value }
export interface ActionSub { $sub: Value }
export interface ActionMul { $mul: Value }
export interface ActionDiv { $div: Value }
export type ActionToggle = '$toggle';
export interface ActionReplace { $replace: [string, string] }
export interface ActionSplice { $splice: [number, number, ...Value[]] }
export interface ActionMerge { $merge: Value }
// Storage info
export interface Info {
map_size: number,
last_page: number,
last_transaction: number,
max_readers: number,
num_readers: number,
}
// Storage stats
export interface Stats {
page_size: number,
btree_depth: number,
branch_pages: number,
leaf_pages: number,
overflow_pages: number,
data_entries: number,
}
// Storage options
export interface Options {
// options
map_size?: number,
max_readers?: number,
max_dbs?: number,
// flags
map_async?: boolean,
no_lock?: boolean,
no_mem_init?: boolean,
no_meta_sync?: boolean,
no_read_ahead?: boolean,
no_sub_dir?: boolean,
no_sync?: boolean,
no_tls?: boolean,
read_only?: boolean,
write_map?: boolean,
}
// Storage handle interface
export class Storage {
constructor(path: string, opts?: Options);
get_info(): Info;
get_stats(): Stats;
has_collection(name: string): boolean;
collection(name: string): Collection;
drop_collection(name: string): boolean;
get_collections(): string[];
}
// Collection handle interface
export class Collection {
constructor(storage: Storage, name: string);
insert<T extends GenericDocument>(doc: T): Primary;
find<T extends GenericDocument>(filter: Filter, order?: Order): Documents<T>;
update(filter: Filter, modify: Modify): number;
remove(filter: Filter): number;
dump<T extends GenericDocument>(): Documents<T>;
load<T extends GenericDocument>(docs: Documents<T>): number;
purge(): void;
has(id: Primary): boolean;
get<T extends GenericDocument>(id: Primary): T | null;
put<T extends GenericDocument>(doc: T): void;
delete(id: Primary): boolean;
get_indexes(): KeyFields;
set_indexes(indexes: KeyFields): void;
has_index(path: string): void;
ensure_index(path: string, kind: IndexKind, key: KeyType): boolean;
drop_index(path: string): boolean;
}
// Get openned databases
export function openned(): string[];