UNPKG

weaviate-client

Version:
316 lines (315 loc) • 9.25 kB
import { CommandBase } from '../validation/commandBase.js'; import Ask from './ask.js'; import Bm25 from './bm25.js'; import { GraphQLGenerate } from './generate.js'; import Group from './group.js'; import GroupBy from './groupBy.js'; import Hybrid from './hybrid.js'; import NearImage from './nearImage.js'; import NearMedia, { NearMediaType, } from './nearMedia.js'; import NearObject from './nearObject.js'; import NearText from './nearText.js'; import NearVector from './nearVector.js'; import Sort from './sort.js'; import Where from './where.js'; export { FusionType } from './hybrid.js'; export default class GraphQLGetter extends CommandBase { after; askString; bm25String; className; fields; groupString; hybridString; includesNearMediaFilter; limit; nearImageNotSet; nearMediaString; nearMediaType; nearObjectString; nearTextString; nearVectorString; offset; sortString; whereString; generateString; consistencyLevel; groupByString; tenant; autocut; constructor(client) { super(client); this.includesNearMediaFilter = false; } withFields = (fields) => { this.fields = fields; return this; }; withClassName = (className) => { this.className = className; return this; }; withAfter = (id) => { this.after = id; return this; }; withGroup = (args) => { try { this.groupString = new Group(args).toString(); } catch (e) { this.addError(e.toString()); } return this; }; withWhere = (whereObj) => { try { this.whereString = new Where(whereObj).toString(); } catch (e) { this.addError(e.toString()); } return this; }; withNearText = (args) => { if (this.includesNearMediaFilter) { throw new Error('cannot use multiple near<Media> filters in a single query'); } this.nearTextString = new NearText(args).toString(); this.includesNearMediaFilter = true; return this; }; withBm25 = (args) => { try { this.bm25String = new Bm25(args).toString(); } catch (e) { this.addError(e.toString()); } return this; }; withHybrid = (args) => { try { this.hybridString = new Hybrid(args).toString(); } catch (e) { this.addError(e.toString()); } return this; }; withNearObject = (args) => { if (this.includesNearMediaFilter) { throw new Error('cannot use multiple near<Media> filters in a single query'); } try { this.nearObjectString = new NearObject(args).toString(); this.includesNearMediaFilter = true; } catch (e) { this.addError(e.toString()); } return this; }; withAsk = (askObj) => { try { this.askString = new Ask(askObj).toString(); } catch (e) { this.addError(e.toString()); } return this; }; withNearMedia = (args) => { if (this.includesNearMediaFilter) { throw new Error('cannot use multiple near<Media> filters in a single query'); } try { this.nearMediaString = new NearMedia(args).toString(); this.nearMediaType = args.type; this.includesNearMediaFilter = true; } catch (e) { this.addError(e.toString()); } return this; }; withNearImage = (args) => { if (this.includesNearMediaFilter) { throw new Error('cannot use multiple near<Media> filters in a single query'); } try { this.nearMediaString = new NearImage(args).toString(); this.nearMediaType = NearMediaType.Image; this.includesNearMediaFilter = true; } catch (e) { this.addError(e.toString()); } return this; }; withNearAudio = (args) => { return this.withNearMedia({ ...args, type: NearMediaType.Audio, media: args.audio, }); }; withNearVideo = (args) => { return this.withNearMedia({ ...args, type: NearMediaType.Video, media: args.video, }); }; withNearThermal = (args) => { return this.withNearMedia({ ...args, type: NearMediaType.Thermal, media: args.thermal, }); }; withNearDepth = (args) => { return this.withNearMedia({ ...args, type: NearMediaType.Depth, media: args.depth, }); }; withNearIMU = (args) => { return this.withNearMedia({ ...args, type: NearMediaType.IMU, media: args.imu, }); }; withNearVector = (args) => { if (this.includesNearMediaFilter) { throw new Error('cannot use multiple near<Media> filters in a single query'); } try { this.nearVectorString = new NearVector(args).toString(); this.includesNearMediaFilter = true; } catch (e) { this.addError(e.toString()); } return this; }; withLimit = (limit) => { this.limit = limit; return this; }; withOffset = (offset) => { this.offset = offset; return this; }; withAutocut = (autocut) => { this.autocut = autocut; return this; }; withSort = (args) => { this.sortString = new Sort(args).toString(); return this; }; withGenerate = (args) => { this.generateString = new GraphQLGenerate(args).toString(); return this; }; withConsistencyLevel = (level) => { this.consistencyLevel = level; return this; }; withGroupBy = (args) => { try { this.groupByString = new GroupBy(args).toString(); } catch (e) { this.addError(e.toString()); } return this; }; withTenant = (tenant) => { this.tenant = tenant; return this; }; validateIsSet = (prop, name, setter) => { if (prop == undefined || prop == null || prop.length == 0) { this.addError(`${name} must be set - set with ${setter}`); } }; validate = () => { this.validateIsSet(this.className, 'className', '.withClassName(className)'); this.validateIsSet(this.fields, 'fields', '.withFields(fields)'); }; do = () => { let params = ''; this.validate(); if (this.errors.length > 0) { return Promise.reject(new Error('invalid usage: ' + this.errors.join(', '))); } let args = []; if (this.whereString) { args = [...args, `where:${this.whereString}`]; } if (this.nearTextString) { args = [...args, `nearText:${this.nearTextString}`]; } if (this.nearObjectString) { args = [...args, `nearObject:${this.nearObjectString}`]; } if (this.askString) { args = [...args, `ask:${this.askString}`]; } if (this.nearMediaString) { args = [...args, `near${this.nearMediaType}:${this.nearMediaString}`]; } if (this.nearVectorString) { args = [...args, `nearVector:${this.nearVectorString}`]; } if (this.bm25String) { args = [...args, `bm25:${this.bm25String}`]; } if (this.hybridString) { args = [...args, `hybrid:${this.hybridString}`]; } if (this.groupString) { args = [...args, `group:${this.groupString}`]; } if (this.limit) { args = [...args, `limit:${this.limit}`]; } if (this.offset) { args = [...args, `offset:${this.offset}`]; } if (this.autocut) { args = [...args, `autocut:${this.autocut}`]; } if (this.sortString) { args = [...args, `sort:[${this.sortString}]`]; } if (this.after) { args = [...args, `after:"${this.after}"`]; } if (this.generateString) { if (this.fields?.includes('_additional')) { this.fields.replace('_additional{', `_additional{${this.generateString}`); } else { this.fields = this.fields?.concat(` _additional{${this.generateString}}`); } } if (this.consistencyLevel) { args = [...args, `consistencyLevel:${this.consistencyLevel}`]; } if (this.groupByString) { args = [...args, `groupBy:${this.groupByString}`]; } if (this.tenant) { args = [...args, `tenant:"${this.tenant}"`]; } if (args.length > 0) { params = `(${args.join(',')})`; } return this.client.query(`{Get{${this.className}${params}{${this.fields}}}}`); }; }