rajt 0.0.49 → 0.0.50
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.
- package/package.json +1 -1
- package/src/dynamodb/client.ts +28 -10
- package/src/dynamodb/model.ts +4 -25
package/package.json
CHANGED
package/src/dynamodb/client.ts
CHANGED
|
@@ -21,7 +21,7 @@ import {
|
|
|
21
21
|
} from '@aws-sdk/lib-dynamodb'
|
|
22
22
|
import type { NativeAttributeValue } from '@aws-sdk/util-dynamodb'
|
|
23
23
|
import AbstractModel from './model'
|
|
24
|
-
import { Keys } from './types'
|
|
24
|
+
import { Keys, KeySchema } from './types'
|
|
25
25
|
|
|
26
26
|
const client = new DynamoDBClient(process.env?.AWS_SAM_LOCAL ? {
|
|
27
27
|
region: process.env.AWS_REGION || "us-east-1",
|
|
@@ -48,7 +48,7 @@ export class RawClient {
|
|
|
48
48
|
static async get(TableName: string, key: Keys | Record<string, string>, sk?: string) {
|
|
49
49
|
return DocumentClient.send(new GetCommand({
|
|
50
50
|
TableName,
|
|
51
|
-
Key: this
|
|
51
|
+
Key: this.key(key, sk),
|
|
52
52
|
}))
|
|
53
53
|
}
|
|
54
54
|
|
|
@@ -71,12 +71,12 @@ export class RawClient {
|
|
|
71
71
|
sk?: string
|
|
72
72
|
) {
|
|
73
73
|
return DocumentClient.send(new UpdateCommand({
|
|
74
|
-
...filters, TableName, Key: this
|
|
74
|
+
...filters, TableName, Key: this.key(key, sk),
|
|
75
75
|
}))
|
|
76
76
|
}
|
|
77
77
|
|
|
78
78
|
static async delete(TableName: string, key: Keys | Record<string, string>, sk?: string) {
|
|
79
|
-
return DocumentClient.send(new DeleteCommand({ TableName, Key: this
|
|
79
|
+
return DocumentClient.send(new DeleteCommand({ TableName, Key: this.key(key, sk) }))
|
|
80
80
|
}
|
|
81
81
|
|
|
82
82
|
static async batchGet(batch: BatchGetCommandInput) {
|
|
@@ -87,23 +87,41 @@ export class RawClient {
|
|
|
87
87
|
return DocumentClient.send(new BatchWriteCommand(batch))
|
|
88
88
|
}
|
|
89
89
|
|
|
90
|
-
static
|
|
91
|
-
|
|
92
|
-
|
|
90
|
+
static key(
|
|
91
|
+
key: Keys | Record<string, string>,
|
|
92
|
+
sk?: string,
|
|
93
|
+
schema?: KeySchema,
|
|
94
|
+
defaultSK?: string,
|
|
95
|
+
) {
|
|
93
96
|
let pk: string
|
|
94
97
|
let skValue: string | undefined
|
|
95
98
|
|
|
96
99
|
if (Array.isArray(key)) {
|
|
97
100
|
pk = key[0]
|
|
98
101
|
skValue = key[1] ?? sk
|
|
102
|
+
} else if (typeof key == 'object' && key != null) {
|
|
103
|
+
return key
|
|
99
104
|
} else {
|
|
100
105
|
pk = key
|
|
101
106
|
skValue = sk
|
|
102
107
|
}
|
|
103
108
|
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
109
|
+
if (!schema) {
|
|
110
|
+
const keys = {PK: pk}
|
|
111
|
+
// @ts-ignore
|
|
112
|
+
if (skValue) keys.SK = skValue
|
|
113
|
+
return keys
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
const keys = { [schema.PK]: pk }
|
|
117
|
+
|
|
118
|
+
if (schema?.SK) {
|
|
119
|
+
if (skValue) {
|
|
120
|
+
keys[schema.SK] = skValue
|
|
121
|
+
} else if (defaultSK) {
|
|
122
|
+
keys[schema.SK] = defaultSK
|
|
123
|
+
}
|
|
124
|
+
}
|
|
107
125
|
|
|
108
126
|
return keys
|
|
109
127
|
}
|
package/src/dynamodb/model.ts
CHANGED
|
@@ -75,7 +75,7 @@ export default class AbstractModel<T extends object> {
|
|
|
75
75
|
}
|
|
76
76
|
|
|
77
77
|
async get(key: Keys, sk?: string) {
|
|
78
|
-
const result = await RawClient.get(this.table, key, sk)
|
|
78
|
+
const result = await RawClient.get(this.table, this.#key(key, sk))
|
|
79
79
|
return result.Item ? this.#processItem(result.Item) : undefined
|
|
80
80
|
}
|
|
81
81
|
|
|
@@ -118,13 +118,13 @@ export default class AbstractModel<T extends object> {
|
|
|
118
118
|
UpdateExpression,
|
|
119
119
|
ExpressionAttributeValues,
|
|
120
120
|
ExpressionAttributeNames,
|
|
121
|
-
}, key)
|
|
121
|
+
}, this.#key(key))
|
|
122
122
|
|
|
123
123
|
return this.#processItem(attrs, keys)
|
|
124
124
|
}
|
|
125
125
|
|
|
126
126
|
async delete(key: Keys, sk?: string) {
|
|
127
|
-
return RawClient.delete(this.table, key, sk)
|
|
127
|
+
return RawClient.delete(this.table, this.#key(key, sk))
|
|
128
128
|
}
|
|
129
129
|
|
|
130
130
|
async batchGet(keys: Array<Keys>) {
|
|
@@ -157,28 +157,7 @@ export default class AbstractModel<T extends object> {
|
|
|
157
157
|
|
|
158
158
|
#key(key: Keys, sk?: string) {
|
|
159
159
|
if (!this.#meta.keys) return {}
|
|
160
|
-
|
|
161
|
-
let pk: string
|
|
162
|
-
let skValue: string | undefined
|
|
163
|
-
if (Array.isArray(key)) {
|
|
164
|
-
pk = key[0]
|
|
165
|
-
skValue = key[1] ?? sk
|
|
166
|
-
} else {
|
|
167
|
-
pk = key
|
|
168
|
-
skValue = sk
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
const keys = { [this.#meta.keys.PK]: pk }
|
|
172
|
-
|
|
173
|
-
if (this.#meta.keys?.SK) {
|
|
174
|
-
if (skValue) {
|
|
175
|
-
keys[this.#meta.keys.SK] = skValue
|
|
176
|
-
} else if (this.#meta.defaultSK) {
|
|
177
|
-
keys[this.#meta.keys.SK] = this.#meta.defaultSK
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
return keys
|
|
160
|
+
return RawClient.key(key, sk, this.#meta.keys, this.#meta.defaultSK)
|
|
182
161
|
}
|
|
183
162
|
|
|
184
163
|
#getItemKey(item: Partial<T>, key?: Keys): Record<string, string> {
|