Skip to content

merge dev to main #458

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 2 commits into from
Jun 4, 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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "zenstack-monorepo",
"version": "1.0.0-alpha.124",
"version": "1.0.0-alpha.125",
"description": "",
"scripts": {
"build": "pnpm -r build",
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-alpha.124",
"version": "1.0.0-alpha.125",
"displayName": "ZenStack modeling language compiler",
"description": "ZenStack modeling language compiler",
"homepage": "https://zenstack.dev",
Expand Down
2 changes: 1 addition & 1 deletion packages/next/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@zenstackhq/next",
"version": "1.0.0-alpha.124",
"version": "1.0.0-alpha.125",
"displayName": "ZenStack Next.js integration",
"description": "ZenStack Next.js integration",
"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-alpha.124",
"version": "1.0.0-alpha.125",
"description": "ZenStack plugin and runtime supporting OpenAPI",
"main": "index.js",
"repository": {
Expand Down
2 changes: 1 addition & 1 deletion packages/plugins/react/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@zenstackhq/react",
"displayName": "ZenStack plugin and runtime for ReactJS",
"version": "1.0.0-alpha.124",
"version": "1.0.0-alpha.125",
"description": "ZenStack plugin and runtime for ReactJS",
"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-alpha.124",
"version": "1.0.0-alpha.125",
"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-alpha.124",
"version": "1.0.0-alpha.125",
"description": "ZenStack plugin for generating tanstack-query hooks",
"main": "index.js",
"repository": {
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-alpha.124",
"version": "1.0.0-alpha.125",
"description": "ZenStack plugin for tRPC",
"main": "index.js",
"repository": {
Expand Down
2 changes: 1 addition & 1 deletion 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-alpha.124",
"version": "1.0.0-alpha.125",
"description": "Runtime of ZenStack for both client-side and server-side environments.",
"repository": {
"type": "git",
Expand Down
2 changes: 1 addition & 1 deletion packages/schema/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"publisher": "zenstack",
"displayName": "ZenStack Language Tools",
"description": "A toolkit for building secure CRUD apps with Next.js + Typescript",
"version": "1.0.0-alpha.124",
"version": "1.0.0-alpha.125",
"author": {
"name": "ZenStack Team"
},
Expand Down
14 changes: 12 additions & 2 deletions packages/schema/src/cli/cli-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { getLiteral, PluginError } from '@zenstackhq/sdk';
import colors from 'colors';
import fs from 'fs';
import getLatestVersion from 'get-latest-version';
import { getDocument, LangiumDocument, LangiumDocuments } from 'langium';
import { AstNode, getDocument, LangiumDocument, LangiumDocuments, Mutable } from 'langium';
import { NodeFileSystem } from 'langium/node';
import ora from 'ora';
import path from 'path';
Expand Down Expand Up @@ -193,7 +193,17 @@ export function eagerLoadAllImports(

export function mergeImportsDeclarations(documents: LangiumDocuments, model: Model) {
const importedModels = resolveTransitiveImports(documents, model);
model.declarations.push(...importedModels.flatMap((m) => m.declarations));

const importedDeclarations = importedModels.flatMap((m) => m.declarations);

importedDeclarations.forEach((d) => {
const mutable = d as Mutable<AstNode>;
// The plugin might use $container to access the model
// need to make sure it is always resolved to the main model
mutable.$container = model;
});

model.declarations.push(...importedDeclarations);
}

export async function getPluginDocuments(services: ZModelServices, fileName: string): Promise<LangiumDocument[]> {
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@zenstackhq/sdk",
"version": "1.0.0-alpha.124",
"version": "1.0.0-alpha.125",
"description": "ZenStack plugin development SDK",
"main": "index.js",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion packages/server/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@zenstackhq/server",
"version": "1.0.0-alpha.124",
"version": "1.0.0-alpha.125",
"displayName": "ZenStack Server-side Adapters",
"description": "ZenStack server-side adapters",
"homepage": "https://zenstack.dev",
Expand Down
2 changes: 1 addition & 1 deletion packages/testtools/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@zenstackhq/testtools",
"version": "1.0.0-alpha.124",
"version": "1.0.0-alpha.125",
"description": "ZenStack Test Tools",
"main": "index.js",
"publishConfig": {
Expand Down
53 changes: 45 additions & 8 deletions packages/testtools/src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,18 @@ import tmp from 'tmp';
import { loadDocument } from 'zenstack/cli/cli-util';
import prismaPlugin from 'zenstack/plugins/prisma';

/**
* Use it to represent multiple files in a single string like this
`schema.zmodel
import "user"
${FILE_SPLITTER}user.zmodel
import "schema"
model User {
...
}`
*/
export const FILE_SPLITTER = '#FILE_SPLITTER#';

export type WeakDbOperations = {
[key in keyof DbOperations]: (...args: any[]) => Promise<any>;
};
Expand Down Expand Up @@ -112,17 +124,42 @@ export async function loadSchema(
console.log('Workdir:', projectRoot);
process.chdir(projectRoot);

schema = schema.replaceAll('$projectRoot', projectRoot);

let zmodelPath = path.join(projectRoot, 'schema.zmodel');
const content = addPrelude ? `${MODEL_PRELUDE}\n${schema}` : schema;
if (customSchemaFilePath) {
zmodelPath = path.join(projectRoot, customSchemaFilePath);
fs.mkdirSync(path.dirname(zmodelPath), { recursive: true });
fs.writeFileSync(zmodelPath, content);

const files = schema.split(FILE_SPLITTER);

if (files.length > 1) {
// multiple files
files.forEach((file, index) => {
//first line is the file name
const firstLine = file.indexOf('\n');
const fileName = file.substring(0, firstLine).trim();
let fileContent = file.substring(firstLine + 1);
if (index === 0) {
// The first file is the main schema file
zmodelPath = path.join(projectRoot, fileName);
if (addPrelude) {
// plugin need to be added after import statement
fileContent = `${fileContent}\n${MODEL_PRELUDE}`;
}
}

fileContent = fileContent.replaceAll('$projectRoot', projectRoot);
const filePath = path.join(projectRoot, fileName);
fs.writeFileSync(filePath, fileContent);
});
} else {
fs.writeFileSync('schema.zmodel', content);
schema = schema.replaceAll('$projectRoot', projectRoot);
const content = addPrelude ? `${MODEL_PRELUDE}\n${schema}` : schema;
if (customSchemaFilePath) {
zmodelPath = path.join(projectRoot, customSchemaFilePath);
fs.mkdirSync(path.dirname(zmodelPath), { recursive: true });
fs.writeFileSync(zmodelPath, content);
} else {
fs.writeFileSync('schema.zmodel', content);
}
}

run('npm install');

if (customSchemaFilePath) {
Expand Down
4 changes: 2 additions & 2 deletions tests/integration/test-run/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 32 additions & 0 deletions tests/integration/tests/with-policy/multi-file.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { loadSchema, FILE_SPLITTER } from '@zenstackhq/testtools';

describe('Multiple file test', () => {
it('model loading', async () => {
await loadSchema(
`schema.zmodel
import "post"
${FILE_SPLITTER}post.zmodel
import "user"
model Post {
id Int @id @default(autoincrement())
owner User @relation(fields: [ownerId], references: [id])
ownerId Int

// require login
@@deny('all', auth() == null)

// can be read by owner or space members (only if not private)
@@allow('all', owner == auth())
}
${FILE_SPLITTER}user.zmodel
import "post"
model User {
id Int @id @default(autoincrement())
name String
email String @unique
posts Post[]
}
`
);
});
});