メインコンテンツまでスキップ

Angular SDK(Angular 向け)

Squid を Angular アプリケーションに統合するためのライブラリです。

SDK バージョン

機能

  • Squid Client SDK の初期化と注入(inject)を行うための Angular module と provider

はじめに

要件

この SDK は、Angular ドキュメントに記載されている「現在サポートされているバージョン」 のみをサポートします。他のバージョンでも互換性がある可能性はありますが、積極的にはサポートされません。

インストール

npm を使用する場合:

npm install @squidcloud/angular

Squid の設定

Squid Console を使用して Application を作成します。

  • Angular の root module で SquidModule を import し、Squid の application ID と region を指定して設定します:
import { SquidModule } from '@squidcloud/angular';
// ...
@NgModule({
// ...
imports: [
SquidModule.forRoot({
appId: '<YOUR_APP_ID>',
region: '<YOUR_SQUID_REGION>',
}),
],
// ...

上記により、アプリケーション内のさまざまな service や component に inject できる Squid インスタンスが提供されます。

代わりに、factory function を使用して Squid インスタンスを提供することもできます:

  • Squid クラスと Squid factory provider を import します:
import { provideSquid } from '@squidcloud/angular';
import { Squid } from '@squidcloud/client';
  • アプリケーションの providers に provideSquid provider を追加します:
@NgModule({
// ...
providers: [
{
provide: Squid,
useFactory: provideSquid({
appId: '<YOUR_APP_ID>',
region: '<YOUR_SQUID_REGION>',
}),
deps: [NgZone],
},
],
// ...

この設定により、同一の Angular アプリケーション内で複数の Squid インスタンスを作成できます。 たとえば、Angular アプリケーション内で 2 つの Squid インスタンスを作成できます:

export const usersSquidInjectionToken = new InjectionToken<Squid>('usersSquid');
export const billingSquidInjectionToken = new InjectionToken<Squid>('billingSquid');

@NgModule({
// ...
providers: [
{
provide: usersSquidInjectionToken,
useFactory: provideSquid({
appId: '<YOUR_APP_ID>',
region: '<YOUR_SQUID_REGION>',
}),
deps: [NgZone],
},
{
provide: billingSquidInjectionToken,
useFactory: provideSquid({
appId: '<YOUR_OTHER_APP_ID>',
region: '<YOUR_OTHER_SQUID_REGION>',
}),
deps: [NgZone],
},
],
// ...

Angular component で Squid client を使用する

import { Component } from '@angular/core';
import { Squid } from '@squidcloud/client';

@Component({
selector: 'my-component',
templateUrl: './my.component.html',
styleUrls: ['./my.component.css'],
})
export class MyComponent {
constructor(private readonly squid: Squid) {}
// ...
}

Angular service で Squid client を使用する

import { Injectable } from '@angular/core';
import { Squid } from '@squidcloud/client';

@Injectable({ providedIn: 'root' })
export class MyService {
constructor(private readonly squid: Squid) {}
// ...
}

また、token を使って提供されている場合は、injection token を使って Squid インスタンスを inject できます:

import { Injectable, Inject } from '@angular/core';
import { Squid } from '@squidcloud/client';
import { usersSquidInjectionToken } from './my.module';

@injectable({ providedIn: 'root' })
export class MyService {
constructor(@Inject(usersSquidInjectionToken) private readonly usersSquid: Squid) {}
// ...
}

Squid を使用する component の完全に動作する例:

import { Component } from '@angular/core';
import { Squid } from '@squidcloud/client';
import { map } from 'rxjs';

// 型を定義
type User = { id: string; email: string; age: number };

@Component({
selector: 'my-component',
template: `
<ul>
<li *ngFor="let user of users | async">
{{ user.email }}
</li>
</ul>
<br />
<button (click)="createNewUser()">Create user</button>
`,
})
export class MyComponent {
// データを subscribe
users = this.squid.collection<User>('Users').query().gt('age', 18).dereference().snapshots();

constructor(private readonly squid: Squid) {}

// データを insert
async createNewUser(): Promise<void> {
const userId = crypto.randomUUID();
const email = `${userId}@gmail.com`;
await this.squid
.collection<User>('Users')
.doc(userId)
.insert({
id: userId,
email,
age: Math.floor(Math.random() * 100),
});
}
}