diff --git a/.gitignore b/.gitignore index f29de9fb..0dec16cc 100644 --- a/.gitignore +++ b/.gitignore @@ -24,6 +24,8 @@ index.node **/node_modules **/.DS_Store node/dist +node/examples/**/package-lock.json +node/examples/**/dist ## Rust target diff --git a/node/examples/js/index.js b/node/examples/js/index.js new file mode 100644 index 00000000..625ef1f2 --- /dev/null +++ b/node/examples/js/index.js @@ -0,0 +1,36 @@ +// Copyright 2023 Lance Developers. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +'use strict' + +async function example () { + const lancedb = require('vectordb') + const db = await lancedb.connect('data/sample-lancedb') + + const data = [ + { id: 1, vector: [0.1, 0.2], price: 10 }, + { id: 2, vector: [1.1, 1.2], price: 50 } + ] + + const table = await db.createTable('vectors', data) + console.log(await db.tableNames()) + + const results = await table + .search([0.1, 0.3]) + .limit(20) + .execute() + console.log(results) +} + +example() diff --git a/node/examples/simple/package.json b/node/examples/js/package.json similarity index 79% rename from node/examples/simple/package.json rename to node/examples/js/package.json index d59dc0a6..ad05d814 100644 --- a/node/examples/simple/package.json +++ b/node/examples/js/package.json @@ -1,12 +1,12 @@ { - "name": "vectordb-example", + "name": "vectordb-example-js", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, - "author": "", + "author": "Lance Devs", "license": "Apache-2.0", "dependencies": { "vectordb": "^0.1.0" diff --git a/node/examples/ts/package.json b/node/examples/ts/package.json new file mode 100644 index 00000000..76b1ad55 --- /dev/null +++ b/node/examples/ts/package.json @@ -0,0 +1,22 @@ +{ + "name": "vectordb-example-ts", + "version": "1.0.0", + "description": "", + "main": "dist/index.js", + "types": "dist/index.d.ts", + "scripts": { + "tsc": "tsc -b", + "build": "tsc" + }, + "author": "Lance Devs", + "license": "Apache-2.0", + "devDependencies": { + "@types/node": "^18.16.2", + "ts-node": "^10.9.1", + "ts-node-dev": "^2.0.0", + "typescript": "*" + }, + "dependencies": { + "vectordb": "^0.1.0" + } +} diff --git a/node/examples/simple/index.js b/node/examples/ts/src/index.ts similarity index 60% rename from node/examples/simple/index.js rename to node/examples/ts/src/index.ts index d323ef8d..6aa73d47 100644 --- a/node/examples/simple/index.js +++ b/node/examples/ts/src/index.ts @@ -12,26 +12,24 @@ // See the License for the specific language governing permissions and // limitations under the License. -'use strict' +import * as vectordb from 'vectordb'; -async function example() { - const lancedb = require('vectordb'); - const db = await lancedb.connect('data/sample-lancedb'); +async function example () { + const db = await vectordb.connect('data/sample-lancedb') const data = [ - { id: 1, vector: [0.1, 0.2], price: 10 }, - { id: 2, vector: [1.1, 1.2], price: 50 } + { id: 1, vector: [0.1, 0.2], price: 10 }, + { id: 2, vector: [1.1, 1.2], price: 50 } ] const table = await db.createTable('vectors', data) - console.log(await db.tableNames()); + console.log(await db.tableNames()) - const query = table.search([0.1, 0.3]); - query.limit = 20; - const results = await query.execute(); - console.log(results); + const results = await table + .search([0.1, 0.3]) + .limit(20) + .execute() + console.log(results) } -example(); - - +example().then(_ => { console.log ("All done!") }) diff --git a/node/examples/ts/tsconfig.json b/node/examples/ts/tsconfig.json new file mode 100644 index 00000000..a3fe259b --- /dev/null +++ b/node/examples/ts/tsconfig.json @@ -0,0 +1,10 @@ +{ + "include": ["src/**/*.ts"], + "compilerOptions": { + "target": "es2016", + "module": "commonjs", + "declaration": true, + "outDir": "./dist", + "strict": true + } +} diff --git a/node/src/index.ts b/node/src/index.ts index c1a172b0..a6c2f062 100644 --- a/node/src/index.ts +++ b/node/src/index.ts @@ -174,7 +174,7 @@ export class Query { * Execute the query and return the results as an Array of Objects */ async execute> (): Promise { - let buffer; + let buffer if (this._filter != null) { buffer = await tableSearch.call(this._tbl, this._query_vector, this._limit, this._filter) } else {