@zenstackhq/cli 3.0.0-beta.8 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/test/db.test.ts CHANGED
@@ -15,4 +15,47 @@ describe('CLI db commands test', () => {
15
15
  runCli('db push', workDir);
16
16
  expect(fs.existsSync(path.join(workDir, 'zenstack/dev.db'))).toBe(true);
17
17
  });
18
+
19
+ it('should seed the database with db seed with seed script', () => {
20
+ const workDir = createProject(model);
21
+ const pkgJson = JSON.parse(fs.readFileSync(path.join(workDir, 'package.json'), 'utf8'));
22
+ pkgJson.zenstack = {
23
+ seed: 'node seed.js',
24
+ };
25
+ fs.writeFileSync(path.join(workDir, 'package.json'), JSON.stringify(pkgJson, null, 2));
26
+ fs.writeFileSync(
27
+ path.join(workDir, 'seed.js'),
28
+ `
29
+ import fs from 'node:fs';
30
+ fs.writeFileSync('seed.txt', 'success');
31
+ `,
32
+ );
33
+
34
+ runCli('db seed', workDir);
35
+ expect(fs.readFileSync(path.join(workDir, 'seed.txt'), 'utf8')).toBe('success');
36
+ });
37
+
38
+ it('should seed the database after migrate reset', () => {
39
+ const workDir = createProject(model);
40
+ const pkgJson = JSON.parse(fs.readFileSync(path.join(workDir, 'package.json'), 'utf8'));
41
+ pkgJson.zenstack = {
42
+ seed: 'node seed.js',
43
+ };
44
+ fs.writeFileSync(path.join(workDir, 'package.json'), JSON.stringify(pkgJson, null, 2));
45
+ fs.writeFileSync(
46
+ path.join(workDir, 'seed.js'),
47
+ `
48
+ import fs from 'node:fs';
49
+ fs.writeFileSync('seed.txt', 'success');
50
+ `,
51
+ );
52
+
53
+ runCli('migrate reset --force', workDir);
54
+ expect(fs.readFileSync(path.join(workDir, 'seed.txt'), 'utf8')).toBe('success');
55
+ });
56
+
57
+ it('should skip seeding the database without seed script', () => {
58
+ const workDir = createProject(model);
59
+ runCli('db seed', workDir);
60
+ });
18
61
  });
@@ -0,0 +1,33 @@
1
+ import { describe, expect, it } from 'vitest';
2
+ import { createProject, runCli } from './utils';
3
+ import fs from 'node:fs';
4
+
5
+ const model = `
6
+ model User {
7
+ id String @id @default(cuid())
8
+ email String @unique
9
+ }
10
+ `;
11
+
12
+ describe('CLI format command test', () => {
13
+ it('should format a valid schema successfully', () => {
14
+ const workDir = createProject(model);
15
+ expect(() => runCli('format', workDir)).not.toThrow();
16
+ const updatedContent = fs.readFileSync(`${workDir}/zenstack/schema.zmodel`, 'utf-8');
17
+ expect(
18
+ updatedContent.includes(`model User {
19
+ id String @id @default(cuid())
20
+ email String @unique
21
+ }`),
22
+ ).toBeTruthy();
23
+ });
24
+
25
+ it('should silently ignore invalid schema', () => {
26
+ const invalidModel = `
27
+ model User {
28
+ id String @id @default(cuid())
29
+ `;
30
+ const workDir = createProject(invalidModel);
31
+ expect(() => runCli('format', workDir)).not.toThrow();
32
+ });
33
+ });
@@ -44,4 +44,33 @@ describe('CLI generate command test', () => {
44
44
  runCli('generate', workDir);
45
45
  expect(fs.existsSync(path.join(workDir, 'bar/schema.ts'))).toBe(true);
46
46
  });
47
+
48
+ it('should respect package.json schema dir config', () => {
49
+ const workDir = createProject(model);
50
+ fs.mkdirSync(path.join(workDir, 'foo'));
51
+ fs.renameSync(path.join(workDir, 'zenstack/schema.zmodel'), path.join(workDir, 'foo/schema.zmodel'));
52
+ fs.rmdirSync(path.join(workDir, 'zenstack'));
53
+ const pkgJson = JSON.parse(fs.readFileSync(path.join(workDir, 'package.json'), 'utf8'));
54
+ pkgJson.zenstack = {
55
+ schema: './foo',
56
+ output: './bar',
57
+ };
58
+ fs.writeFileSync(path.join(workDir, 'package.json'), JSON.stringify(pkgJson, null, 2));
59
+ runCli('generate', workDir);
60
+ expect(fs.existsSync(path.join(workDir, 'bar/schema.ts'))).toBe(true);
61
+ });
62
+
63
+ it('should respect lite option', () => {
64
+ const workDir = createProject(model);
65
+ runCli('generate --lite', workDir);
66
+ expect(fs.existsSync(path.join(workDir, 'zenstack/schema.ts'))).toBe(true);
67
+ expect(fs.existsSync(path.join(workDir, 'zenstack/schema-lite.ts'))).toBe(true);
68
+ });
69
+
70
+ it('should respect liteOnly option', () => {
71
+ const workDir = createProject(model);
72
+ runCli('generate --lite-only', workDir);
73
+ expect(fs.existsSync(path.join(workDir, 'zenstack/schema.ts'))).toBe(false);
74
+ expect(fs.existsSync(path.join(workDir, 'zenstack/schema-lite.ts'))).toBe(true);
75
+ });
47
76
  });
