core: fix sync tests

This commit is contained in:
Abdullah Atta
2023-10-07 13:56:24 +05:00
parent 860ec7fea8
commit 51819e9d4a
11 changed files with 62 additions and 65 deletions

View File

@@ -22,14 +22,8 @@ import Database from "..";
class Conflicts {
constructor(private readonly db: Database) {}
async recalculate() {
if (this.db.notes.conflicted.length <= 0) {
await this.db.storage().write("hasConflicts", false);
}
}
check() {
return this.db.storage().read("hasConflicts");
async check() {
return (await this.db.notes.conflicted.count()) > 0;
}
throw() {

View File

@@ -218,7 +218,6 @@ class Sync {
async init(isForceSync?: boolean) {
await this.checkConnection();
await this.conflicts.recalculate();
if (await this.conflicts.check()) {
this.conflicts.throw();
}

View File

@@ -33,6 +33,7 @@ import { Output } from "../interfaces";
import { Attachment } from "../types";
import Database from "../api";
import { SQLCollection } from "../database/sql-collection";
import { isFalse } from "../database";
export class Attachments implements ICollection {
name = "attachments";
@@ -283,16 +284,9 @@ export class Attachments implements ICollection {
}
async attachment(hashOrId: string): Promise<Attachment | undefined> {
return await this.db
.sql()
.selectFrom("attachments")
.selectAll()
.where((eb) =>
eb.or([eb("id", "==", hashOrId), eb("hash", "==", hashOrId)])
)
.where("deleted", "is", null)
.$narrowType<Attachment>()
.executeTakeFirst();
return this.all.find((eb) =>
eb.or([eb("id", "==", hashOrId), eb("hash", "==", hashOrId)])
);
}
markAsUploaded(id: string) {
@@ -372,9 +366,7 @@ export class Attachments implements ICollection {
get pending() {
return this.collection.createFilter<Attachment>((qb) =>
qb.where((eb) =>
eb.or([eb("dateUploaded", "is", null), eb("dateUploaded", "<=", 0)])
)
qb.where(isFalse("dateUploaded"))
);
}
@@ -419,9 +411,11 @@ export class Attachments implements ICollection {
// );
// }
// get all() {
// return this.collection.items();
// }
get all() {
return this.collection.createFilter<Attachment>((qb) =>
qb.where(isFalse("deleted"))
);
}
private async encryptKey(key: SerializedKey) {
const encryptionKey = await this._getEncryptionKey();

View File

@@ -23,6 +23,7 @@ import { Color } from "../types";
import Database from "../api";
import { Tags } from "./tags";
import { SQLCollection } from "../database/sql-collection";
import { isFalse } from "../database";
export const DefaultColors: Record<string, string> = {
red: "#f44336",
@@ -87,7 +88,7 @@ export class Colors implements ICollection {
get all() {
return this.collection.createFilter<Color>((qb) =>
qb.where("deleted", "is", null)
qb.where(isFalse("deleted"))
);
}

View File

@@ -22,6 +22,7 @@ import Database from "../api";
import { Notebook, TrashOrItem, isTrashItem } from "../types";
import { ICollection } from "./collection";
import { SQLCollection } from "../database/sql-collection";
import { isFalse } from "../database";
export class Notebooks implements ICollection {
name = "notebooks";
@@ -79,15 +80,15 @@ export class Notebooks implements ICollection {
get all() {
return this.collection.createFilter<Notebook>((qb) =>
qb.where("dateDeleted", "is", null).where("deleted", "is", null)
qb.where(isFalse("dateDeleted")).where(isFalse("deleted"))
);
}
get pinned() {
return this.collection.createFilter<Notebook>((qb) =>
qb
.where("dateDeleted", "is", null)
.where("deleted", "is", null)
.where(isFalse("dateDeleted"))
.where(isFalse("deleted"))
.where("pinned", "==", true)
);
}

View File

@@ -30,6 +30,7 @@ import Database from "../api";
import { ICollection } from "./collection";
import { NoteContent } from "./session-content";
import { SQLCollection } from "../database/sql-collection";
import { isFalse } from "../database";
type ExportOptions = {
format: "html" | "md" | "txt" | "md-frontmatter";
@@ -149,7 +150,7 @@ export class Notes implements ICollection {
get all() {
return this.collection.createFilter<Note>((qb) =>
qb.where("dateDeleted", "is", null).where("deleted", "is", null)
qb.where(isFalse("dateDeleted")).where(isFalse("deleted"))
);
}
@@ -166,21 +167,26 @@ export class Notes implements ICollection {
get pinned() {
return this.collection.createFilter<Note>((qb) =>
qb
.where("dateDeleted", "is", null)
.where("deleted", "is", null)
.where(isFalse("dateDeleted"))
.where(isFalse("deleted"))
.where("pinned", "==", true)
);
}
// get conflicted() {
// return this.all.filter((item) => item.conflicted === true);
// }
get conflicted() {
return this.collection.createFilter<Note>((qb) =>
qb
.where(isFalse("dateDeleted"))
.where(isFalse("deleted"))
.where("conflicted", "==", true)
);
}
get favorites() {
return this.collection.createFilter<Note>((qb) =>
qb
.where("dateDeleted", "is", null)
.where("deleted", "is", null)
.where(isFalse("dateDeleted"))
.where(isFalse("deleted"))
.where("favorite", "==", true)
);
}
@@ -188,8 +194,8 @@ export class Notes implements ICollection {
get locked() {
return this.collection.createFilter<Note>((qb) =>
qb
.where("dateDeleted", "is", null)
.where("deleted", "is", null)
.where(isFalse("dateDeleted"))
.where(isFalse("deleted"))
.where("locked", "==", true)
);
}

View File

@@ -28,7 +28,7 @@ import {
} from "../types";
import Database from "../api";
import { SQLCollection } from "../database/sql-collection";
import { DatabaseAccessor, DatabaseSchema } from "../database";
import { DatabaseAccessor, DatabaseSchema, isFalse } from "../database";
import { SelectQueryBuilder } from "kysely";
export class Relations implements ICollection {
@@ -171,7 +171,7 @@ class RelationsArray<TType extends keyof RelatableTable> {
.$if(limit !== undefined && limit > 0, (b) => b.limit(limit!))
.selectAll()
// TODO: check if we need to index deleted field.
.where("deleted", "is", null)
.where(isFalse("deleted"))
.execute();
return items as unknown as ItemMap[TType][];
}

View File

@@ -18,6 +18,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import Database from "../api";
import { isFalse } from "../database";
import { SQLCollection } from "../database/sql-collection";
import { Shortcut } from "../types";
import { ICollection } from "./collection";
@@ -82,7 +83,7 @@ export class Shortcuts implements ICollection {
get all() {
return this.collection.createFilter<Shortcut>((qb) =>
qb.where("deleted", "is", null)
qb.where(isFalse("deleted"))
);
}

View File

@@ -22,6 +22,7 @@ import { Tag } from "../types";
import Database from "../api";
import { ICollection } from "./collection";
import { SQLCollection } from "../database/sql-collection";
import { isFalse } from "../database";
export class Tags implements ICollection {
name = "tags";
@@ -71,7 +72,7 @@ export class Tags implements ICollection {
get all() {
return this.collection.createFilter<Tag>((qb) =>
qb.where("deleted", "is", null)
qb.where(isFalse("deleted"))
);
}

View File

@@ -93,7 +93,7 @@ export class SQLCollection<
.selectFrom<keyof DatabaseSchema>(this.type)
.select((a) => a.fn.count<number>("id").as("count"))
.where("id", "==", id)
.where("deleted", "is", null)
.where(isFalse("deleted"))
.limit(1)
.executeTakeFirst()) || {};
@@ -105,7 +105,7 @@ export class SQLCollection<
(await this.db()
.selectFrom<keyof DatabaseSchema>(this.type)
.select((a) => a.fn.count<number>("id").as("count"))
.where("deleted", "is", null)
.where(isFalse("deleted"))
.executeTakeFirst()) || {};
return count || 0;
}
@@ -132,6 +132,7 @@ export class SQLCollection<
return array;
}, [] as SQLiteItem<T>[]);
if (entries.length <= 0) return;
await this.db()
.replaceInto<keyof DatabaseSchema>(this.type)
.values(entries)
@@ -153,9 +154,9 @@ export class SQLCollection<
const ids = await this.db()
.selectFrom<keyof DatabaseSchema>(this.type)
.select("id")
.where("deleted", "is", null)
.where(isFalse("deleted"))
.$if(this.type === "notes" || this.type === "notebooks", (eb) =>
eb.where("dateDeleted", "is", null)
eb.where(isFalse("dateDeleted"))
)
.orderBy(sortOptions.sortBy, sortOptions.sortDirection)
.execute();
@@ -192,11 +193,7 @@ export class SQLCollection<
.selectAll()
.orderBy("dateModified", "asc")
.$if(after > 0, (eb) =>
eb
.where("dateModified", ">", after)
.where((eb) =>
eb.or([eb("synced", "is", null), eb("synced", "==", false)])
)
eb.where("dateModified", ">", after).where(isFalse("synced"))
)
.$if(this.type === "attachments", (eb) =>
eb.where("dateUploaded", ">", 0)
@@ -217,7 +214,7 @@ export class SQLCollection<
const rows = await this.db()
.selectFrom<keyof DatabaseSchema>(this.type)
.where(isFalse("deleted"))
.orderBy("dateModified desc")
.orderBy("dateCreated desc")
.selectAll()
.offset(index)
.limit(chunkSize)
@@ -306,7 +303,7 @@ export class FilteredSelector<T> {
while (true) {
const rows = await this.filter
.selectAll()
.orderBy("dateModified asc")
.orderBy("dateCreated asc")
.offset(index)
.limit(this.batchSize)
.execute();