diff --git a/src/server/templates/python.ts b/src/server/templates/python.ts index 0d00f475..bffbeb28 100644 --- a/src/server/templates/python.ts +++ b/src/server/templates/python.ts @@ -198,6 +198,20 @@ class PythonEnum implements Serializable { } } +class PythonDomain implements Serializable { + name: string + py_type: PythonType + + constructor(name: string, schema: string, py_type: PythonType) { + this.name = `${formatForPyClassName(schema)}${formatForPyClassName(name)}` + this.py_type = py_type + } + + serialize(): string { + return `${this.name}: TypeAlias = ${this.py_type.serialize()}` + } +} + type PythonType = PythonListType | PythonSimpleType class PythonSimpleType implements Serializable { @@ -327,6 +341,7 @@ const PY_TYPE_MAP: Record = { bool: 'bool', // Numbers + int: 'int', int2: 'int', int4: 'int', int8: 'int', diff --git a/src/server/templates/typescript.ts b/src/server/templates/typescript.ts index 352c4ddc..2913c54b 100644 --- a/src/server/templates/typescript.ts +++ b/src/server/templates/typescript.ts @@ -719,17 +719,14 @@ export type Database = { const type = typesById.get(type_id) let tsType = 'unknown' if (type) { - tsType = `${generateNullableUnionTsType( - pgTypeToTsType(schema, type.name, { - types, - schemas, - tables, - views, - }), - true - )}` + tsType = pgTypeToTsType(schema, type.name, { + types, + schemas, + tables, + views, + }) } - return `${JSON.stringify(name)}: ${tsType}` + return `${JSON.stringify(name)}: ${tsType} | null` })} }` ) diff --git a/test/db/00-init.sql b/test/db/00-init.sql index c30e1f4a..2bb808f5 100644 --- a/test/db/00-init.sql +++ b/test/db/00-init.sql @@ -500,4 +500,16 @@ LANGUAGE SQL STABLE AS $$ SELECT interval_test_row.duration_required * 2; -$$; \ No newline at end of file +$$; + +CREATE DOMAIN public.one_to_ten AS int CHECK (VALUE >= 1 AND VALUE <= 10); + +CREATE TYPE composite_type_with_domain_attribute AS ( + name text, + score public.one_to_ten +); + +CREATE TYPE composite_type_with_int_attribute AS ( + a int, + b int +); \ No newline at end of file diff --git a/test/server/typegen.ts b/test/server/typegen.ts index 50a0896b..31158226 100644 --- a/test/server/typegen.ts +++ b/test/server/typegen.ts @@ -1060,6 +1060,14 @@ test('typegen: typescript', async () => { composite_type_with_array_attribute: { my_text_array: string[] | null } + composite_type_with_domain_attribute: { + name: string | null + score: unknown | null + } + composite_type_with_int_attribute: { + a: number | null + b: number | null + } composite_type_with_record_attribute: { todo: Database["public"]["Tables"]["todos"]["Row"] | null } @@ -2285,6 +2293,14 @@ test('typegen w/ one-to-one relationships', async () => { composite_type_with_array_attribute: { my_text_array: string[] | null } + composite_type_with_domain_attribute: { + name: string | null + score: unknown | null + } + composite_type_with_int_attribute: { + a: number | null + b: number | null + } composite_type_with_record_attribute: { todo: Database["public"]["Tables"]["todos"]["Row"] | null } @@ -3510,6 +3526,14 @@ test('typegen: typescript w/ one-to-one relationships', async () => { composite_type_with_array_attribute: { my_text_array: string[] | null } + composite_type_with_domain_attribute: { + name: string | null + score: unknown | null + } + composite_type_with_int_attribute: { + a: number | null + b: number | null + } composite_type_with_record_attribute: { todo: Database["public"]["Tables"]["todos"]["Row"] | null } @@ -4740,6 +4764,14 @@ test('typegen: typescript w/ postgrestVersion', async () => { composite_type_with_array_attribute: { my_text_array: string[] | null } + composite_type_with_domain_attribute: { + name: string | null + score: unknown | null + } + composite_type_with_int_attribute: { + a: number | null + b: number | null + } composite_type_with_record_attribute: { todo: Database["public"]["Tables"]["todos"]["Row"] | null } @@ -5556,6 +5588,16 @@ test('typegen: go', async () => { type PublicCompositeTypeWithRecordAttribute struct { Todo interface{} \`json:"todo"\` + } + + type PublicCompositeTypeWithDomainAttribute struct { + Name string \`json:"name"\` + Score interface{} \`json:"score"\` + } + + type PublicCompositeTypeWithIntAttribute struct { + A interface{} \`json:"a"\` + B interface{} \`json:"b"\` }" `) }) @@ -6077,6 +6119,22 @@ test('typegen: swift', async () => { case MyTextArray = "my_text_array" } } + internal struct CompositeTypeWithDomainAttribute: Codable, Hashable, Sendable { + internal let Name: String + internal let Score: OneToTenSelect + internal enum CodingKeys: String, CodingKey { + case Name = "name" + case Score = "score" + } + } + internal struct CompositeTypeWithIntAttribute: Codable, Hashable, Sendable { + internal let A: AnyJSON + internal let B: AnyJSON + internal enum CodingKeys: String, CodingKey { + case A = "a" + case B = "b" + } + } internal struct CompositeTypeWithRecordAttribute: Codable, Hashable, Sendable { internal let Todo: TodosSelect internal enum CodingKeys: String, CodingKey { @@ -6608,6 +6666,22 @@ test('typegen: swift w/ public access control', async () => { case MyTextArray = "my_text_array" } } + public struct CompositeTypeWithDomainAttribute: Codable, Hashable, Sendable { + public let Name: String + public let Score: OneToTenSelect + public enum CodingKeys: String, CodingKey { + case Name = "name" + case Score = "score" + } + } + public struct CompositeTypeWithIntAttribute: Codable, Hashable, Sendable { + public let A: AnyJSON + public let B: AnyJSON + public enum CodingKeys: String, CodingKey { + case A = "a" + case B = "b" + } + } public struct CompositeTypeWithRecordAttribute: Codable, Hashable, Sendable { public let Todo: TodosSelect public enum CodingKeys: String, CodingKey { @@ -6887,7 +6961,15 @@ test('typegen: python', async () => { my_text_array: List[str] = Field(alias="my_text_array") class PublicCompositeTypeWithRecordAttribute(BaseModel): - todo: PublicTodos = Field(alias="todo")" + todo: PublicTodos = Field(alias="todo") + + class PublicCompositeTypeWithDomainAttribute(BaseModel): + name: str = Field(alias="name") + score: PublicOneToTen = Field(alias="score") + + class PublicCompositeTypeWithIntAttribute(BaseModel): + a: int = Field(alias="a") + b: int = Field(alias="b")" `) })