Skip to content

Commit

Permalink
fix: makse has wildcard path a getter
Browse files Browse the repository at this point in the history
  • Loading branch information
psteinroe committed Jul 14, 2024
1 parent e445d58 commit 539020b
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 9 deletions.
2 changes: 1 addition & 1 deletion packages/postgrest-core/src/mutate-item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ export const mutateItem = async <KeyType, Type extends Record<string, unknown>>(
const orderBy = key.orderByKey
? parseOrderByKey(key.orderByKey)
: undefined;
if (key.isHead === true || filter.hasWildcardPath()) {
if (key.isHead === true || filter.hasWildcardPath) {
// we cannot know whether the new item after mutating still has all paths required for a query if it contains a wildcard,
// because we do not know what columns a table has. we must always revalidate then.
mutations.push(revalidate(k));
Expand Down
5 changes: 4 additions & 1 deletion packages/postgrest-core/src/postgrest-filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ export class PostgrestFilter<Result extends Record<string, unknown>> {
this.params.filters,
this.params.paths,
);
this._hasWildcardPath = this.params.paths.some((p) =>
p.declaration.endsWith('*'),
);
}

public static fromQuery(query: string, opts?: PostgrestQueryParserOptions) {
Expand Down Expand Up @@ -81,7 +84,7 @@ export class PostgrestFilter<Result extends Record<string, unknown>> {
return this._filtersFn(obj);
}

hasWildcardPath(): boolean {
get hasWildcardPath(): boolean {
if (typeof this._hasWildcardPath === 'undefined') {
this._hasWildcardPath = this.params.paths.some((p) =>
p.declaration.endsWith('*'),
Expand Down
2 changes: 1 addition & 1 deletion packages/postgrest-core/src/upsert-item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ export const upsertItem = async <KeyType, Type extends Record<string, unknown>>(
? parseOrderByKey(key.orderByKey)
: undefined;

if (key.isHead === true || filter.hasWildcardPath()) {
if (key.isHead === true || filter.hasWildcardPath) {
// we cannot know whether the new item after merging still has all paths required for a query if it contains a wildcard,
// because we do not know what columns a table has. we must always revalidate then.
mutations.push(revalidate(k));
Expand Down
6 changes: 3 additions & 3 deletions packages/postgrest-core/tests/mutate-item.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ const mutateFnMock = async (
},
getPostgrestFilter() {
return {
hasWildcardPath(): boolean {
get hasWildcardPath(): boolean {
return typeof postgrestFilter.hasWildcardPath === 'boolean'
? postgrestFilter.hasWildcardPath
: false;
Expand Down Expand Up @@ -137,7 +137,7 @@ const mutateRelationMock = async (
},
getPostgrestFilter() {
return {
hasWildcardPath(): boolean {
get hasWildcardPath(): boolean {
return false;
},
denormalize<RelationType>(obj: RelationType): RelationType {
Expand Down Expand Up @@ -205,7 +205,7 @@ const mutateFnResult = async (
},
getPostgrestFilter() {
return {
hasWildcardPath(): boolean {
get hasWildcardPath(): boolean {
return typeof postgrestFilter.hasWildcardPath === 'boolean'
? postgrestFilter.hasWildcardPath
: false;
Expand Down
13 changes: 13 additions & 0 deletions packages/postgrest-core/tests/postgrest-filter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,19 @@ describe('PostgrestFilter', () => {
).toEqual(true);
});

it('should set has wildcard paths', () => {
expect(
PostgrestFilter.fromQuery(
new PostgrestParser(
createClient('https://localhost', 'test')
.from('contact')
.select('some,*')
.eq('username', 'test'),
).queryKey,
).hasWildcardPath,
).toEqual(true);
});

describe('.transform', () => {
it('should transform nested one-to-many relations', () => {
expect(
Expand Down
6 changes: 3 additions & 3 deletions packages/postgrest-core/tests/upsert-item.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ const mutateFnMock = async (
},
getPostgrestFilter() {
return {
hasWildcardPath(): boolean {
get hasWildcardPath(): boolean {
return typeof postgrestFilter.hasWildcardPath === 'boolean'
? postgrestFilter.hasWildcardPath
: false;
Expand Down Expand Up @@ -134,7 +134,7 @@ const mutateRelationMock = async (
},
getPostgrestFilter() {
return {
hasWildcardPath(): boolean {
get hasWildcardPath(): boolean {
return false;
},
denormalize<RelationType>(obj: RelationType): RelationType {
Expand Down Expand Up @@ -202,7 +202,7 @@ const mutateFnResult = async (
},
getPostgrestFilter() {
return {
hasWildcardPath(): boolean {
get hasWildcardPath(): boolean {
return typeof postgrestFilter.hasWildcardPath === 'boolean'
? postgrestFilter.hasWildcardPath
: false;
Expand Down

0 comments on commit 539020b

Please sign in to comment.