@baeta/plugin-prisma 0.0.2 → 0.0.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (40) hide show
  1. package/dist/index.cjs +49 -0
  2. package/dist/index.d.ts +12 -0
  3. package/dist/index.js +49 -0
  4. package/package.json +46 -25
  5. package/.eslintrc.cjs +0 -4
  6. package/CHANGELOG.md +0 -8
  7. package/index.ts +0 -47
  8. package/lib/context.ts +0 -29
  9. package/lib/dmmf.ts +0 -8
  10. package/lib/generator.ts +0 -24
  11. package/lib/model-entries.ts +0 -57
  12. package/lib/model-enum.ts +0 -17
  13. package/lib/model-inputs.ts +0 -32
  14. package/lib/model-list.ts +0 -5
  15. package/lib/model-operations.ts +0 -65
  16. package/lib/model-outputs.ts +0 -15
  17. package/lib/model-relations.ts +0 -35
  18. package/lib/print/directive.ts +0 -28
  19. package/lib/print/module-prisma.ts +0 -13
  20. package/lib/print/module-resolver.ts +0 -48
  21. package/lib/print/module.ts +0 -65
  22. package/lib/print/object.ts +0 -15
  23. package/lib/print/prisma-enum-model.ts +0 -7
  24. package/lib/print/prisma-enum.ts +0 -6
  25. package/lib/print/prisma-input.ts +0 -45
  26. package/lib/print/prisma-model.ts +0 -24
  27. package/lib/print/prisma-output.ts +0 -86
  28. package/lib/print/prisma-scalar.ts +0 -46
  29. package/lib/print/type.ts +0 -37
  30. package/lib/resolver-builder/index.ts +0 -1
  31. package/lib/resolver-builder/resolver-builder-files.ts +0 -69
  32. package/lib/resolver-builder/resolver-builder.ts +0 -80
  33. package/lib/schema-builder/index.ts +0 -1
  34. package/lib/schema-builder/schema-builder-files.ts +0 -150
  35. package/lib/schema-builder/schema-builder-utils.ts +0 -3
  36. package/lib/schema-builder/schema-builder.ts +0 -186
  37. package/lib/visitor.ts +0 -119
  38. package/tsconfig.json +0 -9
  39. package/utils/casing.ts +0 -23
  40. package/utils/string.ts +0 -9
