From ce72e933985f69662dd864181f1e27630427f193 Mon Sep 17 00:00:00 2001 From: tomas Date: Sun, 8 Sep 2024 15:09:27 -0400 Subject: [PATCH] feat: add a sizable equivalent type --- src/interfaces.ts | 3 +++ src/schema/schema.ts | 8 ++++++++ src/tests/custom-types.spec.ts | 12 ++++++++++++ 3 files changed, 23 insertions(+) diff --git a/src/interfaces.ts b/src/interfaces.ts index bad80e1d..9c89d46f 100644 --- a/src/interfaces.ts +++ b/src/interfaces.ts @@ -342,6 +342,9 @@ export interface ISchema { /** Register a simple type, which is equivalent to another */ registerEquivalentType(type: IEquivalentType): IType; + /** Register a simple type, which is equivalent to another */ + registerEquivalentSizableType(type: IEquivalentType): IType; + /** Get an existing type */ getType(name: DataType): IType; diff --git a/src/schema/schema.ts b/src/schema/schema.ts index b8d59407..94418234 100644 --- a/src/schema/schema.ts +++ b/src/schema/schema.ts @@ -394,6 +394,14 @@ export class DbSchema implements _ISchema, ISchema { registerEquivalentType(type: IEquivalentType): IType { const ret = new EquivalentType(type); this._registerType(ret); + + return ret; + } + + registerEquivalentSizableType(type: IEquivalentType): IType { + const ret = new EquivalentType(type); + this._registerTypeSizeable(ret.primary, (_) => ret); + return ret; } diff --git a/src/tests/custom-types.spec.ts b/src/tests/custom-types.spec.ts index b47e54b6..9afbb9d6 100644 --- a/src/tests/custom-types.spec.ts +++ b/src/tests/custom-types.spec.ts @@ -96,5 +96,17 @@ describe('Custom types', () => { expectQueryError(() => none(`SELECT 'throw'::custom`), /Nope/); expectQueryError(() => none(`SELECT 'whatever'::custom`), /invalid input syntax for type custom/); expectQueryError(() => none(`SELECT 42::custom`), /cannot cast type integer to custom/); + }); + + it('can register custom type with length', () => { + db.public.registerEquivalentSizableType({ + name: 'vector', + equivalentTo: DataType.text, + isValid(val: string) { + return true; + } + }); + + none(`CREATE TABLE "test" ("embedding" vector(1536) NOT NULL)`); }) });