* Search for a single instance by its primary key._ * * @param {number|bigint|string|Buffer} param The value of the desired instance's primary key. * @param {object} [options] find options * @param {Transaction} [options.transaction]
(param, options)
| 1935 | * @returns {Promise<Model>} |
| 1936 | */ |
| 1937 | static async findByPk(param, options) { |
| 1938 | // return Promise resolved with null if no arguments are passed |
| 1939 | if ([null, undefined].includes(param)) { |
| 1940 | return null; |
| 1941 | } |
| 1942 | |
| 1943 | options = Utils.cloneDeep(options) || {}; |
| 1944 | |
| 1945 | if (typeof param === 'number' || typeof param === 'bigint' || typeof param === 'string' || Buffer.isBuffer(param)) { |
| 1946 | options.where = { |
| 1947 | [this.primaryKeyAttribute]: param |
| 1948 | }; |
| 1949 | } else { |
| 1950 | throw new Error(`Argument passed to findByPk is invalid: ${param}`); |
| 1951 | } |
| 1952 | |
| 1953 | // Bypass a possible overloaded findOne |
| 1954 | // note: in v6, we don't bypass overload https://github.com/sequelize/sequelize/issues/14003 |
| 1955 | return await this.findOne(options); |
| 1956 | } |
| 1957 | |
| 1958 | /** |
| 1959 | * Search for a single instance. Returns the first instance found, or null if none can be found. |