forked from valkey-io/valkey-glide
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding node modules in examples app (valkey-io#2615)
* Adding node modules in examples app --------- Signed-off-by: TJ Zhang <[email protected]> Signed-off-by: Chloe <[email protected]> Signed-off-by: Chloe Yip <[email protected]> Co-authored-by: TJ Zhang <[email protected]> Co-authored-by: Chloe <[email protected]> Co-authored-by: Chloe Yip <[email protected]>
- Loading branch information
1 parent
0e7616d
commit a69690f
Showing
3 changed files
with
226 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/** | ||
* Copyright Valkey GLIDE Project Contributors - SPDX Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { | ||
GlideClient, | ||
GlideClusterClient, | ||
GlideJson, | ||
Logger, | ||
} from "@valkey/valkey-glide"; | ||
|
||
async function executeJsonCommands() { | ||
// When Valkey is in standalone mode, add address of the primary node, and any replicas you'd like to be able to read from. | ||
const addresses = [ | ||
{ | ||
host: "localhost", | ||
port: 6379, | ||
}, | ||
]; | ||
// Check `GlideClientConfiguration/GlideClusterClientConfiguration` for additional options. | ||
const client = await GlideClient.createClient({ | ||
addresses: addresses, | ||
// if the server uses TLS, you'll need to enable it. Otherwise the connection attempt will time out silently. | ||
// useTLS: true, | ||
clientName: "test_standalone_client", | ||
}); | ||
await jsonSetAndGet(client); | ||
client.close(); | ||
} | ||
|
||
async function jsonSetAndGet(client: GlideClient | GlideClusterClient) { | ||
const value = '{"a": 1.0, "b":2}'; | ||
const result = await GlideJson.set(client, "doc", "$", value); | ||
console.log(result); // 'OK' - Indicates successful setting of the value at path '$' in the key stored at `doc`. | ||
const result2 = await GlideJson.get(client, "doc", { path: ["$"] }); | ||
console.log(result2); | ||
|
||
expect(result).toBe("OK"); | ||
expect(result2).toBe(value); | ||
|
||
console.log("All examples succeeded."); | ||
} | ||
|
||
function setFileLogger() { | ||
Logger.setLoggerConfig("warn", "glide.log"); | ||
} | ||
|
||
function setConsoleLogger() { | ||
Logger.setLoggerConfig("warn"); | ||
} | ||
|
||
setFileLogger(); | ||
setConsoleLogger(); | ||
// Enable for standalone mode | ||
await executeJsonCommands(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,14 @@ | ||
{ | ||
"type": "module", | ||
"dependencies": { | ||
"@valkey/valkey-glide": "latest", | ||
"@types/node": "^20.4.8" | ||
"@types/node": "^20.4.8", | ||
"@valkey/valkey-glide": "../../node", | ||
"uuid": "^11.0.3" | ||
}, | ||
"devDependencies": { | ||
"@jest/globals": "^29.7.0", | ||
"@types/jest": "^29.5.14", | ||
"jest": "^29.7.0", | ||
"typescript": "^5.1.6" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
/** | ||
* Copyright Valkey GLIDE Project Contributors - SPDX Identifier: Apache-2.0 | ||
*/ | ||
import { | ||
Decoder, | ||
GlideClient, | ||
GlideClusterClient, | ||
GlideFt, | ||
FtSearchOptions, | ||
FtSearchReturnType, | ||
Logger, | ||
VectorField, | ||
} from "@valkey/valkey-glide"; | ||
|
||
import { v4 as uuidv4 } from "uuid"; | ||
|
||
const DATA_PROCESSING_TIMEOUT = 1000; | ||
|
||
async function executeVssCommands() { | ||
// When Valkey is in standalone mode, add address of the primary node, and any replicas you'd like to be able to read from. | ||
// JSON modules and VSS modules must be enabled for this to successfully execute, otherwise there will be request errors. | ||
const addresses = [ | ||
{ | ||
host: "localhost", | ||
port: 6380, | ||
}, | ||
]; | ||
// Check `GlideClientConfiguration/GlideClusterClientConfiguration` for additional options. | ||
const client = await GlideClient.createClient({ | ||
addresses: addresses, | ||
// if the server uses TLS, you'll need to enable it. Otherwise the connection attempt will time out silently. | ||
// useTLS: true, | ||
clientName: "test_standalone_client", | ||
}); | ||
await vssCreateAndSearch(client); | ||
client.close(); | ||
} | ||
|
||
async function vssCreateAndSearch(client: GlideClient | GlideClusterClient) { | ||
const prefix = "{" + uuidv4() + "}:"; | ||
const index = prefix + "index"; | ||
const query = "*=>[KNN 2 @VEC $query_vec]"; | ||
|
||
const vectorField_1: VectorField = { | ||
type: "VECTOR", | ||
name: "vec", | ||
alias: "VEC", | ||
attributes: { | ||
algorithm: "HNSW", | ||
distanceMetric: "L2", | ||
dimensions: 2, | ||
}, | ||
}; | ||
|
||
const createResult = await GlideFt.create(client, index, [vectorField_1], { | ||
dataType: "HASH", | ||
prefixes: [prefix], | ||
}); | ||
console.log(createResult); // 'OK' - Indicates successful creation of the index. | ||
|
||
// Binary buffers are used here for memory management | ||
const binaryValue1 = Buffer.alloc(8); | ||
expect( | ||
await client.hset(Buffer.from(prefix + "0"), [ | ||
// value of <Buffer 00 00 00 00 00 00 00 00 00> | ||
{ field: "vec", value: binaryValue1 }, | ||
]), | ||
).toEqual(1); | ||
|
||
const binaryValue2: Buffer = Buffer.alloc(8); | ||
binaryValue2[6] = 0x80; | ||
binaryValue2[7] = 0xbf; | ||
expect( | ||
await client.hset(Buffer.from(prefix + "1"), [ | ||
// value of <Buffer 00 00 00 00 00 00 00 80 BF> | ||
{ field: "vec", value: binaryValue2 }, | ||
]), | ||
).toEqual(1); | ||
|
||
// let server digest the data and update index | ||
const sleep = new Promise((resolve) => | ||
setTimeout(resolve, DATA_PROCESSING_TIMEOUT), | ||
); | ||
await sleep; | ||
|
||
// Additional search options | ||
const optionsWithCount: FtSearchOptions = { | ||
params: [{ key: "query_vec", value: binaryValue1 }], | ||
timeout: 10000, | ||
count: true, | ||
}; | ||
const binaryResultCount: FtSearchReturnType = await GlideFt.search( | ||
client, | ||
index, | ||
query, | ||
{ | ||
decoder: Decoder.Bytes, | ||
...optionsWithCount, | ||
}, | ||
); | ||
expect(binaryResultCount).toEqual([2]); | ||
|
||
const options: FtSearchOptions = { | ||
params: [{ key: "query_vec", value: binaryValue1 }], | ||
timeout: 10000, | ||
}; | ||
const searchResult: FtSearchReturnType = await GlideFt.search( | ||
client, | ||
index, | ||
query, | ||
{ | ||
decoder: Decoder.Bytes, | ||
...options, | ||
}, | ||
); | ||
|
||
const expectedBinaryResult: FtSearchReturnType = [ | ||
2, | ||
[ | ||
{ | ||
key: Buffer.from(prefix + "1"), | ||
value: [ | ||
{ | ||
key: Buffer.from("vec"), | ||
value: binaryValue2, | ||
}, | ||
{ | ||
key: Buffer.from("__VEC_score"), | ||
value: Buffer.from("1"), | ||
}, | ||
], | ||
}, | ||
{ | ||
key: Buffer.from(prefix + "0"), | ||
value: [ | ||
{ | ||
key: Buffer.from("vec"), | ||
value: binaryValue1, | ||
}, | ||
{ | ||
key: Buffer.from("__VEC_score"), | ||
value: Buffer.from("0"), | ||
}, | ||
], | ||
}, | ||
], | ||
]; | ||
expect(createResult).toBe("OK"); | ||
expect(searchResult).toEqual(expectedBinaryResult); | ||
|
||
console.log("All examples succeeded."); | ||
} | ||
|
||
function setFileLogger() { | ||
Logger.setLoggerConfig("warn", "glide.log"); | ||
} | ||
|
||
function setConsoleLogger() { | ||
Logger.setLoggerConfig("warn"); | ||
} | ||
|
||
setFileLogger(); | ||
setConsoleLogger(); | ||
|
||
await executeVssCommands(); |