Skip to content
This repository was archived by the owner on Oct 9, 2025. It is now read-only.

Commit 42c93cd

Browse files
committed
Support for Embedded Tables #46
- improved .order() - introduce .limit() and .offset() to allow setting limits and offset for foreign tables
1 parent 3deafd8 commit 42c93cd

File tree

3 files changed

+76
-9
lines changed

3 files changed

+76
-9
lines changed

src/Builder.js

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ class Builder {
2626
}
2727

2828
request(method) {
29-
if(this.schema){
30-
if(method == 'GET') this.headers['Accept-Profile'] = this.schema
29+
if (this.schema) {
30+
if (method == 'GET') this.headers['Accept-Profile'] = this.schema
3131
else this.headers['Content-Profile'] = this.schema
3232
}
3333
return new Request(method, this.url, this.headers)
@@ -57,7 +57,15 @@ class Builder {
5757
break
5858

5959
case 'order':
60-
request.order(queryFilter.property, queryFilter.ascending, queryFilter.nullsFirst)
60+
request.order(queryFilter.columnName, queryFilter.ascending, queryFilter.nullsFirst)
61+
break
62+
63+
case 'limit':
64+
request.limit(queryFilter.columnName, queryFilter.criteria)
65+
break
66+
67+
case 'offset':
68+
request.offset(queryFilter.columnName, queryFilter.criteria)
6169
break
6270

6371
case 'range':
@@ -105,17 +113,33 @@ class Builder {
105113
return this
106114
}
107115

108-
order(property, ascending = false, nullsFirst = false) {
116+
order(columnName, ascending = false, nullsFirst = false) {
109117
this.queryFilters.push({
110118
filter: 'order',
111-
property,
119+
columnName,
112120
ascending,
113121
nullsFirst,
114122
})
115123

116124
return this
117125
}
118126

127+
limit(columnName, criteria) {
128+
this.queryFilters.push({
129+
filter: 'limit',
130+
columnName,
131+
criteria
132+
})
133+
}
134+
135+
offset(columnName, criteria) {
136+
this.queryFilters.push({
137+
filter: 'offset',
138+
columnName,
139+
criteria
140+
})
141+
}
142+
119143
range(from, to) {
120144
this.queryFilters.push({
121145
filter: 'range',

src/Request.js

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
import { Request as SuperAgent } from 'superagent'
1414
import * as Filters from './utils/Filters'
15+
import * as Helpers from './utils/Helpers'
1516

1617
const contentRangeStructure = /^(\d+)-(\d+)\/(\d+)$/
1718

@@ -81,7 +82,6 @@ class Request extends SuperAgent {
8182

8283
// for ranges, length of array should always be equal to 2
8384
if (['ovr', 'sl', 'sr', 'nxr', 'nxl', 'adj'].includes(operator) && criteria.length != 2) {
84-
8585
return {
8686
body: null,
8787
status: 400,
@@ -145,19 +145,51 @@ class Request extends SuperAgent {
145145
/**
146146
* Tells PostgREST in what order the result should be returned.
147147
*
148-
* @param {string} property The property name to order by.
148+
* @param {string} columnName The columnName name to order by.
149149
* @param {bool} ascending True for descending results, false by default.
150150
* @param {bool} nullsFirst True for nulls first, false by default.
151151
* @returns {Request} The API request object.
152152
*/
153153

154-
order(property, ascending = false, nullsFirst = false) {
154+
order(columnName, ascending = false, nullsFirst = false) {
155+
let { cleanedColumnName, foreignTableName } = Helpers.cleanColumnName(columnName)
156+
155157
this.query(
156-
`order=${property}.${ascending ? 'asc' : 'desc'}.${nullsFirst ? 'nullsfirst' : 'nullslast'}`
158+
`${foreignTableName != null ? `${foreignTableName}.` : ''}order=${cleanedColumnName}.${
159+
ascending ? 'asc' : 'desc'
160+
}.${nullsFirst ? 'nullsfirst' : 'nullslast'}`
157161
)
158162
return this
159163
}
160164

165+
limit(criteria, columnName = null) {
166+
if (typeof criteria != 'number') {
167+
return {
168+
body: null,
169+
status: 400,
170+
statusCode: 400,
171+
statusText: `.limit() cannot be invoked with criteria that is not a number.`,
172+
}
173+
}
174+
175+
this.query(`${columnName != null ? `${columnName}.` : ''}limit=${criteria}`)
176+
return this
177+
}
178+
179+
offset(criteria, columnName = null) {
180+
if (typeof criteria != 'number') {
181+
return {
182+
body: null,
183+
status: 400,
184+
statusCode: 400,
185+
statusText: `.offset() cannot be invoked with criteria that is not a number.`,
186+
}
187+
}
188+
189+
this.query(`${columnName != null ? `${columnName}.` : ''}offset=${criteria}`)
190+
return this
191+
}
192+
161193
/**
162194
* Specify a range of items for PostgREST to return. If the second value is
163195
* not defined, the rest of the collection will be sent back.

src/utils/Helpers.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,14 @@ export function cleanFilterArray(filterArray) {
2323

2424
return cleanedFilterArray
2525
}
26+
27+
export function cleanColumnName(columnName) {
28+
let cleanedColumnName = columnName
29+
let foreignTableName = null
30+
if (columnName.includes('.')) {
31+
cleanedColumnName = columnName.split('.')[1]
32+
foreignTableName = columnName.split('.')[0]
33+
}
34+
35+
return { cleanedColumnName, foreignTableName }
36+
}

0 commit comments

Comments
 (0)