insert method
Inserts a single model into the database and returns the inserted record.
The returned model contains the actual values from the database, including any auto-generated fields (like primary keys).
Example with tracked model:
final user = $User(name: 'John Doe', email: 'john@example.com');
final insertedUser = await repository.insert(user);
print(insertedUser.id); // The auto-generated primary key.
Example with insert DTO:
final dto = $UserInsertDto(name: 'John Doe', email: 'john@example.com');
final insertedUser = await repository.insert(dto);
Example with raw map:
final data = {'name': 'John Doe', 'email': 'john@example.com'};
final insertedUser = await repository.insert(data);
Implementation
Future<T> insert(Object model) async {
final query = _queryOrNull;
if (query != null) {
final inserted = await query.insertManyInputs([model]);
return inserted.first;
}
final inserted = await insertMany([model]);
return inserted.first;
}