prisma-mock 0.0.14 → 0.0.15
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/lib/index.js +73 -56
- package/package.json +1 -1
package/lib/index.js
CHANGED
|
@@ -72,80 +72,97 @@ const createPrismaMock = async (data = {}, pathToSchema, client = (0, jest_mock_
|
|
|
72
72
|
return Promise.all(actions);
|
|
73
73
|
});
|
|
74
74
|
const Delegate = (prop, model) => {
|
|
75
|
-
const nestedUpdate = (args, isCreating) => {
|
|
75
|
+
const nestedUpdate = (args, isCreating, item) => {
|
|
76
76
|
let d = args.data;
|
|
77
77
|
// Get field schema for default values
|
|
78
78
|
const model = cachedSchema.datamodel.models.find(model => {
|
|
79
79
|
return getCamelCase(model.name) === prop;
|
|
80
80
|
});
|
|
81
81
|
model.fields.forEach(field => {
|
|
82
|
-
if (d[field.name]
|
|
82
|
+
if (d[field.name]) {
|
|
83
83
|
const c = d[field.name];
|
|
84
|
-
if (
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
if (c.create || c.createMany) {
|
|
89
|
-
const _c = d, _d = field.name, create = _c[_d], rest = __rest(_c, [typeof _d === "symbol" ? _d : _d + ""]);
|
|
90
|
-
d = rest;
|
|
91
|
-
// @ts-ignore
|
|
92
|
-
const name = getCamelCase(field.type);
|
|
93
|
-
const delegate = Delegate(name, model);
|
|
94
|
-
const joinfield = getJoinField(field);
|
|
95
|
-
if (field.relationFromFields.length > 0) {
|
|
96
|
-
const item = delegate.create({
|
|
97
|
-
data: create.create
|
|
98
|
-
});
|
|
99
|
-
d = Object.assign(Object.assign({}, rest), { [field.relationFromFields[0]]: item[field.relationToFields[0]] });
|
|
84
|
+
if (field.kind === 'object') {
|
|
85
|
+
if (c.connect) {
|
|
86
|
+
const _a = d, _b = field.name, connect = _a[_b], rest = __rest(_a, [typeof _b === "symbol" ? _b : _b + ""]);
|
|
87
|
+
d = Object.assign(Object.assign({}, rest), { [field.relationFromFields[0]]: connect.connect[field.relationToFields[0]] });
|
|
100
88
|
}
|
|
101
|
-
|
|
102
|
-
const
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
89
|
+
if (c.create || c.createMany) {
|
|
90
|
+
const _c = d, _d = field.name, create = _c[_d], rest = __rest(_c, [typeof _d === "symbol" ? _d : _d + ""]);
|
|
91
|
+
d = rest;
|
|
92
|
+
// @ts-ignore
|
|
93
|
+
const name = getCamelCase(field.type);
|
|
94
|
+
const delegate = Delegate(name, model);
|
|
95
|
+
const joinfield = getJoinField(field);
|
|
96
|
+
if (field.relationFromFields.length > 0) {
|
|
97
|
+
const item = delegate.create({
|
|
98
|
+
data: create.create
|
|
99
|
+
});
|
|
100
|
+
d = Object.assign(Object.assign({}, rest), { [field.relationFromFields[0]]: item[field.relationToFields[0]] });
|
|
113
101
|
}
|
|
114
102
|
else {
|
|
115
|
-
|
|
116
|
-
|
|
103
|
+
const map = (val) => (Object.assign(Object.assign({}, val), { [joinfield.name]: {
|
|
104
|
+
connect: joinfield.relationToFields.reduce((prev, cur, index) => {
|
|
105
|
+
let val = d[cur];
|
|
106
|
+
if (!isCreating && !val) {
|
|
107
|
+
val = findOne(args)[cur];
|
|
108
|
+
}
|
|
109
|
+
return Object.assign(Object.assign({}, prev), { [cur]: val });
|
|
110
|
+
}, {}),
|
|
111
|
+
} }));
|
|
112
|
+
if (c.createMany) {
|
|
113
|
+
delegate.createMany(Object.assign(Object.assign({}, c.createMany), { data: c.createMany.data.map(map) }));
|
|
117
114
|
}
|
|
118
115
|
else {
|
|
119
|
-
|
|
116
|
+
if (Array.isArray(c.create)) {
|
|
117
|
+
delegate.createMany(Object.assign(Object.assign({}, c.create), { data: c.create.map(map) }));
|
|
118
|
+
}
|
|
119
|
+
else {
|
|
120
|
+
delegate.create(Object.assign(Object.assign({}, create.create), { data: map(create.create) }));
|
|
121
|
+
}
|
|
120
122
|
}
|
|
121
123
|
}
|
|
122
124
|
}
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
else {
|
|
138
|
-
if (Array.isArray(c.update)) {
|
|
139
|
-
c.update.forEach(update => {
|
|
140
|
-
delegate.update(update);
|
|
141
|
-
});
|
|
125
|
+
if (c.update || c.updateMany) {
|
|
126
|
+
const name = getCamelCase(field.type);
|
|
127
|
+
const delegate = Delegate(name, model);
|
|
128
|
+
if (c.updateMany) {
|
|
129
|
+
if (Array.isArray(c.updateMany)) {
|
|
130
|
+
c.updateMany.forEach(updateMany => {
|
|
131
|
+
delegate.updateMany(updateMany);
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
else {
|
|
135
|
+
delegate.updateMany(c.updateMany);
|
|
136
|
+
}
|
|
142
137
|
}
|
|
143
138
|
else {
|
|
144
|
-
|
|
145
|
-
|
|
139
|
+
if (Array.isArray(c.update)) {
|
|
140
|
+
c.update.forEach(update => {
|
|
141
|
+
delegate.update(update);
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
else {
|
|
145
|
+
const item = findOne(args);
|
|
146
|
+
delegate.update({ data: c.update, where: getFieldRelationshipWhere(item, field) });
|
|
147
|
+
}
|
|
146
148
|
}
|
|
147
149
|
}
|
|
148
150
|
}
|
|
151
|
+
if (c.increment) {
|
|
152
|
+
d = Object.assign(Object.assign({}, d), { [field.name]: item[field.name] + c.increment });
|
|
153
|
+
}
|
|
154
|
+
if (c.decrement) {
|
|
155
|
+
d = Object.assign(Object.assign({}, d), { [field.name]: item[field.name] - c.decrement });
|
|
156
|
+
}
|
|
157
|
+
if (c.multiply) {
|
|
158
|
+
d = Object.assign(Object.assign({}, d), { [field.name]: item[field.name] * c.multiply });
|
|
159
|
+
}
|
|
160
|
+
if (c.divide) {
|
|
161
|
+
d = Object.assign(Object.assign({}, d), { [field.name]: item[field.name] / c.divide });
|
|
162
|
+
}
|
|
163
|
+
if (c.set) {
|
|
164
|
+
d = Object.assign(Object.assign({}, d), { [field.name]: c.set });
|
|
165
|
+
}
|
|
149
166
|
}
|
|
150
167
|
if (isCreating && !d[field.name] && field.default) {
|
|
151
168
|
if (typeof field.default === 'object') {
|
|
@@ -316,7 +333,7 @@ const createPrismaMock = async (data = {}, pathToSchema, client = (0, jest_mock_
|
|
|
316
333
|
// }
|
|
317
334
|
const newItems = data[prop].map(e => {
|
|
318
335
|
if (matchFnc(args.where)(e)) {
|
|
319
|
-
let data = nestedUpdate(args, false);
|
|
336
|
+
let data = nestedUpdate(args, false, e);
|
|
320
337
|
return Object.assign(Object.assign({}, e), data);
|
|
321
338
|
}
|
|
322
339
|
return e;
|
|
@@ -325,7 +342,7 @@ const createPrismaMock = async (data = {}, pathToSchema, client = (0, jest_mock_
|
|
|
325
342
|
return data;
|
|
326
343
|
};
|
|
327
344
|
const create = args => {
|
|
328
|
-
const d = nestedUpdate(args, true);
|
|
345
|
+
const d = nestedUpdate(args, true, null);
|
|
329
346
|
data = Object.assign(Object.assign({}, data), { [prop]: [...data[prop], d] });
|
|
330
347
|
data = checkIds(model, data);
|
|
331
348
|
return findOne(Object.assign({ where: { id: d.id } }, args));
|