package/dist/index.cjs ADDED
@@ -0,0 +1,49 @@
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } }// lib/client-generator.ts
2
+ var _pluginexec = require('@baeta/plugin-exec');
3
+ var _promises = require('fs/promises');
4
+ var _path = require('path');
5
+ async function compareSchemas(current, generated) {
6
+ const [currentSchema, generatedSchema] = await Promise.all([
7
+ _promises.readFile.call(void 0, _path.resolve.call(void 0, process.cwd(), current), "utf-8"),
8
+ _promises.readFile.call(void 0, _path.resolve.call(void 0, process.cwd(), generated), "utf-8").catch(() => null)
9
+ ]);
10
+ return currentSchema === generatedSchema;
11
+ }
12
+ function createPrismaClientPlugin(options) {
13
+ const { prismaSchema, generateCommand, generatedSchemaPath } = options;
14
+ const schema = _path.resolve.call(void 0, process.cwd(), prismaSchema);
15
+ const skip = async (ctx) => {
16
+ if (ctx.watching && ctx.changedFile !== schema) {
17
+ return true;
18
+ }
19
+ if (!ctx.watching && generatedSchemaPath) {
20
+ return compareSchemas(prismaSchema, generatedSchemaPath);
21
+ }
22
+ return false;
23
+ };
24
+ return _pluginexec.createExecPlugin.call(void 0, {
25
+ name: "prisma-client",
26
+ actionName: "Prisma client",
27
+ exec: _nullishCoalesce(generateCommand, () => ( "prisma generate")),
28
+ watch: () => {
29
+ return {
30
+ include: [_path.resolve.call(void 0, process.cwd(), prismaSchema)],
31
+ ignore: []
32
+ };
33
+ },
34
+ skip
35
+ });
36
+ }
37
+
38
+ // index.ts
39
+ var prisma_default = prismaPlugin;
40
+ function prismaPlugin(options) {
41
+ if (options.generateClient === false) {
42
+ return [];
43
+ }
44
+ return [createPrismaClientPlugin(options)];
45
+ }
46
+
47
+
48
+
49
+ exports.default = prisma_default; exports.prismaPlugin = prismaPlugin;
@@ -0,0 +1,12 @@
1
+ import * as _baeta_generator_sdk from '@baeta/generator-sdk';
2
+
3
+ interface PrismaPluginOptions {
4
+ prismaSchema: string;
5
+ generateClient?: boolean;
6
+ generateCommand?: string;
7
+ generatedSchemaPath?: string;
8
+ }
9
+
10
+ declare function prismaPlugin(options: PrismaPluginOptions): _baeta_generator_sdk.GeneratorPluginV1<{}>[];
11
+
12
+ export { PrismaPluginOptions, prismaPlugin as default, prismaPlugin };
package/dist/index.js ADDED
@@ -0,0 +1,49 @@
1
+ // lib/client-generator.ts
2
+ import { createExecPlugin } from "@baeta/plugin-exec";
3
+ import { readFile } from "fs/promises";
4
+ import { resolve } from "path";
5
+ async function compareSchemas(current, generated) {
6
+ const [currentSchema, generatedSchema] = await Promise.all([
7
+ readFile(resolve(process.cwd(), current), "utf-8"),
8
+ readFile(resolve(process.cwd(), generated), "utf-8").catch(() => null)
9
+ ]);
10
+ return currentSchema === generatedSchema;
11
+ }
12
+ function createPrismaClientPlugin(options) {
13
+ const { prismaSchema, generateCommand, generatedSchemaPath } = options;
14
+ const schema = resolve(process.cwd(), prismaSchema);
15
+ const skip = async (ctx) => {
16
+ if (ctx.watching && ctx.changedFile !== schema) {
17
+ return true;
18
+ }
19
+ if (!ctx.watching && generatedSchemaPath) {
20
+ return compareSchemas(prismaSchema, generatedSchemaPath);
21
+ }
22
+ return false;
23
+ };
24
+ return createExecPlugin({
25
+ name: "prisma-client",
26
+ actionName: "Prisma client",
27
+ exec: generateCommand ?? "prisma generate",
28
+ watch: () => {
29
+ return {
30
+ include: [resolve(process.cwd(), prismaSchema)],
31
+ ignore: []
32
+ };
33
+ },
34
+ skip
35
+ });
36
+ }
37
+
38
+ // index.ts
39
+ var prisma_default = prismaPlugin;
40
+ function prismaPlugin(options) {
41
+ if (options.generateClient === false) {
42
+ return [];
43
+ }
44
+ return [createPrismaClientPlugin(options)];
45
+ }
46
+ export {
47
+ prisma_default as default,
48
+ prismaPlugin
49
+ };
package/package.json CHANGED
@@ -1,38 +1,59 @@
1
1
  {
2
2
  "name": "@baeta/plugin-prisma",
3
- "version": "0.0.2",
4
- "license": "MIT",
5
- "author": "Andrei Pampu <pampu.andrei@pm.me>",
3
+ "version": "0.0.13",
6
4
  "homepage": "https://github.com/andreisergiu98/baeta#readme",
5
+ "bugs": {
6
+ "url": "https://github.com/andreisergiu98/baeta/issues"
7
+ },
7
8
  "repository": {
8
9
  "type": "git",
9
10
  "url": "https://github.com/andreisergiu98/baeta.git",
10
11
  "directory": "plugins/prisma"
11
12
  },
12
- "bugs": {
13
- "url": "https://github.com/andreisergiu98/baeta/issues"
14
- },
13
+ "license": "MIT",
14
+ "author": "Andrei Pampu <pampu.andrei@pm.me>",
15
15
  "type": "module",
16
+ "files": [
17
+ "dist",
18
+ "package.json"
19
+ ],
20
+ "scripts": {
21
+ "build": "tsup",
22
+ "prepack": "prep",
23
+ "postpack": "prep --clean",
24
+ "types": "tsc --noEmit"
25
+ },
16
26
  "dependencies": {
17
- "@baeta/plugin": "^0.0.5",
18
- "@prisma/generator-helper": "^4.4.0",
19
- "@prisma/internals": "^4.4.0",
20
- "@prisma/sdk": "^4.0.0",
21
- "change-case-all": "^1.0.15",
22
- "pluralize": "^8.0.0",
23
- "ramda": "^0.28.0",
24
- "tslib": "^2.4.0"
27
+ "@baeta/generator-sdk": "^0.0.3",
28
+ "@baeta/plugin-exec": "^0.0.1",
29
+ "execa": "^6.1.0"
25
30
  },
26
31
  "devDependencies": {
27
- "@baeta/eslint-config": "^0.0.0",
28
- "@types/node": "^18.8.1",
29
- "@types/pluralize": "^0.0.29",
30
- "@types/ramda": "^0.28.15",
31
- "eslint": "^8.24.0",
32
- "graphql": "^16.6.0",
33
- "typescript": "^4.8.3"
34
- },
35
- "peerDependencies": {
36
- "graphql": "^15.3.0 || ^16.0.0"
37
- }
32
+ "@baeta/build-tools": "^0.0.0",
33
+ "@baeta/tsconfig": "^0.0.0",
34
+ "@types/node": "^18.11.18",
35
+ "typescript": "^4.9.4"
36
+ },
37
+ "engines": {
38
+ "node": ">=18.0.0"
39
+ },
40
+ "publishConfig": {
41
+ "access": "public",
42
+ "exports": {
43
+ ".": {
44
+ "import": "./dist/index.js",
45
+ "require": "./dist/index.cjs",
46
+ "types": "./dist/index.d.ts"
47
+ }
48
+ },
49
+ "types": "dist/index.d.ts"
50
+ },
51
+ "exports": {
52
+ ".": {
53
+ "import": "./dist/index.js",
54
+ "require": "./dist/index.cjs",
55
+ "types": "./dist/index.d.ts"
56
+ }
57
+ },
58
+ "types": "dist/index.d.ts"
38
59
  }
