klauz-db 0.5.2 → 1.0.5
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 +363 -615
- package/dist/index.cjs +395 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.mts +84 -0
- package/dist/index.d.ts +84 -0
- package/dist/index.mjs +354 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +27 -26
- package/lib/Collection.d.ts +0 -15
- package/lib/Collection.js +0 -242
- package/lib/Collection.js.map +0 -1
- package/lib/Error.d.ts +0 -3
- package/lib/Error.js +0 -18
- package/lib/Error.js.map +0 -1
- package/lib/Klauz.d.ts +0 -7
- package/lib/Klauz.js +0 -29
- package/lib/Klauz.js.map +0 -1
- package/lib/Types.d.ts +0 -41
- package/lib/Types.js +0 -3
- package/lib/Types.js.map +0 -1
- package/lib/index.d.ts +0 -1
- package/lib/index.js +0 -18
- package/lib/index.js.map +0 -1
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,395 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/index.ts
|
|
31
|
+
var index_exports = {};
|
|
32
|
+
__export(index_exports, {
|
|
33
|
+
KlauzDB: () => KlauzDB,
|
|
34
|
+
KlauzError: () => KlauzError,
|
|
35
|
+
KlauzNotFoundError: () => KlauzNotFoundError,
|
|
36
|
+
KlauzStorageError: () => KlauzStorageError,
|
|
37
|
+
KlauzValidationError: () => KlauzValidationError
|
|
38
|
+
});
|
|
39
|
+
module.exports = __toCommonJS(index_exports);
|
|
40
|
+
|
|
41
|
+
// src/collection.ts
|
|
42
|
+
var import_node_path2 = require("path");
|
|
43
|
+
|
|
44
|
+
// src/errors/error-message.ts
|
|
45
|
+
var import_zod = require("zod");
|
|
46
|
+
|
|
47
|
+
// src/errors/klauz-error.ts
|
|
48
|
+
var KlauzError = class extends Error {
|
|
49
|
+
constructor(message, code = "KLAUZ_ERROR", options) {
|
|
50
|
+
super(message, options);
|
|
51
|
+
this.code = code;
|
|
52
|
+
this.name = "KlauzError";
|
|
53
|
+
}
|
|
54
|
+
code;
|
|
55
|
+
};
|
|
56
|
+
var KlauzValidationError = class extends KlauzError {
|
|
57
|
+
constructor(message, options) {
|
|
58
|
+
super(message, "KLAUZ_VALIDATION_ERROR", options);
|
|
59
|
+
this.name = "KlauzValidationError";
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
var KlauzStorageError = class extends KlauzError {
|
|
63
|
+
constructor(message, options) {
|
|
64
|
+
super(message, "KLAUZ_STORAGE_ERROR", options);
|
|
65
|
+
this.name = "KlauzStorageError";
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
var KlauzNotFoundError = class extends KlauzError {
|
|
69
|
+
constructor(message, options) {
|
|
70
|
+
super(message, "KLAUZ_NOT_FOUND", options);
|
|
71
|
+
this.name = "KlauzNotFoundError";
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
// src/errors/error-message.ts
|
|
76
|
+
function errorMessage(err) {
|
|
77
|
+
if (err instanceof import_zod.ZodError) {
|
|
78
|
+
const error = err.errors.at(-1);
|
|
79
|
+
return {
|
|
80
|
+
error: error?.message ?? "Invalid params",
|
|
81
|
+
code: "KLAUZ_VALIDATION_ERROR"
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
if (err instanceof KlauzError || err instanceof KlauzValidationError) {
|
|
85
|
+
return {
|
|
86
|
+
error: err.message,
|
|
87
|
+
code: err.code
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
if (err instanceof Error) {
|
|
91
|
+
return {
|
|
92
|
+
error: err.message,
|
|
93
|
+
code: "KLAUZ_ERROR"
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
return {
|
|
97
|
+
error: "Unknown error",
|
|
98
|
+
code: "KLAUZ_ERROR"
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// src/schemas/collection-schemas.ts
|
|
103
|
+
var import_zod2 = __toESM(require("zod"));
|
|
104
|
+
var collectionNamePattern = /^[a-zA-Z0-9_-]+$/;
|
|
105
|
+
function parseSchema(schema, data) {
|
|
106
|
+
const result = schema.safeParse(data);
|
|
107
|
+
if (!result.success) {
|
|
108
|
+
const error = result.error.errors.at(-1);
|
|
109
|
+
throw new KlauzValidationError(error?.message ?? "Invalid params", {
|
|
110
|
+
cause: result.error
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
return result.data;
|
|
114
|
+
}
|
|
115
|
+
var klauzPropsSchema = import_zod2.default.object({
|
|
116
|
+
path: import_zod2.default.string().trim().min(1)
|
|
117
|
+
}).strict();
|
|
118
|
+
var collectionNameSchema = import_zod2.default.string().trim().min(1).regex(collectionNamePattern, "Collection name must contain only letters, numbers, hyphens and underscores");
|
|
119
|
+
var collectionDataSchema = import_zod2.default.record(
|
|
120
|
+
import_zod2.default.string(),
|
|
121
|
+
import_zod2.default.unknown(),
|
|
122
|
+
{ message: `Content must be a object. Use 'addMany' method, to insert a new array` }
|
|
123
|
+
);
|
|
124
|
+
var collectionDataArraySchema = import_zod2.default.array(collectionDataSchema);
|
|
125
|
+
var findAllOptionsSchema = import_zod2.default.object({
|
|
126
|
+
hideInfo: import_zod2.default.array(import_zod2.default.string().min(1)).optional()
|
|
127
|
+
}).strict().optional();
|
|
128
|
+
var findOptionsSchema = import_zod2.default.object({
|
|
129
|
+
where: import_zod2.default.function(),
|
|
130
|
+
hideInfo: import_zod2.default.array(import_zod2.default.string().min(1)).optional()
|
|
131
|
+
}).strict();
|
|
132
|
+
var updateOptionsSchema = import_zod2.default.object({
|
|
133
|
+
where: import_zod2.default.function(),
|
|
134
|
+
values: collectionDataSchema
|
|
135
|
+
}).strict();
|
|
136
|
+
var deleteOptionsSchema = import_zod2.default.object({
|
|
137
|
+
where: import_zod2.default.function()
|
|
138
|
+
}).strict();
|
|
139
|
+
|
|
140
|
+
// src/storage/json-storage.ts
|
|
141
|
+
var import_promises = require("fs/promises");
|
|
142
|
+
var import_node_path = require("path");
|
|
143
|
+
var JsonStorage = class {
|
|
144
|
+
constructor(filePath) {
|
|
145
|
+
this.filePath = filePath;
|
|
146
|
+
}
|
|
147
|
+
filePath;
|
|
148
|
+
async exists() {
|
|
149
|
+
try {
|
|
150
|
+
await (0, import_promises.access)(this.filePath);
|
|
151
|
+
return true;
|
|
152
|
+
} catch {
|
|
153
|
+
return false;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
async read() {
|
|
157
|
+
const content = await (0, import_promises.readFile)(this.filePath, {
|
|
158
|
+
encoding: "utf-8"
|
|
159
|
+
});
|
|
160
|
+
return JSON.parse(content);
|
|
161
|
+
}
|
|
162
|
+
async write(data) {
|
|
163
|
+
await (0, import_promises.mkdir)((0, import_node_path.dirname)(this.filePath), {
|
|
164
|
+
recursive: true
|
|
165
|
+
});
|
|
166
|
+
await (0, import_promises.writeFile)(this.filePath, JSON.stringify(data, null, 2));
|
|
167
|
+
}
|
|
168
|
+
async delete() {
|
|
169
|
+
await (0, import_promises.unlink)(this.filePath);
|
|
170
|
+
}
|
|
171
|
+
};
|
|
172
|
+
|
|
173
|
+
// src/collection.ts
|
|
174
|
+
var Collection = class {
|
|
175
|
+
constructor(props) {
|
|
176
|
+
this.props = props;
|
|
177
|
+
const fileExtension = ".json";
|
|
178
|
+
const { path, name } = props;
|
|
179
|
+
this.#name = name;
|
|
180
|
+
this.#path = (0, import_node_path2.join)(path, `.${name}${fileExtension}`);
|
|
181
|
+
this.#storage = new JsonStorage(this.#path);
|
|
182
|
+
}
|
|
183
|
+
props;
|
|
184
|
+
#name;
|
|
185
|
+
#path;
|
|
186
|
+
#storage;
|
|
187
|
+
#content = {};
|
|
188
|
+
async init() {
|
|
189
|
+
await this.#load();
|
|
190
|
+
return this;
|
|
191
|
+
}
|
|
192
|
+
async #load() {
|
|
193
|
+
if (!await this.#storage.exists()) {
|
|
194
|
+
const content = {
|
|
195
|
+
collection_name: this.#name,
|
|
196
|
+
created_at: (/* @__PURE__ */ new Date()).toISOString(),
|
|
197
|
+
updated_at: (/* @__PURE__ */ new Date()).toISOString(),
|
|
198
|
+
data: []
|
|
199
|
+
};
|
|
200
|
+
this.#content = content;
|
|
201
|
+
await this.#save(this.#content);
|
|
202
|
+
return;
|
|
203
|
+
}
|
|
204
|
+
try {
|
|
205
|
+
const content = await this.#storage.read();
|
|
206
|
+
this.#content = this.#normalizeContent(content);
|
|
207
|
+
} catch (err) {
|
|
208
|
+
throw new KlauzStorageError("Invalid collection JSON file", { cause: err });
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
#normalizeContent(content) {
|
|
212
|
+
const updatedAt = content.updated_at ?? content.last_interaction ?? content.created_at;
|
|
213
|
+
return {
|
|
214
|
+
collection_name: content.collection_name,
|
|
215
|
+
created_at: content.created_at,
|
|
216
|
+
updated_at: updatedAt,
|
|
217
|
+
data: content.data
|
|
218
|
+
};
|
|
219
|
+
}
|
|
220
|
+
async #save(content) {
|
|
221
|
+
await this.#storage.write(content);
|
|
222
|
+
}
|
|
223
|
+
async #setCollectionDataValue(data) {
|
|
224
|
+
this.#content.updated_at = (/* @__PURE__ */ new Date()).toISOString();
|
|
225
|
+
this.#content.data.push(data);
|
|
226
|
+
await this.#save(this.#content);
|
|
227
|
+
}
|
|
228
|
+
async #setCollectionData(data) {
|
|
229
|
+
this.#content.updated_at = (/* @__PURE__ */ new Date()).toISOString();
|
|
230
|
+
this.#content.data = data;
|
|
231
|
+
await this.#save(this.#content);
|
|
232
|
+
}
|
|
233
|
+
async #getCollectionData() {
|
|
234
|
+
await this.#load();
|
|
235
|
+
return this.#content.data;
|
|
236
|
+
}
|
|
237
|
+
#getLastNumericId(data = this.#content.data) {
|
|
238
|
+
const lastObject = data.findLast((obj) => typeof obj._zid === "number");
|
|
239
|
+
return lastObject?._zid ?? 0;
|
|
240
|
+
}
|
|
241
|
+
#removeInternalFields(data) {
|
|
242
|
+
const safeData = { ...data };
|
|
243
|
+
Reflect.deleteProperty(safeData, "_zid");
|
|
244
|
+
return safeData;
|
|
245
|
+
}
|
|
246
|
+
#cloneData(data) {
|
|
247
|
+
return data.map((obj) => ({ ...obj }));
|
|
248
|
+
}
|
|
249
|
+
#hideInfo(data, hideInfo) {
|
|
250
|
+
const output = this.#cloneData(data);
|
|
251
|
+
if (!hideInfo || hideInfo.length === 0) return output;
|
|
252
|
+
for (const obj of output) {
|
|
253
|
+
for (const info of hideInfo) {
|
|
254
|
+
Reflect.deleteProperty(obj, info);
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
return output;
|
|
258
|
+
}
|
|
259
|
+
get information() {
|
|
260
|
+
return this.#getInformation();
|
|
261
|
+
}
|
|
262
|
+
async #getInformation() {
|
|
263
|
+
await this.#load();
|
|
264
|
+
return {
|
|
265
|
+
collection_name: this.#content.collection_name,
|
|
266
|
+
created_at: this.#content.created_at,
|
|
267
|
+
updated_at: this.#content.updated_at
|
|
268
|
+
};
|
|
269
|
+
}
|
|
270
|
+
async add(data) {
|
|
271
|
+
try {
|
|
272
|
+
if (arguments.length > 1) throw Error("Invalid params");
|
|
273
|
+
await this.#load();
|
|
274
|
+
const obj = this.#removeInternalFields(parseSchema(collectionDataSchema, data));
|
|
275
|
+
const lastId = this.#getLastNumericId();
|
|
276
|
+
Reflect.set(obj, "_zid", lastId + 1);
|
|
277
|
+
await this.#setCollectionDataValue(obj);
|
|
278
|
+
return { ...obj };
|
|
279
|
+
} catch (err) {
|
|
280
|
+
return errorMessage(err);
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
async addMany(data) {
|
|
284
|
+
try {
|
|
285
|
+
if (arguments.length > 1) throw Error("Invalid params");
|
|
286
|
+
await this.#load();
|
|
287
|
+
const objs = parseSchema(collectionDataArraySchema, data).map((obj) => this.#removeInternalFields(obj));
|
|
288
|
+
let lastId = this.#getLastNumericId();
|
|
289
|
+
for (const obj of objs) {
|
|
290
|
+
Reflect.set(obj, "_zid", ++lastId);
|
|
291
|
+
}
|
|
292
|
+
await this.#setCollectionData([
|
|
293
|
+
...this.#content.data,
|
|
294
|
+
...objs
|
|
295
|
+
]);
|
|
296
|
+
return this.#cloneData(objs);
|
|
297
|
+
} catch (err) {
|
|
298
|
+
return errorMessage(err);
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
async findAll(options) {
|
|
302
|
+
try {
|
|
303
|
+
if (arguments.length > 1) throw Error("Invalid params");
|
|
304
|
+
const collectionData = await this.#getCollectionData();
|
|
305
|
+
const zOpts = parseSchema(findAllOptionsSchema, options);
|
|
306
|
+
return this.#hideInfo(collectionData, zOpts?.hideInfo);
|
|
307
|
+
} catch (err) {
|
|
308
|
+
return errorMessage(err);
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
async find(options) {
|
|
312
|
+
try {
|
|
313
|
+
if (arguments.length > 1) throw Error("Invalid params");
|
|
314
|
+
const { where, ...opts } = parseSchema(findOptionsSchema, options);
|
|
315
|
+
const collectionData = await this.#getCollectionData();
|
|
316
|
+
const output = collectionData.filter((obj) => where.call(void 0, obj));
|
|
317
|
+
return this.#hideInfo(output, opts.hideInfo);
|
|
318
|
+
} catch (err) {
|
|
319
|
+
return errorMessage(err);
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
async update(options) {
|
|
323
|
+
try {
|
|
324
|
+
if (arguments.length > 1) throw Error("Invalid params");
|
|
325
|
+
const { where, values } = parseSchema(updateOptionsSchema, options);
|
|
326
|
+
const safeValues = this.#removeInternalFields(values ?? {});
|
|
327
|
+
const collectionData = await this.#getCollectionData();
|
|
328
|
+
const output = [];
|
|
329
|
+
for (const obj of collectionData) {
|
|
330
|
+
if (where.call(void 0, obj)) {
|
|
331
|
+
Object.assign(obj, {
|
|
332
|
+
...obj,
|
|
333
|
+
...safeValues,
|
|
334
|
+
_zid: obj._zid
|
|
335
|
+
});
|
|
336
|
+
output.push({ ...obj });
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
if (output.length === 0) throw Error("Data not found");
|
|
340
|
+
await this.#setCollectionData(collectionData);
|
|
341
|
+
return output;
|
|
342
|
+
} catch (err) {
|
|
343
|
+
return errorMessage(err);
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
async delete(options) {
|
|
347
|
+
try {
|
|
348
|
+
if (arguments.length > 1) throw Error("Invalid params");
|
|
349
|
+
const { where } = parseSchema(deleteOptionsSchema, options);
|
|
350
|
+
const collectionData = await this.#getCollectionData();
|
|
351
|
+
const dataPostDelete = collectionData.filter((obj) => !where(obj));
|
|
352
|
+
if (collectionData.length === dataPostDelete.length) return;
|
|
353
|
+
await this.#setCollectionData(dataPostDelete);
|
|
354
|
+
} catch (err) {
|
|
355
|
+
return errorMessage(err);
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
async reset() {
|
|
359
|
+
try {
|
|
360
|
+
await this.#load();
|
|
361
|
+
await this.#setCollectionData([]);
|
|
362
|
+
} catch (err) {
|
|
363
|
+
return errorMessage(err);
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
async drop() {
|
|
367
|
+
await this.#storage.delete();
|
|
368
|
+
}
|
|
369
|
+
};
|
|
370
|
+
|
|
371
|
+
// src/klauz-db.ts
|
|
372
|
+
var KlauzDB = class {
|
|
373
|
+
path = "";
|
|
374
|
+
constructor(props) {
|
|
375
|
+
const { path } = parseSchema(klauzPropsSchema, props);
|
|
376
|
+
this.path = path;
|
|
377
|
+
}
|
|
378
|
+
async createCollection(collectionName) {
|
|
379
|
+
const name = parseSchema(collectionNameSchema, collectionName);
|
|
380
|
+
const collection = new Collection({
|
|
381
|
+
name,
|
|
382
|
+
path: this.path
|
|
383
|
+
});
|
|
384
|
+
return collection.init();
|
|
385
|
+
}
|
|
386
|
+
};
|
|
387
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
388
|
+
0 && (module.exports = {
|
|
389
|
+
KlauzDB,
|
|
390
|
+
KlauzError,
|
|
391
|
+
KlauzNotFoundError,
|
|
392
|
+
KlauzStorageError,
|
|
393
|
+
KlauzValidationError
|
|
394
|
+
});
|
|
395
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/collection.ts","../src/errors/error-message.ts","../src/errors/klauz-error.ts","../src/schemas/collection-schemas.ts","../src/storage/json-storage.ts","../src/klauz-db.ts"],"sourcesContent":["export * from './klauz-db'\nexport * from './types'\nexport * from './errors/klauz-error'\n","import { join } from \"node:path\";\r\nimport { errorMessage } from \"./errors/error-message\";\r\nimport { KlauzStorageError } from \"./errors/klauz-error\";\r\nimport {\r\n collectionDataArraySchema,\r\n collectionDataSchema,\r\n deleteOptionsSchema,\r\n findAllOptionsSchema,\r\n findOptionsSchema,\r\n parseSchema,\r\n updateOptionsSchema\r\n} from \"./schemas/collection-schemas\";\r\nimport { JsonStorage } from \"./storage/json-storage\";\r\nimport {\r\n CollectionContent,\n CollectionData,\n CollectionDataWithZID,\n CollectionProps,\n DeleteOptions,\n FindOptions,\n FindOptionsWithoutWhere,\n KzObject,\n LegacyCollectionContent,\n Output,\n UpdateOptions,\n ZID\n} from \"./types\";\r\n\r\nexport class Collection {\r\n readonly #name: string\r\n readonly #path: string\r\n readonly #storage: JsonStorage\r\n #content = {} as CollectionContent\r\n\r\n constructor(private readonly props: CollectionProps) {\r\n const fileExtension = '.json'\r\n const { path, name } = props\r\n this.#name = name\r\n this.#path = join(path, `.${name}${fileExtension}`)\r\n this.#storage = new JsonStorage(this.#path)\r\n }\r\n\r\n async init(): Promise<this> {\r\n await this.#load()\r\n return this\r\n }\r\n\r\n async #load(): Promise<void> {\r\n if (!await this.#storage.exists()) {\r\n const content: CollectionContent = {\r\n collection_name: this.#name,\n created_at: new Date().toISOString(),\n updated_at: new Date().toISOString(),\n data: []\n }\n this.#content = content\r\n await this.#save(this.#content)\r\n return\r\n }\n\n try {\n const content = await this.#storage.read<LegacyCollectionContent>()\n this.#content = this.#normalizeContent(content)\n } catch (err) {\n throw new KlauzStorageError('Invalid collection JSON file', { cause: err })\n }\n }\n\n #normalizeContent(content: LegacyCollectionContent): CollectionContent {\n const updatedAt = content.updated_at ?? content.last_interaction ?? content.created_at\n\n return {\n collection_name: content.collection_name,\n created_at: content.created_at,\n updated_at: updatedAt,\n data: content.data\n }\n }\n\r\n async #save(content: CollectionContent): Promise<void> {\r\n await this.#storage.write(content)\r\n }\r\n\r\n async #setCollectionDataValue(data: CollectionDataWithZID): Promise<void> {\n this.#content.updated_at = new Date().toISOString()\n this.#content.data.push(data)\n await this.#save(this.#content)\n }\n\n async #setCollectionData(data: CollectionDataWithZID[]): Promise<void> {\n this.#content.updated_at = new Date().toISOString()\n this.#content.data = data\n await this.#save(this.#content)\n }\r\n\r\n async #getCollectionData(): Promise<CollectionDataWithZID[]> {\r\n await this.#load()\r\n return this.#content.data\r\n }\r\n\r\n #getLastNumericId(data = this.#content.data): ZID {\r\n const lastObject = data.findLast((obj: CollectionDataWithZID) => typeof obj._zid === 'number')\r\n return lastObject?._zid ?? 0\r\n }\r\n\r\n #removeInternalFields(data: CollectionData): CollectionData {\r\n const safeData = { ...data }\r\n Reflect.deleteProperty(safeData, '_zid')\r\n return safeData\r\n }\r\n\r\n #cloneData(data: CollectionDataWithZID[]): CollectionDataWithZID[] {\r\n return data.map((obj) => ({ ...obj }))\r\n }\r\n\r\n #hideInfo(data: CollectionDataWithZID[], hideInfo?: string[]): CollectionDataWithZID[] {\r\n const output = this.#cloneData(data)\r\n\r\n if (!hideInfo || hideInfo.length === 0) return output\r\n\r\n for (const obj of output) {\r\n for (const info of hideInfo) {\r\n Reflect.deleteProperty(obj, info)\r\n }\r\n }\r\n\r\n return output\r\n }\r\n\r\n get information(): Promise<Omit<CollectionContent, 'data'>> {\r\n return this.#getInformation()\r\n }\r\n\r\n async #getInformation(): Promise<Omit<CollectionContent, 'data'>> {\r\n await this.#load()\r\n return {\r\n collection_name: this.#content.collection_name,\n created_at: this.#content.created_at,\n updated_at: this.#content.updated_at,\n }\n }\n\r\n async add(data: CollectionData): Promise<Output<CollectionDataWithZID>> {\r\n try {\r\n if (arguments.length > 1) throw Error('Invalid params')\r\n await this.#load()\r\n const obj = this.#removeInternalFields(parseSchema(collectionDataSchema, data)) as CollectionDataWithZID\r\n const lastId = this.#getLastNumericId()\r\n Reflect.set(obj, '_zid', lastId + 1)\r\n await this.#setCollectionDataValue(obj)\r\n return { ...obj }\r\n } catch (err) {\r\n return errorMessage(err)\r\n }\r\n }\r\n\r\n async addMany(data: CollectionData[]): Promise<Output<CollectionDataWithZID[]>> {\r\n try {\r\n if (arguments.length > 1) throw Error('Invalid params')\r\n await this.#load()\r\n const objs = parseSchema(collectionDataArraySchema, data).map((obj) => this.#removeInternalFields(obj)) as CollectionDataWithZID[]\r\n let lastId = this.#getLastNumericId()\r\n\r\n for (const obj of objs) {\r\n Reflect.set(obj, '_zid', ++lastId)\r\n }\r\n\r\n await this.#setCollectionData([\r\n ...this.#content.data,\r\n ...objs\r\n ])\r\n\r\n return this.#cloneData(objs)\r\n } catch (err) {\r\n return errorMessage(err)\r\n }\r\n }\r\n\r\n async findAll(options?: FindOptionsWithoutWhere): Promise<Output<CollectionDataWithZID[]>> {\r\n try {\r\n if (arguments.length > 1) throw Error('Invalid params')\r\n const collectionData = await this.#getCollectionData()\r\n const zOpts = parseSchema(findAllOptionsSchema, options) as FindOptionsWithoutWhere\r\n return this.#hideInfo(collectionData, zOpts?.hideInfo)\r\n } catch (err) {\r\n return errorMessage(err)\r\n }\r\n }\r\n\r\n async find<T>(options: FindOptions<KzObject<T>>): Promise<Output<CollectionDataWithZID[]>> {\r\n try {\r\n if (arguments.length > 1) throw Error('Invalid params')\r\n const { where, ...opts } = parseSchema(findOptionsSchema, options) as FindOptions<T>\r\n const collectionData = await this.#getCollectionData() as any\r\n const output = collectionData.filter((obj: KzObject<T>) => where.call(undefined, obj)) as CollectionDataWithZID[]\r\n return this.#hideInfo(output, opts.hideInfo)\r\n } catch (err) {\r\n return errorMessage(err)\r\n }\r\n }\r\n\r\n async update<T>(options: UpdateOptions<KzObject<T>>): Promise<Output<CollectionDataWithZID[]>> {\r\n try {\r\n if (arguments.length > 1) throw Error('Invalid params')\r\n const { where, values } = parseSchema(updateOptionsSchema, options) as UpdateOptions<T>\r\n const safeValues = this.#removeInternalFields(values ?? {})\r\n const collectionData = await this.#getCollectionData() as any\r\n const output = [] as CollectionDataWithZID[]\r\n for (const obj of collectionData) {\r\n if (where.call(undefined, obj)) {\r\n Object.assign(obj, {\r\n ...obj,\r\n ...safeValues,\r\n _zid: obj._zid\r\n })\r\n output.push({ ...obj })\r\n }\r\n }\r\n if (output.length === 0) throw Error('Data not found')\r\n await this.#setCollectionData(collectionData)\r\n return output\r\n } catch (err) {\r\n return errorMessage(err)\r\n }\r\n }\r\n\r\n async delete<T>(options: DeleteOptions<KzObject<T>>): Promise<Output<void>> {\r\n try {\r\n if (arguments.length > 1) throw Error('Invalid params')\r\n const { where } = parseSchema(deleteOptionsSchema, options) as DeleteOptions<T>\r\n const collectionData = await this.#getCollectionData() as any\r\n const dataPostDelete = collectionData.filter((obj: KzObject<T>) => !where(obj)) as CollectionDataWithZID[]\r\n if (collectionData.length === dataPostDelete.length) return\r\n await this.#setCollectionData(dataPostDelete)\r\n } catch (err) {\r\n return errorMessage(err)\r\n }\r\n }\r\n\r\n async reset(): Promise<Output<void>> {\r\n try {\r\n await this.#load()\r\n await this.#setCollectionData([])\r\n } catch (err) {\r\n return errorMessage(err)\r\n }\r\n }\r\n\r\n async drop(): Promise<void> {\r\n await this.#storage.delete()\r\n }\r\n}\r\n","import { ZodError } from \"zod\"\nimport { KlauzError, KlauzValidationError } from \"./klauz-error\"\n\nexport function errorMessage(err: unknown) {\n if (err instanceof ZodError) {\n const error = err.errors.at(-1)\n return {\n error: error?.message ?? 'Invalid params',\n code: 'KLAUZ_VALIDATION_ERROR'\n }\n }\n\n if (err instanceof KlauzError || err instanceof KlauzValidationError) {\n return {\n error: err.message,\n code: err.code\n }\n }\n\n if (err instanceof Error) {\n return {\n error: err.message,\n code: 'KLAUZ_ERROR'\n }\n }\n\n return {\n error: 'Unknown error',\n code: 'KLAUZ_ERROR'\n }\n}\n","export type KlauzErrorCode =\n | 'KLAUZ_ERROR'\n | 'KLAUZ_VALIDATION_ERROR'\n | 'KLAUZ_STORAGE_ERROR'\n | 'KLAUZ_NOT_FOUND'\n\nexport class KlauzError extends Error {\n constructor(\n message: string,\n public readonly code: KlauzErrorCode = 'KLAUZ_ERROR',\n options?: ErrorOptions\n ) {\n super(message, options)\n this.name = 'KlauzError'\n }\n}\n\nexport class KlauzValidationError extends KlauzError {\n constructor(message: string, options?: ErrorOptions) {\n super(message, 'KLAUZ_VALIDATION_ERROR', options)\n this.name = 'KlauzValidationError'\n }\n}\n\nexport class KlauzStorageError extends KlauzError {\n constructor(message: string, options?: ErrorOptions) {\n super(message, 'KLAUZ_STORAGE_ERROR', options)\n this.name = 'KlauzStorageError'\n }\n}\n\nexport class KlauzNotFoundError extends KlauzError {\n constructor(message: string, options?: ErrorOptions) {\n super(message, 'KLAUZ_NOT_FOUND', options)\n this.name = 'KlauzNotFoundError'\n }\n}\n","import z from 'zod'\nimport { KlauzValidationError } from '../errors/klauz-error'\n\nconst collectionNamePattern = /^[a-zA-Z0-9_-]+$/\n\nexport function parseSchema<TSchema extends z.ZodTypeAny>(schema: TSchema, data: unknown): z.infer<TSchema> {\n const result = schema.safeParse(data)\n\n if (!result.success) {\n const error = result.error.errors.at(-1)\n throw new KlauzValidationError(error?.message ?? 'Invalid params', {\n cause: result.error\n })\n }\n\n return result.data\n}\n\nexport const klauzPropsSchema = z.object({\n path: z.string().trim().min(1)\n}).strict()\n\nexport const collectionNameSchema = z\n .string()\n .trim()\n .min(1)\n .regex(collectionNamePattern, 'Collection name must contain only letters, numbers, hyphens and underscores')\n\nexport const collectionDataSchema = z.record(\n z.string(),\n z.unknown(),\n { message: `Content must be a object. Use 'addMany' method, to insert a new array` }\n)\n\nexport const collectionDataArraySchema = z.array(collectionDataSchema)\n\nexport const findAllOptionsSchema = z.object({\n hideInfo: z.array(z.string().min(1)).optional()\n}).strict().optional()\n\nexport const findOptionsSchema = z.object({\n where: z.function(),\n hideInfo: z.array(z.string().min(1)).optional()\n}).strict()\n\nexport const updateOptionsSchema = z.object({\n where: z.function(),\n values: collectionDataSchema\n}).strict()\n\nexport const deleteOptionsSchema = z.object({\n where: z.function()\n}).strict()\n","import { access, mkdir, readFile, unlink, writeFile } from 'node:fs/promises'\nimport { dirname } from 'node:path'\n\nexport class JsonStorage {\n constructor(private readonly filePath: string) {}\n\n async exists(): Promise<boolean> {\n try {\n await access(this.filePath)\n return true\n } catch {\n return false\n }\n }\n\n async read<T>(): Promise<T> {\n const content = await readFile(this.filePath, {\n encoding: 'utf-8'\n })\n\n return JSON.parse(content) as T\n }\n\n async write<T>(data: T): Promise<void> {\n await mkdir(dirname(this.filePath), {\n recursive: true\n })\n\n await writeFile(this.filePath, JSON.stringify(data, null, 2))\n }\n\n async delete(): Promise<void> {\n await unlink(this.filePath)\n }\n}\n","import { Collection } from \"./collection\";\nimport { collectionNameSchema, klauzPropsSchema, parseSchema } from \"./schemas/collection-schemas\";\nimport { KlauzProps } from \"./types\";\n\nexport class KlauzDB {\n public path: string = ''\n \n constructor(props: KlauzProps) {\n const { path } = parseSchema(klauzPropsSchema, props)\n this.path = path\n }\n\n async createCollection(collectionName: string): Promise<Collection> {\n const name = parseSchema(collectionNameSchema, collectionName)\n const collection = new Collection({\n name,\n path: this.path\n })\n return collection.init()\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,IAAAA,oBAAqB;;;ACArB,iBAAyB;;;ACMlB,IAAM,aAAN,cAAyB,MAAM;AAAA,EAClC,YACI,SACgB,OAAuB,eACvC,SACF;AACE,UAAM,SAAS,OAAO;AAHN;AAIhB,SAAK,OAAO;AAAA,EAChB;AAAA,EALoB;AAMxB;AAEO,IAAM,uBAAN,cAAmC,WAAW;AAAA,EACjD,YAAY,SAAiB,SAAwB;AACjD,UAAM,SAAS,0BAA0B,OAAO;AAChD,SAAK,OAAO;AAAA,EAChB;AACJ;AAEO,IAAM,oBAAN,cAAgC,WAAW;AAAA,EAC9C,YAAY,SAAiB,SAAwB;AACjD,UAAM,SAAS,uBAAuB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AAM,OAAU,SAA4D;AACxE,QAAI;AACA,UAAI,UAAU,SAAS,EAAG,OAAM,MAAM,gBAAgB;AACtD,YAAM,EAAE,MAAM,IAAI,YAAY,qBAAqB,OAAO;AAC1D,YAAM,iBAAiB,MAAM,KAAK,mBAAmB;AACrD,YAAM,iBAAiB,eAAe,OAAO,CAAC,QAAqB,CAAC,MAAM,GAAG,CAAC;AAC9E,UAAI,eAAe,WAAW,eAAe,OAAQ;AACrD,YAAM,KAAK,mBAAmB,cAAc;AAAA,IAChD,SAAS,KAAK;AACV,aAAO,aAAa,GAAG;AAAA,IAC3B;AAAA,EACJ;AAAA,EAEA,MAAM,QAA+B;AACjC,QAAI;AACA,YAAM,KAAK,MAAM;AACjB,YAAM,KAAK,mBAAmB,CAAC,CAAC;AAAA,IACpC,SAAS,KAAK;AACV,aAAO,aAAa,GAAG;AAAA,IAC3B;AAAA,EACJ;AAAA,EAEA,MAAM,OAAsB;AACxB,UAAM,KAAK,SAAS,OAAO;AAAA,EAC/B;AACJ;;;AKvPO,IAAM,UAAN,MAAc;AAAA,EACV,OAAe;AAAA,EAEtB,YAAY,OAAmB;AAC3B,UAAM,EAAE,KAAK,IAAI,YAAY,kBAAkB,KAAK;AACpD,SAAK,OAAO;AAAA,EAChB;AAAA,EAEA,MAAM,iBAAiB,gBAA6C;AAChE,UAAM,OAAO,YAAY,sBAAsB,cAAc;AAC7D,UAAM,aAAa,IAAI,WAAW;AAAA,MAC9B;AAAA,MACA,MAAM,KAAK;AAAA,IACf,CAAC;AACD,WAAO,WAAW,KAAK;AAAA,EAC3B;AACJ;","names":["import_node_path","import_zod","z"]}
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
type ZID = number;
|
|
2
|
+
type CollectionProps = {
|
|
3
|
+
path: string;
|
|
4
|
+
name: string;
|
|
5
|
+
};
|
|
6
|
+
type CollectionContent = {
|
|
7
|
+
collection_name: string;
|
|
8
|
+
created_at: string;
|
|
9
|
+
updated_at: string;
|
|
10
|
+
data: Array<CollectionDataWithZID>;
|
|
11
|
+
};
|
|
12
|
+
type LegacyCollectionContent = Omit<CollectionContent, 'updated_at'> & {
|
|
13
|
+
updated_at?: string;
|
|
14
|
+
last_interaction?: string;
|
|
15
|
+
};
|
|
16
|
+
type CollectionData = Record<string, any>;
|
|
17
|
+
type CollectionDataWithZID = {
|
|
18
|
+
[K in keyof CollectionData]: CollectionData[K];
|
|
19
|
+
} & {
|
|
20
|
+
_zid?: ZID;
|
|
21
|
+
};
|
|
22
|
+
type KlauzProps = {
|
|
23
|
+
path: string;
|
|
24
|
+
};
|
|
25
|
+
type ErrorPayload = {
|
|
26
|
+
error: string;
|
|
27
|
+
code: string;
|
|
28
|
+
};
|
|
29
|
+
type Output<T> = T | ErrorPayload;
|
|
30
|
+
type KzObject<T> = T & {
|
|
31
|
+
_zid: ZID;
|
|
32
|
+
};
|
|
33
|
+
type Callback<T> = (obj: T) => any;
|
|
34
|
+
type FindOptions<T> = {
|
|
35
|
+
where: Callback<T>;
|
|
36
|
+
hideInfo?: Array<string>;
|
|
37
|
+
};
|
|
38
|
+
type FindOptionsWithoutWhere = Omit<FindOptions<any>, "where">;
|
|
39
|
+
type UpdateOptions<T> = {
|
|
40
|
+
where: Callback<T>;
|
|
41
|
+
values?: CollectionData;
|
|
42
|
+
};
|
|
43
|
+
type DeleteOptions<T> = {
|
|
44
|
+
where: Callback<T>;
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
declare class Collection {
|
|
48
|
+
#private;
|
|
49
|
+
private readonly props;
|
|
50
|
+
constructor(props: CollectionProps);
|
|
51
|
+
init(): Promise<this>;
|
|
52
|
+
get information(): Promise<Omit<CollectionContent, 'data'>>;
|
|
53
|
+
add(data: CollectionData): Promise<Output<CollectionDataWithZID>>;
|
|
54
|
+
addMany(data: CollectionData[]): Promise<Output<CollectionDataWithZID[]>>;
|
|
55
|
+
findAll(options?: FindOptionsWithoutWhere): Promise<Output<CollectionDataWithZID[]>>;
|
|
56
|
+
find<T>(options: FindOptions<KzObject<T>>): Promise<Output<CollectionDataWithZID[]>>;
|
|
57
|
+
update<T>(options: UpdateOptions<KzObject<T>>): Promise<Output<CollectionDataWithZID[]>>;
|
|
58
|
+
delete<T>(options: DeleteOptions<KzObject<T>>): Promise<Output<void>>;
|
|
59
|
+
reset(): Promise<Output<void>>;
|
|
60
|
+
drop(): Promise<void>;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
declare class KlauzDB {
|
|
64
|
+
path: string;
|
|
65
|
+
constructor(props: KlauzProps);
|
|
66
|
+
createCollection(collectionName: string): Promise<Collection>;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
type KlauzErrorCode = 'KLAUZ_ERROR' | 'KLAUZ_VALIDATION_ERROR' | 'KLAUZ_STORAGE_ERROR' | 'KLAUZ_NOT_FOUND';
|
|
70
|
+
declare class KlauzError extends Error {
|
|
71
|
+
readonly code: KlauzErrorCode;
|
|
72
|
+
constructor(message: string, code?: KlauzErrorCode, options?: ErrorOptions);
|
|
73
|
+
}
|
|
74
|
+
declare class KlauzValidationError extends KlauzError {
|
|
75
|
+
constructor(message: string, options?: ErrorOptions);
|
|
76
|
+
}
|
|
77
|
+
declare class KlauzStorageError extends KlauzError {
|
|
78
|
+
constructor(message: string, options?: ErrorOptions);
|
|
79
|
+
}
|
|
80
|
+
declare class KlauzNotFoundError extends KlauzError {
|
|
81
|
+
constructor(message: string, options?: ErrorOptions);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export { type Callback, type CollectionContent, type CollectionData, type CollectionDataWithZID, type CollectionProps, type DeleteOptions, type FindOptions, type FindOptionsWithoutWhere, KlauzDB, KlauzError, type KlauzErrorCode, KlauzNotFoundError, type KlauzProps, KlauzStorageError, KlauzValidationError, type KzObject, type LegacyCollectionContent, type Output, type UpdateOptions, type ZID };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
type ZID = number;
|
|
2
|
+
type CollectionProps = {
|
|
3
|
+
path: string;
|
|
4
|
+
name: string;
|
|
5
|
+
};
|
|
6
|
+
type CollectionContent = {
|
|
7
|
+
collection_name: string;
|
|
8
|
+
created_at: string;
|
|
9
|
+
updated_at: string;
|
|
10
|
+
data: Array<CollectionDataWithZID>;
|
|
11
|
+
};
|
|
12
|
+
type LegacyCollectionContent = Omit<CollectionContent, 'updated_at'> & {
|
|
13
|
+
updated_at?: string;
|
|
14
|
+
last_interaction?: string;
|
|
15
|
+
};
|
|
16
|
+
type CollectionData = Record<string, any>;
|
|
17
|
+
type CollectionDataWithZID = {
|
|
18
|
+
[K in keyof CollectionData]: CollectionData[K];
|
|
19
|
+
} & {
|
|
20
|
+
_zid?: ZID;
|
|
21
|
+
};
|
|
22
|
+
type KlauzProps = {
|
|
23
|
+
path: string;
|
|
24
|
+
};
|
|
25
|
+
type ErrorPayload = {
|
|
26
|
+
error: string;
|
|
27
|
+
code: string;
|
|
28
|
+
};
|
|
29
|
+
type Output<T> = T | ErrorPayload;
|
|
30
|
+
type KzObject<T> = T & {
|
|
31
|
+
_zid: ZID;
|
|
32
|
+
};
|
|
33
|
+
type Callback<T> = (obj: T) => any;
|
|
34
|
+
type FindOptions<T> = {
|
|
35
|
+
where: Callback<T>;
|
|
36
|
+
hideInfo?: Array<string>;
|
|
37
|
+
};
|
|
38
|
+
type FindOptionsWithoutWhere = Omit<FindOptions<any>, "where">;
|
|
39
|
+
type UpdateOptions<T> = {
|
|
40
|
+
where: Callback<T>;
|
|
41
|
+
values?: CollectionData;
|
|
42
|
+
};
|
|
43
|
+
type DeleteOptions<T> = {
|
|
44
|
+
where: Callback<T>;
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
declare class Collection {
|
|
48
|
+
#private;
|
|
49
|
+
private readonly props;
|
|
50
|
+
constructor(props: CollectionProps);
|
|
51
|
+
init(): Promise<this>;
|
|
52
|
+
get information(): Promise<Omit<CollectionContent, 'data'>>;
|
|
53
|
+
add(data: CollectionData): Promise<Output<CollectionDataWithZID>>;
|
|
54
|
+
addMany(data: CollectionData[]): Promise<Output<CollectionDataWithZID[]>>;
|
|
55
|
+
findAll(options?: FindOptionsWithoutWhere): Promise<Output<CollectionDataWithZID[]>>;
|
|
56
|
+
find<T>(options: FindOptions<KzObject<T>>): Promise<Output<CollectionDataWithZID[]>>;
|
|
57
|
+
update<T>(options: UpdateOptions<KzObject<T>>): Promise<Output<CollectionDataWithZID[]>>;
|
|
58
|
+
delete<T>(options: DeleteOptions<KzObject<T>>): Promise<Output<void>>;
|
|
59
|
+
reset(): Promise<Output<void>>;
|
|
60
|
+
drop(): Promise<void>;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
declare class KlauzDB {
|
|
64
|
+
path: string;
|
|
65
|
+
constructor(props: KlauzProps);
|
|
66
|
+
createCollection(collectionName: string): Promise<Collection>;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
type KlauzErrorCode = 'KLAUZ_ERROR' | 'KLAUZ_VALIDATION_ERROR' | 'KLAUZ_STORAGE_ERROR' | 'KLAUZ_NOT_FOUND';
|
|
70
|
+
declare class KlauzError extends Error {
|
|
71
|
+
readonly code: KlauzErrorCode;
|
|
72
|
+
constructor(message: string, code?: KlauzErrorCode, options?: ErrorOptions);
|
|
73
|
+
}
|
|
74
|
+
declare class KlauzValidationError extends KlauzError {
|
|
75
|
+
constructor(message: string, options?: ErrorOptions);
|
|
76
|
+
}
|
|
77
|
+
declare class KlauzStorageError extends KlauzError {
|
|
78
|
+
constructor(message: string, options?: ErrorOptions);
|
|
79
|
+
}
|
|
80
|
+
declare class KlauzNotFoundError extends KlauzError {
|
|
81
|
+
constructor(message: string, options?: ErrorOptions);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export { type Callback, type CollectionContent, type CollectionData, type CollectionDataWithZID, type CollectionProps, type DeleteOptions, type FindOptions, type FindOptionsWithoutWhere, KlauzDB, KlauzError, type KlauzErrorCode, KlauzNotFoundError, type KlauzProps, KlauzStorageError, KlauzValidationError, type KzObject, type LegacyCollectionContent, type Output, type UpdateOptions, type ZID };
|