-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathResource.ts
216 lines (164 loc) · 6.51 KB
/
Resource.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
/* eslint-disable class-methods-use-this */
/* eslint-disable no-param-reassign */
import { BaseResource, Filter, BaseRecord, flat } from 'adminjs';
import { PrismaClient } from '@prisma/client';
import { DMMF } from '@prisma/client/runtime';
import { Property } from './Property';
import { lowerCase } from './utils/helpers';
import { ModelManager, Enums } from './types';
import { convertFilter, convertParam } from './utils/converters';
export class Resource extends BaseResource {
private client: PrismaClient;
private model: DMMF.Model;
private enums: Enums;
private manager: ModelManager;
private propertiesObject: Record<string, any>;
constructor(args: { model: DMMF.Model, client: PrismaClient }) {
super(args);
const { model, client } = args;
this.model = model;
this.client = client;
this.enums = (this.client as any)._baseDmmf.datamodelEnumMap;
this.manager = this.client[lowerCase(model.name)];
this.propertiesObject = this.prepareProperties();
}
public databaseName(): string {
return 'prisma';
}
public databaseType(): string {
return (this.client as any)._engineConfig.activeProvider ?? 'database';
}
public id(): string {
return this.model.name;
}
public properties(): Array<Property> {
return [...Object.values(this.propertiesObject)];
}
public property(path: string): Property | null {
return this.propertiesObject[path] ?? null;
}
public build(params: Record<string, any>): BaseRecord {
return new BaseRecord(flat.unflatten(params), this);
}
public async count(filter: Filter): Promise<number> {
return this.manager.count({ where: convertFilter(this.model.fields, filter) });
}
public async find(filter: Filter, params: Record<string, any> = {}): Promise<Array<BaseRecord>> {
const { limit = 10, offset = 0, sort = {} } = params;
const { direction, sortBy } = sort as { direction: 'asc' | 'desc', sortBy: string };
const results = await this.manager.findMany({
where: convertFilter(this.model.fields, filter),
skip: offset,
take: limit,
orderBy: {
[sortBy]: direction,
},
});
return results.map((result) => new BaseRecord(this.prepareReturnValues(result), this));
}
public async findOne(id: string | number): Promise<BaseRecord | null> {
const idProperty = this.properties().find((property) => property.isId());
if (!idProperty) return null;
const result = await this.manager.findUnique({
where: {
[idProperty.path()]: convertParam(idProperty, this.model.fields, id),
},
});
return new BaseRecord(this.prepareReturnValues(result), this);
}
public async findMany(
ids: Array<string | number>,
): Promise<Array<BaseRecord>> {
const idProperty = this.properties().find((property) => property.isId());
if (!idProperty) return [];
const results = await this.manager.findMany({
where: {
[idProperty.path()]: {
in: ids.map((id) => convertParam(idProperty, this.model.fields, id)),
},
},
});
return results.map((result) => new BaseRecord(this.prepareReturnValues(result), this));
}
public async create(params: Record<string, any>): Promise<Record<string, any>> {
const preparedParams = this.prepareParams(params);
const result = await this.manager.create({ data: preparedParams });
return this.prepareReturnValues(result);
}
public async update(pk: string | number, params: Record<string, any> = {}): Promise<Record<string, any>> {
const idProperty = this.properties().find((property) => property.isId());
if (!idProperty) return {};
const preparedParams = this.prepareParams(params);
const result = await this.manager.update({
where: {
[idProperty.path()]: convertParam(idProperty, this.model.fields, pk),
},
data: preparedParams,
});
return this.prepareReturnValues(result);
}
public async delete(id: string | number): Promise<void> {
const idProperty = this.properties().find((property) => property.isId());
if (!idProperty) return;
await this.manager.delete({
where: {
[idProperty.path()]: convertParam(idProperty, this.model.fields, id),
},
});
}
public static isAdapterFor(args: { model: DMMF.Model, client: PrismaClient }): boolean {
const { model, client } = args;
return !!model?.name && !!model?.fields.length && !!client?.[lowerCase(model.name)];
}
private prepareProperties(): { [propertyPath: string]: Property } {
const { fields = [] } = this.model;
return fields.reduce((memo, field) => {
if (field.isReadOnly || (field.relationName && !field.relationFromFields?.length)) {
return memo;
}
const property = new Property(field, Object.keys(memo).length, this.enums);
memo[property.path()] = property;
return memo;
}, {});
}
private prepareParams(params: Record<string, any>): Record<string, any> {
const preparedParams: Record<string, any> = {};
for (const property of this.properties()) {
const param = flat.get(params, property.path());
const key = property.path();
// eslint-disable-next-line no-continue
if (param === undefined) continue;
const type = property.type();
const foreignColumnName = property.foreignColumnName();
if (type === 'reference' && foreignColumnName) {
preparedParams[foreignColumnName] = convertParam(property, this.model.fields, param);
// eslint-disable-next-line no-continue
continue;
}
if (property.isArray()) {
preparedParams[key] = param ? param.map((p) => convertParam(property, this.model.fields, p)) : param
} else {
preparedParams[key] = convertParam(property, this.model.fields, param)
}
}
return preparedParams;
}
private prepareReturnValues(params: Record<string, any>): Record<string, any> {
const preparedValues: Record<string, any> = {};
for (const property of this.properties()) {
const param = flat.get(params, property.path());
const key = property.path();
if (param !== undefined && property.type() !== 'reference') {
preparedValues[key] = param;
// eslint-disable-next-line no-continue
continue;
}
const foreignColumnName = property.foreignColumnName();
// eslint-disable-next-line no-continue
if (!foreignColumnName) continue;
preparedValues[key] = params[foreignColumnName];
}
return preparedValues;
}
}