UNPKG

@cheetah.js/orm

Version:
87 lines (86 loc) 2.25 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Reference = void 0; exports.ref = ref; exports.unwrap = unwrap; exports.isLoaded = isLoaded; exports.refById = refById; /** * Creates a reference to an entity. * This is an identity function - it returns the input unchanged. * Useful for explicit ref creation when needed. * * @param entity - The entity to wrap in a reference * @returns The same entity (identity function) * * @example * ```typescript * const user = new User(); * const userRef = ref(user); // userRef === user (same reference) * ``` */ function ref(entity) { return entity; } /** * Unwraps a reference to get the underlying entity. * This is an identity function - it returns the input unchanged. * Provided for API consistency and explicitness. * * @param reference - The reference to unwrap * @returns The underlying entity (same as input) * * @example * ```typescript * const post = await Post.findOne({ id: 1 }); * const author = unwrap(post.author); // author === post.author * ``` */ function unwrap(reference) { return reference; } /** * Type guard to check if a value is not null or undefined. * Useful when working with optional references. * * @param value - The value to check * @returns True if value is not null/undefined * * @example * ```typescript * const post = await Post.findOne({ id: 1 }); * if (isLoaded(post.author)) { * console.log(post.author.name); // TypeScript knows author is defined * } * ``` */ function isLoaded(value) { return value != null; } /** * @deprecated Use `Ref<T>` type instead. This class is kept for backward compatibility. */ class Reference { constructor(entity) { this.entity = entity; } get() { return this.entity; } } exports.Reference = Reference; /** * Creates a lightweight entity reference by class and id without hitting the DB. * Useful to assign many-to-one relations when only the id is known. * * Example: * const library = await UserLibrary.create({ * user: refById(User, userId), * course: refById(Course, courseId), * }); */ function refById(Cls, id) { const entity = new Cls(); entity.id = id; return entity; }