klauz-db 0.5.1 → 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 -591
- 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/README.md
CHANGED
|
@@ -1,591 +1,363 @@
|
|
|
1
|
-
# KlauzDB [](https://www.npmjs.com/package/klauz-db)
|
|
2
|
-
|
|
3
|
-
KlauzDB é um banco de dados orientado a
|
|
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
|
-
const
|
|
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
|
-
const
|
|
78
|
-
|
|
79
|
-
console.log(
|
|
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
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
###
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
```
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
```
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
admin:
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
const
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
#### Syntax
|
|
365
|
-
```js
|
|
366
|
-
const optionsUpdate = {
|
|
367
|
-
where: (obj) => {},
|
|
368
|
-
values: { key: value }
|
|
369
|
-
}
|
|
370
|
-
collection.update(optionsUpdate)
|
|
371
|
-
```
|
|
372
|
-
|
|
373
|
-
#### Parâmetros
|
|
374
|
-
`optionsUpdate.where: function(obj) {}` *(obrigatório)*<br><br>Função callback que recebe como parâmetro os objetos contidos na Collection.<br>Seu retorno deve ser os objetos que serão atualizados;<br><br>
|
|
375
|
-
`optionsUpdate.values: { key: value }` *(obrigatório)*<br><br>Objeto chave-valor com os novos valores a serem atualizados;
|
|
376
|
-
|
|
377
|
-
#### Retorno
|
|
378
|
-
Array de objetos já com as novas alterações;
|
|
379
|
-
|
|
380
|
-
#### Exemplo
|
|
381
|
-
```js
|
|
382
|
-
// Adicionando dados
|
|
383
|
-
collection.addMany([
|
|
384
|
-
{
|
|
385
|
-
nome: 'User_1',
|
|
386
|
-
admin: false
|
|
387
|
-
},
|
|
388
|
-
{
|
|
389
|
-
nome: 'User_2',
|
|
390
|
-
admin: false
|
|
391
|
-
}
|
|
392
|
-
])
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
// Alterando dados
|
|
396
|
-
|
|
397
|
-
// JavaScript:
|
|
398
|
-
const output1 = collection.update({
|
|
399
|
-
where: obj => obj.nome === 'User_1',
|
|
400
|
-
values: { admin: true }
|
|
401
|
-
})
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
// Typescript:
|
|
405
|
-
type User = { nome: string, admin: boolean }
|
|
406
|
-
const output2 = collection.update<User>({
|
|
407
|
-
where: (obj) => obj._zid === 1,
|
|
408
|
-
values: { admin: true }
|
|
409
|
-
})
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
console.log("output1: ", output1);
|
|
413
|
-
console.log("output2: ", output2);
|
|
414
|
-
// Resultado:
|
|
415
|
-
[
|
|
416
|
-
{
|
|
417
|
-
"nome": "User_1",
|
|
418
|
-
"admin": true,
|
|
419
|
-
"_zid": 1
|
|
420
|
-
}
|
|
421
|
-
]
|
|
422
|
-
//
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
// Update inserindo nova informação
|
|
426
|
-
const output3 = collection.update({
|
|
427
|
-
where: obj => obj.nome === 'User_2',
|
|
428
|
-
values: { idade: 20 }
|
|
429
|
-
})
|
|
430
|
-
|
|
431
|
-
console.log("output3: ", output3);
|
|
432
|
-
// Resultado:
|
|
433
|
-
[
|
|
434
|
-
{
|
|
435
|
-
"nome": "User_2",
|
|
436
|
-
"admin": false,
|
|
437
|
-
"idade": 20,
|
|
438
|
-
"_zid": 2
|
|
439
|
-
}
|
|
440
|
-
]
|
|
441
|
-
//
|
|
442
|
-
```
|
|
443
|
-
<br>
|
|
444
|
-
|
|
445
|
-
### delete
|
|
446
|
-
Remove um ou mais objetos da Collection.
|
|
447
|
-
|
|
448
|
-
#### Syntax
|
|
449
|
-
```js
|
|
450
|
-
const optionsDelete = {
|
|
451
|
-
where: (obj) => {}
|
|
452
|
-
}
|
|
453
|
-
collection.delete(optionsDelete)
|
|
454
|
-
```
|
|
455
|
-
|
|
456
|
-
#### Parâmetros
|
|
457
|
-
`optionsDelete.where: function(obj) {}` *(obrigatório)*<br><br>Função callback que recebe como parâmetro os objetos contidos na Collection.<br>Seu retorno deve ser os objetos que serão removidos;<br><br>
|
|
458
|
-
|
|
459
|
-
#### Retorno
|
|
460
|
-
Não possui retorno;
|
|
461
|
-
|
|
462
|
-
#### Exemplo
|
|
463
|
-
```js
|
|
464
|
-
// Adicionando dados
|
|
465
|
-
collection.addMany([
|
|
466
|
-
{
|
|
467
|
-
nome: 'User_1',
|
|
468
|
-
admin: false
|
|
469
|
-
},
|
|
470
|
-
{
|
|
471
|
-
nome: 'User_2',
|
|
472
|
-
admin: false
|
|
473
|
-
},
|
|
474
|
-
{
|
|
475
|
-
nome: 'User_3',
|
|
476
|
-
admin: false
|
|
477
|
-
}
|
|
478
|
-
])
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
// Deletando dados
|
|
482
|
-
|
|
483
|
-
console.log("collection.findAll(): ", collection.findAll());
|
|
484
|
-
// Consulta antes:
|
|
485
|
-
[
|
|
486
|
-
{
|
|
487
|
-
"nome": "User_1",
|
|
488
|
-
"admin": false,
|
|
489
|
-
"_zid": 1
|
|
490
|
-
},
|
|
491
|
-
{
|
|
492
|
-
"nome": "User_2",
|
|
493
|
-
"admin": false,
|
|
494
|
-
"_zid": 2
|
|
495
|
-
},
|
|
496
|
-
{
|
|
497
|
-
"nome": "User_3",
|
|
498
|
-
"admin": false,
|
|
499
|
-
"_zid": 3
|
|
500
|
-
}
|
|
501
|
-
]
|
|
502
|
-
//
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
// JavaScript:
|
|
506
|
-
collection.delete({
|
|
507
|
-
where: obj => obj.nome === 'User_1'
|
|
508
|
-
})
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
// Typescript:
|
|
512
|
-
type User = { nome: string, admin: boolean };
|
|
513
|
-
collection.delete<User>({
|
|
514
|
-
where: obj => obj._zid === 1
|
|
515
|
-
})
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
console.log("collection.findAll(): ", collection.findAll());
|
|
519
|
-
// Consulta depois:
|
|
520
|
-
[
|
|
521
|
-
{
|
|
522
|
-
"nome": "User_2",
|
|
523
|
-
"admin": false,
|
|
524
|
-
"_zid": 2
|
|
525
|
-
},
|
|
526
|
-
{
|
|
527
|
-
"nome": "User_3",
|
|
528
|
-
"admin": false,
|
|
529
|
-
"_zid": 3
|
|
530
|
-
}
|
|
531
|
-
]
|
|
532
|
-
//
|
|
533
|
-
```
|
|
534
|
-
<br>
|
|
535
|
-
|
|
536
|
-
### reset
|
|
537
|
-
Apaga todos os dados inseridos na Collection.
|
|
538
|
-
|
|
539
|
-
#### Syntax
|
|
540
|
-
```js
|
|
541
|
-
collection.reset()
|
|
542
|
-
```
|
|
543
|
-
|
|
544
|
-
#### Retorno
|
|
545
|
-
Não possui retorno;
|
|
546
|
-
|
|
547
|
-
#### Exemplo
|
|
548
|
-
```js
|
|
549
|
-
// Adicionando dados
|
|
550
|
-
collection.addMany([
|
|
551
|
-
{
|
|
552
|
-
nome: 'User_1',
|
|
553
|
-
admin: false
|
|
554
|
-
},
|
|
555
|
-
{
|
|
556
|
-
nome: 'User_2',
|
|
557
|
-
admin: false
|
|
558
|
-
}
|
|
559
|
-
])
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
// Resetando os dados
|
|
563
|
-
|
|
564
|
-
console.log("collection.findAll(): ", collection.findAll());
|
|
565
|
-
// Consulta antes:
|
|
566
|
-
[
|
|
567
|
-
{
|
|
568
|
-
"nome": "User_1",
|
|
569
|
-
"admin": false,
|
|
570
|
-
"_zid": 1
|
|
571
|
-
},
|
|
572
|
-
{
|
|
573
|
-
"nome": "User_2",
|
|
574
|
-
"admin": false,
|
|
575
|
-
"_zid": 2
|
|
576
|
-
}
|
|
577
|
-
]
|
|
578
|
-
//
|
|
579
|
-
|
|
580
|
-
collection.reset()
|
|
581
|
-
|
|
582
|
-
console.log("collection.findAll(): ", collection.findAll());
|
|
583
|
-
// Consulta depois:
|
|
584
|
-
[]
|
|
585
|
-
//
|
|
586
|
-
```
|
|
587
|
-
<br>
|
|
588
|
-
|
|
589
|
-
## ✒️ Autores
|
|
590
|
-
|
|
591
|
-
* **Desenvolvedor** - *Trabalho & Documentação* - [Victor Nikolaus](https://github.com/vnikolaus)
|
|
1
|
+
# KlauzDB [](https://www.npmjs.com/package/klauz-db)
|
|
2
|
+
|
|
3
|
+
KlauzDB é um banco de dados local orientado a collections, simples e leve, com persistência em arquivos JSON.
|
|
4
|
+
|
|
5
|
+
Ele foi pensado para protótipos, automações, testes, CLIs, aplicações locais e projetos pequenos que precisam persistir dados estruturados sem configurar um banco externo.
|
|
6
|
+
|
|
7
|
+
## Instalação
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install klauz-db
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Usabilidade
|
|
14
|
+
|
|
15
|
+
ESM:
|
|
16
|
+
|
|
17
|
+
```ts
|
|
18
|
+
import { KlauzDB } from 'klauz-db'
|
|
19
|
+
|
|
20
|
+
const db = new KlauzDB({
|
|
21
|
+
path: './db'
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
const users = await db.createCollection('users')
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
CommonJS:
|
|
28
|
+
|
|
29
|
+
```js
|
|
30
|
+
const { KlauzDB } = require('klauz-db')
|
|
31
|
+
|
|
32
|
+
async function main() {
|
|
33
|
+
const db = new KlauzDB({
|
|
34
|
+
path: './db'
|
|
35
|
+
})
|
|
36
|
+
|
|
37
|
+
const users = await db.createCollection('users')
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
main()
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
O arquivo da collection é criado automaticamente no diretório informado:
|
|
44
|
+
|
|
45
|
+
```txt
|
|
46
|
+
db/.users.json
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
## Documentação
|
|
50
|
+
|
|
51
|
+
KlauzDB expõe a classe `KlauzDB`.
|
|
52
|
+
|
|
53
|
+
### createCollection
|
|
54
|
+
|
|
55
|
+
Cria ou carrega uma collection.
|
|
56
|
+
|
|
57
|
+
```ts
|
|
58
|
+
const collection = await db.createCollection('users')
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
Parâmetros:
|
|
62
|
+
|
|
63
|
+
```ts
|
|
64
|
+
collectionName: string
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
Retorno:
|
|
68
|
+
|
|
69
|
+
```ts
|
|
70
|
+
Promise<Collection>
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
Exemplo:
|
|
74
|
+
|
|
75
|
+
```ts
|
|
76
|
+
const users = await db.createCollection('users')
|
|
77
|
+
const info = await users.information
|
|
78
|
+
|
|
79
|
+
console.log(info)
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
Saída:
|
|
83
|
+
|
|
84
|
+
```json
|
|
85
|
+
{
|
|
86
|
+
"collection_name": "users",
|
|
87
|
+
"created_at": "2026-05-21T20:00:00.000Z",
|
|
88
|
+
"updated_at": "2026-05-21T20:00:00.000Z"
|
|
89
|
+
}
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
## Collection API
|
|
93
|
+
|
|
94
|
+
Todas as operações que acessam disco são assíncronas.
|
|
95
|
+
|
|
96
|
+
```ts
|
|
97
|
+
await collection.add(...)
|
|
98
|
+
await collection.addMany(...)
|
|
99
|
+
await collection.findAll(...)
|
|
100
|
+
await collection.find(...)
|
|
101
|
+
await collection.update(...)
|
|
102
|
+
await collection.delete(...)
|
|
103
|
+
await collection.reset()
|
|
104
|
+
await collection.drop()
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
### add
|
|
108
|
+
|
|
109
|
+
Adiciona um registro na collection.
|
|
110
|
+
|
|
111
|
+
```ts
|
|
112
|
+
const user = await users.add({
|
|
113
|
+
name: 'Victor',
|
|
114
|
+
admin: true
|
|
115
|
+
})
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
Retorno:
|
|
119
|
+
|
|
120
|
+
```json
|
|
121
|
+
{
|
|
122
|
+
"name": "Victor",
|
|
123
|
+
"admin": true,
|
|
124
|
+
"_zid": 1
|
|
125
|
+
}
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
O campo `_zid` é controlado pelo KlauzDB. Se enviado pelo usuário, será ignorado.
|
|
129
|
+
|
|
130
|
+
### addMany
|
|
131
|
+
|
|
132
|
+
Adiciona múltiplos registros em lote.
|
|
133
|
+
|
|
134
|
+
```ts
|
|
135
|
+
const output = await users.addMany([
|
|
136
|
+
{
|
|
137
|
+
name: 'Victor',
|
|
138
|
+
admin: true
|
|
139
|
+
},
|
|
140
|
+
{
|
|
141
|
+
name: 'Giovanna',
|
|
142
|
+
admin: false
|
|
143
|
+
}
|
|
144
|
+
])
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
Retorno:
|
|
148
|
+
|
|
149
|
+
```json
|
|
150
|
+
[
|
|
151
|
+
{
|
|
152
|
+
"name": "Victor",
|
|
153
|
+
"admin": true,
|
|
154
|
+
"_zid": 1
|
|
155
|
+
},
|
|
156
|
+
{
|
|
157
|
+
"name": "Giovanna",
|
|
158
|
+
"admin": false,
|
|
159
|
+
"_zid": 2
|
|
160
|
+
}
|
|
161
|
+
]
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
### findAll
|
|
165
|
+
|
|
166
|
+
Retorna todos os registros da collection.
|
|
167
|
+
|
|
168
|
+
```ts
|
|
169
|
+
const allUsers = await users.findAll()
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
Com `hideInfo`:
|
|
173
|
+
|
|
174
|
+
```ts
|
|
175
|
+
const publicUsers = await users.findAll({
|
|
176
|
+
hideInfo: ['admin', '_zid']
|
|
177
|
+
})
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
`hideInfo` remove campos apenas do retorno. Os dados persistidos não são alterados.
|
|
181
|
+
|
|
182
|
+
### find
|
|
183
|
+
|
|
184
|
+
Retorna registros filtrados por callback.
|
|
185
|
+
|
|
186
|
+
```ts
|
|
187
|
+
type User = {
|
|
188
|
+
name: string
|
|
189
|
+
admin: boolean
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
const admins = await users.find<User>({
|
|
193
|
+
where: (user) => user.admin
|
|
194
|
+
})
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
Com `hideInfo`:
|
|
198
|
+
|
|
199
|
+
```ts
|
|
200
|
+
const publicAdmins = await users.find<User>({
|
|
201
|
+
where: (user) => user.admin,
|
|
202
|
+
hideInfo: ['admin']
|
|
203
|
+
})
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
O callback recebe o objeto com `_zid` tipado.
|
|
207
|
+
|
|
208
|
+
```ts
|
|
209
|
+
const user = await users.find<User>({
|
|
210
|
+
where: (user) => user._zid === 1
|
|
211
|
+
})
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
### update
|
|
215
|
+
|
|
216
|
+
Atualiza todos os registros que passarem no callback `where`.
|
|
217
|
+
|
|
218
|
+
```ts
|
|
219
|
+
const updated = await users.update<User>({
|
|
220
|
+
where: (user) => user.name === 'Victor',
|
|
221
|
+
values: {
|
|
222
|
+
admin: false
|
|
223
|
+
}
|
|
224
|
+
})
|
|
225
|
+
```
|
|
226
|
+
|
|
227
|
+
Retorno:
|
|
228
|
+
|
|
229
|
+
```json
|
|
230
|
+
[
|
|
231
|
+
{
|
|
232
|
+
"name": "Victor",
|
|
233
|
+
"admin": false,
|
|
234
|
+
"_zid": 1
|
|
235
|
+
}
|
|
236
|
+
]
|
|
237
|
+
```
|
|
238
|
+
|
|
239
|
+
O campo `_zid` não pode ser alterado por `update`.
|
|
240
|
+
|
|
241
|
+
### delete
|
|
242
|
+
|
|
243
|
+
Remove todos os registros que passarem no callback `where`.
|
|
244
|
+
|
|
245
|
+
```ts
|
|
246
|
+
await users.delete<User>({
|
|
247
|
+
where: (user) => user.admin === false
|
|
248
|
+
})
|
|
249
|
+
```
|
|
250
|
+
|
|
251
|
+
Não possui retorno em caso de sucesso.
|
|
252
|
+
|
|
253
|
+
### reset
|
|
254
|
+
|
|
255
|
+
Remove todos os registros da collection, mantendo o arquivo da collection.
|
|
256
|
+
|
|
257
|
+
```ts
|
|
258
|
+
await users.reset()
|
|
259
|
+
```
|
|
260
|
+
|
|
261
|
+
### drop
|
|
262
|
+
|
|
263
|
+
Remove o arquivo da collection.
|
|
264
|
+
|
|
265
|
+
```ts
|
|
266
|
+
await users.drop()
|
|
267
|
+
```
|
|
268
|
+
|
|
269
|
+
## Erros
|
|
270
|
+
|
|
271
|
+
Métodos de CRUD retornam:
|
|
272
|
+
|
|
273
|
+
```ts
|
|
274
|
+
T | {
|
|
275
|
+
error: string
|
|
276
|
+
code: string
|
|
277
|
+
}
|
|
278
|
+
```
|
|
279
|
+
|
|
280
|
+
Exemplo:
|
|
281
|
+
|
|
282
|
+
```ts
|
|
283
|
+
const output = await users.update<User>({
|
|
284
|
+
where: (user) => user.name === 'missing',
|
|
285
|
+
values: {
|
|
286
|
+
admin: true
|
|
287
|
+
}
|
|
288
|
+
})
|
|
289
|
+
|
|
290
|
+
if ('error' in output) {
|
|
291
|
+
console.log(output.code, output.error)
|
|
292
|
+
}
|
|
293
|
+
```
|
|
294
|
+
|
|
295
|
+
Erros exportados:
|
|
296
|
+
|
|
297
|
+
```ts
|
|
298
|
+
import {
|
|
299
|
+
KlauzError,
|
|
300
|
+
KlauzValidationError,
|
|
301
|
+
KlauzStorageError,
|
|
302
|
+
KlauzNotFoundError
|
|
303
|
+
} from 'klauz-db'
|
|
304
|
+
```
|
|
305
|
+
|
|
306
|
+
## Formato do arquivo
|
|
307
|
+
|
|
308
|
+
Cada collection é salva como um JSON oculto:
|
|
309
|
+
|
|
310
|
+
```json
|
|
311
|
+
{
|
|
312
|
+
"collection_name": "users",
|
|
313
|
+
"created_at": "2026-05-21T20:00:00.000Z",
|
|
314
|
+
"updated_at": "2026-05-21T20:00:00.000Z",
|
|
315
|
+
"data": [
|
|
316
|
+
{
|
|
317
|
+
"name": "Victor",
|
|
318
|
+
"admin": true,
|
|
319
|
+
"_zid": 1
|
|
320
|
+
}
|
|
321
|
+
]
|
|
322
|
+
}
|
|
323
|
+
```
|
|
324
|
+
|
|
325
|
+
## Migração de 0.5.x para 1.0.0
|
|
326
|
+
|
|
327
|
+
Principais mudanças:
|
|
328
|
+
|
|
329
|
+
- API de I/O agora é async.
|
|
330
|
+
- `createCollection` agora deve ser usado com `await`.
|
|
331
|
+
- `collection.information` agora retorna `Promise`.
|
|
332
|
+
- `last_interaction` foi substituído por `updated_at`.
|
|
333
|
+
- Arquivos antigos com `last_interaction` continuam sendo lidos.
|
|
334
|
+
- `hideInfo` não altera mais os dados persistidos.
|
|
335
|
+
- `_zid` é protegido contra insert/update manual.
|
|
336
|
+
- `addMany` salva os dados em lote.
|
|
337
|
+
|
|
338
|
+
Antes:
|
|
339
|
+
|
|
340
|
+
```ts
|
|
341
|
+
const users = db.createCollection('users')
|
|
342
|
+
const allUsers = users.findAll()
|
|
343
|
+
```
|
|
344
|
+
|
|
345
|
+
Agora:
|
|
346
|
+
|
|
347
|
+
```ts
|
|
348
|
+
const users = await db.createCollection('users')
|
|
349
|
+
const allUsers = await users.findAll()
|
|
350
|
+
```
|
|
351
|
+
|
|
352
|
+
## Desenvolvimento
|
|
353
|
+
|
|
354
|
+
```bash
|
|
355
|
+
npm run typecheck
|
|
356
|
+
npm run lint
|
|
357
|
+
npm test
|
|
358
|
+
npm run build
|
|
359
|
+
```
|
|
360
|
+
|
|
361
|
+
## Autor
|
|
362
|
+
|
|
363
|
+
Victor Nikolaus - [GitHub](https://github.com/vnikolaus)
|