@@ -249,6 +249,13 @@ export class Loki extends LokiEventEmitter {
249
249
* @returns {Collection } a reference to the collection which was just added
250
250
*/
251
251
public addCollection < T extends object = object , U extends object = object > ( name : string , options : Collection . Options < T > = { } ) : Collection < T , U > {
252
+ // Return an existing collection if a collection with the same name already exists.
253
+ for ( let i = 0 ; i < this . _collections . length ; i ++ ) {
254
+ if ( this . _collections [ i ] . name === name ) {
255
+ return this . _collections [ i ] as Collection < T , U > ;
256
+ }
257
+ }
258
+ // Create a new collection otherwise.
252
259
const collection = new Collection < T , U > ( name , options ) ;
253
260
this . _collections . push ( collection ) ;
254
261
@@ -267,21 +274,18 @@ export class Loki extends LokiEventEmitter {
267
274
268
275
/**
269
276
* Retrieves reference to a collection by name.
270
- * @param {string } collectionName - name of collection to look up
277
+ * @param {string } name - name of collection to look up
271
278
* @returns {Collection } Reference to collection in database by that name, or null if not found
272
279
*/
273
- public getCollection < T extends object = object > ( collectionName : string ) : Collection < T > {
274
- let i ;
275
- const len = this . _collections . length ;
276
-
277
- for ( i = 0 ; i < len ; i ++ ) {
278
- if ( this . _collections [ i ] . name === collectionName ) {
279
- return this . _collections [ i ] as Collection < T > ;
280
+ public getCollection < T extends object = object , U extends object = object > ( name : string ) : Collection < T , U > {
281
+ for ( let i = 0 ; i < this . _collections . length ; i ++ ) {
282
+ if ( this . _collections [ i ] . name === name ) {
283
+ return this . _collections [ i ] as Collection < T , U > ;
280
284
}
281
285
}
282
286
283
287
// no such collection
284
- this . emit ( "warning" , "collection " + collectionName + " not found" ) ;
288
+ this . emit ( "warning" , "collection " + name + " not found" ) ;
285
289
return null ;
286
290
}
287
291
0 commit comments