Add order-by exercising to the JS test script.
This commit is contained in:
parent
c36be57018
commit
0789219551
1 changed files with 44 additions and 3 deletions
|
@ -21,7 +21,9 @@ var schema = {
|
||||||
|
|
||||||
async function testOpen() {
|
async function testOpen() {
|
||||||
// Open a database.
|
// Open a database.
|
||||||
let db = await datomish.open("/tmp/testing.db");
|
let path = "/tmp/testing" + Date.now() + ".db";
|
||||||
|
console.log("Opening " + path);
|
||||||
|
let db = await datomish.open(path);
|
||||||
|
|
||||||
// Make sure we have our current schema.
|
// Make sure we have our current schema.
|
||||||
await db.ensureSchema(schema);
|
await db.ensureSchema(schema);
|
||||||
|
@ -39,7 +41,7 @@ async function testOpen() {
|
||||||
|
|
||||||
// A simple query.
|
// A simple query.
|
||||||
let results = await db.q("[:find [?url ...] :in $ :where [?e :page/url ?url]]");
|
let results = await db.q("[:find [?url ...] :in $ :where [?e :page/url ?url]]");
|
||||||
console.log("Query results: " + JSON.stringify(results));
|
console.log("Known URLs: " + JSON.stringify(results));
|
||||||
|
|
||||||
// Let's extend our schema. In the real world this would typically happen
|
// Let's extend our schema. In the real world this would typically happen
|
||||||
// across releases.
|
// across releases.
|
||||||
|
@ -56,7 +58,7 @@ async function testOpen() {
|
||||||
await db.transact([
|
await db.transact([
|
||||||
{"db/id": datomish.tempid(),
|
{"db/id": datomish.tempid(),
|
||||||
"page/url": "https://mozilla.org/",
|
"page/url": "https://mozilla.org/",
|
||||||
"page/visitedAt": (new Date())}
|
"page/visitedAt": new Date()}
|
||||||
]);
|
]);
|
||||||
|
|
||||||
// When did we most recently visit this page?
|
// When did we most recently visit this page?
|
||||||
|
@ -69,6 +71,45 @@ async function testOpen() {
|
||||||
{"inputs": {"url": "https://mozilla.org/"}}));
|
{"inputs": {"url": "https://mozilla.org/"}}));
|
||||||
console.log("Most recent visit: " + date);
|
console.log("Most recent visit: " + date);
|
||||||
|
|
||||||
|
// Add some more data about a couple of pages.
|
||||||
|
let start = Date.now();
|
||||||
|
let lr = datomish.tempid();
|
||||||
|
let reddit = datomish.tempid();
|
||||||
|
let res = await db.transact([
|
||||||
|
{"db/id": reddit,
|
||||||
|
"page/url": "http://reddit.com/",
|
||||||
|
"page/title": "Reddit",
|
||||||
|
"page/visitedAt": new Date(start)},
|
||||||
|
{"db/id": lr,
|
||||||
|
"page/url": "https://longreads.com/",
|
||||||
|
"page/title": "Longreads: The best longform stories on the web",
|
||||||
|
"page/visitedAt": (new Date(start + 100))},
|
||||||
|
|
||||||
|
// Two visits each.
|
||||||
|
{"db/id": lr,
|
||||||
|
"page/visitedAt": (new Date(start + 200))},
|
||||||
|
{"db/id": reddit,
|
||||||
|
"page/visitedAt": (new Date(start + 300))}
|
||||||
|
]);
|
||||||
|
|
||||||
|
// These are our new persistent IDs. We can use these directly in later
|
||||||
|
// queries or transactions
|
||||||
|
lr = res.tempid(lr);
|
||||||
|
reddit = res.tempid(reddit);
|
||||||
|
console.log("Persistent IDs are " + lr + ", " + reddit + ".");
|
||||||
|
|
||||||
|
// A query with a limit and order-by. Because we limit to 2, and order
|
||||||
|
// by most recent visit date first, we won't get mozilla.org in our results.
|
||||||
|
let recent = await db.q(
|
||||||
|
`[:find ?url (max ?date)
|
||||||
|
:in $
|
||||||
|
:where
|
||||||
|
[?page :page/url ?url]
|
||||||
|
[?page :page/visitedAt ?date]]`,
|
||||||
|
{"limit": 2, "order-by": [["_max_date", "desc"]]});
|
||||||
|
|
||||||
|
console.log("Recently visited: " + JSON.stringify(recent));
|
||||||
|
|
||||||
// Close: we're done!
|
// Close: we're done!
|
||||||
await db.close();
|
await db.close();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue