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