@storecraft/database-turso 1.0.13 → 1.0.15

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 CHANGED
@@ -5,16 +5,85 @@
5
5
  width='90%' />
6
6
  </div><hr/><br/>
7
7
 
8
- Official `libSql` / `Turso` driver for `StoreCraft` on any platforms.
8
+ Official `libSql` / `Turso` driver for `StoreCraft` on any platform.
9
9
  Includes a vector store.
10
10
 
11
11
  ```bash
12
12
  npm i @storecraft/database-turso
13
13
  ```
14
14
 
15
- ## Setup
16
- You can run a local database,
17
- or,
15
+ ## Local usage
16
+
17
+ ```js
18
+ import 'dotenv/config';
19
+ import http from "node:http";
20
+ import { App } from '@storecraft/core'
21
+ import { NodePlatform } from '@storecraft/core/platform/node';
22
+ import { NodeLocalStorage } from '@storecraft/core/storage/node'
23
+ import { Turso, LibSQLVectorStore } from '@storecraft/database-turso'
24
+ import { migrateToLatest } from '@storecraft/database-turso/migrate.js'
25
+
26
+ const app = new App(
27
+ {
28
+ auth_admins_emails: ['admin@sc.com'],
29
+ }
30
+ )
31
+ .withPlatform(new NodePlatform())
32
+ .withDatabase(
33
+ new Turso(
34
+ {
35
+ url: 'file:local.db',
36
+ }
37
+ )
38
+ )
39
+ .withStorage(new NodeLocalStorage('storage'))
40
+ .withVectorStore(
41
+ new LibSQLVectorStore(
42
+ {
43
+ embedder: new OpenAIEmbedder(),
44
+ url: 'file:local-vector.db'
45
+ }
46
+ )
47
+ )
48
+
49
+ await app.init();
50
+ await migrateToLatest(app.db, false);
51
+ await app.vectorstore.createVectorIndex();
52
+
53
+ const server = http.createServer(app.handler).listen(
54
+ 8000,
55
+ () => {
56
+ console.log(`Server is running on http://localhost:8000`);
57
+ }
58
+ );
59
+
60
+ ```
61
+
62
+ Storecraft will search the following `env` variables
63
+
64
+ ```bash
65
+ LIBSQL_URL=./data.db
66
+ # optional, if absent will use the same as LIBSQL_URL
67
+ LIBSQL_VECTOR_URL=./data-vector.db
68
+ ```
69
+
70
+ So, you can instantiate with empty config
71
+
72
+ ```ts
73
+ .withDatabase(
74
+ new Turso()
75
+ )
76
+ .withVectorStore(
77
+ new LibSQLVectorStore(
78
+ {
79
+ embedder: new OpenAIEmbedder(),
80
+ }
81
+ )
82
+ )
83
+ ```
84
+
85
+
86
+ ## Cloud usage
18
87
 
19
88
  connect to a cloud `libsql` and `Turso` platform
20
89
  - First, login to your [turso](https://turso.tech) account.
@@ -36,8 +105,6 @@ import { migrateToLatest } from '@storecraft/database-turso/migrate.js'
36
105
  const app = new App(
37
106
  {
38
107
  auth_admins_emails: ['admin@sc.com'],
39
- auth_secret_access_token: 'auth_secret_access_token',
40
- auth_secret_refresh_token: 'auth_secret_refresh_token'
41
108
  }
42
109
  )
43
110
  .withPlatform(new NodePlatform())
@@ -48,8 +115,6 @@ const app = new App(
48
115
  // all of these configurations can be inferred by env variables at init
49
116
  url: process.env.LIBSQL_URL,
50
117
  authToken: process.env.LIBSQL_API_TOKEN,
51
- // or local
52
- url: 'file:local.db',
53
118
  }
54
119
  )
55
120
  )
@@ -57,14 +122,17 @@ const app = new App(
57
122
  .withVectorStore(
58
123
  new LibSQLVectorStore(
59
124
  {
60
- embedder: new OpenAIEmbedder()
125
+ embedder: new OpenAIEmbedder(),
126
+ url: process.env.LIBSQL_URL,
127
+ authToken: process.env.LIBSQL_API_TOKEN,
61
128
  }
62
129
  )
63
130
  )
64
131
 
65
132
  await app.init();
66
133
  await migrateToLatest(app.db, false);
67
-
134
+ await app.vectorstore.createVectorIndex();
135
+
68
136
  const server = http.createServer(app.handler).listen(
69
137
  8000,
70
138
  () => {
@@ -74,6 +142,30 @@ const server = http.createServer(app.handler).listen(
74
142
 
75
143
  ```
76
144
 
145
+ Storecraft will search the following `env` variables
146
+
147
+ ```bash
148
+ LIBSQL_URL=libsql://<your_database_name>.something.com
149
+ LIBSQL_AUTH_TOKEN=your_api_key
150
+ ```
151
+
152
+ So, you can instantiate with empty config
153
+
154
+ ```ts
155
+ .withDatabase(
156
+ new Turso()
157
+ )
158
+ .withVectorStore(
159
+ new LibSQLVectorStore(
160
+ {
161
+ embedder: new OpenAIEmbedder(),
162
+ }
163
+ )
164
+ )
165
+ ```
166
+
167
+
168
+
77
169
  ```text
78
170
  Author: Tomer Shalev <tomer.shalev@gmail.com>
79
171
  ```
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@storecraft/database-turso",
3
- "version": "1.0.13",
3
+ "version": "1.0.15",
4
4
  "description": "`Storecraft` database driver for `Turso` (cloud sqlite)",
5
5
  "license": "MIT",
6
6
  "author": "Tomer Shalev (https://github.com/store-craft)",
@@ -71,6 +71,16 @@ export class LibSQLVectorStore {
71
71
  };
72
72
  }
73
73
 
74
+ /** @type {VectorStore["metric"]} */
75
+ get metric() {
76
+ return this.config.similarity;
77
+ };
78
+
79
+ /** @type {VectorStore["dimensions"]} */
80
+ get dimensions() {
81
+ return this.config.dimensions;
82
+ };
83
+
74
84
  get client() {
75
85
  if(!this.config.url) {
76
86
  throw new Error('LibSQLVectorStore::client() - missing url');
@@ -251,7 +261,10 @@ export class LibSQLVectorStore {
251
261
  metadata: parse_json_safely(row.metadata),
252
262
  namespace: String(row.namespace),
253
263
  },
254
- score: Number(row.score)
264
+ // `libsql` score is (1 - Cosine Similarity) which yields a distance
265
+ // between [0, 2] where 0 is the most similar.
266
+ // This is not in accordance with other apis, so we invert it to [-1, 1]
267
+ score: (this.metric==='cosine') ? (1.0 - Number(row.score)) : Number(row.score)
255
268
  }
256
269
  )
257
270
  );
@@ -1,6 +1,6 @@
1
1
 
2
2
  import type { AIEmbedder } from '@storecraft/core/ai';
3
- export * from './index.js';
3
+ export { LibSQLVectorStore, DEFAULT_INDEX_NAME } from './index.js';
4
4
 
5
5
  export type Config = {
6
6
  /**