@hypermode/functions-as
Advanced tools
Comparing version
@@ -211,2 +211,21 @@ import * as utils from "./utils"; | ||
// @ts-expect-error: decorator | ||
@external("hypermode", "getVector") | ||
declare function hostGetVector( | ||
collection: string, | ||
namespace: string, | ||
key: string, | ||
): f32[]; | ||
// @ts-expect-error: decorator | ||
@external("hypermode", "searchCollectionByVector") | ||
declare function hostSearchCollectionByVector( | ||
collection: string, | ||
namespaces: string[], | ||
searchMethod: string, | ||
vector: f32[], | ||
limit: i32, | ||
returnText: bool, | ||
): CollectionSearchResult; | ||
// add batch upsert | ||
@@ -366,3 +385,2 @@ export function upsertBatch( | ||
if (text.length == 0) { | ||
console.error("Text is empty."); | ||
return new CollectionSearchResult( | ||
@@ -397,2 +415,40 @@ collection, | ||
export function searchByVector( | ||
collection: string, | ||
searchMethod: string, | ||
vector: f32[], | ||
limit: i32, | ||
returnText: bool = false, | ||
namespaces: string[] = [], | ||
): CollectionSearchResult { | ||
if (vector.length == 0) { | ||
return new CollectionSearchResult( | ||
collection, | ||
CollectionStatus.Error, | ||
"Vector is empty.", | ||
searchMethod, | ||
[], | ||
); | ||
} | ||
const result = hostSearchCollectionByVector( | ||
collection, | ||
namespaces, | ||
searchMethod, | ||
vector, | ||
limit, | ||
returnText, | ||
); | ||
if (utils.resultIsInvalid(result)) { | ||
console.error("Error searching Text index by vector."); | ||
return new CollectionSearchResult( | ||
collection, | ||
CollectionStatus.Error, | ||
"Error searching Text index by vector.", | ||
searchMethod, | ||
[], | ||
); | ||
} | ||
return result; | ||
} | ||
// fetch embedders for collection & search method, run text through it and | ||
@@ -407,3 +463,2 @@ // classify Text index for similar Texts, return the result keys | ||
if (text.length == 0) { | ||
console.error("Text is empty."); | ||
return new CollectionClassificationResult( | ||
@@ -550,1 +605,22 @@ collection, | ||
} | ||
export function getVector( | ||
collection: string, | ||
searchMethod: string, | ||
key: string, | ||
namespace: string = "", | ||
): f32[] { | ||
if (collection.length == 0) { | ||
console.error("Collection is empty."); | ||
return []; | ||
} | ||
if (searchMethod.length == 0) { | ||
console.error("Search method is empty."); | ||
return []; | ||
} | ||
if (key.length == 0) { | ||
console.error("Key is empty."); | ||
return []; | ||
} | ||
return hostGetVector(collection, namespace, key); | ||
} |
import { JSON } from "json-as"; | ||
import * as utils from "./utils"; | ||
import { NamedParams as Variables } from "./database"; | ||
export { Variables }; | ||
// @ts-expect-error: decorator | ||
@external("hypermode", "executeDQLQuery") | ||
declare function hostExecuteDQLQuery( | ||
hostName: string, | ||
query: string, | ||
variables: string, | ||
): string; | ||
@external("hypermode", "executeDQL") | ||
declare function hostExecuteDQL(hostName: string, request: Request): Response; | ||
// @ts-expect-error: decorator | ||
@external("hypermode", "executeDQLMutations") | ||
declare function hostExecuteDQLMutations( | ||
hostName: string, | ||
setMutations: string[], | ||
delMutations: string[], | ||
): Map<string, string>; | ||
// @ts-expect-error: decorator | ||
@external("hypermode", "dgraphAlterSchema") | ||
@@ -39,21 +25,16 @@ declare function hostDgraphAlterSchema( | ||
* | ||
* executes a dql query on the dgraph database | ||
* Executes a DQL query or mutation on the Dgraph database. | ||
* | ||
* @param hostName - the name of the host | ||
* @param query - the dql query to execute | ||
* @param variables - the variables to use in the query | ||
* @returns The result as a JSON object of type TData, specified by the caller | ||
* @param query - the query to execute | ||
* @param mutations - the mutations to execute | ||
* @returns The response from the Dgraph server | ||
*/ | ||
export function query<TData>( | ||
hostName: string, | ||
query: string = "", | ||
variables: Variables = new Variables(), | ||
): TData { | ||
const varsJson = variables.toJSON(); | ||
const response = hostExecuteDQLQuery(hostName, query, varsJson); | ||
if (utils.resultIsInvalid(response)) { | ||
throw new Error("Error running DQL query."); | ||
export function execute(hostName: string, request: Request): Response { | ||
const response = hostExecuteDQL(hostName, request); | ||
if (!response) { | ||
throw new Error("Error executing DQL."); | ||
} | ||
return JSON.parse<TData>(response); | ||
return response; | ||
} | ||
@@ -63,19 +44,2 @@ | ||
* | ||
* executes a dql mutation on the dgraph database | ||
* | ||
* @param hostName - the name of the host | ||
* @param setMutations - the set mutations to execute, written in dql in rdf format | ||
* @param delMutations - the delete mutations to execute, written in dql in rdf format | ||
* @returns A map of the uids returned, or an empty map if no uids are returned (usually for delete mutations) | ||
*/ | ||
export function mutate( | ||
hostName: string, | ||
setMutations: string[] = [], | ||
delMutations: string[] = [], | ||
): Map<string, string> { | ||
return hostExecuteDQLMutations(hostName, setMutations, delMutations); | ||
} | ||
/** | ||
* | ||
* Alters the schema of the dgraph database | ||
@@ -128,1 +92,86 @@ * | ||
} | ||
/** | ||
* | ||
* Represents a Dgraph request. | ||
* | ||
*/ | ||
export class Request { | ||
constructor(Query: Query | null = null, Mutations: Mutation[] | null = null) { | ||
if (Query) { | ||
this.query = Query; | ||
} | ||
if (Mutations) { | ||
this.mutations = Mutations; | ||
} | ||
} | ||
query: Query = new Query(); | ||
mutations: Mutation[] = []; | ||
} | ||
/** | ||
* | ||
* Represents a Dgraph query. | ||
* | ||
*/ | ||
export class Query { | ||
constructor(query: string = "", variables: Variables = new Variables()) { | ||
this.query = query; | ||
this.variables = variables.toMap(); | ||
} | ||
query: string = ""; | ||
variables: Map<string, string> = new Map<string, string>(); | ||
} | ||
/** | ||
* | ||
* Represents a Dgraph mutation. | ||
* | ||
*/ | ||
export class Mutation { | ||
constructor( | ||
public setJson: string = "", | ||
public delJson: string = "", | ||
public setNquads: string = "", | ||
public delNquads: string = "", | ||
public condition: string = "", | ||
) {} | ||
} | ||
/** | ||
* | ||
* Represents a Dgraph response. | ||
* | ||
*/ | ||
export class Response { | ||
Json: string | null = null; | ||
Uids: Map<string, string> | null = null; | ||
} | ||
export class Variables { | ||
private data: Map<string, string> = new Map<string, string>(); | ||
public set<T>(name: string, value: T): void { | ||
if (isString<T>()) { | ||
this.data.set(name, value as string); | ||
return; | ||
} else if (isInteger<T>()) { | ||
this.data.set(name, JSON.stringify(value)); | ||
return; | ||
} else if (isFloat<T>()) { | ||
this.data.set(name, JSON.stringify(value)); | ||
return; | ||
} else if (isBoolean<T>()) { | ||
this.data.set(name, JSON.stringify(value)); | ||
return; | ||
} else { | ||
throw new Error( | ||
"Unsupported variable type in dgraph. Must be string, integer, float, boolean.", | ||
); | ||
} | ||
} | ||
public toMap(): Map<string, string> { | ||
return this.data; | ||
} | ||
} |
{ | ||
"name": "@hypermode/functions-as", | ||
"version": "0.11.1", | ||
"version": "0.11.2-alpha1", | ||
"description": "Hypermode library for AssemblyScript functions", | ||
@@ -21,16 +21,16 @@ "author": "Hypermode, Inc.", | ||
"json-as": "^0.9.21", | ||
"semver": "^7.6.3", | ||
"xid-ts": "^1.1.0" | ||
}, | ||
"devDependencies": { | ||
"@eslint/js": "^9.9.0", | ||
"@eslint/js": "^9.9.1", | ||
"@types/eslint__js": "^8.42.3", | ||
"@types/node": "^20.14.15", | ||
"@types/node": "^20.16.1", | ||
"as-test": "^0.3.4", | ||
"assemblyscript": "^0.27.29", | ||
"assemblyscript-prettier": "^3.0.1", | ||
"eslint": "^9.9.0", | ||
"eslint": "^9.9.1", | ||
"prettier": "^3.3.3", | ||
"semver": "^7.6.3", | ||
"typescript": "^5.5.4", | ||
"typescript-eslint": "^8.1.0", | ||
"typescript-eslint": "^8.3.0", | ||
"visitor-as": "^0.11.4" | ||
@@ -37,0 +37,0 @@ }, |
73172
3.25%11
-8.33%2397
5.09%5
25%+ Added
+ Added