Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to delete content in table? #1800

Open
Shunshine07 opened this issue May 27, 2024 · 1 comment
Open

How to delete content in table? #1800

Shunshine07 opened this issue May 27, 2024 · 1 comment

Comments

@Shunshine07
Copy link

I have multiple tables lets sat A, B and C.
There is no relation between them. I want to delete all the content of table A but table B and table C should be remain as it is.
Delete all content meaning the table should be empty with holding its schema.

I tried below code but it is not working,
Inside Modal of Table A
@writer async markThisDeleted() { await this.destroyPermanently() }

and that calling it explicitly in other file
const deleted = allContents.map(async (content: any) => await content.markThisDeleted()) database.batch(...deleted);

But this gives error that database,batch should be inside the writer.

If I write above code inside writer:
const deleted = allContents.map(async (content: any) => await content.markThisDeleted()) database.writer(writer => writer.batch(...deleted))

It gives waring that there are two writers in queue.

Moreover, there is not change in database. I can see all the content there.
Can anyone help me with this issue?

Versions:
Watermelon db : 0.24.0
React Native: 0.67.2

@Shunshine07 Shunshine07 changed the title How to delete content os table? How to delete content in table? May 27, 2024
@skam22
Copy link

skam22 commented May 28, 2024

batch only works for prepare statements.

database.writer(async writer => {
  const batchedTasks = allContents.map(content => content.prepareMarkAsDeleted());
  await writer.batch(…batchedTasks);
})

if you wanted to call multiple writers without batching:

database.writer(async writer => {
  for (const content of allContents) {
    await writer.callWriter(() => content.markThisDeleted());
  }
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants