@baeta/plugin-prisma 0.0.2
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/.eslintrc.cjs +4 -0
- package/CHANGELOG.md +8 -0
- package/index.ts +47 -0
- package/lib/context.ts +29 -0
- package/lib/dmmf.ts +8 -0
- package/lib/generator.ts +24 -0
- package/lib/model-entries.ts +57 -0
- package/lib/model-enum.ts +17 -0
- package/lib/model-inputs.ts +32 -0
- package/lib/model-list.ts +5 -0
- package/lib/model-operations.ts +65 -0
- package/lib/model-outputs.ts +15 -0
- package/lib/model-relations.ts +35 -0
- package/lib/print/directive.ts +28 -0
- package/lib/print/module-prisma.ts +13 -0
- package/lib/print/module-resolver.ts +48 -0
- package/lib/print/module.ts +65 -0
- package/lib/print/object.ts +15 -0
- package/lib/print/prisma-enum-model.ts +7 -0
- package/lib/print/prisma-enum.ts +6 -0
- package/lib/print/prisma-input.ts +45 -0
- package/lib/print/prisma-model.ts +24 -0
- package/lib/print/prisma-output.ts +86 -0
- package/lib/print/prisma-scalar.ts +46 -0
- package/lib/print/type.ts +37 -0
- package/lib/resolver-builder/index.ts +1 -0
- package/lib/resolver-builder/resolver-builder-files.ts +69 -0
- package/lib/resolver-builder/resolver-builder.ts +80 -0
- package/lib/schema-builder/index.ts +1 -0
- package/lib/schema-builder/schema-builder-files.ts +150 -0
- package/lib/schema-builder/schema-builder-utils.ts +3 -0
- package/lib/schema-builder/schema-builder.ts +186 -0
- package/lib/visitor.ts +119 -0
- package/package.json +38 -0
- package/tsconfig.json +9 -0
- package/utils/casing.ts +23 -0
- package/utils/string.ts +9 -0
package/lib/visitor.ts
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
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/package.json
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@baeta/plugin-prisma",
|
|
3
|
+
"version": "0.0.2",
|
|
4
|
+
"license": "MIT",
|
|
5
|
+
"author": "Andrei Pampu <pampu.andrei@pm.me>",
|
|
6
|
+
"homepage": "https://github.com/andreisergiu98/baeta#readme",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "https://github.com/andreisergiu98/baeta.git",
|
|
10
|
+
"directory": "plugins/prisma"
|
|
11
|
+
},
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://github.com/andreisergiu98/baeta/issues"
|
|
14
|
+
},
|
|
15
|
+
"type": "module",
|
|
16
|
+
"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"
|
|
25
|
+
},
|
|
26
|
+
"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
|
+
}
|
|
38
|
+
}
|
package/tsconfig.json
ADDED
package/utils/casing.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
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
ADDED