sorla-db 1.0.0 → 1.2.0

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/LICENSE CHANGED
@@ -1,21 +1,21 @@
1
- MIT License
2
-
3
- Copyright (c) 2023 Sysborg
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Sysborg
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,31 +1,274 @@
1
- # sorla-db
2
- A simple and temporary database focused to use in browser environment that will bring capabilities for other tools that will be launched as soon as possible.
3
-
4
- This idea is from our founder #andmarruda that give this idea and alot of this idea are developed by him.
5
-
6
- To create a minify file run
7
- ```
8
- node index.js
9
- ```
10
-
11
- To test the environment using jest run...
12
- Don`t forget to install the dev dependecies before, using:
13
-
14
- ```
15
- npm install --only=dev
16
- ```
17
-
18
- ```
19
- npm test
20
- ```
21
-
22
- Soon some documentations....
23
-
24
- Prohibited characters $ and .,
25
- $ because is used to call operators and . because uses to search key inside a object's object
26
-
27
- ## Find Operators
28
-
29
- ### $and
30
-
31
- The **$and** operator meticulously processes arrays, employing a loop chain mechanism. During this process, it's engineered for efficiency: if it encounters any comparison that yields **false**, it immediately halts the ongoing chain. This intelligent design is aimed at preventing unnecessary computation time, especially when dealing with documents that don't meet the specified condition. It's all about optimizing performance and ensuring swift, precise operations.
1
+ # sorla-db
2
+ A simple and temporary database focused to use in browser environment that will bring capabilities for other tools that will be launched as soon as possible.
3
+
4
+ This idea is from our founder #andmarruda that give this idea and alot of this idea are developed by him.
5
+
6
+ ## Installation
7
+
8
+ ```
9
+ npm install sorla-db
10
+ ```
11
+
12
+ ```js
13
+ // Node / bundlers
14
+ const sorla = require('sorla-db');
15
+ // or
16
+ import sorla from 'sorla-db';
17
+ ```
18
+
19
+ ```html
20
+ <!-- browser, 9.5 kB: registers window.sorla -->
21
+ <script src="https://cdn.jsdelivr.net/npm/sorla-db@1/dist/sorla.min.js"></script>
22
+ ```
23
+
24
+ TypeScript declarations are included.
25
+
26
+ ## Development
27
+
28
+ ```
29
+ npm install
30
+ npm test # jest
31
+ npm run build # dist/sorla.min.js
32
+ ```
33
+
34
+ Prohibited characters $ and .,
35
+ $ because is used to call operators and . because uses to search key inside a object's object
36
+
37
+ ## Registering and creating a collection example
38
+ ```
39
+ window.sorla = new sorla();
40
+ window.sorla.createDb('tags');
41
+ window.sorla.useDb('tags');
42
+ window.sorla.db.createCollection('updatedTags');
43
+ window.sorla.db.updatedTags.insertMany([
44
+ {
45
+ name: 'Sonia',
46
+ age: 53
47
+ },
48
+ {
49
+ name: 'Anderson',
50
+ age: 36
51
+ }
52
+ ]);
53
+ ```
54
+
55
+ ## Inserting documents
56
+
57
+ `insertOne` stores a **copy** of the document and returns its `_id` (a uuid v4 when the document
58
+ has none). `insertMany` validates the whole array first, so an invalid document never leaves the
59
+ collection with half of the batch, and returns the ids.
60
+
61
+ ```
62
+ const id = window.sorla.db.updatedTags.insertOne({ name: 'Ana', nickname: null });
63
+ const ids = window.sorla.db.updatedTags.insertMany([{ name: 'Bia' }, { _id: 'custom', name: 'Caio' }]);
64
+ ```
65
+
66
+ - `null` values are allowed, at any depth
67
+ - a duplicated `_id` throws
68
+ - keys with `$` or `.` throw, at any depth
69
+ - values must be cloneable (no functions or DOM nodes)
70
+
71
+ Documents returned by the collection are read-only: assigning a field throws.
72
+
73
+ ## Reading results
74
+
75
+ `find()` returns a cursor:
76
+
77
+ ```
78
+ const cursor = window.sorla.db.updatedTags.find({ age: { $gte: 18 } });
79
+ cursor.length; // number of documents
80
+ cursor.first(); // data of the first one, null when empty
81
+ cursor.last(); // data of the last one, null when empty
82
+ cursor.toArray(); // plain copies
83
+ while(cursor.hasNext()) cursor.next();
84
+ ```
85
+
86
+ `findOne()` returns the data of the first matching document, or `null`.
87
+
88
+ ## Sorting
89
+
90
+ ```
91
+ window.sorla.db.updatedTags.find({}).sort({ age: -1, name: 1 }).toArray();
92
+ ```
93
+
94
+ `1` is ascending and `-1` descending, and `"a.b"` paths work. `sort()` returns the cursor. Values of
95
+ different types follow MongoDB's order: missing and null, numbers, strings, objects, arrays,
96
+ booleans, dates. Strings compare by code point. Pass a collation to compare by language rules,
97
+ using the `Intl.Collator` options:
98
+
99
+ ```
100
+ cursor.sort({ name: 1 }, { collation: { locale: 'pt-BR', sensitivity: 'base', numeric: true } });
101
+ ```
102
+
103
+ ## Find Operators
104
+
105
+ ### $and
106
+
107
+ The **$and** operator meticulously processes arrays, employing a loop chain mechanism. During this process, it's engineered for efficiency: if it encounters any comparison that yields **false**, it immediately halts the ongoing chain. This intelligent design is aimed at preventing unnecessary computation time, especially when dealing with documents that don't meet the specified condition. It's all about optimizing performance and ensuring swift, precise operations.
108
+
109
+ ### Example
110
+
111
+ ```
112
+ const results = window.sorla.db.updatedTags.find({
113
+ $and: [
114
+ {name: 'Anderson'},
115
+ {age: 36}
116
+ ]
117
+ });
118
+ ```
119
+
120
+ ### $or
121
+ The **$or** operator processes arrays, emploing a loop chain mechanism. During this process, it~s engineered for efficiency: if it enconters any comparison that yields **true**, it imediately halts
122
+ the ongoing chain. The intelligent design is aimed at preventing unnecessary computation time, especially when dealing with documents that the first condition matchs the needs. It's all about optimizing
123
+ performance and ensuring swift, precise operations.
124
+
125
+ ### Example
126
+
127
+ ```
128
+ const results = window.sorla.db.updatedTags.find({
129
+ $or: [
130
+ {name: 'Anderson'},
131
+ {age: 53}
132
+ ]
133
+ });
134
+ ```
135
+
136
+ ### $not
137
+ The **$not** operator processes the queries and invert the bool result, for example if {age: 36} return true for Anderson and false for Sonia then Sonia will be the one who will be returned,
138
+ remember about invert the boolean result? Anderson that is true becomes false and Sonia that is false becomes true.
139
+
140
+ ```
141
+ const results = window.sorla.db.updatedTags.find({
142
+ age: {
143
+ $not: {$eq: 36}
144
+ }
145
+ });
146
+ ```
147
+
148
+ ### $nor
149
+ The **$nor** operator returns the documents that match none of the queries in the array.
150
+
151
+ ```
152
+ const results = window.sorla.db.updatedTags.find({
153
+ $nor: [
154
+ {name: 'Anderson'},
155
+ {age: {$lt: 18}}
156
+ ]
157
+ });
158
+ ```
159
+
160
+ ### Nesting logical operators
161
+ **$and**, **$or** and **$nor** can be nested at any depth, and every query inside them can mix
162
+ fields and other logical operators. This returns everyone aged 30 or more whose name is Sonia
163
+ or who lives in A or B:
164
+
165
+ ```
166
+ const results = window.sorla.db.updatedTags.find({
167
+ $and: [
168
+ {age: {$gte: 30}},
169
+ {$or: [
170
+ {name: 'Sonia'},
171
+ {city: {$in: ['A', 'B']}}
172
+ ]}
173
+ ]
174
+ });
175
+ ```
176
+
177
+ ### More than one operator per field
178
+ A field condition can hold several operators and the document must satisfy all of them,
179
+ at the top level or inside any logical operator:
180
+
181
+ ```
182
+ const results = window.sorla.db.updatedTags.find({
183
+ $or: [
184
+ {age: {$gte: 30, $lte: 40}},
185
+ {name: 'Anderson'}
186
+ ]
187
+ });
188
+ ```
189
+
190
+ **$not** inverts the whole condition it wraps: `{age: {$not: {$gte: 30, $lte: 40}}}` returns
191
+ the documents whose age is *not* between 30 and 40. It must be used inside a field condition.
192
+
193
+ ### Missing fields and null
194
+ A document without the queried field does not match `$eq`, `$gt`, `$in`... but does match
195
+ `$ne` and `$nin`, like MongoDB. `null` can be used as a value: `{name: null}`.
196
+
197
+ Unknown operators and logical operators that do not receive an array throw an error.
198
+
199
+ ## Comparison operators
200
+
201
+ ### $eq
202
+ $eq get all the documents that has exactly the same value as requested.
203
+ For example if we ask age: { $eq: 53 }, will return one document in this example,
204
+ but in general pourpose will return all documents that age is equal than 53.
205
+
206
+ ```
207
+ const results = window.sorla.db.updatedTags.find({
208
+ age: { $eq: 36 }
209
+ });
210
+ ```
211
+
212
+ ### $ne
213
+ $ne or not equal operator search all lines that doens't have the inputed value.
214
+ For example $ne:36, will return 1 line in this example, but for general pourpose will
215
+ return all lines that age isn't 36.
216
+
217
+ ```
218
+ const results = window.sorla.db.updatedTags.find({
219
+ age: { $ne: 36 }
220
+ });
221
+ ```
222
+
223
+ ### $gt
224
+ $gt compares values that is greater than, for example everyone who have age above 20, all the documents will return in this example.
225
+ ```
226
+ const results = window.sorla.db.updatedTags.find({
227
+ age: { $gt: 36 }
228
+ });
229
+ ```
230
+
231
+ ### $gte
232
+ $gte compares values that is greater or equal than, for example everyone who have age above 20, all the documents will return in this example.
233
+ But if we put there for example greater or equal than 53, will return only one document.
234
+ ```
235
+ const results = window.sorla.db.updatedTags.find({
236
+ age: { $gte: 36 }
237
+ });
238
+ ```
239
+
240
+ ### $lt
241
+ $lt compares values that is smaller than, for example everyone who have age smaller than 30, in the documents in this example will return no one,
242
+ because no one has age smaller than 30.
243
+ ```
244
+ const results = window.sorla.db.updatedTags.find({
245
+ age: { $lt: 30 }
246
+ });
247
+ ```
248
+
249
+ ### $lte
250
+ $lte compares values that is smaller or equal than, for example veryone who have age smaller then 36, in the documents in this example will return one,
251
+ because Anderson has age of 36 that is equal 36, remember smaller or equal.
252
+ ```
253
+ const results = window.sorla.db.updatedTags.find({
254
+ age: { $lte: 36 }
255
+ });
256
+ ```
257
+
258
+ ### $in
259
+ $in operator compares if any value inside an array exists, like in operator in SQL, i will add a exemplo here that will return only one document.
260
+ Will return Anderson because age 36 exists is in array object.
261
+ ```
262
+ const results = window.sorla.db.updatedTags.find({
263
+ age: { $in: [36, 44]}
264
+ });
265
+ ```
266
+
267
+ ### $nin
268
+ $nin operator is opposite of $in operator, returns only documents that the values doesn't match with any of the query.
269
+ This will result in a total of 0 documents.
270
+ ```
271
+ const results = window.sorla.db.updatedTags.find({
272
+ age: { $nin: [36, 53]}
273
+ });
274
+ ```