npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

fastschema

v1.0.3

Published

Fastschema SDK for JavaScript

Downloads

95

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 content

  • A 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 created
  • update: When a record is updated
  • delete: 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 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.