package/.eslintrc.cjs DELETED
@@ -1,4 +0,0 @@
1
- module.exports = {
2
- extends: ['@baeta/eslint-config'],
3
- parserOptions: { tsconfigRootDir: __dirname },
4
- };
package/CHANGELOG.md DELETED
@@ -1,8 +0,0 @@
1
- # @baeta/plugin-prisma
2
-
3
- ## 0.0.2
4
-
5
- ### Patch Changes
6
-
7
- - Updated dependencies [[`ffa2fe8`](https://github.com/andreisergiu98/baeta/commit/ffa2fe83df6798375252a01ad14bbd37ffd07d50)]:
8
- - @baeta/plugin@0.0.5
package/index.ts DELETED
@@ -1,47 +0,0 @@
1
- import { createPluginFactoryV1, GeneratorCtx } from '@baeta/plugin';
2
- import { createContext, Store } from './lib/context';
3
- import { generate } from './lib/generator';
4
- import { Casing } from './utils/casing';
5
-
6
- export interface PrismaPluginOptions {
7
- casing?: Casing;
8
- schemaNamespace?: string;
9
- resolverNamespace?: string;
10
- resolverExport?: boolean;
11
- }
12
-
13
- export type PrismaPluginCtx = GeneratorCtx<{
14
- prisma: Store;
15
- }>;
16
-
17
- export default createPluginFactoryV1<PrismaPluginOptions, PrismaPluginCtx>({
18
- name: 'prisma',
19
- watch: (baetaConfig, pluginConfig) => {
20
- const root = baetaConfig.graphql.modulesDir || 'src/modules';
21
- const modulesMatcher =
22
- root + '/*/' + (pluginConfig.schemaNamespace || 'prisma-schema') + '/**';
23
-
24
- const prismaModule = root + '/prisma' + '/**';
25
-
26
- return {
27
- include: [],
28
- ignore: [modulesMatcher, prismaModule],
29
- };
30
- },
31
- setup: async (params) => {
32
- params.ctx.prisma = await createContext();
33
- return params.next();
34
- },
35
- generate: async (params) => {
36
- const files = await generate(params.ctx.prisma, {
37
- modulesDir: params.ctx.config.graphql.modulesDir || 'src/modules',
38
- casing: params.config.casing || 'kebab-case',
39
- schemaNamespace: params.config.schemaNamespace || 'prisma-schema',
40
- resolverNamespace: params.config.resolverNamespace || 'prisma-resolver',
41
- resolverExport: params.config.resolverExport ?? true,
42
- });
43
- params.ctx.fileManager.add(...files);
44
- await params.ctx.fileManager.writeByTag('prisma-sdl');
45
- return params.next();
46
- },
47
- });
package/lib/context.ts DELETED
@@ -1,29 +0,0 @@
1
- import { parseDMMF } from './dmmf';
2
- import { createInputsModelMap } from './model-inputs';
3
- import { createModelsList } from './model-list';
4
- import { createModelsOperationsMaps } from './model-operations';
5
- import { createOutputsModelMaps } from './model-outputs';
6
- import { createModelRelationsList } from './model-relations';
7
-
8
- export type Store = ReturnType<typeof createContext> extends Promise<infer T> ? T : never;
9
-
10
- export async function createContext() {
11
- const dmmf = await parseDMMF({
12
- datamodelPath: 'schema.prisma',
13
- });
14
-
15
- const models = createModelsList(dmmf);
16
- const inputsMap = createInputsModelMap(dmmf, models);
17
- const outputsMap = createOutputsModelMaps(dmmf, models);
18
- const relationsMap = createModelRelationsList(dmmf);
19
- const operationsMap = createModelsOperationsMaps(dmmf);
20
-
21
- return {
22
- dmmf,
23
- models,
24
- inputsMap,
25
- outputsMap,
26
- relationsMap,
27
- operationsMap,
28
- };
29
- }
package/lib/dmmf.ts DELETED
@@ -1,8 +0,0 @@
1
- import { DMMF } from '@prisma/generator-helper';
2
- import { getDMMF, GetDMMFOptions } from '@prisma/internals';
3
-
4
- export { DMMF };
5
-
6
- export function parseDMMF(options: GetDMMFOptions): Promise<DMMF.Document> {
7
- return getDMMF(options);
8
- }
package/lib/generator.ts DELETED
@@ -1,24 +0,0 @@
1
- import { PrismaPluginOptions } from '..';
2
- import { Store } from './context';
3
- import { createResolversBuilder } from './resolver-builder';
4
- import { createSchemaBuilder } from './schema-builder';
5
- import { visit } from './visitor';
6
-
7
- export interface GenerateOptions extends Required<PrismaPluginOptions> {
8
- modulesDir: string;
9
- }
10
-
11
- export async function generate(store: Store, options: GenerateOptions) {
12
- const schemaBuilder = createSchemaBuilder(store, options);
13
- const resolversBuilder = createResolversBuilder(store, options);
14
-
15
- await visit(store.dmmf, [
16
- resolversBuilder.visitor.getVisitor(),
17
- schemaBuilder.visitor.getVisitor(),
18
- ]);
19
-
20
- const resolverFiles = resolversBuilder.compose();
21
- const schemaFiles = schemaBuilder.compose();
22
-
23
- return [...resolverFiles, ...schemaFiles];
24
- }
@@ -1,57 +0,0 @@
1
- interface ModelEntry {
2
- model: string;
3
- modelScore: number;
4
- operationName: string;
5
- }
6
- type ModelEntryMap = Record<string, ModelEntry | undefined>;
7
-
8
- type EntryValue = {
9
- model: string;
10
- };
11
-
12
- export type EntryToModelMap = Record<string, EntryValue | undefined>;
13
-
14
- export function createEntriesModelMap(models: string[], entries: string[]) {
15
- const map: ModelEntryMap = {};
16
-
17
- for (const model of models) {
18
- updateEntriesForModel(map, model, entries);
19
- }
20
-
21
- return normalizeResult(map);
22
- }
23
-
24
- function updateEntriesForModel(map: ModelEntryMap, model: string, entries: string[]) {
25
- for (const input of entries) {
26
- if (!input.startsWith(model) && input !== 'Aggregate' + model) {
27
- continue;
28
- }
29
-
30
- const currentScore = model.length;
31
-
32
- const modelInput = map[input];
33
-
34
- if (modelInput && modelInput.modelScore > currentScore) {
35
- continue;
36
- }
37
-
38
- map[input] = {
39
- model,
40
- operationName: input,
41
- modelScore: currentScore,
42
- };
43
- }
44
- }
45
-
46
- function normalizeResult(value: ModelEntryMap) {
47
- const result: EntryToModelMap = {};
48
-
49
- for (const input of Object.values(value)) {
50
- if (!input) {
51
- continue;
52
- }
53
- result[input.operationName] = { model: input.model };
54
- }
55
-
56
- return result;
57
- }
package/lib/model-enum.ts DELETED
@@ -1,17 +0,0 @@
1
- import { DMMF } from './dmmf';
2
-
3
- export function findModelByEnum(models: string[], enumType: DMMF.SchemaEnum) {
4
- for (const model of models) {
5
- if (!enumType.name.startsWith(model)) {
6
- continue;
7
- }
8
-
9
- if (model + 'ScalarFieldEnum' === enumType.name) {
10
- return model;
11
- }
12
-
13
- if (model + 'OrderByRelevanceFieldEnum' === enumType.name) {
14
- return model;
15
- }
16
- }
17
- }
@@ -1,32 +0,0 @@
1
- import { DMMF } from './dmmf';
2
- import { createEntriesModelMap, EntryToModelMap } from './model-entries';
3
-
4
- export function createInputsModelMap(dmmf: DMMF.Document, models: string[]) {
5
- const inputs = createInputsList(dmmf);
6
- return createEntriesModelMap(models, inputs);
7
- }
8
-
9
- export function findModelByInput(entries: EntryToModelMap, input: DMMF.InputType) {
10
- return entries[input.name]?.model;
11
- }
12
-
13
- function createInputsList(dmmf: DMMF.Document) {
14
- return dmmf.schema.inputObjectTypes.prisma.map((el) => el.name);
15
- }
16
-
17
- export function createInputsIgnoreList(dmmf: DMMF.Document) {
18
- const ignoreList: string[] = [];
19
-
20
- const inputs = createInputsList(dmmf);
21
- const uncheckedInputs = inputs.filter((input) => input.includes('Unchecked'));
22
-
23
- uncheckedInputs.forEach((uncheckedInput) => {
24
- const normalized = uncheckedInput.replace('Unchecked', '');
25
- const exists = inputs.some((input) => input === normalized);
26
- if (exists) {
27
- ignoreList.push(uncheckedInput);
28
- }
29
- });
30
-
31
- return ignoreList;
32
- }
package/lib/model-list.ts DELETED
@@ -1,5 +0,0 @@
1
- import { DMMF } from './dmmf';
2
-
3
- export function createModelsList(dmmf: DMMF.Document) {
4
- return dmmf.mappings.modelOperations.map((modelOperations) => modelOperations.model);
5
- }
@@ -1,65 +0,0 @@
1
- import { plural, singular } from 'pluralize';
2
- import { DMMF } from './dmmf';
3
-
4
- export type ModelOperations = {
5
- model: string;
6
- prisma: string;
7
- operation: string;
8
- };
9
-
10
- export type ModelsOperationsMap = Record<string, ModelOperations | undefined>;
11
-
12
- export function createModelsOperationsMaps(dmmf: DMMF.Document) {
13
- return dmmf.mappings.modelOperations
14
- .map((operations) => createModelOperationsMap(operations))
15
- .reduce((a, b) => ({ ...a, ...b }));
16
- }
17
-
18
- export function findModelByOperationSchema(
19
- map: ModelsOperationsMap,
20
- schema: DMMF.SchemaField
21
- ) {
22
- return map[schema.name]?.model;
23
- }
24
-
25
- function createModelOperationsMap(mapping: DMMF.ModelMapping) {
26
- const model = mapping.model;
27
- const operationMap: ModelsOperationsMap = {};
28
-
29
- function addToMap(key: string, operation?: string) {
30
- const originalName = mapping[key as keyof DMMF.ModelMapping];
31
-
32
- if (!originalName) {
33
- return;
34
- }
35
-
36
- operationMap[originalName] = {
37
- model,
38
- prisma: key,
39
- operation: operation ?? originalName,
40
- };
41
- }
42
-
43
- const keys = Object.keys(mapping);
44
- const modelName = model[0].toLocaleLowerCase() + model.slice(1);
45
-
46
- for (const key of keys) {
47
- if (key === 'model') {
48
- continue;
49
- }
50
-
51
- if (key === 'findUnique') {
52
- addToMap(key, singular(modelName));
53
- continue;
54
- }
55
-
56
- if (key === 'findMany') {
57
- addToMap(key, plural(modelName));
58
- continue;
59
- }
60
-
61
- addToMap(key);
62
- }
63
-
64
- return operationMap;
65
- }
@@ -1,15 +0,0 @@
1
- import { DMMF } from './dmmf';
2
- import { createEntriesModelMap, EntryToModelMap } from './model-entries';
3
-
4
- export function createOutputsModelMaps(dmmf: DMMF.Document, models: string[]) {
5
- const inputs = createOutputsList(dmmf);
6
- return createEntriesModelMap(models, inputs);
7
- }
8
-
9
- export function findModelByOutput(entries: EntryToModelMap, output: DMMF.OutputType) {
10
- return entries[output.name]?.model;
11
- }
12
-
13
- function createOutputsList(dmmf: DMMF.Document) {
14
- return dmmf.schema.outputObjectTypes.prisma.map((el) => el.name);
15
- }
@@ -1,35 +0,0 @@
1
- import { DMMF } from './dmmf';
2
-
3
- export interface ModelRelation {
4
- with: string;
5
- name: string;
6
- }
7
-
8
- export type ModelRelationsMap = Record<string, ModelRelation[] | undefined>;
9
-
10
- export function createModelRelationsList(dmmf: DMMF.Document) {
11
- const map: ModelRelationsMap = {};
12
-
13
- for (const model of dmmf.datamodel.models) {
14
- map[model.name] = getRelationsFromModel(model);
15
- }
16
-
17
- return map;
18
- }
19
-
20
- function getRelationsFromModel(model: DMMF.Model) {
21
- const relations: ModelRelation[] = [];
22
-
23
- for (const field of model.fields) {
24
- if (field.kind !== 'object') {
25
- continue;
26
- }
27
-
28
- relations.push({
29
- with: field.type,
30
- name: field.name,
31
- });
32
- }
33
-
34
- return relations;
35
- }
@@ -1,28 +0,0 @@
1
- export const directives = [
2
- 'directive @inputConstraint(minFields: Int, maxFields: Int) on INPUT_OBJECT',
3
- ];
4
-
5
- export function printDirectives() {
6
- return directives.join('\n');
7
- }
8
-
9
- export function printInputConstraintDirective(
10
- minNumFields?: number | null,
11
- maxNumFields?: number | null
12
- ) {
13
- const args = [];
14
-
15
- if (minNumFields != null) {
16
- args.push(`minFields: ${minNumFields}`);
17
- }
18
-
19
- if (maxNumFields != null) {
20
- args.push(`maxFields: ${maxNumFields}`);
21
- }
22
-
23
- if (args.length === 0) {
24
- return;
25
- }
26
-
27
- return `@inputConstraint(${args.join(', ')})`;
28
- }
@@ -1,13 +0,0 @@
1
- export function printPrismaModule() {
2
- const imports = [
3
- `import { registerPrismaModule } from "@baeta/prisma";`,
4
- `import { createPrismaModule } from "./typedef";`,
5
- ].join('\n');
6
-
7
- const content = [
8
- `export const prismaModule = createPrismaModule();`,
9
- `registerPrismaModule(prismaModule);`,
10
- ].join('\n');
11
-
12
- return [imports, content].join('\n');
13
- }
@@ -1,48 +0,0 @@
1
- import { camelCase, upperCaseFirst } from 'change-case-all';
2
- import { ModelOperations } from '../model-operations';
3
- import { ModelRelation } from '../model-relations';
4
-
5
- export function printResolver(
6
- namespace: string,
7
- mapping: ModelOperations,
8
- prisma: string = 'ctx.prisma'
9
- ) {
10
- const prismaModel = camelCase(mapping.model);
11
- const prismaMethod = mapping.prisma.replace('One', '');
12
- const expectError =
13
- mapping.prisma === 'groupBy' ? '//@ts-expect-error difficult to infer\n ' : '';
14
-
15
- const instruction = `${expectError}return ${prisma}.${prismaModel}.${prismaMethod}(args);`;
16
-
17
- return printFunction(namespace, mapping.operation, instruction);
18
- }
19
-
20
- export function printRelationResolver(
21
- model: string,
22
- relation: ModelRelation,
23
- prisma: string = 'ctx.prisma'
24
- ) {
25
- const prismaModel = camelCase(model);
26
- const instruction = `return ${prisma}.${prismaModel}.findUnique({where: {id: root.id}}).${relation.name}(args);`;
27
-
28
- return printFunction(model, relation.name, instruction, true);
29
- }
30
-
31
- function printFunction(
32
- namespace: string,
33
- func: string,
34
- content: string,
35
- rootArg = false
36
- ) {
37
- const args = ['args', 'ctx'];
38
-
39
- if (rootArg) {
40
- args.unshift('root');
41
- }
42
-
43
- return [
44
- `${upperCaseFirst(namespace)}.${func}(({ ${args.join(', ')} }) => {`,
45
- ` ${content}`,
46
- `});`,
47
- ].join('\n');
48
- }