rajt 0.0.46 → 0.0.48

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.46",
4
+ "version": "0.0.48",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
7
7
  "exports": {
package/src/auth/token.ts CHANGED
@@ -16,14 +16,14 @@ export class Token {
16
16
  const header = c.cx.req.header(this.#name) || c.cx.req.header('HTTP_AUTHORIZATION') || c.cx.req.header('REDIRECT_HTTP_AUTHORIZATION') || null
17
17
 
18
18
  if (header) {
19
- const position = header.toLowerCase().indexOf(this.#prefix.toLowerCase())
20
- if (position !== -1) {
21
- let token = header.slice(position + this.#prefix.length).trim()
22
- const commaPos = token.indexOf(',')
23
- if (commaPos !== -1) token = token.slice(0, commaPos).trim()
24
-
25
- return token
26
- }
19
+ const pos = header.toLowerCase().indexOf(this.#prefix.toLowerCase())
20
+ if (pos < 0) return header
21
+
22
+ let token = header.slice(pos + this.#prefix.length).trim()
23
+ const commaPos = token.indexOf(',')
24
+ if (commaPos > -1) token = token.slice(0, commaPos).trim()
25
+
26
+ return token
27
27
  }
28
28
 
29
29
  return null
@@ -1,6 +1,27 @@
1
1
  import { DynamoDBClient } from '@aws-sdk/client-dynamodb'
2
- import { DynamoDBDocumentClient } from '@aws-sdk/lib-dynamodb'
2
+ import {
3
+ DynamoDBDocumentClient,
4
+
5
+ BatchGetCommand,
6
+ BatchWriteCommand,
7
+
8
+ DeleteCommand,
9
+ GetCommand,
10
+ PutCommand,
11
+ QueryCommand,
12
+ ScanCommand,
13
+ UpdateCommand,
14
+
15
+ ScanCommandInput,
16
+ QueryCommandInput,
17
+ UpdateCommandInput,
18
+
19
+ BatchGetCommandInput,
20
+ BatchWriteCommandInput,
21
+ } from '@aws-sdk/lib-dynamodb'
22
+ import type { NativeAttributeValue } from '@aws-sdk/util-dynamodb'
3
23
  import AbstractModel from './model'
24
+ import { Keys } from './types'
4
25
 
5
26
  const client = new DynamoDBClient(process.env?.AWS_SAM_LOCAL ? {
6
27
  region: process.env.AWS_REGION || "us-east-1",
@@ -11,10 +32,74 @@ const client = new DynamoDBClient(process.env?.AWS_SAM_LOCAL ? {
11
32
  },
12
33
  } : {})
13
34
 
14
- export const ddb = DynamoDBDocumentClient.from(client)
35
+ export const DocumentClient = DynamoDBDocumentClient.from(client)
15
36
 
16
37
  export class Dynamodb {
17
38
  static model<T extends object>(cls: new (...args: any[]) => T) {
18
- return new AbstractModel<T>(cls, ddb)
39
+ return new AbstractModel<T>(cls)
40
+ }
41
+
42
+ static raw() {
43
+ return RawClient
44
+ }
45
+ }
46
+
47
+ export class RawClient {
48
+ static async get(TableName: string, key: Keys, sk?: string) {
49
+ return DocumentClient.send(new GetCommand({
50
+ TableName,
51
+ Key: this.#key(key, sk),
52
+ }))
53
+ }
54
+
55
+ static async scan(TableName: string, filters: ScanCommandInput) {
56
+ return DocumentClient.send(new ScanCommand({ ...filters, TableName }))
57
+ }
58
+
59
+ static async query(TableName: string, filters: QueryCommandInput) {
60
+ return DocumentClient.send(new QueryCommand({ ...filters, TableName }))
61
+ }
62
+
63
+ static async put(TableName: string, Item: Record<string, NativeAttributeValue>) {
64
+ return DocumentClient.send(new PutCommand({ TableName, Item }))
65
+ }
66
+
67
+ static async update(TableName: string, filters: UpdateCommandInput, key: Keys, sk?: string) {
68
+ return DocumentClient.send(new UpdateCommand({
69
+ ...filters, TableName, Key: this.#key(key, sk),
70
+ }))
71
+ }
72
+
73
+ static async delete(TableName: string, key: Keys, sk?: string) {
74
+ return DocumentClient.send(new DeleteCommand({ TableName, Key: this.#key(key, sk) }))
75
+ }
76
+
77
+ static async batchGet(batch: BatchGetCommandInput) {
78
+ return DocumentClient.send(new BatchGetCommand(batch))
79
+ }
80
+
81
+ static async batchWrite(batch: BatchWriteCommandInput) {
82
+ return DocumentClient.send(new BatchWriteCommand(batch))
83
+ }
84
+
85
+ static #key(key: Keys, sk?: string) {
86
+ if (typeof key == 'object' && key != null) return key
87
+
88
+ let pk: string
89
+ let skValue: string | undefined
90
+
91
+ if (Array.isArray(key)) {
92
+ pk = key[0]
93
+ skValue = key[1] ?? sk
94
+ } else {
95
+ pk = key
96
+ skValue = sk
97
+ }
98
+
99
+ const keys = {PK: pk}
100
+ // @ts-ignore
101
+ if (skValue) keys.SK = skValue
102
+
103
+ return keys
19
104
  }
20
105
  }
@@ -1,4 +1,4 @@
1
- export { Dynamodb } from './client'
1
+ export { Dynamodb, DocumentClient, RawClient } from './client'
2
2
  export { Model, Entity, Zip, PartitionKey, SortKey, Key, Keys } from './decorators'
3
3
  export { Schema } from './schema'
4
4
  export { Repository } from './repository'
@@ -1,18 +1,11 @@
1
1
  import {
2
- DynamoDBDocumentClient,
3
- BatchGetCommand,
4
- BatchWriteCommand,
5
- DeleteCommand,
6
- GetCommand,
7
- PutCommand,
8
- QueryCommand,
9
- ScanCommand,
10
- UpdateCommand,
2
+ UpdateCommandInput,
11
3
  } from '@aws-sdk/lib-dynamodb'
12
4
  import type { ModelMetadata, Keys, Model, Filter } from './types'
13
5
  import { getModelMetadata } from './decorators'
14
6
  import QueryBuilder from './query-builder'
15
7
  import Compact from './compact'
8
+ import { RawClient } from './client'
16
9
  import { isArraySchema } from './schema'
17
10
  import getLength from '../utils/lenght'
18
11
 
@@ -20,17 +13,14 @@ export default class AbstractModel<T extends object> {
20
13
  #meta: ModelMetadata
21
14
  cls?: Model<T>
22
15
  lastKey?: Record<string, any>
23
- #db: DynamoDBDocumentClient
24
16
  #queryBuilder?: QueryBuilder
25
17
  #model?: AbstractModel<T>
26
18
 
27
19
  constructor(
28
20
  cls: Model<T> | ModelMetadata,
29
- db: DynamoDBDocumentClient,
30
21
  queryBuilder?: QueryBuilder,
31
22
  model?: AbstractModel<T>
32
23
  ) {
33
- this.#db = db
34
24
  this.#queryBuilder = queryBuilder
35
25
  this.#model = model
36
26
 
@@ -70,34 +60,25 @@ export default class AbstractModel<T extends object> {
70
60
  where(builderFn: (q: QueryBuilder) => void) {
71
61
  const qb = new QueryBuilder()
72
62
  builderFn(qb)
73
- return new AbstractModel<T>(this.#meta, this.#db, qb, this)
63
+ return new AbstractModel<T>(this.#meta, qb, this)
74
64
  }
75
65
 
76
66
  async scan(filterFn?: Filter<T>) {
77
- const result = await this.#db.send(new ScanCommand({
78
- TableName: this.table,
79
- ...this.#queryBuilder?.filters,
80
- }))
67
+ const result = await RawClient.scan(this.table, this.#queryBuilder?.filters)
81
68
 
82
69
  this.lastEvaluatedKey = result.LastEvaluatedKey
83
70
  return this.#processItems(result.Items, filterFn)
84
71
  }
85
72
 
86
73
  async query(filterFn?: Filter<T>) {
87
- const result = await this.#db.send(new QueryCommand({
88
- TableName: this.table,
89
- ...this.#queryBuilder?.conditions,
90
- }))
74
+ const result = await RawClient.query(this.table, this.#queryBuilder?.conditions)
91
75
 
92
76
  this.lastEvaluatedKey = result.LastEvaluatedKey
93
77
  return this.#processItems(result.Items, filterFn)
94
78
  }
95
79
 
96
80
  async get(key: Keys, sk?: string) {
97
- const result = await this.#db.send(new GetCommand({
98
- TableName: this.table,
99
- Key: this.#key(key, sk),
100
- }))
81
+ const result = await RawClient.get(this.table, key, sk)
101
82
  return result.Item ? this.#processItem(result.Item) : undefined
102
83
  }
103
84
 
@@ -112,7 +93,7 @@ export default class AbstractModel<T extends object> {
112
93
  this.#validateKeys(item)
113
94
  }
114
95
 
115
- await this.#db.send(new PutCommand({ TableName: this.table, Item: item }))
96
+ await RawClient.put(this.table, item)
116
97
  return this.#processItem(item, keys)
117
98
  }
118
99
 
@@ -136,28 +117,23 @@ export default class AbstractModel<T extends object> {
136
117
  const UpdateExpression = 'SET ' + UpdateExpressionParts.join(', ')
137
118
  const ExpressionAttributeNames = Object.fromEntries(Object.keys(attrs).map(k => [`#${k}`, k]))
138
119
 
139
- await this.#db.send(new UpdateCommand({
140
- TableName: this.table,
141
- Key: this.#key(key),
120
+ await RawClient.update(this.table, {
142
121
  UpdateExpression,
143
122
  ExpressionAttributeValues,
144
123
  ExpressionAttributeNames,
145
- }))
124
+ } as UpdateCommandInput, key)
146
125
 
147
126
  return this.#processItem(attrs, keys)
148
127
  }
149
128
 
150
129
  async delete(key: Keys, sk?: string) {
151
- return this.#db.send(new DeleteCommand({
152
- TableName: this.table,
153
- Key: this.#key(key, sk),
154
- }))
130
+ return RawClient.delete(this.table, key, sk)
155
131
  }
156
132
 
157
133
  async batchGet(keys: Array<Keys>) {
158
- const result = await this.#db.send(new BatchGetCommand({
134
+ const result = await RawClient.batchGet({
159
135
  RequestItems: { [this.table]: { Keys: keys.map(key => this.#key(key)) } },
160
- }))
136
+ })
161
137
  return (result.Responses?.[this.table] as T[] || []).map(item => this.#processItem(item))
162
138
  }
163
139
 
@@ -171,7 +147,7 @@ export default class AbstractModel<T extends object> {
171
147
  return null
172
148
  }).filter(Boolean) as any[]
173
149
 
174
- return this.#db.send(new BatchWriteCommand({ RequestItems: { [this.table]: WriteRequests } }))
150
+ return RawClient.batchWrite({ RequestItems: { [this.table]: WriteRequests } })
175
151
  }
176
152
 
177
153
  async deleteMany(keys: Array<Keys>) {