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,104 +1,74 @@
1
- const manipulation = require('./manipulation.js');
2
- const operators = require('./operators.js');
3
- const cursor = require('../documents/cursor.js');
4
-
5
- class query extends manipulation
6
- {
7
- constructor(validationSchema)
8
- {
9
- super(validationSchema);
10
- this._operators = new operators();
11
- }
12
-
13
- /**
14
- * Count documents
15
- * @return int
16
- */
17
- count()
18
- {
19
- return this._documents.length;
20
- }
21
-
22
- /**
23
- * Deeps find in object
24
- * @param object query
25
- * @param object projection
26
- * @param bool firstOne
27
- * @return array
28
- */
29
- _deepFind(query, projection, single=false)
30
- {
31
- const c = new cursor();
32
- const find = (documents, query, single, debug=false) => {
33
- let finded = [];
34
- const queryKeys = Object.keys(query);
35
- docsLoop: for(let doc of documents)
36
- {
37
- for(let k of queryKeys)
38
- {
39
- if(!this._operators.isAnyOperator(k))
40
- {
41
- this._operators.currentField = k;
42
-
43
- if(typeof query[k] === 'object')
44
- {
45
- if(find([doc], query[k], single).length === 0)
46
- {
47
- continue docsLoop;
48
- }
49
-
50
- continue;
51
- }
52
- }
53
-
54
- const isLogicalOperator = this._operators.isLogicalOperator(k);
55
- if((isLogicalOperator && !this._operators[k](query[k], doc)) || (!isLogicalOperator && !this._operators.handleComparison(k, query[k], doc)))
56
- {
57
- continue docsLoop;
58
- }
59
- }
60
-
61
- finded.push(doc.clone());
62
- if(single) break;
63
- }
64
-
65
- return finded;
66
- };
67
-
68
- const result = find(this._documents, query, single);
69
- const finded = new cursor(result, projection);
70
-
71
- return finded;
72
- }
73
-
74
- /**
75
- * Find one document
76
- * @param query
77
- * @param projection
78
- * @return object
79
- */
80
- findOne(query, projection = null)
81
- {
82
- const finded = this._deepFind(query, projection, true);
83
- return finded.first();
84
- }
85
-
86
- /**
87
- * Find documents
88
- * @param query
89
- * @param projection
90
- * @return array
91
- */
92
- find(query, projection = null)
93
- {
94
- const finded = this._deepFind(query, projection);
95
- return finded;
96
- }
97
-
98
- //find
99
- //find one and delete
100
- //find one and update
101
- //find one and replace
102
- }
103
-
1
+ const manipulation = require('./manipulation.js');
2
+ const operators = require('./operators.js');
3
+ const cursor = require('../documents/cursor.js');
4
+
5
+ class query extends manipulation
6
+ {
7
+ constructor(validationSchema)
8
+ {
9
+ super(validationSchema);
10
+ this._operators = new operators();
11
+ }
12
+
13
+ /**
14
+ * Count documents
15
+ * @return int
16
+ */
17
+ count()
18
+ {
19
+ return this._documents.length;
20
+ }
21
+
22
+ /**
23
+ * Deeps find in object
24
+ * @param object query
25
+ * @param object projection
26
+ * @param bool firstOne
27
+ * @return array
28
+ */
29
+ _deepFind(query, projection, single=false)
30
+ {
31
+ const finded = [];
32
+ for(const doc of this._documents)
33
+ {
34
+ if(!this._operators.matches(doc, query))
35
+ continue;
36
+
37
+ finded.push(doc.clone());
38
+ if(single) break;
39
+ }
40
+
41
+ return new cursor(finded, projection);
42
+ }
43
+
44
+ /**
45
+ * Find one document
46
+ * @param query
47
+ * @param projection
48
+ * @return object
49
+ */
50
+ findOne(query, projection = null)
51
+ {
52
+ const finded = this._deepFind(query, projection, true);
53
+ return finded.first();
54
+ }
55
+
56
+ /**
57
+ * Find documents
58
+ * @param query
59
+ * @param projection
60
+ * @return array
61
+ */
62
+ find(query, projection = null)
63
+ {
64
+ const finded = this._deepFind(query, projection);
65
+ return finded;
66
+ }
67
+
68
+ //find
69
+ //find one and delete
70
+ //find one and update
71
+ //find one and replace
72
+ }
73
+
104
74
  module.exports = query;
package/src/sorla.js CHANGED
@@ -1,123 +1,123 @@
1
- const database = require('./dbs/database.js');
2
-
3
- class sorla{
4
- /**
5
- * Constructs class Sorla to manage databases
6
- * @author Anderson Arruda < andmarruda@gmail.com >
7
- * @version 1.0.0
8
- * @param
9
- * @return void
10
- */
11
- constructor()
12
- {
13
- this._dbs = [];
14
- this._selectedDb = null;
15
- }
16
-
17
- /**
18
- * Search a database`s position in the list of databases by its name and mark it as selected
19
- * @author Anderson Arruda < andmarruda@gmail.com >
20
- * @version
21
- * @param
22
- * @return
23
- **/
24
- _findDbIndex(name)
25
- {
26
- return this._dbs.findIndex(db => db.name == name);
27
- }
28
-
29
- /**
30
- * Verify if a database name already exists in the list of databases
31
- * @author Anderson Arruda < andmarruda@gmail.com >
32
- * @version 1.0.0
33
- * @param string name
34
- * @return boolean
35
- */
36
- dbExists(name)
37
- {
38
- return this._findDbIndex(name) > -1;
39
- }
40
-
41
- /**
42
- * Mark a database as selected
43
- * @author Anderson Arruda < andmarruda@gmail.com >
44
- * @version 1.0.0
45
- * @param
46
- * @return
47
- **/
48
- _selectDb(pos)
49
- {
50
- if(!this._dbs.hasOwnProperty(pos))
51
- {
52
- throw new Error('Database does not exists');
53
- }
54
- this._selectedDb = this._dbs[pos];
55
- }
56
-
57
- /**
58
- * Creates a new database
59
- * @author Anderson Arruda < andmarruda@gmail.com >
60
- * @version 1.0.0
61
- * @param string name
62
- * @return int "The position of the new database in the list of databases"
63
- **/
64
- createDb(name)
65
- {
66
- if(this.dbExists(name))
67
- {
68
- return false;
69
- }
70
-
71
- let pos = this._dbs.length;
72
- this._dbs.push(new database(name));
73
- return pos;
74
- }
75
-
76
- /**
77
- * Selects a database by its name or creates a new one if it does not exists and then selects it
78
- * @author Anderson Arruda < andmarruda@gmail.com >
79
- * @version 1.0.0
80
- * @param string name
81
- * @return void
82
- **/
83
- useDb(name)
84
- {
85
- let pos = null;
86
- if(!this.dbExists(name))
87
- {
88
- pos = this.createDb(name);
89
- }
90
-
91
- this._selectDb(pos === null ? this._findDbIndex(name) : pos);
92
- }
93
-
94
- /**
95
- * Get the number of created databases
96
- * @author Anderson Arruda < andmarruda@gmail.com >
97
- * @version 1.0.0
98
- * @param
99
- * @return int
100
- **/
101
- countDb()
102
- {
103
- return this._dbs.length;
104
- }
105
-
106
- /**
107
- * Return selected db
108
- * @author Anderson Arruda < anderson@sysborg.com.br >
109
- * @version 1.0.0
110
- * @param
111
- * @return instanceof database
112
- **/
113
- get db()
114
- {
115
- return this._selectedDb;
116
- }
117
- }
118
-
119
- module.exports = sorla;
120
- if(typeof window !== 'undefined')
121
- {
122
- window.sorla = sorla;
1
+ const database = require('./dbs/database.js');
2
+
3
+ class sorla{
4
+ /**
5
+ * Constructs class Sorla to manage databases
6
+ * @author Anderson Arruda < andmarruda@gmail.com >
7
+ * @version 1.0.0
8
+ * @param
9
+ * @return void
10
+ */
11
+ constructor()
12
+ {
13
+ this._dbs = [];
14
+ this._selectedDb = null;
15
+ }
16
+
17
+ /**
18
+ * Search a database`s position in the list of databases by its name and mark it as selected
19
+ * @author Anderson Arruda < andmarruda@gmail.com >
20
+ * @version
21
+ * @param
22
+ * @return
23
+ **/
24
+ _findDbIndex(name)
25
+ {
26
+ return this._dbs.findIndex(db => db.name == name);
27
+ }
28
+
29
+ /**
30
+ * Verify if a database name already exists in the list of databases
31
+ * @author Anderson Arruda < andmarruda@gmail.com >
32
+ * @version 1.0.0
33
+ * @param string name
34
+ * @return boolean
35
+ */
36
+ dbExists(name)
37
+ {
38
+ return this._findDbIndex(name) > -1;
39
+ }
40
+
41
+ /**
42
+ * Mark a database as selected
43
+ * @author Anderson Arruda < andmarruda@gmail.com >
44
+ * @version 1.0.0
45
+ * @param
46
+ * @return
47
+ **/
48
+ _selectDb(pos)
49
+ {
50
+ if(!this._dbs.hasOwnProperty(pos))
51
+ {
52
+ throw new Error('Database does not exists');
53
+ }
54
+ this._selectedDb = this._dbs[pos];
55
+ }
56
+
57
+ /**
58
+ * Creates a new database
59
+ * @author Anderson Arruda < andmarruda@gmail.com >
60
+ * @version 1.0.0
61
+ * @param string name
62
+ * @return int "The position of the new database in the list of databases"
63
+ **/
64
+ createDb(name)
65
+ {
66
+ if(this.dbExists(name))
67
+ {
68
+ return false;
69
+ }
70
+
71
+ let pos = this._dbs.length;
72
+ this._dbs.push(new database(name));
73
+ return pos;
74
+ }
75
+
76
+ /**
77
+ * Selects a database by its name or creates a new one if it does not exists and then selects it
78
+ * @author Anderson Arruda < andmarruda@gmail.com >
79
+ * @version 1.0.0
80
+ * @param string name
81
+ * @return void
82
+ **/
83
+ useDb(name)
84
+ {
85
+ let pos = null;
86
+ if(!this.dbExists(name))
87
+ {
88
+ pos = this.createDb(name);
89
+ }
90
+
91
+ this._selectDb(pos === null ? this._findDbIndex(name) : pos);
92
+ }
93
+
94
+ /**
95
+ * Get the number of created databases
96
+ * @author Anderson Arruda < andmarruda@gmail.com >
97
+ * @version 1.0.0
98
+ * @param
99
+ * @return int
100
+ **/
101
+ countDb()
102
+ {
103
+ return this._dbs.length;
104
+ }
105
+
106
+ /**
107
+ * Return selected db
108
+ * @author Anderson Arruda < anderson@sysborg.com.br >
109
+ * @version 1.0.0
110
+ * @param
111
+ * @return instanceof database
112
+ **/
113
+ get db()
114
+ {
115
+ return this._selectedDb;
116
+ }
117
+ }
118
+
119
+ module.exports = sorla;
120
+ if(typeof window !== 'undefined')
121
+ {
122
+ window.sorla = sorla;
123
123
  }
package/src/utils.js CHANGED
@@ -1,41 +1,42 @@
1
- class utils
2
- {
3
- /**
4
- * Get a uuid v4 string
5
- * @author Anderson Arruda < anderson@sysborg.com.br >
6
- * @version 1.0.0
7
- * @param
8
- * @return string
9
- **/
10
- get uuid()
11
- {
12
- return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c)
13
- {
14
- let r = Math.random() * 16 | 0,
15
- v = c == 'x' ? r : (r & 0x3 | 0x8);
16
-
17
- return v.toString(16);
18
- });
19
- }
20
-
21
- /**
22
- * Get data using dots to access subfields
23
- * @param object data
24
- * @param string field
25
- */
26
- getDotData(data, field)
27
- {
28
- let fields = field.split('.');
29
-
30
- fields.forEach(field => {
31
- if(typeof data[field] !== 'undefined')
32
- {
33
- data = data[field];
34
- }
35
- });
36
-
37
- return data;
38
- }
39
- }
40
-
1
+ class utils
2
+ {
3
+ /**
4
+ * Get a uuid v4 string
5
+ * @author Anderson Arruda < anderson@sysborg.com.br >
6
+ * @version 1.0.0
7
+ * @param
8
+ * @return string
9
+ **/
10
+ get uuid()
11
+ {
12
+ return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c)
13
+ {
14
+ let r = Math.random() * 16 | 0,
15
+ v = c == 'x' ? r : (r & 0x3 | 0x8);
16
+
17
+ return v.toString(16);
18
+ });
19
+ }
20
+
21
+ /**
22
+ * Get data using dots to access subfields
23
+ * @param object data
24
+ * @param string field
25
+ */
26
+ getDotData(data, field)
27
+ {
28
+ let value = data;
29
+ for(const part of field.split('.'))
30
+ {
31
+ // a missing step means the field does not exist (it used to return the parent object)
32
+ if(value === null || typeof value !== 'object' || !(part in value))
33
+ return undefined;
34
+
35
+ value = value[part];
36
+ }
37
+
38
+ return value;
39
+ }
40
+ }
41
+
41
42
  module.exports = utils;
