Singleton
Using singleton
Section titled “Using singleton”We can create a client instance in a host program, rather than in starsquid, so we can more flexibly manage the client.
Initialize a Squidex client using SquidexClientFactory
method from module "starsquid/api"
.
import { SquidexClientFactory } from "starsquid/api";
export const squidexClient = SquidexClientFactory( import.meta.env.SQUIDEX_APP_NAME, import.meta.env.SQUIDEX_CLIENT_ID, import.meta.env.SQUIDEX_CLIENT_SECRET, import.meta.env.SQUIDEX_URL);
Then passing the squidexClient. It will use you already initialize client to fetch data from Squidex. That means it won’t create a new instance of SquidexClient, which implements the singletong feature, and you can also use this singleton to do more things, such as fetch data in real time.
import { squidexCollections } from "starsquid/loaders";import { squidexClient } from "./scripts/client";
export const collections = squidexCollections({ squidexUrl: import.meta.env.SQUIDEX_URL, squidexAppName: import.meta.env.SQUIDEX_APP_NAME, squidexClientId: import.meta.env.SQUIDEX_CLIENT_ID, squidexClientSecret: import.meta.env.SQUIDEX_CLIENT_SECRET, squidexClient: squidexClient,});
How does singleton work?
Section titled “How does singleton work?”The relationship between App, Collection and Squidex Client.
Factory writing inspired by astro-ghostcms-loader.