Skip to content

Commit

Permalink
FIX: LokiJS workaround, need to use throttledSaves = false in config …
Browse files Browse the repository at this point in the history
…when loading multiple large JSON files
  • Loading branch information
erikvullings committed Jul 30, 2021
1 parent 3654644 commit 531380b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 14 deletions.
23 changes: 9 additions & 14 deletions src/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ const importJSON = (collectionName: string, filename: string) => {
const collection = db.getCollection(collectionName);
if (json instanceof Array) {
collection.insert(json);
// for (const o of json) {
// collection.insertOne
// }
console.log(
`Finished reading ${filename}, number of entries in collection '${collection.name}': ${collection.count()}`,
);
} else {
console.warn(`JSON file is not an array! Ignoring ${filename}.`);
}
Expand All @@ -37,18 +37,13 @@ const databaseInitialize = (options?: ILokiConfiguration) => {
db.collections.forEach((c) => {
collectionStore[c.name] = db.getCollection(c.name);
});
} else if (options) {
} else if (options && options.collections && typeof options.collections === 'object') {
const { collections } = options;
if (collections && typeof collections === 'object') {
for (const collectionName of Object.keys(collections)) {
const collection = collections[collectionName];
db.addCollection(collectionName, collection);
collection.jsonImport && importJSON(collectionName, collection.jsonImport);
}
for (const collectionName of Object.keys(collections)) {
const collection = collections[collectionName];
collectionStore[collectionName] = db.addCollection(collectionName, collection);
collection.jsonImport && importJSON(collectionName, collection.jsonImport);
}
db.collections.forEach((c) => {
collectionStore[c.name] = db.getCollection(c.name);
});
}
// kick off any program logic or start listening to external events
runProgramLogic();
Expand Down Expand Up @@ -77,7 +72,7 @@ export const startDatabase = (file = 'rest_easy_loki.db', cb?: () => void, optio
autoload: true,
autoloadCallback,
autosave: true,
autosaveInterval: 4000,
throttledSaves: options ? options.throttledSaves : true,
} as Partial<LokiConfigOptions>);
};

Expand Down
1 change: 1 addition & 0 deletions src/models/loki-configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export interface ExtendedCollectionOptions<E> extends CollectionOptions<E> {
}

export interface ILokiConfiguration<T = {}> {
throttledSaves?: boolean;
/** Create collections on startup if there are no collections yet */
collections?: {
/** Name of the collection */
Expand Down

0 comments on commit 531380b

Please sign in to comment.