Skip to content

Commit

Permalink
Improve docs: add record API code examples for JS/TS.
Browse files Browse the repository at this point in the history
  • Loading branch information
ignatz committed Jan 20, 2025
1 parent 8f9d8ad commit a6bf5f0
Show file tree
Hide file tree
Showing 13 changed files with 447 additions and 7 deletions.
2 changes: 1 addition & 1 deletion docs/examples/record_api_dart/test/record_api_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Future<Client> connect() async {
}

void main() {
test('Test examples', () async {
test('Test code examples', () async {
final client = await connect();

final tableStream = await subscribeAll(client);
Expand Down
28 changes: 28 additions & 0 deletions docs/examples/record_api_ts/eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import pluginJs from "@eslint/js";
import tseslint from "typescript-eslint";

export default [
pluginJs.configs.recommended,
...tseslint.configs.recommended,
{
ignores: ["dist/", "node_modules/", "types"],
},
{
files: ["scripts/*.{js,mjs,cjs,mts,ts,tsx,jsx}"],
rules: {
// https://typescript-eslint.io/rules/no-explicit-any/
"@typescript-eslint/no-explicit-any": "warn",
// http://eslint.org/docs/rules/no-unused-vars
"@typescript-eslint/no-unused-vars": [
"error",
{
vars: "all",
args: "after-used",
argsIgnorePattern: "^_",
varsIgnorePattern: "^_",
},
],
"no-empty": ["error", { allowEmptyCatch: true }],
},
},
];
21 changes: 21 additions & 0 deletions docs/examples/record_api_ts/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "record_api_ts",
"version": "1.0.0",
"description": "Example uses of record APIs for documentation purposes.",
"scripts": {
"check": "tsc --noEmit --skipLibCheck && eslint",
"test": "vitest run"
},
"devDependencies": {
"@eslint/js": "^9.18.0",
"@types/node": "^22.10.6",
"eslint": "^9.18.0",
"prettier": "^3.4.2",
"typescript": "^5.7.3",
"typescript-eslint": "^8.20.0",
"vitest": "^2.1.8"
},
"dependencies": {
"trailbase": "workspace:*"
}
}
4 changes: 4 additions & 0 deletions docs/examples/record_api_ts/src/create.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { Client } from "trailbase";

export const create = async (client: Client): Promise<string | number> =>
await client.records("simple_strict_table").createId({ text_not_null: "test" });
4 changes: 4 additions & 0 deletions docs/examples/record_api_ts/src/delete.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { Client } from "trailbase";

export const remove = async (client: Client, id: string | number) =>
await client.records("simple_strict_table").delete(id);
4 changes: 4 additions & 0 deletions docs/examples/record_api_ts/src/read.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { Client } from "trailbase";

export const read = async (client: Client, id: string | number) =>
await client.records("simple_strict_table").read(id);
7 changes: 7 additions & 0 deletions docs/examples/record_api_ts/src/subscribe.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Client } from "trailbase";

export const subscribe = async (client: Client, id: string | number) =>
await client.records("simple_strict_table").subscribe(id);

export const subscribeAll = async (client: Client) =>
await client.records("simple_strict_table").subscribe("*");
4 changes: 4 additions & 0 deletions docs/examples/record_api_ts/src/update.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { Client } from "trailbase";

export const update = async (client: Client, id: string | number, record: object) =>
await client.records("simple_strict_table").update(id, record);
60 changes: 60 additions & 0 deletions docs/examples/record_api_ts/tests/basic.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { Client, type Event } from "trailbase";
import { expect, test } from "vitest";

import { create } from "../src/create.ts";
import { read } from "../src/read.ts";
import { update } from "../src/update.ts";
import { remove } from "../src/delete.ts";
import { subscribe, subscribeAll } from "../src/subscribe.ts";

async function connect(): Promise<Client> {
const client = new Client("http://localhost:4000");
await client.login("admin@localhost", "secret");
return client;
}

test("Test code examples", async () => {
const client = await connect();

const tableStream = await subscribeAll(client);

const id = await create(client);

const recordStream = await subscribe(client, id);

{
const record = await read(client, id);
expect(record).toMatchObject({ "text_not_null": "test" });
}

{
await update(client, id, { "text_not_null": "updated" });
const record = await read(client, id);
expect(record).toMatchObject({ "text_not_null": "updated" });
}

await remove(client, id);

{
const events: Event[] = [];
for await (const event of tableStream) {
events.push(event);
if (events.length === 3) {
break;
}
}
tableStream.cancel();
}

{
const events: Event[] = [];
for await (const event of recordStream) {
events.push(event);
if (events.length === 2) {
break;
}
}
recordStream.cancel();
}

});
21 changes: 21 additions & 0 deletions docs/examples/record_api_ts/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"compilerOptions": {
"strict": true,
"strictNullChecks": true,
"forceConsistentCasingInFileNames": true,
"esModuleInterop": false,
"moduleResolution": "bundler",
"target": "ESNext",
"module": "ESNext",
"noEmit": true,
"paths": {
"@/*": ["./src/*"],
"@bindings/*": ["../../../trailbase-core/bindings/*"]
}
},
"include": ["src/**/*"],
"exclude": [
"dist",
"node_modules"
]
}
25 changes: 25 additions & 0 deletions docs/src/content/docs/documentation/APIs/record_apis.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,13 @@ The create endpoint lets you insert new records and potentially override
existing ones depending on conflict resolution strategy.

import createDartCode from "@examples/record_api_dart/lib/src/create.dart?raw";
import createTsCode from "@examples/record_api_ts/src/create.ts?raw";

<Tabs>
<TabItem label="JS/TS">
<Code lang="ts" code={createTsCode} />
</TabItem>

<TabItem label="Dart">
<Code lang="dart" code={createDartCode} />
</TabItem>
Expand All @@ -157,8 +162,13 @@ import createDartCode from "@examples/record_api_dart/lib/src/create.dart?raw";
The read endpoint lets you read specific records given their id.

import readDartCode from "@examples/record_api_dart/lib/src/read.dart?raw";
import readTsCode from "@examples/record_api_ts/src/read.ts?raw";

<Tabs>
<TabItem label="JS/TS">
<Code lang="ts" code={readTsCode} />
</TabItem>

<TabItem label="Dart">
<Code lang="dart" code={readDartCode} />
</TabItem>
Expand All @@ -169,8 +179,13 @@ import readDartCode from "@examples/record_api_dart/lib/src/read.dart?raw";
The update endpoint lets you modify, i.e. partially update, existing records given their id

import updateDartCode from "@examples/record_api_dart/lib/src/update.dart?raw";
import updateTsCode from "@examples/record_api_ts/src/update.ts?raw";

<Tabs>
<TabItem label="JS/TS">
<Code lang="ts" code={updateTsCode} />
</TabItem>

<TabItem label="Dart">
<Code lang="dart" code={updateDartCode} />
</TabItem>
Expand All @@ -179,8 +194,13 @@ import updateDartCode from "@examples/record_api_dart/lib/src/update.dart?raw";
### Delete

import deleteDartCode from "@examples/record_api_dart/lib/src/update.dart?raw";
import deleteTsCode from "@examples/record_api_ts/src/delete.ts?raw";

<Tabs>
<TabItem label="JS/TS">
<Code lang="ts" code={updateTsCode} />
</TabItem>

<TabItem label="Dart">
<Code lang="dart" code={deleteDartCode} />
</TabItem>
Expand Down Expand Up @@ -230,8 +250,13 @@ an API or specific records given their id. Change events can be insertions,
updates, and deletions.

import subscribeDartCode from "@examples/record_api_dart/lib/src/subscribe.dart?raw";
import subscribeTsCode from "@examples/record_api_ts/src/subscribe.ts?raw";

<Tabs>
<TabItem label="JS/TS">
<Code lang="ts" code={subscribeTsCode} />
</TabItem>

<TabItem label="Dart">
<Code lang="dart" code={subscribeDartCode} />
</TabItem>
Expand Down
Loading

0 comments on commit a6bf5f0

Please sign in to comment.