Skip to content
Open
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
15 changes: 15 additions & 0 deletions src/server/templates/python.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -327,6 +341,7 @@ const PY_TYPE_MAP: Record<string, string> = {
bool: 'bool',

// Numbers
int: 'int',
int2: 'int',
int4: 'int',
int8: 'int',
Expand Down
17 changes: 7 additions & 10 deletions src/server/templates/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`
})}
}`
)
Expand Down
14 changes: 13 additions & 1 deletion test/db/00-init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -500,4 +500,16 @@ LANGUAGE SQL
STABLE
AS $$
SELECT interval_test_row.duration_required * 2;
$$;
$$;

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
);
84 changes: 83 additions & 1 deletion test/server/typegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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"\`
}"
`)
})
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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")"
`)
})

Expand Down
Loading