0.1.1 • Published 2 years ago

ngx-comunica v0.1.1

Weekly downloads
-
License
-
Repository
-
Last release
2 years ago

Comunica for Angular

This service makes it easier to use Comunica from an Angular app.

Install

npm i --save ngx-comunica

Add to angular.json

"scripts": [
    ...
    "node_modules/ngx-comunica/assets/comunica-browser.js"
]

Add to app.module.ts

import { ComunicaModule } from 'ngx-comunica';

@NgModule({
  imports: [
    ...,
    ComunicaModule
  ]
})

Use

SELECT queries

Return query results one by one as an observable

This is the preferred approach since it provides the user with results immediately instead of having to wait for the whole query execution to be finished.

import { ComunicaService, ResponseType, Source, SourceType } from 'ngx-comunica';
import * as N3 from "n3";

export class MyComponent {

    public queryResults: any[] = [];
    public variables: string[] = [];
    public queryComplete: boolean = false;
    public store: N3.Store = new N3.Store();

    constructor(
        private _comunica: ComunicaService
    ) { }

    setStore(){
        const source = new Source(this.store, SourceType.RDFJS, "main");
        this._comunica.addSource(this.store);
    }

    executeQuery(query: string){

        this.queryResults = [];
        this.variables = [];
        this.queryComplete = false;

        const extensionFunctions = [];  // Optional extension functions
        this._comunica.selectQuery(query, extensionFunctions, ResponseType.SINGLE).subscribe({
            next: (res: any) => {

                if(res == undefined) return;

                // Extract variable names from first result
                if(!this.variables.length){
                    this.variables = Object.keys(res);
                }

                this.queryResults.push(res);

            },
            complete: () => this.queryComplete = true,
            error: (err) => console.log(err)
        })

    }

Return all query results when finished as promise

This approach is only recommended in situations where you need the full result once.

import { ComunicaService, ResponseType } from 'ngx-comunica';
import { lastValueFrom } from 'rxjs';
import * as N3 from "n3";

export class MyComponent {

    public queryResults: any[] = [];
    public variables: string[] = [];
    public queryComplete: boolean = false;
    public store: N3.Store = new N3.Store();

    // ...(see SELECT EXAMPLE)

    async executeQuery(query: string){

        this.queryResults = [];
        this.variables = [];
        this.queryComplete = false;

        try{
            const extensionFunctions = [];  // Optional extension functions
            this.queryResults = await lastValueFrom(this._comunica.selectQuery(query, extensionFunctions, ResponseType.ACCUMULATED));
            this.variables = Object.keys(this.queryResults[0]);
            this.queryComplete = true;
        }catch(err){
            console.log(err);
        }

    }

Update queries

It is possible to execute update queries. When doing so, the content of the store is extended (if there are matches to the query).

import { ComunicaService, Serialization } from 'ngx-comunica';
import { lastValueFrom } from 'rxjs';
import * as N3 from "n3";

export class MyComponent {

    public queryResult?: any;
    public queryComplete: boolean = false;
    public store: N3.Store = new N3.Store();

    // ...(see SELECT EXAMPLE)

    async executeQuery(query: string){

        this.queryResult = undefined;
        this.queryComplete = false;

        try{
            const extensionFunctions = [];  // Optional extension functions
            this.queryResult = await this._comunica.updateQuery(query, extensionFunctions);
            this.queryComplete = true;
        }catch(err){
            console.log(err);
        }

    }

Construct queries

Construct query results are returned as a promise in the desired serialization (Default is JSON-LD).

import { ComunicaService, Serialization } from 'ngx-comunica';
import { lastValueFrom } from 'rxjs';
import * as N3 from "n3";

export class MyComponent {

    public queryResult?: any;
    public queryComplete: boolean = false;
    public store: N3.Store = new N3.Store();

    // ...(see SELECT EXAMPLE)

    async executeQuery(query: string){

        this.queryResult = undefined;
        this.queryComplete = false;

        try{
            const extensionFunctions = [];  // Optional extension functions
            this.queryResult = await this._comunica.constructQuery(query, extensionFunctions, Serialization.Turtle);
            this.queryComplete = true;
        }catch(err){
            console.log(err);
        }

    }
0.1.0

2 years ago

0.1.1

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago