Skip to content

Commit b1307d2

Browse files
authored
Merge pull request #253 from UW-Macrostrat/keyset-pagination
Keyset Pagination
2 parents f52ee3f + 98335fe commit b1307d2

File tree

3 files changed

+48
-10
lines changed

3 files changed

+48
-10
lines changed

v2/definitions/strat_name_concepts.ts

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,19 @@ module.exports = function (req, res, next, cb) {
2626
JOIN macrostrat.refs ON snm.ref_id = refs.id
2727
*/
2828
}),
29-
params = {};
29+
params = {},
30+
where = [];
3031

3132
if ("all" in req.query) {
3233
// do nothing
34+
} else if (req.query.concept_like) {
35+
where.push("lower(name) ILIKE lower(:name)");
36+
params["name"] = req.query.concept_like + "%";
3337
} else if ("concept_name" in req.query) {
34-
sql += " WHERE name = ANY(:concept_name)";
38+
where.push("name = ANY(:concept_name)");
3539
params["concept_name"] = req.query.concept_name;
3640
} else if (req.query.concept_id || req.query.strat_name_concept_id) {
37-
sql += " WHERE concept_id = ANY(:concept_id)";
41+
where.push("concept_id = ANY(:concept_id)");
3842
if (req.query.concept_id) {
3943
params["concept_id"] = larkin.parseMultipleIds(req.query.concept_id);
4044
} else {
@@ -43,15 +47,28 @@ module.exports = function (req, res, next, cb) {
4347
);
4448
}
4549
} else if (req.query.strat_name_id) {
46-
sql +=
47-
" WHERE concept_id IN (SELECT concept_id FROM macrostrat.lookup_strat_names WHERE strat_name_id IN (:strat_name_ids))";
50+
where.push("concept_id IN (SELECT concept_id FROM macrostrat.lookup_strat_names WHERE strat_name_id IN (:strat_name_ids))");
4851
params["strat_name_ids"] = larkin.parseMultipleIds(req.query.strat_name_id);
4952
}
5053

54+
// pagination
55+
const lastId = req.query.last_id ? parseInt(req.query.last_id, 10) : null;
56+
const pageSize = req.query.page_size ? parseInt(req.query.page_size, 10) : 5; // defaults to 5
57+
58+
if (req.query.last_id) {
59+
where.push("concept_id > :last_id");
60+
params["last_id"] = lastId;
61+
}
62+
63+
if (where.length > 0) {
64+
sql += " WHERE " + where.join(" AND ");
65+
}
66+
5167
sql += " GROUP BY concept_id, author ORDER BY concept_id";
5268

53-
if ("sample" in req.query) {
54-
sql += " LIMIT 5";
69+
if ("sample" in req.query || req.query.last_id) {
70+
sql += " LIMIT :page_size";
71+
params["page_size"] = pageSize;
5572
}
5673

5774
larkin.queryPg("burwell", sql, params, function (error, result) {

v2/definitions/strat_names.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ module.exports = function (req, res, next, cb) {
88
}
99

1010
var where = [],
11-
params = {};
11+
params = {},
12+
orderBy = [];
1213

1314
if (req.query.rule) {
1415
if (req.query.rule === "down") {
@@ -123,12 +124,27 @@ module.exports = function (req, res, next, cb) {
123124
FROM macrostrat.lookup_strat_names l
124125
`;
125126

127+
// pagination
128+
const lastId = req.query.last_id ? parseInt(req.query.last_id, 10) : null;
129+
const pageSize = req.query.page_size ? parseInt(req.query.page_size, 10) : 5; // defaults to 5
130+
131+
if (req.query.last_id) {
132+
where.push("strat_name_id > :last_id");
133+
params["last_id"] = lastId;
134+
orderBy.push("strat_name_id ASC");
135+
}
136+
126137
if (where.length > 0) {
127138
sql += " WHERE " + where.join(" AND ");
128139
}
129140

130-
if ("sample" in req.query) {
131-
sql += " LIMIT 5";
141+
if (orderBy.length > 0) {
142+
sql += " ORDER BY " + orderBy.join(", ");
143+
}
144+
145+
if ("sample" in req.query || req.query.last_id) {
146+
sql += " LIMIT :page_size";
147+
params["page_size"] = pageSize;
132148
}
133149

134150
larkin.queryPg("burwell", sql, params, function (error, response) {

v2/defs.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -711,6 +711,8 @@
711711
"retrieve only stratigraphic names linked the specified reference_id (see /defs/refs)",
712712
all: "return all lithostratigraphic names",
713713
format: "Desired output format",
714+
last_id: "integer, used to paginate results. If specified, will return strat_names with an ID greater than last_id",
715+
page_size: "integer, used to paginate results. If specified, will return at most page_size results. Must be paired with last_idd",
714716
},
715717
output_formats: ["json", "csv"],
716718
examples: [
@@ -746,8 +748,11 @@
746748
parameters: {
747749
concept_id: "unique id",
748750
concept_name: "string specifying concept name",
751+
concept_like: "lithostratigraphic name concept, with open-ended string matching",
749752
all: "return all lithostratigraphic names",
750753
format: "Desired output format",
754+
last_id: "integer, used to paginate results. If specified, will return strat_names with an ID greater than last_id",
755+
page_size: "integer, used to paginate results. If specified, will return at most page_size results. Must be paired with last_idd",
751756
},
752757
output_formats: ["json", "csv"],
753758
examples: ["api/v2/defs/concept_id?all"],

0 commit comments

Comments
 (0)