import { BaseWriteActions } from "./base-write-actions.js" ; import { FindOrCreateActionInterface , TransactionModes } from "./model.interface.js" ; import { InvalidTransaction } from "../errors/invalid-transaction.js" ; export abstract class FindOrCreateActions extends BaseWriteActions implements FindOrCreateActionInterface { param data throws public async firstOrCreate < T > ( data ) ; public async firstOrCreate ( data ) { const tables = this . tableNames ( this . connector . migrationSchema . tables ) . concat ( this . table . name ) ; const transaction = this . createTransaction ( tables , TransactionModes . Write ) ; if ( transaction . mode !== TransactionModes . Write ) { throw new InvalidTransaction ( 'Transaction not in write mode' ) ; } const record = await this . first ( ) ; if ( ! record ) { return this . create ( data ) ; } return record ; } param id param data throws public async findOrCreate < T > ( id , data ) ; public async findOrCreate ( id , data ) { const tables = this . tableNames ( this . connector . migrationSchema . tables ) . concat ( this . table . name ) ; const transaction = this . createTransaction ( tables , TransactionModes . Write ) ; if ( transaction . mode !== TransactionModes . Write ) { throw new InvalidTransaction ( 'Transaction not in write mode' ) ; } this . setTransaction ( transaction ) ; const record = await this . find ( id ) ; if ( ! record ) { return this . create ( data ) ; } return record ; } param indexName param id param data throws public async findIndexOrCreate < T > ( indexName , id , data ) ; public async findIndexOrCreate ( indexName , id , data ) { const tables = this . tableNames ( this . connector . migrationSchema . tables ) . concat ( this . table . name ) ; const transaction = this . createTransaction ( tables , TransactionModes . Write ) ; if ( transaction . mode !== TransactionModes . Write ) { throw new InvalidTransaction ( 'Transaction not in write mode' ) ; } const record = await this . findIndex ( indexName , id ) ; if ( ! record ) { return this . create ( data ) ; } return record ; } }