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

Commit

Permalink
fix: collection deletes are now processed correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
goknsh committed Feb 2, 2023
1 parent 3b63bc7 commit 40e97e3
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 14 deletions.
5 changes: 5 additions & 0 deletions .changeset/little-bikes-cheer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte-query-pocketbase': patch
---

fix: collection deletes are now processed correctly
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# svelte-query-pocketbase

TanStack Query Svelte store wrappers around Pocketbase Realtime.
TanStack Query wrappers around Pocketbase Realtime for Svelte

## Installation

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "svelte-query-pocketbase",
"description": "TanStack Query Svelte store wrappers around Pocketbase Realtime",
"description": "TanStack Query wrappers around Pocketbase Realtime for Svelte",
"version": "0.0.1-beta.8",
"type": "module",
"scripts": {
Expand Down
4 changes: 2 additions & 2 deletions src/lib/queries/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ const collectionStoreCallback = async <
data = produce(data, (draft) => {
let updateIndex = draft.findIndex((item) => item.id === expandedRecord.id);
if (updateIndex !== -1) {
if (new Date(expandedRecord.updated) > new Date(draft[updateIndex].updated)) {
if (new Date(expandedRecord.updated) >= new Date(draft[updateIndex].updated)) {
draft[updateIndex] = expandedRecord as Draft<T>;
} else {
actionIgnored = true;
Expand All @@ -69,7 +69,7 @@ const collectionStoreCallback = async <
data = produce(data, (draft) => {
let deleteIndex = draft.findIndex((item) => item.id === expandedRecord.id);
if (deleteIndex !== -1) {
if (new Date(expandedRecord.updated) > new Date(draft[deleteIndex].updated)) {
if (new Date(expandedRecord.updated) >= new Date(draft[deleteIndex].updated)) {
draft.splice(deleteIndex, 1);
} else {
actionIgnored = true;
Expand Down
7 changes: 4 additions & 3 deletions src/lib/queries/infinite-collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ const infiniteCollectionStoreCallback = async <
allItems = produce(allItems, (draft) => {
updateIndex = draft.findIndex((item) => item.id === expandedRecord.id);
if (updateIndex !== -1) {
if (new Date(expandedRecord.updated) > new Date(draft[updateIndex].updated)) {
if (new Date(expandedRecord.updated) >= new Date(draft[updateIndex].updated)) {
draft[updateIndex] = expandedRecord as Draft<T>;
} else {
actionIgnored = true;
Expand All @@ -79,7 +79,7 @@ const infiniteCollectionStoreCallback = async <
case 'delete':
allItems = produce(allItems, (draft) => {
deleteIndex = draft.findIndex((item) => item.id === expandedRecord.id);
if (new Date(expandedRecord.updated) > new Date(draft[deleteIndex].updated)) {
if (new Date(expandedRecord.updated) >= new Date(draft[deleteIndex].updated)) {
draft.splice(deleteIndex, 1);
} else {
actionIgnored = true;
Expand Down Expand Up @@ -165,9 +165,10 @@ const infiniteCollectionStoreCallback = async <

if (
totalItemsWeHave === totalItemsInDatabase &&
data.pages.length > 1 &&
data.pages[data.pages.length - 1].items.length === 0
) {
// delete empty pages only if we have all items in the database
// delete empty pages only if we have all items in the database, and there is at least one page
data = produce(data, (draft) => {
draft.pages.pop();
draft.pageParams.pop();
Expand Down
8 changes: 1 addition & 7 deletions src/routes/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,7 @@
const pocketbase = new Pocketbase('https://voel.local');
const query = createInfiniteCollectionQuery(pocketbase.collection('test'), {
page: 2,
perPage: 1,
queryParams: { sort: '+name' },
sortFunction: (a, b) => (a.name < b.name ? -1 : a.name > b.name ? 1 : 0)
// filterFunction: (item) => item.name.includes('bunbee')
});
const query = createInfiniteCollectionQuery(pocketbase.collection('test'));
// onMount(() => {
// setTimeout(() => {
Expand Down

0 comments on commit 40e97e3

Please sign in to comment.