Skip to content

Commit

Permalink
Merge pull request #55 from comake/update_database_mapping_functions
Browse files Browse the repository at this point in the history
feat: Update entity handling for runtime verb parameters
  • Loading branch information
jagzmz authored Dec 2, 2024
2 parents 84a551e + bcd4465 commit 00ab3b6
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ rmlmapper-5.0.0-r362-all.jar
tmp
comake-skl-js-engine-*.tgz
test/deploy/package-lock.json
.env
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@comake/skl-js-engine",
"version": "0.25.0",
"version": "0.25.1",
"description": "Standard Knowledge Language Javascript Engine",
"keywords": [
"skl",
Expand Down
38 changes: 36 additions & 2 deletions src/SklEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ import {
getValueIfDefined,
ensureArray,
} from './util/Util';
import { SKL, SHACL, RDFS, SKL_ENGINE, XSD, RDF } from './util/Vocabularies';
import { SKL, SHACL, RDFS, SKL_ENGINE, XSD, RDF, SKLSO_PROPERTY, SKLSO_DATA_NAMESPACE } from './util/Vocabularies';
import { GroupByOptions, GroupByResponse } from './storage/GroupOptionTypes';

Check failure on line 57 in src/SklEngine.ts

View workflow job for this annotation

GitHub Actions / lint

All imports in the declaration are only used as types. Use `import type`

Check failure on line 57 in src/SklEngine.ts

View workflow job for this annotation

GitHub Actions / lint

`./storage/GroupOptionTypes` import should occur before import of `./storage/operator/Exists`
import { AxiosRequestConfig } from 'axios';

Check failure on line 58 in src/SklEngine.ts

View workflow job for this annotation

GitHub Actions / lint

All imports in the declaration are only used as types. Use `import type`

Check failure on line 58 in src/SklEngine.ts

View workflow job for this annotation

GitHub Actions / lint

`axios` import should occur before import of `jsonld`

Check failure on line 58 in src/SklEngine.ts

View workflow job for this annotation

GitHub Actions / lint

'/home/runner/work/skl-js-engine/skl-js-engine/node_modules/axios/index.js' imported multiple times

Expand Down Expand Up @@ -630,15 +630,49 @@ export class SKLEngine {
return args;
}


Check failure on line 633 in src/SklEngine.ts

View workflow job for this annotation

GitHub Actions / lint

Trailing spaces not allowed

Check failure on line 633 in src/SklEngine.ts

View workflow job for this annotation

GitHub Actions / lint

More than 1 blank line not allowed

private replaceTypeAndId(entity: Record<string, any>): Record<string, any> {
if (typeof entity !== 'object') {
throw new Error('Entity is not an object');
}
const clonedEntity = structuredClone(entity);
if (clonedEntity[SKLSO_PROPERTY.type]) {
clonedEntity['@type'] = clonedEntity[SKLSO_PROPERTY.type];

Check failure on line 641 in src/SklEngine.ts

View workflow job for this annotation

GitHub Actions / lint

Expected indentation of 6 spaces but found 8
}
if (clonedEntity[SKLSO_PROPERTY.identifier]) {
clonedEntity['@id'] = SKLSO_DATA_NAMESPACE + clonedEntity[SKLSO_PROPERTY.identifier];
}
return clonedEntity;
}

private async updateEntityFromVerbArgs(args: Record<string, any>): Promise<void> {
await this.update(args.id ?? args.ids, args.attributes);
let ids = args.id ?? args.ids;
if (!Array.isArray(ids)) {
ids = [ids];
}
ids = ids.map((id: string) => `${SKLSO_DATA_NAMESPACE}${id}`);
await this.update(ids, args.attributes);
}

private async saveEntityOrEntitiesFromVerbArgs(args: Record<string, any>): Promise<OrArray<Entity>> {

if (args.entity && typeof args.entity === 'object') {
args.entity = this.replaceTypeAndId(args.entity);
}
if (args.entities && Array.isArray(args.entities)) {
args.entities = args.entities.map(this.replaceTypeAndId);
}
return await this.save(args.entity ?? args.entities);
}

private async destroyEntityOrEntitiesFromVerbArgs(args: Record<string, any>): Promise<OrArray<Entity>> {
if (args.entity && typeof args.entity === 'object') {
args.entity = this.replaceTypeAndId(args.entity);
}
if (args.entities && Array.isArray(args.entities)) {
args.entities = args.entities.map(this.replaceTypeAndId);
}
return await this.destroy(args.entity ?? args.entities);
}

Expand Down
8 changes: 8 additions & 0 deletions src/util/Vocabularies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,14 @@ export const SKL = createNamespace(SKL_NAMESPACE, [
'headers',
]);

export const SKLSO_DATA_NAMESPACE = 'https://skl.so/d/';

export const SKLSO_PROPERTY_NAMESPACE = 'https://skl.so/p/';
export const SKLSO_PROPERTY = createNamespace(SKLSO_PROPERTY_NAMESPACE, [
'type',
'identifier',
]);

export const SKL_ENGINE_NAMESPACE = 'https://standardknowledge.com/ontologies/skl-engine/';
export const SKL_ENGINE = createNamespace(SKL_ENGINE_NAMESPACE, [
'update',
Expand Down

0 comments on commit 00ab3b6

Please sign in to comment.