simplentity 0.1.0

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/dist/index.js ADDED
@@ -0,0 +1,116 @@
1
+ // src/entity.ts
2
+ var Entity = class {
3
+ #entityConfig;
4
+ #props;
5
+ constructor(props, entityConfig) {
6
+ this.#entityConfig = entityConfig;
7
+ Object.freeze(this.#entityConfig);
8
+ this.#props = Object.entries(entityConfig).reduce(
9
+ (acc, [key, field]) => {
10
+ const value = props[key] ?? field.getDefaultValue();
11
+ if (field.getConfig().hasDefault && value === void 0) {
12
+ throw new Error(`The field "${key}" has a default value but undefined was provided.`);
13
+ }
14
+ acc[key] = value;
15
+ return acc;
16
+ },
17
+ {}
18
+ );
19
+ }
20
+ /**
21
+ * Get the value of the field by key
22
+ * @param key
23
+ */
24
+ get(key) {
25
+ return this.#props[key];
26
+ }
27
+ /**
28
+ * Set the value of the field by key
29
+ *
30
+ * WARNING: This method should be called only from the methods of the entity.
31
+ * Its accessor should be protected but TypeScript declaration does not allow protected methods in exported classes.
32
+ * @param key
33
+ * @param value
34
+ */
35
+ set(key, value) {
36
+ this.#props[key] = value;
37
+ }
38
+ // biome-ignore lint/style/useNamingConvention: toJSON is a name to be used in JSON.stringify
39
+ toJSON() {
40
+ return this.#props;
41
+ }
42
+ };
43
+ var entity = (fields) => {
44
+ return class extends Entity {
45
+ constructor(props) {
46
+ super(props, fields);
47
+ }
48
+ };
49
+ };
50
+
51
+ // src/field.ts
52
+ var Field = class {
53
+ config;
54
+ constructor() {
55
+ this.config = {
56
+ notRequired: false,
57
+ hasDefault: false,
58
+ default: void 0
59
+ };
60
+ }
61
+ notRequired() {
62
+ this.config.notRequired = true;
63
+ return this;
64
+ }
65
+ default(value) {
66
+ this.config.default = value;
67
+ this.config.hasDefault = true;
68
+ return this;
69
+ }
70
+ defaultFn(fn) {
71
+ this.config.defaultFn = fn;
72
+ this.config.hasDefault = true;
73
+ return this;
74
+ }
75
+ getConfig() {
76
+ return this.config;
77
+ }
78
+ getDefaultValue() {
79
+ return this.config.default ?? this.config.defaultFn?.();
80
+ }
81
+ };
82
+
83
+ // src/fields/boolean.ts
84
+ var BooleanField = class extends Field {
85
+ };
86
+ var boolean = () => {
87
+ return new BooleanField();
88
+ };
89
+
90
+ // src/fields/date.ts
91
+ var DateField = class extends Field {
92
+ };
93
+ var date = () => {
94
+ return new DateField();
95
+ };
96
+
97
+ // src/fields/number.ts
98
+ var NumberField = class extends Field {
99
+ };
100
+ var number = () => {
101
+ return new NumberField();
102
+ };
103
+
104
+ // src/fields/string.ts
105
+ var StringField = class extends Field {
106
+ };
107
+ var string = () => {
108
+ return new StringField();
109
+ };
110
+ export {
111
+ boolean,
112
+ date,
113
+ entity,
114
+ number,
115
+ string
116
+ };
package/package.json ADDED
@@ -0,0 +1,34 @@
1
+ {
2
+ "name": "simplentity",
3
+ "version": "0.1.0",
4
+ "type": "module",
5
+ "module": "./dist/index.js",
6
+ "types": "./dist/index.d.ts",
7
+ "main": "./dist/cjs/index.js",
8
+ "files": [
9
+ "dist"
10
+ ],
11
+ "scripts": {
12
+ "check": "bun biome check ./src",
13
+ "build": "bun run ./build.ts"
14
+ },
15
+ "devDependencies": {
16
+ "@biomejs/biome": "1.9.4",
17
+ "@types/bun": "latest",
18
+ "lefthook": "^1.11.8",
19
+ "tsup": "^8.4.0"
20
+ },
21
+ "peerDependencies": {
22
+ "typescript": "^5.8.0"
23
+ },
24
+ "trustedDependencies": [
25
+ "@biomejs/biome",
26
+ "lefthook"
27
+ ],
28
+ "repository": {
29
+ "url": "https://github.com/Lazialize/simplentity"
30
+ },
31
+ "bugs": {
32
+ "url": "https://github.com/Lazialize/simplentity/issues"
33
+ }
34
+ }