-
-
Notifications
You must be signed in to change notification settings - Fork 162
feat: postgrest 13 add maxaffected in client libraries #619
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
avallete
wants to merge
11
commits into
avallete/psql-436-feedback-request-postgrest-js-and-postgrest-v13-integration
Choose a base branch
from
avallete/psql-372-postgrest-add-maxaffected-in-client-libraries
base: avallete/psql-436-feedback-request-postgrest-js-and-postgrest-v13-integration
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 2 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
9ec17a4
feat: add max-affected method
avallete 8132588
Merge branch 'avallete/psql-436-feedback-request-postgrest-js-and-pos…
avallete 58742a6
Merge branch 'avallete/psql-436-feedback-request-postgrest-js-and-pos…
avallete 5017cd3
chore(review): use postgrest code instead of message
avallete fe3e80f
chore: refactor in favor of Headers API
avallete f39dc34
chore: move maxAffected to PostgrestTransformBuilder
avallete 549abdf
chore: bump ci minimal node to 18
avallete 154af4e
chore: apply pr comment
avallete ff593be
feat: add maxAffected for rpc calls
avallete c5f81be
Merge branch 'avallete/psql-436-feedback-request-postgrest-js-and-pos…
avallete 9ef7ee0
chore: use string based version
avallete File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
export class HeaderManager { | ||
avallete marked this conversation as resolved.
Show resolved
Hide resolved
|
||
private headers: Map<string, Set<string>> = new Map() | ||
|
||
/** | ||
* Create a new HeaderManager, optionally parsing an existing header string | ||
* @param header The header name to manage | ||
* @param existingValue Optional existing header value to parse | ||
*/ | ||
constructor(private readonly header: string, existingValue?: string) { | ||
if (existingValue) { | ||
this.parseHeaderString(existingValue) | ||
} | ||
} | ||
|
||
/** | ||
* Parse an existing header string into the internal Set | ||
* @param headerString The header string to parse | ||
*/ | ||
private parseHeaderString(headerString: string): void { | ||
if (!headerString.trim()) return | ||
|
||
const values = headerString.split(',') | ||
values.forEach((value) => { | ||
const trimmedValue = value.trim() | ||
if (trimmedValue) { | ||
this.add(trimmedValue) | ||
} | ||
}) | ||
} | ||
|
||
/** | ||
* Add a value to the header. If the header doesn't exist, it will be created. | ||
* @param value The value to add | ||
*/ | ||
add(value: string): void { | ||
if (!this.headers.has(this.header)) { | ||
this.headers.set(this.header, new Set()) | ||
} | ||
this.headers.get(this.header)!.add(value) | ||
} | ||
|
||
/** | ||
* Get the formatted string value for the header | ||
*/ | ||
get(): string { | ||
const values = this.headers.get(this.header) | ||
return values ? Array.from(values).join(',') : '' | ||
} | ||
|
||
/** | ||
* Check if the header has a specific value | ||
* @param value The value to check | ||
*/ | ||
has(value: string): boolean { | ||
return this.headers.get(this.header)?.has(value) ?? false | ||
} | ||
|
||
/** | ||
* Clear all values for the header | ||
*/ | ||
clear(): void { | ||
this.headers.delete(this.header) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
import { PostgrestClient } from '../src/index' | ||
import { Database } from './types.override' | ||
import { expectType } from 'tsd' | ||
import { InvalidMethodError } from '../src/PostgrestFilterBuilder' | ||
|
||
const REST_URL_13 = 'http://localhost:3001' | ||
const postgrest13 = new PostgrestClient<Database, { postgrestVersion: 13 }>(REST_URL_13) | ||
const postgrest12 = new PostgrestClient<Database>(REST_URL_13) | ||
|
||
describe('maxAffected', () => { | ||
// Type checking tests | ||
test('maxAffected should show type warning on postgrest 12 clients', async () => { | ||
const resUpdate = await postgrest12 | ||
.from('messages') | ||
.update({ channel_id: 2 }) | ||
.eq('message', 'foo') | ||
.maxAffected(1) | ||
expectType<InvalidMethodError<'maxAffected method only available on postgrest 13+'>>(resUpdate) | ||
}) | ||
test('maxAffected should show type warning on non update / delete', async () => { | ||
const resSelect = await postgrest13.from('messages').select('*').maxAffected(10) | ||
const resInsert = await postgrest13 | ||
.from('messages') | ||
.insert({ message: 'foo', username: 'supabot', channel_id: 1 }) | ||
.maxAffected(10) | ||
const resUpsert = await postgrest13 | ||
.from('messages') | ||
.upsert({ id: 3, message: 'foo', username: 'supabot', channel_id: 2 }) | ||
.maxAffected(10) | ||
const resUpdate = await postgrest13 | ||
.from('messages') | ||
.update({ channel_id: 2 }) | ||
.eq('message', 'foo') | ||
.maxAffected(1) | ||
.select() | ||
const resDelete = await postgrest13 | ||
.from('messages') | ||
.delete() | ||
.eq('message', 'foo') | ||
.maxAffected(1) | ||
.select() | ||
expectType<InvalidMethodError<'maxAffected method only available on update or delete'>>( | ||
resSelect | ||
) | ||
expectType<InvalidMethodError<'maxAffected method only available on update or delete'>>( | ||
resInsert | ||
) | ||
expectType<InvalidMethodError<'maxAffected method only available on update or delete'>>( | ||
resUpsert | ||
) | ||
expectType<InvalidMethodError<'maxAffected method only available on update or delete'>>( | ||
// @ts-expect-error update method shouldn't return an error | ||
resUpdate | ||
) | ||
expectType<InvalidMethodError<'maxAffected method only available on update or delete'>>( | ||
// @ts-expect-error delete method shouldn't return an error | ||
resDelete | ||
) | ||
}) | ||
|
||
// Runtime behavior tests | ||
test('update should fail when maxAffected is exceeded', async () => { | ||
// First create multiple rows | ||
await postgrest13.from('messages').insert([ | ||
{ message: 'test1', username: 'supabot', channel_id: 1 }, | ||
{ message: 'test1', username: 'supabot', channel_id: 1 }, | ||
{ message: 'test1', username: 'supabot', channel_id: 1 }, | ||
]) | ||
|
||
// Try to update all rows with maxAffected=2 | ||
const result = await postgrest13 | ||
.from('messages') | ||
.update({ message: 'updated' }) | ||
.eq('message', 'test1') | ||
.maxAffected(2) | ||
const { error } = result | ||
expect(error).toBeDefined() | ||
expect(error?.message).toBe('Query result exceeds max-affected preference constraint') | ||
avallete marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}) | ||
|
||
test('update should succeed when within maxAffected limit', async () => { | ||
// First create a single row | ||
await postgrest13 | ||
.from('messages') | ||
.insert([{ message: 'test2', username: 'supabot', channel_id: 1 }]) | ||
|
||
// Try to update with maxAffected=2 | ||
const { data, error } = await postgrest13 | ||
.from('messages') | ||
.update({ message: 'updated' }) | ||
.eq('message', 'test2') | ||
.maxAffected(2) | ||
.select() | ||
|
||
expect(error).toBeNull() | ||
expect(data).toHaveLength(1) | ||
expect(data?.[0].message).toBe('updated') | ||
}) | ||
|
||
test('delete should fail when maxAffected is exceeded', async () => { | ||
// First create multiple rows | ||
await postgrest13.from('messages').insert([ | ||
{ message: 'test3', username: 'supabot', channel_id: 1 }, | ||
{ message: 'test3', username: 'supabot', channel_id: 1 }, | ||
{ message: 'test3', username: 'supabot', channel_id: 1 }, | ||
]) | ||
|
||
// Try to delete all rows with maxAffected=2 | ||
const { error } = await postgrest13 | ||
.from('messages') | ||
.delete() | ||
.eq('message', 'test3') | ||
.maxAffected(2) | ||
.select() | ||
|
||
expect(error).toBeDefined() | ||
expect(error?.message).toBe('Query result exceeds max-affected preference constraint') | ||
avallete marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}) | ||
|
||
test('delete should succeed when within maxAffected limit', async () => { | ||
// First create a single row | ||
await postgrest13 | ||
.from('messages') | ||
.insert([{ message: 'test4', username: 'supabot', channel_id: 1 }]) | ||
|
||
// Try to delete with maxAffected=2 | ||
const { data, error } = await postgrest13 | ||
.from('messages') | ||
.delete() | ||
.eq('message', 'test4') | ||
.maxAffected(2) | ||
.select() | ||
|
||
expect(error).toBeNull() | ||
expect(data).toHaveLength(1) | ||
expect(data?.[0].message).toBe('test4') | ||
}) | ||
}) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.