Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
- [x] Nested to-one
- [x] Incremental update for numeric fields
- [x] Array update
- [ ] Strict typing for checked/unchecked input
- [x] Upsert
- [ ] Implement with "on conflict"
- [x] Delete
Expand Down Expand Up @@ -71,6 +72,7 @@
- [x] Custom table name
- [x] Custom field name
- [ ] Strict undefined checks
- [ ] DbNull vs JsonNull
- [ ] Benchmark
- [ ] Plugin
- [ ] Post-mutation hooks should be called after transaction is committed
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "zenstack-v3",
"version": "3.0.0-alpha.10",
"version": "3.0.0-alpha.11",
"description": "ZenStack",
"packageManager": "pnpm@10.12.1",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"publisher": "zenstack",
"displayName": "ZenStack CLI",
"description": "FullStack database toolkit with built-in access control and automatic API generation.",
"version": "3.0.0-alpha.10",
"version": "3.0.0-alpha.11",
"type": "module",
"author": {
"name": "ZenStack Team"
Expand Down
33 changes: 30 additions & 3 deletions packages/cli/src/actions/action-utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { findUp } from '@zenstackhq/common-helpers';
import { loadDocument } from '@zenstackhq/language';
import { PrismaSchemaGenerator } from '@zenstackhq/sdk';
import colors from 'colors';
Expand Down Expand Up @@ -53,10 +52,13 @@ export function handleSubProcessError(err: unknown) {
}
}

