fastschema
v1.0.3
Published
Fastschema SDK for JavaScript
Downloads
12
Maintainers
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
<script src="https://unpkg.com/fastschema@latest/dist/fastschema.umd.js"></script>
<script>
const fs = new fastschema.FastSchema("http://localhost:8000");
</script>
NPM
npm install fastschema
Login and initialize
The initialization must be done before any other operation.
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
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.
const schemaTag = fs.schema("tag");
Update a schema
await fs.schemas().update("tag", {
schema: {
// Same as create
},
rename_fields: {
// Rename fields
},
rename_tables: {
// Rename tables
},
});
Delete a schema
await fs.schemas().delete("tag");
Content operations
Get content
fs.schema("tag").get<Tag>(params);
params
can be one of the following:
id: number | string
: ID of the contentA filter object that represents the following interface:
interface ListOptions { filter?: Filter; page?: number; limit?: number; sort?: string; select?: string; }
Refer to the Filter documentation for more information about the filter object.
Create content
interface Tag {
name: string;
description: string;
}
const createdTag = await fs.schema("tag").create<Tag>({
name: "Tag 01",
description: "A description",
});
Update content
const updated = await fs.schema("tag").update(id, {
description: "updated desc tag 1",
});
Delete content
await fs.schema("tag").delete(id);
Upload files
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 createdupdate
: When a record is updateddelete
: When a record is deleted*
: Listen to all events
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.
schemaTag.on("create", id, cb1);
schemaTag.on("update", id, cb1);
schemaTag.on("delete", id, cb1);
or use the configuration events:
schemaTag.on({
id?: number;
once?: boolean;
select?: string;
filter?: Filter;
}, cb1);
The configuration object can have the following properties:
id
: ID of the recordonce
: If true, the callback will be called only onceselect
: 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.