Skip to content

Commit c11ff73

Browse files
committed
Improved types [skip ci]
1 parent 59a44a3 commit c11ff73

8 files changed

Lines changed: 61 additions & 12 deletions

File tree

src/sparse-vector.js

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,36 @@ export class SparseVector {
1010
/** @type {number[]} */
1111
values = [];
1212

13+
/**
14+
* @overload
15+
* @param {string | number[]} value
16+
*
17+
* @overload
18+
* @param {Map<number, number>} value
19+
* @param {number} dimensions
20+
*/
1321
constructor(value, dimensions) {
14-
if (typeof value === 'string') {
15-
this.#fromSql(value);
16-
} else if (dimensions !== undefined) {
22+
if (dimensions !== undefined) {
1723
this.#fromMap(value, dimensions);
24+
} else if (typeof value === 'string') {
25+
this.#fromSql(value);
1826
} else {
1927
this.#fromDense(value);
2028
}
2129
}
2230

31+
/**
32+
* @returns {string}
33+
*/
2334
toPostgres() {
2435
const values = this.values;
2536
const elements = this.indices.map((index, i) => format('%i:%f', index + 1, values[i])).join(',');
2637
return format('{%s}/%d', elements, this.dimensions);
2738
}
2839

40+
/**
41+
* @returns {string}
42+
*/
2943
toString() {
3044
return this.toPostgres();
3145
}
@@ -41,6 +55,9 @@ export class SparseVector {
4155
return arr;
4256
}
4357

58+
/**
59+
* @param {string} value
60+
*/
4461
#fromSql(value) {
4562
const parts = value.split('/', 2);
4663

@@ -54,6 +71,9 @@ export class SparseVector {
5471
}
5572
}
5673

74+
/**
75+
* @param {number[]} value
76+
*/
5777
#fromDense(value) {
5878
this.dimensions = value.length;
5979

@@ -66,6 +86,10 @@ export class SparseVector {
6686
}
6787
}
6888

89+
/**
90+
* @param {Map<number, number>} map
91+
* @param {number} dimensions
92+
*/
6993
#fromMap(map, dimensions) {
7094
this.dimensions = Number(dimensions);
7195

src/utils.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ export function vectorToSql(value) {
1818
export { vectorFromSql as halfvecFromSql };
1919
export { vectorToSql as halfvecToSql };
2020

21+
/**
22+
* @returns {?SparseVector}
23+
*/
2124
export function sparsevecFromSql(value) {
2225
if (value === null) {
2326
return null;
@@ -32,6 +35,11 @@ export function sparsevecToSql(value) {
3235
return value;
3336
}
3437

38+
/**
39+
* @param {string} name
40+
* @param {number | null | undefined} dimensions
41+
* @returns {string}
42+
*/
3543
function typeWithDimensions(name, dimensions) {
3644
if (dimensions === undefined || dimensions === null) {
3745
return name;
@@ -44,18 +52,34 @@ function typeWithDimensions(name, dimensions) {
4452
return format('%s(%d)', name, dimensions);
4553
}
4654

55+
/**
56+
* @param {number | null | undefined} dimensions
57+
* @returns {string}
58+
*/
4759
export function vectorType(dimensions) {
4860
return typeWithDimensions('vector', dimensions);
4961
}
5062

63+
/**
64+
* @param {number | null | undefined} dimensions
65+
* @returns {string}
66+
*/
5167
export function halfvecType(dimensions) {
5268
return typeWithDimensions('halfvec', dimensions);
5369
}
5470

71+
/**
72+
* @param {number | null | undefined} dimensions
73+
* @returns {string}
74+
*/
5575
export function bitType(dimensions) {
5676
return typeWithDimensions('bit', dimensions);
5777
}
5878

79+
/**
80+
* @param {number | null | undefined} dimensions
81+
* @returns {string}
82+
*/
5983
export function sparsevecType(dimensions) {
6084
return typeWithDimensions('sparsevec', dimensions);
6185
}

types/mikro-orm/bit.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
export class BitType extends Type<string, string> {
22
constructor();
3-
getColumnType(prop: any, platform: any): any;
3+
getColumnType(prop: any, platform: any): string;
44
}
55
import { Type } from '@mikro-orm/core';

types/mikro-orm/halfvec.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ export class HalfvecType extends Type<string, string> {
22
constructor();
33
convertToDatabaseValue(value: any, platform: any): any;
44
convertToJSValue(value: any, platform: any): any;
5-
getColumnType(prop: any, platform: any): any;
5+
getColumnType(prop: any, platform: any): string;
66
}
77
import { Type } from '@mikro-orm/core';

types/mikro-orm/sparsevec.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ export class SparsevecType extends Type<string, string> {
22
constructor();
33
convertToDatabaseValue(value: any, platform: any): any;
44
convertToJSValue(value: any, platform: any): import("../sparse-vector.js").SparseVector | null;
5-
getColumnType(prop: any, platform: any): any;
5+
getColumnType(prop: any, platform: any): string;
66
}
77
import { Type } from '@mikro-orm/core';

types/mikro-orm/vector.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ export class VectorType extends Type<string, string> {
22
constructor();
33
convertToDatabaseValue(value: any, platform: any): any;
44
convertToJSValue(value: any, platform: any): any;
5-
getColumnType(prop: any, platform: any): any;
5+
getColumnType(prop: any, platform: any): string;
66
}
77
import { Type } from '@mikro-orm/core';

types/sparse-vector.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export class SparseVector {
2-
constructor(value: any, dimensions: any);
2+
constructor(value: string | number[]);
3+
constructor(value: Map<number, number>, dimensions: number);
34
dimensions: number;
45
indices: number[];
56
values: number[];

types/utils.d.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ export function vectorFromSql(value: any): any;
22
export function vectorToSql(value: any): any;
33
export function sparsevecFromSql(value: any): SparseVector | null;
44
export function sparsevecToSql(value: any): any;
5-
export function vectorType(dimensions: any): any;
6-
export function halfvecType(dimensions: any): any;
7-
export function bitType(dimensions: any): any;
8-
export function sparsevecType(dimensions: any): any;
5+
export function vectorType(dimensions: number | null | undefined): string;
6+
export function halfvecType(dimensions: number | null | undefined): string;
7+
export function bitType(dimensions: number | null | undefined): string;
8+
export function sparsevecType(dimensions: number | null | undefined): string;
99
import { SparseVector } from './sparse-vector.js';
1010
export { vectorFromSql as halfvecFromSql, vectorToSql as halfvecToSql };

0 commit comments

Comments
 (0)