|
1 | 1 | /* global global */ |
2 | 2 | import {LokiEventEmitter} from "./event_emitter"; |
3 | 3 | import {Collection} from "./collection"; |
| 4 | +import {clone} from "./clone"; |
4 | 5 | import {Doc, StorageAdapter} from "../../common/types"; |
5 | 6 | import {PLUGINS} from "../../common/plugin"; |
6 | 7 |
|
@@ -191,26 +192,21 @@ export class Loki extends LokiEventEmitter { |
191 | 192 | * @param {boolean} options.removeNonSerializable - nulls properties not safe for serialization. |
192 | 193 | */ |
193 | 194 | 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 = []; |
195 | 197 |
|
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 | + } |
200 | 201 |
|
201 | | - // since our toJSON is not invoked for reference database adapters, this will let us mimic |
202 | 202 | 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; |
211 | 207 | } |
212 | 208 |
|
213 | | - return databaseCopy; |
| 209 | + return dbCopy; |
214 | 210 | } |
215 | 211 |
|
216 | 212 | /** |
@@ -341,7 +337,7 @@ export class Loki extends LokiEventEmitter { |
341 | 337 | _serializationMethod: this._serializationMethod, |
342 | 338 | _autosave: this._autosave, |
343 | 339 | _autosaveInterval: this._autosaveInterval, |
344 | | - _collections: this._collections, |
| 340 | + _collections: this._collections as any as Collection.Serialized[], |
345 | 341 | databaseVersion: this.databaseVersion, |
346 | 342 | engineVersion: this.engineVersion, |
347 | 343 | filename: this.filename, |
@@ -388,8 +384,9 @@ export class Loki extends LokiEventEmitter { |
388 | 384 | } |
389 | 385 |
|
390 | 386 | // 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(); |
393 | 390 |
|
394 | 391 | for (let idx = 0; idx < dbcopy._collections.length; idx++) { |
395 | 392 | dbcopy._collections[idx]._data = []; |
@@ -680,13 +677,11 @@ export class Loki extends LokiEventEmitter { |
680 | 677 |
|
681 | 678 | /** |
682 | 679 | * Inflates a loki database from a JS object |
683 | | - * |
684 | 680 | * @param {object} dbObject - a serialized loki database object |
685 | 681 | * @param {object} options - apply or override collection level settings |
686 | 682 | * @param {boolean} options.retainDirtyFlags - whether collection dirty flags will be preserved |
687 | 683 | */ |
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 { |
690 | 685 | // Legacy support. |
691 | 686 | // if (dbObject.databaseVersion === 1.5) { |
692 | 687 | // dbObject = dbObject as LokiJS.Loki; |
@@ -1084,7 +1079,7 @@ export namespace Loki { |
1084 | 1079 | _serializationMethod: SerializationMethod; |
1085 | 1080 | _autosave: boolean; |
1086 | 1081 | _autosaveInterval: number; |
1087 | | - _collections: Collection[]; |
| 1082 | + _collections: Collection.Serialized[]; |
1088 | 1083 | databaseVersion: 2.0; |
1089 | 1084 | engineVersion: 2.0; |
1090 | 1085 | filename: string; |
|
0 commit comments