Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/commands/records/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import { requireAuth } from '../../lib/credentials.js';
import { handleError, getRootOpts, CLIError } from '../../lib/errors.js';
import { outputJson, outputSuccess } from '../../lib/output.js';
import { trackCommandUsage } from '../../lib/command-telemetry.js';
import { schemaProfileHeaders } from './profile.js';

export function registerRecordsCreateCommand(recordsCmd: Command): void {
recordsCmd
.command('create <table>')
.description('Create record(s) in a table')
.option('--data <json>', 'JSON data to insert (object or array of objects)')
.option('--schema <name>', 'Schema to target (default: public)')
.action(async (table: string, opts, cmd) => {
const { json } = getRootOpts(cmd);
try {
Expand All @@ -32,6 +34,7 @@ export function registerRecordsCreateCommand(recordsCmd: Command): void {
{
method: 'POST',
body: JSON.stringify(records),
headers: schemaProfileHeaders('write', opts.schema),
},
);

Expand Down
4 changes: 3 additions & 1 deletion src/commands/records/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import { requireAuth } from '../../lib/credentials.js';
import { handleError, getRootOpts, CLIError } from '../../lib/errors.js';
import { outputJson, outputSuccess } from '../../lib/output.js';
import { trackCommandUsage } from '../../lib/command-telemetry.js';
import { schemaProfileHeaders } from './profile.js';

export function registerRecordsDeleteCommand(recordsCmd: Command): void {
recordsCmd
.command('delete <table>')
.description('Delete records from a table matching a filter')
.option('--filter <filter>', 'Filter expression (e.g. "id=eq.123")')
.option('--schema <name>', 'Schema to target (default: public)')
.action(async (table: string, opts, cmd) => {
const { json } = getRootOpts(cmd);
try {
Expand All @@ -25,7 +27,7 @@ export function registerRecordsDeleteCommand(recordsCmd: Command): void {

const res = await ossFetch(
`/api/database/records/${encodeURIComponent(table)}?${params}`,
{ method: 'DELETE' },
{ method: 'DELETE', headers: schemaProfileHeaders('write', opts.schema) },
);

const data = await res.json() as { data?: unknown[] };
Expand Down
4 changes: 3 additions & 1 deletion src/commands/records/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { requireAuth } from '../../lib/credentials.js';
import { handleError, getRootOpts } from '../../lib/errors.js';
import { outputJson, outputTable } from '../../lib/output.js';
import { trackCommandUsage } from '../../lib/command-telemetry.js';
import { schemaProfileHeaders } from './profile.js';

export function registerRecordsCommands(recordsCmd: Command): void {
recordsCmd
Expand All @@ -14,6 +15,7 @@ export function registerRecordsCommands(recordsCmd: Command): void {
.option('--order <order>', 'Order by (e.g. "created_at.desc")')
.option('--limit <n>', 'Limit number of records', parseInt)
.option('--offset <n>', 'Offset for pagination', parseInt)
.option('--schema <name>', 'Schema to target (default: public)')
.action(async (table: string, opts, cmd) => {
const { json } = getRootOpts(cmd);
try {
Expand All @@ -28,7 +30,7 @@ export function registerRecordsCommands(recordsCmd: Command): void {

const query = params.toString();
const path = `/api/database/records/${encodeURIComponent(table)}${query ? `?${query}` : ''}`;
const res = await ossFetch(path);
const res = await ossFetch(path, { headers: schemaProfileHeaders('read', opts.schema) });
const data = await res.json() as { data?: Record<string, unknown>[] };
const records = data.data ?? [];

Expand Down
22 changes: 22 additions & 0 deletions src/commands/records/profile.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { describe, it, expect } from 'vitest';
import { schemaProfileHeaders } from './profile.js';

describe('schemaProfileHeaders', () => {
it('returns no headers when no schema is given (defaults to public)', () => {
expect(schemaProfileHeaders('read')).toEqual({});
expect(schemaProfileHeaders('write', undefined)).toEqual({});
expect(schemaProfileHeaders('read', '')).toEqual({});
});

it('uses Accept-Profile for reads', () => {
expect(schemaProfileHeaders('read', 'analytics')).toEqual({
'Accept-Profile': 'analytics',
});
});

it('uses Content-Profile for writes', () => {
expect(schemaProfileHeaders('write', 'analytics')).toEqual({
'Content-Profile': 'analytics',
});
});
});
15 changes: 15 additions & 0 deletions src/commands/records/profile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* PostgREST selects the target schema via a profile header: `Accept-Profile`
* for reads and `Content-Profile` for writes/RPC. The table path stays bare.
*
* Returns the header(s) to send for a `--schema` option, or `{}` when it is
* unset (the backend then uses its default schema, `public`).
*/
export function schemaProfileHeaders(
kind: 'read' | 'write',
schema?: string,
): Record<string, string> {
if (!schema) return {};
const header = kind === 'read' ? 'Accept-Profile' : 'Content-Profile';
return { [header]: schema };
}
3 changes: 3 additions & 0 deletions src/commands/records/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import { requireAuth } from '../../lib/credentials.js';
import { handleError, getRootOpts, CLIError } from '../../lib/errors.js';
import { outputJson, outputSuccess } from '../../lib/output.js';
import { trackCommandUsage } from '../../lib/command-telemetry.js';
import { schemaProfileHeaders } from './profile.js';

export function registerRecordsUpdateCommand(recordsCmd: Command): void {
recordsCmd
.command('update <table>')
.description('Update records in a table matching a filter')
.option('--filter <filter>', 'Filter expression (e.g. "id=eq.123")')
.option('--data <json>', 'JSON data to update')
.option('--schema <name>', 'Schema to target (default: public)')
.action(async (table: string, opts, cmd) => {
const { json } = getRootOpts(cmd);
try {
Expand Down Expand Up @@ -39,6 +41,7 @@ export function registerRecordsUpdateCommand(recordsCmd: Command): void {
{
method: 'PATCH',
body: JSON.stringify(body),
headers: schemaProfileHeaders('write', opts.schema),
},
);

Expand Down
Loading