https://github.com/fastschema/sdk-js
https://github.com/fastschema/sdk-js
Last synced: 5 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/fastschema/sdk-js
- Owner: fastschema
- Created: 2024-07-04T02:36:07.000Z (almost 2 years ago)
- Default Branch: master
- Last Pushed: 2025-09-18T10:56:27.000Z (9 months ago)
- Last Synced: 2025-09-18T12:49:59.159Z (9 months ago)
- Language: TypeScript
- Size: 146 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Javascript SDK for FastSchema
**Document**
https://fastschema.com/docs/sdk/
FastSchema SDK provides a convenient way to connect to the FastSchema backend and perform various operations.
The FastSchema JavaScript SDK works in both Node.js and browser environments. To use the SDK, you need to install it using npm.
## Installation
FastSchema SDK can be installed using browser script tags or npm.
### Browser
```html
const fs = new fastschema.FastSchema("http://localhost:8000");
```
### NPM
```bash
npm install fastschema
```
## Login and initialize
The initialization must be done before any other operation.
```typescript
import { FastSchema } from "fastschema";
// Create a new instance of FastSchema
const fs = new FastSchema("https://localhost:8000");
// Login
await fs.auth().login({
login: "admin",
password: "123",
});
// Initialize: This must be called before any other operation
await fs.init();
```
## Schema operations
### Create schema
```typescript
await fs.schemas().create({
name: "tag",
namespace: "tags",
label_field: "name",
fields: [
{
name: "name",
label: "Name",
type: "string",
sortable: true,
filterable: true,
unique: false,
},
{
name: "description",
label: "Description",
type: "string",
optional: true,
},
],
});
```
### Get schema
This operation will throw an error if the schema does not exist.
```typescript
const schemaTag = fs.schema("tag");
```
### Update a schema
```typescript
await fs.schemas().update("tag", {
schema: {
// Same as create
},
rename_fields: {
// Rename fields
},
rename_tables: {
// Rename tables
},
});
```
### Delete a schema
```typescript
await fs.schemas().delete("tag");
```
## Content operations
### Get content
```typescript
fs.schema("tag").get(params);
```
`params` can be one of the following:
- `id: number | string`: ID of the content
- A filter object that represents the following interface:
```typescript
interface ListOptions {
filter?: Filter;
page?: number;
limit?: number;
sort?: string;
select?: string;
}
```
Refer to the [Filter documentation](https://fastschema.com/docs/headless-cms/list-records.html#filter) for more information about the filter object.
### Create content
```typescript
interface Tag {
name: string;
description: string;
}
const createdTag = await fs.schema("tag").create({
name: "Tag 01",
description: "A description",
});
```
### Update content
```typescript
const updated = await fs.schema("tag").update(id, {
description: "updated desc tag 1",
});
```
### Delete content
```typescript
await fs.schema("tag").delete(id);
```
### Upload files
```typescript
const files: File[] = [];
for (let i = 0; i < 5; i++) {
files.push(new File([`test ${i}`], `test${i}.txt`));
}
const result = await fs.file().upload(files);
```
**Note**
Nodejs version before 20 does not support the `File` object.
You can use package `@web-std/file` to create a `File` object.
## Realtime Updates
FastSchema provides a way to listen to events in real-time.
- `create`: When a new record is created
- `update`: When a record is updated
- `delete`: When a record is deleted
- `*`: Listen to all events
```typescript
const schemaTag = fs.schema("tag");
const cb1 = (data: T, event: EventType, error: Error) => {
console.log("Event:", event, "Data:", data, "Error:", error);
};
const cb2 = (data: T[], event: EventType, error: Error) => {
console.log("Event:", event, "Data:", data, "Error:", error);
};
const cb3 = (data: T | T[], event: EventType, error: Error) => {
console.log("Event:", event, "Data:", data, "Error:", error);
};
schemaTag.on("create", cb1);
schemaTag.on("update", cb2);
schemaTag.on("delete", cb2);
schemaTag.on("*", cb3);
```
You can also listen to events for a specific record.
```typescript
schemaTag.on("create", id, cb1);
schemaTag.on("update", id, cb1);
schemaTag.on("delete", id, cb1);
```
or use the configuration events:
```typescript
schemaTag.on({
id?: number;
once?: boolean;
select?: string;
filter?: Filter;
}, cb1);
```
The configuration object can have the following properties:
- `id`: ID of the record
- `once`: If true, the callback will be called only once
- `select`: Fields to select, separated by commas. This is useful when you want to select only specific fields to reduce the payload size.
- `filter`: Filter object, used to filter the records that will trigger the event.