Skip to content

There can be more than Notion and Miro. AFFiNE(pronounced [ə‘fain]) is a next-gen knowledge base that brings planning, sorting and creating all together. Privacy first, open-source, customizable and ready to use.

fix(nbstore): fix v1 doc storage timestamp (#10282)

EYHNc39a93e1fdcd
diff --git a/packages/common/nbstore/src/impls/idb/v1/doc.ts b/packages/common/nbstore/src/impls/idb/v1/doc.ts
index 82b88384d1..1fe145c249 100644
--- a/packages/common/nbstore/src/impls/idb/v1/doc.ts
+++ b/packages/common/nbstore/src/impls/idb/v1/doc.ts
@@ -17,6 +17,13 @@ import {
import { getIdConverter } from '../../../utils/id-converter';
import { DocIDBConnection } from './db';
+/**
+ * We use a fixed timestamp in v1 because the v1 should never be changed.
+ * This date is chosen because it is large enough to overwrite some previous error data.
+ * In our sync storage, only a larger timestamp can overwrite smaller one.
+ */
+const CONST_TIMESTAMP = new Date(1893456000000);
+
/**
* @deprecated readonly
*/
@@ -45,7 +52,14 @@ export class IndexedDBV1DocStorage extends DocStorageBase {
return null;
}
const oldId = (await this.getIdConverter()).newIdToOldId(docId);
- return this.rawGetDoc(oldId);
+ const rawDoc = await this.rawGetDoc(oldId);
+ return rawDoc
+ ? {
+ docId: rawDoc.docId,
+ bin: rawDoc.bin,
+ timestamp: CONST_TIMESTAMP,
+ }
+ : null;
}
protected override async getDocSnapshot() {
@@ -54,7 +68,7 @@ export class IndexedDBV1DocStorage extends DocStorageBase {
override async pushDocUpdate(update: DocUpdate) {
// no more writes to old db
- return { docId: update.docId, timestamp: new Date() };
+ return { docId: update.docId, timestamp: CONST_TIMESTAMP };
}
override async deleteDoc(docId: string) {
@@ -110,7 +124,7 @@ export class IndexedDBV1DocStorage extends DocStorageBase {
);
return Object.fromEntries(
- oldIds.map(id => [idConverter.oldIdToNewId(id), new Date(1)])
+ oldIds.map(id => [idConverter.oldIdToNewId(id), CONST_TIMESTAMP])
);
}
diff --git a/packages/common/nbstore/src/impls/sqlite/v1/doc.ts b/packages/common/nbstore/src/impls/sqlite/v1/doc.ts
index 8100912c87..8027feb212 100644
--- a/packages/common/nbstore/src/impls/sqlite/v1/doc.ts
+++ b/packages/common/nbstore/src/impls/sqlite/v1/doc.ts
@@ -9,6 +9,13 @@ import { isEmptyUpdate } from '../../../utils/is-empty-update';
import type { SpaceType } from '../../../utils/universal-id';
import { apis } from './db';
+/**
+ * We use a fixed timestamp in v1 because the v1 should never be changed.
+ * This date is chosen because it is large enough to overwrite some previous error data.
+ * In our sync storage, only a larger timestamp can overwrite smaller one.
+ */
+const CONST_TIMESTAMP = new Date(1893456000000);
+
/**
* @deprecated readonly
*/
@@ -34,7 +41,7 @@ export class SqliteV1DocStorage extends DocStorageBase<{
override async pushDocUpdate(update: DocUpdate) {
// no more writes
- return { docId: update.docId, timestamp: new Date() };
+ return { docId: update.docId, timestamp: CONST_TIMESTAMP };
}
override async getDoc(docId: string) {
@@ -52,7 +59,7 @@ export class SqliteV1DocStorage extends DocStorageBase<{
return {
docId,
bin,
- timestamp: new Date(),
+ timestamp: CONST_TIMESTAMP,
};
}
@@ -69,8 +76,9 @@ export class SqliteV1DocStorage extends DocStorageBase<{
const idConverter = await this.getIdConverter();
return timestamps.reduce(
- (ret, { docId, timestamp }) => {
- ret[idConverter.oldIdToNewId(docId ?? this.options.id)] = timestamp;
+ (ret, { docId }) => {
+ ret[idConverter.oldIdToNewId(docId ?? this.options.id)] =
+ CONST_TIMESTAMP;
return ret;
},
{} as Record<string, Date>
diff --git a/packages/common/nbstore/src/sync/doc/peer.ts b/packages/common/nbstore/src/sync/doc/peer.ts
index b38e7ee483..426498199f 100644
--- a/packages/common/nbstore/src/sync/doc/peer.ts
+++ b/packages/common/nbstore/src/sync/doc/peer.ts
@@ -199,7 +199,8 @@ export class DocSyncPeer {
throwIfAborted(signal);
if (
!this.remote.isReadonly &&
- (pushedClock === null || pushedClock !== clock?.timestamp)
+ clock &&
+ (pushedClock === null || pushedClock !== clock.timestamp)
) {
await this.jobs.pullAndPush(docId, signal);
} else {