-
-
Notifications
You must be signed in to change notification settings - Fork 12
merge dev to main (v3.0.0-alpha.17) #137
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
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
2d917f3
chore: unify vitest config and minor fixes in CLI (#133)
ymc9 a2aff81
feat: add sql.js dialect (#134)
ymc9 47b110a
chore: make datasource url optional (#135)
ymc9 990584c
chore: bump version 3.0.0-alpha.17 (#136)
github-actions[bot] aeffd32
readme update (#138)
ymc9 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -118,7 +118,7 @@ npm install -D @types/pg | |
| Run the following command to sync schema to the database for local development: | ||
|
|
||
| ```bash | ||
| npx zenstack db push | ||
| npx zen db push | ||
| ``` | ||
|
|
||
| > Under the hood, the command uses `prisma db push` to do the job. | ||
|
|
@@ -127,17 +127,17 @@ See [database migration](#database-migration) for how to use migration to manage | |
|
|
||
| ## Compiling ZModel schema | ||
|
|
||
| ZModel needs to be compiled to TypeScript before being used to create a database client. Simply run the following command: | ||
| ZModel needs to be compiled to TypeScript before being used to create a database db. Simply run the following command: | ||
|
|
||
| ```bash | ||
| npx zenstack generate | ||
| npx zen generate | ||
| ``` | ||
|
|
||
| A `schema.ts` file will be created inside the `zenstack` folder. The file should be included as part of your source tree for compilation/bundling. You may choose to include or ignore it in source control (and generate on the fly during build). Just remember to rerun the "generate" command whenever you make changes to the ZModel schema. | ||
|
|
||
| ## Creating ZenStack client | ||
|
|
||
| Now you can use the compiled TypeScript schema to instantiate a database client. | ||
| Now you can use the compiled TypeScript schema to instantiate a database db. | ||
|
|
||
| ### SQLite | ||
|
|
||
|
|
@@ -147,7 +147,7 @@ import { schema } from './zenstack/schema'; | |
| import SQLite from 'better-sqlite3'; | ||
| import { SqliteDialect } from 'kysely'; | ||
|
|
||
| const client = new ZenStackClient(schema, { | ||
| const db = new ZenStackClient(schema, { | ||
| dialect: new SqliteDialect({ database: new SQLite('./dev.db') }), | ||
| }); | ||
| ``` | ||
|
|
@@ -159,11 +159,10 @@ import { ZenStackClient } from '@zenstackhq/runtime'; | |
| import { schema } from './zenstack/schema'; | ||
| import { PostgresDialect } from 'kysely'; | ||
| import { Pool } from 'pg'; | ||
| import { parseIntoClientConfig } from 'pg-connection-string'; | ||
|
|
||
| const client = new ZenStackClient(schema, { | ||
| const db = new ZenStackClient(schema, { | ||
| dialect: new PostgresDialect({ | ||
| pool: new Pool(parseIntoClientConfig(process.env.DATABASE_URL)), | ||
| pool: new Pool({ connectionString: process.env.DATABASE_URL }), | ||
| }), | ||
| }); | ||
| ``` | ||
|
|
@@ -177,20 +176,20 @@ const client = new ZenStackClient(schema, { | |
| A few quick examples: | ||
|
|
||
| ```ts | ||
| const user = await client.user.create({ | ||
| const user = await db.user.create({ | ||
| data: { | ||
| name: 'Alex', | ||
| email: '[email protected]', | ||
| posts: { create: { title: 'Hello world' } }, | ||
| }, | ||
| }); | ||
|
|
||
| const userWithPosts = await client.user.findUnique({ | ||
| const userWithPosts = await db.user.findUnique({ | ||
| where: { id: user.id }, | ||
| include: { posts: true }, | ||
| }); | ||
|
|
||
| const groupedPosts = await client.post.groupBy({ | ||
| const groupedPosts = await db.post.groupBy({ | ||
| by: 'published', | ||
| _count: true, | ||
| }); | ||
|
|
@@ -205,7 +204,7 @@ ZenStack uses Kysely to handle database operations, and it also directly exposes | |
| Please check [Kysely documentation](https://kysely.dev/docs/intro) for more details. Here're a few quick examples: | ||
|
|
||
| ```ts | ||
| await client.$qb | ||
| await db.$qb | ||
| .selectFrom('User') | ||
| .leftJoin('Post', 'Post.authorId', 'User.id') | ||
| .select(['User.id', 'User.email', 'Post.title']) | ||
|
|
@@ -215,7 +214,7 @@ await client.$qb | |
| Query builder can also be "blended" into ORM API calls as a local escape hatch for building complex filter conditions. It allows for greater flexibility without forcing you to entirely resort to the query builder API. | ||
|
|
||
| ```ts | ||
| await client.user.findMany({ | ||
| await db.user.findMany({ | ||
| where: { | ||
| age: { gt: 18 }, | ||
| // "eb" is a Kysely expression builder | ||
|
|
@@ -243,7 +242,7 @@ ZenStack v3 allows you to define database-evaluated computed fields with the fol | |
| 2. Provide its implementation using query builder when constructing `ZenStackClient` | ||
|
|
||
| ```ts | ||
| const client = new ZenStackClient(schema, { | ||
| const db = new ZenStackClient(schema, { | ||
| ... | ||
| computedFields: { | ||
| User: { | ||
|
|
@@ -279,7 +278,7 @@ _Coming soon..._ | |
|
|
||
| ### Runtime plugins | ||
|
|
||
| V3 introduces a new runtime plugin mechanism that allows you to tap into the ORM's query execution in various ways. A plugin implements the [RuntimePlugin](./packages/runtime/src/client/plugin.ts#L121) interface, and can be installed with the `ZenStackClient.$use` API. | ||
| V3 introduces a new runtime plugin mechanism that allows you to tap into the ORM's query execution in various ways. A plugin implements the [RuntimePlugin](./packages/runtime/src/client/plugin.ts#L121) interface, and can be installed with the `ZenStackdb.$use` API. | ||
|
|
||
| You can use a plugin to achieve the following goals: | ||
|
|
||
|
|
@@ -288,7 +287,7 @@ You can use a plugin to achieve the following goals: | |
| ORM query interception allows you to intercept the high-level ORM API calls. The interceptor's configuration is compatible with Prisma's [query client extension](https://www.prisma.io/docs/orm/prisma-client/client-extensions/query). | ||
|
|
||
| ```ts | ||
| client.$use({ | ||
| db.$use({ | ||
| id: 'cost-logger', | ||
| onQuery: { | ||
| $allModels: { | ||
|
|
@@ -312,7 +311,7 @@ Kysely query interception allows you to intercept the low-level query builder AP | |
| Kysely query interception works against the low-level Kysely `OperationNode` structures. It's harder to implement but can guarantee intercepting all CRUD operations. | ||
|
|
||
| ```ts | ||
| client.$use({ | ||
| db.$use({ | ||
| id: 'insert-interception-plugin', | ||
| onKyselyQuery({query, proceed}) { | ||
| if (query.kind === 'InsertQueryNode') { | ||
|
|
@@ -332,7 +331,7 @@ function sanitizeInsertData(query: InsertQueryNode) { | |
| Another popular interception use case is, instead of intercepting calls, "listening on" entity changes. | ||
|
|
||
| ```ts | ||
| client.$use({ | ||
| db.$use({ | ||
| id: 'mutation-hook-plugin', | ||
| beforeEntityMutation({ model, action }) { | ||
| console.log(`Before ${model} ${action}`); | ||
|
|
@@ -346,7 +345,7 @@ client.$use({ | |
| You can provide an extra `mutationInterceptionFilter` to control what to intercept, and opt in for loading the affected entities before and/or after the mutation. | ||
|
|
||
| ```ts | ||
| client.$use({ | ||
| db.$use({ | ||
| id: 'mutation-hook-plugin', | ||
| mutationInterceptionFilter: ({ model }) => { | ||
| return { | ||
|
|
@@ -375,19 +374,19 @@ ZenStack v3 delegates database schema migration to Prisma. The CLI provides Pris | |
| - Sync schema to dev database and create a migration record: | ||
|
|
||
| ```bash | ||
| npx zenstack migrate dev | ||
| npx zen migrate dev | ||
| ``` | ||
|
|
||
| - Deploy new migrations: | ||
|
|
||
| ```bash | ||
| npx zenstack migrate deploy | ||
| npx zen migrate deploy | ||
| ``` | ||
|
|
||
| - Reset dev database | ||
|
|
||
| ```bash | ||
| npx zenstack migrate reset | ||
| npx zen migrate reset | ||
| ``` | ||
|
|
||
| See [Prisma Migrate](https://www.prisma.io/docs/orm/prisma-migrate) documentation for more details. | ||
|
|
@@ -398,7 +397,7 @@ See [Prisma Migrate](https://www.prisma.io/docs/orm/prisma-migrate) documentatio | |
| 1. Remove "@prisma/client" dependency | ||
| 1. Install "better-sqlite3" or "pg" based on database type | ||
| 1. Move "schema.prisma" to "zenstack" folder and rename it to "schema.zmodel" | ||
| 1. Run `npx zenstack generate` | ||
| 1. Run `npx zen generate` | ||
| 1. Replace `new PrismaClient()` with `new ZenStackClient(schema, { ... })` | ||
|
|
||
| # Limitations | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| { | ||
| "name": "zenstack-v3", | ||
| "version": "3.0.0-alpha.16", | ||
| "version": "3.0.0-alpha.17", | ||
| "description": "ZenStack", | ||
| "packageManager": "[email protected]", | ||
| "scripts": { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| import base from '@zenstackhq/vitest-config/base'; | ||
| import { defineConfig, mergeConfig } from 'vitest/config'; | ||
| import base from '../../vitest.base.config'; | ||
|
|
||
| export default mergeConfig(base, defineConfig({})); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| Forked from https://github.com/betarixm/kysely-sql-js | ||
|
|
||
| ## Usage | ||
|
|
||
| ```ts | ||
| import { type GeneratedAlways, Kysely } from 'kysely'; | ||
| import initSqlJs from 'sql.js'; | ||
|
|
||
| import { SqlJsDialect } from '@zenstackhq/kysely-sql-js'; | ||
|
|
||
| interface Database { | ||
| person: { | ||
| id: GeneratedAlways<number>; | ||
| first_name: string | null; | ||
| last_name: string | null; | ||
| age: number; | ||
| }; | ||
| } | ||
|
|
||
| const SqlJsStatic = await initSqlJs(); | ||
|
|
||
| export const db = new Kysely<Database>({ | ||
| dialect: new SqlJsDialect({ sqlJs: new SqlJsStatic.Database() }), | ||
| }); | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| import config from '@zenstackhq/eslint-config/base.js'; | ||
|
|
||
| /** @type {import("eslint").Linter.Config} */ | ||
| export default config; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| { | ||
| "name": "@zenstackhq/kysely-sql-js", | ||
| "version": "3.0.0-alpha.17", | ||
| "description": "Kysely dialect for sql.js", | ||
| "type": "module", | ||
| "scripts": { | ||
| "build": "tsup-node", | ||
| "watch": "tsup-node --watch", | ||
| "lint": "eslint src --ext ts", | ||
| "pack": "pnpm pack" | ||
| }, | ||
| "keywords": [], | ||
| "author": "ZenStack Team", | ||
| "license": "MIT", | ||
| "files": [ | ||
| "dist" | ||
| ], | ||
| "exports": { | ||
| ".": { | ||
| "import": { | ||
| "types": "./dist/index.d.ts", | ||
| "default": "./dist/index.js" | ||
| }, | ||
| "require": { | ||
| "types": "./dist/index.d.cts", | ||
| "default": "./dist/index.cjs" | ||
| } | ||
| } | ||
| }, | ||
| "devDependencies": { | ||
| "@types/sql.js": "^1.4.9", | ||
| "@zenstackhq/eslint-config": "workspace:*", | ||
| "@zenstackhq/typescript-config": "workspace:*", | ||
| "@zenstackhq/vitest-config": "workspace:*", | ||
| "sql.js": "^1.13.0", | ||
| "kysely": "catalog:" | ||
| }, | ||
| "peerDependencies": { | ||
| "sql.js": "^1.13.0", | ||
| "kysely": "catalog:" | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.