Skip to content

Commit b848d1c

Browse files
committed
fix(default): add quotes around json default, closes #22
1 parent 596e3d7 commit b848d1c

File tree

4 files changed

+46
-5
lines changed

4 files changed

+46
-5
lines changed

__tests__/fixtures/table.datamodel.ts

+7
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,10 @@ export const datamodelTableWithBlockIdAndCompositeUnqiue = /* Prisma */ `
8080
@@unique([email, role])
8181
}
8282
`;
83+
84+
export const datamodelTableWithJsonDefault = /* Prisma */ `
85+
model Example {
86+
id String @id @default(uuid())
87+
jsonField Json @default("{\\"example\\": 0.7}")
88+
}
89+
`;

__tests__/table.test.ts

+18
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {
22
datamodelSingleTable,
33
datamodelTableWithBlockId,
44
datamodelTableWithBlockIdAndCompositeUnqiue,
5+
datamodelTableWithJsonDefault,
56
datamodelTableWithOneCompositeUniqueIndex,
67
datamodelTableWithSingleCompositeUniqueIndex,
78
datamodelTableWithStringDefaults,
@@ -138,6 +139,7 @@ describe('Tables', () => {
138139
expect(tables.length).toEqual(1);
139140
expect(tables[0]).toMatch(expected);
140141
});
142+
141143
test('generate a table with block id and composite unique index', async () => {
142144
const dmmf = await generateDMMF(
143145
datamodelTableWithBlockIdAndCompositeUnqiue
@@ -161,4 +163,20 @@ describe('Tables', () => {
161163
expect(tables.length).toEqual(1);
162164
expect(tables[0]).toMatch(expected);
163165
});
166+
167+
test('generate a table with json default', async () => {
168+
const dmmf = await generateDMMF(
169+
datamodelTableWithJsonDefault
170+
);
171+
172+
const expected = `Table Example {
173+
id String [pk]
174+
jsonField Json [not null, default: '{"example": 0.7}']
175+
}`;
176+
177+
const tables = generateTables(dmmf.datamodel.models);
178+
179+
expect(tables.length).toEqual(1);
180+
expect(tables[0]).toMatch(expected);
181+
});
164182
});

src/generator/table.ts

+9-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { DBMLKeywords } from './../keywords';
1+
import { DBMLKeywords, PrismaScalars } from './../keywords';
22
import { DMMF } from '@prisma/generator-helper';
33

44
export function generateTables(models: DMMF.Model[]): string[] {
@@ -23,9 +23,9 @@ const generateTableIndexes = (model: DMMF.Model): string => {
2323
: '';
2424
};
2525

26-
const hasCompositeUniqueIndices = (uniqueFields: string[][]) : boolean => {
27-
return uniqueFields.filter(composite => composite.length > 1).length > 0;
28-
}
26+
const hasCompositeUniqueIndices = (uniqueFields: string[][]): boolean => {
27+
return uniqueFields.filter((composite) => composite.length > 1).length > 0;
28+
};
2929

3030
const generateTableBlockId = (primaryFields: string[] | undefined): string => {
3131
if (primaryFields === undefined || primaryFields.length === 0) {
@@ -82,7 +82,11 @@ const generateColumnDefinition = (field: DMMF.Field): string => {
8282
}
8383

8484
if (field.hasDefaultValue && typeof field.default != 'object') {
85-
if (field.type === 'String' || field.kind === 'enum') {
85+
if (
86+
field.type === PrismaScalars.String ||
87+
field.type === PrismaScalars.Json ||
88+
field.kind === 'enum'
89+
) {
8690
columnDefinition.push(`${DBMLKeywords.Default}: '${field.default}'`);
8791
} else {
8892
columnDefinition.push(`${DBMLKeywords.Default}: ${field.default}`);

src/keywords.ts

+12
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,15 @@ export enum DBMLKeywords {
88
Pk = 'pk',
99
Indexes = 'indexes',
1010
}
11+
12+
export enum PrismaScalars {
13+
String = 'String',
14+
Boolean = 'Boolean',
15+
Int = 'Int',
16+
Float = 'Float',
17+
DateTime = 'DateTime',
18+
Json = 'Json',
19+
BigInt = 'BigInt',
20+
Decimal = 'Decimal',
21+
Bytes = 'Bytes',
22+
}

0 commit comments

Comments
 (0)