Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

core: disable transactions for now

Abdullah Atta811f2305ec8e
diff --git a/packages/core/src/api/index.ts b/packages/core/src/api/index.ts
index 03c9a5044..20815e6bc 100644
--- a/packages/core/src/api/index.ts
+++ b/packages/core/src/api/index.ts
@@ -130,7 +130,7 @@ class Database {
private _sql?: Kysely<DatabaseSchema>;
sql: DatabaseAccessor = () => {
- if (this._transaction) return this._transaction.value;
+ // if (this._transaction) return this._transaction.value;
if (!this._sql)
throw new Error(
@@ -144,25 +144,26 @@ class Database {
private _transaction?: QueueValue<Transaction<DatabaseSchema>>;
transaction = async (
- executor: (tr: Transaction<DatabaseSchema>) => Promise<void>
+ executor: (tr: Kysely<DatabaseSchema>) => Promise<void>
) => {
- if (this._transaction) {
- await executor(this._transaction.use()).finally(() =>
- this._transaction?.discard()
- );
- return;
- }
-
- return this.sql()
- .transaction()
- .execute(async (tr) => {
- this._transaction = new QueueValue(
- tr,
- () => (this._transaction = undefined)
- );
- await executor(this._transaction.use());
- })
- .finally(() => this._transaction?.discard());
+ await executor(this.sql());
+ // if (this._transaction) {
+ // await executor(this._transaction.use()).finally(() =>
+ // this._transaction?.discard()
+ // );
+ // return;
+ // }
+
+ // return this.sql()
+ // .transaction()
+ // .execute(async (tr) => {
+ // this._transaction = new QueueValue(
+ // tr,
+ // () => (this._transaction = undefined)
+ // );
+ // await executor(this._transaction.use());
+ // })
+ // .finally(() => this._transaction?.discard());
};
options!: Options;
diff --git a/packages/core/src/database/sql-cached-collection.ts b/packages/core/src/database/sql-cached-collection.ts
index 0a898cf8c..c747b3364 100644
--- a/packages/core/src/database/sql-cached-collection.ts
+++ b/packages/core/src/database/sql-cached-collection.ts
@@ -21,7 +21,7 @@ import { MaybeDeletedItem, isDeleted } from "../types";
import EventManager from "../utils/event-manager";
import { DatabaseAccessor, DatabaseCollection, DatabaseSchema } from ".";
import { SQLCollection } from "./sql-collection";
-import { Transaction } from "kysely";
+import { Kysely, Transaction } from "kysely";
import { Sanitizer } from "./sanitizer";
export class SQLCachedCollection<
@@ -36,7 +36,7 @@ export class SQLCachedCollection<
constructor(
sql: DatabaseAccessor,
startTransaction: (
- executor: (tr: Transaction<DatabaseSchema>) => Promise<void>
+ executor: (tr: Kysely<DatabaseSchema>) => Promise<void>
) => Promise<void>,
type: TCollectionType,
eventManager: EventManager,
diff --git a/packages/core/src/database/sql-collection.ts b/packages/core/src/database/sql-collection.ts
index 88df76639..35481a46c 100644
--- a/packages/core/src/database/sql-collection.ts
+++ b/packages/core/src/database/sql-collection.ts
@@ -41,6 +41,7 @@ import {
AnyColumn,
AnyColumnWithTable,
ExpressionOrFactory,
+ Kysely,
SelectQueryBuilder,
SqlBool,
Transaction,
@@ -72,8 +73,8 @@ export class SQLCollection<
{
constructor(
private readonly db: DatabaseAccessor,
- private readonly startTransaction: (
- executor: (tr: Transaction<DatabaseSchema>) => Promise<void>
+ _startTransaction: (
+ executor: (tr: Kysely<DatabaseSchema>) => Promise<void>
) => Promise<void>,
private readonly type: TCollectionType,
private readonly eventManager: EventManager,
@@ -113,21 +114,23 @@ export class SQLCollection<
}
async softDelete(ids: string[]) {
- await this.startTransaction(async (tx) => {
- for (const chunk of toChunks(ids, MAX_SQL_PARAMETERS)) {
- await tx
- .replaceInto<keyof DatabaseSchema>(this.type)
- .values(
- chunk.map((id) => ({
- id,
- deleted: true,
- dateModified: Date.now(),
- synced: false
- }))
- )
- .execute();
- }
- });
+ await this.db()
+ .transaction()
+ .execute(async (tx) => {
+ for (const chunk of toChunks(ids, MAX_SQL_PARAMETERS)) {
+ await tx
+ .replaceInto<keyof DatabaseSchema>(this.type)
+ .values(
+ chunk.map((id) => ({
+ id,
+ deleted: true,
+ dateModified: Date.now(),
+ synced: false
+ }))
+ )
+ .execute();
+ }
+ });
this.eventManager.publish(EVENTS.databaseUpdated, <DeleteEvent>{
type: "softDelete",
collection: this.type,
@@ -138,14 +141,16 @@ export class SQLCollection<
async delete(ids: string[]) {
if (ids.length <= 0) return;
- await this.startTransaction(async (tx) => {
- for (const chunk of toChunks(ids, MAX_SQL_PARAMETERS)) {
- await tx
- .deleteFrom<keyof DatabaseSchema>(this.type)
- .where("id", "in", chunk)
- .execute();
- }
- });
+ await this.db()
+ .transaction()
+ .execute(async (tx) => {
+ for (const chunk of toChunks(ids, MAX_SQL_PARAMETERS)) {
+ await tx
+ .deleteFrom<keyof DatabaseSchema>(this.type)
+ .where("id", "in", chunk)
+ .execute();
+ }
+ });
this.eventManager.publish(EVENTS.databaseUpdated, <DeleteEvent>{
type: "delete",
collection: this.type,
@@ -206,14 +211,16 @@ export class SQLCollection<
if (entries.length <= 0) return [];
- await this.startTransaction(async (tx) => {
- for (const chunk of toChunks(entries, MAX_SQL_PARAMETERS)) {
- await tx
- .replaceInto<keyof DatabaseSchema>(this.type)
- .values(chunk)
- .execute();
- }
- });
+ await this.db()
+ .transaction()
+ .execute(async (tx) => {
+ for (const chunk of toChunks(entries, MAX_SQL_PARAMETERS)) {
+ await tx
+ .replaceInto<keyof DatabaseSchema>(this.type)
+ .values(chunk)
+ .execute();
+ }
+ });
return entries;
}
@@ -224,19 +231,21 @@ export class SQLCollection<
) {
if (!this.sanitizer.sanitize(this.type, partial)) return;
- await this.startTransaction(async (tx) => {
- for (const chunk of toChunks(ids, MAX_SQL_PARAMETERS)) {
- await tx
- .updateTable<keyof DatabaseSchema>(this.type)
- .where("id", "in", chunk)
- .set({
- ...partial,
- dateModified: Date.now(),
- synced: partial.synced || false
- })
- .execute();
- }
- });
+ await this.db()
+ .transaction()
+ .execute(async (tx) => {
+ for (const chunk of toChunks(ids, MAX_SQL_PARAMETERS)) {
+ await tx
+ .updateTable<keyof DatabaseSchema>(this.type)
+ .where("id", "in", chunk)
+ .set({
+ ...partial,
+ dateModified: Date.now(),
+ synced: partial.synced || false
+ })
+ .execute();
+ }
+ });
if (options.sendEvent) {
this.eventManager.publish(EVENTS.databaseUpdated, <UpdateEvent>{
type: "update",