rajt 0.0.48 → 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 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.48",
4
+ "version": "0.0.50",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
7
7
  "exports": {
@@ -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",
@@ -45,18 +45,18 @@ export class Dynamodb {
45
45
  }
46
46
 
47
47
  export class RawClient {
48
- static async get(TableName: string, key: Keys, sk?: string) {
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.#key(key, sk),
51
+ Key: this.key(key, sk),
52
52
  }))
53
53
  }
54
54
 
55
- static async scan(TableName: string, filters: ScanCommandInput) {
55
+ static async scan(TableName: string, filters: Omit<ScanCommandInput, 'TableName'>) {
56
56
  return DocumentClient.send(new ScanCommand({ ...filters, TableName }))
57
57
  }
58
58
 
59
- static async query(TableName: string, filters: QueryCommandInput) {
59
+ static async query(TableName: string, filters: Omit<QueryCommandInput, 'TableName'>) {
60
60
  return DocumentClient.send(new QueryCommand({ ...filters, TableName }))
61
61
  }
62
62
 
@@ -64,14 +64,19 @@ export class RawClient {
64
64
  return DocumentClient.send(new PutCommand({ TableName, Item }))
65
65
  }
66
66
 
67
- static async update(TableName: string, filters: UpdateCommandInput, key: Keys, sk?: string) {
67
+ static async update(
68
+ TableName: string,
69
+ filters: Omit<UpdateCommandInput, 'TableName' | 'Key'>,
70
+ key: Keys | Record<string, string>,
71
+ sk?: string
72
+ ) {
68
73
  return DocumentClient.send(new UpdateCommand({
69
- ...filters, TableName, Key: this.#key(key, sk),
74
+ ...filters, TableName, Key: this.key(key, sk),
70
75
  }))
71
76
  }
72
77
 
73
- static async delete(TableName: string, key: Keys, sk?: string) {
74
- return DocumentClient.send(new DeleteCommand({ TableName, Key: this.#key(key, sk) }))
78
+ static async delete(TableName: string, key: Keys | Record<string, string>, sk?: string) {
79
+ return DocumentClient.send(new DeleteCommand({ TableName, Key: this.key(key, sk) }))
75
80
  }
76
81
 
77
82
  static async batchGet(batch: BatchGetCommandInput) {
@@ -82,23 +87,41 @@ export class RawClient {
82
87
  return DocumentClient.send(new BatchWriteCommand(batch))
83
88
  }
84
89
 
85
- static #key(key: Keys, sk?: string) {
86
- if (typeof key == 'object' && key != null) return key
87
-
90
+ static key(
91
+ key: Keys | Record<string, string>,
92
+ sk?: string,
93
+ schema?: KeySchema,
94
+ defaultSK?: string,
95
+ ) {
88
96
  let pk: string
89
97
  let skValue: string | undefined
90
98
 
91
99
  if (Array.isArray(key)) {
92
100
  pk = key[0]
93
101
  skValue = key[1] ?? sk
102
+ } else if (typeof key == 'object' && key != null) {
103
+ return key
94
104
  } else {
95
105
  pk = key
96
106
  skValue = sk
97
107
  }
98
108
 
99
- const keys = {PK: pk}
100
- // @ts-ignore
101
- if (skValue) keys.SK = skValue
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
+ }
102
125
 
103
126
  return keys
104
127
  }
@@ -1,6 +1,3 @@
1
- import {
2
- UpdateCommandInput,
3
- } from '@aws-sdk/lib-dynamodb'
4
1
  import type { ModelMetadata, Keys, Model, Filter } from './types'
5
2
  import { getModelMetadata } from './decorators'
6
3
  import QueryBuilder from './query-builder'
@@ -78,7 +75,7 @@ export default class AbstractModel<T extends object> {
78
75
  }
79
76
 
80
77
  async get(key: Keys, sk?: string) {
81
- const result = await RawClient.get(this.table, key, sk)
78
+ const result = await RawClient.get(this.table, this.#key(key, sk))
82
79
  return result.Item ? this.#processItem(result.Item) : undefined
83
80
  }
84
81
 
@@ -121,13 +118,13 @@ export default class AbstractModel<T extends object> {
121
118
  UpdateExpression,
122
119
  ExpressionAttributeValues,
123
120
  ExpressionAttributeNames,
124
- } as UpdateCommandInput, key)
121
+ }, this.#key(key))
125
122
 
126
123
  return this.#processItem(attrs, keys)
127
124
  }
128
125
 
129
126
  async delete(key: Keys, sk?: string) {
130
- return RawClient.delete(this.table, key, sk)
127
+ return RawClient.delete(this.table, this.#key(key, sk))
131
128
  }
132
129
 
133
130
  async batchGet(keys: Array<Keys>) {
@@ -160,28 +157,7 @@ export default class AbstractModel<T extends object> {
160
157
 
161
158
  #key(key: Keys, sk?: string) {
162
159
  if (!this.#meta.keys) return {}
163
-
164
- let pk: string
165
- let skValue: string | undefined
166
- if (Array.isArray(key)) {
167
- pk = key[0]
168
- skValue = key[1] ?? sk
169
- } else {
170
- pk = key
171
- skValue = sk
172
- }
173
-
174
- const keys = { [this.#meta.keys.PK]: pk }
175
-
176
- if (this.#meta.keys?.SK) {
177
- if (skValue) {
178
- keys[this.#meta.keys.SK] = skValue
179
- } else if (this.#meta.defaultSK) {
180
- keys[this.#meta.keys.SK] = this.#meta.defaultSK
181
- }
182
- }
183
-
184
- return keys
160
+ return RawClient.key(key, sk, this.#meta.keys, this.#meta.defaultSK)
185
161
  }
186
162
 
187
163
  #getItemKey(item: Partial<T>, key?: Keys): Record<string, string> {