From b909267bcecf74defce95fb967782a952f5d00b6 Mon Sep 17 00:00:00 2001 From: Carlos Garnacho Date: Mon, 14 Mar 2022 02:23:24 +0100 Subject: docs: Add Javascript examples --- docs/reference/libtracker-sparql/examples.md | 28 ++++++++++++++++--- .../examples/connection-example.js | 32 ++++++++++++++++++++++ .../libtracker-sparql/examples/endpoint-example.js | 23 ++++++++++++++++ .../libtracker-sparql/examples/notifier-example.js | 22 +++++++++++++++ .../examples/private-store-example.js | 26 ++++++++++++++++++ 5 files changed, 127 insertions(+), 4 deletions(-) create mode 100755 docs/reference/libtracker-sparql/examples/connection-example.js create mode 100755 docs/reference/libtracker-sparql/examples/endpoint-example.js create mode 100755 docs/reference/libtracker-sparql/examples/notifier-example.js create mode 100755 docs/reference/libtracker-sparql/examples/private-store-example.js diff --git a/docs/reference/libtracker-sparql/examples.md b/docs/reference/libtracker-sparql/examples.md index 710ad8702..474ac46d8 100644 --- a/docs/reference/libtracker-sparql/examples.md +++ b/docs/reference/libtracker-sparql/examples.md @@ -34,7 +34,7 @@ main loop is not blocked while these operations are executed. Once you end up with the query, remember to call [](tracker_sparql_cursor_close). The same applies to [](tracker_sparql_connection_close) when no longer needed. -
+
{{ examples/connection-example.c }} @@ -43,6 +43,11 @@ The same applies to [](tracker_sparql_connection_close) when no longer needed. {{ examples/connection-example.py }} +
+
+ +{{ examples/connection-example.js }} +
## Creating a private database @@ -65,7 +70,7 @@ main loop is not blocked while these operations are executed. Once you no longer need the connection, remember to call [](tracker_sparql_connection_close) on the [](TrackerSparqlConnection). -
+
{{ examples/private-store-example.c }} @@ -74,6 +79,11 @@ Once you no longer need the connection, remember to call {{ examples/private-store-example.py }} +
+
+ +{{ examples/private-store-example.js }} +
## Creating a SPARQL endpoint @@ -87,7 +97,7 @@ concretely the creation of a D-Bus endpoint, that other applications may query e.g. through a connection created with [](tracker_sparql_connection_bus_new). -
+
{{ examples/endpoint-example.c }} @@ -96,6 +106,11 @@ may query e.g. through a connection created with {{ examples/endpoint-example.py }} +
+
+ +{{ examples/endpoint-example.js }} +
## Receiving notification on changes @@ -109,7 +124,7 @@ on changes of certain RDF classes (Those with the This example demonstrates the use of [](TrackerNotifier) to receive notifications on database updates. -
+
{{ examples/notifier-example.c }} @@ -119,3 +134,8 @@ notifications on database updates. {{ examples/notifier-example.py }}
+
+ +{{ examples/notifier-example.js }} + +
diff --git a/docs/reference/libtracker-sparql/examples/connection-example.js b/docs/reference/libtracker-sparql/examples/connection-example.js new file mode 100755 index 000000000..ce26026c5 --- /dev/null +++ b/docs/reference/libtracker-sparql/examples/connection-example.js @@ -0,0 +1,32 @@ +#!/usr/bin/gjs + +const { GLib, Gio, Tracker } = imports.gi + +try { + let connection = Tracker.SparqlConnection.bus_new( + 'org.freedesktop.Tracker3.Miner.Files', + null, null); + + let stmt = connection.query_statement ( + 'SELECT DISTINCT nie:url(?u) WHERE { ' + + ' ?u a nfo:FileDataObject ; ' + + ' nfo:fileName ~name ' + + '}', null); + + stmt.bind_string('name', ARGV[0]); + + let cursor = stmt.execute(null); + let i = 0; + + while (cursor.next(null)) { + i++; + print(`Result ${i}: ${cursor.get_string(0)[0]}`); + } + + print(`A total of ${i} results were found`); + + cursor.close(); + connection.close(); +} catch (e) { + printerr(`Error: ${e.message}`) +} diff --git a/docs/reference/libtracker-sparql/examples/endpoint-example.js b/docs/reference/libtracker-sparql/examples/endpoint-example.js new file mode 100755 index 000000000..7304743f7 --- /dev/null +++ b/docs/reference/libtracker-sparql/examples/endpoint-example.js @@ -0,0 +1,23 @@ +#!/usr/bin/gjs + +const { GLib, Gio, Tracker } = imports.gi + +try { + let connection = Tracker.SparqlConnection.new( + Tracker.SparqlConnectionFlags.NONE, + null, // Database location, None creates it in-memory + Tracker.sparql_get_ontology_nepomuk(), // Ontology location + null); + + let bus = Gio.bus_get_sync(Gio.BusType.SESSION, null) + + let endpoint = Tracker.EndpointDBus.new( + connection, bus, null, null); + + let loop = GLib.MainLoop.new(null, false); + loop.run(); + + connection.close(); +} catch (e) { + printerr(`Error: ${e.message}`) +} diff --git a/docs/reference/libtracker-sparql/examples/notifier-example.js b/docs/reference/libtracker-sparql/examples/notifier-example.js new file mode 100755 index 000000000..ea34fdb3f --- /dev/null +++ b/docs/reference/libtracker-sparql/examples/notifier-example.js @@ -0,0 +1,22 @@ +#!/usr/bin/gjs + +const { GLib, Gio, Tracker } = imports.gi + +try { + let connection = Tracker.SparqlConnection.bus_new( + 'org.freedesktop.Tracker3.Miner.Files', + null, null); + + let notifier = connection.create_notifier(); + notifier.connect('events', (service, graph, events) => { + for (let event in events) + print (`Event ${event.get_event_type()} on ${event.get_urn()}`); + }); + + let loop = GLib.MainLoop.new(null, false); + loop.run(); + + connection.close(); +} catch (e) { + printerr(`Error: ${e.message}`) +} diff --git a/docs/reference/libtracker-sparql/examples/private-store-example.js b/docs/reference/libtracker-sparql/examples/private-store-example.js new file mode 100755 index 000000000..ff950edb3 --- /dev/null +++ b/docs/reference/libtracker-sparql/examples/private-store-example.js @@ -0,0 +1,26 @@ +#!/usr/bin/gjs + +const { GLib, Gio, Tracker } = imports.gi + +try { + let connection = Tracker.SparqlConnection.new( + Tracker.SparqlConnectionFlags.NONE, + null, // Database location, None creates it in-memory + Tracker.sparql_get_ontology_nepomuk(), // Ontology location + null); + + // Create a resource containing RDF data + let resource = Tracker.Resource.new(null) + resource.set_uri('rdf:type', 'nmm:MusicPiece') + + // Create a batch, and add the resource to it + let batch = connection.create_batch() + batch.add_resource(null, resource) + + // Execute the batch to insert the data + batch.execute(null) + + connection.close(); +} catch (e) { + printerr(`Error: ${e.message}`) +} -- cgit v1.2.1