rajt 0.0.13 → 0.0.15

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/README.md CHANGED
@@ -7,7 +7,7 @@
7
7
  - ⚡️ Fast Cold Start
8
8
  - 📦 Optimized Build
9
9
 
10
- [Read the Docs to Learn More](https://github.com/attla/rajt/blob/main/DOCS.md).
10
+ [Read the Docs to Learn More](https://github.com/attla/rajt/blob/main/DOCS.md)
11
11
 
12
12
  ## Packages
13
13
 
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.13",
4
+ "version": "0.0.15",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
7
7
  "exports": {
@@ -45,6 +45,7 @@
45
45
  "dotenv": "^16.5.0",
46
46
  "esbuild": "^0.25.2",
47
47
  "hono": "^4.7.6",
48
+ "plur": "^5.1.0",
48
49
  "tsconfig-paths": "^4.2.0",
49
50
  "tsx": "^4.19.3",
50
51
  "typescript": "^5.8.3"
@@ -1,20 +1,137 @@
1
- import { registerModelMetadata, registerKeyMetadata } from './metadata-registry'
1
+ import plur from 'plur'
2
2
 
3
- export function Model(opt: string | { table: string }) {
4
- return function (target: Function) {
5
- const table = typeof opt === 'string' ? opt : opt.table
6
- registerModelMetadata(target, { table })
3
+ export type ModelMetadata = {
4
+ table: string,
5
+ keys?: Record<'PK' | 'SK', string>,
6
+ zip: boolean,
7
+ }
8
+
9
+ export type ModelOpts = string | {
10
+ table?: string,
11
+ partitionKey?: string,
12
+ sortKey?: string,
13
+ zip?: boolean,
14
+ }
15
+
16
+ export function getModelMetadata(target: Function | any): ModelMetadata {
17
+ if (!target?.m)
18
+ throw Error(`Entity "${target?.name}" not registred, Use @Entity or @Model.`)
19
+
20
+ const typeKeys = typeof target.m[1]
21
+ return {
22
+ table: target.m[0],
23
+ // @ts-ignore
24
+ keys: typeKeys !== 'undefined' ? (typeKeys === 'string' ? { PK: target.m[1] } : { PK: target.m[1][0], SK: target.m[1][1] }) : undefined,
25
+ zip: target.m[2] || false,
26
+ fields: target.m[3] || [],
27
+ }
28
+ }
29
+
30
+ function _table(target: Function | any, opt?: ModelOpts) {
31
+ if (!target?.m) target.m = []
32
+ const table = opt ? (typeof opt === 'string' ? opt : opt?.table) : undefined
33
+
34
+ target.m[0] = table || plur(target.name.toLocaleUpperCase())
35
+ }
36
+
37
+ function _zip(target: Function | any) {
38
+ if (!target?.m) target.m = []
39
+ target.m[2] = true
40
+ target.m[3] = Object.keys(new target)
41
+ }
42
+
43
+ function _key(target: Function | any, pk: string, sk?: string) {
44
+ if (!target?.m) target.m = []
45
+ target.m[1] = pk && sk? [pk, sk] : [pk]
46
+ }
47
+
48
+ function _model(target: any, opt?: ModelOpts) {
49
+ _table(target, opt)
50
+ const notStr = typeof opt !== 'string'
51
+
52
+ if (!opt || notStr && opt?.zip)
53
+ _zip(target)
54
+
55
+ const pk = opt && notStr ? opt?.partitionKey : undefined
56
+ const sk = opt && notStr ? opt?.sortKey : undefined
57
+ _key(target, pk || 'PK', sk || 'SK')
58
+ }
59
+
60
+ function _pk(target: any, prop: string) {
61
+ if (!target?.m) target.m = []
62
+ if (['string', 'undefined'].includes(typeof target.m[1])) {
63
+ target.m[1] = prop
64
+ } else {
65
+ target.m[1][0] = prop
7
66
  }
8
67
  }
9
68
 
10
- export function PartitionKey(attrName?: string) {
11
- return function (target: any, propertyKey: string) {
12
- registerKeyMetadata(target.constructor, 'PK', attrName || propertyKey)
69
+ function _sk(target: any, prop: string) {
70
+ if (!target?.m) target.m = []
71
+ if (['string', 'undefined'].includes(typeof target.m[1])) {
72
+ target.m[1] = []
73
+ target.m[1][1] = prop
74
+ } else {
75
+ target.m[1][0] = prop
13
76
  }
14
77
  }
15
78
 
16
- export function SortKey(attrName?: string) {
17
- return function (target: any, propertyKey: string) {
18
- registerKeyMetadata(target.constructor, 'SK', attrName || propertyKey)
79
+ export function Entity(target: Function): void
80
+ export function Entity(opt?: ModelOpts): ClassDecorator
81
+ export function Entity(...args: any[]): void | ClassDecorator {
82
+ if (args.length === 1 && typeof args[0] === 'function')
83
+ return _table(args[0])
84
+
85
+ return (target: any) => _table(target, ...args)
86
+ }
87
+
88
+ export function Model(target: Function): void
89
+ export function Model(opt?: ModelOpts): ClassDecorator
90
+ export function Model(...args: any[]): void | ClassDecorator {
91
+ if (args.length === 1 && typeof args[0] === 'function')
92
+ return _model(args[0])
93
+
94
+ return (target: any) => _model(target, ...args)
95
+ }
96
+
97
+ export function Zip(target: Function): void
98
+ export function Zip(): ClassDecorator
99
+ export function Zip(...args: any[]): void | ClassDecorator {
100
+ if (args.length === 1 && typeof args[0] === 'function')
101
+ return _zip(args[0])
102
+
103
+ return (target: any) => _zip(target)
104
+ }
105
+
106
+ export function Key(pk: string, sk?: string) {
107
+ return (target: any) => {
108
+ _key(target, pk, sk)
19
109
  }
20
110
  }
111
+ export const Keys = Key
112
+
113
+ export function PartitionKey(attrName: string): PropertyDecorator
114
+ export function PartitionKey(target: any, propertyKey: string): void
115
+ export function PartitionKey(target: any, propertyKey: string | undefined, parameterIndex: number): void
116
+ export function PartitionKey(...args: any[]): void | PropertyDecorator {
117
+ if (!args.length) return
118
+
119
+ if (typeof args[0] === 'function' && typeof args[1] === 'string' && args[1])
120
+ return _pk(args[0], args[1])
121
+
122
+ if (args.length === 1 && args[0])
123
+ return (target: any) => _pk(target, args[0])
124
+ }
125
+
126
+ export function SortKey(attrName: string): PropertyDecorator
127
+ export function SortKey(target: any, propertyKey: string): void
128
+ export function SortKey(target: any, propertyKey: string | undefined, parameterIndex: number): void
129
+ export function SortKey(...args: any[]): void | PropertyDecorator {
130
+ if (!args.length) return
131
+
132
+ if (typeof args[0] === 'function' && typeof args[1] === 'string' && args[1])
133
+ return _sk(args[0], args[1])
134
+
135
+ if (args.length === 1 && args[0])
136
+ return (target: any) => _sk(target, args[0])
137
+ }
@@ -1,2 +1,3 @@
1
1
  export { Dynamodb } from './client'
2
- export { Model, PartitionKey, SortKey } from './decorators'
2
+ export { Model, Entity, Zip, PartitionKey, SortKey, Key, Keys } from './decorators'
3
+ export { default as Schema } from './schema'
@@ -1,6 +1,6 @@
1
1
  import type { AwsLiteDynamoDB } from '@aws-lite/dynamodb-types'
2
- import { getModelMetadata } from './metadata-registry'
3
- import type { ModelMetadata } from './metadata-registry'
2
+ import { getModelMetadata } from './decorators'
3
+ import type { ModelMetadata } from './decorators'
4
4
  import QueryBuilder from './query-builder'
5
5
 
6
6
  export default class AbstractModel<T extends object> {
@@ -46,6 +46,8 @@ export default class AbstractModel<T extends object> {
46
46
  }
47
47
 
48
48
  schema(pk: string, sk?: string) {
49
+ if (!this.meta.keys) return {}
50
+
49
51
  const keys = { [this.meta.keys.PK]: pk }
50
52
  if (sk && this.meta.keys.SK)
51
53
  keys[this.meta.keys.SK] = sk
@@ -0,0 +1,50 @@
1
+ import { z, ZodTypeAny } from 'zod'
2
+
3
+ function extractZodKeys(schema: ZodTypeAny): any {
4
+ if (schema instanceof z.ZodObject) {
5
+ const shape = schema.shape
6
+ return Object.entries(shape).map(([key, value]) => {
7
+ const inner = unwrap(value as ZodTypeAny)
8
+
9
+ if (inner instanceof z.ZodObject) {
10
+ return { [key]: extractZodKeys(inner) }
11
+ }
12
+
13
+ if (inner instanceof z.ZodArray) {
14
+ const item = unwrap(inner._def.type as ZodTypeAny)
15
+ if (item instanceof z.ZodObject)
16
+ return { [key]: extractZodKeys(item) }
17
+
18
+ return key
19
+ }
20
+
21
+ return key
22
+ })
23
+ }
24
+
25
+ return []
26
+ }
27
+
28
+ function unwrap(schema: ZodTypeAny): ZodTypeAny {
29
+ if (schema instanceof z.ZodOptional || schema instanceof z.ZodNullable)
30
+ return unwrap(schema._def.innerType)
31
+
32
+ if (schema instanceof z.ZodUnion)
33
+ return unwrap(schema._def.options[0] as ZodTypeAny)
34
+
35
+ return schema
36
+ }
37
+
38
+ export default function Schema<T extends ZodTypeAny>(schema: T) {
39
+ return class {
40
+ static _schema = schema
41
+
42
+ static getSchema() {
43
+ return extractZodKeys(schema)
44
+ }
45
+
46
+ constructor(data: z.infer<T>) {
47
+ Object.assign(this, data)
48
+ }
49
+ }
50
+ }
@@ -1,28 +0,0 @@
1
- export type ModelMetadata = {
2
- table: string,
3
- keys: Record<'PK' | 'SK', string>,
4
- }
5
-
6
- const modelRegistry = new Map<Function, ModelMetadata>()
7
-
8
- export function registerModelMetadata(target: Function, metadata: { table: string }) {
9
- const data = modelRegistry.get(target)
10
- if (data)
11
- data.table = metadata.table
12
- else
13
- // @ts-ignore
14
- modelRegistry.set(target, { table: metadata.table, keys: {} })
15
- }
16
-
17
- export function registerKeyMetadata(target: Function, keyType: 'PK' | 'SK', name: string) {
18
- const data = modelRegistry.get(target)
19
- if (data)
20
- data.keys[keyType] = name
21
- else
22
- // @ts-ignore
23
- modelRegistry.set(target, { table: '', keys: { [keyType]: name } })
24
- }
25
-
26
- export function getModelMetadata(target: Function) {
27
- return modelRegistry.get(target)
28
- }