Skip to content

Commit ebeb383

Browse files
committed
Support Date objects
1 parent 9b9f369 commit ebeb383

File tree

3 files changed

+330
-5
lines changed

3 files changed

+330
-5
lines changed

src/__snapshots__/sql-cursor-pagination.test.ts.snap

Lines changed: 259 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6001,6 +6001,265 @@ exports[`SqlCursorPagination > selects the third row when selecting the last one
60016001
}
60026002
`;
60036003

6004+
exports[`SqlCursorPagination > supports \`Date\` objects 1`] = `Infinity`;
6005+
6006+
exports[`SqlCursorPagination > supports \`Date\` objects 2`] = `
6007+
{
6008+
"bindings": [],
6009+
"strings": [
6010+
"\`created_at\` asc",
6011+
],
6012+
}
6013+
`;
6014+
6015+
exports[`SqlCursorPagination > supports \`Date\` objects 3`] = `
6016+
{
6017+
"bindings": [],
6018+
"sql": "\`created_at\` asc",
6019+
}
6020+
`;
6021+
6022+
exports[`SqlCursorPagination > supports \`Date\` objects 4`] = `
6023+
{
6024+
"bindings": [],
6025+
"sql": "\`created_at\` asc",
6026+
}
6027+
`;
6028+
6029+
exports[`SqlCursorPagination > supports \`Date\` objects 5`] = `
6030+
{
6031+
"bindings": [],
6032+
"sql": "\`created_at\` asc",
6033+
}
6034+
`;
6035+
6036+
exports[`SqlCursorPagination > supports \`Date\` objects 6`] = `
6037+
{
6038+
"bindings": {},
6039+
"sql": "\`created_at\` asc",
6040+
}
6041+
`;
6042+
6043+
exports[`SqlCursorPagination > supports \`Date\` objects 7`] = `
6044+
{
6045+
"bindings": [],
6046+
"strings": [
6047+
"\`created_at\` asc",
6048+
],
6049+
}
6050+
`;
6051+
6052+
exports[`SqlCursorPagination > supports \`Date\` objects 8`] = `
6053+
{
6054+
"bindings": [],
6055+
"strings": [
6056+
"1",
6057+
],
6058+
}
6059+
`;
6060+
6061+
exports[`SqlCursorPagination > supports \`Date\` objects 9`] = `
6062+
{
6063+
"bindings": [],
6064+
"sql": "1",
6065+
}
6066+
`;
6067+
6068+
exports[`SqlCursorPagination > supports \`Date\` objects 10`] = `
6069+
{
6070+
"bindings": [],
6071+
"sql": "1",
6072+
}
6073+
`;
6074+
6075+
exports[`SqlCursorPagination > supports \`Date\` objects 11`] = `
6076+
{
6077+
"bindings": [],
6078+
"sql": "1",
6079+
}
6080+
`;
6081+
6082+
exports[`SqlCursorPagination > supports \`Date\` objects 12`] = `
6083+
{
6084+
"bindings": {},
6085+
"sql": "1",
6086+
}
6087+
`;
6088+
6089+
exports[`SqlCursorPagination > supports \`Date\` objects 13`] = `
6090+
{
6091+
"bindings": [],
6092+
"strings": [
6093+
"1",
6094+
],
6095+
}
6096+
`;
6097+
6098+
exports[`SqlCursorPagination > supports \`Date\` objects 14`] = `
6099+
{
6100+
"edges": [
6101+
{
6102+
"cursor": "qWIDywo8Rl5JFVR8PbE4avrrnn3nzUfyAoDe1bmnz_A.peApzMGipEv7qdRPeraULkDntlGs3jzzSXfTO0844SbARw55FvI9DVExhkzZQqjFXqcZingVwDtjbLqIMAb9imrX2g-TfU5uZMKycOMWyaxoe0_td6xYAxwqgqE",
6103+
"node": {
6104+
"admin": 0,
6105+
"created_at": 1970-01-19T13:42:23.417Z,
6106+
"email": "purus.accumsan@icloud.com",
6107+
"email_alias": "purus.accumsan@icloud.com",
6108+
"first_name": "Cooper",
6109+
"id": 4,
6110+
"last_name": "Molina",
6111+
},
6112+
},
6113+
{
6114+
"cursor": "cHd5PKookDvc0P9q8oi0BSxJxjJkY6FRTwsqWafjLZ8.YAXWYP7H8IsYyMv3dZx4TW_o4h2Fn8HwaXlIPQKcyPVef9PKxWABJZVbjF0Y8hd600AmKPdPKVkl7B8P9K2QekYzFK_B47C1uU6ZWDaE0QmLpdEFrEyMdV9AYEc",
6115+
"node": {
6116+
"admin": 1,
6117+
"created_at": 1970-01-19T21:08:52.719Z,
6118+
"email": "diam.vel@outlook.edu",
6119+
"email_alias": "diam.vel@outlook.edu",
6120+
"first_name": "Anika",
6121+
"id": 5,
6122+
"last_name": "Molina",
6123+
},
6124+
},
6125+
{
6126+
"cursor": "gy1A_fKEUWvU0VS0tFndV9XLHcdIA5pgHxXlO74ArBU.l1apu5fAVtgM1M7cMboOxpnfJLvnYcxU3Blc0uE5KK1BxA4wQuByOlCnfNgSNz9d9rAkMNSNaotEdSkgSaQ41jsQ7546X5zmDK5SECHdJJmXK0DScDAUZHYzZzE",
6127+
"node": {
6128+
"admin": 1,
6129+
"created_at": 1970-01-20T01:45:59.350Z,
6130+
"email": "eu@hotmail.ca",
6131+
"email_alias": "eu@hotmail.ca",
6132+
"first_name": "Joseph",
6133+
"id": 3,
6134+
"last_name": "Rhodes",
6135+
},
6136+
},
6137+
{
6138+
"cursor": "gHfYIMbXWcV96xDGzfF2UZt7MlGcVH9C5UoBW6WLMLA.NJELaBjwP6gyYY9PI5VzRnMTcd_x-wrfz6bpT_WylUbOK9WA2DSrQCkplgCrfG8f8LWB7X1OVhLVbG3qPS7XcFNNmNxawaBSio-GwanlWDhf1cwH4aAYhzr9VRo",
6139+
"node": {
6140+
"admin": 0,
6141+
"created_at": 1970-01-20T05:48:58.189Z,
6142+
"email": "rhoncus.donec@aol.edu",
6143+
"email_alias": "rhoncus.donec@aol.edu",
6144+
"first_name": "Anika",
6145+
"id": 1,
6146+
"last_name": "Duncan",
6147+
},
6148+
},
6149+
{
6150+
"cursor": "3hmzBHhp3sfAyr-SP3O2te1916dubaKGvqrhkyyeYIU.GzliO7qp_3sFJaGndjb85HrZIuBsAFgRm-S-XGISNsZxRDAGca84BaSK-VbZQQMZPb2kMKoP0bvzQtoet2Rmvq0-MoskwTbbghnCq_IjcKuREsuZ0LSUbWNDRvw",
6151+
"node": {
6152+
"admin": 0,
6153+
"created_at": 1970-01-20T10:35:12.324Z,
6154+
"email": "ut.nisi@yahoo.org",
6155+
"email_alias": "ut.nisi@yahoo.org",
6156+
"first_name": "Jermaine",
6157+
"id": 2,
6158+
"last_name": "O'connor",
6159+
},
6160+
},
6161+
],
6162+
"pageInfo": {
6163+
"endCursor": "3hmzBHhp3sfAyr-SP3O2te1916dubaKGvqrhkyyeYIU.GzliO7qp_3sFJaGndjb85HrZIuBsAFgRm-S-XGISNsZxRDAGca84BaSK-VbZQQMZPb2kMKoP0bvzQtoet2Rmvq0-MoskwTbbghnCq_IjcKuREsuZ0LSUbWNDRvw",
6164+
"hasNextPage": false,
6165+
"hasPreviousPage": false,
6166+
"startCursor": "qWIDywo8Rl5JFVR8PbE4avrrnn3nzUfyAoDe1bmnz_A.peApzMGipEv7qdRPeraULkDntlGs3jzzSXfTO0844SbARw55FvI9DVExhkzZQqjFXqcZingVwDtjbLqIMAb9imrX2g-TfU5uZMKycOMWyaxoe0_td6xYAxwqgqE",
6167+
},
6168+
Symbol(edgesWithRawCursor): [
6169+
{
6170+
"cursor": "qWIDywo8Rl5JFVR8PbE4avrrnn3nzUfyAoDe1bmnz_A.peApzMGipEv7qdRPeraULkDntlGs3jzzSXfTO0844SbARw55FvI9DVExhkzZQqjFXqcZingVwDtjbLqIMAb9imrX2g-TfU5uZMKycOMWyaxoe0_td6xYAxwqgqE",
6171+
"node": {
6172+
"admin": 0,
6173+
"created_at": 1970-01-19T13:42:23.417Z,
6174+
"email": "purus.accumsan@icloud.com",
6175+
"email_alias": "purus.accumsan@icloud.com",
6176+
"first_name": "Cooper",
6177+
"id": 4,
6178+
"last_name": "Molina",
6179+
},
6180+
"rawCursor": {
6181+
"fields": {
6182+
"created_at": "1970-01-19T13:42:23.417Z",
6183+
},
6184+
"queryName": "TestQuery",
6185+
},
6186+
},
6187+
{
6188+
"cursor": "cHd5PKookDvc0P9q8oi0BSxJxjJkY6FRTwsqWafjLZ8.YAXWYP7H8IsYyMv3dZx4TW_o4h2Fn8HwaXlIPQKcyPVef9PKxWABJZVbjF0Y8hd600AmKPdPKVkl7B8P9K2QekYzFK_B47C1uU6ZWDaE0QmLpdEFrEyMdV9AYEc",
6189+
"node": {
6190+
"admin": 1,
6191+
"created_at": 1970-01-19T21:08:52.719Z,
6192+
"email": "diam.vel@outlook.edu",
6193+
"email_alias": "diam.vel@outlook.edu",
6194+
"first_name": "Anika",
6195+
"id": 5,
6196+
"last_name": "Molina",
6197+
},
6198+
"rawCursor": {
6199+
"fields": {
6200+
"created_at": "1970-01-19T21:08:52.719Z",
6201+
},
6202+
"queryName": "TestQuery",
6203+
},
6204+
},
6205+
{
6206+
"cursor": "gy1A_fKEUWvU0VS0tFndV9XLHcdIA5pgHxXlO74ArBU.l1apu5fAVtgM1M7cMboOxpnfJLvnYcxU3Blc0uE5KK1BxA4wQuByOlCnfNgSNz9d9rAkMNSNaotEdSkgSaQ41jsQ7546X5zmDK5SECHdJJmXK0DScDAUZHYzZzE",
6207+
"node": {
6208+
"admin": 1,
6209+
"created_at": 1970-01-20T01:45:59.350Z,
6210+
"email": "eu@hotmail.ca",
6211+
"email_alias": "eu@hotmail.ca",
6212+
"first_name": "Joseph",
6213+
"id": 3,
6214+
"last_name": "Rhodes",
6215+
},
6216+
"rawCursor": {
6217+
"fields": {
6218+
"created_at": "1970-01-20T01:45:59.350Z",
6219+
},
6220+
"queryName": "TestQuery",
6221+
},
6222+
},
6223+
{
6224+
"cursor": "gHfYIMbXWcV96xDGzfF2UZt7MlGcVH9C5UoBW6WLMLA.NJELaBjwP6gyYY9PI5VzRnMTcd_x-wrfz6bpT_WylUbOK9WA2DSrQCkplgCrfG8f8LWB7X1OVhLVbG3qPS7XcFNNmNxawaBSio-GwanlWDhf1cwH4aAYhzr9VRo",
6225+
"node": {
6226+
"admin": 0,
6227+
"created_at": 1970-01-20T05:48:58.189Z,
6228+
"email": "rhoncus.donec@aol.edu",
6229+
"email_alias": "rhoncus.donec@aol.edu",
6230+
"first_name": "Anika",
6231+
"id": 1,
6232+
"last_name": "Duncan",
6233+
},
6234+
"rawCursor": {
6235+
"fields": {
6236+
"created_at": "1970-01-20T05:48:58.189Z",
6237+
},
6238+
"queryName": "TestQuery",
6239+
},
6240+
},
6241+
{
6242+
"cursor": "3hmzBHhp3sfAyr-SP3O2te1916dubaKGvqrhkyyeYIU.GzliO7qp_3sFJaGndjb85HrZIuBsAFgRm-S-XGISNsZxRDAGca84BaSK-VbZQQMZPb2kMKoP0bvzQtoet2Rmvq0-MoskwTbbghnCq_IjcKuREsuZ0LSUbWNDRvw",
6243+
"node": {
6244+
"admin": 0,
6245+
"created_at": 1970-01-20T10:35:12.324Z,
6246+
"email": "ut.nisi@yahoo.org",
6247+
"email_alias": "ut.nisi@yahoo.org",
6248+
"first_name": "Jermaine",
6249+
"id": 2,
6250+
"last_name": "O'connor",
6251+
},
6252+
"rawCursor": {
6253+
"fields": {
6254+
"created_at": "1970-01-20T10:35:12.324Z",
6255+
},
6256+
"queryName": "TestQuery",
6257+
},
6258+
},
6259+
],
6260+
}
6261+
`;
6262+
60046263
exports[`SqlCursorPagination > supports field aliases 1`] = `2`;
60056264

60066265
exports[`SqlCursorPagination > supports field aliases 2`] = `

src/cursor.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,17 @@ export function buildCursor<TNode extends Record<string, unknown>>({
155155
if (!(alias in node)) {
156156
throw new ErrUnexpected(`"${alias}" field is missing`);
157157
}
158-
fields[alias] = FieldValue.parse(node[alias]);
158+
const value = node[alias];
159+
if (value instanceof Date) {
160+
try {
161+
const resolved = value.toISOString();
162+
fields[alias] = resolved;
163+
} catch (e) {
164+
throw new ErrUnexpected(`Invalid date in "${alias}" field`);
165+
}
166+
} else {
167+
fields[alias] = FieldValue.parse(value);
168+
}
159169
}
160170

161171
return { fields, queryName };

0 commit comments

Comments
 (0)