Skip to content
This repository was archived by the owner on Mar 14, 2024. It is now read-only.

Commit 40e97e3

Browse files
committed
fix: collection deletes are now processed correctly
1 parent 3b63bc7 commit 40e97e3

File tree

6 files changed

+14
-14
lines changed

6 files changed

+14
-14
lines changed

.changeset/little-bikes-cheer.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte-query-pocketbase': patch
3+
---
4+
5+
fix: collection deletes are now processed correctly

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# svelte-query-pocketbase
22

3-
TanStack Query Svelte store wrappers around Pocketbase Realtime.
3+
TanStack Query wrappers around Pocketbase Realtime for Svelte
44

55
## Installation
66

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "svelte-query-pocketbase",
3-
"description": "TanStack Query Svelte store wrappers around Pocketbase Realtime",
3+
"description": "TanStack Query wrappers around Pocketbase Realtime for Svelte",
44
"version": "0.0.1-beta.8",
55
"type": "module",
66
"scripts": {

src/lib/queries/collection.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ const collectionStoreCallback = async <
5555
data = produce(data, (draft) => {
5656
let updateIndex = draft.findIndex((item) => item.id === expandedRecord.id);
5757
if (updateIndex !== -1) {
58-
if (new Date(expandedRecord.updated) > new Date(draft[updateIndex].updated)) {
58+
if (new Date(expandedRecord.updated) >= new Date(draft[updateIndex].updated)) {
5959
draft[updateIndex] = expandedRecord as Draft<T>;
6060
} else {
6161
actionIgnored = true;
@@ -69,7 +69,7 @@ const collectionStoreCallback = async <
6969
data = produce(data, (draft) => {
7070
let deleteIndex = draft.findIndex((item) => item.id === expandedRecord.id);
7171
if (deleteIndex !== -1) {
72-
if (new Date(expandedRecord.updated) > new Date(draft[deleteIndex].updated)) {
72+
if (new Date(expandedRecord.updated) >= new Date(draft[deleteIndex].updated)) {
7373
draft.splice(deleteIndex, 1);
7474
} else {
7575
actionIgnored = true;

src/lib/queries/infinite-collection.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ const infiniteCollectionStoreCallback = async <
6666
allItems = produce(allItems, (draft) => {
6767
updateIndex = draft.findIndex((item) => item.id === expandedRecord.id);
6868
if (updateIndex !== -1) {
69-
if (new Date(expandedRecord.updated) > new Date(draft[updateIndex].updated)) {
69+
if (new Date(expandedRecord.updated) >= new Date(draft[updateIndex].updated)) {
7070
draft[updateIndex] = expandedRecord as Draft<T>;
7171
} else {
7272
actionIgnored = true;
@@ -79,7 +79,7 @@ const infiniteCollectionStoreCallback = async <
7979
case 'delete':
8080
allItems = produce(allItems, (draft) => {
8181
deleteIndex = draft.findIndex((item) => item.id === expandedRecord.id);
82-
if (new Date(expandedRecord.updated) > new Date(draft[deleteIndex].updated)) {
82+
if (new Date(expandedRecord.updated) >= new Date(draft[deleteIndex].updated)) {
8383
draft.splice(deleteIndex, 1);
8484
} else {
8585
actionIgnored = true;
@@ -165,9 +165,10 @@ const infiniteCollectionStoreCallback = async <
165165

166166
if (
167167
totalItemsWeHave === totalItemsInDatabase &&
168+
data.pages.length > 1 &&
168169
data.pages[data.pages.length - 1].items.length === 0
169170
) {
170-
// delete empty pages only if we have all items in the database
171+
// delete empty pages only if we have all items in the database, and there is at least one page
171172
data = produce(data, (draft) => {
172173
draft.pages.pop();
173174
draft.pageParams.pop();

src/routes/+page.svelte

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,7 @@
66
77
const pocketbase = new Pocketbase('https://voel.local');
88
9-
const query = createInfiniteCollectionQuery(pocketbase.collection('test'), {
10-
page: 2,
11-
perPage: 1,
12-
queryParams: { sort: '+name' },
13-
sortFunction: (a, b) => (a.name < b.name ? -1 : a.name > b.name ? 1 : 0)
14-
// filterFunction: (item) => item.name.includes('bunbee')
15-
});
9+
const query = createInfiniteCollectionQuery(pocketbase.collection('test'));
1610
1711
// onMount(() => {
1812
// setTimeout(() => {

0 commit comments

Comments
 (0)