Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Update entity handling for runtime verb parameters #55

Merged
merged 2 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -6,8 +6,8 @@
} from '@comake/openapi-operation-executor';
import { OpenApiOperationExecutor } from '@comake/openapi-operation-executor';
import { getIdFromNodeObjectIfDefined, type ReferenceNodeObject } from '@comake/rmlmapper-js';
import axios from 'axios';

Check failure on line 9 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
import type { AxiosError, AxiosResponse } from 'axios';

Check failure on line 10 in src/SklEngine.ts

View workflow job for this annotation

GitHub Actions / lint

`axios` import should occur before import of `axios`
import type { ContextDefinition, GraphObject, NodeObject } from 'jsonld';
import type { Frame } from 'jsonld/jsonld-spec';
import { JSONPath } from 'jsonpath-plus';
Expand Down Expand Up @@ -53,9 +53,9 @@
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

export type VerbHandler = <T extends OrArray<NodeObject> = OrArray<NodeObject>>(
params: JSONObject,
Expand Down Expand Up @@ -630,15 +630,49 @@
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
Loading