Skip to content

Commit

Permalink
chore: Adapted to prisma 6
Browse files Browse the repository at this point in the history
  • Loading branch information
unlight committed Jan 18, 2025
1 parent 10d5cb9 commit 597ed09
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 94 deletions.
2 changes: 1 addition & 1 deletion prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ datasource database {

generator client {
provider = "prisma-client-js"
previewFeatures = ["fullTextSearchPostgres"]
previewFeatures = ["fullTextSearchPostgres", "postgresqlExtensions", "nativeDistinct", "strictUndefinedChecks"]
}

generator nestgraphql {
Expand Down
2 changes: 1 addition & 1 deletion src/handlers/combine-scalar-filters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ function isScalarFilter(inputType: InputType) {
}

function postBegin(args: EventArguments) {
const { schema, modelNames } = args;
const { modelNames, schema } = args;
const inputTypes = schema.inputObjectTypes.prisma;
const enumTypes = schema.enumTypes.model || [];
const types = [
Expand Down
42 changes: 21 additions & 21 deletions src/handlers/model-output-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import {
} from 'ts-morph';

import { createComment } from '../helpers/create-comment';
import { isManyAndReturnOutputType as isManyAndReturnOutputType } from '../helpers/is-many-and-return';
import { getGraphqlImport } from '../helpers/get-graphql-import';
import { getOutputTypeName } from '../helpers/get-output-type-name';
import { getPropertyType } from '../helpers/get-property-type';
import { ImportDeclarationMap } from '../helpers/import-declaration-map';
import { isManyAndReturnOutputType } from '../helpers/is-many-and-return';
import {
createObjectSettings,
ObjectSetting,
Expand All @@ -27,7 +27,7 @@ import { EventArguments, OutputType } from '../types';
const nestjsGraphql = '@nestjs/graphql';

export function modelOutputType(outputType: OutputType, args: EventArguments) {
const { getSourceFile, models, config, modelFields, fieldSettings, eventEmitter } =
const { config, eventEmitter, fieldSettings, getSourceFile, modelFields, models } =
args;

if (isManyAndReturnOutputType(outputType.name)) return;
Expand All @@ -46,15 +46,15 @@ export function modelOutputType(outputType: OutputType, args: EventArguments) {
);
const importDeclarations = new ImportDeclarationMap();
const classStructure: ClassDeclarationStructure = {
kind: StructureKind.Class,
isExported: true,
name: outputType.name,
decorators: [
{
name: 'ObjectType',
arguments: [],
name: 'ObjectType',
},
],
isExported: true,
kind: StructureKind.Class,
name: outputType.name,
properties: [],
};
(sourceFileStructure.statements as StatementStructures[]).push(classStructure);
Expand All @@ -67,8 +67,8 @@ export function modelOutputType(outputType: OutputType, args: EventArguments) {
if (model.documentation) {
const objectTypeOptions: PlainObject = {};
const { documentation, settings } = createObjectSettings({
text: model.documentation,
config,
text: model.documentation,
});
if (documentation) {
if (!classStructure.leadingTrivia) {
Expand All @@ -87,7 +87,7 @@ export function modelOutputType(outputType: OutputType, args: EventArguments) {
if (config.omitModelsCount && field.name === '_count') continue;

let fileType = 'model';
const { location, isList, type, namespace } = field.outputType;
const { isList, location, namespace, type } = field.outputType;

let outputTypeName = String(type);
if (namespace !== 'model') {
Expand Down Expand Up @@ -128,13 +128,13 @@ export function modelOutputType(outputType: OutputType, args: EventArguments) {
} else {
const graphqlImport = getGraphqlImport({
config,
sourceFile,
fileType,
location,
getSourceFile,
isId: modelField?.isId,
location,
noTypeId: config.noTypeId,
sourceFile,
typeName: outputTypeName,
getSourceFile,
});

graphqlType = graphqlImport.name;
Expand All @@ -145,12 +145,12 @@ export function modelOutputType(outputType: OutputType, args: EventArguments) {
}

const property = propertyStructure({
name: field.name,
isNullable: field.isNullable,
hasExclamationToken: true,
hasQuestionToken: location === 'outputObjectTypes',
propertyType,
isList,
isNullable: field.isNullable,
name: field.name,
propertyType,
});

if (typeof property.leadingTrivia === 'string' && modelField?.documentation) {
Expand Down Expand Up @@ -179,31 +179,31 @@ export function modelOutputType(outputType: OutputType, args: EventArguments) {

if (shouldHideField) {
importDeclarations.add('HideField', nestjsGraphql);
property.decorators.push({ name: 'HideField', arguments: [] });
property.decorators.push({ arguments: [], name: 'HideField' });
} else {
// Generate `@Field()` decorator
property.decorators.push({
name: 'Field',
arguments: [
isList ? `() => [${graphqlType}]` : `() => ${graphqlType}`,
JSON5.stringify({
...settings?.fieldArguments(),
nullable: Boolean(field.isNullable),
defaultValue: ['number', 'string', 'boolean'].includes(
typeof modelField?.default,
)
? modelField?.default
: undefined,
description: modelField?.documentation,
nullable: Boolean(field.isNullable),
}),
],
name: 'Field',
});

for (const setting of settings || []) {
if (shouldBeDecorated(setting) && (setting.match?.(field.name) ?? true)) {
property.decorators.push({
name: setting.name,
arguments: setting.arguments as string[],
name: setting.name,
});
ok(setting.from, "Missed 'from' part in configuration or field setting");
importDeclarations.create(setting);
Expand All @@ -213,17 +213,17 @@ export function modelOutputType(outputType: OutputType, args: EventArguments) {
for (const decorate of config.decorate) {
if (decorate.isMatchField(field.name) && decorate.isMatchType(outputTypeName)) {
property.decorators.push({
name: decorate.name,
arguments: decorate.arguments?.map(x => pupa(x, { propertyType })),
name: decorate.name,
});
importDeclarations.create(decorate);
}
}
}

eventEmitter.emitSync('ClassProperty', property, {
location,
isList,
location,
propertyType,
});
}
Expand All @@ -233,8 +233,8 @@ export function modelOutputType(outputType: OutputType, args: EventArguments) {
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
if (shouldBeDecorated(setting)) {
classStructure.decorators.push({
name: setting.name,
arguments: setting.arguments as string[],
name: setting.name,
});
importDeclarations.create(setting);
}
Expand Down
4 changes: 3 additions & 1 deletion src/helpers/get-model-name.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ function getModelName(args: {
name: string;
modelNames: string[];
}): string | undefined {
const { name, modelNames } = args;
const { modelNames, name } = args;
for (const keyword of splitKeywords) {
const [test] = name.split(keyword, 1);
if (modelNames.includes(test)) {
Expand Down Expand Up @@ -97,6 +97,8 @@ const splitKeywords = [
'AvgOrderBy',
'Create',
'Update',
'ScalarRelationFilter',
'NullableScalarRelationFilter',
].sort((a, b) => b.length - a.length);

const endsWithKeywords = [
Expand Down
Loading

0 comments on commit 597ed09

Please sign in to comment.