Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions config/karma.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ module.exports = function (config) {
test: /\.ts$/
}
]
},
externals: {
fs: "fs"
}
},
webpackMiddleware: {
Expand Down
3 changes: 2 additions & 1 deletion config/nycrc.node.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"exclude": [
"**/*.helper.[jt]s",
"**/*.spec.[jt]s",
"**/spec/**/*.[jt]s"
"**/spec/**/*.[jt]s",
"**/lokijs/**/*.js"
],
"report-dir": "./coverage/node/"
}
10 changes: 5 additions & 5 deletions packages/common/types.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
/**
* @hidden
*/
import { Loki } from "../loki/src/loki";
import { Loki } from "../loki/src";
import { Serialization } from "../loki/src/serialization/migration";

export interface StorageAdapter {
loadDatabase(dbname: string): Promise<any>;
loadDatabase(dbname: string): Promise<string | Loki | Serialization.Serialized>;

saveDatabase?(dbname: string, serialization: string): Promise<void>;

Expand All @@ -25,11 +26,10 @@ export type Doc<T extends object = object> = T & {
};
};

export interface Dict<T> {
export type Dict<T> = {
[index: string]: T;

[index: number]: T;
}
};



Expand Down
22 changes: 22 additions & 0 deletions packages/fs-storage/spec/node/fs_storage.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
import { Loki } from "../../../loki/src/loki";
import { FSStorage } from "../../src/fs_storage";

declare var require: (moduleId: string) => any;
const loki = require("../../../lokijs/lokijs.js");

describe("testing fs storage", function () {

interface Name {
Expand Down Expand Up @@ -68,4 +71,23 @@ describe("testing fs storage", function () {
});
});
});

it("from lokijs", (done) => {
const legacyDB = new loki("legacyDB", {persistenceMethod: "fs"});
const coll = legacyDB.addCollection("myColl");
coll.insert({name: "Hello World"});
legacyDB.saveDatabase(() => {
// Load with LokiDB.
const db = new Loki("legacyDB");
return db.initializePersistence()
.then(() => {
return db.loadDatabase();
}).then(() => {
expect(db.getCollection<Name>("myColl").find()[0].name).toEqual("Hello World");
return db.deleteDatabase();
}).then(() => {
done();
});
});
});
});
10 changes: 3 additions & 7 deletions packages/full-text-search/src/full_text_search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { PLUGINS } from "../../common/plugin";
import { Query } from "./query_types";
import { Scorer } from "./scorer";
import { Analyzer } from "./analyzer/analyzer";
import { Serialization } from "../../loki/src/serialization/migration";

