Skip to content

Commit 2209d4c

Browse files
authored
Merge pull request #27 from BriefHQ/data-type-narrow
fix: narrowed supported data types
2 parents c086dc9 + 38265cc commit 2209d4c

File tree

4 files changed

+178
-31
lines changed

4 files changed

+178
-31
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "drizzle-zero",
3-
"version": "0.2.1",
3+
"version": "0.2.2",
44
"description": "Generate Zero schemas from Drizzle ORM schemas",
55
"type": "module",
66
"scripts": {

src/drizzle-to-zero.ts

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,7 @@ import type { JSONValue } from "@rocicorp/zero";
44
* Represents the basic data types supported by Drizzle ORM.
55
* These are the fundamental types that can be used in table column definitions.
66
*/
7-
type DrizzleDataType =
8-
| "number"
9-
| "bigint"
10-
| "boolean"
11-
| "date"
12-
| "string"
13-
| "json";
7+
type DrizzleDataType = "number" | "bigint" | "boolean" | "date";
148

159
/**
1610
* Maps Drizzle data types to their corresponding Zero schema types.
@@ -21,8 +15,6 @@ export const drizzleDataTypeToZeroType = {
2115
bigint: "number",
2216
boolean: "boolean",
2317
date: "number",
24-
string: "string",
25-
json: "json",
2618
} as const satisfies Record<DrizzleDataType, string>;
2719

2820
/**
@@ -33,15 +25,31 @@ export type DrizzleDataTypeToZeroType = typeof drizzleDataTypeToZeroType;
3325

3426
/**
3527
* Represents specific Postgres column types supported by Drizzle ORM.
36-
* These are more specialized types that need custom handling when converting to Zero.
3728
*/
38-
type DrizzleColumnType = "PgNumeric" | "PgDateString" | "PgTimestampString";
29+
type DrizzleColumnType =
30+
| "PgText"
31+
| "PgChar"
32+
| "PgVarchar"
33+
| "PgUUID"
34+
| "PgEnumColumn"
35+
| "PgJsonb"
36+
| "PgJson"
37+
| "PgNumeric"
38+
| "PgDateString"
39+
| "PgTimestampString";
3940

4041
/**
4142
* Maps Postgres-specific Drizzle column types to their corresponding Zero schema types.
4243
* Handles special cases where Postgres types need specific Zero type representations.
4344
*/
4445
export const drizzleColumnTypeToZeroType = {
46+
PgText: "string",
47+
PgChar: "string",
48+
PgVarchar: "string",
49+
PgUUID: "string",
50+
PgEnumColumn: "string",
51+
PgJsonb: "json",
52+
PgJson: "json",
4553
PgNumeric: "number",
4654
PgDateString: "number",
4755
PgTimestampString: "number",

src/tables.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ const createZeroTableSchema = <
218218

219219
if (!type) {
220220
throw new Error(
221-
`drizzle-zero: Unsupported column type: ${column.dataType}. It must be supported by Zero, e.g.: ${Object.keys(drizzleDataTypeToZeroType).join(" | ")}`,
221+
`drizzle-zero: Unsupported column type: ${column.columnType} (${column.dataType}). It must be supported by Zero, e.g.: ${Object.keys({ ...drizzleDataTypeToZeroType, ...drizzleColumnTypeToZeroType }).join(" | ")}`,
222222
);
223223
}
224224

tests/tables.test.ts

Lines changed: 157 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,22 @@ import {
55
bigserial,
66
boolean,
77
char,
8+
cidr,
89
date,
910
doublePrecision,
11+
geometry,
12+
inet,
1013
integer,
14+
interval,
1115
json,
1216
jsonb,
17+
line,
18+
macaddr,
1319
numeric,
1420
pgEnum,
1521
pgSchema,
1622
pgTable,
23+
point,
1724
primaryKey,
1825
real,
1926
serial,
@@ -180,24 +187,6 @@ describe.concurrent("tables", () => {
180187
Expect<Equal<typeof result, typeof expected>>;
181188
});
182189

183-
test("pg - array types", () => {
184-
const table = pgTable("test", {
185-
id: text().primaryKey(),
186-
tags: text().array().notNull(),
187-
scores: jsonb().array(),
188-
});
189-
190-
expect(() =>
191-
createZeroTableSchema(table, {
192-
id: true,
193-
tags: true,
194-
scores: true,
195-
}),
196-
).toThrowErrorMatchingInlineSnapshot(
197-
`[Error: drizzle-zero: Unsupported column type: array. It must be supported by Zero, e.g.: number | bigint | boolean | date | string | json]`,
198-
);
199-
});
200-
201190
test("pg - complex custom types", () => {
202191
type UserMetadata = {
203192
preferences: {
@@ -1225,6 +1214,22 @@ describe.concurrent("tables", () => {
12251214
Expect<Equal<typeof result, typeof expected>>;
12261215
});
12271216

1217+
test("pg - invalid column type", () => {
1218+
const table = pgTable("test", {
1219+
id: text().primaryKey(),
1220+
invalid: text().notNull(),
1221+
});
1222+
1223+
expect(() =>
1224+
createZeroTableSchema(table, {
1225+
id: true,
1226+
invalid: "someinvalidtype",
1227+
} as unknown as ColumnsConfig<typeof table>),
1228+
).toThrowErrorMatchingInlineSnapshot(
1229+
`[Error: drizzle-zero: Invalid column config for column invalid - expected boolean or object but was string]`,
1230+
);
1231+
});
1232+
12281233
test("pg - invalid column selection", () => {
12291234
const table = pgTable("test", {
12301235
id: text().primaryKey(),
@@ -1241,6 +1246,140 @@ describe.concurrent("tables", () => {
12411246
);
12421247
});
12431248

1249+
test("pg - array types", () => {
1250+
const table = pgTable("test", {
1251+
id: text().primaryKey(),
1252+
tags: text().array().notNull(),
1253+
scores: jsonb().array(),
1254+
});
1255+
1256+
expect(() =>
1257+
createZeroTableSchema(table, {
1258+
id: true,
1259+
tags: true,
1260+
scores: true,
1261+
}),
1262+
).toThrowErrorMatchingInlineSnapshot(
1263+
`[Error: drizzle-zero: Unsupported column type: PgArray (array). It must be supported by Zero, e.g.: number | bigint | boolean | date | PgText | PgChar | PgVarchar | PgUUID | PgEnumColumn | PgJsonb | PgJson | PgNumeric | PgDateString | PgTimestampString]`,
1264+
);
1265+
});
1266+
1267+
test("pg - interval types", () => {
1268+
const table = pgTable("test", {
1269+
id: text().primaryKey(),
1270+
interval: interval().notNull(),
1271+
});
1272+
1273+
expect(() =>
1274+
createZeroTableSchema(table, {
1275+
id: true,
1276+
interval: true,
1277+
}),
1278+
).toThrowErrorMatchingInlineSnapshot(
1279+
`[Error: drizzle-zero: Unsupported column type: PgInterval (string). It must be supported by Zero, e.g.: number | bigint | boolean | date | PgText | PgChar | PgVarchar | PgUUID | PgEnumColumn | PgJsonb | PgJson | PgNumeric | PgDateString | PgTimestampString]`,
1280+
);
1281+
});
1282+
1283+
test("pg - cidr types", () => {
1284+
const table = pgTable("test", {
1285+
id: text().primaryKey(),
1286+
cidr: cidr().notNull(),
1287+
});
1288+
1289+
expect(() =>
1290+
createZeroTableSchema(table, {
1291+
id: true,
1292+
cidr: true,
1293+
}),
1294+
).toThrowErrorMatchingInlineSnapshot(
1295+
`[Error: drizzle-zero: Unsupported column type: PgCidr (string). It must be supported by Zero, e.g.: number | bigint | boolean | date | PgText | PgChar | PgVarchar | PgUUID | PgEnumColumn | PgJsonb | PgJson | PgNumeric | PgDateString | PgTimestampString]`,
1296+
);
1297+
});
1298+
1299+
test("pg - macaddr types", () => {
1300+
const table = pgTable("test", {
1301+
id: text().primaryKey(),
1302+
macaddr: macaddr().notNull(),
1303+
});
1304+
1305+
expect(() =>
1306+
createZeroTableSchema(table, {
1307+
id: true,
1308+
macaddr: true,
1309+
}),
1310+
).toThrowErrorMatchingInlineSnapshot(
1311+
`[Error: drizzle-zero: Unsupported column type: PgMacaddr (string). It must be supported by Zero, e.g.: number | bigint | boolean | date | PgText | PgChar | PgVarchar | PgUUID | PgEnumColumn | PgJsonb | PgJson | PgNumeric | PgDateString | PgTimestampString]`,
1312+
);
1313+
});
1314+
1315+
test("pg - inet types", () => {
1316+
const table = pgTable("test", {
1317+
id: text().primaryKey(),
1318+
inet: inet().notNull(),
1319+
});
1320+
1321+
expect(() =>
1322+
createZeroTableSchema(table, {
1323+
id: true,
1324+
inet: true,
1325+
}),
1326+
).toThrowErrorMatchingInlineSnapshot(
1327+
`[Error: drizzle-zero: Unsupported column type: PgInet (string). It must be supported by Zero, e.g.: number | bigint | boolean | date | PgText | PgChar | PgVarchar | PgUUID | PgEnumColumn | PgJsonb | PgJson | PgNumeric | PgDateString | PgTimestampString]`,
1328+
);
1329+
});
1330+
1331+
test("pg - point types", () => {
1332+
const table = pgTable("test", {
1333+
id: text().primaryKey(),
1334+
point: point().notNull(),
1335+
});
1336+
1337+
expect(() =>
1338+
createZeroTableSchema(table, {
1339+
id: true,
1340+
point: true,
1341+
}),
1342+
).toThrowErrorMatchingInlineSnapshot(
1343+
`[Error: drizzle-zero: Unsupported column type: PgPointTuple (array). It must be supported by Zero, e.g.: number | bigint | boolean | date | PgText | PgChar | PgVarchar | PgUUID | PgEnumColumn | PgJsonb | PgJson | PgNumeric | PgDateString | PgTimestampString]`,
1344+
);
1345+
});
1346+
1347+
test("pg - line types", () => {
1348+
const table = pgTable("test", {
1349+
id: text().primaryKey(),
1350+
line: line().notNull(),
1351+
});
1352+
1353+
expect(() =>
1354+
createZeroTableSchema(table, {
1355+
id: true,
1356+
line: true,
1357+
}),
1358+
).toThrowErrorMatchingInlineSnapshot(
1359+
`[Error: drizzle-zero: Unsupported column type: PgLine (array). It must be supported by Zero, e.g.: number | bigint | boolean | date | PgText | PgChar | PgVarchar | PgUUID | PgEnumColumn | PgJsonb | PgJson | PgNumeric | PgDateString | PgTimestampString]`,
1360+
);
1361+
});
1362+
1363+
test("pg - geometry types", () => {
1364+
const table = pgTable("test", {
1365+
id: text().primaryKey(),
1366+
location: geometry("location", {
1367+
type: "point",
1368+
mode: "xy",
1369+
srid: 4326,
1370+
}).notNull(),
1371+
});
1372+
1373+
expect(() =>
1374+
createZeroTableSchema(table, {
1375+
id: true,
1376+
location: true,
1377+
}),
1378+
).toThrowErrorMatchingInlineSnapshot(
1379+
`[Error: drizzle-zero: Unsupported column type: PgGeometryObject (json). It must be supported by Zero, e.g.: number | bigint | boolean | date | PgText | PgChar | PgVarchar | PgUUID | PgEnumColumn | PgJsonb | PgJson | PgNumeric | PgDateString | PgTimestampString]`,
1380+
);
1381+
});
1382+
12441383
test("pg - no primary key", () => {
12451384
const table = pgTable("test", {
12461385
id: text(),

0 commit comments

Comments
 (0)