Skip to content

Commit f0da2f9

Browse files
authored
Updated SomeId to allow null (#35)
Updated `SomeId` to allow `null` as a valid `_id` because, well, it's technically allowed since it's a JSON scalar.
1 parent 9b32edb commit f0da2f9

File tree

6 files changed

+29
-15
lines changed

6 files changed

+29
-15
lines changed

etc/astra-db-ts.api.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -962,7 +962,7 @@ export interface RunCommandOptions extends WithNamespace, WithTimeout {
962962
export type SomeDoc = Record<string, any>;
963963

964964
// @public
965-
export type SomeId = string | number | bigint | boolean | Date | UUID | ObjectId;
965+
export type SomeId = string | number | bigint | boolean | Date | UUID | ObjectId | null;
966966

967967
// @public
968968
export type Sort = Record<string, SortDirection> | {

src/data-api/ids.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,21 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
import { uuidv4, uuidv7, UUID as UUIDv7 } from 'uuidv7';
15+
import { UUID as UUIDv7, uuidv4, uuidv7 } from 'uuidv7';
1616
import MongoObjectId from 'bson-objectid';
1717

18+
/**
19+
* All possible types for a document ID. JSON scalar types, `Date`, `UUID`, and `ObjectId`.
20+
*
21+
* Note that the `_id` *can* technically be `null`. Trying to set the `_id` to `null` doesn't mean "auto-generate
22+
* an ID" like it may in some other databases; it quite literally means "set the ID to `null`".
23+
*
24+
* It's heavily recommended to properly type this in your Schema, so you know what to expect for your `_id` field.
25+
*
26+
* @public
27+
*/
28+
export type SomeId = string | number | bigint | boolean | Date | UUID | ObjectId | null;
29+
1830
const uuidRegex = new RegExp('^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$');
1931

2032
/**

src/data-api/types/common.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,9 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
import { ObjectId, SomeDoc, UUID, WithId } from '@/src/data-api';
15+
import { SomeDoc, WithId } from '@/src/data-api';
1616
import type { ToDotNotation } from '@/src/data-api/types';
1717

18-
/**
19-
* All possible types for a document ID. JSON scalar types, `Date`, `UUID`, and `ObjectId`.
20-
*
21-
* @public
22-
*/
23-
export type SomeId = string | number | bigint | boolean | Date | UUID | ObjectId;
24-
2518
/**
2619
* Allowed types to specify an ascending or descending sort.
2720
*

src/data-api/types/utils.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
import type { SomeId } from '@/src/data-api/types';
15+
16+
import { SomeId } from '@/src/data-api';
1617

1718
/**
1819
* Checks if a type can possibly be some number

tests/integration/data-api/collection/insert-one.test.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,26 @@ describe('integration.data-api.collection.insert-one', () => {
3737
assert.ok(res.insertedId, '123');
3838
});
3939

40+
it('should insertOne document with a null id', async () => {
41+
const res = await collection.insertOne({ name: 'Lzzy', _id: null });
42+
assert.ok(res);
43+
assert.strictEqual(res.insertedId, null);
44+
const found = await collection.findOne({ _id: null });
45+
assert.strictEqual(found?.name, 'Lzzy');
46+
});
47+
4048
it('should insertOne document with a UUID', async () => {
4149
const id = UUID.v7();
4250
const res = await collection.insertOne({ _id: id });
4351
assert.ok(res);
44-
assert.strictEqual(res.insertedId.toString(), id.toString());
52+
assert.strictEqual(res.insertedId?.toString(), id.toString());
4553
});
4654

4755
it('should insertOne document with an ObjectId', async () => {
4856
const id = new ObjectId();
4957
const res = await collection.insertOne({ _id: id });
5058
assert.ok(res);
51-
assert.strictEqual(res.insertedId.toString(), id.toString());
59+
assert.strictEqual(res.insertedId?.toString(), id.toString());
5260
});
5361

5462
it('should insertOne document with a non-_id UUID', async () => {

tests/typing/strict-filter.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414

1515
/* eslint-disable @typescript-eslint/no-unused-vars */
1616

17-
import type { SomeDoc } from '@/src/data-api';
18-
import type { SomeId, StrictFilter } from '@/src/data-api/types';
17+
import type { SomeDoc, SomeId } from '@/src/data-api';
18+
import type { StrictFilter } from '@/src/data-api/types';
1919
import type { ConvolutedSchema2, Equal, Expect, Schema, SuperBasicSchema } from '@/tests/typing/prelude';
2020

2121
type test1 = Expect<Equal<StrictFilter<SuperBasicSchema>, {

0 commit comments

Comments
 (0)