@rudderjs/orm 0.0.6
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/LICENSE +21 -0
- package/README.md +604 -0
- package/boost/guidelines.md +234 -0
- package/dist/attribute.d.ts +36 -0
- package/dist/attribute.d.ts.map +1 -0
- package/dist/attribute.js +36 -0
- package/dist/attribute.js.map +1 -0
- package/dist/cast.d.ts +14 -0
- package/dist/cast.d.ts.map +1 -0
- package/dist/cast.js +85 -0
- package/dist/cast.js.map +1 -0
- package/dist/collection.d.ts +73 -0
- package/dist/collection.d.ts.map +1 -0
- package/dist/collection.js +152 -0
- package/dist/collection.js.map +1 -0
- package/dist/factory.d.ts +80 -0
- package/dist/factory.d.ts.map +1 -0
- package/dist/factory.js +129 -0
- package/dist/factory.js.map +1 -0
- package/dist/index.d.ts +195 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +450 -0
- package/dist/index.js.map +1 -0
- package/dist/resource.d.ts +95 -0
- package/dist/resource.d.ts.map +1 -0
- package/dist/resource.js +115 -0
- package/dist/resource.js.map +1 -0
- package/package.json +39 -0
package/dist/resource.js
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
// ─── JsonResource ───────────────────────────────────────────
|
|
2
|
+
/**
|
|
3
|
+
* Base class for API resource transformations.
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* class UserResource extends JsonResource<User> {
|
|
7
|
+
* toArray() {
|
|
8
|
+
* return {
|
|
9
|
+
* id: this.resource.id,
|
|
10
|
+
* name: this.resource.name,
|
|
11
|
+
* email: this.resource.email,
|
|
12
|
+
* // conditional
|
|
13
|
+
* admin: this.when(this.resource.role === 'admin', true),
|
|
14
|
+
* posts: this.whenLoaded('posts', PostResource.collection(this.resource.posts as Post[])),
|
|
15
|
+
* }
|
|
16
|
+
* }
|
|
17
|
+
* }
|
|
18
|
+
*
|
|
19
|
+
* // In a route handler:
|
|
20
|
+
* res.json(new UserResource(user).toArray())
|
|
21
|
+
* res.json(UserResource.collection(users).toResponse())
|
|
22
|
+
*/
|
|
23
|
+
export class JsonResource {
|
|
24
|
+
resource;
|
|
25
|
+
constructor(resource) {
|
|
26
|
+
this.resource = resource;
|
|
27
|
+
}
|
|
28
|
+
when(condition, value, fallback) {
|
|
29
|
+
return condition ? value : fallback;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Include `then` only when `value` is not null/undefined.
|
|
33
|
+
* If `then` is a function it receives the non-null value.
|
|
34
|
+
*/
|
|
35
|
+
whenNotNull(value, then, fallback) {
|
|
36
|
+
if (value !== null && value !== undefined) {
|
|
37
|
+
return typeof then === 'function'
|
|
38
|
+
? then(value)
|
|
39
|
+
: then;
|
|
40
|
+
}
|
|
41
|
+
return fallback;
|
|
42
|
+
}
|
|
43
|
+
whenLoaded(relation, value, fallback) {
|
|
44
|
+
const res = this.resource;
|
|
45
|
+
if (relation in res && res[relation] !== undefined) {
|
|
46
|
+
return value !== undefined ? value : res[relation];
|
|
47
|
+
}
|
|
48
|
+
return fallback;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Merge `attributes` into the output only when `condition` is true.
|
|
52
|
+
* Returns `{}` when false — spread the result at the call site.
|
|
53
|
+
*
|
|
54
|
+
* @example
|
|
55
|
+
* toArray() {
|
|
56
|
+
* return {
|
|
57
|
+
* id: this.resource.id,
|
|
58
|
+
* ...this.mergeWhen(this.resource.isAdmin, {
|
|
59
|
+
* permissions: this.resource.permissions,
|
|
60
|
+
* lastLogin: this.resource.lastLogin,
|
|
61
|
+
* }),
|
|
62
|
+
* }
|
|
63
|
+
* }
|
|
64
|
+
*/
|
|
65
|
+
mergeWhen(condition, attributes) {
|
|
66
|
+
return condition ? attributes : {};
|
|
67
|
+
}
|
|
68
|
+
/** Create a `ResourceCollection` from an array of raw items using this resource class. */
|
|
69
|
+
static collection(items, meta) {
|
|
70
|
+
return new ResourceCollection(items.map(item => new this(item)), meta);
|
|
71
|
+
}
|
|
72
|
+
toJSON() {
|
|
73
|
+
const result = this.toArray();
|
|
74
|
+
if (result instanceof Promise) {
|
|
75
|
+
throw new Error('[RudderJS] JsonResource.toJSON() does not support async toArray(). Use toArray() directly.');
|
|
76
|
+
}
|
|
77
|
+
return result;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
// ─── ResourceCollection ─────────────────────────────────────
|
|
81
|
+
/**
|
|
82
|
+
* Wraps multiple `JsonResource` instances for collection responses.
|
|
83
|
+
*
|
|
84
|
+
* @example
|
|
85
|
+
* class UserResource extends JsonResource<User> { ... }
|
|
86
|
+
*
|
|
87
|
+
* // From a route handler:
|
|
88
|
+
* const collection = UserResource.collection(users)
|
|
89
|
+
* res.json(await collection.toResponse())
|
|
90
|
+
* // → { data: [...] }
|
|
91
|
+
*
|
|
92
|
+
* // With pagination metadata:
|
|
93
|
+
* const collection = UserResource.collection(users, { total: 100, page: 1, perPage: 15 })
|
|
94
|
+
* res.json(await collection.toResponse())
|
|
95
|
+
* // → { data: [...], meta: { total: 100, page: 1, perPage: 15 } }
|
|
96
|
+
*/
|
|
97
|
+
export class ResourceCollection {
|
|
98
|
+
items;
|
|
99
|
+
meta;
|
|
100
|
+
constructor(items, meta) {
|
|
101
|
+
this.items = items;
|
|
102
|
+
this.meta = meta;
|
|
103
|
+
}
|
|
104
|
+
static of(items, meta) {
|
|
105
|
+
return new ResourceCollection(items, meta);
|
|
106
|
+
}
|
|
107
|
+
async toArray(req) {
|
|
108
|
+
return Promise.all(this.items.map(item => item.toArray(req)));
|
|
109
|
+
}
|
|
110
|
+
async toResponse(req) {
|
|
111
|
+
const data = await this.toArray(req);
|
|
112
|
+
return this.meta !== undefined ? { data, meta: this.meta } : { data };
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
//# sourceMappingURL=resource.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"resource.js","sourceRoot":"","sources":["../src/resource.ts"],"names":[],"mappings":"AAAA,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
|
+
"url": "https://github.com/rudderjs/rudder",
|
|
8
|
+
"directory": "packages/orm"
|
|
9
|
+
},
|
|
10
|
+
"type": "module",
|
|
11
|
+
"files": [
|
|
12
|
+
"dist",
|
|
13
|
+
"boost"
|
|
14
|
+
],
|
|
15
|
+
"main": "./dist/index.js",
|
|
16
|
+
"types": "./dist/index.d.ts",
|
|
17
|
+
"exports": {
|
|
18
|
+
".": {
|
|
19
|
+
"import": "./dist/index.js",
|
|
20
|
+
"types": "./dist/index.d.ts"
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"@rudderjs/contracts": "0.0.3"
|
|
25
|
+
},
|
|
26
|
+
"devDependencies": {
|
|
27
|
+
"@types/node": "^20.0.0",
|
|
28
|
+
"typescript": "^5.4.0"
|
|
29
|
+
},
|
|
30
|
+
"author": "Suleiman Shahbari",
|
|
31
|
+
"scripts": {
|
|
32
|
+
"build": "tsc -p tsconfig.build.json",
|
|
33
|
+
"dev": "tsc -p tsconfig.build.json --watch",
|
|
34
|
+
"typecheck": "tsc --noEmit",
|
|
35
|
+
"lint": "eslint src",
|
|
36
|
+
"test": "tsc -p tsconfig.test.json && node --test dist-test/index.test.js; EXIT=$?; rm -rf dist-test; exit $EXIT",
|
|
37
|
+
"clean": "rm -rf dist"
|
|
38
|
+
}
|
|
39
|
+
}
|