[tracker/wip/carlosg/doc-updates: 20/23] docs: Add Javascript examples




commit b909267bcecf74defce95fb967782a952f5d00b6
Author: Carlos Garnacho <carlosg gnome org>
Date:   Mon Mar 14 02:23:24 2022 +0100

    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(-)
---
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.
 
-<div class="gi-lang-c gi-lang-javascript">
+<div class="gi-lang-c">
 
 {{ examples/connection-example.c }}
 
@@ -43,6 +43,11 @@ The same applies to [](tracker_sparql_connection_close) when no longer needed.
 
 {{ examples/connection-example.py }}
 
+</div>
+<div class="gi-lang-javascript">
+
+{{ examples/connection-example.js }}
+
 </div>
 
 ## 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).
 
-<div class="gi-lang-c gi-lang-javascript">
+<div class="gi-lang-c">
 
 {{ examples/private-store-example.c }}
 
@@ -74,6 +79,11 @@ Once you no longer need the connection, remember to call
 
 {{ examples/private-store-example.py }}
 
+</div>
+<div class="gi-lang-javascript">
+
+{{ examples/private-store-example.js }}
+
 </div>
 
 ## 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).
 
-<div class="gi-lang-c gi-lang-javascript">
+<div class="gi-lang-c">
 
 {{ examples/endpoint-example.c }}
 
@@ -96,6 +106,11 @@ may query e.g. through a connection created with
 
 {{ examples/endpoint-example.py }}
 
+</div>
+<div class="gi-lang-javascript">
+
+{{ examples/endpoint-example.js }}
+
 </div>
 
 ## 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.
 
-<div class="gi-lang-c gi-lang-javascript">
+<div class="gi-lang-c">
 
 {{ examples/notifier-example.c }}
 
@@ -119,3 +134,8 @@ notifications on database updates.
 {{ examples/notifier-example.py }}
 
 </div>
+<div class="gi-lang-javascript">
+
+{{ examples/notifier-example.js }}
+
+</div>
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}`)
+}


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]