@exabugs/dynamodb-client 1.15.0 → 1.16.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,45 @@
1
+ # Records クライアントのヘルパー
2
+
3
+ Lambda などのサーバー側から Records(IAM 版 Function URL)を呼ぶ定型を、ライブラリが持つ([ADR 0006](adr/0006-library-owns-table-operations.md))。`@exabugs/dynamodb-client/client/iam` から import する。
4
+
5
+ | 関数 | 用途 |
6
+ | ------------------------------------ | -------------------------------------------------------------------------------------------- |
7
+ | `createRecordsClient(options?)` | `DynamoClient` の生成と `connect()`。`connect()` の呼び忘れを防ぐ |
8
+ | `withCollection(name, fn, options?)` | 作る → `fn(collection)` → 確実に close。Lambda の 1 回の呼び出しで使い切る |
9
+ | `lazyCollection(name, options?)` | 最初の使用時に 1 回だけ接続し、コンテナ内で再利用。失敗はキャッシュしない。`.reset()` で破棄 |
10
+ | `createAdminDirectory(options?)` | `@exabugs/dynamodb-client/notify`。`admins` の全件から通知の宛先(メール)を返す |
11
+
12
+ `options`: `url`(省略時は環境変数 `RECORDS_IAM_URL`)、`region`(省略時は `AWS_REGION`、無ければ `REGION`)、`timeout`(ms。Records の Lambda の timeout より短くする)。
13
+
14
+ `createAdminDirectory` の主なオプション: `recipientFields`(宛先を取るフィールド。既定 `['id']`、`['email','id']` で `email ?? id`)、`maxAdmins`(既定 200。件数が超えたら例外、ちょうどは成功)、`sortById`(id 昇順で読む)、`source`(`authorizedCollection` の `list` と同じ形。権限チェック層を通すアプリが渡す。指定すると自前で接続しない)。
15
+
16
+ ## Lambda での使い方
17
+
18
+ コンテナで使い回す(コールドスタートで 1 回だけ接続):
19
+
20
+ ```ts
21
+ import { lazyCollection } from '@exabugs/dynamodb-client/client/iam';
22
+
23
+ const venues = lazyCollection<VenueRecord>('venues', { timeout: 15_000 });
24
+
25
+ export const handler = async () => {
26
+ const active = await (await venues()).find({ status: 'active' }).toArray();
27
+ // ...
28
+ };
29
+ ```
30
+
31
+ 呼び出しごとに使い切る:
32
+
33
+ ```ts
34
+ import { withCollection } from '@exabugs/dynamodb-client/client/iam';
35
+
36
+ await withCollection<WeatherRecord>('weather', (weather) =>
37
+ weather.updateOne({ id }, { $set: data }, { upsert: true })
38
+ );
39
+ ```
40
+
41
+ asanowa の `functions/weather` の `getDynamoClient`(`new DynamoClient` + `connect()` + `db()` のキャッシュ)は、`lazyCollection` の 1 行になる。
42
+
43
+ ## テスト
44
+
45
+ `DynamoClient` を丸ごとモックすると `connect()` の呼び忘れを見逃す。本物の `DynamoClient` を使い、`fetch` だけ差し替える(`vi.stubGlobal('fetch', ...)`。例: `__tests__/client/recordsClient.test.ts`)。コレクションを差し替えたいときは、`lazyCollection` の戻り値の関数をアプリ側のモジュールに閉じ込め、そのモジュールをモックする。
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@exabugs/dynamodb-client",
3
- "version": "1.15.0",
3
+ "version": "1.16.0",
4
4
  "description": "DynamoDB Single-Table Client SDK with MongoDB-like API, Shadow Records, and Lambda implementation for serverless applications",
5
5
  "author": "exabugs",
6
6
  "license": "MIT",
@@ -43,7 +43,8 @@
43
43
  "LICENSE",
44
44
  "CHANGELOG.md",
45
45
  "docs/NOTIFY.md",
46
- "docs/DYNAMODB_ACCESS.md"
46
+ "docs/DYNAMODB_ACCESS.md",
47
+ "docs/RECORDS_CLIENT.md"
47
48
  ],
48
49
  "exports": {
49
50
  ".": {
@@ -39,8 +39,7 @@ module "media" {
39
39
  region = "ap-northeast-1"
40
40
 
41
41
  # Records の IAM 版 Function URL とエイリアスの ARN(本体の出力)。media の Lambda は
42
- # DynamoDB に直接触れず、Records を呼ぶ(ADR 0005)。table_name / table_arn / shadow_* は
43
- # deprecated(1.16.0 で削除)で、渡しても無視される
42
+ # DynamoDB に直接触れず、Records を呼ぶ(ADR 0005)
44
43
  records_iam_function_url = module.records.iam_function_url
45
44
  records_iam_alias_arn = module.records.iam_alias_arn
46
45
 
@@ -36,18 +36,6 @@ variable "records_iam_alias_arn" {
36
36
  }
37
37
  }
38
38
 
39
- variable "table_name" {
40
- description = "deprecated。1.16.0 で削除(ADR 0005)。media の Lambda は DynamoDB に直接触れなくなったため無視される。"
41
- type = string
42
- default = ""
43
- }
44
-
45
- variable "table_arn" {
46
- description = "deprecated。1.16.0 で削除(ADR 0005)。media の Lambda は DynamoDB に直接触れなくなったため無視される。"
47
- type = string
48
- default = ""
49
- }
50
-
51
39
  variable "image_cache_ttl_days" {
52
40
  description = "cache/{fileId}/{width}(オンデマンドリサイズ結果)のS3 Lifecycleによる自動削除までの日数"
53
41
  type = number
@@ -60,31 +48,6 @@ variable "allowed_upload_origins" {
60
48
  default = []
61
49
  }
62
50
 
63
- # 以下の shadow_* は deprecated(1.16.0 で削除。ADR 0005)。Records 経由になったため無視される。
64
- variable "shadow_created_at_field" {
65
- description = "deprecated。1.16.0 で削除(ADR 0005)。media の Lambda は DynamoDB に直接触れなくなったため無視される。"
66
- type = string
67
- default = "createdAt"
68
- }
69
-
70
- variable "shadow_updated_at_field" {
71
- description = "deprecated。1.16.0 で削除(ADR 0005)。media の Lambda は DynamoDB に直接触れなくなったため無視される。"
72
- type = string
73
- default = "updatedAt"
74
- }
75
-
76
- variable "shadow_string_max_bytes" {
77
- description = "deprecated。1.16.0 で削除(ADR 0005)。media の Lambda は DynamoDB に直接触れなくなったため無視される。"
78
- type = number
79
- default = 100
80
- }
81
-
82
- variable "shadow_number_padding" {
83
- description = "deprecated。1.16.0 で削除(ADR 0005)。media の Lambda は DynamoDB に直接触れなくなったため無視される。"
84
- type = number
85
- default = 15
86
- }
87
-
88
51
  variable "signing_public_key_param" {
89
52
  description = "CloudFront署名用の公開鍵(PEM形式)を保持するSSM Parameter名。値そのものは呼び出し側が別途登録する"
90
53
  type = string