Skip to content

Commit

Permalink
feat(sparql-in-memory): in memory query executor
Browse files Browse the repository at this point in the history
  • Loading branch information
adlerfaulkner committed Nov 2, 2023
1 parent 241c244 commit 301b413
Show file tree
Hide file tree
Showing 34 changed files with 13,411 additions and 8,770 deletions.
19,788 changes: 12,611 additions & 7,177 deletions package-lock.json

Large diffs are not rendered by default.

11 changes: 8 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@comake/skl-js-engine",
"version": "0.15.5",
"version": "0.16.0",
"description": "Standard Knowledge Language Javascript Engine",
"keywords": [
"skl",
Expand Down Expand Up @@ -44,15 +44,20 @@
"dependencies": {
"@comake/openapi-operation-executor": "^0.11.1",
"@comake/rmlmapper-js": "^0.5.2",
"@comunica/query-sparql-rdfjs": "^2.10.0",
"@rdfjs/data-model": "^1.3.0",
"jsonld": "^8.1.0",
"jsonpath-plus": "^7.2.0",
"luxon": "^3.1.1",
"memory-level": "^1.0.0",
"mime-types": "^2.1.35",
"n3": "^1.16.2",
"n3": "^1.17.2",
"quadstore": "13.0.0-alpha.1",
"quadstore-comunica": "^3.3.0",
"rdf-data-factory": "^1.1.2",
"rdf-validate-shacl": "^0.4.4",
"sparql-http-client": "^2.4.1",
"sparqljs": "^3.6.2"
"sparqljs": "^3.7.1"
},
"devDependencies": {
"@commitlint/cli": "^17.0.3",
Expand Down
10 changes: 0 additions & 10 deletions src/Callbacks.ts

This file was deleted.

60 changes: 29 additions & 31 deletions src/SklEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,15 @@ import type { Frame } from 'jsonld/jsonld-spec';
import { JSONPath } from 'jsonpath-plus';
import SHACLValidator from 'rdf-validate-shacl';
import type ValidationReport from 'rdf-validate-shacl/src/validation-report';
import type { Callbacks } from './Callbacks';
import { Mapper } from './mapping/Mapper';
import type { SklEngineOptions } from './SklEngineOptions';
import type { FindAllOptions, FindOneOptions, FindOptionsWhere } from './storage/FindOptionsTypes';
import { MemoryQueryAdapter } from './storage/memory/MemoryQueryAdapter';
import { InversePath } from './storage/operator/InversePath';
import { ZeroOrMorePath } from './storage/operator/ZeroOrMorePath';
import type { QueryAdapter, RawQueryResult } from './storage/QueryAdapter';
import { SparqlQueryAdapter } from './storage/sparql/SparqlQueryAdapter';
import type { QueryAdapter, RawQueryResult } from './storage/query-adapter/QueryAdapter';
import { SparqlQueryAdapter } from './storage/query-adapter/sparql/SparqlQueryAdapter';
import type {
Callbacks,
OrArray,
Entity,
OperationResponse,
Expand Down Expand Up @@ -57,25 +56,15 @@ export type VerbInterface = Record<string, VerbHandler>;
export type MappingResponseOption<T extends boolean> = T extends true ? JSONObject : NodeObject;

export class SKLEngine {
private readonly adapter: QueryAdapter;
private readonly queryAdapter: QueryAdapter;
private readonly functions?: Record<string, (args: any | any[]) => any>;
private readonly inputFiles?: Record<string, string>;
private readonly globalCallbacks?: Callbacks;
private readonly disableValidation?: boolean;
public readonly verb: VerbInterface;

public constructor(options: SklEngineOptions) {
switch (options.type) {
case 'memory':
this.adapter = new MemoryQueryAdapter(options);
break;
case 'sparql':
this.adapter = new SparqlQueryAdapter(options);
break;
default:
throw new Error('No schema source found in setSchema args.');
}

this.queryAdapter = new SparqlQueryAdapter(options);
this.disableValidation = options.disableValidation;
this.globalCallbacks = options.callbacks;
this.inputFiles = options.inputFiles;
Expand All @@ -92,74 +81,83 @@ export class SKLEngine {
}

public async executeRawQuery<T extends RawQueryResult>(query: string): Promise<T[]> {
return await this.adapter.executeRawQuery<T>(query);
return await this.queryAdapter.executeRawQuery<T>(query);
}

public async executeRawEntityQuery(query: string, frame?: Frame): Promise<GraphObject> {
return await this.adapter.executeRawEntityQuery(query, frame);
return await this.queryAdapter.executeRawEntityQuery(query, frame);
}

public async find(options?: FindOneOptions): Promise<Entity> {
const entity = await this.adapter.find(options);
const entity = await this.queryAdapter.find(options);
if (entity) {
return entity;
}
throw new Error(`No schema found with fields matching ${JSON.stringify(options)}`);
}

public async findBy(where: FindOptionsWhere): Promise<Entity> {
const entity = await this.adapter.findBy(where);
const entity = await this.queryAdapter.findBy(where);
if (entity) {
return entity;
}
throw new Error(`No schema found with fields matching ${JSON.stringify(where)}`);
}

public async findAll(options?: FindAllOptions): Promise<Entity[]> {
return await this.adapter.findAll(options);
return await this.queryAdapter.findAll(options);
}

public async findAllBy(where: FindOptionsWhere): Promise<Entity[]> {
return await this.adapter.findAllBy(where);
return await this.queryAdapter.findAllBy(where);
}

public async exists(options?: FindAllOptions): Promise<boolean> {
return await this.adapter.exists(options);
return await this.queryAdapter.exists(options);
}

public async count(options?: FindAllOptions): Promise<number> {
return await this.adapter.count(options);
return await this.queryAdapter.count(options);
}

public async save(entity: Entity): Promise<Entity>;
public async save(entities: Entity[]): Promise<Entity[]>;
public async save(entityOrEntities: Entity | Entity[]): Promise<Entity | Entity[]> {
if (Array.isArray(entityOrEntities)) {
return await this.adapter.save(entityOrEntities);
return await this.queryAdapter.save(entityOrEntities);
}
return await this.adapter.save(entityOrEntities);
return await this.queryAdapter.save(entityOrEntities);
}

public async update(id: string, attributes: Partial<Entity>): Promise<void>;
public async update(ids: string[], attributes: Partial<Entity>): Promise<void>;
public async update(idOrIds: string | string[], attributes: Partial<Entity>): Promise<void> {
if (Array.isArray(idOrIds)) {
return await this.adapter.update(idOrIds, attributes);
return await this.queryAdapter.update(idOrIds, attributes);
}
return await this.queryAdapter.update(idOrIds, attributes);
}

public async delete(id: string): Promise<void>;
public async delete(ids: string[]): Promise<void>;
public async delete(idOrIds: string | string[]): Promise<void> {
if (Array.isArray(idOrIds)) {
return await this.queryAdapter.delete(idOrIds);
}
return await this.adapter.update(idOrIds, attributes);
return await this.queryAdapter.delete(idOrIds);
}

public async destroy(entity: Entity): Promise<Entity>;
public async destroy(entities: Entity[]): Promise<Entity[]>;
public async destroy(entityOrEntities: Entity | Entity[]): Promise<Entity | Entity[]> {
if (Array.isArray(entityOrEntities)) {
return await this.adapter.destroy(entityOrEntities);
return await this.queryAdapter.destroy(entityOrEntities);
}
return await this.adapter.destroy(entityOrEntities);
return await this.queryAdapter.destroy(entityOrEntities);
}

public async destroyAll(): Promise<void> {
return await this.adapter.destroyAll();
return await this.queryAdapter.destroyAll();
}

public async performMapping(
Expand Down
11 changes: 3 additions & 8 deletions src/SklEngineOptions.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import type { Callbacks } from './Callbacks';
import type { MemoryQueryAdapterOptions } from './storage/memory/MemoryQueryAdapterOptions';
import type { SparqlQueryAdapterOptions } from './storage/sparql/SparqlQueryAdapterOptions';
import type { SparqlQueryAdapterOptions } from './storage/query-adapter/sparql/SparqlQueryAdapterOptions';
import type { Callbacks } from './util/Types';

export type SklEngineStorageOptions =
| MemoryQueryAdapterOptions
| SparqlQueryAdapterOptions;

export type SklEngineOptions = SklEngineStorageOptions & {
export type SklEngineOptions = SparqlQueryAdapterOptions & {
/**
* Callbacks to execute upon events.
*/
Expand Down
17 changes: 7 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
// Mapping
export * from './mapping/Mapper';

// Storage/Memory
export * from './storage/memory/MemoryQueryAdapter';
export * from './storage/memory/MemoryQueryAdapterOptions';

// Storage/Operator
export * from './storage/operator/Equal';
export * from './storage/operator/GreaterThan';
Expand All @@ -22,16 +18,17 @@ export * from './storage/operator/SequencePath';
export * from './storage/operator/ZeroOrMorePath';

// Storage/Sparql
export * from './storage/sparql/SparqlQueryAdapter';
export * from './storage/sparql/SparqlQueryBuilder';
export * from './storage/sparql/SparqlQueryExecutor';
export * from './storage/sparql/SparqlUpdateBuilder';
export * from './storage/sparql/VariableGenerator';
export * from './storage/query-adapter/sparql/SparqlQueryAdapter';
export * from './storage/query-adapter/sparql/SparqlQueryBuilder';
export * from './storage/query-adapter/sparql/SparqlUpdateBuilder';
export * from './storage/query-adapter/sparql/VariableGenerator';
export * from './storage/query-adapter/sparql/query-executor/InMemorySparqlQueryExecutor';
export * from './storage/query-adapter/sparql/query-executor/SparqlEndpointQueryExecutor';

// Storage
export * from './storage/FindOperator';
export * from './storage/FindOptionsTypes';
export * from './storage/QueryAdapter';
export * from './storage/query-adapter/QueryAdapter';

// Util
export * from './util/TripleUtil';
Expand Down
13 changes: 0 additions & 13 deletions src/storage/BaseQueryAdapterOptions.ts

This file was deleted.

Loading

0 comments on commit 301b413

Please sign in to comment.