package/Brainstorming.md DELETED
@@ -1,22 +0,0 @@
1
- For the first query version needs only to see if has some non logical operator or non logical comparison to resend to find as a recursive method.
2
-
3
- - Preparing sort of data using new capabilities
4
- - improving find behavior
5
-
6
- For comparison operators and deny operator like not
7
- adding nor https://www.mongodb.com/docs/manual/reference/operator/query/nor/
8
-
9
- https://www.mongodb.com/docs/manual/reference/operator/query-comparison/
10
- https://www.mongodb.com/docs/manual/reference/operator/query/expr/
11
- https://www.mongodb.com/docs/manual/reference/operator/query/regex/
12
-
13
-
14
- Preparing a bunch of misc tests to search data, add sort data and create test sortable data.
15
-
16
- //erros and improvements
17
- Bugs appears after resolving not, need to check and test why
18
- npm run test
19
-
20
- needs to improve projection to use cursor
21
-
22
- fix new bugs in test
package/debug-not.js DELETED
@@ -1,24 +0,0 @@
1
- const sorla = require('./src/sorla.js');
2
- const helper = require('./src/helper.js');
3
-
4
- const numberOfObjects = 20;
5
- const sdb = new sorla();
6
- sdb.createDb('test');
7
- sdb.useDb('test');
8
-
9
- sdb.db.createCollection('collection_test');
10
-
11
- const objects = helper.createFakeCollection(sdb, 'collection_test', numberOfObjects);
12
- const [itemIndex1, ] = helper.getRandomComparisonData(objects);
13
-
14
- const finded = sdb.db.collection_test.find({
15
- uuid: {
16
- $not: {
17
- $eq: itemIndex1.uuid
18
- }
19
- }
20
- });
21
-
22
- console.log(finded.length, numberOfObjects);
23
-
24
- //console.log(finded);