Skip to content

Commit c97010b

Browse files
Merge branch 'main' into upgrade-nextra-doc-to-v3.2
2 parents 16f4895 + 2349efd commit c97010b

File tree

13 files changed

+140
-17
lines changed

13 files changed

+140
-17
lines changed

docs/pages/storage/queries.mdx

+1-1
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ function Page() {
149149
</Tabs.Tab>
150150
</Tabs>
151151

152-
## `useDirectoryUrls`
152+
## `useDirectoryFileUrls`
153153

154154
Convenience hook that returns the files in a directory similar to `useDirectory` but adds the `url` for each.
155155

packages/postgrest-core/CHANGELOG.md

+12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
# @supabase-cache-helpers/postgrest-core
22

3+
## 0.8.3
4+
5+
### Patch Changes
6+
7+
- b06405b: fix: make delete work on single query
8+
9+
## 0.8.2
10+
11+
### Patch Changes
12+
13+
- bfdf3ac: fix update fetcher when primary key value is equal to false or 0
14+
315
## 0.8.1
416

517
### Patch Changes

packages/postgrest-core/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@supabase-cache-helpers/postgrest-core",
3-
"version": "0.8.1",
3+
"version": "0.8.3",
44
"type": "module",
55
"main": "./dist/index.js",
66
"source": "./src/index.ts",

packages/postgrest-core/src/delete-item.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,16 @@ export const deleteItem = async <KeyType, Type extends Record<string, unknown>>(
116116
} else if (isAnyPostgrestResponse<Type>(currentData)) {
117117
const { data } = currentData;
118118
if (!Array.isArray(data)) {
119-
return { data: null };
119+
if (
120+
data &&
121+
op.primaryKeys.some(
122+
(pk) => transformedInput[pk] !== data[pk],
123+
)
124+
) {
125+
return currentData;
126+
} else {
127+
return { data: null };
128+
}
120129
}
121130

122131
const newData = filterByPks(

packages/postgrest-core/src/update-fetcher.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ export const buildUpdateFetcher =
5757
let filterBuilder = qb.update(payload as any, opts); // todo fix type;
5858
for (const key of primaryKeys) {
5959
const value = input[key];
60-
if (!value)
60+
// The value can be 0 or false, so we need to check if it's null or undefined instead of falsy
61+
if (value === null || value === undefined)
6162
throw new Error(`Missing value for primary key ${String(key)}`);
6263
filterBuilder = filterBuilder.eq(key as string, value);
6364
}

packages/postgrest-core/tests/delete-item.spec.ts

+12
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,18 @@ describe('deleteItem', () => {
286286
});
287287
});
288288

289+
it('should not change data if its single and primary keys do not match', async () => {
290+
expect(
291+
await mutateFnResult(
292+
{ id_1: '0', id_2: '1', value: 'a' },
293+
{},
294+
{ data: { id_1: '0', id_2: '0', value: 'test' } },
295+
),
296+
).toMatchObject({
297+
data: { id_1: '0', id_2: '0', value: 'test' },
298+
});
299+
});
300+
289301
it('should delete item from cached array and subtract count', async () => {
290302
expect(
291303
await mutateFnResult(

packages/postgrest-core/tests/update-fetcher.spec.ts

+36
Original file line numberDiff line numberDiff line change
@@ -147,4 +147,40 @@ describe('update', () => {
147147
.maybeSingle();
148148
expect(data?.username).toEqual(`${testRunPrefix}-username-4`);
149149
});
150+
151+
it('should not throw error when primary key value is false', async () => {
152+
const qb = {
153+
update: vi.fn().mockReturnThis(),
154+
eq: vi.fn().mockReturnThis(),
155+
select: vi.fn().mockReturnThis(),
156+
throwOnError: vi.fn().mockReturnThis(),
157+
single: vi.fn().mockResolvedValue({ data: null }),
158+
};
159+
160+
await expect(
161+
buildUpdateFetcher(qb as any, ['is_active'], {
162+
stripPrimaryKeys: false,
163+
queriesForTable: () => [],
164+
})({ is_active: false, username: 'testuser' }),
165+
).resolves.not.toThrow();
166+
expect(qb.eq).toHaveBeenCalledWith('is_active', false);
167+
});
168+
169+
it('should not throw error when primary key value is 0', async () => {
170+
const qb = {
171+
update: vi.fn().mockReturnThis(),
172+
eq: vi.fn().mockReturnThis(),
173+
select: vi.fn().mockReturnThis(),
174+
throwOnError: vi.fn().mockReturnThis(),
175+
single: vi.fn().mockResolvedValue({ data: null }),
176+
};
177+
178+
await expect(
179+
buildUpdateFetcher(qb as any, ['id'], {
180+
stripPrimaryKeys: false,
181+
queriesForTable: () => [],
182+
})({ id: 0, username: 'testuser' }),
183+
).resolves.not.toThrow();
184+
expect(qb.eq).toHaveBeenCalledWith('id', 0);
185+
});
150186
});

packages/postgrest-react-query/CHANGELOG.md

+20
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,25 @@
11
# @supabase-cache-helpers/postgrest-react-query
22

3+
## 1.11.2
4+
5+
### Patch Changes
6+
7+
- Updated dependencies [b06405b]
8+
- @supabase-cache-helpers/postgrest-core@0.8.3
9+
10+
## 1.11.1
11+
12+
### Patch Changes
13+
14+
- Updated dependencies [bfdf3ac]
15+
- @supabase-cache-helpers/postgrest-core@0.8.2
16+
17+
## 1.11.0
18+
19+
### Minor Changes
20+
21+
- 74571ce: fix: retain union type in useQuery result
22+
323
## 1.10.1
424

525
### Patch Changes

packages/postgrest-react-query/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@supabase-cache-helpers/postgrest-react-query",
3-
"version": "1.10.1",
3+
"version": "1.11.2",
44
"author": "Philipp Steinrötter <[email protected]>",
55
"homepage": "https://supabase-cache-helpers.vercel.app",
66
"bugs": {

packages/postgrest-react-query/src/query/use-query.ts

+28-9
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,18 @@ import {
1313

1414
import { buildQueryOpts } from './build-query-opts';
1515

16+
/**
17+
* Applies Omit over a union, while preserving its union-ness.
18+
*/
19+
type DistributiveOmit<T, K extends keyof any> = T extends any
20+
? Omit<T, K>
21+
: never;
22+
1623
/**
1724
* Represents the return value of the `useQuery` hook when `query` is expected to return
1825
* a single row.
1926
*/
20-
export type UseQuerySingleReturn<Result> = Omit<
27+
export type UseQuerySingleReturn<Result> = DistributiveOmit<
2128
UseReactQueryResult<PostgrestSingleResponse<Result>['data'], PostgrestError>,
2229
'refetch'
2330
> &
@@ -31,7 +38,7 @@ export type UseQuerySingleReturn<Result> = Omit<
3138
* Represents the return value of the `useQuery` hook when `query` is expected to return
3239
* either a single row or an empty response.
3340
*/
34-
export type UseQueryMaybeSingleReturn<Result> = Omit<
41+
export type UseQueryMaybeSingleReturn<Result> = DistributiveOmit<
3542
UseReactQueryResult<
3643
PostgrestMaybeSingleResponse<Result>['data'],
3744
PostgrestError
@@ -48,7 +55,7 @@ export type UseQueryMaybeSingleReturn<Result> = Omit<
4855
* Represents the return value of the `useQuery` hook when `query` is expected to return
4956
* one or more rows.
5057
*/
51-
export type UseQueryReturn<Result> = Omit<
58+
export type UseQueryReturn<Result> = DistributiveOmit<
5259
UseReactQueryResult<PostgrestResponse<Result>['data'], PostgrestError>,
5360
'refetch'
5461
> &
@@ -62,7 +69,7 @@ export type UseQueryReturn<Result> = Omit<
6269
* Represents the return value of the `useQuery` hook when the type of the query response
6370
* is not known.
6471
*/
65-
export type UseQueryAnyReturn<Result> = Omit<
72+
export type UseQueryAnyReturn<Result> = DistributiveOmit<
6673
UseReactQueryResult<AnyPostgrestResponse<Result>['data'], PostgrestError>,
6774
'refetch'
6875
> &
@@ -131,12 +138,24 @@ function useQuery<Result>(
131138
'queryKey' | 'queryFn'
132139
>,
133140
): UseQueryAnyReturn<Result> {
134-
const { data, ...rest } = useReactQuery<
135-
AnyPostgrestResponse<Result>,
136-
PostgrestError
137-
>(buildQueryOpts<Result>(query, config));
141+
const result = useReactQuery<AnyPostgrestResponse<Result>, PostgrestError>(
142+
buildQueryOpts<Result>(query, config),
143+
);
144+
145+
// isPending and isLoadingError are the only cases in which no data is present
146+
if (result.isPending || result.isLoadingError) {
147+
return {
148+
...result,
149+
data: undefined,
150+
count: null,
151+
};
152+
}
138153

139-
return { data: data?.data, count: data?.count ?? null, ...rest };
154+
return {
155+
...result,
156+
data: result.data?.data,
157+
count: result.data?.count,
158+
};
140159
}
141160

142161
export { useQuery };

packages/postgrest-swr/CHANGELOG.md

+14
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,19 @@
11
# @supabase-cache-helpers/postgrest-swr
22

3+
## 1.10.3
4+
5+
### Patch Changes
6+
7+
- Updated dependencies [b06405b]
8+
- @supabase-cache-helpers/postgrest-core@0.8.3
9+
10+
## 1.10.2
11+
12+
### Patch Changes
13+
14+
- Updated dependencies [bfdf3ac]
15+
- @supabase-cache-helpers/postgrest-core@0.8.2
16+
317
## 1.10.1
418

519
### Patch Changes

packages/postgrest-swr/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@supabase-cache-helpers/postgrest-swr",
3-
"version": "1.10.1",
3+
"version": "1.10.3",
44
"author": "Philipp Steinrötter <[email protected]>",
55
"homepage": "https://supabase-cache-helpers.vercel.app",
66
"bugs": {

turbo.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
"outputs": ["dist/**", ".next/**"]
88
},
99
"test": {
10-
"outputs": ["coverage/**"],
11-
"dependsOn": []
10+
"dependsOn": ["^build"],
11+
"outputs": ["coverage/**"]
1212
},
1313
"clean": {
1414
"outputs": []

0 commit comments

Comments
 (0)