Basic usage
To start working with Surrealize, you first need to make a Surrealize instance.
import { Surrealize } from "surrealize";
const surrealize = new Surrealize();
Configuration
You can configure the Surrealize instance by passing an options object.
import { Surrealize } from "surrealize";
type SurrealizeOptions = {
/**
* The underlying surrealdb connection.
*/
surreal?: Surreal;
/**
* The transformer to use for encoding and decoding values between surrealdb and surrealize.
*/
transformer?: Transformer;
/**
* Set the connection as the default connection.
*/
default?: boolean;
};
const surrealize = new Surrealize({
/* ... */
} as SurrealizeOptions);
Option: surreal
The surreal option allows you to pass a Surreal instance to the Surrealize instance.
import { Surreal } from "surrealdb";
import { Surrealize } from "surrealize";
const surreal = new Surreal();
const surrealize = new Surrealize({
surreal,
});
You can always access the underlying Surreal instance by calling the connection-property on the Surrealize instance.
import { Surrealize } from "surrealize";
const surrealize = new Surrealize();
// get the underlying surrealdb connection
const surreal = surrealize.connection;
Option: transformer
The transformer option allows you to pass a custom Transformer instance to the Surrealize instance to customize how values are encoded and decoded between surrealdb and surrealize.
import { Surrealize, Transformer } from "surrealize";
const surrealize = new Surrealize({
transformer: new Transformer([
/* list of custom types */
]),
});
Option: default
The default option allows you to set the Surrealize instance as the default instance for all queries.
This means that you don't need to pass the Surrealize instance to every query.
import { Surrealize } from "surrealize";
const surrealize = new Surrealize({
default: true,
});
// execute a query without passing the Surrealize instance
const result = await query.execute();
Usage
Once you have a Surrealize instance, you can start using it to query the database.
import { Surrealize } from "surrealize";
const surrealize = new Surrealize();
// query the database
const result = await surrealize.execute(query);
The execute method takes a Query object and returns a Promise that resolves to the result of the query.
A Query object is a representation of a query in the database.