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: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "zenstack-v3",
"displayName": "ZenStack",
"description": "ZenStack",
"version": "3.6.3",
"version": "3.6.4",
"type": "module",
"author": {
"name": "ZenStack Team",
Expand Down
2 changes: 1 addition & 1 deletion packages/auth-adapters/better-auth/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@zenstackhq/better-auth",
"displayName": "ZenStack Better Auth Adapter",
"description": "ZenStack Better Auth Adapter. This adapter is modified from better-auth's Prisma adapter.",
"version": "3.6.3",
"version": "3.6.4",
"type": "module",
"author": {
"name": "ZenStack Team",
Expand Down
28 changes: 4 additions & 24 deletions packages/auth-adapters/better-auth/src/adapter.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { BetterAuthOptions } from '@better-auth/core';
import type { DBAdapter, DBAdapterDebugLogOption, Where } from '@better-auth/core/db/adapter';
import type { DBAdapter, Where } from '@better-auth/core/db/adapter';
import { BetterAuthError } from '@better-auth/core/error';
import type { ClientContract, ModelOperations, UpdateInput } from '@zenstackhq/orm';
import type { GetModels, SchemaDef } from '@zenstackhq/orm/schema';
Expand All @@ -8,30 +8,9 @@ import {
type AdapterFactoryCustomizeAdapterCreator,
type AdapterFactoryOptions,
} from 'better-auth/adapters';
import { getSupportsArrays, type AdapterConfig } from './config';

/**
* Options for the ZenStack adapter factory.
*/
export interface AdapterConfig {
/**
* Database provider
*/
provider: 'sqlite' | 'postgresql';

/**
* Enable debug logs for the adapter
*
* @default false
*/
debugLogs?: DBAdapterDebugLogOption | undefined;

/**
* Use plural table names
*
* @default false
*/
usePlural?: boolean | undefined;
}
export type { AdapterConfig } from './config';

/**
* Create a Better-Auth adapter for ZenStack ORM.
Expand Down Expand Up @@ -220,6 +199,7 @@ export const zenstackAdapter = <Schema extends SchemaDef>(db: ClientContract<Sch
adapterName: 'ZenStack Adapter',
usePlural: config.usePlural ?? false,
debugLogs: config.debugLogs ?? false,
supportsArrays: getSupportsArrays(config),
transaction: (cb) =>
db.$transaction((tx) => {
const adapter = createAdapterFactory({
Expand Down
36 changes: 36 additions & 0 deletions packages/auth-adapters/better-auth/src/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import type { DBAdapterDebugLogOption } from '@better-auth/core/db/adapter';

/**
* Options for the ZenStack adapter factory.
*/
export interface AdapterConfig {
/**
* Database provider
*/
provider: 'sqlite' | 'postgresql';

/**
* Enable debug logs for the adapter
*
* @default false
*/
debugLogs?: DBAdapterDebugLogOption | undefined;

/**
* Use plural table names
*
* @default false
*/
usePlural?: boolean | undefined;

/**
* Preserve Better Auth array fields as native database arrays.
*
* Defaults to true for PostgreSQL and false for SQLite.
*/
supportsArrays?: boolean | undefined;
}

export function getSupportsArrays(config: AdapterConfig) {
return config.supportsArrays ?? config.provider === 'postgresql';
}
13 changes: 8 additions & 5 deletions packages/auth-adapters/better-auth/src/schema-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import type { DBAdapterSchemaCreation } from 'better-auth/adapters';
import type { BetterAuthDBSchema, DBFieldAttribute, DBFieldType } from 'better-auth/db';
import fs from 'node:fs';
import { match } from 'ts-pattern';
import type { AdapterConfig } from './adapter';
import { getSupportsArrays, type AdapterConfig } from './config';

export async function generateSchema(
file: string | undefined,
Expand Down Expand Up @@ -95,13 +95,15 @@ async function updateSchema(

let changed = false;

const supportsArrays = getSupportsArrays(config);
for (const [name, table] of Object.entries(tables)) {
const c = addOrUpdateModel(
name,
table,
zmodel,
tables,
toManyRelations,
supportsArrays,
!!options.advanced?.database?.useNumberId,
);
changed = changed || c;
Expand Down Expand Up @@ -251,15 +253,15 @@ function initializeZmodel(config: AdapterConfig) {
return zmodel;
}

function getMappedFieldType({ bigint, type }: DBFieldAttribute) {
function getMappedFieldType({ bigint, type }: DBFieldAttribute, supportsArrays: boolean) {
return match<DBFieldType, { type: string; array?: boolean }>(type)
.with('string', () => ({ type: 'String' }))
.with('number', () => (bigint ? { type: 'BigInt' } : { type: 'Int' }))
.with('boolean', () => ({ type: 'Boolean' }))
.with('date', () => ({ type: 'DateTime' }))
.with('json', () => ({ type: 'Json' }))
.with('string[]', () => ({ type: 'String', array: true }))
.with('number[]', () => ({ type: 'Int', array: true }))
.with('string[]', () => (supportsArrays ? { type: 'String', array: true } : { type: 'Json' }))
.with('number[]', () => (supportsArrays ? { type: 'Int', array: true } : { type: 'Json' }))
.when(
(v) => Array.isArray(v) && v.every((e) => typeof e === 'string'),
() => {
Expand All @@ -278,6 +280,7 @@ function addOrUpdateModel(
zmodel: Model,
tables: BetterAuthDBSchema,
toManyRelations: Map<string, Set<string>>,
supportsArrays: boolean,
numericId: boolean,
): boolean {
let changed = false;
Expand Down Expand Up @@ -305,7 +308,7 @@ function addOrUpdateModel(

if (!field.references) {
// scalar field
const { array, type } = getMappedFieldType(field);
const { array, type } = getMappedFieldType(field, supportsArrays);

const df: DataField = {
$type: 'DataField',
Expand Down
115 changes: 115 additions & 0 deletions packages/auth-adapters/better-auth/test/adapter.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import type { BetterAuthOptions } from '@better-auth/core';
import type { BetterAuthDBSchema } from 'better-auth/db';
import fs from 'node:fs';
import path from 'node:path';
import tmp from 'tmp';
import { describe, expect, it } from 'vitest';
import { type AdapterConfig, zenstackAdapter } from '../src/adapter';
import { generateSchema } from '../src/schema-generator';

const oauthClientSchema = {
oauthClient: {
modelName: 'oauthClient',
fields: {
name: {
type: 'string',
required: true,
},
scopes: {
type: 'string[]',
required: true,
},
retryDelays: {
type: 'number[]',
required: false,
},
},
},
} satisfies BetterAuthDBSchema;

function makeAuthOptions() {
return {
plugins: [
{
id: 'oauth-provider',
schema: oauthClientSchema,
},
],
} as unknown as BetterAuthOptions;
}

function makeDb(captured: { createData?: Record<string, unknown> }) {
return {
oauthClient: {
create: async ({ data }: { data: Record<string, unknown> }) => {
captured.createData = data;
return data;
},
},
$transaction: async <T>(cb: (tx: unknown) => Promise<T>) => cb(makeDb(captured)),
};
}

async function createOauthClient(config: AdapterConfig) {
const captured: { createData?: Record<string, unknown> } = {};
const adapter = zenstackAdapter(makeDb(captured) as any, config)(makeAuthOptions());

await adapter.create({
model: 'oauthClient',
data: {
id: 'client-1',
name: 'client',
scopes: ['openid', 'profile'],
retryDelays: [1, 2],
},
forceAllowId: true,
});

return captured.createData;
}

async function generateOauthClientSchema(config: AdapterConfig) {
const { name: workDir, removeCallback } = tmp.dirSync({ unsafeCleanup: true });
const schemaPath = path.join(workDir, 'schema.zmodel');

try {
const result = await generateSchema(schemaPath, oauthClientSchema, config, makeAuthOptions());
return result.code;
} finally {
if (fs.existsSync(workDir)) {
removeCallback();
}
}
}

describe('ZenStack Better Auth adapter', () => {
it('preserves native array inputs for PostgreSQL (#2615)', async () => {
const data = await createOauthClient({ provider: 'postgresql' });

expect(data?.scopes).toEqual(['openid', 'profile']);
expect(data?.retryDelays).toEqual([1, 2]);
});

it('serializes array inputs when native arrays are disabled (#2615)', async () => {
const data = await createOauthClient({ provider: 'postgresql', supportsArrays: false });

expect(data?.scopes).toBe(JSON.stringify(['openid', 'profile']));
expect(data?.retryDelays).toBe(JSON.stringify([1, 2]));
});

it('generates native array fields when the adapter supports arrays (#2615)', async () => {
const schema = await generateOauthClientSchema({ provider: 'postgresql' });

expect(schema).toMatch(/scopes\s+String\[\]/);
expect(schema).toMatch(/retryDelays\s+Int\[\]\?/);
});

it('generates JSON fields when the adapter does not support arrays (#2615)', async () => {
const schema = await generateOauthClientSchema({ provider: 'sqlite' });

expect(schema).toMatch(/scopes\s+Json/);
expect(schema).toMatch(/retryDelays\s+Json\?/);
expect(schema).not.toMatch(/scopes\s+String\[\]/);
expect(schema).not.toMatch(/retryDelays\s+Int\[\]/);
});
});
2 changes: 1 addition & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@zenstackhq/cli",
"displayName": "ZenStack CLI",
"description": "FullStack database toolkit with built-in access control and automatic API generation.",
"version": "3.6.3",
"version": "3.6.4",
"type": "module",
"author": {
"name": "ZenStack Team",
Expand Down
2 changes: 1 addition & 1 deletion packages/clients/client-helpers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@zenstackhq/client-helpers",
"displayName": "ZenStack Client Helpers",
"description": "Helpers for implementing clients that consume ZenStack's CRUD service",
"version": "3.6.3",
"version": "3.6.4",
"type": "module",
"author": {
"name": "ZenStack Team",
Expand Down
2 changes: 1 addition & 1 deletion packages/clients/tanstack-query/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@zenstackhq/tanstack-query",
"displayName": "ZenStack TanStack Query Integration",
"description": "TanStack Query Client for consuming ZenStack v3's CRUD service",
"version": "3.6.3",
"version": "3.6.4",
"type": "module",
"author": {
"name": "ZenStack Team",
Expand Down
2 changes: 1 addition & 1 deletion packages/common-helpers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@zenstackhq/common-helpers",
"displayName": "ZenStack Common Helpers",
"description": "ZenStack Common Helpers",
"version": "3.6.3",
"version": "3.6.4",
"type": "module",
"author": {
"name": "ZenStack Team",
Expand Down
2 changes: 1 addition & 1 deletion packages/config/eslint-config/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@zenstackhq/eslint-config",
"version": "3.6.3",
"version": "3.6.4",
"type": "module",
"private": true,
"license": "MIT"
Expand Down
2 changes: 1 addition & 1 deletion packages/config/tsdown-config/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@zenstackhq/tsdown-config",
"version": "3.6.3",
"version": "3.6.4",
"private": true,
"type": "module",
"license": "MIT",
Expand Down
2 changes: 1 addition & 1 deletion packages/config/typescript-config/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@zenstackhq/typescript-config",
"version": "3.6.3",
"version": "3.6.4",
"private": true,
"license": "MIT"
}
2 changes: 1 addition & 1 deletion packages/config/vitest-config/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@zenstackhq/vitest-config",
"type": "module",
"version": "3.6.3",
"version": "3.6.4",
"private": true,
"license": "MIT",
"exports": {
Expand Down
2 changes: 1 addition & 1 deletion packages/create-zenstack/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "create-zenstack",
"displayName": "Create ZenStack",
"description": "Create a new ZenStack project",
"version": "3.6.3",
"version": "3.6.4",
"type": "module",
"author": {
"name": "ZenStack Team",
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-v3",
"publisher": "zenstack",
"version": "3.6.3",
"version": "3.6.4",
"displayName": "ZenStack V3 Language Tools",
"description": "VSCode extension for ZenStack (v3) 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
Expand Up @@ -2,7 +2,7 @@
"name": "@zenstackhq/language",
"displayName": "ZenStack Language Tooling",
"description": "ZenStack ZModel language specification",
"version": "3.6.3",
"version": "3.6.4",
"type": "module",
"author": {
"name": "ZenStack Team",
Expand Down
2 changes: 1 addition & 1 deletion packages/orm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@zenstackhq/orm",
"displayName": "ZenStack ORM",
"description": "ZenStack ORM",
"version": "3.6.3",
"version": "3.6.4",
"type": "module",
"author": {
"name": "ZenStack Team",
Expand Down
2 changes: 1 addition & 1 deletion packages/plugins/policy/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@zenstackhq/plugin-policy",
"displayName": "ZenStack Access Policy Plugin",
"description": "ZenStack plugin that enforces access control policies defined in the schema",
"version": "3.6.3",
"version": "3.6.4",
"type": "module",
"author": {
"name": "ZenStack Team",
Expand Down
Loading
Loading