@@ -9,8 +9,7 @@ model User {
9
9
  }
10
10
  `;
11
11
 
12
- // skip due to timeout in CI
13
- describe.skip('CLI migrate commands test', () => {
12
+ describe('CLI migrate commands test', () => {
14
13
  it('should generate a database with migrate dev', () => {
15
14
  const workDir = createProject(model);
16
15
  runCli('migrate dev --name init', workDir);
@@ -57,4 +57,25 @@ model User {
57
57
  runCli('generate', workDir);
58
58
  expect(fs.existsSync(path.join(workDir, 'prisma/schema.prisma'))).toBe(true);
59
59
  });
60
+
61
+ it('can generate a Prisma schema with custom output relative to zenstack.output', () => {
62
+ const workDir = createProject(`
63
+ plugin prisma {
64
+ provider = '@core/prisma'
65
+ output = './schema.prisma'
66
+ }
67
+
68
+ model User {
69
+ id String @id @default(cuid())
70
+ }
71
+ `);
72
+
73
+ const pkgJson = JSON.parse(fs.readFileSync(path.join(workDir, 'package.json'), 'utf8'));
74
+ pkgJson.zenstack = {
75
+ output: './relative',
76
+ };
77
+ fs.writeFileSync(path.join(workDir, 'package.json'), JSON.stringify(pkgJson, null, 2));
78
+ runCli('generate', workDir);
79
+ expect(fs.existsSync(path.join(workDir, 'relative/schema.prisma'))).toBe(true);
80
+ });
60
81
  });
@@ -1,4 +1,4 @@
1
- import { ExpressionUtils } from '@zenstackhq/runtime/schema';
1
+ import { ExpressionUtils } from '@zenstackhq/orm/schema';
2
2
  import { createTestProject, generateTsSchema, generateTsSchemaInPlace } from '@zenstackhq/testtools';
3
3
  import fs from 'node:fs';
4
4
  import path from 'node:path';
@@ -360,4 +360,86 @@ model User extends Base {
360
360
  expect(schema.enums).toMatchObject({ Role: expect.any(Object) });
361
361
  expect(schema.models).toMatchObject({ User: expect.any(Object) });
362
362
  });
363
+
364
+ it('generates correct default literal function arguments', async () => {
365
+ const { schema } = await generateTsSchema(`
366
+ model User {
367
+ id String @id @default(uuid(7))
368
+ }
369
+ `);
370
+
371
+ expect(schema.models).toMatchObject({
372
+ User: {
373
+ name: 'User',
374
+ fields: {
375
+ id: {
376
+ name: 'id',
377
+ type: 'String',
378
+ id: true,
379
+ attributes: [
380
+ {
381
+ name: '@id',
382
+ },
383
+ {
384
+ name: '@default',
385
+ args: [
386
+ {
387
+ name: 'value',
388
+ value: {
389
+ kind: 'call',
390
+ function: 'uuid',
391
+ args: [
392
+ {
393
+ kind: 'literal',
394
+ value: 7,
395
+ },
396
+ ],
397
+ },
398
+ },
399
+ ],
400
+ },
401
+ ],
402
+ default: {
403
+ kind: 'call',
404
+ function: 'uuid',
405
+ args: [
406
+ {
407
+ kind: 'literal',
408
+ value: 7,
409
+ },
410
+ ],
411
+ },
412
+ },
413
+ },
414
+ idFields: ['id'],
415
+ uniqueFields: {
416
+ id: {
417
+ type: 'String',
418
+ },
419
+ },
420
+ },
421
+ });
422
+ });
423
+
424
+ it('supports lite schema generation', async () => {
425
+ const { schemaLite } = await generateTsSchema(
426
+ `
427
+ model User {
428
+ id String @id @default(uuid())
429
+ name String
430
+ email String @unique
431
+
432
+ @@map('users')
433
+ }
434
+ `,
435
+ undefined,
436
+ undefined,
437
+ undefined,
438
+ true,
439
+ );
440
+
441
+ expect(schemaLite!.models.User.attributes).toBeUndefined();
442
+ expect(schemaLite!.models.User.fields.id.attributes).toBeUndefined();
443
+ expect(schemaLite!.models.User.fields.email.attributes).toBeUndefined();
444
+ });
363
445
  });