Skip to content

Commit a88b5e8

Browse files
committed
updated readme examples slightly
1 parent 2a3ac88 commit a88b5e8

File tree

2 files changed

+31
-10
lines changed

2 files changed

+31
-10
lines changed

README.md

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ interface Idea extends VectorDoc {
2121
}
2222

2323
// 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*' });
2626

2727
(async () => {
2828
try {
@@ -81,6 +81,7 @@ const db = client.db('https://f1183f14-dc85-4fbf-8aae-f1ca97338bbb-us-east-1.app
8181
console.log(`${doc.idea}: ${doc.$similarity}`);
8282
}
8383

84+
// Cleanup (if desired)
8485
await collection.drop();
8586
} finally {
8687
// Cleans up all open http sessions
@@ -132,6 +133,10 @@ a rich set of types to help you write type-safe code.
132133
Here are some examples of how you can properly leverage types to make your code more robust:
133134

134135
```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+
135140
import { DataAPIClient, StrictFilter, StrictSort, UUID } from '@datastax/astra-db-ts';
136141

137142
const client = new DataAPIClient('*TOKEN*');
@@ -189,9 +194,10 @@ import { DataAPIClient } from '@datastax/astra-db-ts';
189194
// Reference an untyped collection
190195
const client = new DataAPIClient('*TOKEN*');
191196
const db = client.db('*ENDPOINT*', { namespace: '*NAMESPACE*' });
192-
const collection = db.collection('*COLLECTION*');
193197

194198
(async () => {
199+
const collection = await db.createCollection('dates_test');
200+
195201
// Insert documents with some dates
196202
await collection.insertOne({ dateOfBirth: new Date(1394104654000) });
197203
await collection.insertOne({ dateOfBirth: new Date('1863-05-28') });
@@ -210,8 +216,10 @@ const collection = db.collection('*COLLECTION*');
210216
// Will print *around* `new Date()` (i.e. when server processed the request)
211217
const found = await collection.findOne({ dateOfBirth: { $lt: new Date('1900-01-01') } });
212218
console.log(found?.lastModified);
219+
220+
// Cleanup (if desired)
221+
await collection.drop();
213222
})();
214-
215223
```
216224

217225
### Working with ObjectIds and UUIDs
@@ -235,7 +243,7 @@ const db = client.db('*ENDPOINT*', { namespace: '*NAMESPACE*' });
235243

236244
(async () => {
237245
// 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' } });
239247

240248
// You can manually set whatever ID you want
241249
await collection.insertOne({ _id: new ObjectId("65fd9b52d7fabba03349d013"), name: 'John' });
@@ -256,7 +264,10 @@ const db = client.db('*ENDPOINT*', { namespace: '*NAMESPACE*' });
256264
// And let's get Jane as a document
257265
// (Prints "Jane", the generated UUIDv4, and true)
258266
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();
260271
})();
261272
```
262273

@@ -280,6 +291,7 @@ const client = new DataAPIClient('*TOKEN*', {
280291
monitorCommands: true,
281292
},
282293
});
294+
const db = client.db('*ENDPOINT*');
283295

284296
client.on('commandStarted', (event) => {
285297
console.log(`Running command ${event.commandName}`);
@@ -293,13 +305,21 @@ client.on('commandFailed', (event) => {
293305
console.error(`Command ${event.commandName} failed w/ error ${event.error}`);
294306
});
295307

296-
const db = client.db('*ENDPOINT*');
297-
const coll = db.collection('*COLLECTION*');
298-
299308
(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+
300314
// Should log
301315
// - "Running command insertOne"
302316
// - "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();
304324
})();
305325
```

etc/astra-db-ts.api.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,7 @@ export type CurrentDate<Schema> = {
318318

319319
// @public
320320
export class CursorIsStartedError extends DataAPIError {
321+
// @internal
321322
constructor(message: string);
322323
}
323324

0 commit comments

Comments
 (0)