@itrocks/lazy-loading 0.0.1

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 ADDED
@@ -0,0 +1,14 @@
1
+ ISC License
2
+ Copyright (C) 2024, Baptiste Pillot <baptiste@pillot.fr>
3
+
4
+ Permission to use, copy, modify, and/or distribute this software for any
5
+ purpose with or without fee is hereby granted, provided that the above
6
+ copyright notice and this permission notice appear in all copies.
7
+
8
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9
+ WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10
+ MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11
+ ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12
+ WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
13
+ OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
14
+ CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,7 @@
1
+ [![npm version](https://img.shields.io/npm/v/@itrocks/lazy-loading?logo=npm)](https://www.npmjs.org/package/@itrocks/lazy-loading)
2
+ [![npm downloads](https://img.shields.io/npm/dm/@itrocks/lazy-loading)](https://www.npmjs.org/package/@itrocks/lazy-loading)
3
+ [![GitHub](https://img.shields.io/github/last-commit/itrocks-ts/lazy-loading?color=2dba4e&label=commit&logo=github)](https://github.com/itrocks-ts/lazy-loading)
4
+ [![issues](https://img.shields.io/github/issues/itrocks-ts/lazy-loading)](https://github.com/itrocks-ts/lazy-loading/issues)
5
+ [![discord](https://img.shields.io/discord/1314141024020467782?color=7289da&label=discord&logo=discord&logoColor=white)](https://25.re/ditr)
6
+
7
+ # lazy-loading
@@ -0,0 +1,6 @@
1
+ import { Type } from '@itrocks/class-type';
2
+ export declare const PROTECT_GET: unique symbol;
3
+ export type PropertyDescriptorWithProtectGet = PropertyDescriptor & ThisType<any> & {
4
+ [PROTECT_GET]?: true;
5
+ };
6
+ export declare function initClass<T extends object>(classType: Type<T>): Type<T> | undefined;
@@ -0,0 +1,99 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.PROTECT_GET = void 0;
4
+ exports.initClass = initClass;
5
+ const class_type_1 = require("@itrocks/class-type");
6
+ const property_type_1 = require("@itrocks/property-type");
7
+ const reflect_1 = require("@itrocks/reflect");
8
+ const storage_1 = require("@itrocks/storage");
9
+ const store_1 = require("@itrocks/store");
10
+ exports.PROTECT_GET = Symbol('protectGet');
11
+ function defineCollectionProperty(type, property, builtClass) {
12
+ const descriptor = {
13
+ configurable: true,
14
+ enumerable: true,
15
+ async get() {
16
+ const elementType = type.elementType;
17
+ const ids = this[property + '_ids'];
18
+ return this[property] = ids
19
+ ? await (0, storage_1.dataSource)().readMultiple(elementType, ids)
20
+ : await (0, storage_1.dataSource)().readCollection(this, property, elementType);
21
+ },
22
+ set(value) {
23
+ delete this[property + '_ids'];
24
+ Object.defineProperty(this, property, { configurable: true, enumerable: true, value, writable: true });
25
+ Reflect.deleteMetadata(exports.PROTECT_GET, builtClass.prototype, property);
26
+ }
27
+ };
28
+ Object.defineProperty(builtClass.prototype, property, descriptor);
29
+ Reflect.defineMetadata(exports.PROTECT_GET, true, builtClass.prototype, property);
30
+ return property;
31
+ }
32
+ function defineObjectProperty(type, property, builtClass) {
33
+ const descriptor = {
34
+ configurable: true,
35
+ enumerable: true,
36
+ async get() {
37
+ const id = this[property + '_id'];
38
+ return this[property] = id ? await (0, storage_1.dataSource)().read(type, id) : undefined;
39
+ },
40
+ set(value) {
41
+ delete this[property + '_id'];
42
+ Object.defineProperty(this, property, { configurable: true, enumerable: true, value, writable: true });
43
+ Reflect.deleteMetadata(exports.PROTECT_GET, builtClass.prototype, property);
44
+ }
45
+ };
46
+ Object.defineProperty(builtClass.prototype, property, descriptor);
47
+ Reflect.defineMetadata(exports.PROTECT_GET, true, builtClass.prototype, property);
48
+ return property;
49
+ }
50
+ function initClass(classType) {
51
+ try {
52
+ if (!(0, store_1.storeOf)(classType))
53
+ return;
54
+ }
55
+ catch {
56
+ return;
57
+ }
58
+ const properties = [];
59
+ // @ts-ignore TS2415 classType is always a heritable class, not a function.
60
+ const BuiltClass = (() => class extends classType {
61
+ constructor(...args) {
62
+ super(...args);
63
+ for (const property of properties) {
64
+ delete this[property];
65
+ }
66
+ }
67
+ })();
68
+ for (const property of Object.values(new reflect_1.ReflectClass(classType).properties)) {
69
+ const type = property.type;
70
+ if (!type)
71
+ continue;
72
+ if ((type instanceof property_type_1.CollectionType) && (0, class_type_1.isAnyType)(type.elementType)) {
73
+ properties.push(defineCollectionProperty(type, property.name, BuiltClass));
74
+ }
75
+ else if ((0, class_type_1.isAnyType)(type)) {
76
+ properties.push(defineObjectProperty(type, property.name, BuiltClass));
77
+ }
78
+ }
79
+ return properties.length ? BuiltClass : undefined;
80
+ }
81
+ const Module = require('module');
82
+ const superRequire = Module.prototype.require;
83
+ Module.prototype.require = function () {
84
+ const original = superRequire.call(this, ...arguments);
85
+ let module;
86
+ for (const [name, type] of Object.entries(original)) {
87
+ if (!(0, class_type_1.isAnyType)(type))
88
+ continue;
89
+ const withORM = initClass(type);
90
+ if (!withORM)
91
+ continue;
92
+ if (!module) {
93
+ module = { ...original };
94
+ }
95
+ // @ts-ignore TS18048 but module is always initialized
96
+ module[name] = withORM;
97
+ }
98
+ return module ?? original;
99
+ };
@@ -0,0 +1,6 @@
1
+ import { Type } from '@itrocks/class-type';
2
+ export declare const PROTECT_GET: unique symbol;
3
+ export type PropertyDescriptorWithProtectGet = PropertyDescriptor & ThisType<any> & {
4
+ [PROTECT_GET]?: true;
5
+ };
6
+ export declare function initClass<T extends object>(classType: Type<T>): Type<T> | undefined;
@@ -0,0 +1,97 @@
1
+ import { createRequire } from 'module'
2
+ const require = createRequire(import.meta.url)
3
+ import { isAnyType } from '@itrocks/class-type';
4
+ import { CollectionType } from '@itrocks/property-type';
5
+ import { ReflectClass } from '@itrocks/reflect';
6
+ import { dataSource } from '@itrocks/storage';
7
+ import { storeOf } from '@itrocks/store';
8
+ export const PROTECT_GET = Symbol('protectGet');
9
+ function defineCollectionProperty(type, property, builtClass) {
10
+ const descriptor = {
11
+ configurable: true,
12
+ enumerable: true,
13
+ async get() {
14
+ const elementType = type.elementType;
15
+ const ids = this[property + '_ids'];
16
+ return this[property] = ids
17
+ ? await dataSource().readMultiple(elementType, ids)
18
+ : await dataSource().readCollection(this, property, elementType);
19
+ },
20
+ set(value) {
21
+ delete this[property + '_ids'];
22
+ Object.defineProperty(this, property, { configurable: true, enumerable: true, value, writable: true });
23
+ Reflect.deleteMetadata(PROTECT_GET, builtClass.prototype, property);
24
+ }
25
+ };
26
+ Object.defineProperty(builtClass.prototype, property, descriptor);
27
+ Reflect.defineMetadata(PROTECT_GET, true, builtClass.prototype, property);
28
+ return property;
29
+ }
30
+ function defineObjectProperty(type, property, builtClass) {
31
+ const descriptor = {
32
+ configurable: true,
33
+ enumerable: true,
34
+ async get() {
35
+ const id = this[property + '_id'];
36
+ return this[property] = id ? await dataSource().read(type, id) : undefined;
37
+ },
38
+ set(value) {
39
+ delete this[property + '_id'];
40
+ Object.defineProperty(this, property, { configurable: true, enumerable: true, value, writable: true });
41
+ Reflect.deleteMetadata(PROTECT_GET, builtClass.prototype, property);
42
+ }
43
+ };
44
+ Object.defineProperty(builtClass.prototype, property, descriptor);
45
+ Reflect.defineMetadata(PROTECT_GET, true, builtClass.prototype, property);
46
+ return property;
47
+ }
48
+ export function initClass(classType) {
49
+ try {
50
+ if (!storeOf(classType))
51
+ return;
52
+ }
53
+ catch {
54
+ return;
55
+ }
56
+ const properties = [];
57
+ // @ts-ignore TS2415 classType is always a heritable class, not a function.
58
+ const BuiltClass = (() => class extends classType {
59
+ constructor(...args) {
60
+ super(...args);
61
+ for (const property of properties) {
62
+ delete this[property];
63
+ }
64
+ }
65
+ })();
66
+ for (const property of Object.values(new ReflectClass(classType).properties)) {
67
+ const type = property.type;
68
+ if (!type)
69
+ continue;
70
+ if ((type instanceof CollectionType) && isAnyType(type.elementType)) {
71
+ properties.push(defineCollectionProperty(type, property.name, BuiltClass));
72
+ }
73
+ else if (isAnyType(type)) {
74
+ properties.push(defineObjectProperty(type, property.name, BuiltClass));
75
+ }
76
+ }
77
+ return properties.length ? BuiltClass : undefined;
78
+ }
79
+ const Module = require('module');
80
+ const superRequire = Module.prototype.require;
81
+ Module.prototype.require = function () {
82
+ const original = superRequire.call(this, ...arguments);
83
+ let module;
84
+ for (const [name, type] of Object.entries(original)) {
85
+ if (!isAnyType(type))
86
+ continue;
87
+ const withORM = initClass(type);
88
+ if (!withORM)
89
+ continue;
90
+ if (!module) {
91
+ module = { ...original };
92
+ }
93
+ // @ts-ignore TS18048 but module is always initialized
94
+ module[name] = withORM;
95
+ }
96
+ return module ?? original;
97
+ };
package/package.json ADDED
@@ -0,0 +1,50 @@
1
+ {
2
+ "author": {
3
+ "name": "Baptiste Pillot",
4
+ "email": "baptiste@pillot.fr"
5
+ },
6
+ "dependencies": {
7
+ "@itrocks/class-type": "latest",
8
+ "@itrocks/property-type": "latest",
9
+ "@itrocks/reflect": "latest",
10
+ "@itrocks/storage": "latest",
11
+ "@itrocks/store": "latest"
12
+ },
13
+ "description": "",
14
+ "devDependencies": {
15
+ "@types/node": "^22.10",
16
+ "typescript": "^5.6"
17
+ },
18
+ "engines": {
19
+ "node": ">=18"
20
+ },
21
+ "exports": {
22
+ "import": "./esm/lazy-loading.js",
23
+ "require": "./cjs/lazy-loading.js"
24
+ },
25
+ "files": [
26
+ "LICENSE",
27
+ "README.md",
28
+ "*/*.d.ts",
29
+ "*/*.js",
30
+ "!*/esm.*"
31
+ ],
32
+ "homepage": "https://it.rocks",
33
+ "keywords": [
34
+ "backend",
35
+ "it.rocks"
36
+ ],
37
+ "license": "ISC",
38
+ "name": "@itrocks/lazy-loading",
39
+ "repository": {
40
+ "type": "git",
41
+ "url": "git+https://github.com/itrocks-ts/lazy-loading.git"
42
+ },
43
+ "scripts": {
44
+ "build": "npm run build:cjs && npm run build:esm",
45
+ "build:cjs": "tsc -p tsconfig.cjs.json",
46
+ "build:esm": "tsc -p tsconfig.esm.json && node esm/esm"
47
+ },
48
+ "types": "./esm/lazy-loading.d.ts",
49
+ "version": "0.0.1"
50
+ }