@storecraft/database-mongodb 1.0.11 → 1.0.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +64 -1
- package/package.json +10 -1
- package/vector-search-extension.js +110 -0
package/README.md
CHANGED
@@ -8,6 +8,7 @@
|
|
8
8
|
[](https://github.com/store-craft/storecraft/actions/workflows/test.database-mongodb.yml)
|
9
9
|
|
10
10
|
Official `mongodb` driver for `StoreCraft` on **Node.js** / **Deno** / **Bun** platforms.
|
11
|
+
Also, an example of [Semantic Vector Search](https://www.mongodb.com/developer/products/atlas/semantic-search-mongodb-atlas-vector-search/) extension at `@storecraft/database-mongodb/vector-search-extension`
|
11
12
|
|
12
13
|
```bash
|
13
14
|
npm i @storecraft/database-mongodb
|
@@ -20,8 +21,10 @@ import 'dotenv/config';
|
|
20
21
|
import http from "node:http";
|
21
22
|
import { App } from '@storecraft/core'
|
22
23
|
import { NodePlatform } from '@storecraft/core/platform/node';
|
23
|
-
import { MongoDB
|
24
|
+
import { MongoDB } from '@storecraft/database-mongodb'
|
25
|
+
import { migrateToLatest } from '@storecraft/database-mongodb/migrate.js'
|
24
26
|
import { NodeLocalStorage } from '@storecraft/core/storage/node'
|
27
|
+
import { MongoVectorSearch } from '@storecraft/database-mongodb/vector-search-extension'
|
25
28
|
|
26
29
|
const app = new App(
|
27
30
|
{
|
@@ -32,6 +35,9 @@ const app = new App(
|
|
32
35
|
)
|
33
36
|
.withPlatform(new NodePlatform())
|
34
37
|
.withDatabase(new MongoDB({ db_name: 'test', url: '...', options: {}}))
|
38
|
+
.withExtensions(
|
39
|
+
'mongo-vector-search': new MongoVectorSearch({ openai_key: process.env.OPENAI })
|
40
|
+
)
|
35
41
|
|
36
42
|
await app.init();
|
37
43
|
await migrateToLatest(app.db, false);
|
@@ -45,6 +51,63 @@ const server = http.createServer(app.handler).listen(
|
|
45
51
|
|
46
52
|
```
|
47
53
|
|
54
|
+
## (Recommended) setup semantic/ai vector search extension for products
|
55
|
+
|
56
|
+
1. in [Atlas](https://cloud.mongodb.com/) dashboard, create a vector index (call it `vector_index`) for `products` collection:
|
57
|
+
|
58
|
+
```json
|
59
|
+
{
|
60
|
+
"fields": [
|
61
|
+
{
|
62
|
+
"numDimensions": 1536,
|
63
|
+
"path": "embedding",
|
64
|
+
"similarity": "cosine",
|
65
|
+
"type": "vector"
|
66
|
+
}
|
67
|
+
]
|
68
|
+
}
|
69
|
+
```
|
70
|
+
|
71
|
+
2. Now, every upserted product will be eligible for semantic search by it's title + description.
|
72
|
+
3. The extension is publicly available via HTTP (`POST` request)
|
73
|
+
```js
|
74
|
+
await fetch(
|
75
|
+
'http://localhost:8000/api/extensions/mongo-vector-search/search',
|
76
|
+
{
|
77
|
+
method: 'POST',
|
78
|
+
body: JSON.stringify(
|
79
|
+
{
|
80
|
+
query: 'I am interested in Nintendo related clothing, such as shirts',
|
81
|
+
limit: 1
|
82
|
+
}
|
83
|
+
)
|
84
|
+
}
|
85
|
+
)
|
86
|
+
```
|
87
|
+
|
88
|
+
returns `ProductType[]` array
|
89
|
+
|
90
|
+
```json
|
91
|
+
[
|
92
|
+
{
|
93
|
+
"title": "Super Mario T Shirt",
|
94
|
+
"handle": "super-mario-t-shirt",
|
95
|
+
"description": "This Super mario shirt is XL size and
|
96
|
+
features a colorful print of Lugi and Mario.",
|
97
|
+
"media": [
|
98
|
+
"storage://images/super-mario-shirt_1738686680944_w_819_h_460.jpeg"
|
99
|
+
],
|
100
|
+
"price": 100,
|
101
|
+
"qty": 1,
|
102
|
+
"active": true,
|
103
|
+
"id": "pr_67a240e4000000d34bcf0743",
|
104
|
+
"created_at": "2025-02-04T16:31:32.909Z",
|
105
|
+
"updated_at": "2025-02-04T16:58:25.286Z"
|
106
|
+
}
|
107
|
+
]
|
108
|
+
```
|
109
|
+
|
110
|
+
|
48
111
|
```text
|
49
112
|
Author: Tomer Shalev <tomer.shalev@gmail.com>
|
50
113
|
```
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@storecraft/database-mongodb",
|
3
|
-
"version": "1.0.
|
3
|
+
"version": "1.0.12",
|
4
4
|
"description": "Storecraft database driver for mongodb on node / bun / deno platform",
|
5
5
|
"license": "MIT",
|
6
6
|
"author": "Tomer Shalev (https://github.com/store-craft)",
|
@@ -19,6 +19,15 @@
|
|
19
19
|
"type": "module",
|
20
20
|
"main": "index.js",
|
21
21
|
"types": "./types.public.d.ts",
|
22
|
+
"exports": {
|
23
|
+
".": {
|
24
|
+
"import": "./index.js",
|
25
|
+
"types": "./types.public.d.ts"
|
26
|
+
},
|
27
|
+
"./*": {
|
28
|
+
"import": "./*"
|
29
|
+
}
|
30
|
+
},
|
22
31
|
"scripts": {
|
23
32
|
"database-mongodb:test": "node ./tests/runner.test.js",
|
24
33
|
"test": "npm run database-mongodb:test",
|
@@ -0,0 +1,110 @@
|
|
1
|
+
/**
|
2
|
+
* @import {extension} from '@storecraft/core/extensions'
|
3
|
+
*/
|
4
|
+
import { MongoDB } from './index.js';
|
5
|
+
/**
|
6
|
+
*
|
7
|
+
* @typedef {object} Config
|
8
|
+
* @property {string} openai_key OpenAI key
|
9
|
+
*/
|
10
|
+
|
11
|
+
/**
|
12
|
+
* @implements {extension<Config>}
|
13
|
+
*/
|
14
|
+
export class MongoVectorSearch {
|
15
|
+
/** @param {Config} config */
|
16
|
+
constructor(config) {
|
17
|
+
this.config = config
|
18
|
+
}
|
19
|
+
|
20
|
+
info = {
|
21
|
+
name: 'Mongo Vector Search',
|
22
|
+
}
|
23
|
+
|
24
|
+
/** @type {extension["onInit"]} */
|
25
|
+
onInit = (app) => {
|
26
|
+
this.app = app;
|
27
|
+
|
28
|
+
app.pubsub.on(
|
29
|
+
'products/upsert',
|
30
|
+
async (evt) => {
|
31
|
+
const product = evt.payload.current;
|
32
|
+
// @ts-ignore
|
33
|
+
product.embedding = await embed_text(
|
34
|
+
`This product's title is ${product.title}, it's price is
|
35
|
+
${product.price} and has the following description
|
36
|
+
${product.description}`
|
37
|
+
);
|
38
|
+
console.log('Update Product ' + product.handle);
|
39
|
+
}
|
40
|
+
)
|
41
|
+
}
|
42
|
+
|
43
|
+
/** @type {extension["invokeAction"]} */
|
44
|
+
invokeAction = (handle) => {
|
45
|
+
|
46
|
+
if (handle==='search') {
|
47
|
+
/** @param {{query: string, limit:number}} args */
|
48
|
+
return async (args) => {
|
49
|
+
|
50
|
+
const db = (/** @type {MongoDB} */ (this.app.db));
|
51
|
+
|
52
|
+
return db.collection('products').aggregate(
|
53
|
+
[
|
54
|
+
{
|
55
|
+
"$vectorSearch": {
|
56
|
+
queryVector: await embed_text(
|
57
|
+
args.query, this.config.openai_key
|
58
|
+
),
|
59
|
+
path: "embedding",
|
60
|
+
exact: true,
|
61
|
+
limit: args.limit ?? 1,
|
62
|
+
index: "vector_index",
|
63
|
+
},
|
64
|
+
},
|
65
|
+
{
|
66
|
+
"$project": {
|
67
|
+
embedding: 0, _relations: 0, _id: 0
|
68
|
+
}
|
69
|
+
}
|
70
|
+
],
|
71
|
+
).toArray();
|
72
|
+
|
73
|
+
}
|
74
|
+
}
|
75
|
+
}
|
76
|
+
|
77
|
+
}
|
78
|
+
|
79
|
+
|
80
|
+
/**
|
81
|
+
*
|
82
|
+
* @param {string} text
|
83
|
+
* @param {string} openai_key
|
84
|
+
* @returns {Promise<number[]>} 1536 vector
|
85
|
+
*/
|
86
|
+
export const embed_text = async (text = 'hello', openai_key) => {
|
87
|
+
|
88
|
+
const r = await fetch(
|
89
|
+
'https://api.openai.com/v1/embeddings',
|
90
|
+
{
|
91
|
+
method: 'POST',
|
92
|
+
headers: {
|
93
|
+
'Authorization': `Bearer ${openai_key}`,
|
94
|
+
'Content-Type': 'application/json'
|
95
|
+
},
|
96
|
+
body: JSON.stringify(
|
97
|
+
{
|
98
|
+
input: text,
|
99
|
+
model: 'text-embedding-3-small',
|
100
|
+
encoding_format: 'float',
|
101
|
+
dimensions: 1536
|
102
|
+
}
|
103
|
+
)
|
104
|
+
}
|
105
|
+
);
|
106
|
+
|
107
|
+
const json = await r.json();
|
108
|
+
|
109
|
+
return json.data?.[0]?.embedding;
|
110
|
+
}
|