@zenstackhq/cli 3.0.0-beta.9 → 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/.turbo/turbo-build.log +8 -8
- package/dist/index.cjs +373 -170
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +373 -165
- package/dist/index.js.map +1 -1
- package/package.json +13 -13
- package/src/actions/action-utils.ts +36 -8
- package/src/actions/db.ts +9 -5
- package/src/actions/format.ts +27 -0
- package/src/actions/generate.ts +100 -18
- package/src/actions/index.ts +4 -2
- package/src/actions/init.ts +3 -3
- package/src/actions/migrate.ts +35 -24
- package/src/actions/seed.ts +38 -0
- package/src/actions/templates.ts +5 -5
- package/src/constants.ts +3 -0
- package/src/index.ts +60 -15
- package/src/plugins/typescript.ts +20 -1
- package/src/utils/exec-utils.ts +35 -0
- package/test/db.test.ts +43 -0
- package/test/format.test.ts +33 -0
- package/test/generate.test.ts +29 -0
- package/test/migrate.test.ts +1 -2
- package/test/ts-schema-gen.test.ts +23 -1
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
|
+
});
|
package/test/generate.test.ts
CHANGED
|
@@ -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
|
});
|
package/test/migrate.test.ts
CHANGED
|
@@ -9,8 +9,7 @@ model User {
|
|
|
9
9
|
}
|
|
10
10
|
`;
|
|
11
11
|
|
|
12
|
-
|
|
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);
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ExpressionUtils } from '@zenstackhq/
|
|
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';
|
|
@@ -420,4 +420,26 @@ model User {
|
|
|
420
420
|
},
|
|
421
421
|
});
|
|
422
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
|
+
});
|
|
423
445
|
});
|