rajt 0.0.22 → 0.0.23
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
|
@@ -34,7 +34,7 @@ function _key(target: Function | any, pk: string, sk?: string) {
|
|
|
34
34
|
target.m[1] = pk && sk ? [pk, sk] : [pk]
|
|
35
35
|
}
|
|
36
36
|
|
|
37
|
-
function _model(target: any, opt?: ModelOpts) {
|
|
37
|
+
export function _model(target: any, opt?: ModelOpts) {
|
|
38
38
|
_table(target, opt)
|
|
39
39
|
const notStr = typeof opt !== 'string'
|
|
40
40
|
|
package/src/dynamodb/index.ts
CHANGED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { ZodTypeAny } from 'zod'
|
|
2
|
+
import { Dynamodb } from './client'
|
|
3
|
+
import { Schema } from './schema'
|
|
4
|
+
import { _model } from './decorators'
|
|
5
|
+
import type { ModelOpts } from './types'
|
|
6
|
+
|
|
7
|
+
export function Repository<M extends object, S extends ZodTypeAny>(
|
|
8
|
+
schema: S,
|
|
9
|
+
model: new (...args: any[]) => M,
|
|
10
|
+
opts?: ModelOpts
|
|
11
|
+
) {
|
|
12
|
+
const BaseSchemaClass = Schema(schema, model)
|
|
13
|
+
_model(BaseSchemaClass, opts)
|
|
14
|
+
|
|
15
|
+
return class extends BaseSchemaClass {
|
|
16
|
+
static model = Dynamodb.model<M>(BaseSchemaClass as unknown as new (...args: any[]) => M)
|
|
17
|
+
} as unknown as (typeof BaseSchemaClass) & {
|
|
18
|
+
model: ReturnType<typeof Dynamodb.model<M>>
|
|
19
|
+
new (...args: any[]): InstanceType<typeof BaseSchemaClass>
|
|
20
|
+
}
|
|
21
|
+
}
|
package/src/dynamodb/schema.ts
CHANGED
|
@@ -3,9 +3,7 @@ import type { SchemaStructure } from './types'
|
|
|
3
3
|
|
|
4
4
|
function extractZodKeys(schema: ZodTypeAny): SchemaStructure {
|
|
5
5
|
if (schema instanceof z.ZodObject) {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
return Object.entries(shape).map(([key, value]) => {
|
|
6
|
+
return Object.entries(schema.shape).map(([key, value]) => {
|
|
9
7
|
const inner = unwrap(value as ZodTypeAny)
|
|
10
8
|
|
|
11
9
|
if (inner instanceof z.ZodObject)
|
|
@@ -33,14 +31,24 @@ function unwrap(schema: ZodTypeAny): ZodTypeAny {
|
|
|
33
31
|
return schema
|
|
34
32
|
}
|
|
35
33
|
|
|
36
|
-
export
|
|
37
|
-
|
|
34
|
+
export function Schema<
|
|
35
|
+
T extends ZodTypeAny,
|
|
36
|
+
B extends object
|
|
37
|
+
>(
|
|
38
|
+
schema: T,
|
|
39
|
+
BaseClass?: new (...args: any[]) => B
|
|
40
|
+
) {
|
|
41
|
+
const Base = (BaseClass || class {})
|
|
42
|
+
|
|
43
|
+
return class extends Base {
|
|
38
44
|
static _schema = schema
|
|
39
|
-
static defaultSortKey?: string
|
|
40
|
-
|
|
41
|
-
#
|
|
45
|
+
static defaultSortKey?: string
|
|
46
|
+
|
|
47
|
+
#PK?: string
|
|
48
|
+
#SK?: string
|
|
42
49
|
|
|
43
50
|
constructor(data: z.infer<T>) {
|
|
51
|
+
super()
|
|
44
52
|
Object.assign(this, data)
|
|
45
53
|
}
|
|
46
54
|
|