rajt 0.0.44 → 0.0.46
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/model.ts +17 -3
package/package.json
CHANGED
package/src/dynamodb/model.ts
CHANGED
|
@@ -13,6 +13,7 @@ import type { ModelMetadata, Keys, Model, Filter } from './types'
|
|
|
13
13
|
import { getModelMetadata } from './decorators'
|
|
14
14
|
import QueryBuilder from './query-builder'
|
|
15
15
|
import Compact from './compact'
|
|
16
|
+
import { isArraySchema } from './schema'
|
|
16
17
|
import getLength from '../utils/lenght'
|
|
17
18
|
|
|
18
19
|
export default class AbstractModel<T extends object> {
|
|
@@ -264,6 +265,9 @@ export default class AbstractModel<T extends object> {
|
|
|
264
265
|
}
|
|
265
266
|
|
|
266
267
|
#getItemWithoutKeys(item: Partial<T>): Partial<T> {
|
|
268
|
+
if (Array.isArray(item))
|
|
269
|
+
return item?.length ? item.map(i => this.#getItemWithoutKeys(i)) as T : [] as T
|
|
270
|
+
|
|
267
271
|
if (!this.#meta.keys || !item) return { ...item }
|
|
268
272
|
|
|
269
273
|
const { PK, SK } = this.#meta.keys
|
|
@@ -280,13 +284,23 @@ export default class AbstractModel<T extends object> {
|
|
|
280
284
|
|
|
281
285
|
#processItem(item: any, keys?: Record<string, string>): T {
|
|
282
286
|
if (this.#meta.zip && item?.V) {
|
|
283
|
-
const
|
|
287
|
+
const value = Compact.decode<T>(item.V, this.#meta.fields)
|
|
288
|
+
const model = isArraySchema(this.#meta.fields) && Array.isArray(value)
|
|
289
|
+
? value.map(v => new this.cls!(v))
|
|
290
|
+
: new this.cls!(value)
|
|
291
|
+
|
|
284
292
|
if (!keys) keys = this.#getItemKey(item)
|
|
285
293
|
|
|
286
|
-
|
|
287
|
-
return model.withKey(keys[this.#meta.keys.PK], keys[this.#meta.keys.SK] || undefined)
|
|
294
|
+
return this.#withKey(model as T, keys)
|
|
288
295
|
}
|
|
289
296
|
|
|
290
297
|
return new this.cls!(item)
|
|
291
298
|
}
|
|
299
|
+
|
|
300
|
+
#withKey(model: T, keys: Record<string, string>): T {
|
|
301
|
+
// @ts-ignore
|
|
302
|
+
if (Array.isArray(model)) return model.map(m => this.#withKey(m, keys))
|
|
303
|
+
// @ts-ignore
|
|
304
|
+
return model.withKey(keys[this.#meta.keys.PK], keys[this.#meta.keys.SK] || undefined)
|
|
305
|
+
}
|
|
292
306
|
}
|