Skip to content

Support for multiple schemas #13

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
4 changes: 4 additions & 0 deletions lib/postgrest/builders/query_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ def initialize(url:, headers:, schema:)
def select(*columns, extra_headers: {})
columns.compact!
columns = ['*'] if columns.length.zero?
extra_headers[:'Accept-Profile'] ||= schema
request = HTTP.new(uri: uri, query: { select: columns.join(',') }, headers: headers.merge(extra_headers))

FilterBuilder.new(request)
Expand All @@ -25,20 +26,23 @@ def insert(values)

def upsert(values, extra_headers: {})
extra_headers[:Prefer] ||= 'return=representation,resolution=merge-duplicates'
extra_headers[:'Content-Profile'] ||= schema
request = HTTP.new(uri: uri, body: values, http_method: :post, headers: headers.merge(extra_headers))

BaseBuilder.new(request)
end

def update(values, extra_headers: {})
extra_headers[:Prefer] ||= 'return=representation'
extra_headers[:'Content-Profile'] ||= schema
request = HTTP.new(uri: uri, body: values, http_method: :patch, headers: headers.merge(extra_headers))

FilterBuilder.new(request)
end

def delete(extra_headers: {})
extra_headers[:Prefer] ||= 'return=representation'
extra_headers[:'Content-Profile'] ||= schema
request = HTTP.new(uri: uri, http_method: :delete, headers: headers.merge(extra_headers))

FilterBuilder.new(request)
Expand Down
41 changes: 34 additions & 7 deletions spec/postgrest/builders/query_builder_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module Postgrest
RSpec.describe Builders::QueryBuilder do
let(:url) { 'https://postgrest_server.com' }
let(:read_profile) { { :'Accept-Profile' => 'public' } }
let(:write_profile) { { :'Content-Profile' => 'public' } }
subject { described_class.new(url: url, headers: {}, schema: 'public') }

describe 'attributes' do
Expand All @@ -10,10 +12,17 @@ module Postgrest
end

describe '#select' do
context 'common scenario' do
let(:instance) { subject.select }

it { expect(instance).to be_a(Builders::FilterBuilder) }
it { expect(instance.http.headers).to eq(read_profile) }
end

context 'when passing extra headers' do
it 'merges the headers' do
instance = subject.select(extra_headers: { foo: 123 })
expect(instance.http.headers).to eq({ foo: 123 })
instance = subject.select(extra_headers: { :'Accept-Profile' => 'other', foo: 123 })
expect(instance.http.headers).to eq({ :'Accept-Profile' => 'other', foo: 123 })
end
end

Expand All @@ -40,7 +49,7 @@ module Postgrest

it { expect(instance).to be_a(Builders::BaseBuilder) }
it { expect(instance.http.body).to eq({ name: 'John', age: 32 }) }
it { expect(instance.http.headers).to eq({ Prefer: 'return=representation' }) }
it { expect(instance.http.headers).to eq(write_profile.merge({ Prefer: 'return=representation' })) }
end
end

Expand All @@ -50,7 +59,16 @@ module Postgrest

it { expect(instance).to be_a(Builders::BaseBuilder) }
it { expect(instance.http.body).to eq({ name: 'John', age: 32 }) }
it { expect(instance.http.headers).to eq({ Prefer: 'return=representation,resolution=merge-duplicates' }) }
it { expect(instance.http.headers).to eq(write_profile.merge({ Prefer: 'return=representation,resolution=merge-duplicates' })) }
end

context 'with custom headers' do
describe 'sets the headers' do
let(:instance) { subject.upsert({ name: 'John', age: 32 }, extra_headers: { :'Content-Profile' => 'other', foo: 123 }) }

it { expect(instance).to be_a(Builders::BaseBuilder) }
it { expect(instance.http.headers).to eq({ :'Content-Profile' => 'other', foo: 123, Prefer: 'return=representation,resolution=merge-duplicates' }) }
end
end
end

Expand All @@ -60,7 +78,16 @@ module Postgrest

it { expect(instance).to be_a(Builders::FilterBuilder) }
it { expect(instance.http.body).to eq({ name: 'John', age: 32 }) }
it { expect(instance.http.headers).to eq({ Prefer: 'return=representation' }) }
it { expect(instance.http.headers).to eq(write_profile.merge({ Prefer: 'return=representation' })) }
end

context 'with custom headers' do
describe 'sets the headers' do
let(:instance) { subject.update({ name: 'John', age: 32 }, extra_headers: { :'Content-Profile' => 'other', foo: 123 }) }

it { expect(instance).to be_a(Builders::FilterBuilder) }
it { expect(instance.http.headers).to eq({ :'Content-Profile' => 'other', foo: 123, Prefer: 'return=representation' }) }
end
end
end

Expand All @@ -70,15 +97,15 @@ module Postgrest
let(:instance) { subject.delete(extra_headers: { foo: 123 }) }

it { expect(instance).to be_a(Builders::FilterBuilder) }
it { expect(instance.http.headers).to eq({ Prefer: 'return=representation', foo: 123 }) }
it { expect(instance.http.headers).to eq(write_profile.merge({ foo: 123, Prefer: 'return=representation' })) }
end
end

context 'common scenario' do
let(:instance) { subject.delete }

it { expect(instance).to be_a(Builders::FilterBuilder) }
it { expect(instance.http.headers).to eq({ Prefer: 'return=representation' }) }
it { expect(instance.http.headers).to eq(write_profile.merge({ Prefer: 'return=representation' })) }
end
end
end
Expand Down