@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
@@ -1,65 +0,0 @@
1
- import { camelCase } from 'change-case-all';
2
- import { parse } from 'path';
3
- import { ModuleResolvers } from '../resolver-builder';
4
- import { printRelationResolver, printResolver } from './module-resolver';
5
-
6
- export function printModule(model: ModuleResolvers) {
7
- const name = model.name;
8
- const moduleName = `${camelCase(name)}Module`;
9
-
10
- const hasQueries = model.queries.length > 0;
11
- const hasMutations = model.mutations.length > 0;
12
- const hasRelations = model.relations.length > 0;
13
-
14
- const queries = model.queries.map((operations) => printResolver('Query', operations));
15
-
16
- const mutations = model.mutations.map((operations) =>
17
- printResolver('Mutation', operations)
18
- );
19
-
20
- const relations = model.relations.map((relation) =>
21
- printRelationResolver(model.name, relation)
22
- );
23
-
24
- return [
25
- printImports(name),
26
- printInit(moduleName, name),
27
- printDestructure(moduleName, model.name, hasQueries, hasMutations, hasRelations),
28
- queries.join('\n\n'),
29
- mutations.join('\n\n'),
30
- relations.join('\n\n'),
31
- ].join('\n\n');
32
- }
33
-
34
- export function printModuleExport(filename: string) {
35
- const exportFile = parse(filename).name;
36
- return `export * from "./${exportFile}";`;
37
- }
38
-
39
- function printImports(name: string) {
40
- return [`import { create${name}Module } from "../typedef";`].join('\n');
41
- }
42
-
43
- function printInit(moduleName: string, name: string) {
44
- return [`export const ${moduleName} = create${name}Module();`].join('\n');
45
- }
46
-
47
- function printDestructure(
48
- moduleName: string,
49
- modelName: string,
50
- hasQueries: boolean,
51
- hasMutations: boolean,
52
- hasRelations: boolean
53
- ) {
54
- const values = [];
55
- if (hasQueries) {
56
- values.push('Query');
57
- }
58
- if (hasMutations) {
59
- values.push('Mutation');
60
- }
61
- if (hasRelations) {
62
- values.push(modelName);
63
- }
64
- return `const { ${values.join(', ')} } = ${moduleName};`;
65
- }
@@ -1,15 +0,0 @@
1
- import { filterEmpty, indent } from '../../utils/string';
2
-
3
- export function printObject(
4
- kind: string,
5
- name: string,
6
- fields: Array<string | undefined>,
7
- directive?: string
8
- ) {
9
- const directiveLine = directive ? ` ${directive}` : '';
10
- const firstLine = kind + ' ' + name + directiveLine + ' {';
11
- const content = filterEmpty(fields).map(indent(2)).join('\n');
12
- const lastLine = '}';
13
-
14
- return [firstLine, content, lastLine].join('\n');
15
- }
@@ -1,7 +0,0 @@
1
- import { DMMF } from '../dmmf';
2
- import { printObject } from './object';
3
-
4
- export function printPrismaEnumModel(enumModel: DMMF.DatamodelEnum) {
5
- const fields = Object.values(enumModel.values).map((value) => value.name);
6
- return printObject('enum', enumModel.name, fields);
7
- }
@@ -1,6 +0,0 @@
1
- import { DMMF } from '../dmmf';
2
- import { printObject } from './object';
3
-
4
- export function printPrismaEnum(enumType: DMMF.SchemaEnum) {
5
- return printObject('enum', enumType.name, enumType.values);
6
- }
@@ -1,45 +0,0 @@
1
- import { DMMF } from '../dmmf';
2
- import { printInputConstraintDirective } from './directive';
3
- import { printObject } from './object';
4
- import { printType } from './type';
5
-
6
- export function printPrismaInput(inputType: DMMF.InputType) {
7
- const { minNumFields, maxNumFields } = inputType.constraints;
8
- const requireConstraint = minNumFields === 1 && inputType.fields.length === 1;
9
-
10
- let directive: string | undefined;
11
- if (!requireConstraint) {
12
- directive = printInputConstraintDirective(minNumFields, maxNumFields);
13
- }
14
-
15
- const fields = inputType.fields.map((field) =>
16
- printInputField(field, requireConstraint)
17
- );
18
-
19
- return printObject('input', inputType.name, fields, directive);
20
- }
21
-
22
- function printInputField(field: DMMF.SchemaArg, requireConstraint: boolean) {
23
- const type = printInputFieldScalar(field, requireConstraint);
24
- if (!type) {
25
- return;
26
- }
27
- return `${field.name}: ${type}`;
28
- }
29
-
30
- function printInputFieldScalar(field: DMMF.SchemaArg, requireConstraint: boolean) {
31
- const inputTypes = field.inputTypes.filter((inputType) => inputType.type !== 'Null');
32
-
33
- if (inputTypes.length === 0) {
34
- return;
35
- }
36
-
37
- const inputType = inputTypes[inputTypes.length - 1];
38
- const inputTypeName = inputType.type.toString();
39
-
40
- return printType(inputTypeName, {
41
- kind: inputType.location,
42
- list: inputType.isList,
43
- required: (field.isRequired || requireConstraint) && !field.isNullable,
44
- });
45
- }
@@ -1,24 +0,0 @@
1
- import { DMMF } from '../dmmf';
2
- import { printObject } from './object';
3
- import { printType } from './type';
4
-
5
- export function printPrismaModel(model: DMMF.Model) {
6
- const fields = model.fields.map((field) => printModelField(field));
7
- return printObject('type', model.name, fields);
8
- }
9
-
10
- function printModelField(field: DMMF.Field) {
11
- return `${field.name}: ${printFieldScalar(field)}`;
12
- }
13
-
14
- function printFieldScalar(field: DMMF.Field) {
15
- let name = field.type;
16
- if (field.isId) {
17
- name = 'ID';
18
- }
19
- return printType(name, {
20
- kind: field.kind,
21
- list: field.isList,
22
- required: field.isRequired && field.kind !== 'object',
23
- });
24
- }
@@ -1,86 +0,0 @@
1
- import { filterEmpty } from '../../utils/string';
2
- import { DMMF } from '../dmmf';
3
- import { ModelsOperationsMap } from '../model-operations';
4
- import { printObject } from './object';
5
- import { mapPrismaTypeToScalar } from './prisma-scalar';
6
- import { printType } from './type';
7
-
8
- export function printPrismaOutput(outputType: DMMF.OutputType) {
9
- const content = outputType.fields.map((field) => printOutputTypeField(field));
10
- return printObject('type', outputType.name, content);
11
- }
12
-
13
- export function printPrismaOperation(
14
- operations: ModelsOperationsMap,
15
- field: DMMF.SchemaField
16
- ) {
17
- const operation = operations[field.name];
18
-
19
- if (!operation) {
20
- return;
21
- }
22
-
23
- const args = printOutputFieldArgs(field.args);
24
- const output = printOutputFieldReturnType(field);
25
- return `${operation.operation}${args}: ${output}`;
26
- }
27
-
28
- function printOutputTypeField(field: DMMF.SchemaField) {
29
- const args = printOutputFieldArgs(field.args);
30
- const returnType = printOutputFieldReturnType(field);
31
- return `${field.name}${args}: ${returnType}`;
32
- }
33
-
34
- function printOutputFieldArgs(args: DMMF.SchemaArg[]) {
35
- const argList = filterEmpty(args.map((arg) => printOutputFieldArg(arg)));
36
-
37
- if (argList.length === 0) {
38
- return '';
39
- }
40
-
41
- return `(${argList.join(', ')})`;
42
- }
43
-
44
- function printOutputFieldArg(arg: DMMF.SchemaArg) {
45
- const type = printOutputFieldArgType(arg);
46
-
47
- if (!type) {
48
- return;
49
- }
50
-
51
- return `${arg.name}: ${type}`;
52
- }
53
-
54
- function printOutputFieldArgType(arg: DMMF.SchemaArg) {
55
- const inputType = arg.inputTypes[0];
56
-
57
- if (!inputType) {
58
- return;
59
- }
60
-
61
- let type = inputType.type.toString();
62
-
63
- if (inputType.location === 'scalar') {
64
- type = mapPrismaTypeToScalar(type);
65
- }
66
-
67
- if (inputType.isList) {
68
- type = `[${type}!]`;
69
- }
70
-
71
- if (arg.isRequired) {
72
- type += '!';
73
- }
74
-
75
- return type;
76
- }
77
-
78
- function printOutputFieldReturnType(field: DMMF.SchemaField) {
79
- const outputType = field.outputType;
80
-
81
- return printType(outputType.type.toString(), {
82
- kind: outputType.location,
83
- list: outputType.isList,
84
- required: !field.isNullable,
85
- });
86
- }
@@ -1,46 +0,0 @@
1
- export enum PrismaScalars {
2
- string = 'String',
3
- boolean = 'Boolean',
4
- int = 'Int',
5
- float = 'Float',
6
- dateTime = 'DateTime',
7
- json = 'Json',
8
- bigInt = 'BigInt',
9
- decimal = 'Decimal',
10
- bytes = 'Bytes',
11
- }
12
-
13
- export function mapPrismaTypeToScalar(scalar: string) {
14
- switch (scalar) {
15
- case PrismaScalars.string: {
16
- return 'String';
17
- }
18
- case PrismaScalars.boolean: {
19
- return 'Boolean';
20
- }
21
- case PrismaScalars.int: {
22
- return 'Int';
23
- }
24
- case PrismaScalars.float: {
25
- return 'Float';
26
- }
27
- case PrismaScalars.dateTime: {
28
- return 'DateTime';
29
- }
30
- case PrismaScalars.json: {
31
- return 'Json';
32
- }
33
- case PrismaScalars.bigInt: {
34
- return 'BigInt';
35
- }
36
- case PrismaScalars.decimal: {
37
- return 'Decimal';
38
- }
39
- case PrismaScalars.bytes: {
40
- return 'Bytes';
41
- }
42
- default: {
43
- throw new Error(`Unrecognized scalar type: ${scalar}`);
44
- }
45
- }
46
- }
package/lib/print/type.ts DELETED
@@ -1,37 +0,0 @@
1
- import { mapPrismaTypeToScalar } from './prisma-scalar';
2
-
3
- interface ScalarOptions {
4
- kind?: string;
5
- list?: boolean;
6
- required?: boolean;
7
- }
8
-
9
- export function printType(name: string, options: ScalarOptions = {}) {
10
- let type = name;
11
-
12
- if (options.kind === 'scalar' && name !== 'ID') {
13
- type = mapPrismaTypeToScalar(type);
14
- }
15
-
16
- if (options.list) {
17
- type = `[${type}!]`;
18
- }
19
-
20
- if (options.required) {
21
- type += '!';
22
- }
23
-
24
- return type;
25
- }
26
-
27
- export const scalars = [
28
- 'scalar DateTime',
29
- 'scalar BigInt',
30
- 'scalar Json',
31
- 'scalar Decimal',
32
- 'scalar Bytes',
33
- ];
34
-
35
- export function printScalars() {
36
- return scalars.join('\n');
37
- }
@@ -1 +0,0 @@
1
- export * from './resolver-builder';
@@ -1,69 +0,0 @@
1
- import { File } from '@baeta/plugin';
2
- import { join } from 'path';
3
- import { Casing, changeCase } from '../../utils/casing';
4
- import { printModule, printModuleExport } from '../print/module';
5
- import { printPrismaModule } from '../print/module-prisma';
6
- import { ModuleResolvers, ModuleResolversMap } from './resolver-builder';
7
-
8
- const tag = 'prisma-resolver';
9
-
10
- export interface ModuleFileOptions {
11
- root: string;
12
- casing: Casing;
13
- namespace: string;
14
- filename: string;
15
- createExport?: boolean;
16
- }
17
-
18
- export function createModuleFiles(map: ModuleResolversMap, options: ModuleFileOptions) {
19
- const files: File[] = [];
20
-
21
- for (const model of Object.values(map)) {
22
- if (!model) {
23
- continue;
24
- }
25
-
26
- const modulePath = createModulePath(
27
- model,
28
- options.root,
29
- options.namespace,
30
- options.casing
31
- );
32
-
33
- files.push(createModuleResolvers(model, modulePath, options.filename));
34
-
35
- if (options.createExport !== false) {
36
- files.push(createModuleResolversExport(modulePath, options.filename));
37
- }
38
- }
39
-
40
- files.push(createPrismaModule(options.root));
41
-
42
- return files;
43
- }
44
-
45
- function createModulePath(
46
- model: ModuleResolvers,
47
- rootDir: string,
48
- namespace: string,
49
- moduleCasing: Casing
50
- ) {
51
- const moduleDir = changeCase(model.name, moduleCasing);
52
- return join(rootDir, moduleDir, namespace);
53
- }
54
-
55
- function createModuleResolvers(model: ModuleResolvers, dir: string, filename: string) {
56
- const filepath = join(dir, filename);
57
- const content = printModule(model);
58
- return new File(filepath, content, tag);
59
- }
60
-
61
- function createModuleResolversExport(dir: string, filename: string) {
62
- const filepath = join(dir, 'index.ts');
63
- const content = printModuleExport(filename);
64
- return new File(filepath, content, tag);
65
- }
66
-
67
- function createPrismaModule(dir: string) {
68
- return new File(join(dir, 'prisma/resolvers.ts'), printPrismaModule(), tag);
69
- }
@@ -1,80 +0,0 @@
1
- import { concat, mergeDeepWith } from 'ramda';
2
- import { Store } from '../context';
3
- import { GenerateOptions } from '../generator';
4
- import { findModelByOperationSchema, ModelOperations } from '../model-operations';
5
- import { ModelRelation } from '../model-relations';
6
- import { createVisitorBuilder } from '../visitor';
7
- import { createModuleFiles } from './resolver-builder-files';
8
-
9
- export interface ModuleResolvers {
10
- name: string;
11
- queries: ModelOperations[];
12
- mutations: ModelOperations[];
13
- relations: ModelRelation[];
14
- }
15
-
16
- export type ModuleResolversMap = Record<string, ModuleResolvers | undefined>;
17
-
18
- export function createResolversBuilder(store: Store, options: GenerateOptions) {
19
- const { operationsMap, relationsMap } = store;
20
-
21
- const modelDefinitions: ModuleResolversMap = {};
22
-
23
- function pushDefinition(model: string, data: Partial<ModuleResolvers>) {
24
- const current: ModuleResolvers = modelDefinitions[model] ?? {
25
- name: model,
26
- queries: [],
27
- mutations: [],
28
- relations: [],
29
- };
30
- modelDefinitions[model] = mergeDeepWith(concat, current, data);
31
- }
32
-
33
- const visitor = createVisitorBuilder();
34
-
35
- visitor.onModel((model) => {
36
- const relations = relationsMap[model.name];
37
- if (!relations) {
38
- return;
39
- }
40
-
41
- pushDefinition(model.name, {
42
- relations,
43
- });
44
- });
45
-
46
- visitor.onOutputType((outputType) => {
47
- for (const field of outputType.fields) {
48
- const type = outputType.name;
49
- const model = findModelByOperationSchema(operationsMap, field);
50
- const operations = operationsMap[field.name];
51
-
52
- if (!model || !operations) {
53
- continue;
54
- }
55
-
56
- if (type === 'Query') {
57
- pushDefinition(model, { queries: [operations] });
58
- }
59
-
60
- if (type === 'Mutation') {
61
- pushDefinition(model, { mutations: [operations] });
62
- }
63
- }
64
- });
65
-
66
- function compose() {
67
- return createModuleFiles(modelDefinitions, {
68
- root: options.modulesDir,
69
- casing: options.casing,
70
- filename: 'resolvers.ts',
71
- namespace: options.resolverNamespace,
72
- createExport: options.resolverExport,
73
- });
74
- }
75
-
76
- return {
77
- compose,
78
- visitor,
79
- };
80
- }
@@ -1 +0,0 @@
1
- export * from './schema-builder';
@@ -1,150 +0,0 @@
1
- import { File } from '@baeta/plugin';
2
- import { join } from 'path';
3
- import { Casing, changeCase } from '../../utils/casing';
4
- import { filterEmpty } from '../../utils/string';
5
- import { printObject } from '../print/object';
6
- import {
7
- GlobalSchemaDefinition,
8
- ModuleDefinitionMap,
9
- ModuleSchemaDefinition,
10
- } from './schema-builder';
11
-
12
- const tag = 'prisma-sdl';
13
-
14
- export interface SchemaFileOptions {
15
- root: string;
16
- casing: Casing;
17
- namespace: string;
18
- }
19
-
20
- export function createGlobalSchemas(
21
- definition: GlobalSchemaDefinition,
22
- options: SchemaFileOptions
23
- ) {
24
- const dir = options.root + '/prisma';
25
-
26
- const files = [
27
- createSchemaFile(
28
- dir,
29
- createName('prisma', 'enums', options.casing),
30
- definition.enums
31
- ),
32
- createSchemaFile(
33
- dir,
34
- createName('prisma', 'inputs', options.casing),
35
- definition.inputTypes
36
- ),
37
- createSchemaFile(
38
- dir,
39
- createName('prisma', 'outputs', options.casing),
40
- definition.outputTypes
41
- ),
42
- createSchemaFile(
43
- dir,
44
- createName('prisma', 'scalars', options.casing),
45
- definition.scalars
46
- ),
47
- createSchemaFile(
48
- dir,
49
- createName('prisma', 'directives', options.casing),
50
- definition.directives
51
- ),
52
- ];
53
-
54
- return filterEmpty(files);
55
- }
56
-
57
- export function createModuleSchemas(
58
- definitionMap: ModuleDefinitionMap,
59
- options: SchemaFileOptions
60
- ) {
61
- const definitions = Object.values(definitionMap);
62
- return definitions.map((definition) => createModuleSchema(definition, options)).flat();
63
- }
64
-
65
- function createModuleSchema(
66
- definition: ModuleSchemaDefinition | undefined,
67
- options: SchemaFileOptions
68
- ) {
69
- if (!definition) {
70
- return [];
71
- }
72
-
73
- const name = definition.name;
74
- const dir = createSchemaPath(
75
- definition,
76
- options.root,
77
- options.namespace,
78
- options.casing
79
- );
80
-
81
- const files = [
82
- createSchemaFile(dir, createName(name, 'model', options.casing), definition.model),
83
- createSchemaFile(dir, createName(name, 'enums', options.casing), definition.enums),
84
- createSchemaFile(
85
- dir,
86
- createName(name, 'input', options.casing),
87
- definition.inputTypes
88
- ),
89
- createSchemaFile(
90
- dir,
91
- createName(name, 'output', options.casing),
92
- definition.outputTypes
93
- ),
94
- createSDLOperationFile(
95
- 'Query',
96
- dir,
97
- createName(name, 'query', options.casing),
98
- definition.queries
99
- ),
100
- createSDLOperationFile(
101
- 'Mutation',
102
- dir,
103
- createName(name, 'mutation', options.casing),
104
- definition.mutations
105
- ),
106
- ];
107
-
108
- return filterEmpty(files);
109
- }
110
-
111
- function createSDLOperationFile(
112
- type: string,
113
- dir: string,
114
- name: string,
115
- content: string[]
116
- ) {
117
- if (content.length === 0) {
118
- return;
119
- }
120
- const fileContent = printObject('type', type, content);
121
- return createSchemaFile(dir, name, fileContent);
122
- }
123
-
124
- function createSchemaFile(dir: string, name: string, content: string | string[]) {
125
- if (content.length === 0) {
126
- return;
127
- }
128
- const filepath = join(dir, name);
129
- const fileContent = Array.isArray(content) ? content.join('\n\n') : content;
130
- return new File(filepath, fileContent, tag);
131
- }
132
-
133
- export function createSchemaPath(
134
- definition: ModuleSchemaDefinition,
135
- rootDir: string,
136
- namespace: string,
137
- moduleCasing: Casing
138
- ) {
139
- const moduleDir = changeCase(definition.name, moduleCasing);
140
- return join(rootDir, moduleDir, namespace);
141
- }
142
-
143
- function createName(name: string, suffix?: string, casing: Casing = 'kebab-case') {
144
- const names = [changeCase(name, casing)];
145
- if (suffix) {
146
- names.push(changeCase(suffix, casing));
147
- }
148
- names.push('gql');
149
- return names.join('.');
150
- }
@@ -1,3 +0,0 @@
1
- export function isOperation(name: string): name is 'Query' | 'Mutation' {
2
- return ['Query', 'Mutation'].includes(name);
3
- }