This repository has been archived by the owner on Nov 9, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
173 lines (162 loc) · 5.24 KB
/
index.js
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
const path = require('path');
const fs = require('fs');
const loki = require('lokijs');
/**
* Helper with in memory databse for data driven testing and result capturing.
*/
class Loki extends Helper {
/**
*
* @param {Object} config configuration can be overridded by values found in `codecept.json
*/
constructor(config) {
super(config);
// set defaults
this.options = {
dbName: "db.json",
dbOpts: {
autosave: true,
autosaveInterval: 1000,
},
dbSeed: true
};
// override defaults with config
Object.assign(this.options, config);
}
_init() {
//set global directory
this.dir = global.codecept_dir;
this.debugSection('Dir', this.dir);
//Creates/Initialises the database
this.db = new loki(this.options.dbName, this.options.dbOpts);
//Creates seed data directory if necessary and imports the contents.
this.seedDir = path.join("./seed");
this.options.dbSeed && mkdirSync(this.seedDir);
this.options.dbSeed && this._loadSeedData();
}
_afterSuite() {
this._stopDb();
}
_stopDb() {
this.db.close();
}
_loadSeedData() {
this.importData(this.seedDir);
}
/**
*
* Will check for an existing collection of the same name and return that, if it already exists.
* @param {string} collection creates a collection with supplied name.
*/
addCollection(collection) {
return !this.findCollection(collection) && this.db.addCollection(collection);
}
/**
*
* @param {string} collection name of desired collection.
*/
findCollection(collection) {
return this.db.getCollection(collection);
}
/**
*
* Removes a matching collection.
* @param {string} collection finds a collection that matches the supplied name
*/
removeCollection(collection) {
return this.db.removeCollection(collection);
}
/**
*
* Inserts records from the supplied data to a matching collection.
* @param {string} collection name of desired collection.
* @param {Array}data takes an array of objects as records for the destination collection.
*/
insert(collection, data) {
const c = this.findCollection(collection);
return c.insert(data);
}
/**
*
* Clears data from a matching collection.
* @param {string} collection name of desired collection.
*/
clear(collection) {
const c = this.findCollection(collection);
return c.clear();
}
/**
* Searches the Users colection for matching values.
* @param {string} collection name of desired collection.
* @param {Object} query used in searching the destination collection for matching values.
* @example
* // Searches for a user with an email of "[email protected]"
* I.find("Users",{email: "[email protected]"})
* @returns {Array}Returns an array of objects that match.
*/
find(collection, query) {
const c = this.findCollection(collection);
return c.find(query);
}
/**
*
* Effectively the same as `find()` but returns a single object rather than an array.
* @param {string} collection name of desired collection.
* @param {Object} query used in searching the destination collection for matching values.
*/
findOne(collection, query) {
const c = this.findCollection(collection);
return c.findOne(query);
}
/**
*
* Finds a list of values based on the supplied query and runs an update function on each result.
* @param {string} collection name of desired collection.
* @param {Object} query used in searching the destination collection for matching values.
* @param {function} updateFunction executes against each of the results in the result array.
* @example
* // Before {email: "[email protected]", firstname: "Some", surname: "One", address1: "1 Some Place"}
* I.findAndUpdate("users",{email: "[email protected]"},(res)=>{res.number = "01234567890"})
* // Find updated record
* I.findOne("Users",{email: "[email protected]"})
* // After {email: "[email protected]", firstname: "Some", surname: "One", address1: "1 Some Place", number:01234567890}
*/
findAndUpdate(collection, query, updateFunction) {
const c = this.findCollection(collection);
return c.findAndUpdate(query, updateFunction);
}
/**
*
* Like `findAndUpdate()` but finds a record in a collection and removes it.
* @param {string} collection name of desired collection.
* @param {Object} query used in searching the destination collection for matching values.
*/
findAndRemove(collection, query) {
const c = this.findCollection(collection);
return c.findAndRemove(query);
}
/**
*
* Imports data from a directory into a collection. Uses file names to create a collection of the same name and imports the contents of the file as records to the destination.
* @param {string} dir takes a directory as a string.
*/
importData(dir) {
const files = fs.readdirSync(dir);
files.map(i => {
const fileName = i.substr(0, i.lastIndexOf('.')) || i;
const filePath = path.join(this.dir, dir, i);
const collection = this.db.addCollection(fileName);
return collection.insert(require(filePath));
});
}
}
function mkdirSync(dir) {
try {
fs.mkdirSync(dir);
} catch (e) {
if (e.code !== "EEXIST") {
throw e;
}
}
}
module.exports = Loki;