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
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,12 @@ const PRESERVED_NATIVE_TYPES: Record<string, string> = {
decimal: 'Numeric',
timestamp: 'Timestamp',
'timestamp without time zone': 'Timestamp',
timestamptz: 'Timestamptz',
'timestamp with time zone': 'Timestamptz',
// timestamptz introspects to the string representation: the Temporal-backed
// `Timestamptz` needs a global `Temporal`, which some runtimes (e.g. Node 24)
// do not ship, so an inferred contract would fail on first read. This also
// matches the `orm init` scaffold default (`createdAt TimestamptzString`).
timestamptz: 'TimestamptzString',
'timestamp with time zone': 'TimestamptzString',
date: 'Date',
time: 'Time',
'time without time zone': 'Time',
Expand All @@ -47,7 +51,7 @@ const PARAMETERIZED_NATIVE_TYPES: Record<string, string> = {
numeric: 'Numeric',
decimal: 'Numeric',
timestamp: 'Timestamp',
timestamptz: 'Timestamptz',
timestamptz: 'TimestamptzString',
time: 'Time',
timetz: 'Timetz',
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ const authoringTypes = {
kind: 'typeConstructor',
output: { codecId: 'pg/timestamptz-temporal@1', nativeType: 'timestamptz' },
},
TimestamptzString: {
kind: 'typeConstructor',
output: { codecId: 'pg/timestamptz-string@1', nativeType: 'timestamptz' },
},
VarChar: {
kind: 'typeConstructor',
args: [{ kind: 'number', name: 'length', integer: true, minimum: 1, optional: true }],
Expand Down Expand Up @@ -136,7 +140,7 @@ describe('Postgres PSL inference round trip', () => {
expect(inferred).not.toContain('bareAmount Numeric()');
expect(inferred).toMatch(/jsonValue\s+Json/);
expect(inferred).toMatch(/jsonbValue\s+Jsonb/);
expect(inferred).toMatch(/occurredAt\s+Timestamptz/);
expect(inferred).toMatch(/occurredAt\s+TimestamptzString/);
expect(inferred).toMatch(/label\s+VarChar\(191\)/);

const emitted = parseAndEmit(inferred);
Expand Down Expand Up @@ -164,7 +168,7 @@ describe('Postgres PSL inference round trip', () => {
json_value: { codecId: 'pg/json@1', nativeType: 'json', nullable: false },
jsonb_value: { codecId: 'pg/jsonb@1', nativeType: 'jsonb', nullable: false },
occurred_at: {
codecId: 'pg/timestamptz-temporal@1',
codecId: 'pg/timestamptz-string@1',
nativeType: 'timestamptz',
nullable: false,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ describe('createPostgresTypeMap', () => {
nativeType: 'numeric',
});
expect(typeMap.resolve('timestamptz')).toEqual({
pslType: { name: 'Timestamptz' },
pslType: { name: 'TimestamptzString' },
nativeType: 'timestamptz',
});
expect(typeMap.resolve('timestamp with time zone')).toEqual({
pslType: { name: 'Timestamptz' },
pslType: { name: 'TimestamptzString' },
nativeType: 'timestamp with time zone',
});
expect(typeMap.resolve('jsonb')).toEqual({ pslType: { name: 'Jsonb' }, nativeType: 'jsonb' });
Expand Down Expand Up @@ -138,28 +138,36 @@ describe('createPostgresTypeMap', () => {
});
});

describe('representation-explicit spellings stay out of introspection', () => {
describe('temporal representation spellings in introspection', () => {
const map = createPostgresTypeMap();

it.each([
['date', 'Date'],
['timestamp', 'Timestamp'],
['timestamp without time zone', 'Timestamp'],
['timestamptz', 'Timestamptz'],
['timestamp with time zone', 'Timestamptz'],
['time', 'Time'],
['time without time zone', 'Time'],
])('resolves %s to the bare %s, never a *String spelling', (nativeType, pslName) => {
expect(map.resolve(nativeType)).toMatchObject({ pslType: { name: pslName } });
});

it('keeps precision on the bare spelling', () => {
it.each([
['timestamptz', 'TimestamptzString'],
['timestamp with time zone', 'TimestamptzString'],
])(
'resolves %s to the runtime-safe %s spelling (Temporal is unavailable on some runtimes)',
(nativeType, pslName) => {
expect(map.resolve(nativeType)).toMatchObject({ pslType: { name: pslName } });
},
);

it('keeps precision on the TimestamptzString spelling', () => {
expect(map.resolve('timestamptz(6)')).toMatchObject({
pslType: { name: 'Timestamptz', args: ['6'] },
pslType: { name: 'TimestamptzString', args: ['6'] },
});
});

it('never produces a *String name for any native type it knows', () => {
it('produces a *String spelling only for timestamptz', () => {
const natives = [
'date',
'timestamp',
Expand All @@ -182,6 +190,7 @@ describe('representation-explicit spellings stay out of introspection', () => {
: [],
);

expect(stringSpellings).toEqual([]);
expect(stringSpellings.length).toBeGreaterThan(0);
expect(new Set(stringSpellings)).toEqual(new Set(['TimestamptzString']));
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ describe('printPsl', () => {
// WARNING: This table has no primary key in the database
model AuditLog {
event String
timestamp Timestamptz
timestamp TimestamptzString

@@map("audit_log")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ describe('printPsl', () => {
// Contract inferred from the live database schema. Edit as needed, then run \`prisma contract emit\`.

model Post {
id Int @id @default(autoincrement())
title String @default("Untitled")
isPublished Boolean @default(false) @map("is_published")
viewCount Int @default(0) @map("view_count")
createdAt Timestamptz @default(now()) @map("created_at")
id Int @id @default(autoincrement())
title String @default("Untitled")
isPublished Boolean @default(false) @map("is_published")
viewCount Int @default(0) @map("view_count")
createdAt TimestamptzString @default(now()) @map("created_at")

@@map("post")
}
Expand Down Expand Up @@ -431,10 +431,10 @@ describe('printPsl', () => {
// Contract inferred from the live database schema. Edit as needed, then run \`prisma contract emit\`.

model Data {
id Int @id
computed String @default(dbgenerated("my_custom_func()"))
payload Jsonb @default(dbgenerated("'{}'::jsonb"))
touchedAt Timestamptz @default(dbgenerated("clock_timestamp()")) @map("touched_at")
id Int @id
computed String @default(dbgenerated("my_custom_func()"))
payload Jsonb @default(dbgenerated("'{}'::jsonb"))
touchedAt TimestamptzString @default(dbgenerated("clock_timestamp()")) @map("touched_at")

@@map("data")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -283,8 +283,9 @@ export const postgresNativeAuthoringTypes = {
},
// The representation-explicit spellings. Same columns, same precision, same native types — the
// only difference is that a read hands back PostgreSQL's own text instead of a `Temporal.*`, so a
// value Temporal cannot express still round-trips. Authoring-only: they claim no introspection
// mapping, because a bare `timestamptz` column introspects to the Temporal-backed name.
// value Temporal cannot express still round-trips, and they work on runtimes without `Temporal`.
// `timestamptz` introspects to `TimestamptzString` (the `orm init` scaffold default); the remaining
// spellings stay authoring-only and claim no introspection mapping.
DateString: {
kind: 'typeConstructor',
output: { codecId: 'pg/date-string@1', nativeType: 'date' },
Expand Down