Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove typebox Static<> type inference in favor of TypeScript #107

Merged
merged 2 commits into from
Sep 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions src/control/configureIndex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { buildValidator } from '../validator';
import type { IndexName } from './types';
import { handleIndexRequestError } from './utils';

import { Static, Type } from '@sinclair/typebox';
import { Type } from '@sinclair/typebox';
import { ReplicasSchema, PodTypeSchema, IndexNameSchema } from './types';

const ConfigureIndexOptionsSchema = Type.Object(
Expand All @@ -14,8 +14,7 @@ const ConfigureIndexOptionsSchema = Type.Object(
},
{ additionalProperties: false }
);

export type ConfigureIndexOptions = Static<typeof ConfigureIndexOptionsSchema>;
export type ConfigureIndexOptions = { replicas?: number; podType?: string };

export const configureIndex = (api: IndexOperationsApi) => {
const indexNameValidator = buildValidator(
Expand Down
10 changes: 6 additions & 4 deletions src/control/createCollection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { IndexOperationsApi } from '../pinecone-generated-ts-fetch';
import { buildConfigValidator } from '../validator';
import { handleIndexRequestError } from './utils';
import { CollectionNameSchema, IndexNameSchema } from './types';
import { Static, Type } from '@sinclair/typebox';
import type { CollectionName, IndexName } from './types';
import { Type } from '@sinclair/typebox';

const CreateCollectionOptionsSchema = Type.Object(
{
Expand All @@ -12,9 +13,10 @@ const CreateCollectionOptionsSchema = Type.Object(
{ additionalProperties: false }
);

export type CreateCollectionOptions = Static<
typeof CreateCollectionOptionsSchema
>;
export type CreateCollectionOptions = {
name: CollectionName;
source: IndexName;
};

export const createCollection = (api: IndexOperationsApi) => {
const validator = buildConfigValidator(
Expand Down
15 changes: 13 additions & 2 deletions src/control/createIndex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { buildConfigValidator } from '../validator';
import { debugLog } from '../utils';
import { handleApiError } from '../errors';
import { handleIndexRequestError } from './utils';
import { Static, Type } from '@sinclair/typebox';
import { Type } from '@sinclair/typebox';
import {
IndexNameSchema,
DimensionSchema,
Expand All @@ -14,8 +14,19 @@ import {
MetadataConfigSchema,
CollectionNameSchema,
} from './types';
import type { IndexName } from './types';

export type CreateIndexOptions = Static<typeof CreateIndexOptionsSchema>;
export type CreateIndexOptions = {
name: IndexName;
dimension: number;
metric?: string;
pods?: number;
replicas?: number;
podType?: string;
metadataConfig?: { indexed: Array<string> };
sourceCollection?: string;
waitUntilReady?: boolean;
};

const CreateIndexOptionsSchema = Type.Object(
{
Expand Down
6 changes: 3 additions & 3 deletions src/control/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Type, Static } from '@sinclair/typebox';
import { Type } from '@sinclair/typebox';

const nonemptyString = Type.String({ minLength: 1 });
const positiveInteger = Type.Integer({ minimum: 1 });
Expand All @@ -9,7 +9,7 @@ const positiveInteger = Type.Integer({ minimum: 1 });
// no descriptive information is returned for an index named empty
// string. To avoid this confusing case, we require lenth > 1.
export const IndexNameSchema = nonemptyString;
export type IndexName = Static<typeof IndexNameSchema>;
export type IndexName = string;

export const PodTypeSchema = nonemptyString;
export const ReplicasSchema = positiveInteger;
Expand All @@ -29,4 +29,4 @@ export const MetadataConfigSchema = Type.Object(
// no descriptive information is returned for an collection named empty
// string. To avoid this confusing case, we require lenth > 1.
export const CollectionNameSchema = nonemptyString;
export type CollectionName = Static<typeof CollectionNameSchema>;
export type CollectionName = string;
13 changes: 7 additions & 6 deletions src/data/deleteMany.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { VectorOperationsProvider } from './vectorOperationsProvider';
import { handleApiError } from '../errors';
import { buildConfigValidator } from '../validator';
import { Static, Type } from '@sinclair/typebox';
import { Type } from '@sinclair/typebox';
import type { DeleteRequest } from '../pinecone-generated-ts-fetch/models/DeleteRequest';
import { RecordIdSchema } from './types';
import type { RecordId } from './types';

const DeleteManyByRecordIdSchema = Type.Array(RecordIdSchema);

Expand All @@ -17,11 +18,11 @@ const DeleteManySchema = Type.Union([
DeleteManyByFilterSchema,
]);

export type DeleteManyByVectorIdOptions = Static<
typeof DeleteManyByRecordIdSchema
>;
export type DeleteManyByFilterOptions = Static<typeof DeleteManyByFilterSchema>;
export type DeleteManyOptions = Static<typeof DeleteManySchema>;
export type DeleteManyByVectorIdOptions = Array<RecordId>;
export type DeleteManyByFilterOptions = object;
export type DeleteManyOptions =
| DeleteManyByVectorIdOptions
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've never seen the | used on the first entry like this. Is that correct/valid syntax?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was a prettier addition, but yeah seems like valid syntax. I did have to look it up though, found this: prettier/prettier#6299

| DeleteManyByFilterOptions;

export const deleteMany = (
apiProvider: VectorOperationsProvider,
Expand Down
7 changes: 2 additions & 5 deletions src/data/describeIndexStats.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { handleApiError } from '../errors';
import { buildConfigValidator } from '../validator';
import { Static, Type } from '@sinclair/typebox';
import { Type } from '@sinclair/typebox';
import { VectorOperationsProvider } from './vectorOperationsProvider';

export type IndexStatsNamespaceSummary = {
Expand All @@ -20,10 +20,7 @@ const DescribeIndexStatsOptionsSchema = Type.Object(
},
{ additionalProperties: false }
);

export type DescribeIndexStatsOptions = Static<
typeof DescribeIndexStatsOptionsSchema
>;
export type DescribeIndexStatsOptions = { filter: object };

export const describeIndexStats = (apiProvider: VectorOperationsProvider) => {
const validator = buildConfigValidator(
Expand Down
15 changes: 11 additions & 4 deletions src/data/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ import { buildConfigValidator } from '../validator';
import {
RecordIdSchema,
RecordSparseValuesSchema,
RecordValues,
RecordValuesSchema,
} from './types';
import type { PineconeRecord, RecordMetadata } from './types';
import { Static, Type } from '@sinclair/typebox';
import { Type } from '@sinclair/typebox';
import { VectorOperationsProvider } from './vectorOperationsProvider';

const shared = {
Expand Down Expand Up @@ -38,9 +39,15 @@ const QueryByVectorValues = Type.Object(

const QuerySchema = Type.Union([QueryByRecordId, QueryByVectorValues]);

export type QueryByRecordId = Static<typeof QueryByRecordId>;
export type QueryByVectorValues = Static<typeof QueryByVectorValues>;
export type QueryOptions = Static<typeof QuerySchema>;
type QueryShared = {
topK: number;
includeValues?: boolean;
includeMetadata?: boolean;
filter?: object;
};
export type QueryByRecordId = QueryShared & { id: string };
export type QueryByVectorValues = QueryShared & { vector: RecordValues };
export type QueryOptions = QueryByRecordId | QueryByVectorValues;

export interface ScoredPineconeRecord<T extends RecordMetadata = RecordMetadata>
extends PineconeRecord<T> {
Expand Down
17 changes: 12 additions & 5 deletions src/data/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Static, Type } from '@sinclair/typebox';
import { Type } from '@sinclair/typebox';

export const PineconeConfigurationSchema = Type.Object(
{
Expand All @@ -8,7 +8,11 @@ export const PineconeConfigurationSchema = Type.Object(
},
{ additionalProperties: false }
);
export type PineconeConfiguration = Static<typeof PineconeConfigurationSchema>;
export type PineconeConfiguration = {
environment: string;
apiKey: string;
projectId?: string;
};

export const RecordIdSchema = Type.String({ minLength: 1 });
export const RecordValuesSchema = Type.Array(Type.Number());
Expand All @@ -29,9 +33,12 @@ export const PineconeRecordSchema = Type.Object(
{ additionalProperties: false }
);

export type RecordId = Static<typeof RecordIdSchema>;
export type RecordValues = Static<typeof RecordValuesSchema>;
export type RecordSparseValues = Static<typeof RecordSparseValuesSchema>;
export type RecordId = string;
export type RecordValues = Array<number>;
export type RecordSparseValues = {
indices: Array<number>;
values: Array<number>;
};
export type RecordMetadataValue = string | boolean | number | Array<string>;
export type RecordMetadata = Record<string, RecordMetadataValue>;
export type PineconeRecord<T extends RecordMetadata = RecordMetadata> = {
Expand Down