Skip to content

Commit 22db7b5

Browse files
liontariaiymc9
andauthored
Enable JSON fields for SQLite (#2081)
Co-authored-by: Yiming <[email protected]> Co-authored-by: ymc9 <[email protected]>
1 parent d8f95e5 commit 22db7b5

File tree

4 files changed

+29
-43
lines changed

4 files changed

+29
-43
lines changed

packages/schema/src/plugins/prisma/schema-generator.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ import {
8282
const MODEL_PASSTHROUGH_ATTR = '@@prisma.passthrough';
8383
const FIELD_PASSTHROUGH_ATTR = '@prisma.passthrough';
8484
const PROVIDERS_SUPPORTING_NAMED_CONSTRAINTS = ['postgresql', 'mysql', 'cockroachdb'];
85-
const PROVIDERS_SUPPORTING_TYPEDEF_FIELDS = ['postgresql'];
85+
const PROVIDERS_SUPPORTING_TYPEDEF_FIELDS = ['postgresql', 'sqlite'];
8686

8787
// Some database providers like postgres and mysql have default limit to the length of identifiers
8888
// Here we use a conservative value that should work for most cases, and truncate names if needed

tests/integration/tests/enhancements/json/crud.test.ts

+19-15
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
11
import { createPostgresDb, dropPostgresDb, loadSchema } from '@zenstackhq/testtools';
22

3-
describe('Json field CRUD', () => {
3+
describe.each(['sqlite' as const, 'postgresql' as const])('Json field CRUD - %p', (provider) => {
44
let dbUrl: string;
55
let prisma: any;
66

77
beforeEach(async () => {
8-
dbUrl = await createPostgresDb('json-field-typing');
8+
if (provider === 'postgresql') {
9+
dbUrl = await createPostgresDb('json-field-typing');
10+
}
911
});
1012

1113
afterEach(async () => {
12-
if (prisma) {
13-
await prisma.$disconnect();
14+
if (provider === 'postgresql') {
15+
if (prisma) {
16+
await prisma.$disconnect();
17+
}
18+
await dropPostgresDb(dbUrl);
1419
}
15-
await dropPostgresDb(dbUrl);
1620
});
1721

1822
it('works with simple cases', async () => {
@@ -41,7 +45,7 @@ describe('Json field CRUD', () => {
4145
}
4246
`,
4347
{
44-
provider: 'postgresql',
48+
provider,
4549
dbUrl,
4650
enhancements: ['validation'],
4751
}
@@ -90,7 +94,7 @@ describe('Json field CRUD', () => {
9094
}
9195
`,
9296
{
93-
provider: 'postgresql',
97+
provider,
9498
dbUrl,
9599
}
96100
);
@@ -137,7 +141,7 @@ describe('Json field CRUD', () => {
137141
}
138142
`,
139143
{
140-
provider: 'postgresql',
144+
provider,
141145
dbUrl,
142146
}
143147
);
@@ -211,7 +215,7 @@ describe('Json field CRUD', () => {
211215
}
212216
`,
213217
{
214-
provider: 'postgresql',
218+
provider,
215219
dbUrl,
216220
}
217221
);
@@ -255,7 +259,7 @@ describe('Json field CRUD', () => {
255259
}
256260
`,
257261
{
258-
provider: 'postgresql',
262+
provider,
259263
dbUrl,
260264
}
261265
);
@@ -291,7 +295,7 @@ describe('Json field CRUD', () => {
291295
}
292296
`,
293297
{
294-
provider: 'postgresql',
298+
provider,
295299
dbUrl,
296300
}
297301
);
@@ -328,7 +332,7 @@ describe('Json field CRUD', () => {
328332
}
329333
`,
330334
{
331-
provider: 'postgresql',
335+
provider,
332336
dbUrl,
333337
}
334338
);
@@ -368,7 +372,7 @@ describe('Json field CRUD', () => {
368372
}
369373
`,
370374
{
371-
provider: 'postgresql',
375+
provider,
372376
dbUrl,
373377
}
374378
);
@@ -401,7 +405,7 @@ describe('Json field CRUD', () => {
401405
}
402406
`,
403407
{
404-
provider: 'postgresql',
408+
provider,
405409
dbUrl,
406410
}
407411
);
@@ -446,7 +450,7 @@ describe('Json field CRUD', () => {
446450
}
447451
`,
448452
{
449-
provider: 'postgresql',
453+
provider,
450454
dbUrl,
451455
compile: true,
452456
extraSourceFiles: [

tests/integration/tests/enhancements/json/typing.test.ts

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { loadSchema } from '@zenstackhq/testtools';
22

3-
describe('JSON field typing', () => {
3+
describe.each(['sqlite' as const, 'postgresql' as const])('JSON field typing - %p', (provider) => {
44
it('works with simple field', async () => {
55
await loadSchema(
66
`
@@ -23,7 +23,7 @@ describe('JSON field typing', () => {
2323
}
2424
`,
2525
{
26-
provider: 'postgresql',
26+
provider,
2727
pushDb: false,
2828
compile: true,
2929
extraSourceFiles: [
@@ -64,7 +64,7 @@ async function main() {
6464
}
6565
`,
6666
{
67-
provider: 'postgresql',
67+
provider,
6868
pushDb: false,
6969
compile: true,
7070
extraSourceFiles: [
@@ -105,7 +105,7 @@ async function main() {
105105
}
106106
`,
107107
{
108-
provider: 'postgresql',
108+
provider,
109109
pushDb: false,
110110
compile: true,
111111
extraSourceFiles: [
@@ -151,7 +151,7 @@ async function main() {
151151
}
152152
`,
153153
{
154-
provider: 'postgresql',
154+
provider,
155155
pushDb: false,
156156
compile: true,
157157
extraSourceFiles: [
@@ -203,7 +203,7 @@ async function main() {
203203
}
204204
`,
205205
{
206-
provider: 'postgresql',
206+
provider,
207207
pushDb: false,
208208
compile: true,
209209
extraSourceFiles: [
@@ -252,7 +252,7 @@ async function main() {
252252
}
253253
`,
254254
{
255-
provider: 'postgresql',
255+
provider,
256256
pushDb: false,
257257
compile: true,
258258
extraSourceFiles: [
@@ -303,7 +303,7 @@ async function main() {
303303
}
304304
`,
305305
{
306-
provider: 'postgresql',
306+
provider,
307307
pushDb: false,
308308
compile: true,
309309
extraSourceFiles: [
@@ -348,7 +348,7 @@ async function main() {
348348
}
349349
`,
350350
{
351-
provider: 'postgresql',
351+
provider,
352352
pushDb: false,
353353
compile: true,
354354
extraSourceFiles: [

tests/integration/tests/enhancements/json/validation.test.ts

-18
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,6 @@
11
import { loadModel, loadModelWithError, loadSchema } from '@zenstackhq/testtools';
22

33
describe('JSON field typing', () => {
4-
it('is only supported by postgres', async () => {
5-
await expect(
6-
loadSchema(
7-
`
8-
type Profile {
9-
age Int @gt(0)
10-
}
11-
12-
model User {
13-
id Int @id @default(autoincrement())
14-
profile Profile @json
15-
@@allow('all', true)
16-
}
17-
`
18-
)
19-
).rejects.toThrow('Datasource provider "sqlite" does not support "@json" fields');
20-
});
21-
224
it('requires field to have @json attribute', async () => {
235
await expect(
246
loadModelWithError(

0 commit comments

Comments
 (0)