Permalink
Browse files

`@phenomic/core`: add options to sort db results

Closes #1225
  • Loading branch information...
MoOx committed May 8, 2018
1 parent 0a03aa3 commit a1f869ab5669083073dcdb310a1c86bfa87b213b
@@ -27,13 +27,13 @@ exports[`should build example correctly 2`] = `
Array [
"phenomic/content/pages/item/.json",
"phenomic/content/pages/item/about.json",
"phenomic/content/posts/by-default/1/desc/limit-4.json",
"phenomic/content/posts/by-default/1/desc/limit-6.json",
"phenomic/content/posts/by-default/1/desc/limit-6/after-c2Vjb25kLXBvc3Q=.json",
"phenomic/content/posts/by-default/1/desc/limit-6/after-dGhpcmQtcG9zdA==.json",
"phenomic/content/posts/by-default/1/desc/limit-6/after-Zm91cnRoLXBvc3Q=.json",
"phenomic/content/posts/by-default/1/desc/limit-6/after-ZmlmdGgtcG9zdA==.json",
"phenomic/content/posts/by-default/1/desc/limit-6/after-Zmlyc3QtcG9zdA==.json",
"phenomic/content/posts/by-default/1/desc/date/limit-4.json",
"phenomic/content/posts/by-default/1/desc/date/limit-6.json",
"phenomic/content/posts/by-default/1/desc/date/limit-6/after-c2Vjb25kLXBvc3Q=.json",
"phenomic/content/posts/by-default/1/desc/date/limit-6/after-dGhpcmQtcG9zdA==.json",
"phenomic/content/posts/by-default/1/desc/date/limit-6/after-Zm91cnRoLXBvc3Q=.json",
"phenomic/content/posts/by-default/1/desc/date/limit-6/after-ZmlmdGgtcG9zdA==.json",
"phenomic/content/posts/by-default/1/desc/date/limit-6/after-Zmlyc3QtcG9zdA==.json",
"phenomic/content/posts/item/fifth-post.json",
"phenomic/content/posts/item/first-post.json",
"phenomic/content/posts/item/fourth-post.json",
@@ -18,12 +18,12 @@ Array [
exports[`should build example correctly 2`] = `
Array [
"phenomic/content/posts/by-default/1/desc/limit-2.json",
"phenomic/content/posts/by-default/1/desc/limit-2/after-c2Vjb25kLXBvc3Q=.json",
"phenomic/content/posts/by-default/1/desc/limit-2/after-dGhpcmQtcG9zdA==.json",
"phenomic/content/posts/by-default/1/desc/limit-2/after-Zm91cnRoLXBvc3Q=.json",
"phenomic/content/posts/by-default/1/desc/limit-2/after-ZmlmdGgtcG9zdA==.json",
"phenomic/content/posts/by-default/1/desc/limit-2/after-Zmlyc3QtcG9zdA==.json",
"phenomic/content/posts/by-default/1/desc/date/limit-2.json",
"phenomic/content/posts/by-default/1/desc/date/limit-2/after-c2Vjb25kLXBvc3Q=.json",
"phenomic/content/posts/by-default/1/desc/date/limit-2/after-dGhpcmQtcG9zdA==.json",
"phenomic/content/posts/by-default/1/desc/date/limit-2/after-Zm91cnRoLXBvc3Q=.json",
"phenomic/content/posts/by-default/1/desc/date/limit-2/after-ZmlmdGgtcG9zdA==.json",
"phenomic/content/posts/by-default/1/desc/date/limit-2/after-Zmlyc3QtcG9zdA==.json",
"phenomic/content/posts/item/fifth-post.json",
"phenomic/content/posts/item/first-post.json",
"phenomic/content/posts/item/fourth-post.json",
@@ -19,12 +19,12 @@ Array [
exports[`should build example correctly 2`] = `
Array [
"phenomic/content/posts/by-default/1/desc/limit-2.json",
"phenomic/content/posts/by-default/1/desc/limit-2/after-c2Vjb25kLXBvc3Q=.json",
"phenomic/content/posts/by-default/1/desc/limit-2/after-dGhpcmQtcG9zdA==.json",
"phenomic/content/posts/by-default/1/desc/limit-2/after-Zm91cnRoLXBvc3Q=.json",
"phenomic/content/posts/by-default/1/desc/limit-2/after-ZmlmdGgtcG9zdA==.json",
"phenomic/content/posts/by-default/1/desc/limit-2/after-Zmlyc3QtcG9zdA==.json",
"phenomic/content/posts/by-default/1/desc/date/limit-2.json",
"phenomic/content/posts/by-default/1/desc/date/limit-2/after-c2Vjb25kLXBvc3Q=.json",
"phenomic/content/posts/by-default/1/desc/date/limit-2/after-dGhpcmQtcG9zdA==.json",
"phenomic/content/posts/by-default/1/desc/date/limit-2/after-Zm91cnRoLXBvc3Q=.json",
"phenomic/content/posts/by-default/1/desc/date/limit-2/after-ZmlmdGgtcG9zdA==.json",
"phenomic/content/posts/by-default/1/desc/date/limit-2/after-Zmlyc3QtcG9zdA==.json",
"phenomic/content/posts/item/fifth-post.json",
"phenomic/content/posts/item/first-post.json",
"phenomic/content/posts/item/fourth-post.json",
@@ -77,6 +77,7 @@ let queries = props => {
by: Some("default"),
value: None,
order: None,
sort: None,
limit: Some(2),
after: Some(props##params##after)
})
@@ -15,6 +15,12 @@ declare type Url = {
hash?: string
};
declare type PhenomicDBConfig = {|
sortFunctions?: {
[key: string]: (PhenomicDBEntry, PhenomicDBEntry) => number
}
|};
declare type PhenomicDBEntryInput = {|
data: Object,
partial: Object
@@ -62,12 +68,13 @@ declare type PhenomicDB = {|
) => mixed | PhenomicDBEntryPartial,
getList: (
sub: null | string | $ReadOnlyArray<string>,
config?: {
query?: {
gt?: string,
gte?: string,
lt?: string,
lte?: string,
limit?: number,
sort?: string,
reverse?: boolean
},
filter?: string,
@@ -117,6 +124,7 @@ declare type PhenomicInputConfig = {|
port?: number,
socketPort?: number,
bundleName?: string,
db?: PhenomicDBConfig,
...PhenomicInputPlugins
|};
@@ -215,6 +223,7 @@ declare type PhenomicConfig = {|
port: number,
socketPort: number,
bundleName: string,
db: PhenomicDBConfig,
plugins: $ReadOnlyArray<PhenomicPlugin>
|};
@@ -225,6 +234,7 @@ declare type PhenomicQueryConfig = {|
by?: string,
value?: string,
order?: string,
sort?: string,
limit?: number
|};
@@ -71,8 +71,7 @@
"posttest": "yarn examples && yarn website:test"
},
"lint-staged": {
"*.{js,json,css,md}": ["git-exec-and-restage prettier --write"],
"*.re": ["git-exec-and-restage refmt --in-place"]
"*.{js,json,css,md}": ["git-exec-and-restage prettier --write"]
},
"babel": {
"presets": [
@@ -1,15 +1,15 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`should build url for items by date 1`] = `"/posts/by-date/1/desc.json"`;
exports[`should build url for items by date 1`] = `"/posts/by-date/1/desc/date.json"`;
exports[`should build url for single item 1`] = `"/posts/item/test.json"`;
exports[`should build url for specific tag 1`] = `"/posts/by-tag/test/desc.json"`;
exports[`should build url for specific tag 1`] = `"/posts/by-tag/test/desc/date.json"`;
exports[`should build url for specific tag 2`] = `"/posts/by-tag/test/asc.json"`;
exports[`should build url for specific tag 2`] = `"/posts/by-tag/test/asc/date.json"`;
exports[`should build url for specific tag 3`] = `"/posts/by-tag/test/asc/limit-10.json"`;
exports[`should build url for specific tag 3`] = `"/posts/by-tag/test/asc/date/limit-10.json"`;
exports[`should build url for specific tag 4`] = `"/posts/by-tag/test/asc/limit-10/after-BASE64.json"`;
exports[`should build url for specific tag 4`] = `"/posts/by-tag/test/asc/date/limit-10/after-BASE64.json"`;
exports[`should build url for specific tag 5`] = `"local/posts/by-tag/test/asc/limit-10/after-BASE64.json"`;
exports[`should build url for specific tag 5`] = `"local/posts/by-tag/test/asc/date/limit-10/after-BASE64.json"`;
@@ -11,6 +11,7 @@ type listConfig = {
by: option(string),
value: option(string),
order: option(string),
sort: option(string),
limit: option(int)
};
@@ -19,6 +20,7 @@ type paginatedListConfig = {
by: option(string),
value: option(string),
order: option(string),
sort: option(string),
limit: option(int),
after: option(string)
};
@@ -36,6 +38,7 @@ let query = (queryConfig) =>
"by": Js.Nullable.undefined,
"value": Js.Nullable.undefined,
"order": Js.Nullable.undefined,
"sort": Js.Nullable.undefined,
"limit": Js.Nullable.undefined,
"after": Js.Nullable.undefined
}
@@ -45,6 +48,7 @@ let query = (queryConfig) =>
"by": Js.Nullable.from_opt(queryConfigList.by),
"value": Js.Nullable.from_opt(queryConfigList.value),
"order": Js.Nullable.from_opt(queryConfigList.order),
"sort": Js.Nullable.from_opt(queryConfigList.sort),
"limit": Js.Nullable.from_opt(queryConfigList.limit),
"after": Js.Nullable.undefined
}
@@ -54,6 +58,7 @@ let query = (queryConfig) =>
"by": Js.Nullable.from_opt(queryConfigPaginatedList.by),
"value": Js.Nullable.from_opt(queryConfigPaginatedList.value),
"order": Js.Nullable.from_opt(queryConfigPaginatedList.order),
"sort": Js.Nullable.from_opt(queryConfigPaginatedList.sort),
"limit": Js.Nullable.from_opt(queryConfigPaginatedList.limit),
"after": Js.Nullable.from_opt(queryConfigPaginatedList.after)
}
@@ -31,6 +31,7 @@ function query(config: PhenomicQueryConfig): PhenomicQueryConfig {
value: config.by && config.value ? config.value : "1",
order: config.order ? config.order : "desc",
limit: config.limit ? parseInt(config.limit, 10) : undefined,
sort: config.sort || "date",
after: config.after
});
}
@@ -30,6 +30,7 @@ function url(config: Object): string {
`by-${protect(config.by)}`,
protect(config.value || "1"),
protect(config.order || "desc"),
protect(config.sort || "date"),
...(config.limit ? [`limit-${protect(config.limit)}`] : []),
...(config.limit && config.after
? [`after-${protect(config.after)}`]
@@ -2,11 +2,12 @@
import request from "supertest";
import db from "../../db";
import createDB from "../../db";
import dbFixtures from "../../db/__tests__/__fixtures__";
import createServer from "..";
const db = createDB({});
db._setDatabase(dbFixtures);
const server = createServer({
@@ -34,11 +35,11 @@ it("should return basic response", async () => {
it("should return handle simple query for list", async () => {
await request(server)
.get("/news/by-default/1/asc.json")
.get("/news/by-default/1/asc/date.json")
.expect(200);
await request(server)
.get("/news/by-default/1/asc/limit-2.json")
.get("/news/by-default/1/asc/date/limit-2.json")
.expect(200);
// await request(server)
Oops, something went wrong.

0 comments on commit a1f869a

Please sign in to comment.