@@ -21,62 +21,70 @@ interface Idea extends VectorDoc {
21
21
}
22
22
23
23
// Connect to the db
24
- const client = new DataAPIClient (' *TOKEN* ' );
25
- const db = client .db (' *ENDPOINT* ' , { namespace: ' *NAMESPACE* ' } );
24
+ const client = new DataAPIClient (' AstraCS:OengMjURbGWRjuMTBMqXWwOn:3bcbf200a056069bb00f17fa52d92d935952a1f2ac58c99596edabb1e1b3950c ' );
25
+ const db = client .db (' https://f1183f14-dc85-4fbf-8aae-f1ca97338bbb-us-east-1.apps.astra.datastax.com ' );
26
26
27
27
(async () => {
28
- // Creates collection, or gets it if it already exists with same options
29
- const collection = await db .createCollection <Idea >(' vector_5_collection' , {
30
- vector: {
31
- dimension: 5 ,
32
- metric: ' cosine'
33
- },
34
- });
35
-
36
- // Insert many ideas into the collection
37
- const ideas = [
38
- {
39
- idea: ' An AI quilt to help you sleep forever' ,
40
- $vector: [0.1 , 0.15 , 0.3 , 0.12 , 0.05 ],
41
- },
42
- {
43
- _id: new UUID (' e7f1f3a0-7e3d-11eb-9439-0242ac130002' ),
44
- idea: ' Vision Vector Frame—A deep learning display that controls your mood' ,
45
- $vector: [0.1 , 0.05 , 0.08 , 0.3 , 0.6 ],
46
- },
47
- {
48
- idea: ' A smartwatch that tells you what to eat based on your mood' ,
49
- $vector: [0.2 , 0.3 , 0.1 , 0.4 , 0.15 ],
50
- },
51
- ];
52
- await collection .insertMany (ideas );
53
-
54
- // Insert a specific idea into the collection
55
- const sneakersIdea = {
56
- _id: new ObjectId (' 507f191e810c19729de860ea' ),
57
- idea: ' ChatGPT-integrated sneakers that talk to you' ,
58
- $vector: [0.45 , 0.09 , 0.01 , 0.2 , 0.11 ],
59
- }
60
- await collection .insertOne (sneakersIdea );
61
-
62
- // Actually, let's change that idea
63
- await collection .updateOne (
64
- { _id: sneakersIdea ._id },
65
- { $set: { idea: ' Gemini-integrated sneakers that talk to you' } },
66
- );
67
-
68
- // Get similar results as desired
69
- const cursor = collection .find ({}, {
70
- vector: [0.1 , 0.15 , 0.3 , 0.12 , 0.05 ],
71
- includeSimilarity: true ,
72
- limit: 2 ,
73
- });
74
-
75
- for await (const doc of cursor ) {
76
- // Prints the following:
77
- // - An AI quilt to help you sleep forever: 1
78
- // - A smartwatch that tells you what to eat based on your mood: 0.85490346
79
- console .log (` ${doc .idea }: ${doc .$similarity } ` );
28
+ try {
29
+ // Creates collection, or gets it if it already exists with same options
30
+ const collection = await db .createCollection <Idea >(' vector_5_collection' , {
31
+ vector: {
32
+ dimension: 5 ,
33
+ metric: ' cosine'
34
+ },
35
+ checkExists: false ,
36
+ });
37
+
38
+ // Insert many ideas into the collection
39
+ const ideas = [
40
+ {
41
+ idea: ' An AI quilt to help you sleep forever' ,
42
+ $vector: [0.1 , 0.15 , 0.3 , 0.12 , 0.05 ],
43
+ },
44
+ {
45
+ _id: new UUID (' e7f1f3a0-7e3d-11eb-9439-0242ac130002' ),
46
+ idea: ' Vision Vector Frame—A deep learning display that controls your mood' ,
47
+ $vector: [0.1 , 0.05 , 0.08 , 0.3 , 0.6 ],
48
+ },
49
+ {
50
+ idea: ' A smartwatch that tells you what to eat based on your mood' ,
51
+ $vector: [0.2 , 0.3 , 0.1 , 0.4 , 0.15 ],
52
+ },
53
+ ];
54
+ await collection .insertMany (ideas );
55
+
56
+ // Insert a specific idea into the collection
57
+ const sneakersIdea = {
58
+ _id: new ObjectId (' 507f191e810c19729de860ea' ),
59
+ idea: ' ChatGPT-integrated sneakers that talk to you' ,
60
+ $vector: [0.45 , 0.09 , 0.01 , 0.2 , 0.11 ],
61
+ }
62
+ await collection .insertOne (sneakersIdea );
63
+
64
+ // Actually, let's change that idea
65
+ await collection .updateOne (
66
+ { _id: sneakersIdea ._id },
67
+ { $set: { idea: ' Gemini-integrated sneakers that talk to you' } },
68
+ );
69
+
70
+ // Get similar results as desired
71
+ const cursor = collection .find ({}, {
72
+ vector: [0.1 , 0.15 , 0.3 , 0.12 , 0.05 ],
73
+ includeSimilarity: true ,
74
+ limit: 2 ,
75
+ });
76
+
77
+ for await (const doc of cursor ) {
78
+ // Prints the following:
79
+ // - An AI quilt to help you sleep forever: 1
80
+ // - A smartwatch that tells you what to eat based on your mood: 0.85490346
81
+ console .log (` ${doc .idea }: ${doc .$similarity } ` );
82
+ }
83
+
84
+ await collection .drop ();
85
+ } finally {
86
+ // Cleans up all open http sessions
87
+ await client .close ();
80
88
}
81
89
})();
82
90
```
@@ -176,31 +184,34 @@ Native JS `Date` objects can be used anywhere in documents to represent dates an
176
184
Document fields stored using the ` { $date: number } ` will also be returned as Date objects when read.
177
185
178
186
``` typescript
179
- import { DataApiClient } from ' @datastax/astra-db-ts' ;
187
+ import { DataAPIClient } from ' @datastax/astra-db-ts' ;
180
188
181
189
// Reference an untyped collection
182
- const client = new DataApiClient (' TOKEN' );
183
- const db = client .db (' ENDPOINT' , { namespace: ' NAMESPACE' });
184
- const collection = db .collection (' COLLECTION' );
185
-
186
- // Insert documents with some dates
187
- await collection .insertOne ({ dateOfBirth: new Date (1394104654000 ) });
188
- await collection .insertOne ({ dateOfBirth: new Date (' 1863-05-28' ) });
189
-
190
- // Update a document with a date and setting lastModified to now
191
- await collection .updateOne (
192
- {
193
- dateOfBirth: new Date (' 1863-05-28' ),
194
- },
195
- {
196
- $set: { message: ' Happy Birthday!' },
197
- $currentDate: { lastModified: true },
198
- },
199
- );
190
+ const client = new DataAPIClient (' *TOKEN*' );
191
+ const db = client .db (' *ENDPOINT*' , { namespace: ' *NAMESPACE*' });
192
+ const collection = db .collection (' *COLLECTION*' );
193
+
194
+ (async () => {
195
+ // Insert documents with some dates
196
+ await collection .insertOne ({ dateOfBirth: new Date (1394104654000 ) });
197
+ await collection .insertOne ({ dateOfBirth: new Date (' 1863-05-28' ) });
198
+
199
+ // Update a document with a date and setting lastModified to now
200
+ await collection .updateOne (
201
+ {
202
+ dateOfBirth: new Date (' 1863-05-28' ),
203
+ },
204
+ {
205
+ $set: { message: ' Happy Birthday!' },
206
+ $currentDate: { lastModified: true },
207
+ },
208
+ );
209
+
210
+ // Will print *around* `new Date()` (i.e. when server processed the request)
211
+ const found = await collection .findOne ({ dateOfBirth: { $lt: new Date (' 1900-01-01' ) } });
212
+ console .log (found ?.lastModified );
213
+ })();
200
214
201
- // Will print *around* `new Date()` (i.e. when server processed the request)
202
- const found = await collection .findOne ({ dateOfBirth: { $lt: new Date (' 1900-01-01' ) } });
203
- console .log (found ?.lastModified );
204
215
```
205
216
206
217
### Working with ObjectIds and UUIDs
@@ -285,8 +296,10 @@ client.on('commandFailed', (event) => {
285
296
const db = client .db (' *ENDPOINT*' );
286
297
const coll = db .collection (' *COLLECTION*' );
287
298
288
- // Should log
289
- // - "Running command insertOne"
290
- // - "Command insertOne succeeded in <time>ms"
291
- await coll .insertOne ({ name: ' Queen' });
299
+ (async () => {
300
+ // Should log
301
+ // - "Running command insertOne"
302
+ // - "Command insertOne succeeded in <time>ms"
303
+ await coll .insertOne ({ name: ' Queen' });
304
+ })();
292
305
```
0 commit comments