---
title: "Functions | SurrealDB"
description: "Compute inside the database. Define SurrealQL functions, run JavaScript, and load sandboxed WebAssembly extensions, all in the same transaction as your data."
url: https://surrealdb.com/surrealdb/functions
---

FUNCTIONS

# Functions inside the database

Push logic to where the data lives. Define reusable SurrealQL functions, drop into JavaScript when you need it, and load sandboxed WebAssembly extensions in Rust - all executing in the same ACID transaction as your query, with no service to call and no data to ship over the wire.

[Read the docs](https://surrealdb.com/docs/reference/query-language/statements/define/function) [Start free with SurrealDB](https://studio.surrealdb.com/current/instances/deploy)

1. ![Babcock](https://surrealdb.com/assets/static/babcock.lo4rnVg1.svg)
2. ![ING](https://surrealdb.com/assets/static/ing.X3I6S3_V.svg)
3. ![British Airways](https://surrealdb.com/assets/static/british-airways.KEsZiwV-.svg)
4. ![Nvidia](https://surrealdb.com/assets/static/nvidia.DaIEuMil.svg)
5. ![Apple](https://surrealdb.com/assets/static/apple.D5pq4flY.svg)
6. ![SpaceX](https://surrealdb.com/assets/static/spacex.CQJEk-IL.svg)
7. ![Samsung](https://surrealdb.com/assets/static/samsung.CH-vQgnb.svg)
8. ![adidas](https://surrealdb.com/assets/static/adidas.DdTC5qhk.svg)
9. ![Tencent](https://surrealdb.com/assets/static/tencent.paQmLxyy.svg)
10. ![Alibaba](https://surrealdb.com/assets/static/alibaba.B16idgfM.svg)
11. ![PolyAI](https://surrealdb.com/assets/static/poly-ai.c3w_fAg6.svg)
12. ![Later](https://surrealdb.com/assets/static/later.Ds736jFO.svg)
13. ![Verizon](https://surrealdb.com/assets/static/verizon.BI7CajdX.svg)
14. ![Liberty Mutual](https://surrealdb.com/assets/static/liberty-mutual.B7qOU1pd.svg)
15. ![Walmart](https://surrealdb.com/assets/static/walmart.BjDg_Sr8.svg)
16. ![Carrier](https://surrealdb.com/assets/static/carrier.D21gC6NX.svg)
17. ![Saks Fifth Avenue](https://surrealdb.com/assets/static/saks-fifth-avenue.COIDpLSb.svg)
18. ![San Francisco Compute Company](https://surrealdb.com/assets/static/sfcc.B7jlImq4.svg)
19. ![Shield AI](https://surrealdb.com/assets/static/shield-ai.pINZ0KJr.svg)
20. ![Wix](https://surrealdb.com/assets/static/wix.DvHhmoBi.svg)

01 |IN-DATABASE LOGIC

## From one-liners to compiled extensions

Custom functions, embedded JavaScript, and WebAssembly modules give you a ladder from a quick helper to native-speed compute, all callable directly from SurrealQL.

CUSTOM FUNCTIONS

## Reusable logic, defined once

DEFINE FUNCTION stores named, typed procedures in the database. Call them from any query the way you call built-ins.

```
DEFINE FUNCTION fn::get::person(    $first: string,    $last: string,    $birthday: string) {    LET $person = SELECT * FROM person WHERE        first = $first AND        last = $last AND        birthday = $birthday;    RETURN IF $person[0].id {        $person[0]    } ELSE {        CREATE person SET            first = $first,            last = $last,            birthday = $birthday    };};LET $person = fn::get::person('Tobie', 'Morgan Hitchcock', '2007-06-22');
```

WEBASSEMBLY EXTENSIONS

## Native-speed logic, sandboxed

Write an extension in Rust, compile it to a WebAssembly module, and load it into a running database. Its functions become callable from SurrealQL, sandboxed and inside the transaction.

```
DEFINE MODULE mod::sentiment    FROM f"modules:/sentiment.surli" UNSIGNED;UPDATE article SET    sentiment = mod::sentiment::analyze(content),    keywords = mod::sentiment::extract(content)WHERE created_at > time::now() - 1h;
```

## A full compute surface, next to your data

Built-in function families, custom procedures, scripting, and compiled extensions - every layer runs in the engine, in the transaction.

### [Custom functions](https://surrealdb.com/docs/reference/query-language/statements/define/function)

Typed, reusable SurrealQL procedures stored in the database and callable from any query.

Learn more

### [Built-in functions](https://surrealdb.com/surrealdb/features#functions-and-extensibility)

Array, string, math, time, vector, geo, search, and more - hundreds of functions ready in the query language.

Learn more

### [JavaScript scripting](https://surrealdb.com/surrealdb/features#functions-and-extensibility)

Drop into embedded JavaScript with the current record as context when SurrealQL is not enough.

Learn more

### [WebAssembly extensions](https://surrealdb.com/surrealdb/extensions)

Write extensions in Rust, compile to WASM, and run them at near-native speed inside a sandbox.

Learn more

### [AI-ready by design](https://surrealdb.com/surrealdb/extensions)

Call models and inference next to the data and write results back in a single statement.

Learn more

### [Hot-load, no downtime](https://surrealdb.com/surrealdb/extensions)

Load and upgrade extension modules on a running instance without restarting the database.

Learn more

02 |ONE ENGINE

## Move the compute to the data

Pulling rows into an application server, transforming them, and writing them back costs a round trip and breaks atomicity. Running the logic in the database removes both problems.

### No round trips

Logic executes beside the data, so there is no fetch-transform-write hop across the network for every operation.

### One transaction

Functions and extensions participate in the same ACID transaction as the query that calls them - results commit together or not at all.

### One deployment

No function-as-a-service to provision, secure, and scale separately. The compute ships with the database.

## Keep exploring

### [Extensions](https://surrealdb.com/surrealdb/extensions)

The WebAssembly extension system in depth.

Explore

### [Functions documentation](https://surrealdb.com/docs/reference/query-language/functions/database-functions)

Every built-in family and how to define your own.

Explore

### [All features](https://surrealdb.com/surrealdb/features#functions-and-extensibility)

The complete functions and extensibility set.

Explore

GET STARTED

## Run your logic where your data lives

Define functions, script in JavaScript, and load WebAssembly extensions - all inside SurrealDB.

![Samsung](https://surrealdb.com/assets/static/4c58b81e7b3c9466.C_Hv0eml.svg)![NVIDIA](https://surrealdb.com/assets/static/nvidia.DaIEuMil.svg)![Apple](https://surrealdb.com/assets/static/f7dc2519e0d212bc.Cn8MYAK7.svg)![Verizon](https://surrealdb.com/assets/static/18b99996c689000f.B5PQ-nI9.svg)![Tencent](https://surrealdb.com/assets/static/401d8346058682c8.DqM87mst.svg)

SOC 2 Type 2

GDPR

Cyber Essentials Plus

ISO 27001

[Read the docs](https://surrealdb.com/docs/reference/query-language/statements/define/function) [Start free with SurrealDB](https://studio.surrealdb.com/current/instances/deploy)

```json
{"@context":"https://schema.org","@type":"Organization","name":"SurrealDB","url":"https://surrealdb.com","logo":"https://surrealdb.com/assets/static/logo.BG7_TG2b.svg","description":"SurrealDB is the unified data layer for AI. A multi-model database for documents, graphs, vectors, and time-series.","foundingDate":"2022","legalName":"SurrealDB Ltd","identifier":{"@type":"PropertyValue","propertyID":"GB-COH","value":"13615201"},"address":{"@type":"PostalAddress","streetAddress":"3rd Floor, 1 Ashley Road","addressLocality":"Altrincham","addressRegion":"Cheshire","postalCode":"WA14 2DT","addressCountry":"GB"},"contactPoint":[{"@type":"ContactPoint","contactType":"customer support","email":"support@surrealdb.com","url":"https://surrealdb.com/contact","availableLanguage":"English"},{"@type":"ContactPoint","contactType":"sales","email":"info@surrealdb.com","url":"https://surrealdb.com/contact","availableLanguage":"English"},{"@type":"ContactPoint","contactType":"security","email":"security@surrealdb.com","url":"https://surrealdb.com/.well-known/security.txt","availableLanguage":"English"},{"@type":"ContactPoint","contactType":"legal","email":"legal@surrealdb.com","url":"https://surrealdb.com/legal","availableLanguage":"English"}],"hasCertification":[{"@type":"Certification","name":"SOC 2 Type 2"},{"@type":"Certification","name":"GDPR"},{"@type":"Certification","name":"Cyber Essentials Plus"},{"@type":"Certification","name":"ISO 27001"}],"owns":[{"@type":"SoftwareApplication","name":"SurrealDB","url":"https://surrealdb.com/surrealdb"},{"@type":"SoftwareApplication","name":"Agent Memory","url":"https://surrealdb.com/agent-memory"}],"knowsAbout":["multi-model databases","document databases","graph databases","vector search","time-series databases","SurrealQL","Agent Memory","real-time databases","embedded databases","context layer","graph ontology","distributed database","knowledge graphs","distributed transaction protocols","highly-scalable databases"],"sameAs":["https://www.wikidata.org/wiki/Q124316308","https://github.com/surrealdb/surrealdb","https://twitter.com/surrealdb","https://www.youtube.com/@surrealdb","https://www.linkedin.com/company/surrealdb","https://discord.gg/surrealdb","https://www.reddit.com/r/surrealdb","https://www.instagram.com/surrealdb","https://medium.com/surrealdb","https://dev.to/surrealdb"]}
```

```json
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://surrealdb.com"},{"@type":"ListItem","position":2,"name":"SurrealDB","item":"https://surrealdb.com/surrealdb"},{"@type":"ListItem","position":3,"name":"Functions","item":"https://surrealdb.com/surrealdb/functions"}]}
```
