初投稿です。JSforce 2.0でのレコードの型が気になりました。いずれドキュメントなどで公開されるのだと思いますが私は今知りたかったので……。
環境は次の通りです。
- JSforce 2.0.0-alpha.11
- TypeScript 3.8.3
あるオブジェクトのレコードの型は SObjectRecord
とスキーマとオブジェクト名で表せるようです。
type Account = SObjectRecord<StandardSchema, "Account">;
const accounts: Account[] = await conn
.sobject("Account")
.find();
const id: string = accounts[0].Id;
項目を絞るときはそのパターンを追加します。
type Account = SObjectRecord<StandardSchema, "Account", "Id" | "Name">;
const accounts: Account[] = await conn
.sobject("Account")
.find(null, ["Id", "Name"]);
const name: string = accounts[0].Name;
親のレコードを含むときも同様です。
type Account = SObjectRecord<StandardSchema, "Account", "Id" | "Name" | { Owner: "Id" | "Name" }>;
const accounts: Account[] = await conn
.sobject("Account")
.find(null, ["Id", "Name", "Owner.Id", "Owner.Name"]);
const ownerId: string = accounts[0].Owner.Id;
子のレコードを含むときは SObjectChildRelationshipProp
で拡張します。
type Contact = SObjectRecord<StandardSchema, "Contact", "Id" | "Name">;
type Account = SObjectRecord<StandardSchema, "Account", "Id" | "Name"> &
SObjectChildRelationshipProp<"Contacts", Contact>;
const accounts: Account[] = await conn
.sobject("Account")
.find(null, ["Id", "Name"])
.include("Contacts", null, ["Id", "Name"])
.end();
const contactIds: string[] = accounts[0].Contacts?.records.map(contact => contact.Id) ?? [];
これで大体のレコードに型を付けられそうです。やっていきましょう。