Skip to content

Commit

Permalink
now save
Browse files Browse the repository at this point in the history
  • Loading branch information
kaplanelad committed Oct 14, 2024
1 parent ae5231b commit 427b168
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
2 changes: 0 additions & 2 deletions examples/demo/src/fixtures/notes.yaml
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
---
- id: 1
title: Loco note 1
content: content 1
created_at: "2023-11-12T12:34:56.789"
updated_at: "2023-11-12T12:34:56.789"
- id: 2
title: Loco note 2
content: content 2
created_at: "2023-11-12T12:34:56.789"
updated_at: "2023-11-12T12:34:56.789"
- id: 3
Expand Down
10 changes: 6 additions & 4 deletions src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,7 @@ pub async fn reset<M: MigratorTrait>(db: &DatabaseConnection) -> Result<(), sea_
/// Returns a [`AppResult`] if could not render the path content into
/// [`Vec<serde_json::Value>`] or could not inset the vector to DB.
#[allow(clippy::type_repetition_in_bounds)]
#[allow(clippy::unused_async)]
pub async fn seed<A>(_db: &DatabaseConnection, path: &str) -> AppResult<()>
pub async fn seed<A>(db: &DatabaseConnection, path: &str) -> AppResult<()>
where
<<A as ActiveModelTrait>::Entity as EntityTrait>::Model: IntoActiveModel<A>,
for<'de> <<A as ActiveModelTrait>::Entity as EntityTrait>::Model: serde::de::Deserialize<'de>,
Expand All @@ -235,8 +234,11 @@ where
let seed_data: Vec<serde_json::Value> = serde_yaml::from_reader(File::open(path)?)?;

for row in seed_data {
let mut entity = <A as ActiveModelTrait>::default();
entity.set_from_json(row)?;
let mut model = <A as ActiveModelTrait>::default();
model.set_from_json(row)?;
<A as ActiveModelTrait>::Entity::insert(model)
.exec(db)
.await?;
}

Ok(())
Expand Down

0 comments on commit 427b168

Please sign in to comment.