surrealdb_sql/statements/remove/
scope.rs

1use crate::ctx::Context;
2use crate::dbs::{Options, Transaction};
3use crate::err::Error;
4use crate::iam::{Action, ResourceKind};
5use crate::{Base, Ident, Value};
6use derive::Store;
7use revision::revisioned;
8use serde::{Deserialize, Serialize};
9use std::fmt::{self, Display, Formatter};
10
11#[derive(Clone, Debug, Default, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Store, Hash)]
12#[revisioned(revision = 1)]
13pub struct RemoveScopeStatement {
14	pub name: Ident,
15}
16
17impl RemoveScopeStatement {
18	/// Process this type returning a computed simple Value
19	pub(crate) async fn compute(
20		&self,
21		_ctx: &Context<'_>,
22		opt: &Options,
23		txn: &Transaction,
24	) -> Result<Value, Error> {
25		// Allowed to run?
26		opt.is_allowed(Action::Edit, ResourceKind::Scope, &Base::Db)?;
27		// Claim transaction
28		let mut run = txn.lock().await;
29		// Clear the cache
30		run.clear_cache();
31		// Delete the definition
32		let key = crate::key::database::sc::new(opt.ns(), opt.db(), &self.name);
33		run.del(key).await?;
34		// Remove the resource data
35		let key = crate::key::scope::all::new(opt.ns(), opt.db(), &self.name);
36		run.delp(key, u32::MAX).await?;
37		// Ok all good
38		Ok(Value::None)
39	}
40}
41
42impl Display for RemoveScopeStatement {
43	fn fmt(&self, f: &mut Formatter) -> fmt::Result {
44		write!(f, "REMOVE SCOPE {}", self.name)
45	}
46}