@baeta/plugin-prisma 0.0.2 → 0.0.14
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.
- package/CHANGELOG.md +8 -3
- package/dist/index.cjs +49 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.js +49 -0
- package/package.json +46 -25
- package/.eslintrc.cjs +0 -4
- package/index.ts +0 -47
- package/lib/context.ts +0 -29
- package/lib/dmmf.ts +0 -8
- package/lib/generator.ts +0 -24
- package/lib/model-entries.ts +0 -57
- package/lib/model-enum.ts +0 -17
- package/lib/model-inputs.ts +0 -32
- package/lib/model-list.ts +0 -5
- package/lib/model-operations.ts +0 -65
- package/lib/model-outputs.ts +0 -15
- package/lib/model-relations.ts +0 -35
- package/lib/print/directive.ts +0 -28
- package/lib/print/module-prisma.ts +0 -13
- package/lib/print/module-resolver.ts +0 -48
- package/lib/print/module.ts +0 -65
- package/lib/print/object.ts +0 -15
- package/lib/print/prisma-enum-model.ts +0 -7
- package/lib/print/prisma-enum.ts +0 -6
- package/lib/print/prisma-input.ts +0 -45
- package/lib/print/prisma-model.ts +0 -24
- package/lib/print/prisma-output.ts +0 -86
- package/lib/print/prisma-scalar.ts +0 -46
- package/lib/print/type.ts +0 -37
- package/lib/resolver-builder/index.ts +0 -1
- package/lib/resolver-builder/resolver-builder-files.ts +0 -69
- package/lib/resolver-builder/resolver-builder.ts +0 -80
- package/lib/schema-builder/index.ts +0 -1
- package/lib/schema-builder/schema-builder-files.ts +0 -150
- package/lib/schema-builder/schema-builder-utils.ts +0 -3
- package/lib/schema-builder/schema-builder.ts +0 -186
- package/lib/visitor.ts +0 -119
- package/tsconfig.json +0 -9
- package/utils/casing.ts +0 -23
- package/utils/string.ts +0 -9
|
@@ -1,186 +0,0 @@
|
|
|
1
|
-
import { buildSchema, validateSchema } from 'graphql';
|
|
2
|
-
import { concat, mergeDeepWith } from 'ramda';
|
|
3
|
-
import { Store } from '../context';
|
|
4
|
-
import { DMMF } from '../dmmf';
|
|
5
|
-
import { GenerateOptions } from '../generator';
|
|
6
|
-
import { findModelByEnum } from '../model-enum';
|
|
7
|
-
import { findModelByInput } from '../model-inputs';
|
|
8
|
-
import { findModelByOperationSchema } from '../model-operations';
|
|
9
|
-
import { findModelByOutput } from '../model-outputs';
|
|
10
|
-
import { directives, printDirectives } from '../print/directive';
|
|
11
|
-
import { printPrismaEnum } from '../print/prisma-enum';
|
|
12
|
-
import { printPrismaEnumModel } from '../print/prisma-enum-model';
|
|
13
|
-
import { printPrismaInput } from '../print/prisma-input';
|
|
14
|
-
import { printPrismaModel } from '../print/prisma-model';
|
|
15
|
-
import { printPrismaOperation, printPrismaOutput } from '../print/prisma-output';
|
|
16
|
-
import { printScalars, scalars } from '../print/type';
|
|
17
|
-
import { createVisitorBuilder } from '../visitor';
|
|
18
|
-
import { createGlobalSchemas, createModuleSchemas } from './schema-builder-files';
|
|
19
|
-
import { isOperation } from './schema-builder-utils';
|
|
20
|
-
|
|
21
|
-
export interface GlobalSchemaDefinition {
|
|
22
|
-
enums: string[];
|
|
23
|
-
scalars: string[];
|
|
24
|
-
directives: string[];
|
|
25
|
-
inputTypes: string[];
|
|
26
|
-
outputTypes: string[];
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
export interface ModuleSchemaDefinition {
|
|
30
|
-
name: string;
|
|
31
|
-
model: string[];
|
|
32
|
-
enums: string[];
|
|
33
|
-
queries: string[];
|
|
34
|
-
mutations: string[];
|
|
35
|
-
inputTypes: string[];
|
|
36
|
-
outputTypes: string[];
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
export type ModuleDefinitionMap = Record<string, ModuleSchemaDefinition | undefined>;
|
|
40
|
-
|
|
41
|
-
export function createSchemaBuilder(store: Store, options: GenerateOptions) {
|
|
42
|
-
const { models, inputsMap, outputsMap, operationsMap } = store;
|
|
43
|
-
|
|
44
|
-
const moduleDefinitions: ModuleDefinitionMap = {};
|
|
45
|
-
|
|
46
|
-
let globalDefinitions: GlobalSchemaDefinition = {
|
|
47
|
-
enums: [],
|
|
48
|
-
scalars: scalars,
|
|
49
|
-
directives: directives,
|
|
50
|
-
inputTypes: [],
|
|
51
|
-
outputTypes: [],
|
|
52
|
-
};
|
|
53
|
-
|
|
54
|
-
const schema = [printScalars(), printDirectives()];
|
|
55
|
-
|
|
56
|
-
function pushDefinition(model: string, def: Partial<ModuleSchemaDefinition>) {
|
|
57
|
-
const current = moduleDefinitions[model] ?? {
|
|
58
|
-
name: model,
|
|
59
|
-
model: [],
|
|
60
|
-
queries: [],
|
|
61
|
-
mutations: [],
|
|
62
|
-
inputTypes: [],
|
|
63
|
-
outputTypes: [],
|
|
64
|
-
enums: [],
|
|
65
|
-
};
|
|
66
|
-
moduleDefinitions[model] = mergeDeepWith(concat, current, def);
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
function addToGlobal(def: Partial<GlobalSchemaDefinition>) {
|
|
70
|
-
globalDefinitions = mergeDeepWith(concat, globalDefinitions, def);
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
function addToSchema(value: string) {
|
|
74
|
-
schema.push(value);
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
const visitor = createVisitorBuilder();
|
|
78
|
-
|
|
79
|
-
visitor.onModel((model) => {
|
|
80
|
-
const content = printPrismaModel(model);
|
|
81
|
-
addToSchema(content);
|
|
82
|
-
pushDefinition(model.name, { model: [content] });
|
|
83
|
-
});
|
|
84
|
-
|
|
85
|
-
visitor.onModelEnum((enumModel) => {
|
|
86
|
-
const content = printPrismaEnumModel(enumModel);
|
|
87
|
-
addToSchema(content);
|
|
88
|
-
addToGlobal({ enums: [content] });
|
|
89
|
-
});
|
|
90
|
-
|
|
91
|
-
visitor.onSchemaEnum((enumType) => {
|
|
92
|
-
const content = printPrismaEnum(enumType);
|
|
93
|
-
const model = findModelByEnum(models, enumType);
|
|
94
|
-
|
|
95
|
-
addToSchema(content);
|
|
96
|
-
|
|
97
|
-
if (!model) {
|
|
98
|
-
return addToGlobal({ enums: [content] });
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
pushDefinition(model, { enums: [content] });
|
|
102
|
-
});
|
|
103
|
-
|
|
104
|
-
visitor.onInputType((inputType) => {
|
|
105
|
-
const content = printPrismaInput(inputType);
|
|
106
|
-
const model = findModelByInput(inputsMap, inputType);
|
|
107
|
-
|
|
108
|
-
addToSchema(content);
|
|
109
|
-
|
|
110
|
-
if (!model) {
|
|
111
|
-
return addToGlobal({ inputTypes: [content] });
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
pushDefinition(model, { inputTypes: [content] });
|
|
115
|
-
});
|
|
116
|
-
|
|
117
|
-
visitor.onOutputType((outputType) => {
|
|
118
|
-
const content = printPrismaOutput(outputType);
|
|
119
|
-
|
|
120
|
-
addToSchema(content);
|
|
121
|
-
|
|
122
|
-
if (isOperation(outputType.name)) {
|
|
123
|
-
return handleOperation(outputType, outputType.name);
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
const model = findModelByOutput(outputsMap, outputType);
|
|
127
|
-
|
|
128
|
-
if (!model) {
|
|
129
|
-
return addToGlobal({ outputTypes: [content] });
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
pushDefinition(model, { outputTypes: [content] });
|
|
133
|
-
});
|
|
134
|
-
|
|
135
|
-
function handleOperation(outputType: DMMF.OutputType, operation: 'Query' | 'Mutation') {
|
|
136
|
-
for (const field of outputType.fields) {
|
|
137
|
-
const model = findModelByOperationSchema(operationsMap, field);
|
|
138
|
-
|
|
139
|
-
if (!model) {
|
|
140
|
-
continue;
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
const content = printPrismaOperation(operationsMap, field);
|
|
144
|
-
|
|
145
|
-
if (!content) {
|
|
146
|
-
continue;
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
if (operation === 'Query') {
|
|
150
|
-
pushDefinition(model, {
|
|
151
|
-
queries: [content],
|
|
152
|
-
});
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
if (operation === 'Mutation') {
|
|
156
|
-
pushDefinition(model, {
|
|
157
|
-
mutations: [content],
|
|
158
|
-
});
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
function validate() {
|
|
164
|
-
validateSchema(buildSchema(schema.join('\n')));
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
function compose() {
|
|
168
|
-
validate();
|
|
169
|
-
|
|
170
|
-
const fileOptions = {
|
|
171
|
-
root: options.modulesDir,
|
|
172
|
-
namespace: options.schemaNamespace,
|
|
173
|
-
casing: options.casing,
|
|
174
|
-
};
|
|
175
|
-
|
|
176
|
-
return [
|
|
177
|
-
...createGlobalSchemas(globalDefinitions, fileOptions),
|
|
178
|
-
...createModuleSchemas(moduleDefinitions, fileOptions),
|
|
179
|
-
];
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
return {
|
|
183
|
-
visitor,
|
|
184
|
-
compose,
|
|
185
|
-
};
|
|
186
|
-
}
|
package/lib/visitor.ts
DELETED
|
@@ -1,119 +0,0 @@
|
|
|
1
|
-
import { DMMF } from '@prisma/generator-helper';
|
|
2
|
-
|
|
3
|
-
type VisitorInput<T> = (model: T) => void | Promise<void>;
|
|
4
|
-
|
|
5
|
-
export interface Visitor {
|
|
6
|
-
model: Array<VisitorInput<DMMF.Model>>;
|
|
7
|
-
modelEnum: Array<VisitorInput<DMMF.DatamodelEnum>>;
|
|
8
|
-
schemaEnum: Array<VisitorInput<DMMF.SchemaEnum>>;
|
|
9
|
-
inputType: Array<VisitorInput<DMMF.InputType>>;
|
|
10
|
-
outputType: Array<VisitorInput<DMMF.OutputType>>;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export type VisitorBuilder = ReturnType<typeof createVisitorBuilder>;
|
|
14
|
-
|
|
15
|
-
export function createVisitorBuilder() {
|
|
16
|
-
const visitor: Visitor = {
|
|
17
|
-
model: [],
|
|
18
|
-
modelEnum: [],
|
|
19
|
-
schemaEnum: [],
|
|
20
|
-
inputType: [],
|
|
21
|
-
outputType: [],
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
function getVisitor() {
|
|
25
|
-
return visitor;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
function onModel(input: VisitorInput<DMMF.Model>) {
|
|
29
|
-
visitor.model.push(input);
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
function onModelEnum(input: VisitorInput<DMMF.DatamodelEnum>) {
|
|
33
|
-
visitor.modelEnum.push(input);
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
function onSchemaEnum(input: VisitorInput<DMMF.SchemaEnum>) {
|
|
37
|
-
visitor.schemaEnum.push(input);
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
function onInputType(input: VisitorInput<DMMF.InputType>) {
|
|
41
|
-
visitor.inputType.push(input);
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
function onOutputType(input: VisitorInput<DMMF.OutputType>) {
|
|
45
|
-
visitor.outputType.push(input);
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
return {
|
|
49
|
-
getVisitor,
|
|
50
|
-
onModel,
|
|
51
|
-
onModelEnum,
|
|
52
|
-
onSchemaEnum,
|
|
53
|
-
onInputType,
|
|
54
|
-
onOutputType,
|
|
55
|
-
};
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
function visitItem<T>(
|
|
59
|
-
item: T,
|
|
60
|
-
visitor: Visitor,
|
|
61
|
-
getInputs: (visitor: Visitor) => Array<VisitorInput<T>>
|
|
62
|
-
) {
|
|
63
|
-
const handlers = getInputs(visitor);
|
|
64
|
-
return handlers.map((handler) => handler(item));
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
export function visitItems<T>(
|
|
68
|
-
items: T[],
|
|
69
|
-
visitors: Visitor[],
|
|
70
|
-
getInputs: (visitor: Visitor) => Array<VisitorInput<T>>
|
|
71
|
-
) {
|
|
72
|
-
const promises: Array<void | Promise<void>> = [];
|
|
73
|
-
|
|
74
|
-
for (const item of items) {
|
|
75
|
-
for (const visitor of visitors) {
|
|
76
|
-
promises.push(...visitItem(item, visitor, getInputs));
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
return Promise.all(promises);
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
export function visit(document: DMMF.Document, visitors: Visitor[]) {
|
|
84
|
-
const models = visitItems(
|
|
85
|
-
document.datamodel.models,
|
|
86
|
-
visitors,
|
|
87
|
-
(visitor) => visitor.model
|
|
88
|
-
);
|
|
89
|
-
|
|
90
|
-
const modelEnums = visitItems(
|
|
91
|
-
document.datamodel.enums,
|
|
92
|
-
visitors,
|
|
93
|
-
(visitor) => visitor.modelEnum
|
|
94
|
-
);
|
|
95
|
-
|
|
96
|
-
const inputTypes = visitItems(
|
|
97
|
-
document.schema.inputObjectTypes.prisma,
|
|
98
|
-
visitors,
|
|
99
|
-
(visitor) => visitor.inputType
|
|
100
|
-
);
|
|
101
|
-
|
|
102
|
-
const outputTypes = visitItems(
|
|
103
|
-
document.schema.outputObjectTypes.prisma,
|
|
104
|
-
visitors,
|
|
105
|
-
(visitor) => visitor.outputType
|
|
106
|
-
);
|
|
107
|
-
|
|
108
|
-
const enumTypes = visitItems(
|
|
109
|
-
document.schema.enumTypes.prisma,
|
|
110
|
-
visitors,
|
|
111
|
-
(visitor) => visitor.schemaEnum
|
|
112
|
-
);
|
|
113
|
-
|
|
114
|
-
return Promise.all([models, modelEnums, inputTypes, outputTypes, enumTypes]).then(
|
|
115
|
-
() => {
|
|
116
|
-
// do nothing
|
|
117
|
-
}
|
|
118
|
-
);
|
|
119
|
-
}
|
package/tsconfig.json
DELETED
package/utils/casing.ts
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import { camelCase, paramCase, pascalCase, snakeCase } from 'change-case-all';
|
|
2
|
-
|
|
3
|
-
export type Casing = 'camelCase' | 'PascalCase' | 'snake_case' | 'kebab-case';
|
|
4
|
-
|
|
5
|
-
export function changeCase(input: string, casing: Casing) {
|
|
6
|
-
if (casing === 'camelCase') {
|
|
7
|
-
return camelCase(input);
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
if (casing === 'PascalCase') {
|
|
11
|
-
return pascalCase(input);
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
if (casing === 'snake_case') {
|
|
15
|
-
return snakeCase(input);
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
if (casing === 'kebab-case') {
|
|
19
|
-
return paramCase(input);
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
return input;
|
|
23
|
-
}
|
package/utils/string.ts
DELETED