sorla-db 1.0.0 → 1.1.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/Brainstorming.md +21 -21
- package/LICENSE +21 -21
- package/README.md +216 -31
- package/debug-not.js +23 -23
- package/dist/sorla.min.js +1 -1
- package/example/simple_data_example.html +195 -195
- package/index.js +61 -61
- package/package.json +42 -42
- package/src/collections/collection.js +46 -46
- package/src/dbs/database.js +71 -71
- package/src/documents/cursor.js +143 -143
- package/src/documents/document.js +87 -87
- package/src/helper.js +40 -40
- package/src/query_language/manipulation.js +160 -160
- package/src/query_language/operators.js +242 -288
- package/src/query_language/query.js +73 -103
- package/src/sorla.js +122 -122
- package/src/utils.js +40 -40
- package/test/feature_collection.test.js +74 -72
- package/test/feature_collection_queries.test.js +223 -268
- package/test/feature_cursor.test.js +84 -0
- package/test/feature_database.test.js +62 -0
- package/test/feature_insert.test.js +116 -0
- package/test/feature_nested_queries.test.js +94 -0
- package/test/feature_query_equivalence.test.js +165 -0
- package/test/unit_database.test.js +35 -35
- package/test/{unit_document_test.js → unit_document.test.js} +38 -38
- package/test/unit_operators.test.js +78 -0
- package/test/unit_sorla.test.js +41 -41
- package/test/unit_test.test.js +24 -24
- package/webpack.config.js +10 -10
|
@@ -1,88 +1,88 @@
|
|
|
1
|
-
const utils = require('../utils');
|
|
2
|
-
const cursor = require('./cursor');
|
|
3
|
-
const manipulation = require('../query_language/manipulation');
|
|
4
|
-
|
|
5
|
-
class documents
|
|
6
|
-
{
|
|
7
|
-
constructor(object)
|
|
8
|
-
{
|
|
9
|
-
this._data = object;
|
|
10
|
-
this._utils = new utils();
|
|
11
|
-
this._cloned = false;
|
|
12
|
-
this._deleteReceiver = null;
|
|
13
|
-
|
|
14
|
-
return new Proxy(this, {
|
|
15
|
-
deleteProperty: (target, prop, receiver) => {
|
|
16
|
-
if(target._cloned && target._deleteReceiver instanceof cursor)
|
|
17
|
-
{
|
|
18
|
-
if(prop in target._data)
|
|
19
|
-
{
|
|
20
|
-
console.log('2', prop);
|
|
21
|
-
delete target._data[prop];
|
|
22
|
-
return true;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
return false;
|
|
26
|
-
}
|
|
27
|
-
},
|
|
28
|
-
|
|
29
|
-
get: (target, prop) => {
|
|
30
|
-
if(prop.indexOf('.') > -1)
|
|
31
|
-
return target._utils.getDotData(target._data, prop);
|
|
32
|
-
|
|
33
|
-
if(prop in target._data)
|
|
34
|
-
return target._data[prop];
|
|
35
|
-
|
|
36
|
-
return target[prop];
|
|
37
|
-
},
|
|
38
|
-
|
|
39
|
-
set: (target, prop, value, receiver) => {
|
|
40
|
-
if(!(prop in target._data) && !target.hasOwnProperty(prop))
|
|
41
|
-
return false;
|
|
42
|
-
|
|
43
|
-
if(target.hasOwnProperty(prop) && ['_data', '_utils'].indexOf(prop) === -1){
|
|
44
|
-
target[prop] = value;
|
|
45
|
-
return true;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
if(receiver instanceof manipulation || target._cloned && receiver instanceof cursor) {
|
|
49
|
-
target._data[prop] = value;
|
|
50
|
-
return true;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
return false;
|
|
54
|
-
}
|
|
55
|
-
});
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
/**
|
|
59
|
-
* @method clone
|
|
60
|
-
* @description Clone the document
|
|
61
|
-
* @return Document
|
|
62
|
-
*/
|
|
63
|
-
clone()
|
|
64
|
-
{
|
|
65
|
-
const cloned = new documents(structuredClone(this._data));
|
|
66
|
-
cloned._cloned = true;
|
|
67
|
-
return cloned;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
* @method delete prop
|
|
72
|
-
* @description Delete a property from the document
|
|
73
|
-
* @param string prop
|
|
74
|
-
* @param cursor receiver
|
|
75
|
-
* @return boolean
|
|
76
|
-
*/
|
|
77
|
-
deleteProp(prop, receiver)
|
|
78
|
-
{
|
|
79
|
-
this._deleteReceiver = receiver;
|
|
80
|
-
if(this._cloned)
|
|
81
|
-
return Reflect.deleteProperty(this, prop);
|
|
82
|
-
|
|
83
|
-
return false;
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
|
|
1
|
+
const utils = require('../utils');
|
|
2
|
+
const cursor = require('./cursor');
|
|
3
|
+
const manipulation = require('../query_language/manipulation');
|
|
4
|
+
|
|
5
|
+
class documents
|
|
6
|
+
{
|
|
7
|
+
constructor(object)
|
|
8
|
+
{
|
|
9
|
+
this._data = object;
|
|
10
|
+
this._utils = new utils();
|
|
11
|
+
this._cloned = false;
|
|
12
|
+
this._deleteReceiver = null;
|
|
13
|
+
|
|
14
|
+
return new Proxy(this, {
|
|
15
|
+
deleteProperty: (target, prop, receiver) => {
|
|
16
|
+
if(target._cloned && target._deleteReceiver instanceof cursor)
|
|
17
|
+
{
|
|
18
|
+
if(prop in target._data)
|
|
19
|
+
{
|
|
20
|
+
console.log('2', prop);
|
|
21
|
+
delete target._data[prop];
|
|
22
|
+
return true;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
return false;
|
|
26
|
+
}
|
|
27
|
+
},
|
|
28
|
+
|
|
29
|
+
get: (target, prop) => {
|
|
30
|
+
if(prop.indexOf('.') > -1)
|
|
31
|
+
return target._utils.getDotData(target._data, prop);
|
|
32
|
+
|
|
33
|
+
if(prop in target._data)
|
|
34
|
+
return target._data[prop];
|
|
35
|
+
|
|
36
|
+
return target[prop];
|
|
37
|
+
},
|
|
38
|
+
|
|
39
|
+
set: (target, prop, value, receiver) => {
|
|
40
|
+
if(!(prop in target._data) && !target.hasOwnProperty(prop))
|
|
41
|
+
return false;
|
|
42
|
+
|
|
43
|
+
if(target.hasOwnProperty(prop) && ['_data', '_utils'].indexOf(prop) === -1){
|
|
44
|
+
target[prop] = value;
|
|
45
|
+
return true;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
if(receiver instanceof manipulation || target._cloned && receiver instanceof cursor) {
|
|
49
|
+
target._data[prop] = value;
|
|
50
|
+
return true;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
return false;
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* @method clone
|
|
60
|
+
* @description Clone the document
|
|
61
|
+
* @return Document
|
|
62
|
+
*/
|
|
63
|
+
clone()
|
|
64
|
+
{
|
|
65
|
+
const cloned = new documents(structuredClone(this._data));
|
|
66
|
+
cloned._cloned = true;
|
|
67
|
+
return cloned;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* @method delete prop
|
|
72
|
+
* @description Delete a property from the document
|
|
73
|
+
* @param string prop
|
|
74
|
+
* @param cursor receiver
|
|
75
|
+
* @return boolean
|
|
76
|
+
*/
|
|
77
|
+
deleteProp(prop, receiver)
|
|
78
|
+
{
|
|
79
|
+
this._deleteReceiver = receiver;
|
|
80
|
+
if(this._cloned)
|
|
81
|
+
return Reflect.deleteProperty(this, prop);
|
|
82
|
+
|
|
83
|
+
return false;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
|
|
88
88
|
module.exports = documents;
|
package/src/helper.js
CHANGED
|
@@ -1,41 +1,41 @@
|
|
|
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
|
|
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
41
|
}
|
|
@@ -1,161 +1,161 @@
|
|
|
1
|
-
const documents = require('../documents/document.js');
|
|
2
|
-
const operatorsCheck = require('./operators.js');
|
|
3
|
-
|
|
4
|
-
class manipulation {
|
|
5
|
-
constructor(validationSchema = null) {
|
|
6
|
-
this.validationSchema = validationSchema;
|
|
7
|
-
this._index = {};
|
|
8
|
-
this._prohibitedCharacters = ['$', '.'];
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* Generates a unique key as a uuid
|
|
13
|
-
* @return string
|
|
14
|
-
*/
|
|
15
|
-
_generateKey() {
|
|
16
|
-
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
|
|
17
|
-
var r = Math.random() * 16 | 0,
|
|
18
|
-
v = c === 'x' ? r : (r & 0x3 | 0x8);
|
|
19
|
-
return v.toString(16)
|
|
20
|
-
});
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* Valid and set validation schema for collection
|
|
25
|
-
* @author Anderson Arruda < anderson@sysborg.com.br >
|
|
26
|
-
* @version 1.0.0
|
|
27
|
-
* @param validationSchema
|
|
28
|
-
* @return void
|
|
29
|
-
**/
|
|
30
|
-
set validationSchema(validationSchema)
|
|
31
|
-
{
|
|
32
|
-
if(typeof schemaValidation === 'undefined' || schemaValidation === null)
|
|
33
|
-
return;
|
|
34
|
-
|
|
35
|
-
if(['object', 'function'].indexOf(typeof schemaValidation) == -1)
|
|
36
|
-
{
|
|
37
|
-
throw new Error('Invalid schema validation');
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
this._validationSchema = validationSchema;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* Validate data
|
|
45
|
-
* @param object data
|
|
46
|
-
* @return boolean
|
|
47
|
-
*/
|
|
48
|
-
_validate(data)
|
|
49
|
-
{
|
|
50
|
-
//under construction
|
|
51
|
-
return true;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* Verify if exists some document that is not object
|
|
56
|
-
* @param array documents
|
|
57
|
-
* @return boolean
|
|
58
|
-
*/
|
|
59
|
-
_validateDocumentsType(documents)
|
|
60
|
-
{
|
|
61
|
-
const count = documents.reduce((acc, doc) => {
|
|
62
|
-
return typeof doc !== 'object' ? ++acc : acc;
|
|
63
|
-
}, 0);
|
|
64
|
-
|
|
65
|
-
return count;
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
/**
|
|
69
|
-
* Verify if keys has prohibited characters
|
|
70
|
-
* @param object document
|
|
71
|
-
* @return boolean
|
|
72
|
-
*/
|
|
73
|
-
_validateKeys(document)
|
|
74
|
-
{
|
|
75
|
-
const keys = Object.keys(document);
|
|
76
|
-
const operators = new operatorsCheck();
|
|
77
|
-
for(let key of keys)
|
|
78
|
-
{
|
|
79
|
-
const hasProhibitedCharacters = this._prohibitedCharacters.reduce((acc, char) => {
|
|
80
|
-
return key.indexOf(char) > -1 ? ++acc : acc;
|
|
81
|
-
}, 0);
|
|
82
|
-
|
|
83
|
-
if(hasProhibitedCharacters > 0 && !operators.isOperator(key))
|
|
84
|
-
return false;
|
|
85
|
-
|
|
86
|
-
if(typeof document[key] === 'object')
|
|
87
|
-
return this._validateKeys(document[key]);
|
|
88
|
-
|
|
89
|
-
if(Array.isArray(document[key]))
|
|
90
|
-
{
|
|
91
|
-
for(let value of document[key])
|
|
92
|
-
{
|
|
93
|
-
if(typeof value === 'object')
|
|
94
|
-
return this._validateKeys(value);
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
return true;
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
/**
|
|
103
|
-
* Inserts one document to the collection
|
|
104
|
-
* @author Anderson Arruda < anderson@sysborg.com.br >
|
|
105
|
-
* @version 1.0.0
|
|
106
|
-
* @param object document
|
|
107
|
-
* @param function callback - under construction
|
|
108
|
-
* @return void
|
|
109
|
-
**/
|
|
110
|
-
insertOne(document, callback)
|
|
111
|
-
{
|
|
112
|
-
if(typeof document !== 'object' || Array.isArray(document))
|
|
113
|
-
throw new Error('Invalid document type');
|
|
114
|
-
|
|
115
|
-
if(!this._validateKeys(document))
|
|
116
|
-
throw new Error('Invalid document keys');
|
|
117
|
-
|
|
118
|
-
if(typeof document._id === 'undefined')
|
|
119
|
-
{
|
|
120
|
-
let _idKey = '';
|
|
121
|
-
do {
|
|
122
|
-
_idKey = this._generateKey();
|
|
123
|
-
} while(this._documents.find(doc => doc._id === document._id));
|
|
124
|
-
|
|
125
|
-
document._id = _idKey;
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
if(!this._validate(document))
|
|
129
|
-
throw new Error('Invalid document');
|
|
130
|
-
|
|
131
|
-
this._documents.push(new documents(document));
|
|
132
|
-
|
|
133
|
-
if(typeof callback === 'function')
|
|
134
|
-
callback();
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
/**
|
|
138
|
-
* Insert many documents
|
|
139
|
-
* @author Anderson Arruda < anderson@sysborg.com.br >
|
|
140
|
-
* @version 1.0.0
|
|
141
|
-
* @param array documents
|
|
142
|
-
* @param function callback
|
|
143
|
-
* @return void
|
|
144
|
-
*/
|
|
145
|
-
insertMany(documents, callback) {
|
|
146
|
-
console.info(this._validateDocumentsType(documents));
|
|
147
|
-
if(!Array.isArray(documents) || this._validateDocumentsType(documents) > 0)
|
|
148
|
-
throw new Error('Documents must be an array of objects');
|
|
149
|
-
|
|
150
|
-
for(let document of documents)
|
|
151
|
-
this.insertOne(document);
|
|
152
|
-
|
|
153
|
-
if(typeof callback === 'function')
|
|
154
|
-
callback();
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
//create index
|
|
158
|
-
//create indexes
|
|
159
|
-
}
|
|
160
|
-
|
|
1
|
+
const documents = require('../documents/document.js');
|
|
2
|
+
const operatorsCheck = require('./operators.js');
|
|
3
|
+
|
|
4
|
+
class manipulation {
|
|
5
|
+
constructor(validationSchema = null) {
|
|
6
|
+
this.validationSchema = validationSchema;
|
|
7
|
+
this._index = {};
|
|
8
|
+
this._prohibitedCharacters = ['$', '.'];
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Generates a unique key as a uuid
|
|
13
|
+
* @return string
|
|
14
|
+
*/
|
|
15
|
+
_generateKey() {
|
|
16
|
+
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
|
|
17
|
+
var r = Math.random() * 16 | 0,
|
|
18
|
+
v = c === 'x' ? r : (r & 0x3 | 0x8);
|
|
19
|
+
return v.toString(16)
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Valid and set validation schema for collection
|
|
25
|
+
* @author Anderson Arruda < anderson@sysborg.com.br >
|
|
26
|
+
* @version 1.0.0
|
|
27
|
+
* @param validationSchema
|
|
28
|
+
* @return void
|
|
29
|
+
**/
|
|
30
|
+
set validationSchema(validationSchema)
|
|
31
|
+
{
|
|
32
|
+
if(typeof schemaValidation === 'undefined' || schemaValidation === null)
|
|
33
|
+
return;
|
|
34
|
+
|
|
35
|
+
if(['object', 'function'].indexOf(typeof schemaValidation) == -1)
|
|
36
|
+
{
|
|
37
|
+
throw new Error('Invalid schema validation');
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
this._validationSchema = validationSchema;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Validate data
|
|
45
|
+
* @param object data
|
|
46
|
+
* @return boolean
|
|
47
|
+
*/
|
|
48
|
+
_validate(data)
|
|
49
|
+
{
|
|
50
|
+
//under construction
|
|
51
|
+
return true;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Verify if exists some document that is not object
|
|
56
|
+
* @param array documents
|
|
57
|
+
* @return boolean
|
|
58
|
+
*/
|
|
59
|
+
_validateDocumentsType(documents)
|
|
60
|
+
{
|
|
61
|
+
const count = documents.reduce((acc, doc) => {
|
|
62
|
+
return typeof doc !== 'object' ? ++acc : acc;
|
|
63
|
+
}, 0);
|
|
64
|
+
|
|
65
|
+
return count;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Verify if keys has prohibited characters
|
|
70
|
+
* @param object document
|
|
71
|
+
* @return boolean
|
|
72
|
+
*/
|
|
73
|
+
_validateKeys(document)
|
|
74
|
+
{
|
|
75
|
+
const keys = Object.keys(document);
|
|
76
|
+
const operators = new operatorsCheck();
|
|
77
|
+
for(let key of keys)
|
|
78
|
+
{
|
|
79
|
+
const hasProhibitedCharacters = this._prohibitedCharacters.reduce((acc, char) => {
|
|
80
|
+
return key.indexOf(char) > -1 ? ++acc : acc;
|
|
81
|
+
}, 0);
|
|
82
|
+
|
|
83
|
+
if(hasProhibitedCharacters > 0 && !operators.isOperator(key))
|
|
84
|
+
return false;
|
|
85
|
+
|
|
86
|
+
if(typeof document[key] === 'object')
|
|
87
|
+
return this._validateKeys(document[key]);
|
|
88
|
+
|
|
89
|
+
if(Array.isArray(document[key]))
|
|
90
|
+
{
|
|
91
|
+
for(let value of document[key])
|
|
92
|
+
{
|
|
93
|
+
if(typeof value === 'object')
|
|
94
|
+
return this._validateKeys(value);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
return true;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Inserts one document to the collection
|
|
104
|
+
* @author Anderson Arruda < anderson@sysborg.com.br >
|
|
105
|
+
* @version 1.0.0
|
|
106
|
+
* @param object document
|
|
107
|
+
* @param function callback - under construction
|
|
108
|
+
* @return void
|
|
109
|
+
**/
|
|
110
|
+
insertOne(document, callback)
|
|
111
|
+
{
|
|
112
|
+
if(typeof document !== 'object' || Array.isArray(document))
|
|
113
|
+
throw new Error('Invalid document type');
|
|
114
|
+
|
|
115
|
+
if(!this._validateKeys(document))
|
|
116
|
+
throw new Error('Invalid document keys');
|
|
117
|
+
|
|
118
|
+
if(typeof document._id === 'undefined')
|
|
119
|
+
{
|
|
120
|
+
let _idKey = '';
|
|
121
|
+
do {
|
|
122
|
+
_idKey = this._generateKey();
|
|
123
|
+
} while(this._documents.find(doc => doc._id === document._id));
|
|
124
|
+
|
|
125
|
+
document._id = _idKey;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
if(!this._validate(document))
|
|
129
|
+
throw new Error('Invalid document');
|
|
130
|
+
|
|
131
|
+
this._documents.push(new documents(document));
|
|
132
|
+
|
|
133
|
+
if(typeof callback === 'function')
|
|
134
|
+
callback();
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Insert many documents
|
|
139
|
+
* @author Anderson Arruda < anderson@sysborg.com.br >
|
|
140
|
+
* @version 1.0.0
|
|
141
|
+
* @param array documents
|
|
142
|
+
* @param function callback
|
|
143
|
+
* @return void
|
|
144
|
+
*/
|
|
145
|
+
insertMany(documents, callback) {
|
|
146
|
+
console.info(this._validateDocumentsType(documents));
|
|
147
|
+
if(!Array.isArray(documents) || this._validateDocumentsType(documents) > 0)
|
|
148
|
+
throw new Error('Documents must be an array of objects');
|
|
149
|
+
|
|
150
|
+
for(let document of documents)
|
|
151
|
+
this.insertOne(document);
|
|
152
|
+
|
|
153
|
+
if(typeof callback === 'function')
|
|
154
|
+
callback();
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
//create index
|
|
158
|
+
//create indexes
|
|
159
|
+
}
|
|
160
|
+
|
|
161
161
|
module.exports = manipulation;
|