Skip to content

Commit 8f8d6de

Browse files
authored
Merge pull request #96 from zenstackhq/dev
merge dev to main
2 parents 8398575 + 64cfad0 commit 8f8d6de

File tree

41 files changed

+721
-161
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+721
-161
lines changed

TODO.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
- [x] Nested to-one
4545
- [x] Incremental update for numeric fields
4646
- [x] Array update
47+
- [ ] Strict typing for checked/unchecked input
4748
- [x] Upsert
4849
- [ ] Implement with "on conflict"
4950
- [x] Delete
@@ -71,6 +72,7 @@
7172
- [x] Custom table name
7273
- [x] Custom field name
7374
- [ ] Strict undefined checks
75+
- [ ] DbNull vs JsonNull
7476
- [ ] Benchmark
7577
- [ ] Plugin
7678
- [ ] Post-mutation hooks should be called after transaction is committed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "zenstack-v3",
3-
"version": "3.0.0-alpha.10",
3+
"version": "3.0.0-alpha.11",
44
"description": "ZenStack",
55
"packageManager": "[email protected]",
66
"scripts": {

packages/cli/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"publisher": "zenstack",
44
"displayName": "ZenStack CLI",
55
"description": "FullStack database toolkit with built-in access control and automatic API generation.",
6-
"version": "3.0.0-alpha.10",
6+
"version": "3.0.0-alpha.11",
77
"type": "module",
88
"author": {
99
"name": "ZenStack Team"

packages/cli/src/actions/action-utils.ts

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { findUp } from '@zenstackhq/common-helpers';
21
import { loadDocument } from '@zenstackhq/language';
32
import { PrismaSchemaGenerator } from '@zenstackhq/sdk';
43
import colors from 'colors';
@@ -53,10 +52,13 @@ export function handleSubProcessError(err: unknown) {
5352
}
5453
}
5554

56-
export async function generateTempPrismaSchema(zmodelPath: string) {
55+
export async function generateTempPrismaSchema(zmodelPath: string, folder?: string) {
5756
const model = await loadSchemaDocument(zmodelPath);
5857
const prismaSchema = await new PrismaSchemaGenerator(model).generate();
59-
const prismaSchemaFile = path.resolve(path.dirname(zmodelPath), '~schema.prisma');
58+
if (!folder) {
59+
folder = path.dirname(zmodelPath);
60+
}
61+
const prismaSchemaFile = path.resolve(folder, '~schema.prisma');
6062
fs.writeFileSync(prismaSchemaFile, prismaSchema);
6163
return prismaSchemaFile;
6264
}
@@ -83,3 +85,28 @@ export function getPkgJsonConfig(startPath: string) {
8385

8486
return result;
8587
}
88+
89+
type FindUpResult<Multiple extends boolean> = Multiple extends true ? string[] | undefined : string | undefined;
90+
91+
function findUp<Multiple extends boolean = false>(
92+
names: string[],
93+
cwd: string = process.cwd(),
94+
multiple: Multiple = false as Multiple,
95+
result: string[] = [],
96+
): FindUpResult<Multiple> {
97+
if (!names.some((name) => !!name)) {
98+
return undefined;
99+
}
100+
const target = names.find((name) => fs.existsSync(path.join(cwd, name)));
101+
if (multiple === false && target) {
102+
return path.join(cwd, target) as FindUpResult<Multiple>;
103+
}
104+
if (target) {
105+
result.push(path.join(cwd, target));
106+
}
107+
const up = path.resolve(cwd, '..');
108+
if (up === cwd) {
109+
return (multiple && result.length > 0 ? result : undefined) as FindUpResult<Multiple>;
110+
}
111+
return findUp(names, up, multiple, result);
112+
}

packages/cli/src/actions/generate.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export async function run(options: Options) {
2424

2525
// generate TS schema
2626
const tsSchemaFile = path.join(outputPath, 'schema.ts');
27-
await new TsSchemaGenerator().generate(schemaFile, [], tsSchemaFile);
27+
await new TsSchemaGenerator().generate(schemaFile, [], outputPath);
2828

2929
await runPlugins(model, outputPath, tsSchemaFile);
3030

packages/cli/src/actions/migrate.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import fs from 'node:fs';
2+
import path from 'node:path';
23
import { execPackage } from '../utils/exec-utils';
34
import { generateTempPrismaSchema, getSchemaFile } from './action-utils';
45

56
type CommonOptions = {
67
schema?: string;
8+
migrations?: string;
79
};
810

911
type DevOptions = CommonOptions & {
@@ -24,7 +26,8 @@ type StatusOptions = CommonOptions;
2426
*/
2527
export async function run(command: string, options: CommonOptions) {
2628
const schemaFile = getSchemaFile(options.schema);
27-
const prismaSchemaFile = await generateTempPrismaSchema(schemaFile);
29+
const prismaSchemaDir = options.migrations ? path.dirname(options.migrations) : undefined;
30+
const prismaSchemaFile = await generateTempPrismaSchema(schemaFile, prismaSchemaDir);
2831

2932
try {
3033
switch (command) {

packages/cli/src/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,31 +60,36 @@ export function createProgram() {
6060
.action(generateAction);
6161

6262
const migrateCommand = program.command('migrate').description('Update the database schema with migrations.');
63+
const migrationsOption = new Option('--migrations <path>', 'path for migrations');
6364

6465
migrateCommand
6566
.command('dev')
6667
.addOption(schemaOption)
6768
.addOption(new Option('-n, --name <name>', 'migration name'))
6869
.addOption(new Option('--create-only', 'only create migration, do not apply'))
70+
.addOption(migrationsOption)
6971
.description('Create a migration from changes in schema and apply it to the database.')
7072
.action((options) => migrateAction('dev', options));
7173

7274
migrateCommand
7375
.command('reset')
7476
.addOption(schemaOption)
7577
.addOption(new Option('--force', 'skip the confirmation prompt'))
78+
.addOption(migrationsOption)
7679
.description('Reset your database and apply all migrations, all data will be lost.')
7780
.action((options) => migrateAction('reset', options));
7881

7982
migrateCommand
8083
.command('deploy')
8184
.addOption(schemaOption)
85+
.addOption(migrationsOption)
8286
.description('Deploy your pending migrations to your production/staging database.')
8387
.action((options) => migrateAction('deploy', options));
8488

8589
migrateCommand
8690
.command('status')
8791
.addOption(schemaOption)
92+
.addOption(migrationsOption)
8893
.description('check the status of your database migrations.')
8994
.action((options) => migrateAction('status', options));
9095

packages/common-helpers/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@zenstackhq/common-helpers",
3-
"version": "3.0.0-alpha.10",
3+
"version": "3.0.0-alpha.11",
44
"description": "ZenStack Common Helpers",
55
"type": "module",
66
"scripts": {

packages/common-helpers/src/find-up.ts

Lines changed: 0 additions & 34 deletions
This file was deleted.

packages/common-helpers/src/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
export * from './find-up';
21
export * from './is-plain-object';
32
export * from './lower-case-first';
43
export * from './param-case';

0 commit comments

Comments
 (0)