Skip to content

Commit 344b10a

Browse files
committed
killed DataAPITimestamp
1 parent eea4db0 commit 344b10a

File tree

10 files changed

+73
-279
lines changed

10 files changed

+73
-279
lines changed

etc/astra-db-ts.api.md

Lines changed: 14 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -370,8 +370,6 @@ export class CollCodecs {
370370
static forPath(path: string[], optsOrClass: CodecOpts<CollCodecSerDesFns, CollSerCtx, CollDesCtx> | CollCodecClass): RawCodec<CollCodecSerDesFns>;
371371
// (undocumented)
372372
static forType(type: string, optsOrClass: CodecOpts<CollCodecSerDesFns, CollSerCtx, CollDesCtx> | CollCodecClass): RawCodec<CollCodecSerDesFns>;
373-
// (undocumented)
374-
static USE_DATA_API_TIMESTAMPS_FOR_DATES: RawCodec<CollCodecSerDesFns>;
375373
}
376374

377375
// @public (undocumented)
@@ -861,7 +859,7 @@ export class DataAPIDate implements TableCodec<typeof DataAPIDate> {
861859
[$SerializeForTable](ctx: TableSerCtx): readonly [0, (string | undefined)?];
862860
constructor(input?: string | Date | DataAPIDateComponents);
863861
components(): DataAPIDateComponents;
864-
toDate(base?: Date | DataAPITime | DataAPITimestamp): Date;
862+
toDate(base?: Date | DataAPITime): Date;
865863
toString(): string;
866864
}
867865

@@ -968,7 +966,7 @@ export class DataAPITime implements TableCodec<typeof DataAPITime> {
968966
nanoseconds?: number;
969967
}));
970968
components(): DataAPITimeComponents;
971-
toDate(base?: Date | DataAPIDate | DataAPITimestamp): Date;
969+
toDate(base?: Date | DataAPIDate): Date;
972970
toString(): string;
973971
}
974972

@@ -993,41 +991,12 @@ export class DataAPITimeoutError extends DataAPIError {
993991
readonly timeout: Partial<TimeoutDescriptor>;
994992
}
995993

