prisma-mock
Advanced tools
Comparing version 0.10.0 to 0.11.0-alpha.1
@@ -36,2 +36,3 @@ "use strict"; | ||
const getNestedValue_1 = __importDefault(require("./utils/getNestedValue")); | ||
const indexes_1 = __importDefault(require("./indexes")); | ||
function IsFieldDefault(f) { | ||
@@ -66,2 +67,3 @@ return f.name !== undefined; | ||
const manyToManyData = {}; | ||
const indexes = (0, indexes_1.default)(); | ||
// let data = options.data || {} | ||
@@ -76,2 +78,3 @@ // const datamodel = options.datamodel || Prisma.dmmf.datamodel | ||
const removeMultiFieldIds = (model, data) => { | ||
/// [tableName][field][value] = Array | ||
const c = getCamelCase(model.name); | ||
@@ -341,2 +344,5 @@ const idFields = model.idFields || model.primaryKey?.fields; | ||
if (c.create || c.createMany) { | ||
const otherModel = datamodel.models.find((model) => { | ||
return model.name === field.type; | ||
}); | ||
const { [field.name]: create, ...rest } = d; | ||
@@ -346,3 +352,3 @@ d = rest; | ||
const name = getCamelCase(field.type); | ||
const delegate = Delegate(name, model); | ||
const delegate = Delegate(name, otherModel); | ||
const joinfield = getJoinField(field); | ||
@@ -602,2 +608,3 @@ if (field.relationFromFields.length > 0) { | ||
} | ||
// return true | ||
const res = delegate.findMany({ | ||
@@ -832,5 +839,15 @@ where: { | ||
const findMany = (args) => { | ||
let res = data[prop] | ||
.filter(matchFnc(args?.where)) | ||
.map(includes(args)); | ||
const match = matchFnc(args?.where); | ||
const inc = includes(args); | ||
// let res = data[prop] | ||
// .filter(matchFnc(args?.where)) | ||
// .map(includes(args)) | ||
let items = indexes.getIndexedItems(prop, args?.where) || data[prop]; | ||
let res = []; | ||
for (const item of items) { | ||
if (match(item)) { | ||
const i = inc(item); | ||
res.push(i); | ||
} | ||
} | ||
if (args?.distinct) { | ||
@@ -896,6 +913,8 @@ let values = {}; | ||
nbUpdated++; | ||
return { | ||
const newItem = { | ||
...e, | ||
...data, | ||
}; | ||
indexes.updateItem(prop, newItem); | ||
return newItem; | ||
} | ||
@@ -924,3 +943,5 @@ return e; | ||
} | ||
return findOne({ where, ...args }); | ||
const item = findOne({ where, ...args }); | ||
indexes.updateItem(prop, item); | ||
return item; | ||
}; | ||
@@ -963,2 +984,3 @@ const createMany = (args) => { | ||
model.fields.forEach((field) => { | ||
indexes.deleteItemByField(prop, field, item); | ||
const joinfield = getJoinField(field); | ||
@@ -969,10 +991,13 @@ if (!joinfield) | ||
if (joinfield.relationOnDelete === "SetNull") { | ||
delegate.update({ | ||
where: { | ||
[joinfield.relationFromFields[0]]: item[joinfield.relationToFields[0]], | ||
}, | ||
data: { | ||
[joinfield.relationFromFields[0]]: null, | ||
}, | ||
}); | ||
try { | ||
delegate.update({ | ||
where: { | ||
[joinfield.relationFromFields[0]]: item[joinfield.relationToFields[0]], | ||
}, | ||
data: { | ||
[joinfield.relationFromFields[0]]: null, | ||
}, | ||
}); | ||
} | ||
catch (e) { } | ||
} | ||
@@ -1079,6 +1104,8 @@ else if (joinfield.relationOnDelete === "Cascade") { | ||
let updatedItem; | ||
let hasMatch = false; | ||
const match = matchFnc(args.where); | ||
const newItems = data[prop].map((e) => { | ||
if (matchFnc(args.where)(e)) { | ||
if (match(e)) { | ||
hasMatch = true; | ||
let data = nestedUpdate(args, false, e); | ||
data; //? | ||
updatedItem = { | ||
@@ -1088,2 +1115,3 @@ ...e, | ||
}; | ||
indexes.updateItem(prop, updatedItem); | ||
return updatedItem; | ||
@@ -1093,2 +1121,5 @@ } | ||
}); | ||
if (!hasMatch) { | ||
throwKnownError("An operation failed because it depends on one or more records that were required but not found. Record to update not found.", { meta: { cause: "Record to update not found." } }); | ||
} | ||
data = { | ||
@@ -1177,2 +1208,8 @@ ...data, | ||
data = removeMultiFieldIds(model, data); | ||
model.fields.forEach((field) => { | ||
indexes.addIndexFieldIfNeeded(c, field); | ||
}); | ||
data[c].forEach((item) => { | ||
indexes.updateItem(c, item); | ||
}); | ||
const objs = Delegate(c, model); | ||
@@ -1179,0 +1216,0 @@ Object.keys(objs).forEach((fncName) => { |
{ | ||
"name": "prisma-mock", | ||
"version": "0.10.0", | ||
"version": "0.11.0-alpha.1", | ||
"description": "Mock prisma for unit testing database", | ||
@@ -24,3 +24,3 @@ "main": "lib/index.js", | ||
"scripts": { | ||
"preversion": "tsc", | ||
"preversion": "jest && tsc", | ||
"build": "tsc", | ||
@@ -27,0 +27,0 @@ "test": "jest", |
@@ -191,3 +191,3 @@ # Prisma Mock | ||
- @default | ||
- @unique (TODO: no error if duplicate) | ||
- @unique | ||
- @@unique (TODO: no error if duplicate) | ||
@@ -194,0 +194,0 @@ - @relation |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
77550
29
1639