Skip to content

Commit 2a3ac88

Browse files
committed
some last-minute doc updates + error management fixes
1 parent e389fa5 commit 2a3ac88

28 files changed

+492
-283
lines changed

README.md

Lines changed: 93 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -21,62 +21,70 @@ interface Idea extends VectorDoc {
2121
}
2222

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

2727
(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();
8088
}
8189
})();
8290
```
@@ -176,31 +184,34 @@ Native JS `Date` objects can be used anywhere in documents to represent dates an
176184
Document fields stored using the `{ $date: number }` will also be returned as Date objects when read.
177185

178186
```typescript
179-
import { DataApiClient } from '@datastax/astra-db-ts';
187+
import { DataAPIClient } from '@datastax/astra-db-ts';
180188

181189
// 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+
})();
200214

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);
204215
```
205216

206217
### Working with ObjectIds and UUIDs
@@ -285,8 +296,10 @@ client.on('commandFailed', (event) => {
285296
const db = client.db('*ENDPOINT*');
286297
const coll = db.collection('*COLLECTION*');
287298

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+
})();
292305
```

etc/astra-db-ts.api.md

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
```ts
66

77
import { context as context_2 } from 'fetch-h2';
8-
import { Headers as Headers_2 } from 'fetch-h2/dist/lib/headers';
8+
import { Response as Response_2 } from 'fetch-h2';
99
import TypedEmitter from 'typed-emitter';
1010

1111
// @public
@@ -57,10 +57,8 @@ export class AdminCommandStartedEvent extends AdminCommandEvent {
5757

5858
// @public
5959
export class AdminCommandSucceededEvent extends AdminCommandEvent {
60-
// Warning: (ae-forgotten-export) The symbol "GuaranteedAPIResponse" needs to be exported by the entry point index.d.ts
61-
//
6260
// @internal
63-
constructor(info: DevOpsAPIRequestInfo, longRunning: boolean, resp: GuaranteedAPIResponse, started: number);
61+
constructor(info: DevOpsAPIRequestInfo, longRunning: boolean, data: Record<string, any> | undefined, started: number);
6462
readonly duration: number;
6563
readonly resBody?: Record<string, any>;
6664
}
@@ -146,6 +144,7 @@ export interface BulkWriteOrderedOptions extends WithTimeout {
146144

147145
// @public
148146
export class BulkWriteResult<Schema extends SomeDoc> {
147+
// @internal
149148
constructor(
150149
deletedCount?: number,
151150
insertedCount?: number,
@@ -217,6 +216,7 @@ export class Collection<Schema extends SomeDoc = SomeDoc> {
217216

218217
// @public
219218
export class CollectionAlreadyExistsError extends DataAPIError {
219+
// @internal
220220
constructor(namespace: string, collectionName: string);
221221
readonly collectionName: string;
222222
readonly namespace: string;
@@ -299,6 +299,16 @@ export abstract class CumulativeDataAPIError extends DataAPIResponseError {
299299
readonly partialResult: unknown;
300300
}
301301

302+
// @public
303+
export interface CuratedAPIResponse {
304+
body?: string;
305+
headers: Record<string, any>;
306+
httpVersion: 1 | 2;
307+
status: number;
308+
statusText: string;
309+
url: string;
310+
}
311+
302312
// @public
303313
export type CurrentDate<Schema> = {
304314
[K in keyof Schema as Schema[K] extends Date | {
@@ -361,14 +371,16 @@ export interface DataAPIErrorDescriptor {
361371

362372
// @public
363373
export class DataAPIResponseError extends DataAPIError {
374+
// @internal
364375
constructor(message: string, errorDescriptors: DataAPIErrorDescriptor[], detailedErrorDescriptors: DataAPIDetailedErrorDescriptor[]);
365376
readonly detailedErrorDescriptors: DataAPIDetailedErrorDescriptor[];
366377
readonly errorDescriptors: DataAPIErrorDescriptor[];
367378
readonly message: string;
368379
}
369380

370381
// @public
371-
export class DataAPITimeout extends DataAPIError {
382+
export class DataAPITimeoutError extends DataAPIError {
383+
// @internal
372384
constructor(timeout: number);
373385
readonly timeout: number;
374386
}
@@ -552,14 +564,17 @@ export interface DevOpsAPIErrorDescriptor {
552564

553565
// @public
554566
export class DevOpsAPIResponseError extends DevOpsAPIError {
567+
// Warning: (ae-forgotten-export) The symbol "ResponseWithBody" needs to be exported by the entry point index.d.ts
568+
//
555569
// @internal
556-
constructor(resp: GuaranteedAPIResponse);
570+
constructor(resp: ResponseWithBody, data: Record<string, any> | undefined);
557571
readonly errors: DevOpsAPIErrorDescriptor[];
558-
readonly status?: number;
572+
readonly raw: CuratedAPIResponse;
573+
readonly status: number;
559574
}
560575

561576
// @public
562-
export class DevOpsAPITimeout extends DevOpsAPIError {
577+
export class DevOpsAPITimeoutError extends DevOpsAPIError {
563578
// @internal
564579
constructor(url: string, timeout: number);
565580
readonly timeout: number;
@@ -569,9 +584,9 @@ export class DevOpsAPITimeout extends DevOpsAPIError {
569584
// @public
570585
export class DevOpsUnexpectedStateError extends DevOpsAPIError {
571586
// @internal
572-
constructor(message: string, raw?: GuaranteedAPIResponse);
587+
constructor(message: string, expected: string[], data: Record<string, any> | undefined);
573588
readonly dbInfo?: FullDatabaseInfo;
574-
readonly status?: number;
589+
readonly expected: string[];
575590
}
576591

577592
// @public
@@ -934,7 +949,7 @@ export type Sort = Record<string, SortDirection> | {
934949
};
935950

936951
// @public
937-
export type SortDirection = 1 | -1;
952+
export type SortDirection = 1 | -1 | 'asc' | 'desc' | 'ascending' | 'descending';
938953

939954
// @public
940955
export type StrictDateUpdate<Schema extends SomeDoc, InNotation = ToDotNotation<Schema>> = ContainsDate<InNotation> extends true ? {
@@ -1023,6 +1038,7 @@ export type ToDotNotation<Schema extends SomeDoc> = Merge<_ToDotNotation<Schema,
10231038

10241039
// @public
10251040
export class TooManyDocumentsToCountError extends DataAPIError {
1041+
// @internal
10261042
constructor(limit: number, hitServerLimit: boolean);
10271043
readonly hitServerLimit: boolean;
10281044
readonly limit: number;

0 commit comments

Comments
 (0)