@@ -22,7 +22,7 @@ Use your preferred package manager to install `@datastax/astra-db-ts`. Note that
22
22
23
23
Get the * API endpoint* and your * application token* for your Astra DB instance @ [ astra.datastax.com] ( https://astra.datastax.com ) .
24
24
25
- Try the following code after setting the following environment variables:
25
+ ### Collections
26
26
27
27
``` typescript
28
28
import { DataAPIClient , VectorDoc , UUID , ObjectId } from ' @datastax/astra-db-ts' ;
@@ -37,68 +37,146 @@ const client = new DataAPIClient('*TOKEN*');
37
37
const db = client .db (' *ENDPOINT*' , { namespace: ' *NAMESPACE*' });
38
38
39
39
(async () => {
40
- try {
41
- // Creates collections, or gets it if it already exists with same options
42
- const collection = await db .createCollection <Idea >(' vector_5_collection' , {
43
- vector: {
44
- dimension: 5 ,
45
- metric: ' cosine' ,
46
- },
47
- checkExists: false ,
48
- });
49
-
50
- // Insert many ideas into the collections
51
- const ideas = [
52
- {
53
- idea: ' An AI quilt to help you sleep forever' ,
54
- $vector: [0.1 , 0.15 , 0.3 , 0.12 , 0.05 ],
55
- },
56
- {
57
- _id: new UUID (' e7f1f3a0-7e3d-11eb-9439-0242ac130002' ),
58
- idea: ' Vision Vector Frame—A deep learning display that controls your mood' ,
59
- $vector: [0.1 , 0.05 , 0.08 , 0.3 , 0.6 ],
60
- },
61
- {
62
- idea: ' A smartwatch that tells you what to eat based on your mood' ,
63
- $vector: [0.2 , 0.3 , 0.1 , 0.4 , 0.15 ],
64
- },
65
- ];
66
- await collection .insertMany (ideas );
67
-
68
- // Insert a specific idea into the collections
69
- const sneakersIdea = {
70
- _id: new ObjectId (' 507f191e810c19729de860ea' ),
71
- idea: ' ChatGPT-integrated sneakers that talk to you' ,
72
- $vector: [0.45 , 0.09 , 0.01 , 0.2 , 0.11 ],
73
- }
74
- await collection .insertOne (sneakersIdea );
75
-
76
- // Actually, let's change that idea
77
- await collection .updateOne (
78
- { _id: sneakersIdea ._id },
79
- { $set: { idea: ' Gemini-integrated sneakers that talk to you' } },
80
- );
81
-
82
- // Get similar results as desired
83
- const cursor = collection .find ({}, {
84
- vector: [0.1 , 0.15 , 0.3 , 0.12 , 0.05 ],
85
- includeSimilarity: true ,
86
- limit: 2 ,
87
- });
88
-
89
- for await (const doc of cursor ) {
90
- // Prints the following:
91
- // - An AI quilt to help you sleep forever: 1
92
- // - A smartwatch that tells you what to eat based on your mood: 0.85490346
93
- console .log (` ${doc .idea }: ${doc .$similarity } ` );
94
- }
95
-
96
- // Cleanup (if desired)
97
- await collection .drop ();
98
- } finally {
99
- // Cleans up all open http sessions
100
- await client .close ();
40
+ // Creates collections, or gets it if it already exists with same options
41
+ const collection = await db .createCollection <Idea >(' vector_5_collection' , {
42
+ vector: {
43
+ dimension: 5 ,
44
+ metric: ' cosine' ,
45
+ },
46
+ checkExists: false ,
47
+ });
48
+
49
+ // Insert many ideas into the collections
50
+ const ideas = [
51
+ {
52
+ idea: ' An AI quilt to help you sleep forever' ,
53
+ $vector: [0.1 , 0.15 , 0.3 , 0.12 , 0.05 ],
54
+ },
55
+ {
56
+ _id: new UUID (' e7f1f3a0-7e3d-11eb-9439-0242ac130002' ),
57
+ idea: ' Vision Vector Frame—A deep learning display that controls your mood' ,
58
+ $vector: [0.1 , 0.05 , 0.08 , 0.3 , 0.6 ],
59
+ },
60
+ {
61
+ idea: ' A smartwatch that tells you what to eat based on your mood' ,
62
+ $vector: [0.2 , 0.3 , 0.1 , 0.4 , 0.15 ],
63
+ },
64
+ ];
65
+ await collection .insertMany (ideas );
66
+
67
+ // Insert a specific idea into the collections
68
+ const sneakersIdea = {
69
+ _id: new ObjectId (' 507f191e810c19729de860ea' ),
70
+ idea: ' ChatGPT-integrated sneakers that talk to you' ,
71
+ $vector: [0.45 , 0.09 , 0.01 , 0.2 , 0.11 ],
72
+ }
73
+ await collection .insertOne (sneakersIdea );
74
+
75
+ // Actually, let's change that idea
76
+ await collection .updateOne (
77
+ { _id: sneakersIdea ._id },
78
+ { $set: { idea: ' Gemini-integrated sneakers that talk to you' } },
79
+ );
80
+
81
+ // Get similar results as desired
82
+ const cursor = collection .find ({}, {
83
+ vector: [0.1 , 0.15 , 0.3 , 0.12 , 0.05 ],
84
+ includeSimilarity: true ,
85
+ limit: 2 ,
86
+ });
87
+
88
+ for await (const doc of cursor ) {
89
+ // Prints the following:
90
+ // - An AI quilt to help you sleep forever: 1
91
+ // - A smartwatch that tells you what to eat based on your mood: 0.85490346
92
+ console .log (` ${doc .idea }: ${doc .$similarity } ` );
93
+ }
94
+
95
+ // Cleanup (if desired)
96
+ await collection .drop ();
97
+ })();
98
+ ```
99
+
100
+ ### Tables
101
+
102
+ ``` typescript
103
+ import { CreateTableDefinition , DataAPIClient , InferTableSchema , vector } from ' @datastax/astra-db-ts' ;
104
+
105
+ // Connect to the db
106
+ const client = new DataAPIClient ();
107
+ const db = client .db (' https://178cf75b-ec19-4d13-9ca7-b6dcfa613157-us-west-2.apps.astra-dev.datastax.com' , { token: ' AstraCS:EFZEBUXuMzwyFntFYcroNsWQ:e473c9b244253e3c873346cc8584d4767789d93e54c6c5453c996fbd5ab4605d' });
108
+
109
+ // Example table schema using bespoke Data API table definition syntax
110
+ const TableDefinition = <const >{
111
+ columns: {
112
+ id: ' int' ,
113
+ summary: ' text' ,
114
+ tags: { type: ' set' , valueType: ' text' },
115
+ vector: { type: ' vector' , dimension: 3 },
116
+ },
117
+ primaryKey: {
118
+ partitionBy: [' id' ],
119
+ },
120
+ } satisfies CreateTableDefinition ;
121
+
122
+ // Infer the TS-equivalent type from the table definition (like zod or arktype). Equivalent to:
123
+ //
124
+ // interface TableSchema extends Row<'id'> { // Types 'id' as a primary key for insertion command return types
125
+ // summary?: string | null; // Not a primary key, so it's optional and may return as null when found
126
+ // tags?: Set<string>; // Sets/maps/lists are optional to insert, but will actually be returned as empty collections instead of null
127
+ // vector?: DataAPIVector | null; // Vectors, however, may be null.
128
+ // }
129
+ type TableSchema = InferTableSchema <typeof TableDefinition >;
130
+
131
+ (async () => {
132
+ // Create the table if it doesn't alredy exist
133
+ const table = await db .createTable (' dreams' , {
134
+ definition: TableDefinition ,
135
+ ifNotExists: true ,
136
+ });
137
+
138
+ // Enables vector search on the table (on the 'vector' column)
139
+ await table .createVectorIndex (' dreams_vector_idx' , ' vector' , {
140
+ options: { metric: ' cosine' },
141
+ ifNotExists: true ,
142
+ });
143
+
144
+ // Insert some rows into the table
145
+ const rows: TableSchema [] = [{
146
+ id: 102 ,
147
+ summary: ' A dinner on the Moon' ,
148
+ vector: vector ([0.2 , - 0.3 , - 0.5 ]), // Shorthand for `new DataAPIVector([0.2, -0.3, -0.5])`
149
+ }, {
150
+ id: 103 ,
151
+ summary: ' Riding the waves' ,
152
+ tags: new Set ([' sport' ]),
153
+ vector: vector ([0 , 0.2 , 1 ]),
154
+ }, {
155
+ id: 37 ,
156
+ summary: ' Meeting Beethoven at the dentist' ,
157
+ vector: vector ([0.2 , 0.6 , 0 ]),
158
+ }];
159
+ await table .insertMany (rows );
160
+
161
+ // Hm, changed my mind
162
+ await table .updateOne ({ id: 103 }, { $set: { summary: ' Surfers\' paradise' } });
163
+
164
+ // Let's see what we've got
165
+ const cursor = table .find ({}, {
166
+ sort: { vector: vector ([0 , 0.2 , 0.4 ]) },
167
+ includeSimilarity: true ,
168
+ limit: 2 ,
169
+ });
170
+
171
+ // This would print:
172
+ // - Surfers' paradise: 0.98238194
173
+ // - Friendly aliens in town: 0.91873914
174
+ for await (const result of cursor ) {
175
+ console .log (` ${result .summary }: ${result .$similarity } ` );
101
176
}
177
+
178
+ // Cleanup (if desired)
179
+ await table .drop ();
102
180
})();
103
181
```
104
182
0 commit comments