rajt 0.0.50 → 0.0.51
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 +4 -7
- package/src/dynamodb/compact.ts +33 -15
- package/src/dynamodb/schema.ts +15 -9
package/package.json
CHANGED
package/src/dynamodb/client.ts
CHANGED
|
@@ -24,11 +24,11 @@ import AbstractModel from './model'
|
|
|
24
24
|
import { Keys, KeySchema } from './types'
|
|
25
25
|
|
|
26
26
|
const client = new DynamoDBClient(process.env?.AWS_SAM_LOCAL ? {
|
|
27
|
-
region: process.env.AWS_REGION ||
|
|
27
|
+
region: process.env.AWS_REGION || 'us-east-1',
|
|
28
28
|
endpoint: process.env.AWS_ENDPOINT_URL || undefined,
|
|
29
29
|
credentials: {
|
|
30
|
-
accessKeyId: process.env.AWS_ACCESS_KEY_ID ||
|
|
31
|
-
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY ||
|
|
30
|
+
accessKeyId: process.env.AWS_ACCESS_KEY_ID || 'DUMMYIDEXAMPLE',
|
|
31
|
+
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY || 'DUMMYEXAMPLEKEY',
|
|
32
32
|
},
|
|
33
33
|
} : {})
|
|
34
34
|
|
|
@@ -46,10 +46,7 @@ export class Dynamodb {
|
|
|
46
46
|
|
|
47
47
|
export class RawClient {
|
|
48
48
|
static async get(TableName: string, key: Keys | Record<string, string>, sk?: string) {
|
|
49
|
-
return DocumentClient.send(new GetCommand({
|
|
50
|
-
TableName,
|
|
51
|
-
Key: this.key(key, sk),
|
|
52
|
-
}))
|
|
49
|
+
return DocumentClient.send(new GetCommand({ TableName, Key: this.key(key, sk) }))
|
|
53
50
|
}
|
|
54
51
|
|
|
55
52
|
static async scan(TableName: string, filters: Omit<ScanCommandInput, 'TableName'>) {
|
package/src/dynamodb/compact.ts
CHANGED
|
@@ -29,14 +29,28 @@ export default class Compact {
|
|
|
29
29
|
}
|
|
30
30
|
|
|
31
31
|
static {
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
32
|
+
const keys = []
|
|
33
|
+
const values = []
|
|
34
|
+
const reverseTypeMap: Record<string, string> = {}
|
|
35
|
+
for (const key in this.#typeMap) {
|
|
36
|
+
const val = this.#typeMap[key]
|
|
37
|
+
const k = key.replace(/"/g, "'")
|
|
38
|
+
keys.push(k)
|
|
39
|
+
values.push(val)
|
|
40
|
+
|
|
41
|
+
reverseTypeMap[val] = k
|
|
42
|
+
this.#typeMap[k] = val
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
this.#reverseTypeMap = reverseTypeMap
|
|
46
|
+
this.#typeRegex = this.#mapRegex(keys)
|
|
47
|
+
this.#reverseTypeRegex = this.#mapRegex(values)
|
|
35
48
|
}
|
|
36
49
|
|
|
37
50
|
static encode(obj: any, schema: SchemaStructure): string {
|
|
38
51
|
const seen: any[] = []
|
|
39
|
-
|
|
52
|
+
|
|
53
|
+
return this.#pack(
|
|
40
54
|
JSON.stringify(this.zip(obj, schema, seen))
|
|
41
55
|
.replace(/"\^(\d+)"/g, '^$1')
|
|
42
56
|
.replace(/"/g, '~TDQ~')
|
|
@@ -49,7 +63,7 @@ export default class Compact {
|
|
|
49
63
|
static smartDecode<T = any>(val: any, schema: SchemaStructure): T {
|
|
50
64
|
if (!val) return val as T
|
|
51
65
|
|
|
52
|
-
if (Array.isArray(val))
|
|
66
|
+
if (Array.isArray(val)) // @ts-ignore
|
|
53
67
|
return val.map((i: {v: string}) => this.decode<T>(i?.V, schema)).filter(Boolean) as T
|
|
54
68
|
|
|
55
69
|
return val?.V ? this.decode<T>(val.V, schema) : val
|
|
@@ -59,7 +73,7 @@ export default class Compact {
|
|
|
59
73
|
if (!val || typeof val !== 'string') return val as T
|
|
60
74
|
|
|
61
75
|
return this.withSchema(this.unzip(JSON.parse(
|
|
62
|
-
this.#
|
|
76
|
+
this.#unpack(val)
|
|
63
77
|
.replace(/"/g, '~TSQ~')
|
|
64
78
|
.replace(/'/g, '"')
|
|
65
79
|
.replace(/~TSQ~/g, "'")
|
|
@@ -72,7 +86,7 @@ export default class Compact {
|
|
|
72
86
|
if (Array.isArray(obj))
|
|
73
87
|
return obj?.length ? obj.map(item => this.zip(item, schema, seen)) : []
|
|
74
88
|
|
|
75
|
-
if (
|
|
89
|
+
if (this.#cantZip(obj)) return obj
|
|
76
90
|
|
|
77
91
|
return schema.map(key => {
|
|
78
92
|
if (typeof key === 'string')
|
|
@@ -96,8 +110,7 @@ export default class Compact {
|
|
|
96
110
|
const type = typeof val
|
|
97
111
|
const length = getLength(val, type)
|
|
98
112
|
|
|
99
|
-
if (
|
|
100
|
-
return val
|
|
113
|
+
if (this.#cantZip(val, type, length)) return val
|
|
101
114
|
|
|
102
115
|
if (type == 'object') {
|
|
103
116
|
for (const key in val)
|
|
@@ -131,7 +144,7 @@ export default class Compact {
|
|
|
131
144
|
if (!key) return undefined
|
|
132
145
|
|
|
133
146
|
if (typeof key == 'string')
|
|
134
|
-
return [key, value
|
|
147
|
+
return [key, value === undefined ? null : value]
|
|
135
148
|
|
|
136
149
|
const mainKey = Object.keys(key)[0]
|
|
137
150
|
const subKeys = key[mainKey]
|
|
@@ -145,7 +158,7 @@ export default class Compact {
|
|
|
145
158
|
: [mainKey, this.withSchema(value, subKeys)]
|
|
146
159
|
}
|
|
147
160
|
|
|
148
|
-
return [mainKey, value
|
|
161
|
+
return [mainKey, value === undefined ? null : value]
|
|
149
162
|
}
|
|
150
163
|
|
|
151
164
|
static memo(val: any, seen: any[]): any {
|
|
@@ -161,8 +174,7 @@ export default class Compact {
|
|
|
161
174
|
}
|
|
162
175
|
|
|
163
176
|
const length = getLength(val, type)
|
|
164
|
-
if (
|
|
165
|
-
return val
|
|
177
|
+
if (this.#cantZip(val, type, length)) return val
|
|
166
178
|
|
|
167
179
|
const index = seen.indexOf(val)
|
|
168
180
|
if (index !== -1)
|
|
@@ -177,11 +189,17 @@ export default class Compact {
|
|
|
177
189
|
return new RegExp(`(?<![^\\s,\\[\\{:])(${keys.join('|')})(?![^\\s,\\]\\}:])`, 'g')
|
|
178
190
|
}
|
|
179
191
|
|
|
180
|
-
static #
|
|
192
|
+
static #pack(val: string): string {
|
|
181
193
|
return val.replace(this.#typeRegex, match => this.#typeMap[match])
|
|
182
194
|
}
|
|
183
195
|
|
|
184
|
-
static #
|
|
196
|
+
static #unpack(val: string): string {
|
|
185
197
|
return val.replace(this.#reverseTypeRegex, match => this.#reverseTypeMap[match])
|
|
186
198
|
}
|
|
199
|
+
|
|
200
|
+
static #cantZip(val: any, type: string = '', length: number = 0) {
|
|
201
|
+
if (!val || [null, true, false, 'true', 'false'].includes(val)) return true
|
|
202
|
+
|
|
203
|
+
return !type && !length ? false : type != 'object' && length < 2
|
|
204
|
+
}
|
|
187
205
|
}
|
package/src/dynamodb/schema.ts
CHANGED
|
@@ -2,21 +2,27 @@ 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)
|
|
5
|
+
export function isArraySchema(v: any): boolean {
|
|
6
6
|
return v[m] || false
|
|
7
7
|
}
|
|
8
8
|
|
|
9
|
+
export function arraySchema(v: any): any {
|
|
10
|
+
// @ts-ignore
|
|
11
|
+
v[m] = true
|
|
12
|
+
return v
|
|
13
|
+
}
|
|
14
|
+
|
|
9
15
|
export function extractZodKeys(schema: ZodTypeAny): SchemaStructure {
|
|
10
16
|
if (schema instanceof z.ZodObject) {
|
|
11
17
|
return Object.entries(schema.shape).map(([key, value]) => {
|
|
12
18
|
const inner = unwrap(value as ZodTypeAny)
|
|
13
19
|
|
|
14
20
|
if (inner instanceof z.ZodObject)
|
|
15
|
-
return
|
|
21
|
+
return notEmpty(key, extractZodKeys(inner))
|
|
16
22
|
|
|
17
23
|
if (inner instanceof z.ZodArray) {
|
|
18
24
|
const item = unwrap(inner._def.type as ZodTypeAny)
|
|
19
|
-
return item instanceof z.ZodObject ?
|
|
25
|
+
return item instanceof z.ZodObject ? notEmpty(key, extractZodKeys(item)) : key
|
|
20
26
|
}
|
|
21
27
|
|
|
22
28
|
return key
|
|
@@ -25,12 +31,8 @@ export function extractZodKeys(schema: ZodTypeAny): SchemaStructure {
|
|
|
25
31
|
|
|
26
32
|
if (schema instanceof z.ZodArray) {
|
|
27
33
|
const item = unwrap(schema._def.type as ZodTypeAny)
|
|
28
|
-
if (item instanceof z.ZodObject)
|
|
29
|
-
|
|
30
|
-
// @ts-ignore
|
|
31
|
-
r[m] = true
|
|
32
|
-
return r
|
|
33
|
-
}
|
|
34
|
+
if (item instanceof z.ZodObject)
|
|
35
|
+
return arraySchema(extractZodKeys(item))
|
|
34
36
|
|
|
35
37
|
return []
|
|
36
38
|
}
|
|
@@ -60,6 +62,10 @@ export function unwrap(schema: ZodTypeAny): ZodTypeAny {
|
|
|
60
62
|
return schema
|
|
61
63
|
}
|
|
62
64
|
|
|
65
|
+
function notEmpty(key: string, schema: SchemaStructure): string | Record<string, SchemaStructure> {
|
|
66
|
+
return schema?.length ? {[key]: schema} : key
|
|
67
|
+
}
|
|
68
|
+
|
|
63
69
|
export function Schema<
|
|
64
70
|
T extends ZodTypeAny,
|
|
65
71
|
B extends object
|