diff --git a/src/content/doc-sdk-javascript/core/create-a-new-connection.mdx b/src/content/doc-sdk-javascript/core/create-a-new-connection.mdx index d326981cc..a74933acb 100644 --- a/src/content/doc-sdk-javascript/core/create-a-new-connection.mdx +++ b/src/content/doc-sdk-javascript/core/create-a-new-connection.mdx @@ -44,14 +44,6 @@ While the `.connect()` method is the primary method to connect to a SurrealDB in db.use(namespace,database) Switch to a specific namespace and database - - db.let(key,val) - Assigns a value as a parameter for this connection - - - db.unset(key) - Removes a parameter for this connection - @@ -63,7 +55,7 @@ This means that the `.use()` method is not required if you specify the [`namespa ### Connection options -You can specify your connection protocol either as `http`, `https`, `ws`, or `wss`. If you do not specify a protocol, the default protocol is `http`. Since SurrealDB also supports RPC over WebSocket, you can specify with a `/rpc` suffix. +You can specify your connection protocol either as `http`, `https`, `ws`, or `wss`. Since SurrealDB also supports RPC over WebSocket, by default, it is specified with a `/rpc` suffix. diff --git a/src/content/doc-sdk-javascript/core/data-maniplulation.mdx b/src/content/doc-sdk-javascript/core/data-maniplulation.mdx new file mode 100644 index 000000000..c0786564c --- /dev/null +++ b/src/content/doc-sdk-javascript/core/data-maniplulation.mdx @@ -0,0 +1,595 @@ +--- +sidebar_position: 4 +sidebar_label: Data manipulation +title: JavaScript | SDK | Data manipulation +description: SurrealDB supports a number of methods for interacting with the database and performing CRUD operations. +--- + +import Label from "@components/shared/Label.astro"; + +# Data manipulation + +SurrealDB supports a number of methods for interacting with the database and performing CRUD operations. + +## `.select()` {#select} + +Selects all records in a table, or a specific record, from the database. + +```ts title="Method Syntax" +async db.select(thing) +``` + +### Arguments + + + + + + + + + + + + + +
ArgumentsDescription
+ thing + + The table name or a [`RecordId`](/docs/sdk/javascript/data-types#recordid) to select. +
+ +### Example usage +```ts +type Person = { + id: string; + name: string; +}; + +// Select all records from a table +const people = await db.select('person'); + +// Select a specific record from a table +const person = await db.select(new RecordId('person', 'h5wxrf2ewk8xjxosxtyc')); +const person = await db.select(new StringRecordId('person:h5wxrf2ewk8xjxosxtyc')); +``` + +### Translated query +This function will run the following query in the database. + +```surql +SELECT * FROM $thing; +``` + +
+ +## `.create()` {#create} + +Creates a record in the database. + +```ts title="Method Syntax" +async db.create(thing, data) +``` + +### Arguments + + + + + + + + + + + + + + + + + +
ArgumentsDescription
+ thing + + The table name or a [`RecordId`](/docs/sdk/javascript/data-types#recordid) to create. +
+ data + + The document / record data to create. +
+ +### Example usage +```ts +type Person = { + id: string; + name: string; + settings: { + active: boolean; + marketing: boolean; + }; +}; + +// Create a record with a random ID +const [person] = await db.create('person'); + +// Create a record with a specific ID +const person = await db.create(new RecordId('person', 'tobie'), { + name: 'Tobie', + settings: { + active: true, + marketing: true, + }, +}); + +// The content you are creating the record with might differ from the return type +const [record] = await db.create< + Person, + Pick +>( + new RecordId('person', 'tobie'), + { + name: 'Tobie', + } +); +``` + +### Translated query +This function will run the following query in the database. + +```surql +CREATE $thing CONTENT $data; +``` + +
+ +## `.insert()` {#insert} + +Inserts one or multiple records in the database. + +```ts title="Method Syntax" +async db.insert(table, data) +``` + +### Arguments + + + + + + + + + + + + + + + + + +
ArgumentsDescription
+ table + + Optionally pass along a table to insert into. +
+ data + + Either a single document/record or an array of documents/records to insert +
+ +### Example usage +```ts +type Person = { + id: string; + name: string; + settings: { + active: boolean; + marketing: boolean; + }; +}; + +// Insert a single record +const [person] = await db.insert('person', { + name: 'Tobie', + settings: { + active: true, + marketing: true, + }, +}); + +// Insert multiple records +const people = await db.insert('person', [ + { + name: 'Tobie', + settings: { + active: true, + marketing: true, + }, + }, + { + name: 'Jaime', + settings: { + active: true, + marketing: true, + }, + }, +]); + +// The content you are creating the record with might differ from the return type +const people = await db.insert< + Person, + Pick +>('person', [ + { name: 'Tobie' }, + { name: 'Jaime' }, +]); +``` + +### Translated query +This function will run the following query in the database. + +```surql +INSERT INTO $table $data; +``` + +
+ +## `.insert_relation()` {#insert_relation} + +Inserts one or multiple relations in the database. + +```ts title="Method Syntax" +async db.insert(table, data) +``` + +### Arguments + + + + + + + + + + + + + + + + + +
ArgumentsDescription
+ table + + Optionally pass along a table to insert into. +
+ data + + Either a single document/record or an array of documents/records to insert +
+ +### Example usage +```ts +type Likes = { + id: RecordId<"likes">; + in: RecordId<"person">; + out: RecordId<"post">; +}; + +// Insert a single record +const [person] = await db.insert_relation('likes', { + in: new RecordId('person', 'tobie'), + out: new RecordId('post', 123), +}); + +// Insert multiple records across tables +const people = await db.insert('likes', [ + { + in: new RecordId('person', 'tobie'), + out: new RecordId('post', 123), + }, + { + in: new RecordId('person', 'jaime'), + out: new RecordId('post', 456), + }, +]); +``` + +### Translated query +This function will run the following query in the database. + +```surql +INSERT RELATION INTO $table $data; +``` + +
+ +## `.update()` {#update} + +Updates all records in a table, or a specific record, in the database. + +```ts title="Method Syntax" +async db.update(thing, data) +``` + + +> [!NOTE] +> This function replaces the current document / record data with the specified data. + +### Arguments + + + + + + + + + + + + + + + + + +
ArgumentsDescription
+ thing + + The table name or the specific [`RecordId`](/docs/sdk/javascript/data-types#recordid) to update. +
+ data + + The document / record data to update. +
+ +### Example usage +```ts +type Person = { + id: string; + name: string; + settings: { + active: boolean; + marketing: boolean; + }; +}; + +// Update all records in a table +const people = await db.update('person'); + +// Update a record with a specific ID +const person = await db.update(new RecordId('person', 'tobie'), { + name: 'Tobie', + settings: { + active: true, + marketing: true, + }, +}); + +// The content you are updating the record with might differ from the return type +const record = await db.update< + Person, + Pick +>(new RecordId('person', 'tobie'), { + name: 'Tobie', +}); +``` + +### Translated query +This function will run the following query in the database. + +```surql +UPDATE $thing CONTENT $data; +``` + +
+ +## `.merge()` {#merge} + +Modifies all records in a table, or a specific record, in the database. + +```ts title="Method Syntax" +async db.merge(thing, data) +``` + + +> [!NOTE] +> This function merges the current document / record data with the specified data. + +### Arguments + + + + + + + + + + + + + + + + + +
ArgumentsDescription
+ thing + + The table name or the specific [`RecordId`](/docs/sdk/javascript/data-types#recordid) to merge. +
+ data + + The document / record data to merge. +
+ +### Example usage +```ts +type Person = { + id: string; + name: string; + updated_at: Date; + settings: { + active: boolean; + marketing: boolean; + }; +}; + +// Update all records in a table +const people = await db.merge('person', { + updated_at: new Date(), +}); + +// Update a record with a specific ID +const person = await db.merge(new RecordId('person', 'tobie'), { + updated_at: new Date(), + settings: { + active: true, + }, +}); + +// The content you are merging the record with might differ from the return type +const record = await db.merge< + Person, + Pick +>(new RecordId('person', 'tobie'), { + name: 'Tobie', +}); +``` + +### Translated query +This function will run the following query in the database. + +```surql +UPDATE $thing MERGE $data; +``` + +
+ +## `.patch()` {#patch} + +Applies JSON Patch changes to all records, or a specific record, in the database. + +```ts title="Method Syntax" +async db.patch(thing, data) +``` + +> [!NOTE] +> This function patches the current document / record data with the specified JSON Patch data. +::: + +### Arguments + + + + + + + + + + + + + + + + + +
ArgumentsDescription
+ thing + + The table name or the specific [`RecordId`](/docs/sdk/javascript/data-types#recordid) to patch. +
+ data + + The JSON Patch data with which to patch the records. +
+ +### Example usage +```ts +// Update all records in a table +const people = await db.patch('person', [ + { op: 'replace', path: '/created_at', value: new Date() }, +]); + +// Update a record with a specific ID +const person = await db.patch(new RecordId('person', 'tobie'), [ + { op: 'replace', path: '/settings/active', value: false }, + { op: 'add', path: '/tags', value: ['developer', 'engineer'] }, + { op: 'remove', path: '/temp' }, +]); +``` + +### Translated query +This function will run the following query in the database. + +```surql +UPDATE $thing PATCH $data; +``` + +
+ +## `.delete()` {#delete} + +Deletes all records in a table, or a specific record, from the database. + +```ts title="Method Syntax" +async db.delete(thing) +``` + +### Arguments + + + + + + + + + + + + + +
ArgumentsDescription
+ thing + + The table name or a [`RecordId`](/docs/sdk/javascript/data-types#recordid) to delete. +
+ +### Example usage +```ts +// Delete all records from a table +await db.delete('person'); + +// Delete a specific record from a table +await db.delete(new RecordId('person', 'h5wxrf2ewk8xjxosxtyc')); +``` + +### Translated query +This function will run the following query in the database. + +```surql +DELETE $thing; +``` diff --git a/src/content/doc-sdk-javascript/core/authenticating-users.mdx b/src/content/doc-sdk-javascript/core/handling-authentication.mdx similarity index 56% rename from src/content/doc-sdk-javascript/core/authenticating-users.mdx rename to src/content/doc-sdk-javascript/core/handling-authentication.mdx index 11669f3f0..7d048855c 100644 --- a/src/content/doc-sdk-javascript/core/authenticating-users.mdx +++ b/src/content/doc-sdk-javascript/core/handling-authentication.mdx @@ -1,31 +1,80 @@ --- sidebar_position: 2 -sidebar_label: Authenticating users -title: JavaScript | SDK | Authenticating users +sidebar_label: Handle authentication +title: JavaScript | SDK | Handle authentication description: SurrealDB supports a number of methods for authenticating users and securing the database. --- import Label from "@components/shared/Label.astro"; import Since from "@components/shared/Since.astro"; +import Version from '@components/Version.astro'; +import Tabs from "@components/Tabs/Tabs.astro"; +import TabItem from "@components/Tabs/TabItem.astro"; -# Authenticating users +# Handle authentication Since SurrealDB is a database that is designed to be used in a distributed environment, it is important to secure the database and the data that is stored in it. SurrealDB provides a number of methods for authenticating users and securing the database. +In your SurrealDB database, you can create authentication login using the [`DEFINE ACCESS`](/docs/surrealql/statements/define/access) statement which supports [JWT](/docs/surrealql/statements/define/access/jwt) and [Record](/docs/surrealql/statements/define/access/record) Access methods. + +The access method used will inform the input for `access` in the `.signup()` and `.signin()` methods. + +> [!IMPORTANT] +> If you are not on Version of SurrealDB, you will use the `scope` property instead of `access`. + + +## Defining access in your application + +The JavaScript SDK has a [`.query()` method](/docs/sdk/javascript/core/writing-surrealql) which allows you to write secure SurrealQL statements from within your application. Using this method, you can define access for your users and securely manage authentication. See the code example below: + + + +```ts +... +// Assign the variable on the connection +const authentication = await db.query( +" DEFINE ACCESS account ON DATABASE TYPE RECORD + SIGNUP ( CREATE user SET email = $email, pass = crypto::argon2::generate($pass) ) + SIGNIN ( SELECT * FROM user WHERE email = $email AND crypto::argon2::compare(pass, $pass) ) + DURATION FOR TOKEN 15m, FOR SESSION 12h " +; +); +... +``` + + + +```ts +... +// Assign the variable on the connection +const authentication = await db.query( +" DEFINE SCOPE user SESSION 24h + SIGNUP ( CREATE user SET email = $email, pass = crypto::argon2::generate($pass) ) + SIGNIN ( SELECT * FROM user WHERE email = $email AND crypto::argon2::compare(pass, $pass) ) +; " +); +... +``` + + + ## User authentication -SurrealDB supports user authentication using a number of methods, including: +After you have defined your authentication login, you can use the following methods to authenticate users: ## `.signup()` {#signup} -Signs up to a specific authentication scope. +Signs up to a specific authentication scope / access method. ```ts title="Method Syntax" async db.signup({`{ namespace, database, [ scope | access ], [...] }`}) ``` ### Arguments + + + @@ -54,41 +103,83 @@ async db.signup({`{ namespace, database, [ scope | access ], [...] }`}) + +
- scope + access - The scope to sign up to. Also pass any variables used in the scope. Only supported in SurrealDB 1.x + The access to sign up to. Also pass any variables used in the access under the `variables` key. Only supported from SurrealDB 2.x onwards
+ +
+ + + + + + + + + + + + + + + + + +
ArgumentsDescription
- access + namespace - The access to sign up to. Also pass any variables used in the access under the `variables` key. Only supported from SurrealDB 2.x onwards + The namespace to sign up to +
+ database + + The database to sign up to +
+ scope + + The scope to sign up to. Also pass any variables used in the scope. Only supported in SurrealDB 1.x
+
+
### Example usage + + + + + ```ts // With Record Access const token = await db.signup({ namespace: 'surrealdb', database: 'docs', - scope: 'user', + access: 'account', - // Also pass any properties required by the scope definition + // Also pass any properties required by the access definition variables: { email: 'info@surrealdb.com', pass: '123456', }, }); +``` + + + +```ts // With Scopes const token = await db.signup({ @@ -101,6 +192,8 @@ const token = await db.signup({ pass: '123456', }); ``` + +
@@ -113,6 +206,10 @@ async db.signin({`{ ... }`}) ``` ### Arguments + + + + @@ -142,7 +239,7 @@ async db.signin({`{ ... }`}) + +
namespace - The namespace to sign in to @@ -151,7 +248,7 @@ async db.signin({`{ ... }`})
database - The database to sign in to @@ -159,40 +256,102 @@ async db.signin({`{ ... }`})
- scope - + access - The scope to sign in to. Also pass any variables used in the scope. Only supported in SurrealDB 1.x + The access to sign in to. Also pass any variables used in the access under the `variables` key. Only supported from SurrealDB 2.x onwards
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + +
PropertiesDescription
- access - + username + - The access to sign in to. Also pass any variables used in the access under the `variables` key. Only supported from SurrealDB 2.x onwards + The username of the database user +
+ password + + The password of the database user +
+ namespace + + The namespace to sign in to +
+ database + + The database to sign in to +
+ scope + + The scope to sign in to. Also pass any variables used in the scope. Only supported in SurrealDB 1.x
+
+
### Example usage + + + + ```ts // Authenticate with a root user const token = await db.signin({ username: 'root', password: 'surrealdb', }); +``` + + +```ts // Authenticate with a Namespace user const token = await db.signin({ namespace: 'surrealdb', username: 'tobie', password: 'surrealdb', }); +``` + + +```ts // Authenticate with a Database user const token = await db.signin({ namespace: 'surrealdb', @@ -200,20 +359,26 @@ const token = await db.signin({ username: 'tobie', password: 'surrealdb', }); - +``` + + +```ts // Authenticate with Record Access const token = await db.signin({ namespace: 'surrealdb', database: 'docs', - scope: 'user', + access: 'account', - // Also pass any properties required by the scope definition + // Also pass any properties required by the access definition variables: { email: 'info@surrealdb.com', pass: '123456', }, }); - +``` + + +```ts // Authenticate with Scopes const token = await db.signin({ namespace: 'surrealdb', @@ -225,6 +390,9 @@ const token = await db.signin({ pass: '123456', }); ``` + + +
diff --git a/src/content/doc-sdk-javascript/core/index.mdx b/src/content/doc-sdk-javascript/core/index.mdx index 4560d8c18..dfc89c195 100644 --- a/src/content/doc-sdk-javascript/core/index.mdx +++ b/src/content/doc-sdk-javascript/core/index.mdx @@ -10,9 +10,10 @@ description: The SurrealDB SDK for JavaScript enables simple and advanced queryi In this section, we will go over the core concepts of the SurrealDB SDK for JavaScript. You will learn how to connect to a SurrealDB instance, manage authentication, and interact with the database. - [Create a new Connection](/docs/sdk/javascript/core/create-a-new-connection) -- [Handle authentication](/docs/sdk/javascript/core/handle-user-authentication) -- [Querying](/docs/sdk/javascript/core/querying) -- [Transactions](/docs/sdk/javascript/core/transactions) -- [Error Handling](/docs/sdk/javascript/core/error-handling) -- [Best Practices](/docs/sdk/javascript/core/best-practices) +- [Handle authentication](/docs/sdk/javascript/core/handling-authentication) +- [Set parameters](/docs/sdk/javascript/core/parameters) +- [Data manipulation](/docs/sdk/javascript/core/data-maniplulation) +- [Real-Time Streaming](/docs/sdk/javascript/core/streaming) +- [Run SurrealQL queries](/docs/sdk/javascript/core/writing-surrealql) +- [Utilities methods](/docs/sdk/javascript/core/utilities) diff --git a/src/content/doc-sdk-javascript/core/parameters.mdx b/src/content/doc-sdk-javascript/core/parameters.mdx index 463e5611e..fe994aff7 100644 --- a/src/content/doc-sdk-javascript/core/parameters.mdx +++ b/src/content/doc-sdk-javascript/core/parameters.mdx @@ -1,16 +1,19 @@ --- sidebar_position: 3 -sidebar_label: Parameters -title: JavaScript | SDK | Parameters -description: The SurrealDB SDK for JavaScript allows you to define parameters that can be used to store and retrieve data from SurrealDB. +sidebar_label: Set parameters +title: JavaScript | SDK | Set parameters +description: In this section, you will learn how to set parameters in the SurrealDB JavaScript SDK. --- import Label from "@components/shared/Label.astro"; -# Parameters +# Set parameters Within your application, you can define parameters that can be used to store and retrieve data from SurrealDB. Parameters are used to store data in a structured format, and can be used to store data in a key-value pair format. +>[!IMPORTANT] +> Parameters allow you to define global (database-wide) parameters that are available to every client. + ## `.let()` {#let} Assigns a value as a parameter for this connection. diff --git a/src/content/doc-sdk-javascript/core/streaming.mdx b/src/content/doc-sdk-javascript/core/streaming.mdx index ee8f440de..1e841771c 100644 --- a/src/content/doc-sdk-javascript/core/streaming.mdx +++ b/src/content/doc-sdk-javascript/core/streaming.mdx @@ -1,13 +1,13 @@ --- sidebar_position: 4 -sidebar_label: Live Queries -title: JavaScript | SDK | Live Queries +sidebar_label: Real-Time data streaming +title: JavaScript | SDK | Real-Time data streaming description: The SurrealDB SDK for JavaScript allows you to create live queries that listen for changes in the database and automatically update your application when changes occur. --- import Label from "@components/shared/Label.astro"; -# Live Queries +# Real-Time data streaming You can use the SurrealDB JavaScript SDK to create live queries that listen for changes in the database and automatically update your application when changes occur. This feature is useful for building real-time applications that need to respond to changes in the database. diff --git a/src/content/doc-sdk-javascript/core/utilities.mdx b/src/content/doc-sdk-javascript/core/utilities.mdx index 8e36e8eb5..89d1f4ad8 100644 --- a/src/content/doc-sdk-javascript/core/utilities.mdx +++ b/src/content/doc-sdk-javascript/core/utilities.mdx @@ -1,11 +1,11 @@ --- sidebar_position: 7 -sidebar_label: Utilities -title: JavaScript | SDK | Utilities -description: The SurrealDB SDK for JavaScript enables simple and advanced querying of a remote or embedded database. +sidebar_label: Utilities methods +title: JavaScript | SDK | Utilities methods +description: The SurrealDB SDK for JavaScript provides some built-in utilities to work with responses. --- -# Utilities +# Utilities methods The JavaScript SDK provides some built-in utilities to work with responses. @@ -115,6 +115,10 @@ jsonify({ A class containing a query and it's bindings, which can be passed to the [`.query()`](/docs/sdk/javascript/core/data-querying#query) method. +> [!NOTE] +>The additional benefit of using this method over the `.query()` method is that it encodes the query and its variables into CBOR upfront, which can be helpful when writing large queries often. + + ```ts title="Signature" new PreparedQuery(query: string, bindings: Record) ``` diff --git a/src/content/doc-sdk-javascript/core/writing-surrealql.mdx b/src/content/doc-sdk-javascript/core/writing-surrealql.mdx index ac78a03fb..45640323f 100644 --- a/src/content/doc-sdk-javascript/core/writing-surrealql.mdx +++ b/src/content/doc-sdk-javascript/core/writing-surrealql.mdx @@ -73,8 +73,6 @@ const people = result[1].result; With `.query_raw()`, you will get back the raw RPC response. This contrast to the `.query()` method, this will not throw for errors that occur in individual queries, but will rather give those back as a string, and this will include the time it took to execute the individual queries. -
-

# Built-in methods @@ -88,586 +86,3 @@ Passing a [`Table`](/docs/sdk/javascript/data-types#table) instance, or a `strin If you instead pass a [`RecordId`](/docs/sdk/javascript/data-types#recordid) or [`StringRecordId`](/docs/sdk/javascript/data-types#recordid) instance, the method will return a single object of generic type `T` back.
- -## `.select()` {#select} - -Selects all records in a table, or a specific record, from the database. - -```ts title="Method Syntax" -async db.select(thing) -``` - -### Arguments - - - - - - - - - - - - - -
ArgumentsDescription
- thing - - The table name or a [`RecordId`](/docs/sdk/javascript/data-types#recordid) to select. -
- -### Example usage -```ts -type Person = { - id: string; - name: string; -}; - -// Select all records from a table -const people = await db.select('person'); - -// Select a specific record from a table -const person = await db.select(new RecordId('person', 'h5wxrf2ewk8xjxosxtyc')); -const person = await db.select(new StringRecordId('person:h5wxrf2ewk8xjxosxtyc')); -``` - -### Translated query -This function will run the following query in the database. - -```surql -SELECT * FROM $thing; -``` - -
- -## `.create()` {#create} - -Creates a record in the database. - -```ts title="Method Syntax" -async db.create(thing, data) -``` - -### Arguments - - - - - - - - - - - - - - - - - -
ArgumentsDescription
- thing - - The table name or a [`RecordId`](/docs/sdk/javascript/data-types#recordid) to create. -
- data - - The document / record data to create. -
- -### Example usage -```ts -type Person = { - id: string; - name: string; - settings: { - active: boolean; - marketing: boolean; - }; -}; - -// Create a record with a random ID -const [person] = await db.create('person'); - -// Create a record with a specific ID -const person = await db.create(new RecordId('person', 'tobie'), { - name: 'Tobie', - settings: { - active: true, - marketing: true, - }, -}); - -// The content you are creating the record with might differ from the return type -const [record] = await db.create< - Person, - Pick ->( - new RecordId('person', 'tobie'), - { - name: 'Tobie', - } -); -``` - -### Translated query -This function will run the following query in the database. - -```surql -CREATE $thing CONTENT $data; -``` - -
- -## `.insert()` {#insert} - -Inserts one or multiple records in the database. - -```ts title="Method Syntax" -async db.insert(table, data) -``` - -### Arguments - - - - - - - - - - - - - - - - - -
ArgumentsDescription
- table - - Optionally pass along a table to insert into. -
- data - - Either a single document/record or an array of documents/records to insert -
- -### Example usage -```ts -type Person = { - id: string; - name: string; - settings: { - active: boolean; - marketing: boolean; - }; -}; - -// Insert a single record -const [person] = await db.insert('person', { - name: 'Tobie', - settings: { - active: true, - marketing: true, - }, -}); - -// Insert multiple records -const people = await db.insert('person', [ - { - name: 'Tobie', - settings: { - active: true, - marketing: true, - }, - }, - { - name: 'Jaime', - settings: { - active: true, - marketing: true, - }, - }, -]); - -// The content you are creating the record with might differ from the return type -const people = await db.insert< - Person, - Pick ->('person', [ - { name: 'Tobie' }, - { name: 'Jaime' }, -]); -``` - -### Translated query -This function will run the following query in the database. - -```surql -INSERT INTO $table $data; -``` - -
- -## `.insert_relation()` {#insert_relation} - -Inserts one or multiple relations in the database. - -```ts title="Method Syntax" -async db.insert(table, data) -``` - -### Arguments - - - - - - - - - - - - - - - - - -
ArgumentsDescription
- table - - Optionally pass along a table to insert into. -
- data - - Either a single document/record or an array of documents/records to insert -
- -### Example usage -```ts -type Likes = { - id: RecordId<"likes">; - in: RecordId<"person">; - out: RecordId<"post">; -}; - -// Insert a single record -const [person] = await db.insert_relation('likes', { - in: new RecordId('person', 'tobie'), - out: new RecordId('post', 123), -}); - -// Insert multiple records across tables -const people = await db.insert('likes', [ - { - in: new RecordId('person', 'tobie'), - out: new RecordId('post', 123), - }, - { - in: new RecordId('person', 'jaime'), - out: new RecordId('post', 456), - }, -]); -``` - -### Translated query -This function will run the following query in the database. - -```surql -INSERT RELATION INTO $table $data; -``` - -
- -## `.update()` {#update} - -Updates all records in a table, or a specific record, in the database. - -```ts title="Method Syntax" -async db.update(thing, data) -``` - - -> [!NOTE] -> This function replaces the current document / record data with the specified data. - -### Arguments - - - - - - - - - - - - - - - - - -
ArgumentsDescription
- thing - - The table name or the specific [`RecordId`](/docs/sdk/javascript/data-types#recordid) to update. -
- data - - The document / record data to update. -
- -### Example usage -```ts -type Person = { - id: string; - name: string; - settings: { - active: boolean; - marketing: boolean; - }; -}; - -// Update all records in a table -const people = await db.update('person'); - -// Update a record with a specific ID -const person = await db.update(new RecordId('person', 'tobie'), { - name: 'Tobie', - settings: { - active: true, - marketing: true, - }, -}); - -// The content you are updating the record with might differ from the return type -const record = await db.update< - Person, - Pick ->(new RecordId('person', 'tobie'), { - name: 'Tobie', -}); -``` - -### Translated query -This function will run the following query in the database. - -```surql -UPDATE $thing CONTENT $data; -``` - -
- -## `.merge()` {#merge} - -Modifies all records in a table, or a specific record, in the database. - -```ts title="Method Syntax" -async db.merge(thing, data) -``` - - -> [!NOTE] -> This function merges the current document / record data with the specified data. - -### Arguments - - - - - - - - - - - - - - - - - -
ArgumentsDescription
- thing - - The table name or the specific [`RecordId`](/docs/sdk/javascript/data-types#recordid) to merge. -
- data - - The document / record data to merge. -
- -### Example usage -```ts -type Person = { - id: string; - name: string; - updated_at: Date; - settings: { - active: boolean; - marketing: boolean; - }; -}; - -// Update all records in a table -const people = await db.merge('person', { - updated_at: new Date(), -}); - -// Update a record with a specific ID -const person = await db.merge(new RecordId('person', 'tobie'), { - updated_at: new Date(), - settings: { - active: true, - }, -}); - -// The content you are merging the record with might differ from the return type -const record = await db.merge< - Person, - Pick ->(new RecordId('person', 'tobie'), { - name: 'Tobie', -}); -``` - -### Translated query -This function will run the following query in the database. - -```surql -UPDATE $thing MERGE $data; -``` - -
- -## `.patch()` {#patch} - -Applies JSON Patch changes to all records, or a specific record, in the database. - -```ts title="Method Syntax" -async db.patch(thing, data) -``` - -> [!NOTE] -> This function patches the current document / record data with the specified JSON Patch data. -::: - -### Arguments - - - - - - - - - - - - - - - - - -
ArgumentsDescription
- thing - - The table name or the specific [`RecordId`](/docs/sdk/javascript/data-types#recordid) to patch. -
- data - - The JSON Patch data with which to patch the records. -
- -### Example usage -```ts -// Update all records in a table -const people = await db.patch('person', [ - { op: 'replace', path: '/created_at', value: new Date() }, -]); - -// Update a record with a specific ID -const person = await db.patch(new RecordId('person', 'tobie'), [ - { op: 'replace', path: '/settings/active', value: false }, - { op: 'add', path: '/tags', value: ['developer', 'engineer'] }, - { op: 'remove', path: '/temp' }, -]); -``` - -### Translated query -This function will run the following query in the database. - -```surql -UPDATE $thing PATCH $data; -``` - -
- -## `.delete()` {#delete} - -Deletes all records in a table, or a specific record, from the database. - -```ts title="Method Syntax" -async db.delete(thing) -``` - -### Arguments - - - - - - - - - - - - - -
ArgumentsDescription
- thing - - The table name or a [`RecordId`](/docs/sdk/javascript/data-types#recordid) to delete. -
- -### Example usage -```ts -// Delete all records from a table -await db.delete('person'); - -// Delete a specific record from a table -await db.delete(new RecordId('person', 'h5wxrf2ewk8xjxosxtyc')); -``` - -### Translated query -This function will run the following query in the database. - -```surql -DELETE $thing; -``` diff --git a/src/content/doc-sdk-javascript/index.mdx b/src/content/doc-sdk-javascript/index.mdx index 8e1872eaf..f4eed0301 100644 --- a/src/content/doc-sdk-javascript/index.mdx +++ b/src/content/doc-sdk-javascript/index.mdx @@ -138,8 +138,19 @@ The [SurrealDB SDK for JavaScript](https://npmjs.com/package/surrealdb) is the p You can find example repositories that demonstrate how to integrate SurrealDB in a number of different environments: -- [Surreal Stickies](https://github.com/surrealdb/examples/tree/main/notes-v2) -- [TypeScript Starter](https://github.com/surrealdb/examples/tree/main/ts-bun-starter) + + + + + ## Sources diff --git a/src/content/doc-sdk-javascript/methods/create.mdx b/src/content/doc-sdk-javascript/methods/create.mdx index 37a5ede7b..7b0c959af 100644 --- a/src/content/doc-sdk-javascript/methods/create.mdx +++ b/src/content/doc-sdk-javascript/methods/create.mdx @@ -11,8 +11,8 @@ import Label from "@components/shared/Label.astro"; Creates a record in the database. -```javascript title="Method Syntax" -db.create(thing, data) +```ts title="Method Syntax" +db.create(thing, data) ``` ### Arguments diff --git a/src/content/doc-sdk-javascript/methods/delete.mdx b/src/content/doc-sdk-javascript/methods/delete.mdx index f031c4c29..aec107b3b 100644 --- a/src/content/doc-sdk-javascript/methods/delete.mdx +++ b/src/content/doc-sdk-javascript/methods/delete.mdx @@ -12,7 +12,7 @@ import Label from "@components/shared/Label.astro"; Deletes all records in a table, or a specific record, from the database. ```ts title="Method Syntax" -async db.delete(thing) +async db.delete(thing,data) ``` ### Arguments diff --git a/src/content/doc-sdk-javascript/methods/index.mdx b/src/content/doc-sdk-javascript/methods/index.mdx index a3a2b37b4..4c2eeab61 100644 --- a/src/content/doc-sdk-javascript/methods/index.mdx +++ b/src/content/doc-sdk-javascript/methods/index.mdx @@ -67,7 +67,7 @@ This page lists out the methods that are available in the SurrealDB class. Selects all records in a table, or a specific record - async db.live<T>(tabel, callback,diff) + async db.live<T>(table, callback,diff) Initiate a live query diff --git a/src/content/doc-sdk-javascript/methods/insert.mdx b/src/content/doc-sdk-javascript/methods/insert.mdx index a337ef957..709a6ac57 100644 --- a/src/content/doc-sdk-javascript/methods/insert.mdx +++ b/src/content/doc-sdk-javascript/methods/insert.mdx @@ -12,7 +12,7 @@ import Label from "@components/shared/Label.astro"; Inserts one or multiple records in the database. ```ts title="Method Syntax" -async db.insert(thing, data) +async db.insert(thing, data) ``` ### Arguments diff --git a/src/content/doc-sdk-javascript/methods/merge.mdx b/src/content/doc-sdk-javascript/methods/merge.mdx index f9ab01eb2..5ad526841 100644 --- a/src/content/doc-sdk-javascript/methods/merge.mdx +++ b/src/content/doc-sdk-javascript/methods/merge.mdx @@ -12,7 +12,7 @@ import Label from "@components/shared/Label.astro"; Modifies all records in a table, or a specific record, in the database. ```javascript title="Method Syntax" -async db.merge(thing, data) +async db.merge(thing, data) ``` > [!NOTE] diff --git a/src/content/doc-sdk-javascript/methods/patch.mdx b/src/content/doc-sdk-javascript/methods/patch.mdx index 49eb190ab..70e4ef2ff 100644 --- a/src/content/doc-sdk-javascript/methods/patch.mdx +++ b/src/content/doc-sdk-javascript/methods/patch.mdx @@ -12,7 +12,7 @@ import Label from "@components/shared/Label.astro"; Applies JSON Patch changes to all records, or a specific record, in the database. ```ts title="Method Syntax" -async db.patch(thing, data) +async db.patch(thing, data) ``` > [!NOTE] diff --git a/src/content/doc-sdk-javascript/methods/update.mdx b/src/content/doc-sdk-javascript/methods/update.mdx index 8553e28c6..e70219921 100644 --- a/src/content/doc-sdk-javascript/methods/update.mdx +++ b/src/content/doc-sdk-javascript/methods/update.mdx @@ -12,7 +12,7 @@ import Label from "@components/shared/Label.astro"; Updates all records in a table, or a specific record, in the database. ```ts title="Method Syntax" -async db.update(thing, data) +async db.update(thing, data) ``` > [!NOTE]