-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.html
More file actions
4776 lines (4595 loc) · 192 KB
/
index.html
File metadata and controls
4776 lines (4595 loc) · 192 KB
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
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
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>SleekDB - PHP NoSQL Flat File Database</title>
<link rel="stylesheet" type="text/css" href="/assets/app.css" />
<link rel="stylesheet" type="text/css" href="/assets/normalize.css" />
<link rel="stylesheet" href="/assets/highlighter/styles/darcula.css" />
<script src="/assets/highlighter/highlight.pack.js"></script>
<script src="https://unpkg.com/ionicons@4.3.0/dist/ionicons.js"></script>
<!-- Search Engine -->
<meta name="description" content="SleekDB is a simple flat file NoSQL like database implemented in PHP without any third-party dependencies that store data in plain JSON files." />
<meta
name="image"
content="https://sleekdb.github.io/assets/SleekDB_Thumbnail.png"
/>
<!-- Schema.org for Google -->
<meta itemprop="name" content="SleekDB - PHP NoSQL Flat File Database" />
<meta itemprop="description" content="SleekDB is a simple flat file NoSQL like database implemented in PHP without any third-party dependencies that store data in plain JSON files." />
<meta
itemprop="image"
content="https://sleekdb.github.io/assets/SleekDB_Thumbnail.png"
/>
<!-- Twitter -->
<meta name="twitter:card" content="summary" />
<meta name="twitter:title" content="SleekDB - PHP NoSQL Flat File Database" />
<meta name="twitter:description" content="SleekDB is a simple flat file NoSQL like database implemented in PHP without any third-party dependencies that store data in plain JSON files." />
<meta name="twitter:site" content="@rakibtg" />
<meta name="twitter:creator" content="@rakibtg" />
<!-- Open Graph general (Facebook, Pinterest & Google+) -->
<meta name="og:title" content="SleekDB - PHP NoSQL Flat File Database" />
<meta name="og:description" content="SleekDB is a simple flat file NoSQL like database implemented in PHP without any third-party dependencies that store data in plain JSON files." />
<meta
name="og:image"
content="https://sleekdb.github.io/assets/SleekDB_Thumbnail.png"
/>
<meta name="og:url" content="https://sleekdb.github.io/" />
<meta name="og:site_name" content="SleekDB - PHP NoSQL Flat File Database" />
<meta name="og:locale" content="en_US" />
<meta name="og:type" content="website" />
</head>
<body>
<script
async
src="https://www.googletagmanager.com/gtag/js?id=G-YZ3KHTYGDW"
></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag() {
dataLayer.push(arguments);
}
gtag("js", new Date());
gtag("config", "G-YZ3KHTYGDW");
</script>
<div class="container">
<div class="header">
<div class="toggler" title="Toggle sidebar">
<ion-icon name="menu"></ion-icon>
</div>
<div class="left_header">
<div class="logo">
<a href="/">SLEEK<span>DB</span></a>
</div>
</div>
<div class="right_header header_menu">
<a
href="#/versions"
title="Find all versions of SleekDB"
class="menu gotoblock"
><ion-icon class="hide-sm" name="pricetag"></ion-icon> 2.50</a
>
<a
class="menu"
href="https://github.com/rakibtg/SleekDB"
target="_blank"
rel="noopener noreferrer"
title="SleekDB is open-source. See the source code in GitHub"
><ion-icon name="logo-github"></ion-icon>
<span class="hide-sm">GitHub</span></a
>
<a
class="menu"
href="https://twitter.com/rakibtg"
target="_blank"
rel="noopener noreferrer"
title="Follow me on twitter for SleekDB updates."
><ion-icon name="logo-twitter"></ion-icon>
<span class="hide-sm">@rakibtg</span></a
>
<a
class="menu gotoblock"
href="/#/support"
target="_blank"
title="Learn more on supporting this open source database."
><ion-icon name="heart"></ion-icon> <span class="hide-sm">Support</span></a
>
</div>
</div>
<div class="app">
<div class="left">
<div id="docs_menu_sidebar">
<a class="gotoblock" href="#/home">
<ion-icon name="home"></ion-icon>
Home
</a>
<a class="gotoblock" href="#/features">
<ion-icon name="filing"></ion-icon>
Features
</a>
<a class="gotoblock" href="#/installation">
<ion-icon name="play"></ion-icon>
Installation
</a>
<a class="gotoblock" href="#/getting-started">
<ion-icon name="rocket"></ion-icon>
Getting Started
</a>
<a class="gotoblock" href="#/managing-store">
<ion-icon name="cube"></ion-icon>
Managing Store
</a>
<a class="gotoblock" href="#/configurations">
<ion-icon name="construct"></ion-icon>
Configurations
</a>
<a class="gotoblock" href="#/insert-data">
<ion-icon name="create"></ion-icon>
Insert Data
</a>
<a class="gotoblock" href="#/fetch-data">
<ion-icon name="book"></ion-icon>
Fetch Data
</a>
<a class="gotoblock" href="#/edit-data">
<ion-icon name="hammer"></ion-icon>
Edit Data
</a>
<a class="gotoblock" href="#/delete-data">
<ion-icon name="trash"></ion-icon>
Delete Data
</a>
<a class="gotoblock" href="#/criteria">
<ion-icon name="checkmark-circle-outline"></ion-icon>
$criteria
</a>
<a class="gotoblock" href="#/query-builder">
<ion-icon name="build"></ion-icon>
QueryBuilder
</a>
<a class="gotoblock" href="#/cache-management">
<ion-icon name="copy"></ion-icon>
Cache Management
</a>
<a class="gotoblock" href="#/join-stores">
<ion-icon name="git-merge"></ion-icon>
Join Stores
</a>
<a class="gotoblock" href="#/query">
<ion-icon name="flash"></ion-icon>
Query
</a>
<a class="gotoblock" href="#/dates">
<ion-icon name="alarm"></ion-icon>
Working with Dates
</a>
<a class="gotoblock" href="#/searching">
<ion-icon name="search"></ion-icon>
Searching
</a>
<a class="gotoblock" href="#/execution-order">
<ion-icon name="git-network"></ion-icon>
Order of Execution
</a>
<a class="gotoblock" href="#/advanced">
<ion-icon name="flame"></ion-icon>
Advanced
</a>
<a class="gotoblock" href="#/complete-examples">
<ion-icon name="color-wand"></ion-icon>
Complete Examples
</a>
<a class="gotoblock" href="#/cache">
<ion-icon name="logo-buffer"></ion-icon>
Cache
</a>
<a class="gotoblock" href="#/reference">
<ion-icon name="albums"></ion-icon>
Reference
</a>
<a class="gotoblock" href="#/release-notes">
<ion-icon name="megaphone"></ion-icon>
Release Notes
</a>
<a class="gotoblock" href="#/support">
<ion-icon name="heart"></ion-icon>
Support and Contributions
</a>
<a class="gotoblock" href="#/contact">
<ion-icon name="mail"></ion-icon>
Contact
</a>
<a class="gotoblock" href="#/versions">
<ion-icon name="pricetag"></ion-icon>
Versions
</a>
</div>
</div>
<div class="right" id="layoutRight">
<div class="intro" id="block_home">
<h1>SleekDB - A NoSQL Database Built With PHP</h1>
<p>SleekDB is a simple flat file NoSQL database implemented in PHP without any third-party dependencies. It stores data in plain JSON files, making it lightweight and easy to work with.</p>
<p>Unlike relational databases such as SQLite, MySQL, PostgreSQL, or MariaDB, SleekDB is a NoSQL database; think of it more like MongoDB, but simpler and without the overhead.</p>
<p>It's designed for managing a few gigabytes of data with low to medium operation loads. If you need a straightforward database solution without heavy-load IO operations, SleekDB is built for you.</p>
<h2>😎 Effortless Data Management</h2>
<p>SleekDB works great as the database engine for low to medium traffic websites. It makes database management simple and effortless.</p>
<h2>📝 Dynamic Speed, Static Performance</h2>
<p>SleekDB caches all query data by default, making your dynamic website perform as fast as a static site.</p>
<h2>🗄 Flat File, Better Concurrency</h2>
<p>SleekDB is a flat file database, but unlike traditional systems that use a single file, it stores data across multiple JSON files. This approach provides better concurrency support. Plus, query results are cached and reused from a single file, eliminating the need to traverse multiple files on every request.</p>
<h2>🥳 Battle-Tested and Actively Maintained</h2>
<p>SleekDB powers many websites and applications in production right now. Our core team actively maintains and develops the database, ensuring it stays reliable and up-to-date.</p>
<h2>👋 Join the Community</h2>
<p>Join our <a href="https://discord.gg/WJGqUFPtZw" target="_blank" rel="noopener nofollow" class="btn btn-primary btn-block">Discord</a> channel. We'd love to hear from you and get your feedback!</p>
<div class="editlink"
file="home-page.md"
version="2.50">
</div>
</div>
<div class="intro" id="block_features">
<h1>Features</h1>
<h2>Installing SleekDB</h2>
<ul>
<li>
<h1>⚡ Lightweight & Fast</h1>
<p>Stores data in plain-text utilizing JSON format, no binary conversion needed to store or fetch the data. Default query cache layer.</p>
</li>
<li>
<h1>🔆 Schema free data storage</h1>
<p>SleekDB does not require any schema, so you can insert any types of data you want.</p>
</li>
<li>
<h1>🔍 Query on nested properties</h1>
<p>As it supports schema free data, so you can filter and use conditions on nested properties of the JSON documents!</p>
<p>If you write this where clause:</p>
<pre><code class="language-php">where( 'post.author.role', '=', 'admin' )</code></pre>
<p>SleekDB will look for data at:</p>
<pre><code class="language-json">{
"post": {
"author": {
"role": "admin"
}
}
}</code></pre>
</li>
<li>
<h1>✨ Dependency free, only needs PHP to run</h1>
<p>Supports PHP 8+. Requires no third-party plugins or software.</p>
</li>
<li>
<h1>🚀 Default caching layer</h1>
<p>SleekDB will serve data from cache by default and regenerate cache automatically! Query results will be cached and later reused from a single file instead of traversing all the available files.</p>
</li>
<li>
<h1>🌈 Rich Conditions and Filters</h1>
<p>Use multiple conditional comparisons, text search, sorting on multiple properties and nested properties.</p>
<table>
<tbody>
<tr>
<td valign="top">
<ul>
<li>where</li>
<li>orWhere</li>
<li>select</li>
<li>except</li>
<li>in</li>
<li>not in</li>
</ul>
</td>
<td valign="top">
<ul>
<li>join</li>
<li>like</li>
<li>sort</li>
<li>skip</li>
<li>orderBy</li>
<li>update</li>
</ul>
</td>
<td valign="top">
<ul>
<li>limit</li>
<li>search</li>
<li>distinct</li>
<li>exists</li>
<li>first</li>
<li>delete</li>
</ul>
</td>
<td valign="top">
<ul>
<li>like</li>
<li>not like</li>
<li>between</li>
<li>not between</li>
<li>group by</li>
<li>having</li>
</ul>
</td>
</tr>
</tbody>
</table>
</li>
<li>
<h1>👍 Process data on demand</h1>
<p>SleekDB does not require any background process or network protocol in order to process data when you use it in a PHP project. All data for a query will be fetched at runtime within the same PHP process.</p>
</li>
<li>
<h1>😍 Runs everywhere</h1>
<p>Runs perfectly on shared-servers or VPS too.</p>
</li>
<li>
<h1>🍰 Easy to learn and implement</h1>
<p>SleekDB provides a very simple elegant API to handle all of your data.</p>
</li>
<li>
<h1>🍰 Easily import/export or backup data.</h1>
<p>SleekDB use files to store information. That makes tasks like backup, import and export very easy.</p>
</li>
<li>
<h1>💪 Actively maintained</h1>
<p>SleekDB is created by <a rel="noopener nofollow" href="https://twitter.com/rakibtg" target="_blank">@rakibtg</a> who is using it in various types of applications which are in production right now. Our other contributor and active maintainer is <a rel="noopener nofollow" href="https://www.goodsoft.de" target="_blank">Timucin</a> who is making SleekDB much better in terms of code quality and new features.</p>
</li>
<li>
<h1>💌 Support</h1>
<p>For support and other requirements <a class="gotoblock" href="/#/contact">contact</a>.</p>
</li>
</ul>
<div class="editlink"
file="features.md"
version="2.50">
</div>
</div>
<div class="intro" id="block_installation">
<h1>Installation</h1>
<p>To use SleekDB make sure that you have PHP up and running in your system, and SleekDB has write and/or read permission.</p>
<h2>Requirements</h2>
<ul>
<li>PHP >= 8.4</li>
<li>ext-json</li>
<li>ext-mbstring</li>
</ul>
<p><strong>Note:</strong> For PHP 7.x support please use SleekDB <a href="/versions/2.15/">v2.15</a>.</p>
<h2>Composer Installation (recommended)</h2>
<p>To install SleekDB using composer, open a terminal, cd into your project root directory where "composer.json" file lives and run this:</p>
<pre><code class="language-sh">composer require rakibtg/sleekdb</code></pre>
<p>SleekDB should be auto loaded into your app using the composer.
Find SleekDB on <strong><a rel="noopener nofollow" href="https://packagist.org/packages/rakibtg/sleekdb" target="_blank">packagist</a></strong>.</p>
<h2>Install Manually (without composer)</h2>
<ul>
<li><a rel="noopener nofollow" href=" https://github.com/SleekDB/SleekDB/archive/master.zip">Download</a> the latest version and extract the ZIP file inside your project directory.</li>
<li>
<p>Import the SleekDB.php file where you want to use SleekDB.</p>
<p>Example:</p>
<pre><code class="language-php">require_once "../SleekDB/Store.php";</code></pre>
</li>
</ul>
<p>To download older versions please check the <a rel="noopener nofollow" target="_blank" title="Click here to download old versions" href="https://github.com/SleekDB/SleekDB/releases">releases</a>.</p>
<h2>Using dev version (Not recommended)</h2>
<p>We do a lot of experiments on the dev branch, which are not properly documented and the API will not be consistence in some cases. Use from <code>dev</code> branch if you have any specific reason or if you want play around with it.</p>
<pre><code class="language-sh">composer require rakibtg/sleekdb dev-dev</code></pre>
<div class="editlink"
file="installation.md"
version="2.50">
</div>
</div>
<div class="intro" id="block_getting-started">
<h1>Getting Started</h1>
<p>Getting started with SleekDB is super easy. We keep data in a "store", which is similar to MySQL "table" or MongoDB "collection". Each store contains multiple documents, which are equivalent to rows in SQL based relational database.</p>
<blockquote>
<p>As an example for a blog it can be something like this: <strong>posts</strong> > <strong>documents</strong></p>
</blockquote>
<p>Here "posts" is a store and "documents" are a collection of JSON files that contains all the blog posts.</p>
<h2>📌 Important Note</h2>
<blockquote>
<p>With the release of version 2.0 we have updated the internal implementation.<br/>We introduce a new API and the old SleekDB object has to retire!<br/>Although, we are providing downwards compatibility in this version, but will remove the old SleekDB object with version 3.0.<br />This documentation focus on the new API.</p>
</blockquote>
<h2>Query Life Cycle</h2>
<p>Each query will follow the below steps or execution life cycle.</p>
<ol>
<li>
<h1>Store</h1>
<p>The <code>Store</code> class is the first, and in most cases also the only part you need to come in contact with.</p>
</li>
<li>
<h1>Query Builder</h1>
<p>The <code>QueryBuilder</code> class is used to prepare a query. To execute it <code>getQuery</code> method can be used.</p>
</li>
<li>
<h1>Query</h1>
<p>At this step the <code>Query</code> class contains all information needed to execute the query.</p>
</li>
<li>
<h1>Cache</h1>
<p>The <code>Cache</code> class handles everything regarding caching. It will decide when to cache or not to cache.</p>
</li>
</ol>
<h2>First Example - Insert and Fetch Data</h2>
<ol>
<li>
<p>To begin with, we need a valid "path" where we want to store our data.<br/>Both absolute and relative paths are supported.</p>
<pre><code class="language-php">$databaseDirectory = __DIR__ . "/myDatabase";</code></pre>
</li>
<li>
<p>Once we have the data directory, we can initialize the store.<br/>If the store doesn't exist it will be created automatically.</p>
<pre><code class="language-php">$newsStore = new \SleekDB\Store("news", $databaseDirectory);</code></pre>
<p>Optionally you can <strong>pass a configuration array as a third parameter</strong>.<br/>
Read more about <a class="gotoblock" href="/#/configurations">configurations</a>.</p>
</li>
<li>
<p>Inserting your first news article using the <code>insert</code> method. The article has to be an array.</p>
<pre><code class="language-php">$article = [
"title" => "Google Pixel XL",
"about" => "Google announced a new Pixel!",
"author" => [
"avatar" => "profile-12.jpg",
"name" => "Foo Bar"
]
];
$results = $newsStore->insert($article);</code></pre>
<p>The results variable will contain all the inserted data and with their <code>_id</code> property which will be unique and added automatically.</p>
</li>
<li>
<p>To find all news articles we will use the <code>findAll</code> method.</p>
<pre><code class="language-php">$allNews = $newsStore->findAll();
print_r($allNews);</code></pre>
</li>
</ol>
<blockquote>
<p>🎉 Congrats! You just inserted your first data into a SleekDB store and also learned how to fetch them.</p>
</blockquote>
<div class="editlink"
file="getting-started.md"
version="2.50">
</div>
</div>
<div class="intro" id="block_managing-store">
<h1>Stores</h1>
<p>Store is a simple directory where SleekDB will write all your data in JSON documents. "Store" is similar with the idea of "Table" in MySQL or "Collection" in MongoDB. You don't need to create a "store" directory manually.</p>
<h2>Your first store</h2>
<p>To start working with a store we need to create an object.</p>
<p>Later, we can use that object to work with the data in that store.</p>
<ul>
<li>
<p>Creating a store</p>
<pre><code class="language-php">use SleekDB\Store;
$newsStore = new Store('news', $dataDir);</code></pre>
</li>
<li>
<p>Creating an additional store; assuming we are working on a community platform, where need a users store too.</p>
<pre><code class="language-php">$userStore = new Store('users', $dataDir);</code></pre>
</li>
<li>
<p>Another store to keep all the posts shared by the user.</p>
<pre><code class="language-php">$postStore = new Store('posts', $dataDir);</code></pre>
</li>
<li>
<p>Creating a new user</p>
<pre><code class="language-php">$userStore->insert([
'name' => 'Mike Doe',
'email' => 'miked@example.com',
'avatar' => [
'sm' => "/img-sm.jpg",
'lg' => "/img-lg.jpg"
]
]);</code></pre>
</li>
</ul>
<p>In the above example we have created a new user to understand the purpose of a store in SleekDB. In this documentation later we will see more examples on this.</p>
<h2>Deleting A Store</h2>
<p>To delete a store use the <code>deleteStore()</code> method. It deletes a store and wipes all the data and cache it contains.</p>
<p>Example:</p>
<pre><code class="language-php">$userStore->deleteStore();</code></pre>
<div class="editlink"
file="managing-store.md"
version="2.50">
</div>
</div>
<div class="intro" id="block_configurations">
<h1>Configurations</h1>
<p>SleekDB allows a few configuration options, which are</p>
<ul>
<li><a class="gotoblock" href="#/configurations#auto_cache">auto_cache</a></li>
<li><a class="gotoblock" href="#/configurations#cache_lifetime">cache_lifetime</a></li>
<li><a class="gotoblock" href="#/configurations#primary_key">primary_key</a></li>
<li><a class="gotoblock" href="#/configurations#search">search</a></li>
<li><a class="gotoblock" href="#/configurations#folder_permissions">folder_permissions</a></li>
</ul>
<p>They are store wide, which means they will be used on every query, if not changed on a query by query base.</p>
<h2>Using Custom Configuration</h2>
<p>You can pass the configurations array as a third parameter when initializing the Store object:</p>
<pre><code class="language-php">// default configurations
$configuration = [
"auto_cache" => true,
"cache_lifetime" => null,
"primary_key" => "_id",
"search" => [
"min_length" => 2,
"mode" => "or",
"score_key" => "scoreKey",
"algorithm" => Query::SEARCH_ALGORITHM["hits"]
],
"folder_permissions" => 0777
];
$newsStore = new \SleekDB\Store("news", $dataDir, $configuration);</code></pre>
<p>Let's get familiar with the available configuration options.</p>
<h2 id="configurations-auto_cache">auto_cache</h2>
<p>The <code>auto_cache</code> is set to <code>true</code> by default!</p>
<p>This tells SleekDB to use the build in caching system.</p>
<p>To disable build in caching set <code>auto_cache</code> to <code>false</code> in the config array.</p>
<p>Note that you can manually manage caching on a query by query base with methods that SleekDB provides.
Available caching method's are:</p>
<ul>
<li><code>QueryBuilder->regenerateCache()</code></li>
<li><code>QueryBuilder->useCache()</code></li>
<li><code>QueryBuilder->disableCache()</code></li>
</ul>
<h2 id="configurations-cache_lifetime">cache_lifetime</h2>
<p>The <code>cache_lifetime</code> is set to <code>null</code> by default!</p>
<p>Can be an <code>int >= 0</code>, that specifies the lifetime in seconds, or <code>null</code> to define that there is no lifetime.</p>
<p>This specifies the default cache time to live store wide.</p>
<p>If set to null the cache files made with that store instance will have no lifetime and will be regenerated on every insert/update/delete operation.</p>
<blockquote>
<p><code>0</code> means infinite lifetime.</p>
</blockquote>
<p>Note that you can specify the cache lifetime on a query by query base by using the useCache method of the QueryBuilder and pass a lifetime.</p>
<ul>
<li><code>QueryBuilder->useCache($lifetime)</code></li>
</ul>
<blockquote>
<p>Note: You will find more details on caching at <a class="gotoblock" href="/#/cache-management">Cache Management</a></p>
</blockquote>
<h2 id="configurations-primary_key">primary_key</h2>
<p>Set the primary key into any string you want.<br/>
SleekDB will then use that key instead of the default <code>_id</code> key.</p>
<h2 id="configurations-search">search</h2>
<p>On this documentation page we will provide you a brief description regarding the configuration options.</p>
<blockquote>
<p>Note: You will find more on searching at <a class="gotoblock" href="#/searching">Searching</a></p>
</blockquote>
<h3>min_length</h3>
<p>It has to be an <code>integer</code> > 0.<br/>
The minimum length of a word within the search query that will be used for searching.</p>
<h3>mode</h3>
<p>Has to be a <code>string</code>.<br/>
SleekDB provides two modes.</p>
<ul>
<li>"or" </li>
<li>"and"</li>
</ul>
<h3>score_key</h3>
<p>Can be a <code>string</code> or <code>null</code>.<br/>
If this configuration is <code>null</code> no field containing the score will be applied to the documents.<br/>
If it is a <code>string</code> a new field with the specified field name will be applied to every document, which will contain the search score and can be used to for example sort the documents.</p>
<h3>algorithm</h3>
<p>SleekDB provides 4 search algorithms. <code>hits</code>, <code>hits_prioritize</code>, <code>prioritize</code>, <code>prioritize_position</code><br/>
These algorithms change the score generation.<br/>
They are available as a <strong>constant of the <code>Query</code> class</strong>.</p>
<p>Example:</p>
<pre><code class="language-php">Query::SEARCH_ALGORITHM["hits"] // default</code></pre>
<h2 id="configurations-folder_permissions">folder_permissions</h2>
<p><code>folder_permissions</code> is set to <code>0777</code> by default!</p>
<p>It has to be a valid <code>int</code>. Refer to the <a href="https://www.php.net/manual/en/function.chmod.php" target="_blank">official documentation</a> to learn more about permissions.</p>
<p>The given permission is <strong>only used when creating a new folder</strong>. For example when creating a new store, that does not already exist.</p>
<div class="editlink"
file="configurations.md"
version="2.50">
</div>
</div>
<div class="intro" id="block_insert-data">
<h1>Insert Data</h1>
<p>To insert data first you make a PHP array, and simply insert that array into a store.</p>
<h2>Brief repetition of Store object creation</h2>
<p>For a more detailed documentation on <code>Store</code> object creation refer to the corresponding <a class="gotoblock" href="#/managing-store">"Managing Store" documentation</a>.</p>
<pre><code class="language-php">use SleekDB\Store;
$userStore = new Store('users', __DIR__ . "/database");</code></pre>
<h2>Summary</h2>
<ul>
<li><a class="gotoblock" href="#/insert-data#insert">insert</a></li>
<li><a class="gotoblock" href="#/insert-data#insertMany">insertMany</a></li>
<li><a class="gotoblock" href="#/insert-data#updateOrInsert">updateOrInsert</a></li>
<li><a class="gotoblock" href="#/insert-data#updateOrInsertMany">updateOrInsertMany</a></li>
</ul>
<h2 id="insert-data-insert">Insert a single document</h2>
<pre><code class="language-php">function insert(array $data): array</code></pre>
<h3>Parameters</h3>
<ol>
<li>
<h1>$data: array</h1>
<p>One document that will be insert into the Store</p>
<ul>
<li>["name" => "Josh", "age" => 23, "city" => "london"]</li>
</ul>
</li>
</ol>
<h3>Return value</h3>
<p>Returns the inserted document as an <code>array</code> including the automatically generated and unique <code>_id</code> property.</p>
<h3>Example</h3>
<pre><code class="language-php">// Prepare a PHP array to insert.
$user = [
'name' => 'Kazi Hasan',
'products' => [
'totalSaved' => 19,
'totalBought' => 27
],
'location' => [
'town' => 'Nagar',
'city' => 'Dhaka',
'country' => 'Bangladesh'
]
];
// Insert the data.
$user = $userStore->insert($user);</code></pre>
<p><br/></p>
<h2 id="insert-data-insertMany">Insert multiple documents</h2>
<pre><code class="language-php">function insertMany(array $data): array</code></pre>
<h3>Parameters</h3>
<ol>
<li>
<h1>$data: array</h1>
<p>Multiple documents that will be insert into the Store</p>
<ul>
<li>[ ["name" => "Josh", "age" => 23], ["name" => "Mike", "age" => 19], ... ]</li>
</ul>
</li>
</ol>
<h3>Return value</h3>
<p>Returns the inserted documents in an <code>array</code> including their automatically generated and unique <code>_id</code> property.</p>
<h3>Example</h3>
<pre><code class="language-php">// Prepare users data.
$users = [
[
'name' => 'Russell Newman',
'products' => [
'totalSaved' => 5,
'totalBought' => 3
],
'location' => [
'town' => 'Andreas Ave',
'city' => 'Maasdriel',
'country' => 'England'
]
],
[
'name' => 'Willard Bowman',
'products' => [
'totalSaved' => 0,
'totalBought' => 0
],
],
[
'name' => 'Tommy Mendoza',
'products' => [
'totalSaved' => 172,
'totalBought' => 54
],
],
[
'name' => 'Joshua Edwards',
'phone' => '(382)-450-8197'
]
];
// Insert all data.
$users = $userStore->insertMany($users);</code></pre>
<h2 id="insert-data-updateOrInsert">updateOrInsert()</h2>
<p>Update or insert one document.<br/>
This method is especially fast because it uses the _id to update or insert the document directly instead of traversing through all documents.</p>
<pre><code class="language-php">public function updateOrInsert(array $data, bool $autoGenerateIdOnInsert = true): array</code></pre>
<h3>Parameters</h3>
<ol>
<li>
<h1>$data: array</h1>
<p>Document to update or insert.</p>
</li>
<li>
<h1>$autoGenerateIdOnInsert: bool</h1>
<p>Default: <code>true</code><br/>
If <code>false</code> and <strong>the document has a _id</strong>, that does not already exist (insert), the _id will be to insert the new document, an auto-generated _id will be used otherwise.</p>
</li>
</ol>
<h3>Return value</h3>
<p>Returns <code>updated / inserted document</code>.</p>
<h3>Example 1</h3>
<p>Update or Insert a user with _id = 23, apply an auto-generated _id if it is an insert.</p>
<pre><code class="language-php">$user = [
"_id" => 23,
"name" => "John",
...
];
$userStore->updateOrInsert($user);</code></pre>
<h3>Example 2</h3>
<p>Update or Insert a user with _id = 23.</p>
<pre><code class="language-php">$user = [
"_id" => 23,
"name" => "John",
...
];
$userStore->updateOrInsert($user, false);</code></pre>
<h3>Example 3</h3>
<p>Update or Insert a user with no _id.</p>
<pre><code class="language-php">$user = [
"name" => "John",
...
];
$userStore->updateOrInsert($user);</code></pre>
<h2 id="insert-data-updateOrInsertMany">updateOrInsertMany()</h2>
<p>Update or insert multiple documents.<br/>
This method is especially fast because it uses the _id to update or insert the documents directly instead of traversing through all documents.</p>
<pre><code class="language-php">public function updateOrInsertMany(array $data, bool $autoGenerateIdOnInsert = true): array</code></pre>
<h3>Parameters</h3>
<ol>
<li>
<h1>$data: array</h1>
<p>Documents to update or insert.</p>
</li>
<li>
<h1>$autoGenerateIdOnInsert: bool</h1>
<p>Default: <code>true</code><br/>
If <code>false</code> and <strong>a document has a _id</strong>, that does not already exist (insert), the _id will be to insert the new document, an auto-generated _id will be used otherwise.</p>
</li>
</ol>
<h3>Return value</h3>
<p>Returns <code>updated / inserted documents</code>.</p>
<h3>Example 1</h3>
<p>Update or Insert multiple users and apply auto-generated _id's on inserts.</p>
<pre><code class="language-php">$users = [
[
"_id" => 23,
"name" => "John",
...
],
[
"_id" => 25,
"name" => "Max",
...
],
[
"name" => "Lisa",
...
]
...
];
$userStore->updateOrInsertMany($users);</code></pre>
<h3>Example 2</h3>
<p>Update or Insert a users with their _id's.</p>
<pre><code class="language-php">$users = [
[
"_id" => 23,
"name" => "John",
...
],
[
"_id" => 25,
"name" => "Max",
...
],
[
"name" => "Lisa", // <-- will get auto-generated _id
...
]
...
];
$userStore->updateOrInsertMany($users, false);
// Lisa will get a auto-generated _id, because there is no _id in the document!</code></pre>
<div class="editlink"
file="insert-data.md"
version="2.50">
</div>
</div>
<div class="intro" id="block_fetch-data">
<h1>Fetch Data</h1>
<p>To get data from the store SleekDB provides some simple yet powerful methods.</p>
<blockquote>
<p>ℹ️ If you need to make more complex queries look into <a class="gotoblock" href="/#/query-builder">QueryBuilder</a>.</p>
</blockquote>
<h2>Brief repetition of Store object creation</h2>
<p>For a more detailed documentation on <code>Store</code> object creation refer to the corresponding <a class="gotoblock" href="#/managing-store">"Managing Store" documentation</a>.</p>
<pre><code class="language-php">use SleekDB\Store;
$newsStore = new Store('news', __DIR__ . "/database");</code></pre>
<h2>Summary</h2>
<ul>
<li><a class="gotoblock" href="#/fetch-data#findAll">findAll</a></li>
<li><a class="gotoblock" href="#/fetch-data#findById">findById</a></li>
<li><a class="gotoblock" href="#/fetch-data#findBy">findBy</a></li>
<li><a class="gotoblock" href="#/fetch-data#findOneBy">findOneBy</a></li>
<li><a class="gotoblock" href="#/fetch-data#count">count</a></li>
</ul>
<h2 id="fetch-data-findAll">Get all documents</h2>
<pre><code class="language-php">function findAll(array $orderBy = null, int $limit = null, int $offset = null): array</code></pre>
<h3>Parameters</h3>
<ol>
<li>
<h1>$orderBy: array</h1>
<p>Sort the result by one or multiple fields.</p>
<ul>
<li>["name" => "asc"]</li>
<li>["name" => "asc", "age" => "desc"]</li>
</ul>
</li>
<li>
<h1>$limit: int</h1>
<p>Limit the result to a specific amount.</p>
</li>
<li>
<h1>$offset: offset</h1>
<p>Skip a specific amount of documents.</p>
</li>
</ol>
<h3>Return value</h3>
<p>Returns either an <code>array containing all documents</code> of that store or an <code>empty array</code> if there are no documents.</p>
<h3>Example</h3>
<pre><code class="language-php">$allNews = $newsStore->findAll();</code></pre>
<h4>Example result</h4>
<pre><code>[["_id" => 12, "title" => "We love SleekDB"], ["_id" => 14, "title" => "NoSQL with just PHP"], ...]</code></pre>
<p><br/></p>
<h2 id="fetch-data-findById">Get a single document with its _id</h2>
<p>With this method SleekDB doesn't traverse through all files. Instead, it accesses the file directly, what makes this method especially fast.</p>
<pre><code class="language-php">function findById(int|string $id): array|null</code></pre>
<h3>Parameters</h3>
<ol>
<li>
<h1>$id: int|string</h1>
<p>The _id of a document located in the store.</p>
</li>
</ol>
<h3>Return value</h3>
<p>Returns <code>one document</code> or <code>null</code> if document could not be found.</p>
<h3>Example</h3>
<pre><code class="language-php">$news = $newsStore->findById(12);</code></pre>
<h4>Example result</h4>
<pre><code>["_id" => 12, "title" => "SleekDB is the Best", ...]</code></pre>
<p><br/></p>
<h2 id="fetch-data-findBy">Get one or multiple documents</h2>
<pre><code class="language-php">function findBy(array $criteria, array $orderBy = null, int $limit = null, int $offset = null): array</code></pre>
<h3>Parameters</h3>
<ol>
<li>
<h1>$criteria: array</h1>
<p>One or multiple where conditions.<br/>
Visit the <a class="gotoblock" href="#/criteria">$criteria</a> documentation page to learn more on how to define conditions.</p>
</li>
<li>
<h1>$orderBy: array</h1>
<p>Sort the result by one or multiple fields.</p>
<ul>
<li>["name" => "asc"]</li>
<li>["name" => "asc", "age" => "desc"]</li>
</ul>
</li>
<li>
<h1>$limit: int</h1>
<p>Limit the result to a specific amount.</p>
</li>
<li>
<h1>$offset: offset</h1>
<p>Skip a specific amount of documents.</p>
</li>
</ol>
<h3>Return value</h3>
<p>Returns found <code>documents in an array</code> or <code>an empty array</code> if nothing is found.</p>
<h3>Example 1</h3>
<pre><code class="language-php">$news = $newsStore->findBy(["author", "=", "John"], ["title" => "asc"], 10, 20);
// First 20 documents skipped, limited to 10 and ascending sort by title where author is John.</code></pre>
<h4>Result</h4>
<pre><code>[ ["_id" => 12, ... "title" => "Best Database"], ["_id" => 4, ... "title" => "Why SleekDB"], ...]</code></pre>
<h3>Example 2</h3>
<p>More complex where clause.</p>
<pre><code class="language-sql">WHERE ( author = "John" OR author = "Mark" ) AND ( topic like "School%" OR topic like "Work%" )</code></pre>
<pre><code class="language-php">$news = $newsStore->findBy(
[
[
["author", "=", "John"], "OR", ["author", "=", "Mark"],
],
"AND", // <-- Optional
[
["topic", "like", "School%"], "OR", ["topic", "like", "Work%"]
]
],
["title" => "asc"],
10,
20
);</code></pre>
<p><br/></p>
<h2 id="fetch-data-findOneBy">Get one document</h2>
<pre><code class="language-php">function findOneBy(array $criteria): array|null</code></pre>
<h3>Parameters</h3>
<ol>
<li>
<h1>$criteria: array</h1>
<p>One or multiple where conditions.<br/>
Visit the <a class="gotoblock" href="#/criteria">$criteria</a> documentation page to learn more on how to define conditions.</p>
</li>
</ol>
<h3>Return value</h3>
<p>Returns <code>one document</code> or <code>null</code> if nothing is found.</p>
<h3>Example 1</h3>
<pre><code class="language-php">$news = $newsStore->findOneBy(["author", "=", "Mike"]);
// Returns one news article of the author called Mike</code></pre>
<h4>Result</h4>
<pre><code>["_id" => 18, "title" => "SleekDB is super fast", "author" => "Mike"]</code></pre>
<h3>Example 2</h3>
<p>More complex where clause.</p>
<pre><code class="language-sql">WHERE ( author = "John" OR author = "Mark" ) AND ( topic like "School%" OR topic like "Work%" )</code></pre>
<pre><code class="language-php">$news = $newsStore->findOneBy(
[
[
["author", "=", "John"], "OR", ["author", "=", "Mark"],
],
"AND", // <-- Optional
[
["topic", "like", "School%"], "OR", ["topic", "like", "Work%"]
]
]
);</code></pre>
<h2 id="fetch-data-count">Get the amount of all documents stored</h2>
<p>This method is especially fast because it just counts the amount of files.</p>
<pre><code class="language-php">function count(): int</code></pre>
<h3>Return value</h3>
<p>Returns the amount of documents in the store as an <code>int</code>.</p>
<h3>Example</h3>
<pre><code class="language-php">$newsCount = $newsStore->count();
// Returns: 27 </code></pre>
<div class="editlink"
file="fetch-data.md"
version="2.50">
</div>
</div>
<div class="intro" id="block_edit-data">
<h1>Edit Data</h1>
<p>To edit a data object you can use the <code>update()</code> and <code>updateById()</code> method of the <code>Store</code> class.</p>
<blockquote>
<p>ℹ️ If you need to make more complex updates look into <a class="gotoblock" href="/#/query-builder">QueryBuilder</a> and <a class="gotoblock" href="/#/query">Query</a>.</p>
</blockquote>
<h2>Brief repetition of Store object creation</h2>
<p>For a more detailed documentation on <code>Store</code> object creation refer to the corresponding <a class="gotoblock" href="#/managing-store">"Managing Store" documentation</a>.</p>
<pre><code class="language-php">use SleekDB\Store;
$userStore = new Store('users', __DIR__ . "/database");</code></pre>
<h2>Summary</h2>
<ul>
<li><a class="gotoblock" href="#/edit-data#updateById">updateById</a></li>
<li><a class="gotoblock" href="#/edit-data#update">update</a></li>
<li><a class="gotoblock" href="#/edit-data#removeFieldsById">removeFieldsById</a></li>
<li><a class="gotoblock" href="#/edit-data#updateOrInsert">updateOrInsert</a></li>
<li><a class="gotoblock" href="#/edit-data#updateOrInsertMany">updateOrInsertMany</a></li>
</ul>
<h2 id="edit-data-updateById">updateById()</h2>
<p>Update parts of a document.<br/>
This method is especially fast because it uses the _id to update the document directly instead of traversing through all documents.</p>
<pre><code class="language-php">function updateById(int|string $id, array $updatable): array|false</code></pre>
<h3>Parameters</h3>
<ol>
<li>
<h1>$id: int|string</h1>
<p>Id of document to update.</p>
</li>
<li>
<h1>$updatable: array</h1>
<p>Array containing the parts to update.<br/>
Update of nested values possible by using a dot between fieldNames (Example 3)<br/>
If a field does not exist in that document, it will be added.</p>
</li>
</ol>
<h3>Return value</h3>
<p>Returns <code>updated document</code> on success or <code>false</code> if document could not be found.</p>
<h3>Example 1</h3>
<p>Change the status of the user with _id = 23.</p>
<pre><code class="language-php">$userStore->updateById(23, [ "status" => "active" ]);</code></pre>
<h3>Example 2</h3>
<p>Change the name and the age of the user with _id = 24.</p>
<pre><code class="language-php">$userStore->updateById(24, [ "name" => "Georg", "age" => 22 ]);</code></pre>
<h3>Example 3</h3>