Advertisement
the0938

SparqlHelper

Apr 8th, 2022
1,028
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* eslint-disable sonarjs/no-identical-functions */
  2. /* eslint-disable max-classes-per-file */
  3. import { SparqlCreateQueryBuilder } from '../../builders/SparqlCreateQueryBuilder';
  4. import { SparqlUpdateQueryBuilder } from '../../builders/SparqlUpdateQueryBuilder';
  5.  
  6. interface User {
  7.   id: string;
  8.   createdAt: Date;
  9.   updatedAt: Date;
  10.   isDeleted: boolean;
  11.   companyId?: string;
  12. }
  13.  
  14. type CreateUser = User;
  15. type UpdateUser = Pick<User, 'id'> & Partial<Omit<User, 'id'>>;
  16.  
  17. class CreateBuilder extends SparqlCreateQueryBuilder<CreateUser> {
  18.   public constructor() {
  19.     super('User');
  20.   }
  21.  
  22.   protected *buildFieldTriples<TKey extends keyof CreateUser>(
  23.     subject: string,
  24.     key: TKey,
  25.     value: CreateUser[TKey],
  26.   ): Iterable<string> {
  27.     if (value == null) {
  28.       return;
  29.     }
  30.  
  31.     if (key === 'companyId') {
  32.       const directLink = this.buildNode('belongsToCompany');
  33.       const invertLink = this.buildNode('hasUsers');
  34.       const object = this.buildNode(value as string);
  35.  
  36.       yield this.buildTriple(subject, directLink, object);
  37.       yield this.buildTriple(object, invertLink, subject);
  38.  
  39.       return;
  40.     }
  41.  
  42.     const predicate = this.buildNode(key);
  43.     const object = this.buildValue(value);
  44.  
  45.     yield this.buildTriple(subject, predicate, object);
  46.   }
  47. }
  48.  
  49. class UpdateBuilder extends SparqlUpdateQueryBuilder<UpdateUser> {
  50.   public constructor() {
  51.     super('User');
  52.   }
  53.  
  54.   protected *buildFieldDeleteTriples<TKey extends keyof UpdateUser>(subject: string, key: TKey): Iterable<string> {
  55.     if (key === 'companyId') {
  56.       const directLink = this.buildNode('belongsToCompany');
  57.       const invertLink = this.buildNode('hasUsers');
  58.       const object = this.buildVariable(subject, key);
  59.  
  60.       yield this.buildTriple(subject, directLink, object);
  61.       yield this.buildTriple(object, invertLink, subject);
  62.  
  63.       return;
  64.     }
  65.  
  66.     const predicate = this.buildNode(key);
  67.     const object = this.buildVariable(subject, key);
  68.  
  69.     yield this.buildTriple(subject, predicate, object);
  70.   }
  71.  
  72.   protected *buildFieldInsertTriples<TKey extends keyof UpdateUser>(
  73.     subject: string,
  74.     key: TKey,
  75.     value: UpdateUser[TKey],
  76.   ): Iterable<string> {
  77.     if (value == null) {
  78.       return;
  79.     }
  80.  
  81.     if (key === 'companyId') {
  82.       const directLink = this.buildNode('belongsToCompany');
  83.       const invertLink = this.buildNode('hasUsers');
  84.       const object = this.buildNode(value as string);
  85.  
  86.       yield this.buildTriple(subject, directLink, object);
  87.       yield this.buildTriple(object, invertLink, subject);
  88.  
  89.       return;
  90.     }
  91.  
  92.     const predicate = this.buildNode(key);
  93.     const object = this.buildValue(value);
  94.  
  95.     yield this.buildTriple(subject, predicate, object);
  96.   }
  97. }
  98.  
  99. describe('foo', () => {
  100.   it('create', () => {
  101.     const users: CreateUser[] = [
  102.       {
  103.         id: 'USER_A',
  104.         createdAt: new Date(),
  105.         updatedAt: new Date(),
  106.         isDeleted: true,
  107.       },
  108.       {
  109.         id: 'USER_B',
  110.         createdAt: new Date(),
  111.         updatedAt: new Date(),
  112.         isDeleted: false,
  113.         companyId: 'COMPANY_B',
  114.       },
  115.     ];
  116.  
  117.     const query = new CreateBuilder().build(users);
  118.     console.log(query);
  119.   });
  120.  
  121.   it('update', () => {
  122.     const users: UpdateUser[] = [
  123.       {
  124.         id: 'USER_A',
  125.         companyId: 'COMPANY_A',
  126.       },
  127.       {
  128.         id: 'USER_B',
  129.         isDeleted: true,
  130.         companyId: undefined,
  131.       },
  132.     ];
  133.  
  134.     const query = new UpdateBuilder().build(users);
  135.     console.log(query);
  136.   });
  137. });
  138.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement