rajt 0.0.21 → 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
@@ -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.21",
4
+ "version": "0.0.23",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
7
7
  "exports": {
@@ -40,7 +40,7 @@
40
40
  "@hono/zod-validator": "^0.4.3",
41
41
  "@types/node": "^20.11.0",
42
42
  "chokidar-cli": "^3.0.0",
43
- "cripta": "^0.1.6",
43
+ "cripta": "0.1.8",
44
44
  "dotenv": "^16.5.0",
45
45
  "esbuild": "^0.25.2",
46
46
  "hono": "^4.7.6",
@@ -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
 
@@ -1,3 +1,4 @@
1
1
  export { Dynamodb } from './client'
2
2
  export { Model, Entity, Zip, PartitionKey, SortKey, Key, Keys } from './decorators'
3
- export { default as Schema } from './schema'
3
+ export { Schema } from './schema'
4
+ export { Repository } from './repository'
@@ -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
+ }
@@ -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
- const shape = schema.shape
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 default function Schema<T extends ZodTypeAny>(schema: T) {
37
- return class {
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 = undefined
40
- #PK?: string = undefined
41
- #SK?: string = undefined
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