surrealdb-core-nightly 2.1.20250115

A nightly release of the surrealdb-core crate
Documentation
use crate::ctx::Context;
use crate::dbs::Options;
use crate::dbs::Statement;
use crate::dbs::Workable;
use crate::doc::Document;
use crate::err::Error;
use crate::sql::paths::EDGE;
use crate::sql::paths::IN;
use crate::sql::paths::OUT;
use crate::sql::value::Value;
use crate::sql::Dir;
use crate::sql::Relation;
use crate::sql::TableType;

impl Document {
	pub(super) async fn store_edges_data(
		&mut self,
		ctx: &Context,
		opt: &Options,
		_stm: &Statement<'_>,
	) -> Result<(), Error> {
		// Get the table
		let tb = self.tb(ctx, opt).await?;
		// Check if the table is a view
		if tb.drop {
			return Ok(());
		}
		// Store the record edges
		if let Workable::Relate(l, r, _) = &self.extras {
			// Get the namespace
			let ns = opt.ns()?;
			// Get the database
			let db = opt.db()?;
			// Get the record id
			let rid = self.id()?;
			// Get the transaction
			let txn = ctx.tx();
			// Lock the transaction
			let mut txn = txn.lock().await;
			// For enforced relations, ensure that the edges exist
			if matches!(
				tb.kind,
				TableType::Relation(Relation {
					enforced: true,
					..
				})
			) {
				// Check that the `in` record exists
				let key = crate::key::thing::new(ns, db, &l.tb, &l.id);
				if !txn.exists(key, None).await? {
					return Err(Error::IdNotFound {
						value: l.to_string(),
					});
				}
				// Check that the `out` record exists
				let key = crate::key::thing::new(ns, db, &r.tb, &r.id);
				if !txn.exists(key, None).await? {
					return Err(Error::IdNotFound {
						value: r.to_string(),
					});
				}
			}
			// Get temporary edge references
			let (ref o, ref i) = (Dir::Out, Dir::In);
			// Store the left pointer edge
			let key = crate::key::graph::new(ns, db, &l.tb, &l.id, o, &rid);
			txn.set(key, vec![], opt.version).await?;
			// Store the left inner edge
			let key = crate::key::graph::new(ns, db, &rid.tb, &rid.id, i, l);
			txn.set(key, vec![], opt.version).await?;
			// Store the right inner edge
			let key = crate::key::graph::new(ns, db, &rid.tb, &rid.id, o, r);
			txn.set(key, vec![], opt.version).await?;
			// Store the right pointer edge
			let key = crate::key::graph::new(ns, db, &r.tb, &r.id, i, &rid);
			txn.set(key, vec![], opt.version).await?;
			// Store the edges on the record
			self.current.doc.to_mut().put(&*EDGE, Value::Bool(true));
			self.current.doc.to_mut().put(&*IN, l.clone().into());
			self.current.doc.to_mut().put(&*OUT, r.clone().into());
		}
		// Carry on
		Ok(())
	}
}