@signaldb/core
Version:
SignalDB is a client-side database that provides a simple MongoDB-like interface to the data with first-class typescript support to achieve an optimistic UI. Data persistence can be achieved by using storage providers that store the data through a JSON in
46 lines (45 loc) • 1.74 kB
JavaScript
const require_serializeValue = require("./index15.cjs.js");
const require_isFieldExpression = require("./index20.cjs.js");
//#region src/utils/getMatchingKeys.ts
/**
* Extracts the matching and excluded keys for a given field in a selector.
* Supports serialized values and `$in`/`$nin` field expressions for optimization.
* Returns `null` for include/exclude if the field cannot be optimized.
* @template T - The type of the items in the selector.
* @template I - The type of the unique identifier for the items.
* @param field - The name of the field to extract matching keys for.
* @param selector - The selector object containing query criteria.
* @returns An object containing arrays of serialized included and excluded keys,
* or `null` if the field cannot be optimized.
*/
function getMatchingKeys(field, selector) {
const result = {
include: null,
exclude: null
};
const fieldSelector = selector[field];
if (fieldSelector instanceof RegExp) return result;
if (fieldSelector == null) return result;
if (require_isFieldExpression.default(fieldSelector)) {
if (fieldSelector.$ne != null) {
result.exclude = [require_serializeValue.default(fieldSelector.$ne)];
return result;
}
if (Array.isArray(fieldSelector.$in) && fieldSelector.$in.length > 0) {
result.include = fieldSelector.$in.map(require_serializeValue.default);
return result;
}
if (Array.isArray(fieldSelector.$nin) && fieldSelector.$nin.length > 0) {
result.exclude = fieldSelector.$nin.map(require_serializeValue.default);
return result;
}
return {
include: null,
exclude: null
};
}
result.include = [require_serializeValue.default(fieldSelector)];
return result;
}
//#endregion
exports.default = getMatchingKeys;