Skip to content

Commit

Permalink
feat(loki): change and improve nested property support (#81)
Browse files Browse the repository at this point in the history
  • Loading branch information
Viatorus authored Mar 8, 2018
1 parent 1a49470 commit 2730284
Show file tree
Hide file tree
Showing 16 changed files with 826 additions and 730 deletions.
9 changes: 8 additions & 1 deletion packages/common/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,14 @@ export interface StorageAdapter {
exportDatabase?(dbname: string, dbref: Loki): Promise<void>;
}

export type Doc<T extends object = object> = T & { $loki: number; meta: any; };
export type Doc<T extends object = object> = T & {
$loki: number; meta?: {
created: number;
revision: number;
version: number,
updated?: number;
};
};

export interface Dict<T> {
[index: string]: T;
Expand Down
33 changes: 33 additions & 0 deletions packages/full-text-search/spec/generic/full_text_search.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,39 @@ describe("full-text search", () => {
.toBe(2);
});

it("nested", () => {
const coll2 = db.addCollection<{ user: { name: string, id: number } }, { "user.name": string, "user.id": number }>("coll", {
nestedProperties: ["user.name", "user.id"],
fullTextSearch: [{field: "user.name"}]
});
for (let i of coll.find()) {
coll2.insert({user: i});
}

let query: Query = {query: {type: "fuzzy", field: "user.name", value: "quak", fuzziness: 1}};
expect(coll2.find({"$fts": query}).length).toBe(3);

expect(
coll2.find({"$fts": query})).not.toEqual(
coll2.find({"user.id": {"$in": [1, 2, 3]}}));

expect(
coll2
.chain()
.find({"user.id": {"$in": [1, 2, 3]}})
.find({"$fts": query})
.data().length)
.toBe(2);

expect(
coll2
.chain()
.find({"$fts": query})
.find({"user.id": {"$in": [1, 2, 3]}})
.data().length)
.toBe(2);
});

it("update", () => {
coll.updateWhere((user: User) => {
return user.name === "quak";
Expand Down
4 changes: 2 additions & 2 deletions packages/full-text-search/src/full_text_search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ export class FullTextSearch {
}

public addDocument(doc: object, id: number = doc[this._id]): void {
let fieldNames = Object.keys(doc);
let fieldNames = Object.keys(this._invIdxs);
for (let i = 0, fieldName; i < fieldNames.length, fieldName = fieldNames[i]; i++) {
if (this._invIdxs[fieldName] !== undefined) {
if (doc[fieldName] !== undefined) {
this._invIdxs[fieldName].insert(doc[fieldName], id);
}
}
Expand Down
Loading

0 comments on commit 2730284

Please sign in to comment.