Skip to content

Commit 605d9ff

Browse files
committed
minor-ish tweaks/fixes
1 parent 68873cb commit 605d9ff

File tree

16 files changed

+82
-41
lines changed

16 files changed

+82
-41
lines changed

scripts/repl.sh

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ fi
1414
# Rebuild the client (without types or any extra processing for speed)
1515
sh scripts/build.sh -light || exit 2
1616

17+
if [ "$1" = "-stargate" ]; then
18+
export CLIENT_DB_ENVIRONMENT='dse'
19+
export CLIENT_DB_TOKEN='Cassandra:Y2Fzc2FuZHJh:Y2Fzc2FuZHJh'
20+
export CLIENT_DB_URL='http://localhost:8181'
21+
fi
22+
1723
# Start the REPL w/ some utility stuff and stuff
1824
node -i -e "
1925
require('./node_modules/dotenv/config');
@@ -23,8 +29,18 @@ node -i -e "
2329
2430
let client = new $.DataAPIClient(process.env.CLIENT_DB_TOKEN, { environment: process.env.CLIENT_DB_ENVIRONMENT });
2531
let db = client.db(process.env.CLIENT_DB_URL);
26-
let admin = client.admin();
2732
let dbAdmin = db.admin({ environment: process.env.CLIENT_DB_ENVIRONMENT });
33+
34+
const isAstra = process.env.CLIENT_DB_ENVIRONMENT === 'astra';
35+
36+
if (!isAstra) {
37+
dbAdmin.createKeyspace('default_keyspace', { updateDbKeyspace: true });
38+
}
39+
40+
let admin = (isAstra)
41+
? client.admin()
42+
: null;
43+
2844
let coll = db.collection('test_coll');
2945
let table = db.table('test_table');
3046

src/db/db.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,9 @@ import { AstraDbInfo } from '@/src/administration/types/admin/database-info';
116116
export class Db {
117117
readonly #defaultOpts: InternalRootClientOpts;
118118
readonly #httpClient: DataAPIHttpClient;
119-
readonly #endpoint: string;
120119

121-
readonly _keyspace: KeyspaceRef;
120+
readonly #endpoint: string;
121+
readonly #keyspace: KeyspaceRef;
122122
readonly #id?: string;
123123
readonly #region?: string;
124124

@@ -161,7 +161,7 @@ export class Db {
161161
},
162162
};
163163

