@@ -21,8 +21,8 @@ interface Idea extends VectorDoc {
21
21
}
22
22
23
23
// Connect to the db
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 ' );
24
+ const client = new DataAPIClient (' *TOKEN* ' );
25
+ const db = client .db (' *ENDPOINT* ' , { namespace: ' *NAMESPACE* ' } );
26
26
27
27
(async () => {
28
28
try {
@@ -81,6 +81,7 @@ const db = client.db('https://f1183f14-dc85-4fbf-8aae-f1ca97338bbb-us-east-1.app
81
81
console .log (` ${doc .idea }: ${doc .$similarity } ` );
82
82
}
83
83
84
+ // Cleanup (if desired)
84
85
await collection .drop ();
85
86
} finally {
86
87
// Cleans up all open http sessions
@@ -132,6 +133,10 @@ a rich set of types to help you write type-safe code.
132
133
Here are some examples of how you can properly leverage types to make your code more robust:
133
134
134
135
``` typescript
136
+ // First of all:
137
+ // I *highly* recommend writing your query objects & filter objects and such inline with the methods
138
+ // to get the best possible type-checking and autocomplete
139
+
135
140
import { DataAPIClient , StrictFilter , StrictSort , UUID } from ' @datastax/astra-db-ts' ;
136
141
137
142
const client = new DataAPIClient (' *TOKEN*' );
@@ -189,9 +194,10 @@ import { DataAPIClient } from '@datastax/astra-db-ts';
189
194
// Reference an untyped collection
190
195
const client = new DataAPIClient (' *TOKEN*' );
191
196
const db = client .db (' *ENDPOINT*' , { namespace: ' *NAMESPACE*' });
192
- const collection = db .collection (' *COLLECTION*' );
193
197
194
198
(async () => {
199
+ const collection = await db .createCollection (' dates_test' );
200
+
195
201
// Insert documents with some dates
196
202
await collection .insertOne ({ dateOfBirth: new Date (1394104654000 ) });
197
203
await collection .insertOne ({ dateOfBirth: new Date (' 1863-05-28' ) });
@@ -210,8 +216,10 @@ const collection = db.collection('*COLLECTION*');
210
216
// Will print *around* `new Date()` (i.e. when server processed the request)
211
217
const found = await collection .findOne ({ dateOfBirth: { $lt: new Date (' 1900-01-01' ) } });
212
218
console .log (found ?.lastModified );
219
+
220
+ // Cleanup (if desired)
221
+ await collection .drop ();
213
222
})();
214
-
215
223
```
216
224
217
225
### Working with ObjectIds and UUIDs
@@ -235,7 +243,7 @@ const db = client.db('*ENDPOINT*', { namespace: '*NAMESPACE*' });
235
243
236
244
(async () => {
237
245
// Create a collection with a UUIDv7 as the default ID
238
- const collection = await db .createCollection <Person >(' my_collection ' , { defaultId: { type: ' uuidv7' } });
246
+ const collection = await db .createCollection <Person >(' ids_test ' , { defaultId: { type: ' uuidv7' } });
239
247
240
248
// You can manually set whatever ID you want
241
249
await collection .insertOne ({ _id: new ObjectId (" 65fd9b52d7fabba03349d013" ), name: ' John' });
@@ -256,7 +264,10 @@ const db = client.db('*ENDPOINT*', { namespace: '*NAMESPACE*' });
256
264
// And let's get Jane as a document
257
265
// (Prints "Jane", the generated UUIDv4, and true)
258
266
const jane = await collection .findOne ({ name: ' Jane' });
259
- console .log (jane ?.name , jane ?.friendId , friendId .equals (jane ?.friendId ));
267
+ console .log (jane ?.name , jane ?.friendId ?.toString (), friendId .equals (jane ?.friendId ));
268
+
269
+ // Cleanup (if desired)
270
+ await collection .drop ();
260
271
})();
261
272
```
262
273
@@ -280,6 +291,7 @@ const client = new DataAPIClient('*TOKEN*', {
280
291
monitorCommands: true ,
281
292
},
282
293
});
294
+ const db = client .db (' *ENDPOINT*' );
283
295
284
296
client .on (' commandStarted' , (event ) => {
285
297
console .log (` Running command ${event .commandName } ` );
@@ -293,13 +305,21 @@ client.on('commandFailed', (event) => {
293
305
console .error (` Command ${event .commandName } failed w/ error ${event .error } ` );
294
306
});
295
307
296
- const db = client .db (' *ENDPOINT*' );
297
- const coll = db .collection (' *COLLECTION*' );
298
-
299
308
(async () => {
309
+ // Should log
310
+ // - "Running command createCollection"
311
+ // - "Command createCollection succeeded in <time>ms"
312
+ const collection = await db .createCollection (' my_collection' , { checkExists: false });
313
+
300
314
// Should log
301
315
// - "Running command insertOne"
302
316
// - "Command insertOne succeeded in <time>ms"
303
- await coll .insertOne ({ name: ' Queen' });
317
+ await collection .insertOne ({ name: ' Queen' });
318
+
319
+ // Remove all monitoring listeners
320
+ client .removeAllListeners ();
321
+
322
+ // Cleanup (if desired) (with no logging)
323
+ await collection .drop ();
304
324
})();
305
325
```
0 commit comments