Skip to content

merge dev to main #617

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Aug 5, 2023
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
14 changes: 14 additions & 0 deletions .github/workflows/build-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,20 @@ jobs:
build-test:
runs-on: buildjet-8vcpu-ubuntu-2204

services:
postgres:
image: postgres
env:
POSTGRES_PASSWORD: abc123
# Set health checks to wait until postgres has started
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
ports:
- 5432:5432

strategy:
matrix:
node-version: [18.x]
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"name": "zenstack-monorepo",
"version": "1.0.0-beta.13",
"version": "1.0.0-beta.15",
"description": "",
"scripts": {
"build": "pnpm -r build",
"test": "ZENSTACK_TEST=1 pnpm -r run test --silent",
"test-ci": "ZENSTACK_TEST=1 pnpm -r run test --silent",
"test": "ZENSTACK_TEST=1 pnpm -r run test --silent --forceExit",
"test-ci": "ZENSTACK_TEST=1 pnpm -r run test --silent --forceExit",
"lint": "pnpm -r lint",
"publish-all": "pnpm --filter \"./packages/**\" -r publish --access public",
"publish-preview": "pnpm --filter \"./packages/**\" -r publish --force --registry http://localhost:4873"
Expand Down
2 changes: 1 addition & 1 deletion packages/language/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@zenstackhq/language",
"version": "1.0.0-beta.13",
"version": "1.0.0-beta.15",
"displayName": "ZenStack modeling language compiler",
"description": "ZenStack modeling language compiler",
"homepage": "https://zenstack.dev",
Expand Down
2 changes: 1 addition & 1 deletion packages/plugins/openapi/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@zenstackhq/openapi",
"displayName": "ZenStack Plugin and Runtime for OpenAPI",
"version": "1.0.0-beta.13",
"version": "1.0.0-beta.15",
"description": "ZenStack plugin and runtime supporting OpenAPI",
"main": "index.js",
"repository": {
Expand Down
2 changes: 1 addition & 1 deletion packages/plugins/swr/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@zenstackhq/swr",
"displayName": "ZenStack plugin for generating SWR hooks",
"version": "1.0.0-beta.13",
"version": "1.0.0-beta.15",
"description": "ZenStack plugin for generating SWR hooks",
"main": "index.js",
"repository": {
Expand Down
2 changes: 1 addition & 1 deletion packages/plugins/tanstack-query/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@zenstackhq/tanstack-query",
"displayName": "ZenStack plugin for generating tanstack-query hooks",
"version": "1.0.0-beta.13",
"version": "1.0.0-beta.15",
"description": "ZenStack plugin for generating tanstack-query hooks",
"main": "index.js",
"exports": {
Expand Down
2 changes: 1 addition & 1 deletion packages/plugins/trpc/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@zenstackhq/trpc",
"displayName": "ZenStack plugin for tRPC",
"version": "1.0.0-beta.13",
"version": "1.0.0-beta.15",
"description": "ZenStack plugin for tRPC",
"main": "index.js",
"repository": {
Expand Down
3 changes: 1 addition & 2 deletions packages/runtime/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@zenstackhq/runtime",
"displayName": "ZenStack Runtime Library",
"version": "1.0.0-beta.13",
"version": "1.0.0-beta.15",
"description": "Runtime of ZenStack for both client-side and server-side environments.",
"repository": {
"type": "git",
Expand Down Expand Up @@ -45,7 +45,6 @@
"linkDirectory": true
},
"dependencies": {
"@paralleldrive/cuid2": "^2.2.0",
"@types/bcryptjs": "^2.4.2",
"bcryptjs": "^2.4.3",
"buffer": "^6.0.3",
Expand Down
25 changes: 24 additions & 1 deletion packages/runtime/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,38 @@ export enum CrudFailureReason {
* Prisma error codes used
*/
export enum PrismaErrorCode {
/**
* Unique constraint failed
*/
UNIQUE_CONSTRAINT_FAILED = 'P2002',

/**
* A constraint failed on the database
*/
CONSTRAINED_FAILED = 'P2004',

/**
* The required connected records were not found
*/
REQUIRED_CONNECTED_RECORD_NOT_FOUND = 'P2018',

/**
* An operation failed because it depends on one or more records that were required but not found
*/
DEPEND_ON_RECORD_NOT_FOUND = 'P2025',
}

/**
* Field name for storing in-transaction flag
*/
export const PRISIMA_TX_FLAG = '$__zenstack_tx';
export const PRISMA_TX_FLAG = '$__zenstack_tx';

/**
* Field name for getting current enhancer
*/
export const PRISMA_PROXY_ENHANCER = '$__zenstack_enhancer';

/**
* Minimum Prisma version supported
*/
export const PRISMA_MINIMUM_VERSION = '4.8.0';
43 changes: 43 additions & 0 deletions packages/runtime/src/enhancements/model-data-visitor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { resolveField } from './model-meta';
import { ModelMeta } from './types';

/**
* Callback for @see ModelDataVisitor.
*/
export type ModelDataVisitorCallback = (model: string, data: any, scalarData: any) => void;

/**
* Visitor that traverses data returned by a Prisma query.
*/
export class ModelDataVisitor {
constructor(private modelMeta: ModelMeta) {}

/**
* Visits the given model data.
*/
visit(model: string, data: any, callback: ModelDataVisitorCallback) {
if (!data || typeof data !== 'object') {
return;
}

const scalarData: Record<string, unknown> = {};
const subTasks: Array<{ model: string; data: any }> = [];

for (const [k, v] of Object.entries(data)) {
const field = resolveField(this.modelMeta, model, k);
if (field && field.isDataModel) {
if (field.isArray && Array.isArray(v)) {
subTasks.push(...v.map((item) => ({ model: field.type, data: item })));
} else {
subTasks.push({ model: field.type, data: v });
}
} else {
scalarData[k] = v;
}
}

callback(model, data, scalarData);
subTasks.forEach(({ model, data }) => this.visit(model, data, callback));
}
}
3 changes: 2 additions & 1 deletion packages/runtime/src/enhancements/model-meta.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable @typescript-eslint/no-var-requires */
import { lowerCaseFirst } from 'lower-case-first';
import path from 'path';
import { FieldInfo } from '../types';
import { ModelMeta } from './types';

/**
Expand All @@ -26,7 +27,7 @@ export function getDefaultModelMeta(): ModelMeta {
/**
* Resolves a model field to its metadata. Returns undefined if not found.
*/
export function resolveField(modelMeta: ModelMeta, model: string, field: string) {
export function resolveField(modelMeta: ModelMeta, model: string, field: string): FieldInfo | undefined {
return modelMeta.fields[lowerCaseFirst(model)][field];
}

Expand Down
Loading