Skip to content

Commit 19c28f5

Browse files
author
Gilles Ballini
authored
Merge pull request #159 from kuzzleio/factory-method-refactor
Factory method refactor
2 parents 475b4b8 + 23cdd7f commit 19c28f5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+740
-740
lines changed

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
var
22
bluebird = require('bluebird'),
3-
Kuzzle = require('./src/kuzzle');
3+
Kuzzle = require('./src/Kuzzle');
44

55
// Adds on the fly methods promisification
66
Kuzzle.prototype.bluebird = bluebird;

src/kuzzleDataCollection.js renamed to src/Collection.js

Lines changed: 59 additions & 59 deletions
Large diffs are not rendered by default.

src/kuzzleDataMapping.js renamed to src/CollectionMapping.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,26 @@
1313
* (currently handled by ElasticSearch), and your searches may suffer from below-average performances, depending on
1414
* the amount of data you stored in a collection and the complexity of your database.
1515
*
16-
* The KuzzleDataMapping object allow to get the current mapping of a data collection and to modify it if needed.
16+
* The CollectionMapping object allow to get the current mapping of a data collection and to modify it if needed.
1717
*
18-
* @param {object} kuzzleDataCollection - Instance of the inherited KuzzleDataCollection object
18+
* @param {object} collection - Instance of the inherited Collection object
1919
* @param {object} [mapping] - mappings
2020
* @constructor
2121
*/
22-
function KuzzleDataMapping(kuzzleDataCollection, mapping) {
22+
function CollectionMapping(collection, mapping) {
2323
Object.defineProperties(this, {
2424
//read-only properties
2525
collection: {
26-
value: kuzzleDataCollection,
26+
value: collection,
2727
enumerable: true
2828
},
2929
kuzzle: {
30-
value: kuzzleDataCollection.kuzzle,
30+
value: collection.kuzzle,
3131
enumerable: true
3232
},
3333
// writable properties
3434
headers: {
35-
value: JSON.parse(JSON.stringify(kuzzleDataCollection.headers)),
35+
value: JSON.parse(JSON.stringify(collection.headers)),
3636
enumerable: true,
3737
writable: true
3838
},
@@ -63,7 +63,7 @@ function KuzzleDataMapping(kuzzleDataCollection, mapping) {
6363
* @param {object} [options] - Optional parameters
6464
* @param {responseCallback} [cb] - Handles the query response
6565
*/
66-
KuzzleDataMapping.prototype.apply = function (options, cb) {
66+
CollectionMapping.prototype.apply = function (options, cb) {
6767
var
6868
self = this,
6969
data = this.kuzzle.addHeaders({body: {properties: this.mapping}}, this.headers);
@@ -93,7 +93,7 @@ KuzzleDataMapping.prototype.apply = function (options, cb) {
9393
* @param {responseCallback} [cb] - Handles the query response
9494
* @returns {*} this
9595
*/
96-
KuzzleDataMapping.prototype.refresh = function (options, cb) {
96+
CollectionMapping.prototype.refresh = function (options, cb) {
9797
var
9898
self = this,
9999
data = this.kuzzle.addHeaders({}, this.headers);
@@ -139,9 +139,9 @@ KuzzleDataMapping.prototype.refresh = function (options, cb) {
139139
*
140140
* @param {string} field - Name of the field from which the mapping is to be added or updated
141141
* @param {object} mapping - corresponding field mapping
142-
* @returns {KuzzleDataMapping}
142+
* @returns {CollectionMapping}
143143
*/
144-
KuzzleDataMapping.prototype.set = function (field, mapping) {
144+
CollectionMapping.prototype.set = function (field, mapping) {
145145
this.mapping[field] = mapping;
146146

147147
return this;
@@ -156,9 +156,9 @@ KuzzleDataMapping.prototype.set = function (field, mapping) {
156156
* @param content - new headers content
157157
* @param [replace] - default: false = append the content. If true: replace the current headers with tj
158158
*/
159-
KuzzleDataMapping.prototype.setHeaders = function (content, replace) {
159+
CollectionMapping.prototype.setHeaders = function (content, replace) {
160160
this.kuzzle.setHeaders.call(this, content, replace);
161161
return this;
162162
};
163163

164-
module.exports = KuzzleDataMapping;
164+
module.exports = CollectionMapping;

src/kuzzleDocument.js renamed to src/Document.js

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,19 @@
88

99
/**
1010
* Kuzzle handles documents either as realtime messages or as stored documents.
11-
* KuzzleDocument is the object representation of one of these documents.
11+
* Document is the object representation of one of these documents.
1212
*
1313
* Notes:
1414
* - this constructor may be called either with a documentId, a content, neither or both.
1515
* - providing a documentID to the constructor will automatically call refresh, unless a content is also provided
1616
*
1717
*
18-
* @param {object} kuzzleDataCollection - an instanciated KuzzleDataCollection object
18+
* @param {object} kuzzleDataCollection - an instanciated Collection object
1919
* @param {string} [documentId] - ID of an existing document
2020
* @param {object} [content] - Initializes this document with the provided content
2121
* @constructor
2222
*/
23-
function KuzzleDocument(kuzzleDataCollection, documentId, content) {
23+
function Document(kuzzleDataCollection, documentId, content) {
2424
Object.defineProperties(this, {
2525
// read-only properties
2626
collection: {
@@ -99,7 +99,7 @@ function KuzzleDocument(kuzzleDataCollection, documentId, content) {
9999
*
100100
* @return {object} JSON object representing this document
101101
*/
102-
KuzzleDocument.prototype.serialize = function () {
102+
Document.prototype.serialize = function () {
103103
var
104104
data = {};
105105

@@ -119,7 +119,7 @@ KuzzleDocument.prototype.serialize = function () {
119119
*
120120
* @return {string} serialized version of this object
121121
*/
122-
KuzzleDocument.prototype.toString = function () {
122+
Document.prototype.toString = function () {
123123
return JSON.stringify(this.serialize());
124124
};
125125

@@ -134,7 +134,7 @@ KuzzleDocument.prototype.toString = function () {
134134
* @param {responseCallback} [cb] - Handles the query response
135135
* @returns {*} this
136136
*/
137-
KuzzleDocument.prototype.delete = function (options, cb) {
137+
Document.prototype.delete = function (options, cb) {
138138
var self = this;
139139

140140
if (!cb && typeof options === 'function') {
@@ -143,7 +143,7 @@ KuzzleDocument.prototype.delete = function (options, cb) {
143143
}
144144

145145
if (!self.id) {
146-
throw new Error('KuzzleDocument.delete: cannot delete a document without a document ID');
146+
throw new Error('Document.delete: cannot delete a document without a document ID');
147147
}
148148

149149
this.kuzzle.query(this.dataCollection.buildQueryArgs('document', 'delete'), this.serialize(), options, cb && function (err) {
@@ -158,7 +158,7 @@ KuzzleDocument.prototype.delete = function (options, cb) {
158158
* @param {responseCallback} [cb] - Handles the query response
159159
* @returns {*} this
160160
*/
161-
KuzzleDocument.prototype.refresh = function (options, cb) {
161+
Document.prototype.refresh = function (options, cb) {
162162
var self = this;
163163

164164
if (!cb && typeof options === 'function') {
@@ -167,10 +167,10 @@ KuzzleDocument.prototype.refresh = function (options, cb) {
167167
}
168168

169169
if (!self.id) {
170-
throw new Error('KuzzleDocument.refresh: cannot retrieve a document if no ID has been provided');
170+
throw new Error('Document.refresh: cannot retrieve a document if no ID has been provided');
171171
}
172172

173-
this.kuzzle.callbackRequired('KuzzleDocument.refresh', cb);
173+
this.kuzzle.callbackRequired('Document.refresh', cb);
174174

175175
self.kuzzle.query(self.dataCollection.buildQueryArgs('document', 'get'), {_id: self.id}, options, function (error, res) {
176176
var newDocument;
@@ -179,7 +179,7 @@ KuzzleDocument.prototype.refresh = function (options, cb) {
179179
return cb(error);
180180
}
181181

182-
newDocument = new KuzzleDocument(self.dataCollection, self.id, res.result._source);
182+
newDocument = new Document(self.dataCollection, self.id, res.result._source);
183183
newDocument.version = res.result._version;
184184

185185
cb(null, newDocument);
@@ -201,7 +201,7 @@ KuzzleDocument.prototype.refresh = function (options, cb) {
201201
* @param {responseCallback} [cb] - Handles the query response
202202
* @returns {*} this
203203
*/
204-
KuzzleDocument.prototype.save = function (options, cb) {
204+
Document.prototype.save = function (options, cb) {
205205
var
206206
data = this.serialize(),
207207
self = this;
@@ -237,7 +237,7 @@ KuzzleDocument.prototype.save = function (options, cb) {
237237
* @param {object} [options] - Optional parameters
238238
* @returns {*} this
239239
*/
240-
KuzzleDocument.prototype.publish = function (options) {
240+
Document.prototype.publish = function (options) {
241241
var data = this.serialize();
242242

243243
this.kuzzle.query(this.dataCollection.buildQueryArgs('realtime', 'publish'), data, options);
@@ -252,7 +252,7 @@ KuzzleDocument.prototype.publish = function (options) {
252252
* @param {object} data - New content
253253
* @param {boolean} replace - if true: replace this document content with the provided data
254254
*/
255-
KuzzleDocument.prototype.setContent = function (data, replace) {
255+
Document.prototype.setContent = function (data, replace) {
256256
var self = this;
257257

258258
if (replace) {
@@ -274,18 +274,18 @@ KuzzleDocument.prototype.setContent = function (data, replace) {
274274
* @param {object} [options] - subscription options
275275
* @param {responseCallback} cb - callback that will be called each time a change has been detected on this document
276276
*/
277-
KuzzleDocument.prototype.subscribe = function (options, cb) {
277+
Document.prototype.subscribe = function (options, cb) {
278278
var filters;
279279

280280
if (options && !cb && typeof options === 'function') {
281281
cb = options;
282282
options = null;
283283
}
284284

285-
this.kuzzle.callbackRequired('KuzzleDocument.subscribe', cb);
285+
this.kuzzle.callbackRequired('Document.subscribe', cb);
286286

287287
if (!this.id) {
288-
throw new Error('KuzzleDocument.subscribe: cannot subscribe to a document if no ID has been provided');
288+
throw new Error('Document.subscribe: cannot subscribe to a document if no ID has been provided');
289289
}
290290

291291
filters = { ids: { values: [this.id] } };
@@ -302,10 +302,10 @@ KuzzleDocument.prototype.subscribe = function (options, cb) {
302302
* @param content - new headers content
303303
* @param [replace] - default: false = append the content. If true: replace the current headers with tj
304304
*/
305-
KuzzleDocument.prototype.setHeaders = function (content, replace) {
305+
Document.prototype.setHeaders = function (content, replace) {
306306
this.kuzzle.setHeaders.call(this, content, replace);
307307
return this;
308308
};
309309

310310

311-
module.exports = KuzzleDocument;
311+
module.exports = Document;

src/kuzzle.js renamed to src/Kuzzle.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
var
22
uuid = require('uuid'),
3-
KuzzleDataCollection = require('./kuzzleDataCollection'),
4-
KuzzleSecurity = require('./security/kuzzleSecurity'),
5-
KuzzleMemoryStorage = require('./kuzzleMemoryStorage'),
6-
KuzzleUser = require('./security/kuzzleUser'),
3+
Collection = require('./Collection.js'),
4+
Security = require('./security/Security'),
5+
MemoryStorage = require('./MemoryStorage'),
6+
User = require('./security/User'),
77
networkWrapper = require('./networkWrapper');
88

99
/**
@@ -249,7 +249,7 @@ function Kuzzle (host, options, cb) {
249249
* Create an attribute security that embed all methods to manage Role, Profile and User
250250
*/
251251
Object.defineProperty(this, 'security', {
252-
value: new KuzzleSecurity(this),
252+
value: new Security(this),
253253
enumerable: true
254254
});
255255

@@ -282,7 +282,7 @@ function Kuzzle (host, options, cb) {
282282
});
283283

284284
Object.defineProperty(this, 'memoryStorage', {
285-
value: new KuzzleMemoryStorage(this),
285+
value: new MemoryStorage(this),
286286
enumerable: true
287287
});
288288

@@ -610,7 +610,7 @@ Kuzzle.prototype.whoAmI = function (callback) {
610610
return callback(err);
611611
}
612612

613-
callback(null, new KuzzleUser(self.security, response.result._id, response.result._source));
613+
callback(null, new User(self.security, response.result._id, response.result._source));
614614
});
615615
};
616616

@@ -754,7 +754,7 @@ function emitRequest (request, cb) {
754754

755755
this.network.send(request);
756756

757-
// Track requests made to allow KuzzleRoom.subscribeToSelf to work
757+
// Track requests made to allow Room.subscribeToSelf to work
758758
self.requestHistory[request.requestId] = Date.now();
759759
}
760760

@@ -934,14 +934,14 @@ Kuzzle.prototype.getStatistics = function (timestamp, options, cb) {
934934
};
935935

936936
/**
937-
* Create a new instance of a KuzzleDataCollection object.
937+
* Create a new instance of a Collection object.
938938
* If no index is specified, takes the default index.
939939
*
940940
* @param {string} collection - The name of the data collection you want to manipulate
941941
* @param {string} [index] - The name of the data index containing the data collection
942-
* @returns {KuzzleDataCollection} A KuzzleDataCollection instance
942+
* @returns {Collection} A Collection instance
943943
*/
944-
Kuzzle.prototype.dataCollectionFactory = function(collection, index) {
944+
Kuzzle.prototype.collection = function(collection, index) {
945945
this.isValid();
946946

947947
if (!index) {
@@ -961,7 +961,7 @@ Kuzzle.prototype.dataCollectionFactory = function(collection, index) {
961961
}
962962

963963
if (!this.collections[index][collection]) {
964-
this.collections[index][collection] = new KuzzleDataCollection(this, collection, index);
964+
this.collections[index][collection] = new Collection(this, collection, index);
965965
}
966966

967967
return this.collections[index][collection];
@@ -1486,4 +1486,4 @@ Kuzzle.prototype.stopQueuing = function () {
14861486
return this;
14871487
};
14881488

1489-
module.exports = Kuzzle;
1489+
module.exports = Kuzzle;

src/kuzzleMemoryStorage.js renamed to src/MemoryStorage.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
* @param {object} kuzzle - Kuzzle instance to inherit from
2929
* @constructor
3030
*/
31-
function KuzzleMemoryStorage(kuzzle) {
31+
function MemoryStorage(kuzzle) {
3232
Object.defineProperties(this, {
3333
// read-only properties
3434
kuzzle: {
@@ -173,7 +173,7 @@ function KuzzleMemoryStorage(kuzzle) {
173173
commands.zscore = commands.zrevrank;
174174

175175
Object.keys(commands).forEach(function (command) {
176-
KuzzleMemoryStorage.prototype[command] = function () {
176+
MemoryStorage.prototype[command] = function () {
177177
var
178178
args = Array.prototype.slice.call(arguments),
179179
options = null,
@@ -231,4 +231,4 @@ function KuzzleMemoryStorage(kuzzle) {
231231

232232
})();
233233

234-
module.exports = KuzzleMemoryStorage;
234+
module.exports = MemoryStorage;

0 commit comments

Comments
 (0)