Skip to content

Commit

Permalink
update js docs (#867)
Browse files Browse the repository at this point in the history
  • Loading branch information
kearfy authored Sep 18, 2024
1 parent 49203a3 commit 3bb9ad0
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 27 deletions.
92 changes: 79 additions & 13 deletions src/content/doc-sdk-javascript/core/data-querying.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ CREATE $thing CONTENT $data;
Inserts one or multiple records in the database.

```ts title="Method Syntax"
async db.insert<T>(thing, data)
async db.insert<T>(table, data)
```

### Arguments
Expand All @@ -243,11 +243,11 @@ async db.insert<T>(thing, data)
<tbody>
<tr>
<td colspan="2" scope="row" data-label="Arguments">
<code>thing</code>
<Label label="required" />
<code>table</code>
<Label label="optional" />
</td>
<td colspan="2" scope="row" data-label="Description">
The table name or [`RecordId`](/docs/sdk/javascript/data-types#recordid) to insert to.
Optionally pass along a table to insert into.
</td>
</tr>
<tr>
Expand Down Expand Up @@ -282,14 +282,6 @@ const [person] = await db.insert<Person>('person', {
},
});

const person = await db.insert<Person>(new RecordId('person', 'tobie'), {
name: 'Tobie',
settings: {
active: true,
marketing: true,
},
});

// Insert multiple records
const people = await db.insert<Person>('person', [
{
Expand Down Expand Up @@ -322,7 +314,81 @@ const people = await db.insert<
This function will run the following query in the database.

```surql
INSERT INTO $thing $data;
INSERT INTO $table $data;
```

<br />

## `.insert_relation()` {#insert_relation}

Inserts one or multiple relations in the database.

```ts title="Method Syntax"
async db.insert<T>(table, data)
```

### Arguments
<table>
<thead>
<tr>
<th colspan="2" scope="col">Arguments</th>
<th colspan="2" scope="col">Description</th>
</tr>
</thead>
<tbody>
<tr>
<td colspan="2" scope="row" data-label="Arguments">
<code>table</code>
<Label label="optional" />
</td>
<td colspan="2" scope="row" data-label="Description">
Optionally pass along a table to insert into.
</td>
</tr>
<tr>
<td colspan="2" scope="row" data-label="Arguments">
<code>data</code>
<Label label="optional" />
</td>
<td colspan="2" scope="row" data-label="Description">
Either a single document/record or an array of documents/records to insert
</td>
</tr>
</tbody>
</table>

### 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>('likes', {
in: new RecordId('person', 'tobie'),
out: new RecordId('post', 123),
});

// Insert multiple records across tables
const people = await db.insert<Likes>('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;
```

<br />
Expand Down
4 changes: 2 additions & 2 deletions src/content/doc-sdk-javascript/core/initialization.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ After [installing the SDK](/docs/sdk/javascript/setup) and selecting your import
<TabItem value="TS" label="TypeScript" default>

```typescript title="../utils/surreal.ts"
import { Surreal } from "surrealdb.js";
import { Surreal } from "surrealdb";

// highlight-start
let db: Surreal | undefined;
Expand Down Expand Up @@ -50,7 +50,7 @@ export function getDb(): Surreal | undefined {
</TabItem>
<TabItem value="JS" label="JavaScript">
```js title="../utils/surreal.js"
import { Surreal } from "surrealdb.js";
import { Surreal } from "surrealdb";

// highlight-start
let db;
Expand Down
20 changes: 19 additions & 1 deletion src/content/doc-sdk-javascript/core/utilities.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ A method allowing you to write a tagged template string, automatically escaping

```ts
// Optionally, `surrealql` is an alias export of `surql`
import { surql, surrealql } from 'surrealdb.js';
import { surql, surrealql } from 'surrealdb';

const content = { name: "Tobie" };
const query = surql`CREATE person CONTENT ${content}`;
Expand All @@ -184,3 +184,21 @@ await surreal.query(
[name.fill("Jaime")]
);
```

<br />

## String Prefixes

These methods are also based on tagged templates, but they replicate SurrealQLs string prefixes in JS.

### Example

```ts
// Optionally, `surrealql` is an alias export of `surql`
import { s, d, r, u } from 'surrealdb';

const string = s`I am a string`;
const date = d`2024-05-06T17:44:57.085Z`;
const record = r`person:tobie`;
const uuid = u`92b84bde-39c8-4b4b-92f7-626096d6c4d9`;
```
4 changes: 2 additions & 2 deletions src/content/doc-sdk-javascript/engines/node.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ It enables SurrealDB to be run in-memory, or to persist data by running on top o
### Example

```js
import { Surreal } from 'surrealdb.js';
import { Surreal } from 'surrealdb';
import { surrealdbNodeEngines } from 'surrealdb.node';

// Enable the WebAssembly engines
Expand All @@ -28,4 +28,4 @@ await db.connect("mem://");
await db.connect("surrealkv://demo");

// Now use the JavaScript SDK as normal.
```
```
2 changes: 1 addition & 1 deletion src/content/doc-sdk-javascript/engines/wasm.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ This library works with ES modules (import), not CommonJS (require).
### Example

```js
import { Surreal } from 'surrealdb.js';
import { Surreal } from 'surrealdb';
import { surrealdbWasmEngines } from 'surrealdb.wasm';

// Enable the WebAssembly engines
Expand Down
16 changes: 8 additions & 8 deletions src/content/doc-sdk-javascript/setup.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,28 @@ import TabItem from "@components/Tabs/TabItem.astro";
<TabItem value="npm" label="npm" default>

```bash
npm install --save surrealdb.js
npm install --save surrealdb
```

</TabItem>
<TabItem value="yarn" label="yarn">

```bash
yarn add surrealdb.js
yarn add surrealdb
```

</TabItem>
<TabItem value="pnpm" label="pnpm">

```bash
pnpm install surrealdb.js
pnpm install surrealdb
```

</TabItem>
<TabItem value="bun" label="bun">

```bash
bun install surrealdb.js
bun install surrealdb
```

</TabItem>
Expand All @@ -57,11 +57,11 @@ After installing, you can then import the SDK into your project. Depending on yo
> It is recommended to import this in a utility file or a file that is shared across your application.
```bash title="CommonJS"
const { Surreal } = require('surrealdb.js');
const { Surreal } = require('surrealdb');
```
```bash title="ES6"
import { Surreal } from 'surrealdb.js';
import { Surreal } from 'surrealdb';
```
In a deno runtime, you can import the SDK using the following syntax:
Expand All @@ -77,9 +77,9 @@ import Surreal from "https://deno.land/x/[email protected]/mod.ts";
Importing via a CDN is also supported:
```bash title="CDN"
import Surreal from "https://unpkg.com/surrealdb.js";
import Surreal from "https://unpkg.com/surrealdb";
// or
import Surreal from "https://cdn.jsdelivr.net/npm/surrealdb.js";
import Surreal from "https://cdn.jsdelivr.net/npm/surrealdb";
```
### Initialize the SDK
Expand Down

0 comments on commit 3bb9ad0

Please sign in to comment.