export class FullTextSearch {
/// The id field of each document.
Expand Down Expand Up @@ -78,7 +79,7 @@ export class FullTextSearch {
return this._idxSearcher.search(query);
}

public toJSON(): FullTextSearch.Serialization {
public toJSON(): Serialization.FullTextSearch {
let serialized = {id: this._id, ii: {}};
let fieldNames = Object.keys(this._invIdxs);
for (let i = 0; i < fieldNames.length; i++) {
Expand All @@ -88,7 +89,7 @@ export class FullTextSearch {
return serialized;
}

public static fromJSONObject(serialized: FullTextSearch.Serialization, analyzers: Dict<Analyzer> = {}): FullTextSearch {
public static fromJSONObject(serialized: Serialization.FullTextSearch, analyzers: Dict<Analyzer> = {}): FullTextSearch {
let fts = new FullTextSearch([], serialized.id);
let fieldNames = Object.keys(serialized.ii);
for (let i = 0; i < fieldNames.length; i++) {
Expand All @@ -103,9 +104,4 @@ export namespace FullTextSearch {
export interface FieldOptions extends InvertedIndex.FieldOptions {
field: string;
}

export interface Serialization {
id: string;
ii: Dict<InvertedIndex.Serialization>;
}
}
54 changes: 15 additions & 39 deletions packages/full-text-search/src/inverted_index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Analyzer, StandardAnalyzer, analyze } from "./analyzer/analyzer";
import { Serialization } from "../../loki/src/serialization/migration";

/**
* Converts a string into an array of code points.
Expand Down Expand Up @@ -31,7 +32,7 @@ export class InvertedIndex {
public docCount: number = 0;
public docStore: Map<InvertedIndex.DocumentIndex, InvertedIndex.DocStore> = new Map();
public totalFieldLength: number = 0;
public root: InvertedIndex.Index = new Map();
public root: InvertedIndex.Index = new Map() as InvertedIndex.Index;

private _store: boolean;
private _optimizeChanges: boolean;
Expand Down Expand Up @@ -219,20 +220,20 @@ export class InvertedIndex {
* Serialize the inverted index.
* @returns {{docStore: *, _fields: *, index: *}}
*/
public toJSON(): InvertedIndex.Serialization {
public toJSON(): Serialization.FullTextSearch.InvertedIndex {
if (this._store) {
return {
_store: true,
_optimizeChanges: this._optimizeChanges,
store: true,
optimizeChanges: this._optimizeChanges,
docCount: this.docCount,
docStore: [...this.docStore],
totalFieldLength: this.totalFieldLength,
root: InvertedIndex._serializeIndex(this.root)
};
}
return {
_store: false,
_optimizeChanges: this._optimizeChanges,
store: false,
optimizeChanges: this._optimizeChanges,
};
}

Expand All @@ -241,14 +242,14 @@ export class InvertedIndex {
* @param {{docStore: *, _fields: *, index: *}} serialized - The serialized inverted index.
* @param {Analyzer} analyzer[undefined] - an analyzer
*/
public static fromJSONObject(serialized: InvertedIndex.Serialization, analyzer?: Analyzer): InvertedIndex {
public static fromJSONObject(serialized: Serialization.FullTextSearch.InvertedIndex, analyzer?: Analyzer): InvertedIndex {
const invIdx = new InvertedIndex({
store: serialized._store,
optimizeChanges: serialized._optimizeChanges,
store: serialized.store,
optimizeChanges: serialized.optimizeChanges,
analyzer: analyzer
});

if (serialized._store) {
if (serialized.store) {
invIdx.docCount = serialized.docCount;
invIdx.docStore = new Map(serialized.docStore);
invIdx.totalFieldLength = serialized.totalFieldLength;
Expand All @@ -262,8 +263,8 @@ export class InvertedIndex {
return invIdx;
}

private static _serializeIndex(idx: InvertedIndex.Index): InvertedIndex.SerializedIndex {
const serialized: InvertedIndex.SerializedIndex = {};
private static _serializeIndex(idx: InvertedIndex.Index): Serialization.FullTextSearch.Index {
const serialized: Serialization.FullTextSearch.Index = {};
if (idx.dc !== undefined) {
serialized.d = {df: idx.df, dc: [...idx.dc]};
}
Expand All @@ -284,8 +285,8 @@ export class InvertedIndex {
return serialized;
}

private static _deserializeIndex(serialized: InvertedIndex.SerializedIndex): InvertedIndex.Index {
const idx: InvertedIndex.Index = new Map();
private static _deserializeIndex(serialized: Serialization.FullTextSearch.Index): InvertedIndex.Index {
const idx: InvertedIndex.Index = new Map() as InvertedIndex.Index;

if (serialized.k !== undefined) {
for (let i = 0; i < serialized.k.length; i++) {
Expand Down Expand Up @@ -374,31 +375,6 @@ export namespace InvertedIndex {

export type IndexTerm = { index: Index, term: number[] };

export interface SerializedIndex {
d?: {
df: number;
dc: [DocumentIndex, number][]
};
k?: number[];
v?: SerializedIndex[];
}

export type Serialization = SpareSerialization | FullSerialization;

export type SpareSerialization = {
_store: false;
_optimizeChanges: boolean;
};

export type FullSerialization = {
_store: true;
_optimizeChanges: boolean;
docCount: number;
docStore: [DocumentIndex, DocStore][];
totalFieldLength: number;
root: SerializedIndex;
};

export interface DocStore {
fieldLength?: number;
indexRef?: Index[];
Expand Down
23 changes: 23 additions & 0 deletions packages/indexed-storage/spec/web/indexed_storage.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
import { Loki } from "../../../loki/src/loki";
import { IndexedStorage } from "../../src/indexed_storage";

declare var require: (moduleId: string) => any;
const loki = require("../../../lokijs/lokijs.js");
const indexedAdapter = require("../../../lokijs/loki-indexed-adapter.js");

describe("testing indexed storage", function () {

interface Name {
Expand Down Expand Up @@ -68,4 +72,23 @@ describe("testing indexed storage", function () {
});
});
});

it("from lokijs", (done) => {
const legacyDB = new loki("legacyDB", {adapter: new indexedAdapter()});
const coll = legacyDB.addCollection("myColl");
coll.insert({name: "Hello World"});
legacyDB.saveDatabase(() => {
// Load with LokiDB.
const db = new Loki("legacyDB");
return db.initializePersistence()
.then(() => {
return db.loadDatabase();
}).then(() => {
expect(db.getCollection<Name>("myColl").find()[0].name).toEqual("Hello World");
return db.deleteDatabase();
}).then(() => {
done();
});
});
});
});
2 changes: 1 addition & 1 deletion packages/indexed-storage/src/indexed_storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export class IndexedStorage implements StorageAdapter {
* @param {string} dbname - the name of the database to retrieve.
* @returns {Promise} a Promise that resolves after the database was loaded
*/
loadDatabase(dbname: string) {
loadDatabase(dbname: string): Promise<string> {
const appName = this._appname;
const adapter = this;

Expand Down
22 changes: 22 additions & 0 deletions packages/local-storage/spec/web/local_storage.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
import { Loki } from "../../../loki/src/loki";
import { LocalStorage } from "../../src/local_storage";

declare var require: (moduleId: string) => any;
const loki = require("../../../lokijs/lokijs.js");

describe("testing local storage", function () {

interface Name {
Expand Down Expand Up @@ -68,4 +71,23 @@ describe("testing local storage", function () {
});
});
});

it("from lokijs", (done) => {
const legacyDB = new loki("legacyDB", {persistenceMethod: "localStorage"});
const coll = legacyDB.addCollection("myColl");
coll.insert({name: "Hello World"});
legacyDB.saveDatabase(() => {
// Load with LokiDB.
const db = new Loki("legacyDB");
return db.initializePersistence()
.then(() => {
return db.loadDatabase();
}).then(() => {
expect(db.getCollection<Name>("myColl").find()[0].name).toEqual("Hello World");
return db.deleteDatabase();
}).then(() => {
done();
});
});
});
});
Loading