MRS SDK 參考
本文檔說明如何使用 MRS 軟體開發套件,並討論用戶端 API。
章節概觀
另請參閱
- MySQL REST 服務 - 開發人員指南 - 本書說明如何安裝和設定 MySQL REST 服務 (MRS),以及如何透過 REST 呼叫存取資料。
- MySQL REST 服務 - 核心 REST API - 本書提供在啟用 REST 功能後,如何使用 MySQL REST 服務查詢和其他針對表格和檢視表的操作範例。
- MySQL REST 服務 - SQL 參考 - 本書討論 MySQL REST 服務 SQL 擴充功能。
1 MRS SDK 簡介
MySQL REST 服務提供軟體開發套件 (SDK),可簡化撰寫用戶端應用程式和與 REST 服務互動的流程。
SDK 的特色是為每個 MRS REST 服務產生特定的用戶端 API,因此為每個 REST 專案提供最佳支援。
目前支援 TypeScript。計畫支援其他語言。
1.1 SDK 檔案的產生
1.1.1 即時產生 TypeScript SDK
適用於 VS Code 的 MySQL Shell 擴充功能允許在資料庫筆記本中互動執行 TypeScript 程式碼。為了更容易使用 MySQL REST 服務,目前的 REST 服務的 TypeScript SDK 可直接在資料庫筆記本中使用。
每當編輯 REST 資料庫物件時,就會更新 TypeScript SDK,以便使用用戶端 API 即時建立 REST 查詢原型。
這可調整和微調 REST 資料庫物件,直到它們完全符合開發人員的需求,並為開發專案建立用戶端 API 呼叫原型。
1.1.2 為開發專案產生 SDK 檔案
若要為開發專案產生 SDK 檔案,請在 MRS 服務上按一下滑鼠右鍵,然後選取 [匯出 REST 服務 SDK 檔案...]。這可讓您選取開發專案中的目的地資料夾,將檔案放置於其中。
下列檔案將放置於選取的資料夾中。
TypeScript
MrsBaseClasses.ts
- 包含用戶端 API 所使用的 MRS 基底類別的檔案<RestServiceName>.ts
- 使用服務名稱的特定 REST 服務用戶端 API。config.json
- 用於 SDK 產生的組態檔
若要開始使用用戶端 API,請在您的專案中匯入 <RestServiceName>.ts
檔案。
2 跨表格查詢資料
MySQL 支援外部索引鍵,允許跨表格交叉參考相關資料,以及外部索引鍵條件約束,以協助保持相關資料一致。
外部索引鍵關係包含一個保存初始欄位值的父表格,以及一個具有參考父欄位值的欄位值的子表格。外部索引鍵條件約束定義在子表格上。外部索引鍵可在這些表格中的列之間建立一對一、一對多或多對多的關係。
透過 MySQL REST 服務,這些關係可以擴充,以包含來自不同表格的相關資料,這些資料使用每個 MRS 資料庫物件可用的 JSON 關聯性雙重功能嵌入在同一個結果集中。然後,用戶端可以選取應使用特定的 HTTP 查詢語法展開哪些欄位,以指定和瀏覽其他表格上欄位的巢狀路徑,這些欄位由主要 (或父) 表格中的根欄位參考。
MRS SDK 的主要功能是可以查詢兩個資料庫物件之間的這些關聯,並在查詢回應中包含或排除特定的欄位。
此功能可使用下列 API 命令中的 select
選項取得
findFirst()
findMany()
findUnique()
預設情況下,所有物件欄位 (已展開或未展開) 及其值都會在查詢回應中傳回。可以使用純物件格式將特定欄位從查詢回應中排除,其中屬性是要排除之欄位的名稱,且每個值都是 false
。
透過使用 Sakila 範例資料庫 的設定 (其中結構描述可在名為 myService
的 REST 服務下取得),且城市和國家/地區表格 (一對一) 之間的關係透過 JSON/關聯性雙重功能展開,則 lastUpdate
和 country.lastUpdate
欄位可按如下所示排除
.sakila.city.findFirst({ select: { lastUpdate: false, country: { lastUpdate: false } } })
myService
{"city": "A Coruña (La Coruña)",
"links": [
{"rel": "self",
"href": "/myService/sakila/city/1"
},
]"cityId": 1,
"country": {
"country": "Spain",
"countryId": 87
,
}"countryId": 87
}
同樣地,如果擴展了 actor
和 film
表格 (多對多) 之間的關係,則下列命令會排除每個巢狀物件上的識別碼
.sakila.actor.findFirst({ select: { filmActor: { actorId: false, film: { filmId: false, languageId: false, originalLanguageId: false } } } })
myService
{
{"links": [
{"rel": "self",
"href": "/myService/sakila/actor/58"
},
]"actorId": 58,
"lastName": "AKROYD",
"filmActor": [
{"film": {
"title": "BACKLASH UNDEFEATED",
"length": 118,
"rating": "PG-13",
"lastUpdate": "2006-02-15 05:03:42.000000",
"rentalRate": 4.99,
"description": "A Stunning Character Study of a Mad Scientist And a Mad Cow who must Kill a Car in A Monastery",
"releaseYear": 2006,
"rentalDuration": 3,
"replacementCost": 24.99,
"specialFeatures": "Trailers,Behind the Scenes"
,
}"filmId": 48,
"lastUpdate": "2006-02-15 05:05:03.000000"
,
}// ...
,
]"firstName": "CHRISTIAN",
"lastUpdate": "2006-02-15 04:34:33.000000"
}
另一方面,可以使用相同的物件格式並將值設定為 true
,或是使用要包含的欄位名稱清單,來選取並包含查詢回應中的欄位。
同樣地,這適用於一對一關係
.sakila.city.findFirst({ select: { city: true, country: { country: true } } })
myService
{"city": "A Coruña (La Coruña)",
"links": [
{"rel": "self",
"href": "/myService/sakila/city/1"
},
]"country": {
"country": "Spain",
} }
也適用於多對多關係
.sakila.actor.findFirst({ select: ['filmActor.film.title'] })
myService
{
{"links": [
{"rel": "self",
"href": "/myService/sakila/actor/58"
},
]"filmActor": [
{"film": {
"title": "BACKLASH UNDEFEATED"
},
}
{"film": {
"title": "BETRAYED REAR"
}
}// ...
] }
3 檢查 NULL 欄位值
MySQL 支援 NOT NULL
條件約束,可確保給定欄位中的值不能為 NULL。不過,如果省略,欄位可以保存 NULL
值。透過 MySQL REST 服務,使用 $null
或 $notnull
運算子,可以將包含 NULL 值的欄位的記錄包含在結果集中或從結果集中排除。
TypeScript MRS SDK 提供特殊語法,可透過給定欄位在包含 (或不包含) NULL
值時,來篩選結果集中的記錄。透過使用 Sakila 範例資料庫 的設定 (其中結構描述可在名為 myService
的 REST 服務下取得),則可以按如下所示依 NULL
欄位值篩選記錄
.sakila.address.findMany({ select: ["address", "address2"], where: { address2: null } })
myService
{"items": [
{"links": [
{"rel": "self",
"href": "/myService/sakila/address/1"
},
]"address": "47 MySakila Drive",
"address2": null,
"_metadata": {
"etag": "44EA44E1541A6A0A24135C4CC4F30E52AA2B4256181DE9BC1960C78A35F33B27"
},
}
{"links": [
{"rel": "self",
"href": "/myService/sakila/address/2"
},
]"address": "28 MySQL Boulevard",
"address2": null,
"_metadata": {
"etag": "C5C68338EBF92980E1B8FDAE3FE7E3CE9507C4169C3DEC1BDB4E9AF2D961E00D"
},
}
{"links": [
{"rel": "self",
"href": "/myService/sakila/address/3"
},
]"address": "23 Workhaven Lane",
"address2": null,
"_metadata": {
"etag": "7EF99DD02DF9071C8946B6180E74EB11D6B47FDD03A36C9B44B920F2A8D3684B"
},
}
{"links": [
{"rel": "self",
"href": "/myService/sakila/address/4"
},
]"address": "1411 Lillydale Drive",
"address2": null,
"_metadata": {
"etag": "5F4F5E570F2AF2BB5E5A7AE41548CE4965F715F7C040A80B42D0DB79BB57336B"
}
},
]"limit": 25,
"offset": 0,
"hasMore": false,
"count": 4,
"links": [
{"rel": "self",
"href": "/myService/sakila/address/"
}
] }
同樣地,可以按如下所示篩選給定欄位不包含 NULL
的記錄
.sakila.actor.findFirst({ select: ["address", "address2"], where: { address2: { not: null } } })
myService
{"links": [
{"rel": "self",
"href": "/myService/sakila/address/5"
},
]"address": "1913 Hanoi Way",
"address2": "",
"_metadata": {
"etag": "689439C1F6D1F101E9A146F8DE244F01F0CE40AEBFA92AE5CEABA119F9C1573E"
} }
嘗試將此類篩選條件套用至對應至具有 NOT NULL
條件約束的欄位的欄位,應會產生 TypeScript 編譯錯誤
.sakila.actor.findFirst({ where: { address: null } }) myService
Type 'null' is not assignable to type 'string | DataFilterField<IMyServiceSakilaAddressParams, string | undefined> | ComparisonOpExpr<string | undefined>[] | undefined'.
4 使用空間資料類型
MySQL 支援擴充的 SQL 環境 (以 OpenGIS Geometry 模型建立的慣例為基礎),讓一組空間欄位資料類型可以保存幾何值。其中一些保存單一值
GEOMETRY
POINT
LINESTRING
POLYGON
GEOMETRY
可以儲存任何類型的幾何值。其他單一值類型 (POINT
、LINESTRING
和 POLYGON
) 將其值限制為特定幾何類型。
另一方面,有一些空間資料類型旨在保存幾何值的集合
MULTIPOINT
MULTILINESTRING
MULTIPOLYGON
GEOMETRYCOLLECTION
GEOMETRYCOLLECTION
可以儲存任何類型物件的集合。其他集合類型 (MULTIPOINT
、MULTILINESTRING
和 MULTIPOLYGON
) 將集合成員限制為具有特定幾何類型的成員。
MySQL Rest Service SDK 支援兩種格式來表示、操作或處理空間資料
- Well-Known Text (WKT)
- GeoJSON
在插入或更新包含與空間資料類型欄位對應之欄位的記錄時,可以使用這兩種格式。
例如,透過使用 Sakila 範例資料庫 的設定 (其中結構描述可在名為 myService
的 REST 服務下取得),當將記錄插入 address
表格時,您可以按如下所示指定 location
欄位的值 (此欄位具有通用 GEOMETRY
資料類型)
// WKT
.sakila.address.create({ data: { location: "Point(11.11 12.22)" }})
myService
.sakila.address.createMany([{
myService: {
data: "Point(0 0)"
location
}, {
}: {
data: "Point(11.11 12.22)"
location
}
}])
// GeoJSON
.sakila.address.create({ data: {
myService: {
location: "Point",
type: [11.11, 12.22]
coordinates
} }})
當更新相同表格上的記錄時,也應該套用相同的慣例。
// WKT
.sakila.address.update({
myService: {
where: 1
address_id
}: {
data: "Point(11.11 12.22)"
location
}
})
.sakila.address.updateMany({
myService: [{
where: 1
address_id, {
}: 2
address_id,
}]: {
data: "Point(11.11 12.22)"
location
}
})
// GeoJSON
.sakila.address.update({
myService: {
where: 1
address_id
}: {
data: {
location: "Point",
type: [11.11, 12.22]
coordinates
}
}
})
.sakila.address.updateMany({
myService: [{
where: 1
address_id, {
}: 2
address_id,
}]: {
data: {
location: "Point",
type: [11.11, 12.22]
coordinates
}
} })
如果欄位具有狹窄的資料類型 (例如 POINT
),而不是較通用的 GEOMETRY
,則在用戶端指定不相容的類型應該會產生編譯錯誤。例如,假設建立表格 mrs_tests
.spatial_tests
如下
CREATE DATABASE IF NOT EXISTS mrs_tests;
CREATE TABLE IF NOT EXISTS mrs_tests.spatial_tests (id INT AUTO_INCREMENT NOT NULL, ls LINESTRING, PRIMARY KEY (id));
如果表格 (和對應的結構描述) 可從相同的 myService
REST 服務取得,則嘗試插入 POINT
無法運作,因為欄位只接受 LINESTRING
。
.mrsTests.spatialTests.create({
myService: {
data: {
ls: "Point",
type: [0, 0]
coordinates
}
} })
類似於上述的命令會產生編譯錯誤。
類型 ‘Point’ 不可指派給類型 ‘LineString’。
同樣地,當欄位資料類型只允許單一值時,嘗試為單一欄位插入或更新多個值,或反之亦然,也應該會產生編譯錯誤。例如,假設建立 mrs_tests.spatial_tests
表格如下
CREATE TABLE IF NOT EXISTS mrs_tests.wl15912 (id INT AUTO_INCREMENT NOT NULL, ls GEOMETRYCOLLECTION, PRIMARY KEY (id));
嘗試插入 POINT
無法運作,因為欄位只接受 MULTIPOINT
、MULTILINESTRING
或 MULTIPOLYGON
。
.mrsTests.spatialTests.create({
myService: {
data: {
ls: "Point",
type: [0, 0]
coordinates
}
} })
在此情況下,命令會產生下列編譯錯誤
類型 ‘Point’ 不可指派給類型 ‘MultiPoint | MultiLineString | MultiPolygon’。
5 TypeScript 用戶端 API 參考
5.1 create (建立)
create
用於在指定的表格中插入記錄。記錄會表示為純 TypeScript/JavaScript 物件,或表示為封裝建立新記錄所需資料之特定類別的執行個體。若要插入多筆記錄,請參閱 createMany
[#createMany]。
5.1.1 選項 (建立)
名稱 | 類型 | 必要 | 描述 |
---|---|---|---|
資料 | 物件 | 是 | 包含要插入的記錄之欄位名稱與值的對應關係的物件。 |
5.1.2 返回類型 (create)
一個 JSON 物件,代表插入的記錄。
5.1.3 參考 (create)
async function create (args: ICreateOptions<Type>): Promise<Type> {
// ...
}
interface ICreateOptions<Type> {
: Type
data }
5.1.4 範例 (create)
import type { IMyServiceMrsNotesNote } from '/path/to/sdk/myService';
import { MyService } from './myService.mrs.sdk/myService';
const myService = new MyService();
// using a plain object
.mrsNotes.note.create({ data: { title: 'foo' } });
myService
// using a custom class instance
class Note implements IMyServiceMrsNotesNote {
// ...
}
const note = new Note();
.title = 'foo';
note
.mrsNotes.note.create({ data: note }); myService
5.2 createMany
createMany
在給定的資料表中插入一或多筆記錄。這些記錄表示為純 TypeScript/JavaScript 物件,或者,作為封裝建立它們所需資料的特定類別的實例。
5.2.1 選項 (create)
名稱 | 類型 | 必要 | 描述 |
---|---|---|---|
資料 | 物件 | 是 | 包含要插入的記錄之欄位名稱與值的對應關係的物件陣列。 |
5.2.2 返回類型 (createMany)
一個 JSON 物件陣列,代表插入的記錄。
5.2.3 參考 (createMany)
async function createMany (args: ICreateOptions<Type[]>): Promise<Type[]> {
// ...
}
interface ICreateOptions<Type> {
: Type
data }
5.2.4 範例 (createMany)
import type { IMyServiceMrsNotesNote } from '/path/to/sdk/myService';
import { MyService } from './myService.mrs.sdk/myService';
const myService = new MyService();
// using a plain object
.mrsNotes.note.createMany({ data: [{ title: 'foo' }, { title: 'bar' }] });
myService
// using a custom class
class Note implements IMyServiceMrsNotesNote {
// ...
}
const note1 = new Note();
.title = 'foo';
note1
const note2 = new Note({ /* */ });
.title = 'bar';
note1
.mrsNotes.note.createMany({ data: [note1, note2] }); myService
5.3 findFirst
findFirst
用於查詢符合給定可選篩選條件的第一筆記錄。
5.3.1 選項 (findFirst)
名稱 | 類型 | 必要 | 描述 |
---|---|---|---|
where | 物件 | 否 | 套用至特定欄位的篩選條件。 |
select | 物件 | 否 | 指定要在返回的物件中包含哪些屬性。 |
skip | 數字 | 否 | 指定在返回其中一個匹配項之前要跳過的記錄數量。 |
5.3.2 返回類型 (findFirst)
一個 JSON 物件,代表符合篩選條件的第一筆記錄,如果未找到記錄,則為 undefined
。
5.3.3 參考 (findFirst)
async function findFirst (args?: IFindOptions<Selectable, Filterable>): Promise<Selectable | undefined> {
// ...
}
export interface IFindOptions<Selectable, Filterable> {
?: ColumnOrder<Filterable>,
orderBy?: BooleanFieldMapSelect<Selectable> | FieldNameSelect<Selectable>,
select?: number,
skip?: DataFilter<Filterable>,
where }
5.3.4 範例 (findFirst)
import { MyService } from './myService.mrs.sdk/myService';
const myService = new MyService();
// get the first note, without any filter
await myService.mrsNotes.note.findFirst();
// get the last note, without any filter
await myService.mrsNotes.note.findFirst({ orderBy: { id: "DESC" } });
// get the second note, without any filter
await myService.mrsNotes.note.findFirst({ skip: 1 });
// get the title and shared fields of the second note
await myService.mrsNotes.note.findFirst({ select: { title: true, shared: true }, skip: 1 });
// get the title and shared fields of the first note
await myService.mrsNotes.note.findFirst({ select: ["title", "shared"] });
// get the first shared note
await myService.mrsNotes.note.findFirst({ where: { shared: true } });
// get the first note whose title includes the string "foo"
await myService.mrsNotes.note.findFirst({ where: { title: { $like: "%foo%" } } });
5.4 findUnique
findUnique
用於查詢由以下項目唯一識別的單一記錄
- 主鍵欄位
- 唯一欄位
如果沒有找到符合給定 where
條件的記錄,則返回 undefined
。若要在此情況下拋出例外,請參閱 findUniqueOrThrow。
5.4.1 選項 (findUnique)
名稱 | 類型 | 必要 | 描述 |
---|---|---|---|
where | 物件 | 是 | 封裝所有唯一欄位,以便可以選擇個別記錄。 |
select | 物件 | 否 | 指定要在返回的物件中包含哪些屬性。 |
5.4.2 返回類型 (findUnique)
一個 JSON 物件,代表符合篩選條件的特定記錄,如果未找到記錄,則為 undefined
。
5.4.3 參考 (findUnique)
async function findUnique (args?: IFindUniqueOptions<Selectable, Filterable>): Promise<Selectable | undefined> {
// ...
}
interface IFindUniqueOptions<Selectable, Filterable> {
?: BooleanFieldMapSelect<Selectable> | FieldNameSelect<Selectable>,
select?: DataFilter<Filterable>,
where }
5.4.4 範例 (findUnique)
import { MyService } from './myService.mrs.sdk/myService';
const myService = new MyService();
// Get the note with id 4.
// using implicit equality
await myService.mrsNotes.note.findUnique({ where: { id: 4 } });
// or using explicit equality
await myService.mrsNotes.note.findUnique({ where: { id: { $eq: 4 } } });
5.5 findUniqueOrThrow
findUniqueOrThrow
以與 findUnique 相同的方式檢索單一資料記錄。但是,如果查詢沒有找到記錄,它會拋出 NotFoundError
。
findUniqueOrThrow
與 findUnique
的不同之處如下
- 它的返回類型不可為 Null。例如,myService.mrsNotes.note.findUnique() 可以返回 note 或 undefined,但 myService.mrsNotes.note.findUniqueOrThrow() 始終返回 note。
5.6 findMany
findMany
用於查詢一或多個頁面中的所有記錄,並且可選擇性地查詢符合給定篩選條件的記錄。
5.6.1 選項 (findMany)
名稱 | 類型 | 必要 | 描述 |
---|---|---|---|
where | 物件 | 否 | 套用至特定欄位的篩選條件。 |
select | 物件 | 否 | 指定要在返回的物件中包含哪些屬性。 |
skip | 數字 | 否 | 在返回其中一個匹配項之前要跳過的記錄數量。 |
take | 數字 | 否 | 要返回的最大記錄數量。 |
fetchAll | 物件 | 布林值 | 否 |
5.6.2 返回類型 (findMany)
一個 JSON 物件陣列,代表符合篩選條件的記錄。
5.6.3 參考 (findMany)
async function findMany (args?: IFindOptions<Selectable, Filterable>): Promise<IMrsResultList<Selectable>> {
// ...
}
interface IFindOptions<Selectable, Filterable> {
?: IFindAllOptions<Selectable> | boolean,
fetchAll?: ColumnOrder<Filterable>,
orderBy?: BooleanFieldMapSelect<Selectable> | FieldNameSelect<Selectable>,
select?: number,
skip?: number,
take?: DataFilter<Filterable>,
where }
5.6.4 範例 (findMany)
import { MyService } from './myService.mrs.sdk/myService';
const myService = new MyService();
// get all notes of the first page
await myService.mrsNotes.note.findMany();
// get the first 3 notes of the first page
await myService.mrsNotes.note.findMany({ take: 3 });
// get the first 50 notes
await myService.mrsNotes.note.findMany({ fetchAll: true, take: 50 });
// get all notes whose id is greater than 10
await myService.mrsNotes.note.findMany({ fetchAll: true, where: { id: { $gt: 10 } } });
5.7 delete
delete
用於刪除符合給定必要篩選條件的第一筆記錄。
5.7.1 選項 (delete)
名稱 | 類型 | 必要 | 描述 |
---|---|---|---|
where | 物件 | 是 | 套用至特定欄位的篩選條件。 |
5.7.2 返回類型 (delete)
一個 JSON 物件,包含已刪除的記錄數量(始終為 1)。
5.7.3 參考 (delete)
async function delete (args: IDeleteOptions<IMyServiceMrsNotesUserParams>): Promise<IMrsDeleteResult> {
// ...
}
interface IDeleteOptions<Filterable> {
?: DataFilter<Filterable>,
where
}
interface IMrsDeleteResult {
: 1,
itemsDeleted }
5.7.4 範例 (delete)
import { MyService } from './myService.mrs.sdk/myService';
const myService = new MyService();
// delete the first note whose title includes the string "foo"
await myService.mrsNotes.note.delete({ where: { title: { $like: "%foo%" } } });
5.8 deleteMany
delete
用於刪除所有符合給定篩選條件的記錄。
5.8.1 選項 (deleteMany)
名稱 | 類型 | 必要 | 描述 |
---|---|---|---|
where | 物件 | 否 | 套用至特定欄位的篩選條件。 |
5.8.2 返回類型 (deleteMany)
一個 JSON 物件,包含已刪除的記錄數量。
5.8.3 參考 (deleteMany)
async function deleteMany (args: IDeleteOptions<IMyServiceMrsNotesUserParams>): Promise<IMrsDeleteResult> {
// ...
}
interface IDeleteOptions<Filterable> {
?: DataFilter<Filterable>,
where
}
interface IMrsDeleteResult {
: number,
itemsDeleted }
5.8.4 範例 (deleteMany)
import { MyService } from './myService.mrs.sdk/myService';
const myService = new MyService();
// delete all notes whose title includes the string "foo"
await myService.mrsNotes.note.deleteMany({ where: { title: { $like: "%foo%" } } });
// delete all shared notes
await myService.mrsNotes.note.deleteMany({ where: { shared: true } });
5.9 update
update
用於更新具有給定識別碼或主鍵的記錄。
5.9.1 選項 (update)
名稱 | 類型 | 必要 | 描述 |
---|---|---|---|
資料 | 物件 | 是 | 要更新的欄位集及對應的值。 |
where | 物件 | 是 | 符合的識別碼或主鍵。 |
5.9.2 返回類型 (update)
一個 JSON 物件,代表最新的記錄。
5.9.3 參考 (update)
async function update (args: IUpdateOptions<Data, Filterable, ["id"], { batch: false }>): Promise<Data> {
// ...
}
interface IUpdateOptions<Data, Filterable, PrimaryKeys extends Array<string & keyof Filterable>, Config> {
: Data,
data: Config extends IBatchConfig ? Array<UpdateMatch<Filterable, PrimaryKeys>> : UpdateMatch<Filterable, PrimaryKeys>
where
}
interface IBatchConfig {
: true
batch }
5.9.4 範例 (update)
import type { IMyServiceMrsNotesNote } from '/path/to/sdk/myService';
import { MyService } from './myService.mrs.sdk/myService';
const myService = new MyService();
// update the note with id is 1 using a plain object
await myService.mrsNotes.note.update({ where: { id: 1 }, data: { title: 'bar' } } );
// using a custom class instance
class Note implements IMyServiceMrsNotesNote {
// ...
}
const note = new Note();
.shared = false;
note
// update the note with id 1
await myService.mrsNotes.note.update({ where: { id: 1 }, data: note });
5.10 updateMany
updateMany
用於更新所有具有符合識別碼或主鍵的記錄。
5.10.1 選項 (updateMany)
名稱 | 類型 | 必要 | 描述 |
---|---|---|---|
資料 | 物件 | 是 | 要更新的欄位集及對應的值。 |
where | 物件 | 是 | 符合的識別碼或主鍵。 |
5.10.2 返回類型 (updateMany)
一個 JSON 物件陣列,代表最新的記錄。
5.10.3 參考 (updateMany)
async function updateMany (args: IUpdateOptions<Data, Filterable, ["id"], { batch: true }>): Promise<Data[]> {
// ...
}
interface IUpdateOptions<Data, Filterable, PrimaryKeys extends Array<string & keyof Filterable>, Config> {
: Data,
data: Config extends IBatchConfig ? Array<UpdateMatch<Filterable, PrimaryKeys>> : UpdateMatch<Filterable, PrimaryKeys>
where
}
interface IBatchConfig {
: true
batch }
5.10.4 範例 (updateMany)
import type { IMyServiceMrsNotesNote } from '/path/to/sdk/myService';
import { MyService } from './myService.mrs.sdk/myService';
const myService = new MyService();
// update the notes with id 1 and 2 using a plain object
await myService.mrsNotes.note.update({ where: [{ id: 1 }, { id: 2 }], data: { title: 'bar' } });
// using a custom class instance
class Note implements IMyServiceMrsNotesNote {
// ...
}
const note = new Note();
.shared = false;
note
// update the note with id 1 and 2
await myService.mrsNotes.note.update({ where: [{ id: 1 }, { id: 2 }], data: note });
版權所有 (c) 2022, 2023, Oracle 和/或其附屬公司。