rajt 0.0.50 → 0.0.52

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "rajt",
3
3
  "description": "A serverless bundler layer, fully typed for AWS Lambda (Node.js and LLRT) and Cloudflare Workers.",
4
- "version": "0.0.50",
4
+ "version": "0.0.52",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
7
7
  "exports": {
@@ -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 || "us-east-1",
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 || "DUMMYIDEXAMPLE",
31
- secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY || "DUMMYEXAMPLEKEY",
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'>) {
@@ -29,14 +29,28 @@ export default class Compact {
29
29
  }
30
30
 
31
31
  static {
32
- this.#reverseTypeMap = Object.fromEntries(Object.entries(this.#typeMap).map(([k, v]) => [v, k.replace(/"/g, "'")]))
33
- this.#typeRegex = this.#mapRegex(Object.keys(this.#typeMap))
34
- this.#reverseTypeRegex = this.#mapRegex(Object.keys(this.#reverseTypeMap))
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
- return this.#minify(
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.#deminify(val)
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 (!obj || [null, true, false].includes(obj)) return obj
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 ([null, true, false].includes(val) || type != 'object' && length < 2)
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 || null]
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 || null]
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 ([null, true, false].includes(val) || type != 'object' && length < 2)
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 #minify(val: string): string {
192
+ static #pack(val: string): string {
181
193
  return val.replace(this.#typeRegex, match => this.#typeMap[match])
182
194
  }
183
195
 
184
- static #deminify(val: string): string {
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
  }
@@ -20,6 +20,10 @@ export function Repository<
20
20
 
21
21
  return class extends Repo {
22
22
  static model = Dynamodb.model<M>(Repo as any)
23
+
24
+ static get lastKey() {
25
+ return this.model?.lastEvaluatedKey || null
26
+ }
23
27
  } as unknown as (typeof Repo) & {
24
28
  new (...args: any[]): InstanceType<typeof Repo>
25
29
  model: ReturnType<typeof Dynamodb.model<M>>
@@ -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) : boolean {
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 { [key]: extractZodKeys(inner) }
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 ? { [key]: extractZodKeys(item) } : key
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
- const r = extractZodKeys(item)
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