Source: Album.js

const { coreDataDateToJSDate } = require('./helpers');

/**
 * The class that contains information about an album.
 */
class Album {
	/**
	 * Create a new Album class.
	 * @param  {PhotosLibrary} library An instance of PhotosLibrary used for media fetching.
	 * @param  {Object} row A row of database data from `RKAlbum`.
	 * @return {Album} The created Album
	 */
	constructor(library, row) {
		/**
		 * The library used for media fetching.
		 * @type {PhotosLibrary}
		 */
		this.library = library;

		/**
		 * The ID of the Album in the database (database field: modelId).
		 * @type {int}
		 */
		this.id = row.modelId;

		/**
		 * The uuid of the Album in the database (database field: uuid).
		 * @type {string}
		 */
		this.uuid = row.uuid;

		/**
		 * The cloud ID of the Album. (database field: cloudIdentifier).
		 * @type {string}
		 */
		this.cloudId = row.cloudIdentifier;

		/**
		 * The name of the Album. (database field: name).
		 * @type {string}
		 */
		this.name = row.name;

		/**
		 * The type of the Album. (database field: albumType).
		 * @type {int}
		 */
		this.type = row.albumType;

		/**
		 * An Album is magic if it is generated by Apple Photos and not by the user (e.g. Favorites).
		 * @type {Bool}
		 */
		this.magic = Boolean(row.isMagic);

		/**
		 * The date the Album was created. (database field: createDate).
		 *
		 * The database value is converted from a Apple Core Data date to a Javascript Date object.
		 * @type {Date}
		 */
		this.created = coreDataDateToJSDate(row.createDate);

		/**
		 * The uuid of the parent Folder the Album is in. (database field: folderUuid).
		 * @type {string}
		 */
		this.folderUuid = row.folderUuid;

		/**
		 * A list of all the media in the album. Use the media() function to get.
		 * @private
		 * @type {Array.<Media>}
		 */
		this._media = null;
	}

	/**
	 * Load metadata about all pictures and videos from this Album.
	 * @return {Array.<Media>} A list of all pictures and videos from the Album.
	 */
	async media() {
		if (this._media)
			return this._media;

		this._media = await this.library.getMediaForAlbum(this.id);

		return this._media;
	}

	/**
	 * Prepare the Album for JSON serialization.
	 *
	 * This function is needed to exclude the library property from JSON serialization.
	 * @return {Object} The filtered object.
	 */
	toJSON() {
		return {
			...this,
			library: undefined
		}
	}
}

module.exports = Album;