Skip to content

Commit 97c743d

Browse files
committed
Refactor clone/copy of a database.
1 parent de7116c commit 97c743d

File tree

3 files changed

+33
-26
lines changed

3 files changed

+33
-26
lines changed

packages/loki/src/collection.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,16 @@ export class Collection<TData extends object = object, TNested extends object =
305305
this.flushChanges();
306306
}
307307

308+
public copy(): Collection<TData, TNested> {
309+
const collCopy = clone(this, "shallow");
310+
collCopy._dynamicViews = [];
311+
312+
for (let i = 0; i < this._dynamicViews.length; i++) {
313+
collCopy._dynamicViews[i] = this._dynamicViews[i].copy();
314+
}
315+
return collCopy;
316+
}
317+
308318
toJSON(): Collection.Serialized {
309319
return {
310320
name: this.name,
@@ -330,11 +340,8 @@ export class Collection<TData extends object = object, TNested extends object =
330340
};
331341
}
332342

333-
static fromJSONObject(obj: Collection | Collection.Serialized, options?: Collection.DeserializeOptions) {
334-
if (obj instanceof Collection) {
335-
return clone(this, "shallow");
336-
}
337343

344+
static fromJSONObject(obj: Collection.Serialized, options?: Collection.DeserializeOptions) {
338345
let coll = new Collection<any>(obj.name, {
339346
disableChangesApi: obj.disableChangesApi,
340347
disableDeltaChangesApi: obj.disableDeltaChangesApi

packages/loki/src/dynamic_view.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {ResultSet} from "./result_set";
33
import {Collection} from "./collection";
44
import {Doc} from "../../common/types";
55
import {Scorer} from "../../full-text-search/src/scorer";
6+
import {clone} from "./clone";
67

78
/**
89
* DynamicView class is a versatile 'live' view class which can have filters and sorts applied.
@@ -147,6 +148,10 @@ export class DynamicView<TData extends object = object, TNested extends object =
147148
return rs.transform(transform, parameters);
148149
}
149150

151+
public copy(): DynamicView<TData, TNested> {
152+
return clone(this, "shallow");
153+
}
154+
150155
/**
151156
* Override of toJSON to avoid circular references.
152157
*/

packages/loki/src/loki.ts

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/* global global */
22
import {LokiEventEmitter} from "./event_emitter";
33
import {Collection} from "./collection";
4+
import {clone} from "./clone";
45
import {Doc, StorageAdapter} from "../../common/types";
56
import {PLUGINS} from "../../common/plugin";
67

@@ -191,26 +192,21 @@ export class Loki extends LokiEventEmitter {
191192
* @param {boolean} options.removeNonSerializable - nulls properties not safe for serialization.
192193
*/
193194
public copy(options: Loki.CopyOptions = {}): Loki {
194-
const databaseCopy = new Loki(this.filename, {env: this._env});
195+
const dbCopy = clone(this, "shallow");
196+
dbCopy._collections = [];
195197

196-
// currently inverting and letting loadJSONObject do most of the work
197-
databaseCopy.loadJSONObject(this, {
198-
retainDirtyFlags: true
199-
});
198+
for (let i = 0; i < this._collections.length; i++) {
199+
dbCopy._collections[i] = this._collections[i].copy();
200+
}
200201

201-
// since our toJSON is not invoked for reference database adapters, this will let us mimic
202202
if (options.removeNonSerializable) {
203-
// databaseCopy._autosaveHandle = null;
204-
// databaseCopy._persistenceAdapter = null;
205-
206-
for (let idx = 0; idx < databaseCopy._collections.length; idx++) {
207-
// TODO: Move to class.
208-
// databaseCopy._collections[idx]._constraints = null;
209-
// databaseCopy._collections[idx]._ttl = null;
210-
}
203+
dbCopy._autosaveHandler = Promise.resolve();
204+
dbCopy._persistenceAdapter = null;
205+
dbCopy._throttledSaveRunning = null;
206+
dbCopy._throttledSavePending = null;
211207
}
212208

213-
return databaseCopy;
209+
return dbCopy;
214210
}
215211

216212
/**
@@ -341,7 +337,7 @@ export class Loki extends LokiEventEmitter {
341337
_serializationMethod: this._serializationMethod,
342338
_autosave: this._autosave,
343339
_autosaveInterval: this._autosaveInterval,
344-
_collections: this._collections,
340+
_collections: this._collections as any as Collection.Serialized[],
345341
databaseVersion: this.databaseVersion,
346342
engineVersion: this.engineVersion,
347343
filename: this.filename,
@@ -388,8 +384,9 @@ export class Loki extends LokiEventEmitter {
388384
}
389385

390386
// not just an individual collection, so we will need to serialize db container via shallow copy
391-
let dbcopy = new Loki(this.filename);
392-
dbcopy.loadJSONObject(this);
387+
// let dbcopy = new Loki(this.filename);
388+
// dbcopy.loadJSONObject(this);
389+
let dbcopy = this.copy();
393390

394391
for (let idx = 0; idx < dbcopy._collections.length; idx++) {
395392
dbcopy._collections[idx]._data = [];
@@ -680,13 +677,11 @@ export class Loki extends LokiEventEmitter {
680677

681678
/**
682679
* Inflates a loki database from a JS object
683-
*
684680
* @param {object} dbObject - a serialized loki database object
685681
* @param {object} options - apply or override collection level settings
686682
* @param {boolean} options.retainDirtyFlags - whether collection dirty flags will be preserved
687683
*/
688-
// public loadJSONObject(dbObject: Loki, options?: Collection.DeserializeOptions): void;
689-
public loadJSONObject(dbObject: Loki | Loki.Serialized, options: Collection.DeserializeOptions = {}): void {
684+
public loadJSONObject(dbObject: Loki.Serialized, options: Collection.DeserializeOptions = {}): void {
690685
// Legacy support.
691686
// if (dbObject.databaseVersion === 1.5) {
692687
// dbObject = dbObject as LokiJS.Loki;
@@ -1084,7 +1079,7 @@ export namespace Loki {
10841079
_serializationMethod: SerializationMethod;
10851080
_autosave: boolean;
10861081
_autosaveInterval: number;
1087-
_collections: Collection[];
1082+
_collections: Collection.Serialized[];
10881083
databaseVersion: 2.0;
10891084
engineVersion: 2.0;
10901085
filename: string;

0 commit comments

Comments
 (0)