-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve docs: add record API code examples for JS/TS.
- Loading branch information
Showing
13 changed files
with
447 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }], | ||
}, | ||
}, | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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:*" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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("*"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.