Skip to content
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
15 changes: 15 additions & 0 deletions src/core/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,15 @@ class JsonbColumnBuilder<
}
}

class JsonColumnBuilder<
TName extends string,
TJsonType = unknown
> extends BaseColumnBuilder<TJsonType, TName> {
constructor(name: TName) {
super(name, "json", { postgres: "JSON", spanner: "JSON" });
}
}

// --- Column Functions (User-facing API) ---

export function text<TName extends string>(
Expand Down Expand Up @@ -247,6 +256,12 @@ export function jsonb<TName extends string, TJsonType = unknown>( // Changed any
return new JsonbColumnBuilder<TName, TJsonType>(name);
}

export function json<TName extends string, TJsonType = unknown>(
name: TName
): JsonColumnBuilder<TName, TJsonType> {
return new JsonColumnBuilder<TName, TJsonType>(name);
}

// --- Table Definition ---

type ColumnBuilderToConfig<TBuilder> = TBuilder extends BaseColumnBuilder<
Expand Down
9 changes: 9 additions & 0 deletions test/schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
boolean,
timestamp,
jsonb,
json,
sql,
index,
uniqueIndex,
Expand All @@ -26,6 +27,7 @@ describe("Schema Builder", () => {
age: integer("age").default(0),
isAdmin: boolean("is_admin").default(false),
settings: jsonb("settings"),
metadata: json("metadata"),
lastLogin: timestamp("last_login").default(sql`CURRENT_TIMESTAMP`),
apiKey: varchar("api_key", { length: 64 }),
},
Expand Down Expand Up @@ -75,6 +77,12 @@ describe("Schema Builder", () => {
expect(users.columns.settings.dialectTypes.postgres).toBe("JSONB");
expect(users.columns.settings.dialectTypes.spanner).toBe("JSON");

// Check metadata column
expect(users.columns.metadata.name).toBe("metadata");
expect(users.columns.metadata.type).toBe("json");
expect(users.columns.metadata.dialectTypes.postgres).toBe("JSON");
expect(users.columns.metadata.dialectTypes.spanner).toBe("JSON");

// Check lastLogin column
expect(users.columns.lastLogin.name).toBe("last_login");
expect(users.columns.lastLogin.type).toBe("timestamp");
Expand Down Expand Up @@ -119,6 +127,7 @@ describe("Schema Builder", () => {
age: 30,
isAdmin: true,
settings: { theme: "dark" },
metadata: { tags: ["a", "b"] },
lastLogin: new Date(),
apiKey: "secretkey",
};
Expand Down