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/path operators #34

Merged
merged 2 commits into from
Sep 20, 2023
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
8 changes: 4 additions & 4 deletions jest.coverage.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ module.exports = {
...jestConfig,
coverageThreshold: {
'./src': {
branches: 94,
functions: 95,
lines: 95,
statements: 95,
branches: 89,
functions: 93,
lines: 93,
statements: 92,
},
},
};
7 changes: 6 additions & 1 deletion src/SklEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ 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 {
Expand Down Expand Up @@ -651,7 +653,10 @@ export class SKLEngine {
return (await this.findBy({
type: SKL.VerbNounMapping,
[SKL.verb]: verbId,
[SKL.noun]: noun,
[SKL.noun]: InversePath({
subPath: ZeroOrMorePath({ subPath: RDFS.subClassOf as string }),
value: noun,
}),
})) as VerbNounMapping;
}

Expand Down
4 changes: 4 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,15 @@ export * from './storage/operator/GreaterThan';
export * from './storage/operator/GreaterThanOrEqual';
export * from './storage/operator/In';
export * from './storage/operator/Inverse';
export * from './storage/operator/InversePath';
export * from './storage/operator/InverseRelation';
export * from './storage/operator/InverseRelationOrder';
export * from './storage/operator/LessThan';
export * from './storage/operator/LessThanOrEqual';
export * from './storage/operator/Not';
export * from './storage/operator/OneOrMorePath';
export * from './storage/operator/SequencePath';
export * from './storage/operator/ZeroOrMorePath';

// Storage/Sparql
export * from './storage/sparql/SparqlQueryAdapter';
Expand Down
27 changes: 19 additions & 8 deletions src/storage/FindOperator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,23 @@ export type FindOperatorType =
| 'lte'
| 'inverse'
| 'inverseRelation'
| 'inverseRelationOrder';
| 'inverseRelationOrder'
| 'sequencePath'
| 'zeroOrMorePath'
| 'inversePath'
| 'oneOrMorePath';

export interface FindOperatorArgs<T> {
operator: FindOperatorType;
value: T | FindOperator<T>;
export interface FindOperatorArgs<T, TType> {
operator: TType;
value: T | FindOperator<T, any>;
}

export class FindOperator<T> {
export class FindOperator<T, TType extends FindOperatorType> {
public readonly type = 'operator';
public readonly operator: FindOperatorType;
public readonly value: T | FindOperator<T>;
public readonly operator: TType;
public readonly value: T | FindOperator<T, any>;

public constructor(args: FindOperatorArgs<T>) {
public constructor(args: FindOperatorArgs<T, TType>) {
this.operator = args.operator;
this.value = args.value;
}
Expand All @@ -30,4 +34,11 @@ export class FindOperator<T> {
'type' in value &&
value.type === 'operator';
}

public static isPathOperator(operator: FindOperator<any, any>): boolean {
return operator.operator === 'inversePath' ||
operator.operator === 'zeroOrMorePath' ||
operator.operator === 'sequencePath' ||
operator.operator === 'zeroOrMorePath';
}
}
19 changes: 12 additions & 7 deletions src/storage/FindOptionsTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@ export interface FindOneOptions {
}

export type FindOptionsRelations = {
[k: string]: boolean | FindOptionsRelations | FindOperator<InverseRelationOperatorValue>;
[k: string]: boolean | FindOptionsRelations | FindOperator<InverseRelationOperatorValue, 'inverseRelation'>;
};

export type FindOptionsOrderValue = 'ASC' | 'DESC' | 'asc' | 'desc' | 1 | -1;

export type FindOptionsOrder = Record<string, FindOptionsOrderValue | FindOperator<InverseRelationOrderValue>>;
export type FindOptionsOrder =
Record<string, FindOptionsOrderValue | FindOperator<InverseRelationOrderValue, 'inverseRelationOrder'>>;

export type FieldPrimitiveValue = boolean | number | string | Date;

Expand Down Expand Up @@ -56,15 +57,19 @@ export type FindOptionsWhereField =
| OrArray<FieldPrimitiveValue>
| ValueObject
| Exclude<FindOptionsWhere, 'search'>
| OrArray<FindOperator<any>>;
| OrArray<FindOperator<any, any>>;

export type IdOrTypeFindOptionsWhereField =
export type IdFindOptionsWhereField =
| string
| FindOperator<string | string[] | FindOptionsWhere>;
| FindOperator<any, 'in' | 'not' | 'equal' | 'inversePath'>;

export type TypeFindOptionsWhereField =
| string
| FindOperator<string | string[] | FindOptionsWhere, 'in' | 'not' | 'equal' | 'inverse'>;

export interface FindOptionsWhere {
type?: IdOrTypeFindOptionsWhereField;
id?: IdOrTypeFindOptionsWhereField;
type?: TypeFindOptionsWhereField;
id?: IdFindOptionsWhereField;
[k: string]: FindOptionsWhereField | undefined;
}

Expand Down
16 changes: 10 additions & 6 deletions src/storage/memory/MemoryQueryAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,10 @@ export class MemoryQueryAdapter implements QueryAdapter {
): Promise<boolean> {
if (FindOperator.isFindOperator(fieldValue)) {
return await this.handleOperator(
(fieldValue as FindOperator<string>).operator,
(fieldValue as FindOperator<string, any>).operator,
{
in: async(): Promise<boolean> => {
const values = (fieldValue as FindOperator<FieldPrimitiveValue[]>).value as FieldPrimitiveValue[];
const values = (fieldValue as FindOperator<FieldPrimitiveValue[], 'in'>).value as FieldPrimitiveValue[];
for (const valueItem of values) {
if (await this.entityMatchesField(entity, fieldName, valueItem)) {
return true;
Expand All @@ -133,14 +133,14 @@ export class MemoryQueryAdapter implements QueryAdapter {
return false;
},
not: async(): Promise<boolean> => {
if (FindOperator.isFindOperator((fieldValue as FindOperator<string>).value)) {
return !await this.entityMatchesField(entity, fieldName, (fieldValue as FindOperator<string>).value);
if (FindOperator.isFindOperator((fieldValue as FindOperator<string, 'not'>).value)) {
return !await this.entityMatchesField(entity, fieldName, (fieldValue as FindOperator<string, any>).value);
}
const valueItem = (fieldValue as FindOperator<string>).value as string;
const valueItem = (fieldValue as FindOperator<string, 'not'>).value as string;
return !await this.entityMatchesField(entity, fieldName, valueItem);
},
equal: async(): Promise<boolean> => {
const valueItem = (fieldValue as FindOperator<FieldPrimitiveValue>).value as FieldPrimitiveValue;
const valueItem = (fieldValue as FindOperator<FieldPrimitiveValue, 'equal'>).value as FieldPrimitiveValue;
return this.entityMatchesField(entity, fieldName, valueItem);
},
gt: async(): Promise<boolean> => false,
Expand All @@ -150,6 +150,10 @@ export class MemoryQueryAdapter implements QueryAdapter {
inverse: async(): Promise<boolean> => false,
inverseRelation: async(): Promise<boolean> => false,
inverseRelationOrder: async(): Promise<boolean> => false,
inversePath: async(): Promise<boolean> => false,
sequencePath: async(): Promise<boolean> => false,
zeroOrMorePath: async(): Promise<boolean> => false,
oneOrMorePath: async(): Promise<boolean> => false,
},
);
}
Expand Down
4 changes: 2 additions & 2 deletions src/storage/operator/Equal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import { FindOperator } from '../FindOperator';
// eslint-disable-next-line @typescript-eslint/naming-convention
export function Equal<T extends OrArray<number | Date | string | boolean | ValueObject>>(
value: T,
): FindOperator<T> {
return new FindOperator<T>({
): FindOperator<T, 'equal'> {
return new FindOperator({
operator: 'equal',
value,
});
Expand Down
4 changes: 2 additions & 2 deletions src/storage/operator/GreaterThan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { FindOperator } from '../FindOperator';
// eslint-disable-next-line @typescript-eslint/naming-convention
export function GreaterThan<T extends number | Date | string | ValueObject>(
value: T,
): FindOperator<T> {
return new FindOperator<T>({
): FindOperator<T, 'gt'> {
return new FindOperator({
operator: 'gt',
value,
});
Expand Down
4 changes: 2 additions & 2 deletions src/storage/operator/GreaterThanOrEqual.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { FindOperator } from '../FindOperator';
// eslint-disable-next-line @typescript-eslint/naming-convention
export function GreaterThanOrEqual<T extends number | Date | string | ValueObject>(
value: T,
): FindOperator<T> {
return new FindOperator<T>({
): FindOperator<T, 'gte'> {
return new FindOperator({
operator: 'gte',
value,
});
Expand Down
4 changes: 2 additions & 2 deletions src/storage/operator/In.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { FindOperator } from '../FindOperator';
// eslint-disable-next-line @typescript-eslint/naming-convention
export function In<T extends number | Date | string | boolean | ValueObject>(
value: T[],
): FindOperator<T[]> {
return new FindOperator<T[]>({
): FindOperator<T[], 'in'> {
return new FindOperator({
operator: 'in',
value,
});
Expand Down
6 changes: 3 additions & 3 deletions src/storage/operator/Inverse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { FindOperator } from '../FindOperator';

// eslint-disable-next-line @typescript-eslint/naming-convention
export function Inverse<T>(
value: T | FindOperator<T>,
): FindOperator<T> {
return new FindOperator<T>({
value: T | FindOperator<T, any>,
): FindOperator<T, 'inverse'> {
return new FindOperator({
operator: 'inverse',
value,
});
Expand Down
16 changes: 16 additions & 0 deletions src/storage/operator/InversePath.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { FindOperator } from '../FindOperator';

export interface InversePathValue {
subPath: string | FindOperator<any, 'sequencePath' | 'zeroOrMorePath' | 'oneOrMorePath'>;
value?: string;
}

// eslint-disable-next-line @typescript-eslint/naming-convention
export function InversePath<
T extends InversePathValue
>(value: T): FindOperator<T, 'inversePath'> {
return new FindOperator({
operator: 'inversePath',
value,
});
}
6 changes: 4 additions & 2 deletions src/storage/operator/InverseRelation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ export interface InverseRelationOperatorValue {
}

// eslint-disable-next-line @typescript-eslint/naming-convention
export function InverseRelation(value: InverseRelationOperatorValue): FindOperator<InverseRelationOperatorValue> {
return new FindOperator<InverseRelationOperatorValue>({
export function InverseRelation(
value: InverseRelationOperatorValue,
): FindOperator<InverseRelationOperatorValue, 'inverseRelation'> {
return new FindOperator({
operator: 'inverseRelation',
value,
});
Expand Down
4 changes: 2 additions & 2 deletions src/storage/operator/InverseRelationOrder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ export interface InverseRelationOrderValue {
// eslint-disable-next-line @typescript-eslint/naming-convention
export function InverseRelationOrder(
value: InverseRelationOrderValue,
): FindOperator<InverseRelationOrderValue> {
return new FindOperator<InverseRelationOrderValue>({
): FindOperator<InverseRelationOrderValue, 'inverseRelationOrder'> {
return new FindOperator({
operator: 'inverseRelationOrder',
value,
});
Expand Down
4 changes: 2 additions & 2 deletions src/storage/operator/LessThan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { FindOperator } from '../FindOperator';
// eslint-disable-next-line @typescript-eslint/naming-convention
export function LessThan<T extends number | Date | string | ValueObject>(
value: T,
): FindOperator<T> {
return new FindOperator<T>({
): FindOperator<T, 'lt'> {
return new FindOperator({
operator: 'lt',
value,
});
Expand Down
4 changes: 2 additions & 2 deletions src/storage/operator/LessThanOrEqual.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { FindOperator } from '../FindOperator';
// eslint-disable-next-line @typescript-eslint/naming-convention
export function LessThanOrEqual<T extends number | Date | string | ValueObject>(
value: T,
): FindOperator<T> {
return new FindOperator<T>({
): FindOperator<T, 'lte'> {
return new FindOperator({
operator: 'lte',
value,
});
Expand Down
6 changes: 3 additions & 3 deletions src/storage/operator/Not.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { FindOperator } from '../FindOperator';

// eslint-disable-next-line @typescript-eslint/naming-convention
export function Not<T>(
value: T | FindOperator<T>,
): FindOperator<T> {
return new FindOperator<T>({
value: T | FindOperator<T, any>,
): FindOperator<T, 'not'> {
return new FindOperator({
operator: 'not',
value,
});
Expand Down
16 changes: 16 additions & 0 deletions src/storage/operator/OneOrMorePath.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { FindOperator } from '../FindOperator';

export interface OneOrMorePathValue {
subPath: string | FindOperator<any, 'sequencePath' | 'inversePath'>;
value?: string;
}

// eslint-disable-next-line @typescript-eslint/naming-convention
export function OneOrMorePath<
T extends OneOrMorePathValue
>(value: T): FindOperator<T, 'oneOrMorePath'> {
return new FindOperator({
operator: 'oneOrMorePath',
value,
});
}
16 changes: 16 additions & 0 deletions src/storage/operator/SequencePath.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { FindOperator } from '../FindOperator';

export interface SequencePathValue {
subPath: (string | FindOperator<any, 'zeroOrMorePath' | 'inversePath' | 'oneOrMorePath'>)[];
value?: string;
}

// eslint-disable-next-line @typescript-eslint/naming-convention
export function SequencePath<
T extends SequencePathValue
>(value: T): FindOperator<T, 'sequencePath'> {
return new FindOperator({
operator: 'sequencePath',
value,
});
}
16 changes: 16 additions & 0 deletions src/storage/operator/ZeroOrMorePath.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { FindOperator } from '../FindOperator';

export interface ZeroOrMorePathValue {
subPath: string | FindOperator<any, 'sequencePath' | 'inversePath'>;
value?: string;
}

// eslint-disable-next-line @typescript-eslint/naming-convention
export function ZeroOrMorePath<
T extends ZeroOrMorePathValue
>(value: T): FindOperator<T, 'zeroOrMorePath'> {
return new FindOperator({
operator: 'zeroOrMorePath',
value,
});
}
Loading