rajt 0.0.43 → 0.0.45
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/compact.ts +13 -6
- package/src/dynamodb/model.ts +14 -3
- package/src/dynamodb/schema.ts +33 -4
package/package.json
CHANGED
package/src/dynamodb/compact.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { SchemaStructure } from './types'
|
|
2
2
|
import getLength from '../utils/lenght'
|
|
3
|
+
import { isArraySchema } from './schema'
|
|
3
4
|
|
|
4
5
|
export default class Compact {
|
|
5
6
|
static #typeRegex: RegExp
|
|
@@ -49,7 +50,7 @@ export default class Compact {
|
|
|
49
50
|
if (!val) return val as T
|
|
50
51
|
|
|
51
52
|
if (Array.isArray(val))
|
|
52
|
-
return val.map((i: {
|
|
53
|
+
return val.map((i: {v: string}) => this.decode<T>(i?.V, schema)).filter(Boolean) as T
|
|
53
54
|
|
|
54
55
|
return val?.V ? this.decode<T>(val.V, schema) : val
|
|
55
56
|
}
|
|
@@ -68,6 +69,9 @@ export default class Compact {
|
|
|
68
69
|
}
|
|
69
70
|
|
|
70
71
|
static zip(obj: any, schema: SchemaStructure, seen: any[]): any[] {
|
|
72
|
+
if (Array.isArray(obj))
|
|
73
|
+
return obj?.length ? obj.map(item => this.zip(item, schema, seen)) : []
|
|
74
|
+
|
|
71
75
|
if (!obj || [null, true, false].includes(obj)) return obj
|
|
72
76
|
|
|
73
77
|
return schema.map(key => {
|
|
@@ -85,16 +89,16 @@ export default class Compact {
|
|
|
85
89
|
})
|
|
86
90
|
}
|
|
87
91
|
|
|
88
|
-
static unzip(val: any, seen: any[] = []
|
|
92
|
+
static unzip(val: any, seen: any[] = []): any[] {
|
|
93
|
+
if (Array.isArray(val))
|
|
94
|
+
return val?.length ? val.map(item => this.unzip(item, seen)) : []
|
|
95
|
+
|
|
89
96
|
const type = typeof val
|
|
90
97
|
const length = getLength(val, type)
|
|
91
98
|
|
|
92
99
|
if ([null, true, false].includes(val) || type != 'object' && length < 2)
|
|
93
100
|
return val
|
|
94
101
|
|
|
95
|
-
if (Array.isArray(val))
|
|
96
|
-
return val.map(item => this.unzip(item, seen, deep))
|
|
97
|
-
|
|
98
102
|
if (type == 'object') {
|
|
99
103
|
for (const key in val)
|
|
100
104
|
val[key] = this.unzip(val[key], seen)
|
|
@@ -111,10 +115,13 @@ export default class Compact {
|
|
|
111
115
|
return val
|
|
112
116
|
}
|
|
113
117
|
|
|
114
|
-
static withSchema(value: any[], keys: any[]): any {
|
|
118
|
+
static withSchema(value: any[], keys: any[], deep = false): any {
|
|
115
119
|
if (!value || !Array.isArray(value))
|
|
116
120
|
return value
|
|
117
121
|
|
|
122
|
+
if (!deep && isArraySchema(keys))
|
|
123
|
+
return value?.length ? value.map(v => this.withSchema(v, keys, true)) : []
|
|
124
|
+
|
|
118
125
|
return Object.fromEntries(
|
|
119
126
|
keys.map((key, index) => this.entry(key, value[index])).filter(Boolean)
|
|
120
127
|
)
|
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> {
|
|
@@ -280,13 +281,23 @@ export default class AbstractModel<T extends object> {
|
|
|
280
281
|
|
|
281
282
|
#processItem(item: any, keys?: Record<string, string>): T {
|
|
282
283
|
if (this.#meta.zip && item?.V) {
|
|
283
|
-
const
|
|
284
|
+
const value = Compact.decode<T>(item.V, this.#meta.fields)
|
|
285
|
+
const model = isArraySchema(this.#meta.fields) && Array.isArray(value)
|
|
286
|
+
? value.map(v => new this.cls!(v))
|
|
287
|
+
: new this.cls!(value)
|
|
288
|
+
|
|
284
289
|
if (!keys) keys = this.#getItemKey(item)
|
|
285
290
|
|
|
286
|
-
|
|
287
|
-
return model.withKey(keys[this.#meta.keys.PK], keys[this.#meta.keys.SK] || undefined)
|
|
291
|
+
return this.#withKey(model as T, keys)
|
|
288
292
|
}
|
|
289
293
|
|
|
290
294
|
return new this.cls!(item)
|
|
291
295
|
}
|
|
296
|
+
|
|
297
|
+
#withKey(model: T, keys: Record<string, string>): T {
|
|
298
|
+
// @ts-ignore
|
|
299
|
+
if (Array.isArray(model)) return model.map(m => this.#withKey(m))
|
|
300
|
+
// @ts-ignore
|
|
301
|
+
return model.withKey(keys[this.#meta.keys.PK], keys[this.#meta.keys.SK] || undefined)
|
|
302
|
+
}
|
|
292
303
|
}
|
package/src/dynamodb/schema.ts
CHANGED
|
@@ -1,7 +1,12 @@
|
|
|
1
1
|
import { z, ZodTypeAny } from 'zod'
|
|
2
2
|
import type { SchemaStructure } from './types'
|
|
3
3
|
|
|
4
|
-
|
|
4
|
+
const m = Symbol('a')
|
|
5
|
+
export function isArraySchema(v: any) : boolean {
|
|
6
|
+
return v[m] || false
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export function extractZodKeys(schema: ZodTypeAny): SchemaStructure {
|
|
5
10
|
if (schema instanceof z.ZodObject) {
|
|
6
11
|
return Object.entries(schema.shape).map(([key, value]) => {
|
|
7
12
|
const inner = unwrap(value as ZodTypeAny)
|
|
@@ -18,15 +23,39 @@ function extractZodKeys(schema: ZodTypeAny): SchemaStructure {
|
|
|
18
23
|
})
|
|
19
24
|
}
|
|
20
25
|
|
|
26
|
+
if (schema instanceof z.ZodArray) {
|
|
27
|
+
const item = unwrap(schema._def.type as ZodTypeAny)
|
|
28
|
+
if (item instanceof z.ZodObject) {
|
|
29
|
+
const r = extractZodKeys(item)
|
|
30
|
+
// @ts-ignore
|
|
31
|
+
r[m] = true
|
|
32
|
+
return r
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
return []
|
|
36
|
+
}
|
|
37
|
+
|
|
21
38
|
return []
|
|
22
39
|
}
|
|
23
40
|
|
|
24
|
-
function unwrap(schema: ZodTypeAny): ZodTypeAny {
|
|
41
|
+
export function unwrap(schema: ZodTypeAny): ZodTypeAny {
|
|
25
42
|
if (schema instanceof z.ZodOptional || schema instanceof z.ZodNullable)
|
|
26
43
|
return unwrap(schema._def.innerType)
|
|
27
44
|
|
|
28
|
-
if (schema instanceof z.
|
|
29
|
-
return unwrap(schema._def.
|
|
45
|
+
if (schema instanceof z.ZodDefault)
|
|
46
|
+
return unwrap(schema._def.innerType)
|
|
47
|
+
|
|
48
|
+
// if (schema instanceof z.ZodUnion)
|
|
49
|
+
// return unwrap(schema._def.options[0] as ZodTypeAny)
|
|
50
|
+
|
|
51
|
+
if (schema instanceof z.ZodUnion) {
|
|
52
|
+
const options = schema._def.options as ZodTypeAny[]
|
|
53
|
+
const nonEmptyOption = options.find(opt => !(opt instanceof z.ZodUndefined) && !(opt instanceof z.ZodNull))
|
|
54
|
+
return nonEmptyOption ? unwrap(nonEmptyOption) : options[0]
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
if (schema instanceof z.ZodEffects)
|
|
58
|
+
return unwrap(schema._def.schema)
|
|
30
59
|
|
|
31
60
|
return schema
|
|
32
61
|
}
|