2016-09-21 21:07:04 +00:00
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
2016-09-22 22:56:20 +00:00
|
|
|
var datomish = require("../../target/release-node/datomish.js");
|
2016-09-21 21:07:04 +00:00
|
|
|
|
|
|
|
var schema = {
|
|
|
|
"name": "pages",
|
|
|
|
"attributes": [
|
|
|
|
{"name": "page/url",
|
|
|
|
"type": "string",
|
|
|
|
"cardinality": "one",
|
|
|
|
"unique": "identity",
|
|
|
|
"doc": "A page's URL."},
|
|
|
|
{"name": "page/title",
|
|
|
|
"type": "string",
|
|
|
|
"cardinality": "one",
|
2016-10-05 19:54:48 +00:00
|
|
|
"doc": "A page's title."}
|
2016-09-21 21:07:04 +00:00
|
|
|
]
|
|
|
|
};
|
|
|
|
|
|
|
|
async function testOpen() {
|
2016-10-05 19:54:48 +00:00
|
|
|
// Open a database.
|
2016-09-21 21:07:04 +00:00
|
|
|
let db = await datomish.open("/tmp/testing.db");
|
2016-10-05 19:54:48 +00:00
|
|
|
|
|
|
|
// Make sure we have our current schema.
|
2016-09-21 21:07:04 +00:00
|
|
|
await db.ensureSchema(schema);
|
2016-10-05 19:54:48 +00:00
|
|
|
|
|
|
|
// Add some data. Note that we use a temporary ID (the real ID
|
|
|
|
// will be assigned by Datomish).
|
|
|
|
let txResult = await db.transact([
|
|
|
|
{"db/id": datomish.tempid(),
|
|
|
|
"page/url": "https://mozilla.org/",
|
|
|
|
"page/title": "Mozilla"}
|
|
|
|
]);
|
|
|
|
|
2016-09-21 21:07:04 +00:00
|
|
|
console.log("Transaction returned " + JSON.stringify(txResult));
|
|
|
|
console.log("Transaction instant: " + txResult.txInstant);
|
2016-10-05 19:54:48 +00:00
|
|
|
|
|
|
|
// A simple query.
|
2016-10-07 19:03:11 +00:00
|
|
|
let results = await db.q("[:find [?url ...] :in $ :where [?e :page/url ?url]]");
|
2016-09-21 21:07:04 +00:00
|
|
|
console.log("Query results: " + JSON.stringify(results));
|
2016-10-05 19:54:48 +00:00
|
|
|
|
|
|
|
// Let's extend our schema. In the real world this would typically happen
|
|
|
|
// across releases.
|
|
|
|
schema.attributes.push({"name": "page/visitedAt",
|
|
|
|
"type": "instant",
|
|
|
|
"cardinality": "many",
|
|
|
|
"doc": "A visit to the page."});
|
|
|
|
await db.ensureSchema(schema);
|
|
|
|
|
|
|
|
// Now we can make assertions with the new vocabulary about existing
|
|
|
|
// entities.
|
|
|
|
// Note that we simply let Datomish find which page we're talking about by
|
|
|
|
// URL -- the URL is a unique property -- so we just use a tempid again.
|
|
|
|
await db.transact([
|
|
|
|
{"db/id": datomish.tempid(),
|
|
|
|
"page/url": "https://mozilla.org/",
|
|
|
|
"page/visitedAt": (new Date())}
|
|
|
|
]);
|
|
|
|
|
|
|
|
// When did we most recently visit this page?
|
|
|
|
let date = (await db.q(
|
2016-10-07 19:03:11 +00:00
|
|
|
`[:find (max ?date) .
|
2016-10-05 19:54:48 +00:00
|
|
|
:in $ ?url
|
|
|
|
:where
|
|
|
|
[?page :page/url ?url]
|
|
|
|
[?page :page/visitedAt ?date]]`,
|
2016-10-07 19:03:11 +00:00
|
|
|
{"inputs": {"url": "https://mozilla.org/"}}));
|
2016-10-05 19:54:48 +00:00
|
|
|
console.log("Most recent visit: " + date);
|
|
|
|
|
|
|
|
// Close: we're done!
|
2016-09-21 21:07:04 +00:00
|
|
|
await db.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
testOpen()
|
|
|
|
.then((r) => console.log("Done."))
|
|
|
|
.catch((e) => console.log("Failure: " + e.stack));
|