export async function generateTempPrismaSchema(zmodelPath: string) {
export async function generateTempPrismaSchema(zmodelPath: string, folder?: string) {
const model = await loadSchemaDocument(zmodelPath);
const prismaSchema = await new PrismaSchemaGenerator(model).generate();
const prismaSchemaFile = path.resolve(path.dirname(zmodelPath), '~schema.prisma');
if (!folder) {
folder = path.dirname(zmodelPath);
}
const prismaSchemaFile = path.resolve(folder, '~schema.prisma');
fs.writeFileSync(prismaSchemaFile, prismaSchema);
return prismaSchemaFile;
}
Expand All @@ -83,3 +85,28 @@ export function getPkgJsonConfig(startPath: string) {

return result;
}

type FindUpResult<Multiple extends boolean> = Multiple extends true ? string[] | undefined : string | undefined;

function findUp<Multiple extends boolean = false>(
names: string[],
cwd: string = process.cwd(),
multiple: Multiple = false as Multiple,
result: string[] = [],
): FindUpResult<Multiple> {
if (!names.some((name) => !!name)) {
return undefined;
}
const target = names.find((name) => fs.existsSync(path.join(cwd, name)));
if (multiple === false && target) {
return path.join(cwd, target) as FindUpResult<Multiple>;
}
if (target) {
result.push(path.join(cwd, target));
}
const up = path.resolve(cwd, '..');
if (up === cwd) {
return (multiple && result.length > 0 ? result : undefined) as FindUpResult<Multiple>;
}
return findUp(names, up, multiple, result);
}
2 changes: 1 addition & 1 deletion packages/cli/src/actions/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export async function run(options: Options) {

// generate TS schema
const tsSchemaFile = path.join(outputPath, 'schema.ts');
await new TsSchemaGenerator().generate(schemaFile, [], tsSchemaFile);
await new TsSchemaGenerator().generate(schemaFile, [], outputPath);

await runPlugins(model, outputPath, tsSchemaFile);

Expand Down
5 changes: 4 additions & 1 deletion packages/cli/src/actions/migrate.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import fs from 'node:fs';
import path from 'node:path';
import { execPackage } from '../utils/exec-utils';
import { generateTempPrismaSchema, getSchemaFile } from './action-utils';

type CommonOptions = {
schema?: string;
migrations?: string;
};

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

try {
switch (command) {
Expand Down
5 changes: 5 additions & 0 deletions packages/cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,31 +60,36 @@ export function createProgram() {
.action(generateAction);

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

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

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

migrateCommand
.command('deploy')
.addOption(schemaOption)
.addOption(migrationsOption)
.description('Deploy your pending migrations to your production/staging database.')
.action((options) => migrateAction('deploy', options));

migrateCommand
.command('status')
.addOption(schemaOption)
.addOption(migrationsOption)
.description('check the status of your database migrations.')
.action((options) => migrateAction('status', options));

Expand Down
2 changes: 1 addition & 1 deletion packages/common-helpers/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@zenstackhq/common-helpers",
"version": "3.0.0-alpha.10",
"version": "3.0.0-alpha.11",
"description": "ZenStack Common Helpers",
"type": "module",
"scripts": {
Expand Down
34 changes: 0 additions & 34 deletions packages/common-helpers/src/find-up.ts

This file was deleted.

1 change: 0 additions & 1 deletion packages/common-helpers/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export * from './find-up';
export * from './is-plain-object';
export * from './lower-case-first';
export * from './param-case';
Expand Down
2 changes: 1 addition & 1 deletion packages/create-zenstack/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "create-zenstack",
"version": "3.0.0-alpha.10",
"version": "3.0.0-alpha.11",
"description": "Create a new ZenStack project",
"type": "module",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion packages/eslint-config/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@zenstackhq/eslint-config",
"version": "3.0.0-alpha.10",
"version": "3.0.0-alpha.11",
"type": "module",
"private": true,
"license": "MIT"
Expand Down
2 changes: 1 addition & 1 deletion packages/ide/vscode/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "zenstack",
"publisher": "zenstack",
"version": "3.0.0-alpha.10",
"version": "3.0.0-alpha.11",
"displayName": "ZenStack Language Tools",
"description": "VSCode extension for ZenStack ZModel language",
"private": true,
Expand Down
2 changes: 1 addition & 1 deletion packages/language/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@zenstackhq/language",
"description": "ZenStack ZModel language specification",
"version": "3.0.0-alpha.10",
"version": "3.0.0-alpha.11",
"license": "MIT",
"author": "ZenStack Team",
"files": [
Expand Down
2 changes: 1 addition & 1 deletion packages/runtime/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@zenstackhq/runtime",
"version": "3.0.0-alpha.10",
"version": "3.0.0-alpha.11",
"description": "ZenStack Runtime",
"type": "module",
"scripts": {
Expand Down
30 changes: 15 additions & 15 deletions packages/runtime/src/client/contract.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Decimal } from 'decimal.js';
import { type GetModels, type ProcedureDef, type SchemaDef } from '../schema';
import type { AuthType } from '../schema/auth';
import type { OrUndefinedIf, UnwrapTuplePromises } from '../utils/type-utils';
import type { OrUndefinedIf, Simplify, UnwrapTuplePromises } from '../utils/type-utils';
import type { TRANSACTION_UNSUPPORTED_METHODS } from './constants';
import type {
AggregateArgs,
Expand Down Expand Up @@ -299,7 +299,7 @@ export interface ModelOperations<Schema extends SchemaDef, Model extends GetMode
*/
findMany<T extends FindArgs<Schema, Model, true>>(
args?: SelectSubset<T, FindArgs<Schema, Model, true>>,
): ZenStackPromise<Schema, ModelResult<Schema, Model, T>[]>;
): ZenStackPromise<Schema, Simplify<ModelResult<Schema, Model, T>>[]>;

/**
* Returns a uniquely identified entity.
Expand All @@ -309,7 +309,7 @@ export interface ModelOperations<Schema extends SchemaDef, Model extends GetMode
*/
findUnique<T extends FindUniqueArgs<Schema, Model>>(
args?: SelectSubset<T, FindUniqueArgs<Schema, Model>>,
): ZenStackPromise<Schema, ModelResult<Schema, Model, T> | null>;
): ZenStackPromise<Schema, Simplify<ModelResult<Schema, Model, T>> | null>;

/**
* Returns a uniquely identified entity or throws `NotFoundError` if not found.
Expand All @@ -319,7 +319,7 @@ export interface ModelOperations<Schema extends SchemaDef, Model extends GetMode
*/
findUniqueOrThrow<T extends FindUniqueArgs<Schema, Model>>(
args?: SelectSubset<T, FindUniqueArgs<Schema, Model>>,
): ZenStackPromise<Schema, ModelResult<Schema, Model, T>>;
): ZenStackPromise<Schema, Simplify<ModelResult<Schema, Model, T>>>;

/**
* Returns the first entity.
Expand All @@ -329,7 +329,7 @@ export interface ModelOperations<Schema extends SchemaDef, Model extends GetMode
*/
findFirst<T extends FindArgs<Schema, Model, true>>(
args?: SelectSubset<T, FindArgs<Schema, Model, true>>,
): ZenStackPromise<Schema, ModelResult<Schema, Model, T> | null>;
): ZenStackPromise<Schema, Simplify<ModelResult<Schema, Model, T>> | null>;

/**
* Returns the first entity or throws `NotFoundError` if not found.
Expand All @@ -339,7 +339,7 @@ export interface ModelOperations<Schema extends SchemaDef, Model extends GetMode
*/
findFirstOrThrow<T extends FindArgs<Schema, Model, true>>(
args?: SelectSubset<T, FindArgs<Schema, Model, true>>,
): ZenStackPromise<Schema, ModelResult<Schema, Model, T>>;
): ZenStackPromise<Schema, Simplify<ModelResult<Schema, Model, T>>>;

/**
* Creates a new entity.
Expand Down Expand Up @@ -395,7 +395,7 @@ export interface ModelOperations<Schema extends SchemaDef, Model extends GetMode
*/
create<T extends CreateArgs<Schema, Model>>(
args: SelectSubset<T, CreateArgs<Schema, Model>>,
): ZenStackPromise<Schema, ModelResult<Schema, Model, T>>;
): ZenStackPromise<Schema, Simplify<ModelResult<Schema, Model, T>>>;

/**
* Creates multiple entities. Only scalar fields are allowed.
Expand Down Expand Up @@ -446,7 +446,7 @@ export interface ModelOperations<Schema extends SchemaDef, Model extends GetMode
*/
createManyAndReturn<T extends CreateManyAndReturnArgs<Schema, Model>>(
args?: SelectSubset<T, CreateManyAndReturnArgs<Schema, Model>>,
): ZenStackPromise<Schema, ModelResult<Schema, Model, T>[]>;
): ZenStackPromise<Schema, Simplify<ModelResult<Schema, Model, T>>[]>;

/**
* Updates a uniquely identified entity.
Expand Down Expand Up @@ -567,7 +567,7 @@ export interface ModelOperations<Schema extends SchemaDef, Model extends GetMode
*/
update<T extends UpdateArgs<Schema, Model>>(
args: SelectSubset<T, UpdateArgs<Schema, Model>>,
): ZenStackPromise<Schema, ModelResult<Schema, Model, T>>;
): ZenStackPromise<Schema, Simplify<ModelResult<Schema, Model, T>>>;

/**
* Updates multiple entities.
Expand Down Expand Up @@ -617,7 +617,7 @@ export interface ModelOperations<Schema extends SchemaDef, Model extends GetMode
*/
updateManyAndReturn<T extends UpdateManyAndReturnArgs<Schema, Model>>(
args: Subset<T, UpdateManyAndReturnArgs<Schema, Model>>,
): ZenStackPromise<Schema, ModelResult<Schema, Model, T>[]>;
): ZenStackPromise<Schema, Simplify<ModelResult<Schema, Model, T>>[]>;

/**
* Creates or updates an entity.
Expand All @@ -641,7 +641,7 @@ export interface ModelOperations<Schema extends SchemaDef, Model extends GetMode
*/
upsert<T extends UpsertArgs<Schema, Model>>(
args: SelectSubset<T, UpsertArgs<Schema, Model>>,
): ZenStackPromise<Schema, ModelResult<Schema, Model, T>>;
): ZenStackPromise<Schema, Simplify<ModelResult<Schema, Model, T>>>;

/**
* Deletes a uniquely identifiable entity.
Expand All @@ -664,7 +664,7 @@ export interface ModelOperations<Schema extends SchemaDef, Model extends GetMode
*/
delete<T extends DeleteArgs<Schema, Model>>(
args: SelectSubset<T, DeleteArgs<Schema, Model>>,
): ZenStackPromise<Schema, ModelResult<Schema, Model>>;
): ZenStackPromise<Schema, Simplify<ModelResult<Schema, Model>>>;

/**
* Deletes multiple entities.
Expand Down Expand Up @@ -709,7 +709,7 @@ export interface ModelOperations<Schema extends SchemaDef, Model extends GetMode
*/
count<T extends CountArgs<Schema, Model>>(
args?: Subset<T, CountArgs<Schema, Model>>,
): ZenStackPromise<Schema, CountResult<Schema, Model, T>>;
): ZenStackPromise<Schema, Simplify<CountResult<Schema, Model, T>>>;

/**
* Aggregates rows.
Expand All @@ -730,7 +730,7 @@ export interface ModelOperations<Schema extends SchemaDef, Model extends GetMode
*/
aggregate<T extends AggregateArgs<Schema, Model>>(
args: Subset<T, AggregateArgs<Schema, Model>>,
): ZenStackPromise<Schema, AggregateResult<Schema, Model, T>>;
): ZenStackPromise<Schema, Simplify<AggregateResult<Schema, Model, T>>>;

/**
* Groups rows by columns.
Expand Down Expand Up @@ -766,7 +766,7 @@ export interface ModelOperations<Schema extends SchemaDef, Model extends GetMode
*/
groupBy<T extends GroupByArgs<Schema, Model>>(
args: Subset<T, GroupByArgs<Schema, Model>>,
): ZenStackPromise<Schema, GroupByResult<Schema, Model, T>>;
): ZenStackPromise<Schema, Simplify<GroupByResult<Schema, Model, T>>>;
}

//#endregion
Loading