-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
1000 lines (901 loc) · 37.8 KB
/
index.html
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 name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="ikhbalfuady">
<meta name="generator" content="Sopeus">
<title>LDBX - LocalStorage Laravel Syntax </title>
<link rel="canonical" href="https://getbootstrap.com/docs/5.2/examples/pricing/">
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-rbsA2VBKQhggwzxH7pPCaAqO46MgnOM80zW1RWuH61DGLwZJEdK2Kadq2F9CUG65" crossorigin="anonymous">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-kenU1KFdBIe4zVF0s0G1M5b4hcpxyD9F7jL+jjXkk+Q2h455rYXK/7HAuoJl+0I4" crossorigin="anonymous"></script>
<!-- Favicons -->
<link rel="apple-touch-icon" href="/logo.png" sizes="180x180">
<link rel="icon" href="/favicon.ico" sizes="32x32" type="image/png">
<link rel="icon" href="/favicon.ico" sizes="16x16" type="image/png">
<link rel="mask-icon" href="/logo.png" color="#712cf9">
<link rel="icon" href="/favicon.ico">
<meta name="theme-color" content="#712cf9">
<style>
.input-form {
background-color: rgb(235, 235, 235);
border: none;
}
</style>
<!-- Custom styles for this template -->
<link href="style.css?v=1.0.3" rel="stylesheet">
</head>
<body>
<div class="container py-3">
<header>
<div class="d-flex flex-column flex-md-row align-items-center pb-3 mb-4 border-bottom">
<a href="/" class="d-flex align-items-center text-dark text-decoration-none logo">
<span class="fs-4 fw-bold">LDBX</span>
</a> <sup class="d-none d-md-block versioning" >v.1.2.5</sup>
<nav class="d-inline-flex mt-2 mt-md-0 ms-md-auto">
<a class="me-3 py-2 text-dark text-decoration-none" href="https://github.com/ikhbalfuady/ldbx">Github</a>
<a class="me-3 py-2 text-dark text-decoration-none" href="#example">Example</a>
<a class="me-3 py-2 text-dark text-decoration-none" href="#documentation">Documentation</a>
<a class="me-3 py-2 text-dark text-decoration-none" href="#">Support</a>
</nav>
</div>
<div class="pricing-header p-3 pb-md-4 mx-auto text-center">
<h1 class="display-4 fw-normal">LDBX</h1>
<p class="fs-5 text-muted">Local Storage management with sweet syntax style & feature like <a href="https://laravel.com/" target="_blank">Laravel</a> Eloquent using native Javascript</p>
</div>
</header>
<!-- Store Section -->
<main id="example" class="row">
<div>
<h3 class="text-primary fw-bold"># Store</h3>
<p>You can test store into localstorage with this example, you can define the table / source and define the object structure for every data you want to save.</p>
</div>
<div class="col-xs-12 col-sm-6">
<div class="table-responsive">
<div class="text-center px-2"><h4 class="fw-bold">Table / Source</h4>
<input id="tableName" onchange="refreshObject()" value="users" class="form-control input-form" >
</div>
<table class="table text-center">
<thead>
<tr>
<th style="width: 22%;">Key</th>
<th style="width: 22%;">Value</th>
<th style="width: 5%;"></th>
</tr>
</thead>
<tbody id="objectModel">
<tr id="baseObject0">
<td>
<input value="name" onchange="refreshObject()" class="form-control input-form">
</td>
<td>
<input value="john" onchange="refreshObject()" class="form-control input-form">
</td>
<td>
<button onclick="deleteObject(0)" class="btn btn-danger">🗙</button>
</td>
</tr>
<tr id="baseObject1">
<td>
<input value="born_date" onchange="refreshObject()" class="form-control input-form">
</td>
<td>
<input value="1994-02-29" onchange="refreshObject()" class="form-control input-form">
</td>
<td>
<button onclick="deleteObject(1)" class="btn btn-danger">🗙</button>
</td>
</tr>
<tr id="baseObject2">
<td>
<input value="iq" onchange="refreshObject()" class="form-control input-form">
</td>
<td>
<input value="321" onchange="refreshObject()" class="form-control input-form">
</td>
<td>
<button onclick="deleteObject(2)" class="btn btn-danger">🗙</button>
</td>
</tr>
<tr id="baseObject3">
<td>
<input value="is_alive" onchange="refreshObject()" class="form-control input-form">
</td>
<td>
<input value="true" onchange="refreshObject()" class="form-control input-form">
</td>
<td>
<button onclick="deleteObject(3)" class="btn btn-danger">🗙</button>
</td>
</tr>
</tbody>
<tbody id="columnSave">
<tr>
<td colspan="2">When you added new colum, you can update the structure object of data using "<code class="text-danger">fixStructure</code>" </td>
<td>
<button id="addObject" class="btn btn-primary btn-full">+ Add</button>
</td>
</tr>
<tr>
<td ></td>
<td colspan="2" class="text-end">
<button onClick="store()" class="btn btn-success btn-full">Store to LDB</button>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<div class="col-xs-12 col-sm-6">
<div class="text-center px-2"><h4 class="fw-bold">Object Result</h4></div>
<pre id="objectDisplay" class="codex"><code class="language-json"></code></pre>
<div class="text-center px-2"><h4 class="fw-bold">LDB Logs</h4></div>
<pre id="ldbStoreLog" class="codex"><code class="language-json"></code></pre>
</div>
</main>
<!-- Modal Edit -->
<div class="modal fade" id="editModal" tabindex="-1" aria-labelledby="editModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h1 class="modal-title fs-5" id="editModalLabel">Edit : <span id="editId"></span></h1>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<div id="columnEdit"> </div>
</div>
<div class="modal-footer">
<button id="closeModal" type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<button type="button" onClick="updateData()" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
<!-- Modal Filter -->
<div class="modal fade modal-lg" id="filterModal" tabindex="-1" aria-labelledby="filterModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h1 class="modal-title fs-5" id="filterModalLabel">Filter</h1>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<p class="text-danger">*Using coma separator for IN & NOT IN operator</p>
<h4 class="fw-bold text-primary">Single Selector</h4>
<div id="filterList"></div>
<h4 class="fw-bold text-primary">Multiple Selector</h4>
<div id="filterList2"></div>
<pre id="filterDisplay" class="codex"></pre>
</div>
<div class="modal-footer">
<button id="closeModal" type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<button type="button" onClick="resetFilter()" class="btn btn-outline-secondary ">Reset Filter</button>
<button type="button" onClick="reloadData()" class="btn btn-primary">Filter Data</button>
</div>
</div>
</div>
</div>
<!-- Store Section -->
<main class="row">
<div>
<h3 class="text-primary fw-bold"># Manage </h3>
<p>you can Edit, Delete & Query of data you have, and has support to filter by date data type </p>
</div>
<div class="col-xs-12 ">
<div class="table-responsive">
<button onClick="cleanUp()" class="btn btn-danger btn-full">Clear LDB</button>
<button onClick="refreshObject()" class="btn btn-info btn-full">Refresh Data</button>
<button data-bs-toggle="modal" data-bs-target="#filterModal" class="btn btn-secondary">Filter / Query</button>
<table class="table text-center">
<thead id="tableHead"></thead>
<tbody id="tableData">
</tbody>
</table>
</div>
</div>
</main>
<main id="documentation" class="mt-5">
<!-- Usage -->
<div>
<h3 class="text-primary fw-bold"># Documentation</h3>
<p>
first of all, make sure the script has been called in your html
</p>
<pre class="code">
<script src="ldbx.js"></script>
</pre>
</div>
<!-- API -->
<div class="row">
<div class="col-sm-12 mb-4">
<h4 class="fw-bold text-primary">Usages</h4>
</div>
<div class="col-sm-12 docx">
<!-- Start -->
<p><strong>Initialize Table</strong></p>
<p>There are 3 params in constructor :</p>
<blockquote>
<p>using params default</p>
<ol>
<li>Table Name [String] (Required)</li>
<li>Query Log in console [Boolean] (Optional)</li>
<li>Soft Delete [Boolean] (Optional)</li>
</ol>
<p>using config params
you can using object configuration params with structure like this :</p>
<pre><code>{
<span class="hljs-attribute">table</span>: String,
showLog: Boolean,
softDelete: Boolean,
}
</code></pre></blockquote>
<p>Example instance</p>
<pre><code>
<span class="hljs-comment">// Using default params</span>
<span class="hljs-keyword">const</span> DataModel = <span class="hljs-keyword">new</span> LDBX(<span class="hljs-string">'users'</span>, <span class="hljs-keyword">true</span>, <span class="hljs-keyword">false</span>)
<span class="hljs-comment">// Using Config params</span>
<span class="hljs-keyword">const</span> config = {
table: <span class="hljs-string">'users'</span>,
<span class="hljs-built_in">log</span>: <span class="hljs-keyword">true</span>,
softDelete: <span class="hljs-keyword">false</span>,
}
<span class="hljs-keyword">const</span> DataModel = <span class="hljs-keyword">new</span> LDBX(config)
</code></pre><hr>
<p><strong>Get All Data</strong></p>
<pre><code><span class="hljs-keyword">const</span> UserModel = <span class="hljs-keyword">new</span> LDBX(<span class="hljs-string">'users'</span>)
<span class="hljs-keyword">var</span> list = UserModel.<span class="hljs-keyword">get</span>()
</code></pre><hr>
<p><strong>Get first / single data</strong></p>
<pre><code><span class="hljs-keyword">const</span> UserModel = <span class="hljs-keyword">new</span> LDBX(<span class="hljs-string">'users'</span>)
<span class="hljs-keyword">var</span> data = UserModel.<span class="hljs-keyword">where</span>(<span class="hljs-string">'id'</span>, id).first() <span class="hljs-comment">// *first its mean, first of result query</span>
</code></pre><hr>
<p><strong>Single selection where</strong></p>
<pre><code><span class="hljs-keyword">const</span> UserModel = <span class="hljs-keyword">new</span> LDBX(<span class="hljs-string">'users'</span>)
<span class="hljs-keyword">var</span> data = UserModel.<span class="hljs-keyword">where</span>(<span class="hljs-string">'name'</span>, <span class="hljs-string">'ikhbalfuady'</span>).<span class="hljs-keyword">get</span>()
</code></pre><hr>
<p><strong>Multiple selection where</strong></p>
<pre><code><span class="hljs-comment">// using custom operator</span>
<span class="hljs-keyword">const</span> UserModel = <span class="hljs-keyword">new</span> LDBX(<span class="hljs-string">'users'</span>)
<span class="hljs-keyword">var</span> data = UserModel.<span class="hljs-keyword">where</span>(<span class="hljs-string">'age'</span>, <span class="hljs-string">'>='</span> <span class="hljs-number">18</span>)
.<span class="hljs-keyword">where</span>(<span class="hljs-string">'age'</span>, <span class="hljs-string">'<='</span>, <span class="hljs-number">25</span>)
.<span class="hljs-keyword">get</span>()
</code></pre><hr>
<p><strong>Find by id</strong></p>
<pre><code><span class="hljs-keyword">const</span> UserModel = <span class="hljs-keyword">new</span> LDBX(<span class="hljs-string">'users'</span>)
<span class="hljs-keyword">var</span> data = UserModel.find(id)
</code></pre><hr>
<p><strong>Store Data</strong></p>
<pre><code><span class="hljs-title">const</span> <span class="hljs-type">UserModel</span> = new <span class="hljs-type">LDBX</span>('users')
<span class="hljs-title">let</span> <span class="hljs-class"><span class="hljs-keyword">data</span> = {
<span class="hljs-title">name</span>: "<span class="hljs-type">John</span>",
<span class="hljs-title">age</span>: 21
}</span>
<span class="hljs-type">UserModel</span>.save(<span class="hljs-class"><span class="hljs-keyword">data</span>)</span>
</code></pre><hr>
<p><strong>Update Data</strong></p>
<pre><code>const UserModel = new LDBX(<span class="hljs-string">'users'</span>)
let <span class="hljs-keyword">data</span> = UserModel.find(id)
<span class="hljs-keyword">if</span> (!<span class="hljs-keyword">data</span>) throw error(`user <span class="hljs-keyword">with</span> id<span class="hljs-string">"${id}"</span> <span class="hljs-built_in">not</span> found `)
<span class="hljs-comment">// set value into object</span>
<span class="hljs-keyword">data</span>.<span class="hljs-keyword">name</span> = <span class="hljs-string">"Ikhbalfuady"</span>
UserModel.update(<span class="hljs-keyword">data</span>)
</code></pre><hr>
<p><strong>Delete Data</strong></p>
<pre><code><span class="hljs-keyword">const</span> UserModel = <span class="hljs-built_in">new</span> LDBX(<span class="hljs-string">'users'</span>)
UserModel.<span class="hljs-built_in">delete</span>(id) <span class="hljs-comment">// default soft delete</span>
<span class="hljs-comment">// permanent delete</span>
<span class="hljs-keyword">const</span> UserModel = <span class="hljs-built_in">new</span> LDBX(<span class="hljs-string">'users'</span>)
UserModel.<span class="hljs-built_in">delete</span>(id, <span class="hljs-literal">true</span>)
</code></pre><h1 id="avail-where-operator">Avail "<strong>Where</strong>" Operator</h1>
<table>
<thead>
<tr>
<th>Name</th>
<th>value</th>
<th>Syntax</th>
<th>Status</th>
</tr>
</thead>
<tbody>
<tr>
<td>Equal</td>
<td>=</td>
<td>.where('key', '=', value) OR .where('key', value)</td>
<td>READY</td>
</tr>
<tr>
<td>Like / Contains</td>
<td>like</td>
<td>.where('key', 'like', value)</td>
<td>READY</td>
</tr>
<tr>
<td>Less Than</td>
<td><</td>
<td>.where('key', '<', value)</td>
<td>READY</td>
</tr>
<tr>
<td>Less Than Equal</td>
<td><=</td>
<td>.where('key', '<=', value)</td>
<td>READY</td>
</tr>
<tr>
<td>Greater Than</td>
<td>></td>
<td>.where('key', '>', value)</td>
<td>READY</td>
</tr>
<tr>
<td>Greater Than Equal</td>
<td>>=</td>
<td>.where('key', '>=', value)</td>
<td>READY</td>
</tr>
<tr>
<td>Is NULL</td>
<td>N/n</td>
<td>.where('key', 'N', value) OR .where('key', 'n', value)</td>
<td>READY</td>
</tr>
<tr>
<td>Is NOT NULL</td>
<td>NN / nn</td>
<td>.where('key', 'NN', value) OR .where('key', 'nn', value)</td>
<td>READY</td>
</tr>
<tr>
<td>IN</td>
<td>IN</td>
<td>.where('key', 'IN', arrayValue) // arrayValue = [1,2,3]</td>
<td>READY</td>
</tr>
<tr>
<td>NOT IN</td>
<td>NIN</td>
<td>.where('key', 'NIN', arrayValue) // arrayValue = [1,2,3]</td>
<td>READY</td>
</tr>
</tbody>
</table>
<hr>
<h1 id="other-features">Other Features</h1>
<p><strong>trashed()</strong></p>
<p>this is method chaining to getting only trashed data</p>
<pre><code><span class="hljs-keyword">var</span> UserModel = <span class="hljs-keyword">new</span> <span class="hljs-type">LDBX</span>(<span class="hljs-string">'users'</span>)
.trashed()
.<span class="hljs-keyword">get</span>()
</code></pre><p><strong>withTrashed()</strong></p>
<p>this is method chaining to getting all with trashed data if you using <strong>softDelete</strong></p>
<pre><code><span class="hljs-keyword">var</span> UserModel = <span class="hljs-keyword">new</span> <span class="hljs-type">LDBX</span>(<span class="hljs-string">'users'</span>)
.withTrashed()
.<span class="hljs-keyword">get</span>()
</code></pre><p><strong>truncate()</strong></p>
<p>this is method chaining to clean up data </p>
<pre><code><span class="hljs-keyword">var</span> UserModel = <span class="hljs-keyword">new</span> <span class="hljs-type">LDBX</span>(<span class="hljs-string">'users'</span>)
.truncate()
</code></pre><p><strong>hasData()</strong></p>
<p>this is method chaining to checking table has already have data or not</p>
<pre><code><span class="hljs-keyword">var</span> UserModel = <span class="hljs-keyword">new</span> <span class="hljs-type">LDBX</span>(<span class="hljs-string">'users'</span>)
.hasData()
</code></pre><p><strong>getFillableColumn()</strong></p>
<p>this is method chaining to getting fillable columns of object you set</p>
<pre><code><span class="hljs-comment">// getting fillable of existing data</span>
<span class="hljs-selector-tag">var</span> UserModel = new LDBX(<span class="hljs-string">'users'</span>)
<span class="hljs-selector-tag">var</span> user = UserModel.find(<span class="hljs-number">1</span>)
<span class="hljs-selector-tag">var</span> fillableColumns = UserModel.getFillableColumn(user)
</code></pre><p><strong>fixStructure()</strong></p>
<p>this is method chaining to fixing structure like ALTER TABLE in SQL\
when you chaning structure of object model you have, you just call this method for fix that
example :</p>
<pre><code><span class="hljs-comment">// current model</span>
<span class="hljs-keyword">let</span> UsersSpec = {
<span class="hljs-attribute">dbConfig</span>: {
<span class="hljs-attribute">table</span>: <span class="hljs-string">'users'</span>,
<span class="hljs-attribute">showLog</span>: <span class="hljs-literal">true</span>,
<span class="hljs-attribute">softDelete</span>: <span class="hljs-literal">true</span>,
},
<span class="hljs-attribute">dataModel</span>: {
<span class="hljs-attribute">id:</span><span class="hljs-string"> null</span>,
<span class="hljs-attribute">name</span>: <span class="hljs-literal">null</span>,
<span class="hljs-attribute">email</span>: <span class="hljs-literal">null</span>
}
}
<span class="hljs-keyword">const</span> UserModel = <span class="hljs-keyword">new</span> LDBX(UsersSpec.dbConfig)
<span class="hljs-keyword">const</span> fillableColumns = UserModel.getFillableColumn(UsersSpec.dataModel)
<span class="hljs-comment">/* return
['id', 'name', 'email']
*/</span>
<span class="hljs-comment">// and you want to change the users to model to like this</span>
<span class="hljs-keyword">let</span> UsersSpec = {
<span class="hljs-attribute">dbConfig</span>: {
<span class="hljs-attribute">table</span>: <span class="hljs-string">'users'</span>,
<span class="hljs-attribute">showLog</span>: <span class="hljs-literal">true</span>,
<span class="hljs-attribute">softDelete</span>: <span class="hljs-literal">true</span>,
},
<span class="hljs-attribute">dataModel</span>: {
<span class="hljs-attribute">id:</span><span class="hljs-string"> null</span>,
<span class="hljs-attribute">name</span>: <span class="hljs-literal">null</span>,
<span class="hljs-attribute">email</span>: <span class="hljs-literal">null</span>,
<span class="hljs-attribute">phone</span>: <span class="hljs-literal">null</span>, <span class="hljs-comment">// new</span>
<span class="hljs-attribute">born_date</span>: <span class="hljs-literal">null</span>, <span class="hljs-comment">// new</span>
}
}
<span class="hljs-comment">// just do like this</span>
<span class="hljs-keyword">const</span> UserModel = <span class="hljs-keyword">new</span> LDBX(UsersSpec.dbConfig)
<span class="hljs-keyword">const</span> newFillableColumns = UserModel.getFillableColumn(UsersSpec.dataModel)
UserModel.fixStructure(newFillableColumns) <span class="hljs-comment">// all of data will be updated with new fillable structure</span>
</code></pre><p><strong>orderBy(key, type?, dateFormat?)</strong></p>
<p>this is method chaining to implementing "order data" of result query \</p>
<ul>
<li>key : String // object key</li>
<li>type : Enum(asc,desc) (default : asc)</li>
<li>dateFormat : Boolean (default : false) // for sorting date type data</li>
</ul>
<pre><code><span class="hljs-keyword">const</span> UserModel = <span class="hljs-keyword">new</span> LDBX(<span class="hljs-string">'jorunal'</span>)
.orderBy(<span class="hljs-string">'key'</span>) <span class="hljs-comment">// order ASC</span>
.<span class="hljs-keyword">get</span>()
<span class="hljs-keyword">const</span> UserModel = <span class="hljs-keyword">new</span> LDBX(<span class="hljs-string">'jorunal'</span>)
.orderBy(<span class="hljs-string">'key'</span>, <span class="hljs-string">'desc'</span>) <span class="hljs-comment">// order DESC</span>
.<span class="hljs-keyword">get</span>()
<span class="hljs-comment">// ordering date data</span>
<span class="hljs-keyword">const</span> UserModel = <span class="hljs-keyword">new</span> LDBX(<span class="hljs-string">'transaction'</span>)
.orderBy(<span class="hljs-string">'date'</span>, <span class="hljs-string">'desc'</span>, <span class="hljs-literal">true</span>) <span class="hljs-comment">// fot better result activate date format with set 3 in third parameter</span>
.<span class="hljs-keyword">get</span>()
</code></pre><ul>
<li>Atenttion : order can only be executed once per query, even if you add multiple 'orderBy', the last 'orderBy' is recognized</li>
</ul>
<hr>
<p><strong>belongsTo(tableName, foreignKey, newObjectName?, targetKey?)</strong></p>
<p>this is method chaining to implementing relation of some data \
this method will be created new single object for every object result data \</p>
<ul>
<li>tableName : name of table</li>
<li>foreignKey : foreign key object, ex : role_id</li>
<li>newObjectName : object name for the relation , ex : role</li>
<li>targetKey : target key object of source data, default is "id"</li>
</ul>
<pre><code>const UserModel = <span class="hljs-keyword">new</span> LDBX(<span class="hljs-string">'users'</span>)
.belongsTo(<span class="hljs-string">'roles'</span>, <span class="hljs-string">'role_id'</span>, <span class="hljs-string">'role'</span>) // simply <span class="hljs-keyword">version</span>
.<span class="hljs-keyword">first</span>()
/* <span class="hljs-keyword">return</span>
{
id: <span class="hljs-number">1</span>
name: <span class="hljs-string">'John'</span>,
role_id: <span class="hljs-number">1</span>,
role<span class="hljs-variable">s:</span> {
id: <span class="hljs-number">1</span>,
name: <span class="hljs-string">'Admin'</span>,
},
}
*/
const UserModel = <span class="hljs-keyword">new</span> LDBX(<span class="hljs-string">'jorunal'</span>)
.belongsTo(<span class="hljs-string">'coa'</span>, <span class="hljs-string">'coa_revenue_code'</span>, <span class="hljs-string">'coa_revenue'</span>, <span class="hljs-string">'code'</span>) // full <span class="hljs-keyword">version</span>
.<span class="hljs-built_in">get</span>()
/* <span class="hljs-keyword">return</span>
[
{
id: <span class="hljs-number">1</span>
name: <span class="hljs-string">'TRX Forex'</span>,
coa_revenue_code: <span class="hljs-string">'101020230'</span>
coa_revenue: {
code: <span class="hljs-string">'101020230'</span>,
name: <span class="hljs-string">'Fix Revenue'</span>,
}
},
{
id: <span class="hljs-number">2</span>
name: <span class="hljs-string">'TRX others'</span>,
coa_revenue_code: <span class="hljs-string">'4305353353'</span>
coa_revenue: {
code: <span class="hljs-string">'4305353353'</span>,
name: <span class="hljs-string">'Other Revenue'</span>,
}
}
]
*/
</code></pre><hr>
<p><strong>hasMany(tableName, foreignKey, newObjectName?, targetKey?)</strong></p>
<p>this is method chaining to implementing relation of some data\
this method will be created new single object with array result for every object in result data\</p>
<ul>
<li>tableName : name of table</li>
<li>foreignKey : foreign key object, ex : role_id</li>
<li>newObjectName : object name for the relation , ex : role</li>
<li>targetKey : target key object of source data, default is "id"</li>
</ul>
<pre><code>const UserModel = new LDBX(<span class="hljs-string">'users'</span>)
.hasMany(<span class="hljs-string">'roles'</span>, <span class="hljs-string">'role_id'</span>, <span class="hljs-string">'roles'</span>) // simply version
.first()
/* <span class="hljs-keyword">return</span>
{
id: <span class="hljs-number">1</span>
name: <span class="hljs-string">'John'</span>,
roles: [
{
id: <span class="hljs-number">1</span>,
name: <span class="hljs-string">'Admin'</span>,
},
{
id: <span class="hljs-number">2</span>,
name: <span class="hljs-string">'Staff'</span>,
}
],
}
*/
const UserModel = new LDBX(<span class="hljs-string">'jorunal'</span>)
.hasMany(<span class="hljs-string">'coa'</span>, <span class="hljs-string">'coa_revenue_code'</span>, <span class="hljs-string">'coa_revenues'</span>, <span class="hljs-string">'code'</span>) // full version
.get()
/* <span class="hljs-keyword">return</span>
[
{
id: <span class="hljs-number">1</span>
name: <span class="hljs-string">'transaction'</span>,
coa_revenues: [
{
code: <span class="hljs-string">'101020230'</span>,
name: <span class="hljs-string">'Fix Reveneu'</span>,
},
{
code: <span class="hljs-string">'105020230'</span>,
name: <span class="hljs-string">'Other Revenue
],
}
]
*/</span>
</code></pre>
<!-- End -->
</div>
</div>
</main>
<footer class="pt-4 my-md-5 pt-md-5 border-top">
<div class="row">
<div class="col-12 col-md">
<small class="d-block mb-3 text-muted">© 2021–2022 | <a href="https://github.com/ikhbalfuady" >ikhbalfuady</a></small>
</div>
</div>
</footer>
</div>
<script src="ldbx.min.js?v=1.2.5"></script>
<script>
let counter = 4;
const tableName = document.querySelector('#tableName').value
const DBx = new DB();
var DataModel = InitModel()
const objectModel = document.getElementById('objectModel');
function InitModel () {
const tableName = document.querySelector('#tableName').value
return new LDBX(tableName)
}
// Function to add a new tr element
function addTrElement() {
const tr = document.createElement('tr');
tr.id = `baseObject${counter}`;
tr.innerHTML = `
<td>
<input onChange="refreshObject()" class="form-control input-form">
</td>
<td>
<input onChange="refreshObject()" class="form-control input-form">
</td>
<td>
<button onClick="deleteObject(${counter})" class="btn btn-danger">🗙</button>
</td>
`;
objectModel.appendChild(tr);
counter++;
}
// Add an event listener to the delete button
function deleteObject (idx) {
document.querySelector(`#baseObject${idx}`).remove();
refreshObject()
}
// Add an event listener to the addObject button
const addObjectButton = document.getElementById('addObject');
addObjectButton.addEventListener('click', addTrElement);
// Function to get the data of the objectModel element
function getObjectFromTable() {
// Get the table element
const table = document.getElementById('objectModel');
// Initialize an empty object
const object = {};
// Iterate over the rows in the table
for (let i = 0; i < table.rows.length; i++) {
// Get the current row
const row = table.rows[i];
// Get the key and value cells for the current row
const keyCell = row.cells[0];
const valueCell = row.cells[1];
// Get the key and value from the input elements in the cells
let key = keyCell.querySelector('input').value;
let value = valueCell.querySelector('input').value;
var fixValue = value
if (value === 'true') fixValue = true
else if (value === 'false') fixValue = false
else if (!isNaN(value)) fixValue = parseFloat(value)
// Add the key-value pair to the object
object[key] = fixValue;
}
// Return the object
return object;
}
function objectDisplayFormater (data) {
document.getElementById('objectDisplay').innerHTML= JSON.stringify(data);
}
function getColumnFillable () {
var obj = getObjectFromTable()
var res = []
for (k in obj) { res.push(k) }
return res
}
function getFilterDisplay () {
var columns = getColumnFillable()
var uniqueComparasion = ['N', 'NN']
var res = 'var data = Model'
var valueId = el(`#filter_value_id`).value
if (valueId) res += `.where('id', '${valueId}') \n`
for (col of columns) {
var operator = el(`#filter_operator_${col}`).value
var value = el(`#filter_value_${col}`).value
if (uniqueComparasion.includes(operator)) res += `.where('${col}', '${operator}') \n`
if (value) res += `.where('${col}', '${operator}', '${value}') \n`
}
var colx = el(`#filter2_column`).value
var opr1 = el(`#filter2_operator1`).value
var opr2 = el(`#filter2_operator2`).value
var val1 = el(`#filter2_value1`).value
var val2 = el(`#filter2_value2`).value
if (val1 && val2) {
res += `.where('${colx}', '${opr1}', '${val1}').where('${colx}', '${opr2}', '${val2}') \n`
}
res = `${res}.get()`
el(`#filterDisplay`).innerHTML = res
}
function getOperatorEl () {
return `
<option value="=">Equal (=)</option>
<option value="like">Contains (like)</option>
<option value=">">Greather Than (>)</option>
<option value=">=">Greather Than Equal(>=)</option>
<option value="<">Less Than(<)</option>
<option value="<=">Less Than Equal(<=)</option>
<option value="N">IS NULL (N)</option>
<option value="NN">NOT NULL (NN)</option>
<option value="IN">IN (IN)</option>
<option value="NIN">NOT IN (NIN)</option>
`
}
function getFilterList () {
var columns = getColumnFillable()
var filterList = ''
filterList += `
<div class="mb-3 row">
<div class="col-sm-2 col-form-label fw-bold ">ID</div>
<div class="col-sm-10">
<input onChange="getFilterDisplay()" class="form-control" id="filter_value_id">
</div>
</div>
`
for (col of columns) {
filterList += `
<div class="mb-3 row">
<div class="col-sm-2 col-form-label fw-bold ">${col}</div>
<div class="col-6 col-sm-5">
<select onChange="getFilterDisplay()" class="form-control" id="filter_operator_${col}">
${getOperatorEl()}
</select>
</div>
<div class="col-6 col-sm-5">
<input onChange="getFilterDisplay()" class="form-control" id="filter_value_${col}">
</div>
</div>
`
}
el('#filterList').innerHTML = filterList
}
function getFilterList2 () {
var columns = getColumnFillable()
var colOpt = ''
for (col of columns) {
colOpt += `<option value="${col}">${col}</option>`
}
var filterList = ''
filterList += `
<div class="mb-3 row">
<div class="col-sm-12 mb-3">
<select onChange="getFilterDisplay()" class="form-control" id="filter2_column">
${colOpt}
</select>
</div>
<div class="mb-3 col-6 col-sm-3">
<select onChange="getFilterDisplay()" class="form-control" id="filter2_operator1">
${getOperatorEl()}
</select>
</div>
<div class="mb-3 col-6 col-sm-3">
<input onChange="getFilterDisplay()" class="form-control" id="filter2_value1">
</div>
<div class="mb-3 col-6 col-sm-3">
<select onChange="getFilterDisplay()" class="form-control" id="filter2_operator2">
${getOperatorEl()}
</select>
</div>
<div class="mb-3 col-6 col-sm-3">
<input onChange="getFilterDisplay()" class="form-control" id="filter2_value2">
</div>
</div>
`
el('#filterList2').innerHTML = filterList
}
function refreshObject () {
DataModel = InitModel()
objectDisplayFormater(getObjectFromTable())
// objectDisplayFormater(getObjectModelData())
// fixing table structure if changed
var columns = getColumnFillable()
DataModel.fixStructure(columns)
// get data table
getAllData()
getFilterList()
getFilterList2()
}
function initDemoData () {
var send = [
{"name":"John","born_date":"1994-02-29","iq":120,"is_alive":true},
{"name":"Doe","born_date":"1999-05-19","iq":140,"is_alive":true},
{"name":"Alice","born_date":"2000-11-24","iq":115,"is_alive":true},
{"name":"George","born_date":"2000-06-24","iq":130,"is_alive":'false'},
]
var list = DataModel.get() || []
if (tableName === 'users') {
if (list && !list.length) {
for (row of send) {
DataModel.save(row)
}
}
}
}
initDemoData()
refreshObject()
/* Logic */
function el (selector) {
return document.querySelector(selector)
}
function cleanUp() {
if (confirm('Are you sure want to clean up this data ?')) {
DataModel.truncate(); //item
refreshObject()
}
}
function resetFilter () {
var columns = getColumnFillable()
for (col of columns) {
var value = el(`#filter_value_${col}`).value = null
}
el(`#filter2_value1`).value = null
el(`#filter2_value2`).value = null
el(`#filter_value_id`).value = null
getAllData()
}
function handleInOperatorData (val, opr) {
var res = val
if (opr === 'IN' || opr === 'NIN') {
res = val.split(',')
res = res.map(val => {
return DBx.valueFormater(val)
})
console.log(opr, val, res)
}
return res
}
function reloadData () {
var columns = getColumnFillable()
var DataModel = new LDBX(tableName)
for (col of columns) {
var operator = el(`#filter_operator_${col}`).value
var value = el(`#filter_value_${col}`).value
if (value) DataModel.where(col, operator, handleInOperatorData(value, operator) )
}
var valueId = el(`#filter_value_id`).value
if (valueId) DataModel.where('id', '=', valueId)
var colx = el(`#filter2_column`).value
var opr1 = el(`#filter2_operator1`).value
var opr2 = el(`#filter2_operator2`).value
var val1 = el(`#filter2_value1`).value
var val2 = el(`#filter2_value2`).value
if (val1 && val2) {
DataModel.where(colx, opr1, handleInOperatorData(val1, opr1))
DataModel.where(colx, opr2, handleInOperatorData(val2, opr2))
}
const data = DataModel.get()
console.log('DataModel', DataModel)
console.log('query', data)
getAllData(data, true)
}
function getAllData (customList = [], outsource = false) {
var list = customList.length ? customList : DataModel.get()
if (outsource) list = customList
// console.log('getAllData', list)
// make header column
var columns = getColumnFillable()
var thead = ''
thead += `<th>#</th>`
thead += `<th>ID</th>`
for (col of columns) {
thead += `<th>${col}</th>`
}
thead = `<tr>${thead}</tr>`
el('#tableHead').innerHTML = thead
// make table row data
var tbody = ''
if (list) {
for (row of list) {
let td = ''
td += `<td>
<button data-bs-toggle="modal" data-bs-target="#editModal" onClick="editData('${row.id}')" class="btn btn-sm btn-success">Edit</button>
<button onClick="deleteData('${row.id}')" class="btn btn-sm btn-danger">Delete</button>
</td>`
td += `<td>${row.id}</td>`
for (col of columns) {
td += `<td>${row[col]}</td>`
}
tbody += `<tr>${td}</tr>`
}
el('#tableData').innerHTML = tbody
}
}
function makeColumnHead(name) {
return `<td>`
}
function store () {
var data = getObjectFromTable()
var result = DataModel.save(data)
console.log('saving into :',tableName, result)
el('#ldbStoreLog').innerHTML = JSON.stringify(result)
getAllData()
}
// preparation form edit & inject data
function editData (id) {
var data = new LDBX(tableName).find(id)
console.log('edit', data)
var input = ''
for (k in data) {
if (k !== 'id') {
input += `<div class="mb-3">
<label class="form-label fw-bold">${k}</label>
<input id="form_col_${k}" class="form-control" value="${data[k]}">
</div>`
} else input += `<input type="hidden" id="form_col_${k}" class="form-control" value="${data[k]}">`
}
el('#editId').innerHTML = data.id
el('#columnEdit').innerHTML = input
}
// commit changes data
function updateData () {
var id = el(`#form_col_id`).value || null
let DataModel = new LDBX(tableName)
var data = DataModel.find(id)
for (k in data) {
data[k] = el(`#form_col_${k}`).value || null
}
console.log('saving', data)
console.log('DM', DataModel)
DataModel.update(data)
getAllData()
el('#closeModal').click()
alert('Data updated!')
}
function deleteData (id) {
console.log('deleteData', id)
if (confirm('Are you sure want to delete this data ?')) {
let DataModel = new LDBX(tableName)
DataModel.delete(id)
getAllData()
}
}
</script>
</body>
</html>