996-
// @public
997-
export class DataAPITimestamp implements CollCodec<typeof DataAPITimestamp>, TableCodec<typeof DataAPITimestamp> {
998-
static [$DeserializeForCollection](_: string, value: any, ctx: CollDesCtx): readonly [0, (DataAPITimestamp | undefined)?];
999-
static [$DeserializeForTable](_: unknown, value: any, ctx: TableDesCtx): readonly [0, (DataAPITimestamp | undefined)?];
1000-
[$SerializeForCollection](ctx: CollSerCtx): readonly [0, ({
1001-
$date: string;
1002-
} | undefined)?];
1003-
[$SerializeForTable](ctx: TableSerCtx): readonly [0, (string | undefined)?];
1004-
constructor(input?: string | Date | Partial<DataAPITimestampComponents>);
1005-
components(): DataAPITimestampComponents;
1006-
toDate(): Date;
1007-
toString(): string;
1008-
}
1009-
1010-
// @public
1011-
export interface DataAPITimestampComponents {
1012-
date: number;
1013-
hours: number;
1014-
minutes: number;
1015-
month: number;
1016-
nanoseconds: number;
1017-
seconds: number;
1018-
year: number;
1019-
}
1020-
1021994
// @public
1022995
export class DataAPIVector implements CollCodec<typeof DataAPIVector>, TableCodec<typeof DataAPIVector> {
1023996
static [$DeserializeForCollection](_: string, value: any, ctx: CollDesCtx): readonly [0, (DataAPIVector | undefined)?];
1024997
static [$DeserializeForTable](_: unknown, value: any, ctx: TableDesCtx): readonly [0, (DataAPIVector | undefined)?];
1025-
[$SerializeForCollection](ctx: CollSerCtx): readonly [0, (number[] | {
1026-
$binary: string;
1027-
} | undefined)?];
1028-
[$SerializeForTable](ctx: TableSerCtx): readonly [0, (number[] | {
1029-
$binary: string;
1030-
} | undefined)?];
998+
[$SerializeForCollection](ctx: CollSerCtx): readonly [0, any?];
999+
[$SerializeForTable](ctx: TableSerCtx): readonly [0, any?];
10311000
constructor(vector: DataAPIVectorLike, validate?: boolean);
10321001
asArray(): number[];
10331002
asBase64(): string;
@@ -1853,7 +1822,7 @@ export class TableCodecs {
18531822
// @public (undocumented)
18541823
export interface TableCodecSerDesFns {
18551824
// (undocumented)
1856-
deserialize: (key: string | undefined, val: any, ctx: TableDesCtx, definition: SomeDoc) => ReturnType<SerDesFn<any>>;
1825+
deserialize: (key: string | undefined, val: any, ctx: TableDesCtx, definition?: SomeDoc) => ReturnType<SerDesFn<any>>;
18571826
// (undocumented)
18581827
serialize: SerDesFn<TableSerCtx>;
18591828
}
@@ -2001,9 +1970,6 @@ export interface TimeoutDescriptor {
20011970
tableAdminTimeoutMs: number;
20021971
}
20031972

2004-
// @public
2005-
export const timestamp: (timestamp?: string | Date | DataAPITimestampComponents) => DataAPITimestamp;
2006-
20071973
// Warning: (ae-forgotten-export) The symbol "Merge" needs to be exported by the entry point index.d.ts
20081974
// Warning: (ae-forgotten-export) The symbol "_ToDotNotation" needs to be exported by the entry point index.d.ts
20091975
//
@@ -2038,6 +2004,15 @@ export type TypeCodec<Fns extends CodecSerDesFns> = Pick<Fns, 'deserialize'> & {
20382004
type: string;
20392005
};
20402006

2007+
// @public
2008+
export class UnexpectedDataAPIResponseError extends Error {
2009+
// @internal
2010+
constructor(message: string, rawDataAPIResponse: unknown);
2011+
readonly rawDataAPIResponse?: unknown;
2012+
// (undocumented)
2013+
static require<T>(val: T | null | undefined, message: string, rawDataAPIResponse?: unknown): T;
2014+
}
2015+
20412016
// @public
20422017
export type UpdateFilter = Record<string, any>;
20432018

etc/docs/DATATYPES.md

Lines changed: 35 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -140,25 +140,6 @@ const doc = await collection.findOne();
140140
console.log(doc.date instanceof Date); // true
141141
```
142142

143-
If you prefer to use `DataAPITimestamp`s for interop with tables, that's also allowed, though you'll need to enable a certain codec if you want to read the date back as a `DataAPITimestamp`.
144-
145-
- `DataAPITimestamp`s will still serialize to a `$date` by default, even if you don't set the necessary codec.
146-
147-
```typescript
148-
import { CollCodecs, DataAPITimestamp, timestamp } from '@datastax/astra-db-ts';
149-
150-
const collection = db.collection('my_coll', {
151-
serdes: { codecs: [CollCodecs.USE_DATA_API_TIMESTAMPS_FOR_DATES] },
152-
});
153-
154-
await collection.insertOne({
155-
date: timestamp(), // Equivalent to `new DataAPITimestamp()`
156-
});
157-
158-
const doc = await collection.findOne();
159-
console.log(doc.date instanceof DataAPITimestamp); // true
160-
```
161-
162143
### ObjectIds
163144

164145
You can use objectIds in collections using the `ObjectId` class (or the `oid` shorthand). Make sure you're importing this from `'@datastax/astra-db-ts'`, and _not_ from `'bson'`.
@@ -336,32 +317,34 @@ console.log(row.list[0]); // 'value'
336317

337318
### Dates & times
338319

339-
Due to the variety of date & time classes available through the Data API, four custom classes are provided to represent them in the client.
320+
Due to the variety of date & time classes available through the Data API, some custom classes are provided to represent them in the client.
321+
322+
Only the `timestamp` type is represented by the native JavaScript `Date` object.
340323

341324
```typescript
342-
import { date, duration, time, timestamp, ... } from '@datastax/astra-db-ts';
325+
import { date, duration, time, ...g } from '@datastax/astra-db-ts';
343326

344327
await table.insertOne({
345328
date: date(), // Equivalent to `new DataAPIDate()`
346329
time: time(), // Equivalent to `new DataAPITime()`
347-
timestamp: timestamp(), // Equivalent to `new DataAPITimestamp()`
330+
timestamp: new Date(), // Uses the native `Date` object
348331
duration: duration('P5DT30M'), // Equivalent to `new DataAPIDuration(...)`
349332
});
350333

351334
const row = await table.findOne();
352335
console.log(row.date instanceof DataAPIDate); // true
353336
console.log(row.time instanceof DataAPITime); // true
354-
console.log(row.timestamp instanceof DataAPITimestamp); // true
337+
console.log(row.timestamp instanceof Date); // true
355338
console.log(row.duration instanceof DataAPIDuration); // true
356339
```
357340

358-
You can create these classes through the constructor function, or through the respective shorthand, by providing the date/time/duration in a few different ways:
341+
You can create the custom classes through the constructor function, or through their respective shorthands, by providing the date/time/duration in a few different ways:
359342
1. As a raw string formatted as it would be stored in the database (`'1992-05-28'`, `'12:34:56'`, `'2021-09-30T12:34:56.789Z'`, `'P5DT30M'`)
360343
2. As a `Date` object (`new Date(1734070574056)`)
361-
- Durations are the exception here, as they doesn't have a direct `Date` equivalent
344+
- Durations are the exception here, as they don't have a direct `Date` equivalent
362345
3. As the `*Components` object for that respective class (e.g. `{ year: 1992, month: 5, day: 28 }`)
363346

364-
From each class, you can generally:
347+
From each custom class, you can generally:
365348
- Get the string representation of the date/time/duration using `.toString()`
366349
- Get the date/time as a `Date` object using `.toDate()`
367350
- Get the individual components of the date/time using `.components()`
@@ -498,29 +481,29 @@ If you really want to change the behavior of how a certain type is deserialized,
498481

499482
### Tables
500483

501-
| Type | Type | Shorthand | Examples |
502-
|-------------|--------------------|-------------|--------------------------------------------------------------------------------------|
503-
| `ascii` | `string` | - | `'Hello!'` |
504-
| `bigint` | `number` | - | `42` |
505-
| `blob` | `DataAPIBlob` | `blob` | `new DataAPIBlob(Buffer.from(...))`, `blob({ $binary: '<b64_str>' })` |
506-
| `boolean` | `boolean` | - | `true` |
507-
| `date` | `DataAPIDate` | `date` | `new DataAPIDate()`, `date(new Date(1734070574056))`, `date('1992-05-28')`, `date()` |
508-
| `decimal` | `BigNumber` | - | `new BigNumber(123.4567)`, `BigNumber('123456.7e-3')` |
509-
| `double` | `number` | - | `3.14`, `NaN`, `Infinity`, `-Infinity` |
510-
| `duration` | `DataAPIDuration` | `duration` | `new DataAPIDuration('3w')`, `duration('P5DT30M')` |
511-
| `float` | `number` | - | `3.14`, `NaN`, `Infinity`, `-Infinity` |
512-
| `inet`. | `InetAddress` | `inet` | `new InetAddress('::1')`, `inet('127.0.0.1')` |
513-
| `int` | `number` | - | `42` |
514-
| `list` | `Array` | - | `['value']` |
515-
| `map` | `Map` | - | `new Map([['key', 'value']])` |
516-
| `set` | `Set` | - | `new Set(['value'])` |
517-
| `smallint` | `number` | - | `42` |
518-
| `text` | `string` | - | `'Hello!'` |
519-
| `time` | `DataAPITime` | `time` | `new DataAPITime()`, `time(new Date(1734070574056))`, `time('12:34:56')`, `time()` |
520-
| `timestamp` | `DataAPITimestamp` | `timestamp` | `new DataAPITimestamp('...')`, `timestamp(new Date(1734070574056))`, `timestamp()` |
521-
| `timeuuid` | `UUID` | `timeuuid` | `new UUID('...')`, `UUID.v1()`, `uuid('...')`, `uuid(1)` |
522-
| `tinyint` | `number` | - | `42` |
523-
| `uuid` | `UUID` | `uuid` | `new UUID('...')`, `UUID.v4()`, `uuid('...')`, `uuid(7)` |
524-
| `varchar` | `string` | - | `'Hello!'` |
525-
| `varint` | `bigint` | - | `BigInt('42')`, `42n` |
526-
| `vector` | `DataAPIVector` | `vector` | `new DataAPIVector([.1, .2, .3])`, `vector([.1, .2, .3])` |
484+
| Type | Type | Shorthand | Examples |
485+
|-------------|-------------------|------------|--------------------------------------------------------------------------------------|
486+
| `ascii` | `string` | - | `'Hello!'` |
487+
| `bigint` | `number` | - | `42` |
488+
| `blob` | `DataAPIBlob` | `blob` | `new DataAPIBlob(Buffer.from(...))`, `blob({ $binary: '<b64_str>' })` |
489+
| `boolean` | `boolean` | - | `true` |
490+
| `date` | `DataAPIDate` | `date` | `new DataAPIDate()`, `date(new Date(1734070574056))`, `date('1992-05-28')`, `date()` |
491+
| `decimal` | `BigNumber` | - | `new BigNumber(123.4567)`, `BigNumber('123456.7e-3')` |
492+
| `double` | `number` | - | `3.14`, `NaN`, `Infinity`, `-Infinity` |
493+
| `duration` | `DataAPIDuration` | `duration` | `new DataAPIDuration('3w')`, `duration('P5DT30M')` |
494+
| `float` | `number` | - | `3.14`, `NaN`, `Infinity`, `-Infinity` |
495+
| `inet`. | `InetAddress` | `inet` | `new InetAddress('::1')`, `inet('127.0.0.1')` |
496+
| `int` | `number` | - | `42` |
497+
| `list` | `Array` | - | `['value']` |
498+
| `map` | `Map` | - | `new Map([['key', 'value']])` |
499+
| `set` | `Set` | - | `new Set(['value'])` |
500+
| `smallint` | `number` | - | `42` |
501+
| `text` | `string` | - | `'Hello!'` |
502+
| `time` | `DataAPITime` | `time` | `new DataAPITime()`, `time(new Date(1734070574056))`, `time('12:34:56')`, `time()` |
503+
| `timestamp` | `Date` | - | `new Date()`, `new Date(1734070574056)`, `new Date('...')` |
504+
| `timeuuid` | `UUID` | `timeuuid` | `new UUID('...')`, `UUID.v1()`, `uuid('...')`, `uuid(1)` |
505+
| `tinyint` | `number` | - | `42` |
506+
| `uuid` | `UUID` | `uuid` | `new UUID('...')`, `UUID.v4()`, `uuid('...')`, `uuid(7)` |
507+
| `varchar` | `string` | - | `'Hello!'` |
508+
| `varint` | `bigint` | - | `BigInt('42')`, `42n` |
509+
| `vector` | `DataAPIVector` | `vector` | `new DataAPIVector([.1, .2, .3])`, `vector([.1, .2, .3])` |

src/db/types/tables/table-schema.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import {
2525
DataAPIDate,
2626
DataAPIDuration,
2727
DataAPITime,
28-
DataAPITimestamp, FoundRow,
28+
FoundRow,
2929
InetAddress,
3030
SomeRow,
3131
UUID,
@@ -100,7 +100,7 @@ export type InferrableTable =
100100
* // for the table's schema
101101
* type _Proof = Equal<User, {
102102
* name: string,
103-
* dob: DataAPITimestamp,
103+
* dob: Date,
104104
* friends: Set<string>,
105105
* }>;
106106
*
@@ -109,7 +109,7 @@ export type InferrableTable =
109109
* type _ProofPK = Equal<UserPK, {
110110
* name: string,
111111
* height: TypeErr<'Field `height` not found as property in table definition'>,
112-
* dob: DataAPITimestamp,
112+
* dob: Date,
113113
* }>;
114114
*
115115
* // And now `User` can be used wherever.
@@ -267,7 +267,7 @@ interface CqlNonGenericType2TSTypeDict {
267267
smallint: number | null,
268268
text: string | null;
269269
time: DataAPITime | null,
270-
timestamp: DataAPITimestamp | null,
270+
timestamp: Date | null,
271271
tinyint: number | null,
272272
uuid: UUID | null,
273273
varchar: string | null,

src/documents/collections/ser-des/codecs.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
import { UUID } from '@/src/documents/datatypes/uuid';
1717
import { ObjectId } from '@/src/documents/datatypes/object-id';
1818
import { DataAPIVector } from '@/src/documents/datatypes/vector';
19-
import { DataAPITimestamp } from '@/src/documents/datatypes/dates';
2019
import { CollDesCtx, CollSerCtx } from '@/src/documents';
2120
import { EmptyObj, SerDesFn } from '@/src/lib';
2221
import { CodecOpts, RawCodec } from '@/src/lib/api/ser-des/codecs';
@@ -62,8 +61,6 @@ export class CollCodecs {
6261
$objectId: CollCodecs.forType('$objectId', ObjectId),
6362
};
6463

65-
public static USE_DATA_API_TIMESTAMPS_FOR_DATES = CollCodecs.forType('$date', DataAPITimestamp);
66-
6764
public static forPath(path: string[], optsOrClass: CodecOpts<CollCodecSerDesFns, CollSerCtx, CollDesCtx> | CollCodecClass): RawCodec<CollCodecSerDesFns> {
6865
return {
6966
path,

0 commit comments

Comments
 (0)