Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* eslint-disable sonarjs/no-identical-functions */
- /* eslint-disable max-classes-per-file */
- import { SparqlCreateQueryBuilder } from '../../builders/SparqlCreateQueryBuilder';
- import { SparqlUpdateQueryBuilder } from '../../builders/SparqlUpdateQueryBuilder';
- interface User {
- id: string;
- createdAt: Date;
- updatedAt: Date;
- isDeleted: boolean;
- companyId?: string;
- }
- type CreateUser = User;
- type UpdateUser = Pick<User, 'id'> & Partial<Omit<User, 'id'>>;
- class CreateBuilder extends SparqlCreateQueryBuilder<CreateUser> {
- public constructor() {
- super('User');
- }
- protected *buildFieldTriples<TKey extends keyof CreateUser>(
- subject: string,
- key: TKey,
- value: CreateUser[TKey],
- ): Iterable<string> {
- if (value == null) {
- return;
- }
- if (key === 'companyId') {
- const directLink = this.buildNode('belongsToCompany');
- const invertLink = this.buildNode('hasUsers');
- const object = this.buildNode(value as string);
- yield this.buildTriple(subject, directLink, object);
- yield this.buildTriple(object, invertLink, subject);
- return;
- }
- const predicate = this.buildNode(key);
- const object = this.buildValue(value);
- yield this.buildTriple(subject, predicate, object);
- }
- }
- class UpdateBuilder extends SparqlUpdateQueryBuilder<UpdateUser> {
- public constructor() {
- super('User');
- }
- protected *buildFieldDeleteTriples<TKey extends keyof UpdateUser>(subject: string, key: TKey): Iterable<string> {
- if (key === 'companyId') {
- const directLink = this.buildNode('belongsToCompany');
- const invertLink = this.buildNode('hasUsers');
- const object = this.buildVariable(subject, key);
- yield this.buildTriple(subject, directLink, object);
- yield this.buildTriple(object, invertLink, subject);
- return;
- }
- const predicate = this.buildNode(key);
- const object = this.buildVariable(subject, key);
- yield this.buildTriple(subject, predicate, object);
- }
- protected *buildFieldInsertTriples<TKey extends keyof UpdateUser>(
- subject: string,
- key: TKey,
- value: UpdateUser[TKey],
- ): Iterable<string> {
- if (value == null) {
- return;
- }
- if (key === 'companyId') {
- const directLink = this.buildNode('belongsToCompany');
- const invertLink = this.buildNode('hasUsers');
- const object = this.buildNode(value as string);
- yield this.buildTriple(subject, directLink, object);
- yield this.buildTriple(object, invertLink, subject);
- return;
- }
- const predicate = this.buildNode(key);
- const object = this.buildValue(value);
- yield this.buildTriple(subject, predicate, object);
- }
- }
- describe('foo', () => {
- it('create', () => {
- const users: CreateUser[] = [
- {
- id: 'USER_A',
- createdAt: new Date(),
- updatedAt: new Date(),
- isDeleted: true,
- },
- {
- id: 'USER_B',
- createdAt: new Date(),
- updatedAt: new Date(),
- isDeleted: false,
- companyId: 'COMPANY_B',
- },
- ];
- const query = new CreateBuilder().build(users);
- console.log(query);
- });
- it('update', () => {
- const users: UpdateUser[] = [
- {
- id: 'USER_A',
- companyId: 'COMPANY_A',
- },
- {
- id: 'USER_B',
- isDeleted: true,
- companyId: undefined,
- },
- ];
- const query = new UpdateBuilder().build(users);
- console.log(query);
- });
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement