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.
@@ -1,196 +0,0 @@
1
- <!DOCTYPE html>
2
- <html>
3
- <head>
4
- <meta charset="UTF-8">
5
- <title>Simple Data Example</title>
6
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
7
- </head>
8
- <body>
9
- <p><b>Total Items:</b> <span id="total-items"></span></p><hr>
10
-
11
- <h3>Add you data:</h3>
12
- <div>
13
- <label>Name</label>
14
- <input type="text" id="name" placeholder="Name">
15
- </div>
16
- <div>
17
- <label>Inscription</label>
18
- <input type="number" step="1" min="1" id="inscription" placeholder="Inscription">
19
- </div>
20
- <div>
21
- <label>Age</label>
22
- <input type="number" step="1" min="1" id="age" placeholder="Age">
23
- </div>
24
- <div>
25
- <label>Father</label>
26
- <input type="text" id="father" placeholder="Father">
27
- </div>
28
- <div>
29
- <label>Mother</label>
30
- <input type="text" id="mother" placeholder="Mother">
31
- </div>
32
- <button type="button" id="add-data">Add data</button>
33
- <hr>
34
-
35
- <h3>Seach data</h3>
36
- <div>
37
- <label>Search by</label>
38
- <input type="text" id="search-name" placeholder="Search by">
39
- <select id="search-name-type">
40
- <option value="name">Name</option>
41
- <option value="inscription">Inscription</option>
42
- <option value="age">Age</option>
43
- <option value="parents.father">Father</option>
44
- <option value="parents.mother">Mother</option>
45
- </select>
46
- <button type="button" id="search-by-btn">Search</button>
47
- </div>
48
-
49
- <div>
50
- <textarea id="search-result" cols="30" rows="10"></textarea>
51
- </div>
52
-
53
- <script src="../dist/sorla.min.js"></script>
54
-
55
- <script>
56
- function generateUUID() {
57
- return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
58
- var r = Math.random() * 16 | 0,
59
- v = c == 'x' ? r : (r & 0x3 | 0x8);
60
- return v.toString(16);
61
- });
62
- }
63
-
64
- addItem = () => {
65
- const name = document.getElementById('name').value;
66
- const inscription = document.getElementById('inscription').value;
67
- const age = document.getElementById('age').value;
68
- const father = document.getElementById('father').value;
69
- const mother = document.getElementById('mother').value;
70
-
71
- s.db.simple.insertOne({
72
- 'name': name,
73
- 'inscription': inscription,
74
- 'age': age,
75
- 'parents': {
76
- 'father': father,
77
- 'mother': mother
78
- }
79
- });
80
-
81
- document.getElementById('total-items').innerText = s.db.simple.count();
82
- }
83
-
84
- searchData = () => {
85
- const searchName = document.getElementById('search-name').value;
86
- const searchNameType = document.getElementById('search-name-type').value;
87
-
88
- const result = s.db.simple.findOne({
89
- [searchNameType]: searchName
90
- });
91
-
92
- console.log(result);
93
-
94
- document.getElementById('search-result').value = JSON.stringify(result, null, 4);
95
- }
96
-
97
- document.addEventListener('DOMContentLoaded', () => {
98
- window.s = new sorla();
99
- s.useDb('simple_database');
100
- s.db.createCollection('simple');
101
- const uuid_fix = generateUUID();
102
-
103
- //creating data
104
- s.db.simple.insertMany([
105
- {
106
- 'name': 'John Doe',
107
- 'inscription': 123,
108
- 'age': 25,
109
- 'parents': {
110
- 'father': 'John Doe Sr.',
111
- 'mother': 'Jane Doe'
112
- },
113
- 'uuid': generateUUID()
114
- },
115
-
116
- {
117
- "name": "Elizabeth Davis",
118
- "inscription": 618,
119
- "age": 36,
120
- "parents": {
121
- "father": "Elizabeth Brown",
122
- "mother": "Elizabeth Brown"
123
- },
124
- 'uuid': generateUUID()
125
- },
126
-
127
-
128
- {
129
- "name": "John Davis",
130
- "inscription": 817,
131
- "age": 54,
132
- "parents": {
133
- "father": "John Garcia",
134
- "mother": "Patricia Garcia"
135
- },
136
- 'uuid': uuid_fix
137
- },
138
-
139
- {
140
- "name": "Jennifer Miller",
141
- "inscription": 699,
142
- "age": 45,
143
- "parents": {
144
- "father": "Linda Brown",
145
- "mother": "Elizabeth Brown"
146
- },
147
- 'uuid': generateUUID()
148
- },
149
-
150
- {
151
- "name": "Patricia Johnson",
152
- "inscription": 906,
153
- "age": 49,
154
- "parents": {
155
- "father": "Robert Johnson",
156
- "mother": "Elizabeth Johnson"
157
- },
158
- 'uuid': generateUUID()
159
- },
160
-
161
- {
162
- "name": "Michael Johnson",
163
- "inscription": 810,
164
- "age": 20,
165
- "parents": {
166
- "father": "Jennifer Brown",
167
- "mother": "Jennifer Brown"
168
- },
169
- 'uuid': generateUUID()
170
- }
171
- ]);
172
-
173
- document.getElementById('total-items').innerText = s.db.simple.count();
174
- document.getElementById('add-data').addEventListener('click', addItem);
175
- document.getElementById('search-by-btn').addEventListener('click', searchData);
176
-
177
- //operators test
178
- //testing search and
179
- /*const result = s.db.simple.find({'$and': [{'name': 'John Doe'}, {'age': 25}]}, {_id: 0}); //should return 1
180
- console.log('and', result);
181
-
182
- //testing search or
183
- const result2 = s.db.simple.find({'$or': [{'name': 'John Doe'}, {'parents.mother': 'Elizabeth Johnson'}]}); //should return 2
184
- console.log('or', result2);
185
-
186
- //testing search uuid
187
- const result3 = s.db.simple.findOne({'uuid': uuid_fix});
188
- console.log('uuid', uuid_fix, result3);*/
189
-
190
- //search uuid with not
191
- const result4 = s.db.simple.find({'uuid': {'$not': uuid_fix}});
192
- console.log('uuid not', uuid_fix, result4);
193
- });
194
- </script>
195
- </body>
196
- </html>
package/index.js DELETED
@@ -1,62 +0,0 @@
1
-
2
- const fs = require('fs-extra');
3
- const browserify = require('browserify');
4
- const path = require('path');
5
- const terser = require('terser');
6
-
7
- const srcDir = './src';
8
- const distDir = './dist';
9
- const outputFile = `${distDir}/sorla.min.js`;
10
-
11
- function findJsFiles(dir, fileList = [])
12
- {
13
- const files = fs.readdirSync(dir);
14
- files.forEach(file => {
15
- const filePath = path.join(dir, file);
16
- const stat = fs.statSync(filePath);
17
- if (stat.isDirectory())
18
- {
19
- findJsFiles(filePath, fileList);
20
- }
21
- else if (path.extname(filePath) === '.js')
22
- {
23
- fileList.push(filePath);
24
- }
25
- });
26
- return fileList;
27
- }
28
-
29
- function buildForBrowser(entryFile, outputFile) {
30
- const b = browserify();
31
- b.add(entryFile);
32
- b.bundle(async (err, buf) => {
33
- if (err) {
34
- console.error(err);
35
- return;
36
- }
37
-
38
- const code = buf.toString();
39
- try {
40
- const minified = await terser.minify(code, {
41
- compress: true,
42
- mangle: {
43
- keep_classnames: true,
44
- keep_fnames: true
45
- }
46
- });
47
-
48
- if (minified.error) {
49
- console.error(minified.error);
50
- return;
51
- }
52
-
53
- fs.outputFileSync(outputFile, code);
54
- fs.outputFileSync(outputFile, minified.code);
55
- } catch (minifyErr) {
56
- console.error(minifyErr);
57
- }
58
- });
59
- }
60
-
61
- const allJsFiles = findJsFiles(srcDir);
62
- buildForBrowser(allJsFiles, outputFile);
package/src/helper.js DELETED
@@ -1,41 +0,0 @@
1
- //this file works only in dev mode, and is used to help with the development of the app
2
- //and to debug bad behaviors and mistakes
3
-
4
- const faker = require('faker');
5
- const sorla = require('./sorla.js');
6
-
7
- const createFakeCollection = (srla, collectionName, number_objects) => {
8
- if(!(srla instanceof sorla))
9
- throw new Error('srla must be an instance of sorla');
10
-
11
- const objects = [];
12
-
13
- for(let i=0; i<number_objects; i++)
14
- {
15
- const obj = {
16
- name: faker.random.word(),
17
- email: faker.internet.email(),
18
- age: faker.datatype.number(),
19
- uuid: faker.datatype.uuid()
20
- };
21
- srla.db[collectionName].insertOne(structuredClone(obj));
22
- objects.push(obj);
23
- }
24
-
25
- return objects;
26
- }
27
-
28
- const getRandomComparisonData = (objects) => {
29
- const itemIndex1 = Math.floor(Math.random() * objects.length);
30
- let itemIndex2 = null;
31
- do {
32
- itemIndex2 = Math.floor(Math.random() * objects.length);
33
- } while(itemIndex1 === itemIndex2);
34
-
35
- return [objects[itemIndex1], objects[itemIndex2]];
36
- }
37
-
38
- module.exports = {
39
- createFakeCollection,
40
- getRandomComparisonData
41
- }
@@ -1,73 +0,0 @@
1
- const chai = require('chai')
2
- const expect = chai.expect
3
- const faker = require('faker')
4
- const sorla = require('../src/sorla.js')
5
-
6
- describe('SORLA Collection Feature Test', () => {
7
- beforeAll(() => {
8
- this.srla = new sorla();
9
- this.dbname = faker.random.word();
10
- this.srla.createDb(this.dbname);
11
- this.srla.useDb(this.dbname);
12
- });
13
-
14
- beforeEach(() => {
15
- this.collectionName = faker.random.word();
16
- this.srla.db.createCollection(this.collectionName);
17
- });
18
-
19
- it('Create a single document inside of a collection and check by length', () => {
20
- this.srla.db[this.collectionName].insertOne({name: 'Anderson'});
21
- expect(this.srla.db[this.collectionName].count()).to.equal(1);
22
- });
23
-
24
- it('create multiple documents and check by length', () => {
25
- this.srla.db[this.collectionName].insertMany([
26
- {name: 'Anderson'},
27
- {name: 'Arruda'},
28
- {name: 'Sorla'}
29
- ]);
30
- expect(this.srla.db[this.collectionName].count()).to.equal(3);
31
- });
32
-
33
- it('create a document with prohibited character $', () => {
34
- expect(() => this.srla.db[this.collectionName].insertOne({name: 'Anderson', $test: '123'})).to.throw();
35
- });
36
-
37
- it('create a document with prohibited character .', () => {
38
- expect(() => this.srla.db[this.collectionName].insertOne({"name.name": 'Anderson', test: {test: '123'}})).to.throw();
39
- });
40
-
41
- it('create a document with prohibited character . inside of an array', () => {
42
- expect(() => this.srla.db[this.collectionName].insertOne({name: 'Anderson', test: [{"test.number": '123'}, {"test.number": '123'}]})).to.throw();
43
- });
44
-
45
- it('create a document with prohibited character . inside of an object', () => {
46
- expect(() => this.srla.db[this.collectionName].insertOne({name: 'Anderson', test: {"test.number": '123'}})).to.throw();
47
- });
48
-
49
- /*it('get a document by his subfield using test.inscription', () => {
50
- this.srla.db[this.collectionName].insertMany([
51
- {name: 'Anderson', test: {inscription: 123}},
52
- {name: 'Arruda', test: {inscription: 321}},
53
- {name: 'Sorla', test: {inscription: 213}}
54
- ]);
55
- expect(this.srla.db[this.collectionName].findOne({'test.inscription': 123}).length).to.equal(1);
56
- });
57
-
58
- it('Try to create a document without beeing a valid object', () => {
59
- expect(() => this.srla.db[this.collectionName].insertOne('Anderson')).to.throw();
60
- });
61
-
62
- it('Try to create a document with array', () => {
63
- expect(() => this.srla.db[this.collectionName].insertOne(['Anderson'])).to.throw();
64
- });
65
-
66
- it('Try to create many documents without beeing a valid array', () => {
67
- expect(() => this.srla.db[this.collectionName].insertMany('Anderson')).to.throw();
68
- });
69
-
70
- it('Try to create many documents without having valid object', () => {
71
- expect(() => this.srla.db[this.collectionName].insertMany([{test: '123', }, 'anderson'])).to.throw();
72
- });*/
73
- });
@@ -1,269 +0,0 @@
1
- const chai = require('chai')
2
- const expect = chai.expect
3
- const faker = require('faker')
4
- const sorla = require('../src/sorla.js')
5
- const helper = require('../src/helper.js')
6
-
7
- describe('SORLA Collection Queries Feature Test', () => {
8
- beforeAll(() => {
9
- this.srla = new sorla();
10
- this.dbname = faker.random.word();
11
- this.srla.createDb(this.dbname);
12
- this.srla.useDb(this.dbname);
13
- this.collectionName = faker.random.word();
14
- this.srla.db.createCollection(this.collectionName);
15
- this.objects = helper.createFakeCollection(this.srla, this.collectionName, 30);
16
- });
17
-
18
- it('Test if the first document in the collection is the same first document on the objects array', () => {
19
- const firstDoc = this.srla.db[this.collectionName].findOne({}, { _id: 0 });
20
- const firstObj = this.objects[0];
21
- expect(firstDoc).to.deep.equal(firstObj);
22
- });
23
-
24
- it('Takes the data field name from the document inside the collection', () => {
25
- const firstDoc = this.srla.db[this.collectionName].findOne({}, { _id: 0 });
26
- const firstObj = this.objects[0];
27
- expect(firstDoc.name).to.be.a('string').equal(firstObj.name);
28
- });
29
-
30
- it('Takes the data field email from the document inside the collection', () => {
31
- const firstDoc = this.srla.db[this.collectionName].findOne({}, { _id: 0 });
32
- const firstObj = this.objects[0];
33
- expect(firstDoc.email).to.be.a('string').equal(firstObj.email);
34
- });
35
-
36
- it('Takes the data field age from the document inside the collection', () => {
37
- const firstDoc = this.srla.db[this.collectionName].findOne({}, { _id: 0 });
38
- const firstObj = this.objects[0];
39
- expect(firstDoc.age).to.be.a('number').equal(firstObj.age);
40
- });
41
-
42
- it('Creates a few documents and search for one of them', () => {
43
- const [item1,] = helper.getRandomComparisonData(this.objects);
44
- const doc = this.srla.db[this.collectionName].findOne({ uuid: item1.uuid }, { _id: 0 });
45
- expect(doc).to.deep.equal(item1);
46
- });
47
-
48
- it('Creates a few documents and search for some of them', () => {
49
- const [item1, item2] = helper.getRandomComparisonData(this.objects);
50
- const docs = this.srla.db[this.collectionName].find({
51
- $or: [
52
- { uuid: item1.uuid },
53
- { uuid: item2.uuid }
54
- ]
55
- });
56
-
57
- expect(docs.length).to.be.equal(2);
58
- });
59
-
60
- it('Creates a few documents and search using not', () => {
61
- const [item1] = helper.getRandomComparisonData(this.objects);
62
- const docs = this.srla.db[this.collectionName].find(
63
- {
64
- uuid: {
65
- $not: {
66
- $eq: item1.uuid
67
- }
68
- }
69
- }
70
- );
71
-
72
- expect(docs.length).to.be.equal(this.objects.length - 1);
73
- });
74
-
75
- it('Creates a few documents and search using not with more than one expression', () => {
76
- const [item1,] = helper.getRandomComparisonData(this.objects);
77
- const docs = this.srla.db[this.collectionName].find(
78
- {
79
- age: {
80
- $not: {
81
- $eq: item1.age,
82
- $gt: 18
83
- }
84
- }
85
- }
86
- );
87
-
88
- expect(docs.length).to.be.equal(this.objects.length - 1);
89
- });
90
-
91
- it('Creates a few documents and search it using nor', () => {
92
- const [item1,] = helper.getRandomComparisonData(this.objects);
93
- const docs = this.srla.db[this.collectionName].findOne({
94
- $nor: [
95
- {name: item1.name},
96
- {age: item1.age}
97
- ]
98
- });
99
-
100
- expect(docs.name).to.not.be.equal(item1.name);
101
- expect(docs.age).to.not.be.equal(item1.age);
102
- });
103
-
104
- it('Creates a few documents and search it using nor with more than one expression', () => {
105
- const [item1, item2] = helper.getRandomComparisonData(this.objects);
106
- const docs = this.srla.db[this.collectionName].find({
107
- $nor: [
108
- {name: item1.name},
109
- {age: item1.age},
110
- {name: item2.name},
111
- {age: item2.age}
112
- ]
113
- });
114
-
115
- expect(docs.length).to.be.equal(this.objects.length - 2);
116
- });
117
-
118
- it('Test sort number in ascending order', () => {
119
- this.srla.db.createCollection('sortNumberAsc');
120
- this.srla.db.sortNumberAsc.insertMany([
121
- {
122
- name: 'A',
123
- age: 20
124
- },
125
- {
126
- name: 'B',
127
- age: 10
128
- },
129
- {
130
- name: 'C',
131
- age: 30
132
- },
133
- {
134
- name: 'A',
135
- age: 3
136
- },
137
- {
138
- name: 'B',
139
- age: 5
140
- },
141
- {
142
- name: 'C',
143
- age: 1
144
- },
145
- ]);
146
- });
147
-
148
- it('Test sort number in descending order', () => {
149
- });
150
-
151
- it('Test sort string in ascending order', () => {
152
- });
153
-
154
- it('Test sort string in descending order', () => {
155
- });
156
-
157
- it('Test sort date in ascending order', () => {
158
- });
159
-
160
- it('Test sort date in descending order', () => {
161
- });
162
-
163
- it('Test sort boolean in ascending order', () => {
164
- });
165
-
166
- it('Test sort boolean in descending order', () => {
167
- });
168
-
169
- it('Test equal comparison', () => {
170
- const [item1,] = helper.getRandomComparisonData(this.objects);
171
- const doc = this.srla.db[this.collectionName].findOne({ uuid: item1.uuid }, { _id: 0 });
172
- expect(doc).to.deep.equal(item1);
173
- });
174
-
175
- it('Test not equal comparison', () => {
176
- const [item1,] = helper.getRandomComparisonData(this.objects);
177
- const docs = this.srla.db[this.collectionName].find({
178
- uuid: { $ne: item1.uuid }
179
- });
180
-
181
- expect(docs.length).to.be.equal(this.objects.length - 1);
182
- });
183
-
184
- it('Test greater than comparison', () => {
185
- const [item1,] = helper.getRandomComparisonData(this.objects);
186
- const docs = this.srla.db[this.collectionName].find({
187
- age: { $gt: item1.age }
188
- });
189
-
190
- countAges = this.objects.filter(obj => obj.age > item1.age).length;
191
- expect(docs.length).to.be.equal(countAges);
192
- });
193
-
194
- it('Test greater than or equal comparison', () => {
195
- const [item1,] = helper.getRandomComparisonData(this.objects);
196
- const docs = this.srla.db[this.collectionName].find({
197
- age: { $gte: item1.age }
198
- });
199
-
200
- countAges = this.objects.filter(obj => obj.age >= item1.age).length;
201
- expect(docs.length).to.be.equal(countAges);
202
- });
203
-
204
- it('Test less than comparison', () => {
205
- const [item1,] = helper.getRandomComparisonData(this.objects);
206
- const docs = this.srla.db[this.collectionName].find({
207
- age: { $lt: item1.age }
208
- });
209
-
210
- countAges = this.objects.filter(obj => obj.age < item1.age).length;
211
- expect(docs.length).to.be.equal(countAges);
212
- });
213
-
214
- it('Test less than or equal comparison', () => {
215
- const [item1,] = helper.getRandomComparisonData(this.objects);
216
- const docs = this.srla.db[this.collectionName].find({
217
- age: { $lte: item1.age }
218
- });
219
-
220
- countAges = this.objects.filter(obj => obj.age <= item1.age).length;
221
- expect(docs.length).to.be.equal(countAges);
222
- });
223
-
224
- it('Test in comparison', () => {
225
- const [item1, item2] = helper.getRandomComparisonData(this.objects);
226
- const docs = this.srla.db[this.collectionName].find({
227
- age: { $in: [item1.age, item2.age] }
228
- });
229
-
230
- countAges = this.objects.filter(obj => obj.age === item1.age || obj.age === item2.age).length;
231
- expect(docs.length).to.be.equal(countAges);
232
- });
233
-
234
- it('Test not in comparison', () => {
235
- const [item1, item2] = helper.getRandomComparisonData(this.objects);
236
- const docs = this.srla.db[this.collectionName].find({
237
- age: { $nin: [item1.age, item2.age] }
238
- });
239
-
240
- countAges = this.objects.filter(obj => obj.age !== item1.age && obj.age !== item2.age).length;
241
- expect(docs.length).to.be.equal(countAges);
242
- });
243
-
244
- it('Test mixed comparison with and', () => {
245
- const [item1,] = helper.getRandomComparisonData(this.objects);
246
- const docs = this.srla.db[this.collectionName].find({
247
- $and: [
248
- { name: item1.name },
249
- { age: item1.age }
250
- ]
251
- });
252
-
253
- const filtered = this.objects.filter(obj => obj.name === item1.name && obj.age === item1.age);
254
- expect(docs.length).to.be.equal(filtered.length);
255
- });
256
-
257
- it('Test mixed comparison with or', () => {
258
- const [item1, item2] = helper.getRandomComparisonData(this.objects);
259
- const docs = this.srla.db[this.collectionName].find({
260
- $or: [
261
- { name: item1.name },
262
- { age: item2.age }
263
- ]
264
- });
265
-
266
- const filtered = this.objects.filter(obj => obj.name === item1.name || obj.age === item2.age);
267
- expect(docs.length).to.be.equal(filtered.length);
268
- });
269
- });
@@ -1,36 +0,0 @@
1
- const chai = require('chai')
2
- const expect = chai.expect
3
- const faker = require('faker')
4
- const sorla = require('../src/sorla.js')
5
- const collection = require('../src/collections/collection.js');
6
-
7
- describe('SORLA Database Unit Test', () => {
8
- beforeAll(() => {
9
- this.srla = new sorla();
10
- this.dbname = faker.random.word();
11
- this.srla.createDb(this.dbname);
12
- this.srla.useDb(this.dbname);
13
- });
14
-
15
- it('Should create a new collection with specified name', () => {
16
- this.srla.db.createCollection('collection1');
17
- expect(this.srla.db.countCollections()).to.equal(1);
18
- expect(this.srla.db.collection1).to.not.be.undefined;
19
- expect(this.srla.db.collection1).to.be.an.instanceof(collection);
20
- });
21
-
22
- it('Should create a new collection with specified name, second collection', () => {
23
- this.srla.db.createCollection('collection2');
24
- expect(this.srla.db.countCollections()).to.equal(2);
25
- expect(this.srla.db.collection2).to.not.be.undefined;
26
- expect(this.srla.db.collection2).to.be.an.instanceof(collection);
27
- });
28
-
29
- it('Should return a collection undefined when not exists', () => {
30
- expect(this.srla.db.collection3).to.be.undefined;
31
- });
32
-
33
- it('Should avoid create a collection with name that exists in database class', () => {
34
- expect(() => this.srla.db.createCollection('_name')).to.throw();
35
- });
36
- });