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
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,14 @@ generator zero {
// Optional list of Prisma Model names you want to exclude from the generated schema
// This does *not* change tables that replicate to zero-cache - only the types on the client
excludeTables = ["Posts", "Comments", ...]
// When true, skip generating the query builder (`zql` and `builder` exports)
skipBuilder = true
// When true, skip generating the module augmentation for default types in Zero
skipDeclare = true
// When true, enable legacy CRUD mutators (sets `enableLegacyMutators` to `true` in the generated schema)
enableLegacyMutators = true
// When true, enable legacy CRUD queries (sets `enableLegacyQueries` to `true` in the generated schema)
enableLegacyQueries = true
}
```

Expand Down
6 changes: 0 additions & 6 deletions integration/generated/zero/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -555,9 +555,3 @@ export const zql = createBuilder(schema);
* @deprecated Use `zql` instead.
*/
export const builder = zql;
/** Defines the default types for Zero */
declare module '@rocicorp/zero' {
interface DefaultTypes {
schema: Schema;
}
}
4 changes: 4 additions & 0 deletions integration/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ generator zero {
prettier = true
// camelCase = true
excludeTables = ["ExcludedModel"]
// skipBuilder = true
skipDeclare = true
// enableLegacyMutators = true
// enableLegacyQueries = true
}

// ============================================================================
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": "prisma-zero",
"version": "0.1.0",
"version": "0.1.1",
"description": "Generate Zero schemas from Prisma ORM schemas",
"type": "module",
"scripts": {
Expand Down
6 changes: 5 additions & 1 deletion src/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,17 @@ export async function onGenerate(options: GeneratorOptions) {
resolvePrettierConfig: generator.config.resolvePrettierConfig !== 'false', // Default true
camelCase: generator.config.camelCase === 'true', // Default false
excludeTables: loadExcludeTables(generator),
skipBuilder: generator.config.skipBuilder === 'true', // Default false
skipDeclare: generator.config.skipDeclare === 'true', // Default false
enableLegacyMutators: generator.config.enableLegacyMutators === 'true', // Default false
enableLegacyQueries: generator.config.enableLegacyQueries === 'true', // Default false
} satisfies Config;

// Transform the schema
const transformedSchema = transformSchema(dmmf, config);

// Generate code
let output = generateCode(transformedSchema);
let output = generateCode(transformedSchema, config);

// Apply prettier if configured
if (config.prettier) {
Expand Down
77 changes: 50 additions & 27 deletions src/generators/code-generator.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
import type {
Config,
TransformedSchema,
ZeroModel,
ZeroRelationship,
ZeroRelationshipLink,
ZeroTypeMapping,
} from '../types';

function generateImports(schema: TransformedSchema): string {
function generateImports(schema: TransformedSchema, config: Config): string {
const usedImports = new Set<string>();

// These are always used
usedImports.add('table');
usedImports.add('createSchema');
usedImports.add('createBuilder');

// Only add createBuilder if not skipped
if (!config.skipBuilder) {
usedImports.add('createBuilder');
}

// Check which type functions are used in the schema
schema.models.forEach(model => {
Expand Down Expand Up @@ -150,7 +155,7 @@ ${relationshipsStr}
return filteredRelationships.length > 0 ? filteredRelationships.join('') : '';
}

function generateSchema(schema: TransformedSchema): string {
function generateSchema(schema: TransformedSchema, config: Config): string {
let output = '/**\n';
output += ' * The Zero schema object.\n';
output +=
Expand Down Expand Up @@ -178,6 +183,15 @@ function generateSchema(schema: TransformedSchema): string {
output += ' ],\n';
}

// Add legacy options if enabled
if (config.enableLegacyMutators) {
output += ' enableLegacyMutators: true,\n';
}

if (config.enableLegacyQueries) {
output += ' enableLegacyQueries: true,\n';
}

output += '});\n\n';

// Add types
Expand All @@ -188,36 +202,45 @@ function generateSchema(schema: TransformedSchema): string {
output += ' */\n';
output += 'export type Schema = typeof schema;\n';

output += '/**\n';
output += ' * Represents the ZQL query builder.\n';
output +=
' * This type is auto-generated from your Prisma schema definition.\n';
output += ' */\n';
output += 'export const zql = createBuilder(schema);\n';

output += '/**\n';
output += ' * Represents the Zero schema query builder.\n';
output +=
' * This type is auto-generated from your Prisma schema definition.\n';
output += ' *\n';
output += ' * @deprecated Use `zql` instead.\n';
output += ' */\n';
output += 'export const builder = zql;\n';
// Only generate builder if not skipped
if (!config.skipBuilder) {
output += '/**\n';
output += ' * Represents the ZQL query builder.\n';
output +=
' * This type is auto-generated from your Prisma schema definition.\n';
output += ' */\n';
output += 'export const zql = createBuilder(schema);\n';

output += '/**\n';
output += ' * Represents the Zero schema query builder.\n';
output +=
' * This type is auto-generated from your Prisma schema definition.\n';
output += ' *\n';
output += ' * @deprecated Use `zql` instead.\n';
output += ' */\n';
output += 'export const builder = zql;\n';
}

output += '/** Defines the default types for Zero */\n';
output += 'declare module "@rocicorp/zero" {\n';
output += ' interface DefaultTypes {\n';
output += ' schema: Schema;\n';
output += ' }\n';
output += '}\n';
// Only generate declare module if not skipped
if (!config.skipDeclare) {
output += '/** Defines the default types for Zero */\n';
output += 'declare module "@rocicorp/zero" {\n';
output += ' interface DefaultTypes {\n';
output += ' schema: Schema;\n';
output += ' }\n';
output += '}\n';
}

return output;
}

export function generateCode(schema: TransformedSchema): string {
export function generateCode(
schema: TransformedSchema,
config: Config,
): string {
let output = `${HEADER_PREFIX}\n\n`;

output += generateImports(schema);
output += generateImports(schema, config);

output += generateUnionTypes(schema);

Expand All @@ -227,7 +250,7 @@ export function generateCode(schema: TransformedSchema): string {

output += generateRelationships(schema.models);

output += generateSchema(schema);
output += generateSchema(schema, config);

return output;
}
Expand Down
4 changes: 4 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ export type Config = {
resolvePrettierConfig: boolean;
camelCase: boolean;
excludeTables?: string[];
skipBuilder?: boolean;
skipDeclare?: boolean;
enableLegacyMutators?: boolean;
enableLegacyQueries?: boolean;
};

export type ZeroTypeMapping = {
Expand Down
Loading