mikro-orm-find-dataloader
Version:
Additional dataloaders for the MikroORM EntityManager find/findOne/etc methods.
412 lines (410 loc) • 17.6 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.isCol = exports.isRef = exports.hasCol = exports.hasRef = exports.assertHasNewFilterAndMapKey = exports.optsMapToQueries = exports.getFindBatchLoadFn = exports.groupFindQueriesByOpts = exports.groupInversedOrMappedKeysByEntity = exports.groupPrimaryKeysByEntity = void 0;
const core_1 = require("@mikro-orm/core");
/* eslint-enable @typescript-eslint/ban-types */
/* eslint-enable @typescript-eslint/array-type */
function groupPrimaryKeysByEntity(refs) {
const map = new Map();
for (const ref of refs) {
const className = (0, core_1.helper)(ref).__meta.className;
let primaryKeys = map.get(className);
if (primaryKeys == null) {
primaryKeys = new Set();
map.set(className, primaryKeys);
}
primaryKeys.add((0, core_1.helper)(ref).getPrimaryKey());
}
return map;
}
exports.groupPrimaryKeysByEntity = groupPrimaryKeysByEntity;
function groupInversedOrMappedKeysByEntity(collections) {
const entitiesMap = new Map();
for (const col of collections) {
const className = col.property.type;
let propMap = entitiesMap.get(className);
if (propMap == null) {
propMap = new Map();
entitiesMap.set(className, propMap);
}
// Many to Many vs One to Many
const inversedProp = col.property.inversedBy ?? col.property.mappedBy;
if (inversedProp == null) {
throw new Error("Cannot find inversedBy or mappedBy prop: did you forget to set the inverse side of a many-to-many relationship?");
}
let primaryKeys = propMap.get(inversedProp);
if (primaryKeys == null) {
primaryKeys = new Set();
propMap.set(inversedProp, primaryKeys);
}
primaryKeys.add((0, core_1.helper)(col.owner).getPrimaryKey());
}
return entitiesMap;
}
exports.groupInversedOrMappedKeysByEntity = groupInversedOrMappedKeysByEntity;
function allKeysArePK(keys, primaryKeys) {
if (keys == null) {
return false;
}
if (keys.length !== primaryKeys.length) {
return false;
}
for (const key of keys) {
if (!primaryKeys.includes(key)) {
return false;
}
}
return true;
}
// {id: 5, name: "a"} returns false because contains additional fields
// Returns true for all PK formats including {id: 1} or {owner: 1, recipient: 2}
function isPK(filter, meta) {
if (meta == null) {
return false;
}
if (meta.compositePK) {
// COMPOSITE
if (Array.isArray(filter)) {
// PK or PK[] or object[]
// [1, 2]
// [[1, 2], [3, 4]]
// [{owner: 1, recipient: 2}, {owner: 3, recipient: 4}]
// [{owner: 1, recipient: 2, sex: 0}, {owner: 3, recipient: 4, sex: 1}]
if (core_1.Utils.isPrimaryKey(filter, meta.compositePK)) {
// PK
return true;
}
if (core_1.Utils.isPrimaryKey(filter[0], meta.compositePK)) {
// PK[]
return true;
}
const keys = typeof filter[0] === "object" ? Object.keys(filter[0]) : undefined;
if (allKeysArePK(keys, meta.primaryKeys)) {
// object is PK or PK[]
return true;
}
}
else {
// object
// {owner: 1, recipient: 2, sex: 0}
const keys = typeof filter === "object" ? Object.keys(filter) : undefined;
if (allKeysArePK(keys, meta.primaryKeys)) {
// object is PK
return true;
}
}
}
else {
// NOT COMPOSITE
if (Array.isArray(filter)) {
// PK[]
// [1, 2]
// [{id: 1}, {id: 2}] NOT POSSIBLE FOR NON COMPOSITE
if (core_1.Utils.isPrimaryKey(filter[0])) {
return true;
}
}
else {
// PK or object
// 1
// {id: [1, 2], sex: 0} or {id: 1, sex: 0}
if (core_1.Utils.isPrimaryKey(filter)) {
// PK
return true;
}
const keys = typeof filter === "object" ? Object.keys(filter) : undefined;
if (keys?.length === 1 && keys[0] === meta.primaryKeys[0]) {
// object is PK
return true;
}
}
}
return false;
}
// Call this fn only if keyProp.targetMeta != null otherwise you will get false positives
// Returns only PKs in short-hand format like 1 or [1, 1] not {id: 1} or {owner: 1, recipient: 2}
function getPKs(filter, meta) {
if (meta.compositePK) {
// COMPOSITE
if (Array.isArray(filter)) {
// PK or PK[] or object[]
if (core_1.Utils.isPrimaryKey(filter, meta.compositePK)) {
// PK
return [filter];
}
if (core_1.Utils.isPrimaryKey(filter[0], meta.compositePK)) {
// PK[]
return filter;
}
}
}
else {
// NOT COMPOSITE
if (Array.isArray(filter)) {
// PK[] or object[]
if (core_1.Utils.isPrimaryKey(filter[0])) {
return filter;
}
}
else {
// PK or object
if (core_1.Utils.isPrimaryKey(filter)) {
// PK
return [filter];
}
}
}
}
/*
NOT COMPOSITE: NEW QUERY MAP KEY (props in alphabetical order)
1 -> {id: [1]} {id}
[1, 2] -> {id: [1, 2]} {id}
{id: 1, sex: 0} -> {id: [1], sex: [0]} {id,sex}
{id: [1, 2], sex: 0} -> {id: [1, 2], sex: [0]} {id,sex}
[{id: 1}, {id: 2}] NOT POSSIBLE FOR NON COMPOSITE
COMPOSITE PK:
[1, 2] -> {owner: [1], recipient: [2]} {owner,recipient}
[[1, 2], [3, 4]] -> {owner: [1, 2], recipient: [3, 4]} {owner,recipient}
{owner: 1, recipient: 2, sex: 0} -> {owner: [1], recipient: [2], sex: [0]} {owner,recipient,sex}
[{owner: 1, recipient: 2}, {owner: 3, recipient: 4}] -> {owner: [1, 3], recipient: [2, 4]} {owner,recipient}
[{owner: 1, recipient: 2, sex: 0}, {owner: 3, recipient: 4, sex: 1}] NOT POSSIBLE, MUST MATCH EXACTLY THE PK
[{owner: [1], recipient: [2], sex: 0} -> {owner: [1], recipient: [2], sex: [0]} // NOT A PK {owner,recipient,sex}
[{owner: [1], recipient: [2], sex: 0}, {owner: 3, recipient: 4, sex: 1}] -> {owner: [1, 3], recipient: [2, 4], sex: [0, 1]} // NOT A PK {owner,recipient,sex}
*/
const asc = (a, b) => a.localeCompare(b);
const notNull = (el) => el != null;
function getNewFiltersAndMapKeys(cur, meta, entityName) {
const PKs = getPKs(cur, meta);
if (PKs != null) {
const res = [
Object.fromEntries(meta.primaryKeys.map((pk, i) => [pk, meta.compositePK ? PKs[i] : PKs])),
[entityName, `{${meta.primaryKeys.sort(asc).join(",")}}`].filter(notNull).join("|"),
];
return entityName == null ? res : [res];
}
else {
const newFilter = {};
const keys = [];
if (Array.isArray(cur)) {
// COMPOSITE PKs like [{owner: 1, recipient: 2}, {recipient: 4, owner: 3}]
for (const key of meta.primaryKeys) {
newFilter[key] = cur.map((el) => {
if (el[key] == null) {
throw new Error(`Invalid query, missing composite PK ${key}`);
}
return el[key];
});
keys.push(key);
}
return [newFilter, `{${keys.sort(asc).join(",")}}`];
}
else {
for (const [key, value] of Object.entries(cur)) {
// Using $or at the top level means that we can treat it as two separate queries and filter results from either of them
if (key === "$or" && entityName != null) {
return value
.map((el) => getNewFiltersAndMapKeys(el, meta, entityName))
.flat();
}
const keyProp = meta.properties[key];
if (keyProp == null) {
throw new Error(`Cannot find properties for ${key}`);
}
if (keyProp.targetMeta == null) {
newFilter[key] = Array.isArray(value) ? value : [value];
keys.push(key);
}
else {
const [subFilter, subKey] = getNewFiltersAndMapKeys(value, keyProp.targetMeta);
newFilter[key] = subFilter;
keys.push(`${key}:${subKey}`);
}
}
const res = [
newFilter,
[entityName, `{${keys.sort(asc).join(",")}}`].filter(notNull).join("|"),
];
return entityName == null ? res : [res];
}
}
}
// The purpose of this function on a freshly created query map is just to add populate options
// to the query map. A brand new query map already contains an array with the current element
// as its sole value so there is no need to update it, otherwise we would get the cur element twice.
// TODO: use Sets to avoid duplicates even in subsequent updates.
function updateQueryFilter([acc, accOptions], cur, options, newQueryMap) {
if (options?.populate != null && accOptions != null && accOptions.populate !== true) {
if (Array.isArray(options.populate) && options.populate.includes("*")) {
accOptions.populate = true;
}
else if (Array.isArray(options.populate)) {
if (accOptions.populate == null) {
accOptions.populate = new Set(options.populate);
}
else {
for (const el of options.populate) {
accOptions.populate.add(el);
}
}
}
}
if (newQueryMap !== true) {
for (const [key, value] of Object.entries(acc)) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const curValue = cur[key];
if (Array.isArray(value)) {
// value.push(...curValue.reduce<any[]>((acc, cur) => acc.concat(cur), []));
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
value.push(...structuredClone(curValue));
}
else {
updateQueryFilter([value], curValue);
}
}
}
}
function getMandatoryPopulate(cur, meta, options) {
for (const [key, value] of Object.entries(cur)) {
const keyProp = meta.properties[key];
if (keyProp == null) {
throw new Error(`Cannot find properties for ${key}`);
}
// If our current key leads to scalar we don't need to populate anything
if (keyProp.targetMeta != null) {
// Our current key points to either a Reference or a Collection
// We need to populate all Collections
// We also need to populate References whenever we have to further match non-PKs properties
if (keyProp.ref !== true || !isPK(value, keyProp.targetMeta)) {
const furtherPop = getMandatoryPopulate(value, keyProp.targetMeta);
const computedPopulate = furtherPop == null ? `${key}` : `${key}.${furtherPop}`;
if (options != null) {
if (options.populate == null) {
options.populate = new Set();
}
options.populate.add(computedPopulate);
}
else {
return computedPopulate;
}
}
}
}
}
function groupFindQueriesByOpts(dataloaderFinds) {
const queriesMap = new Map();
for (const dataloaderFind of dataloaderFinds) {
const { entityName, meta, filter, options } = dataloaderFind;
const filtersAndKeys = getNewFiltersAndMapKeys(filter, meta, entityName);
dataloaderFind.filtersAndKeys = [];
filtersAndKeys.forEach(([newFilter, key]) => {
dataloaderFind.filtersAndKeys?.push({ key, newFilter });
let queryMap = queriesMap.get(key);
if (queryMap == null) {
const queryMapOpts = {};
queryMap = [structuredClone(newFilter), queryMapOpts];
getMandatoryPopulate(newFilter, meta, queryMapOpts);
updateQueryFilter(queryMap, newFilter, options, true);
queriesMap.set(key, queryMap);
}
else {
updateQueryFilter(queryMap, newFilter, options);
}
});
}
return queriesMap;
}
exports.groupFindQueriesByOpts = groupFindQueriesByOpts;
function getFindBatchLoadFn(em, entityName) {
return async (dataloaderFinds) => {
const optsMap = groupFindQueriesByOpts(dataloaderFinds);
assertHasNewFilterAndMapKey(dataloaderFinds);
const promises = optsMapToQueries(optsMap, em, entityName);
const resultsMap = new Map(await Promise.all(promises));
return dataloaderFinds.map(({ filtersAndKeys, many }) => {
const res = filtersAndKeys.reduce((acc, { key, newFilter }) => {
const entities = resultsMap.get(key);
if (entities == null) {
// Should never happen
/* istanbul ignore next */
throw new Error("Cannot match results");
}
const res = entities[many ? "filter" : "find"]((entity) => {
return filterResult(entity, newFilter);
});
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
acc.push(...(Array.isArray(res) ? res : [res]));
return acc;
}, []);
return many ? res : res[0] ?? null;
});
function filterResult(entity, filter) {
for (const [key, value] of Object.entries(filter)) {
const entityValue = entity[key];
if (Array.isArray(value)) {
// Our current filter is an array
if (Array.isArray(entityValue)) {
// Collection
if (!value.every((el) => entityValue.includes(el))) {
return false;
}
}
else {
// Single value
if (!value.includes(entityValue)) {
return false;
}
}
}
else {
// Our current filter is an object
if (entityValue instanceof core_1.Collection) {
if (!entityValue.getItems().some((entity) => filterResult(entity, value))) {
return false;
}
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
}
else if (!filterResult(entityValue, value)) {
return false;
}
}
}
return true;
}
};
}
exports.getFindBatchLoadFn = getFindBatchLoadFn;
function optsMapToQueries(optsMap, em, entityName) {
return Array.from(optsMap, async ([key, [filter, options]]) => {
const findOptions = {
...(options?.populate != null && {
populate: options.populate === true ? ["*"] : Array.from(options.populate),
}),
};
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
const entities = await em.find(entityName, filter, findOptions);
return [key, entities];
});
}
exports.optsMapToQueries = optsMapToQueries;
function assertHasNewFilterAndMapKey(dataloaderFinds) {
/* if (dataloaderFinds.some((el) => el.key == null || el.newFilter == null)) {
throw new Error("Missing key or newFilter");
} */
}
exports.assertHasNewFilterAndMapKey = assertHasNewFilterAndMapKey;
function hasRef(entity) {
return entity;
}
exports.hasRef = hasRef;
function hasCol(entity) {
return entity;
}
exports.hasCol = hasCol;
function isRef(refOrCol) {
return !(refOrCol instanceof core_1.Collection);
}
exports.isRef = isRef;
function isCol(refOrCol) {
return refOrCol instanceof core_1.Collection;
}
exports.isCol = isCol;