164-
this._keyspace = {
164+
this.#keyspace = {
165165
ref: (rootOpts.environment === 'astra')
166166
? this.#defaultOpts.dbOptions.keyspace ?? DEFAULT_KEYSPACE
167167
: this.#defaultOpts.dbOptions.keyspace ?? undefined,
@@ -175,7 +175,7 @@ export class Db {
175175
emitter: rootOpts.emitter,
176176
logging: this.#defaultOpts.dbOptions.logging,
177177
fetchCtx: rootOpts.fetchCtx,
178-
keyspace: this._keyspace,
178+
keyspace: this.#keyspace,
179179
userAgent: rootOpts.userAgent,
180180
emissionStrategy: EmissionStrategy.Normal,
181181
additionalHeaders: this.#defaultOpts.dbOptions.additionalHeaders,
@@ -221,10 +221,10 @@ export class Db {
221221
* ```
222222
*/
223223
public get keyspace(): string {
224-
if (!this._keyspace.ref) {
224+
if (!this.#keyspace.ref) {
225225
throw new Error('No keyspace set for DB (can\'t do db.keyspace, or perform any operation requiring it). Use `db.useKeyspace`, or pass the keyspace as an option parameter explicitly.');
226226
}
227-
return this._keyspace.ref;
227+
return this.#keyspace.ref;
228228
}
229229

230230
/**
@@ -312,7 +312,7 @@ export class Db {
312312
* @param keyspace - The keyspace to use
313313
*/
314314
public useKeyspace(keyspace: string) {
315-
this._keyspace.ref = keyspace;
315+
this.#keyspace.ref = keyspace;
316316
}
317317

318318
/**

src/documents/datatypes/vector.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ export type DataAPIVectorLike = number[] | string | Float32Array | DataAPIVector
2121
export class DataAPIVector {
2222
readonly #vector: Exclude<DataAPIVectorLike, DataAPIVector>;
2323

24-
public [$SerializeForTable] = () => serialize(this.#vector);
25-
public [$SerializeForCollection] = this[$SerializeForTable];
24+
public [$SerializeForTable]: () => { $binary: string } | number[];
25+
public [$SerializeForCollection]: () => { $binary: string } | number[];
2626

2727
public constructor(vector: DataAPIVectorLike, validate = true) {
2828
if (validate && !DataAPIVector.isVectorLike(vector)) {
@@ -33,6 +33,9 @@ export class DataAPIVector {
3333
? vector.raw()
3434
: vector;
3535

36+
this[$SerializeForTable] = () => serialize(this.#vector);
37+
this[$SerializeForCollection] = this[$SerializeForTable];
38+
3639
Object.defineProperty(this, $CustomInspect, {
3740
value: this.toString,
3841
});

src/documents/tables/ser-des.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,11 @@ const DefaultTableSerDesCfg = {
120120
return false;
121121
}
122122

123-
deserializeObj(ctx, ctx.rootObj, key, ctx.tableSchema[key]);
123+
const schema = ctx.tableSchema[key];
124+
125+
if (schema) {
126+
deserializeObj(ctx, ctx.rootObj, key, schema);
127+
}
124128

125129
if (!ctx.sparseData) {
126130
for (const key in ctx.tableSchema) {

src/documents/tables/table.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -356,10 +356,6 @@ export class Table<Schema extends SomeRow = SomeRow> {
356356
return this.#commands.findOne(filter, options);
357357
}
358358

359-
public async countRows(filter: Filter<Schema>, upperBound: number, options?: WithTimeout): Promise<number> {
360-
return this.#commands.countDocuments(filter, upperBound, options, TooManyRowsToCountError);
361-
}
362-
363359
public async drop(options?: WithTimeout): Promise<void> {
364360
await this.#db.dropCollection(this.name, { keyspace: this.keyspace, ...options });
365361
}

src/documents/types/common.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
// limitations under the License.
1414

1515
import type { SomeDoc, WithId } from '@/src/documents/collections';
16-
import type { ToDotNotation } from '@/src/documents';
16+
import { DataAPIVector, ToDotNotation } from '@/src/documents';
1717

1818
/**
1919
* Allowed types to specify an ascending or descending sort.
@@ -52,10 +52,7 @@ export type SortDirection = 1 | -1 | 'asc' | 'desc' | 'ascending' | 'descending'
5252
*
5353
* @public
5454
*/
55-
export type Sort =
56-
| Record<string, SortDirection>
57-
| { $vector: number[] }
58-
| { $vectorize: string };
55+
export type Sort = Record<string, SortDirection | number[] | DataAPIVector>;
5956

6057
/**
6158
* Specifies which fields should be included/excluded in the returned documents.

src/documents/utils.ts

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

1515
import { SomeDoc } from '@/src/documents/collections';
1616
import { Sort } from '@/src/documents/types';
17+
import { DataAPIVector } from '@/src/documents/datatypes';
18+
import { $SerializeForTable } from '@/src/documents/tables';
1719

1820
declare const $ERROR: unique symbol;
1921

@@ -69,14 +71,18 @@ export const normalizedSort = (sort: SomeDoc): Sort => {
6971
const ret: Sort = {};
7072

7173
for (const key in sort) {
72-
if (typeof sort[key] === 'string') {
73-
if (sort[key][0] === 'a') {
74+
const val = sort[key];
75+
76+
if (typeof val === 'string') {
77+
if (val[0] === 'a') {
7478
ret[key] = 1;
75-
} else if (sort[key][0] === 'd') {
79+
} else if (val[0] === 'd') {
7680
ret[key] = -1;
7781
}
82+
} if (val instanceof DataAPIVector) {
83+
ret[key] = val[$SerializeForTable]() as Sort[string];
7884
} else {
79-
ret[key] = sort[key];
85+
ret[key] = val;
8086
}
8187
}
8288

tests/integration/db/db.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ parallel('integration.db', { dropEphemeral: 'colls:after' }, ({ db }) => {
3838
});
3939

4040
it('should create a collections in another keyspace', async () => {
41-
const res = await db.createCollection('coll_2c', { keyspace: OTHER_KEYSPACE });
41+
const res = await db.createCollection('coll_2c', { keyspace: OTHER_KEYSPACE, indexing: { deny: ['*'] } });
4242
assert.ok(res);
4343
assert.strictEqual(res.name, 'coll_2c');
4444
assert.strictEqual(res.keyspace, OTHER_KEYSPACE);
@@ -65,12 +65,12 @@ parallel('integration.db', { dropEphemeral: 'colls:after' }, ({ db }) => {
6565
});
6666

6767
it('should fail creating collections with different options', async () => {
68-
const res = await db.createCollection('coll_6c', { indexing: { deny: ['*'] } });
68+
const res = await db.createCollection('coll_6c', { indexing: { deny: ['*'] }, defaultId: { type: 'uuid' } });
6969
assert.ok(res);
7070
assert.strictEqual(res.name, 'coll_6c');
7171
assert.strictEqual(res.keyspace, DEFAULT_KEYSPACE);
7272
try {
73-
await db.createCollection('coll_6c', { indexing: { allow: ['*'] } });
73+
await db.createCollection('coll_6c', { indexing: { deny: ['*'] }, defaultId: { type: 'uuidv6' } });
7474
assert.fail('Expected an error');
7575
} catch (e) {
7676
assert.ok(e instanceof DataAPIResponseError);

tests/integration/documents/collections/count-documents.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ parallel('integration.documents.collections.count-documents', { truncate: 'colls
2626

2727
before(async () => {
2828
await collection.insertMany(docs);
29-
await collection_.insertMany(Array.from({ length: 1001 }, () => ({})));
29+
await collection_.insertMany(Array.from({ length: 1001 }, () => ({})), { ordered: true, chunkSize: 100 });
3030
});
3131

3232
it('should return a single doc for an _id filter', async () => {

tests/integration/documents/collections/find-one-and-delete.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ parallel('integration.documents.collections.find-one-and-delete', { truncate: 'c
8383

8484
it('should findOneAndDelete with $vector sort', async (key) => {
8585
await collection.insertMany([
86-
{ name: 'a', $vector: [1.0, 1.0, 1.0, 1.0, 1.0], key },
87-
{ name: 'c', $vector: [-.1, -.2, -.3, -.4, -.5], key },
86+
{ name: 'a', $vector: [1.0, 1.0, 0.9, 1.0, 1.0], key },
87+
{ name: 'c', $vector: [-.4, -.2, -.3, -.4, -.1], key },
8888
{ name: 'b', $vector: [-.1, -.2, -.3, -.4, -.5], key },
8989
]);
9090

0 commit comments

Comments
 (0)