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.
@@ -0,0 +1,68 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/fields/string.ts
21
+ var string_exports = {};
22
+ __export(string_exports, {
23
+ string: () => string
24
+ });
25
+ module.exports = __toCommonJS(string_exports);
26
+
27
+ // src/field.ts
28
+ var Field = class {
29
+ config;
30
+ constructor() {
31
+ this.config = {
32
+ notRequired: false,
33
+ hasDefault: false,
34
+ default: void 0
35
+ };
36
+ }
37
+ notRequired() {
38
+ this.config.notRequired = true;
39
+ return this;
40
+ }
41
+ default(value) {
42
+ this.config.default = value;
43
+ this.config.hasDefault = true;
44
+ return this;
45
+ }
46
+ defaultFn(fn) {
47
+ this.config.defaultFn = fn;
48
+ this.config.hasDefault = true;
49
+ return this;
50
+ }
51
+ getConfig() {
52
+ return this.config;
53
+ }
54
+ getDefaultValue() {
55
+ return this.config.default ?? this.config.defaultFn?.();
56
+ }
57
+ };
58
+
59
+ // src/fields/string.ts
60
+ var StringField = class extends Field {
61
+ };
62
+ var string = () => {
63
+ return new StringField();
64
+ };
65
+ // Annotate the CommonJS export names for ESM import in node:
66
+ 0 && (module.exports = {
67
+ string
68
+ });
@@ -0,0 +1,147 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ boolean: () => boolean,
24
+ date: () => date,
25
+ entity: () => entity,
26
+ number: () => number,
27
+ string: () => string
28
+ });
29
+ module.exports = __toCommonJS(index_exports);
30
+
31
+ // src/entity.ts
32
+ var Entity = class {
33
+ #entityConfig;
34
+ #props;
35
+ constructor(props, entityConfig) {
36
+ this.#entityConfig = entityConfig;
37
+ Object.freeze(this.#entityConfig);
38
+ this.#props = Object.entries(entityConfig).reduce(
39
+ (acc, [key, field]) => {
40
+ const value = props[key] ?? field.getDefaultValue();
41
+ if (field.getConfig().hasDefault && value === void 0) {
42
+ throw new Error(`The field "${key}" has a default value but undefined was provided.`);
43
+ }
44
+ acc[key] = value;
45
+ return acc;
46
+ },
47
+ {}
48
+ );
49
+ }
50
+ /**
51
+ * Get the value of the field by key
52
+ * @param key
53
+ */
54
+ get(key) {
55
+ return this.#props[key];
56
+ }
57
+ /**
58
+ * Set the value of the field by key
59
+ *
60
+ * WARNING: This method should be called only from the methods of the entity.
61
+ * Its accessor should be protected but TypeScript declaration does not allow protected methods in exported classes.
62
+ * @param key
63
+ * @param value
64
+ */
65
+ set(key, value) {
66
+ this.#props[key] = value;
67
+ }
68
+ // biome-ignore lint/style/useNamingConvention: toJSON is a name to be used in JSON.stringify
69
+ toJSON() {
70
+ return this.#props;
71
+ }
72
+ };
73
+ var entity = (fields) => {
74
+ return class extends Entity {
75
+ constructor(props) {
76
+ super(props, fields);
77
+ }
78
+ };
79
+ };
80
+
81
+ // src/field.ts
82
+ var Field = class {
83
+ config;
84
+ constructor() {
85
+ this.config = {
86
+ notRequired: false,
87
+ hasDefault: false,
88
+ default: void 0
89
+ };
90
+ }
91
+ notRequired() {
92
+ this.config.notRequired = true;
93
+ return this;
94
+ }
95
+ default(value) {
96
+ this.config.default = value;
97
+ this.config.hasDefault = true;
98
+ return this;
99
+ }
100
+ defaultFn(fn) {
101
+ this.config.defaultFn = fn;
102
+ this.config.hasDefault = true;
103
+ return this;
104
+ }
105
+ getConfig() {
106
+ return this.config;
107
+ }
108
+ getDefaultValue() {
109
+ return this.config.default ?? this.config.defaultFn?.();
110
+ }
111
+ };
112
+
113
+ // src/fields/boolean.ts
114
+ var BooleanField = class extends Field {
115
+ };
116
+ var boolean = () => {
117
+ return new BooleanField();
118
+ };
119
+
120
+ // src/fields/date.ts
121
+ var DateField = class extends Field {
122
+ };
123
+ var date = () => {
124
+ return new DateField();
125
+ };
126
+
127
+ // src/fields/number.ts
128
+ var NumberField = class extends Field {
129
+ };
130
+ var number = () => {
131
+ return new NumberField();
132
+ };
133
+
134
+ // src/fields/string.ts
135
+ var StringField = class extends Field {
136
+ };
137
+ var string = () => {
138
+ return new StringField();
139
+ };
140
+ // Annotate the CommonJS export names for ESM import in node:
141
+ 0 && (module.exports = {
142
+ boolean,
143
+ date,
144
+ entity,
145
+ number,
146
+ string
147
+ });
@@ -0,0 +1,3 @@
1
+ import { entity } from "./entity.ts";
2
+ import { boolean, date, number, string } from "./fields/index.ts";
3
+ export { entity, string, number, boolean, date };
@@ -0,0 +1,42 @@
1
+ import type { Field } from "./field.ts";
2
+ type EntityConfig = {
3
+ [key: string]: Field<unknown>;
4
+ };
5
+ type EntityConfigTypeResolver<T extends EntityConfig> = {
6
+ [K in keyof T]: T[K]["_"]["notRequired"] extends true ? FieldTypeResolver<T[K]> | undefined : FieldTypeResolver<T[K]>;
7
+ };
8
+ type RequiredFieldKeys<T extends EntityConfig> = {
9
+ [K in keyof T]: T[K]["_"]["notRequired"] extends true ? never : T[K]["_"]["hasDefault"] extends true ? never : K;
10
+ }[keyof T];
11
+ type FieldTypeResolver<T> = T extends Field<infer U> ? U : never;
12
+ type EntityPropInputResolver<T extends EntityConfig> = {
13
+ [K in RequiredFieldKeys<T>]: FieldTypeResolver<T[K]>;
14
+ } & {
15
+ [K in keyof Omit<T, RequiredFieldKeys<T>>]?: FieldTypeResolver<T[K]>;
16
+ };
17
+ /**
18
+ * Create an entity class with the given fields
19
+ * @param fields
20
+ */
21
+ export declare const entity: <Config extends EntityConfig>(fields: Config) => {
22
+ new (props: EntityPropInputResolver<Config>): {
23
+ readonly "__#1@#entityConfig": Config;
24
+ "__#1@#props": EntityConfigTypeResolver<Config>;
25
+ /**
26
+ * Get the value of the field by key
27
+ * @param key
28
+ */
29
+ get<K extends keyof Config>(key: K): EntityConfigTypeResolver<Config>[K];
30
+ /**
31
+ * Set the value of the field by key
32
+ *
33
+ * WARNING: This method should be called only from the methods of the entity.
34
+ * Its accessor should be protected but TypeScript declaration does not allow protected methods in exported classes.
35
+ * @param key
36
+ * @param value
37
+ */
38
+ set<K extends keyof Config>(key: K, value: EntityConfigTypeResolver<Config>[K]): void;
39
+ toJSON(): EntityConfigTypeResolver<Config>;
40
+ };
41
+ };
42
+ export {};
package/dist/entity.js ADDED
@@ -0,0 +1,52 @@
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
+ export {
51
+ entity
52
+ };
@@ -0,0 +1,35 @@
1
+ type FieldConfig<T> = {
2
+ notRequired: boolean;
3
+ hasDefault: boolean;
4
+ data: T;
5
+ };
6
+ type FieldRuntimeConfig<T> = {
7
+ notRequired: boolean;
8
+ hasDefault: boolean;
9
+ default: T | undefined;
10
+ defaultFn?: () => T;
11
+ };
12
+ interface ConfigurableFieldBase<T> {
13
+ _: FieldConfig<T>;
14
+ }
15
+ type NotRequired<T extends ConfigurableFieldBase<unknown>> = T & {
16
+ _: {
17
+ notRequired: true;
18
+ };
19
+ };
20
+ type HasDefault<T extends ConfigurableFieldBase<unknown>> = T & {
21
+ _: {
22
+ hasDefault: true;
23
+ };
24
+ };
25
+ export declare abstract class Field<T> implements ConfigurableFieldBase<T> {
26
+ _: FieldConfig<T>;
27
+ protected config: FieldRuntimeConfig<T>;
28
+ constructor();
29
+ notRequired(): NotRequired<this>;
30
+ default(value: T): HasDefault<this>;
31
+ defaultFn(fn: () => T): HasDefault<this>;
32
+ getConfig(): FieldRuntimeConfig<T>;
33
+ getDefaultValue(): T | undefined;
34
+ }
35
+ export {};
package/dist/field.js ADDED
@@ -0,0 +1,34 @@
1
+ // src/field.ts
2
+ var Field = class {
3
+ config;
4
+ constructor() {
5
+ this.config = {
6
+ notRequired: false,
7
+ hasDefault: false,
8
+ default: void 0
9
+ };
10
+ }
11
+ notRequired() {
12
+ this.config.notRequired = true;
13
+ return this;
14
+ }
15
+ default(value) {
16
+ this.config.default = value;
17
+ this.config.hasDefault = true;
18
+ return this;
19
+ }
20
+ defaultFn(fn) {
21
+ this.config.defaultFn = fn;
22
+ this.config.hasDefault = true;
23
+ return this;
24
+ }
25
+ getConfig() {
26
+ return this.config;
27
+ }
28
+ getDefaultValue() {
29
+ return this.config.default ?? this.config.defaultFn?.();
30
+ }
31
+ };
32
+ export {
33
+ Field
34
+ };
@@ -0,0 +1,8 @@
1
+ import { Field } from "../field.ts";
2
+ declare class BooleanField extends Field<boolean> {
3
+ }
4
+ /**
5
+ * Create a new boolean field
6
+ */
7
+ export declare const boolean: () => BooleanField;
8
+ export {};
@@ -0,0 +1,41 @@
1
+ // src/field.ts
2
+ var Field = class {
3
+ config;
4
+ constructor() {
5
+ this.config = {
6
+ notRequired: false,
7
+ hasDefault: false,
8
+ default: void 0
9
+ };
10
+ }
11
+ notRequired() {
12
+ this.config.notRequired = true;
13
+ return this;
14
+ }
15
+ default(value) {
16
+ this.config.default = value;
17
+ this.config.hasDefault = true;
18
+ return this;
19
+ }
20
+ defaultFn(fn) {
21
+ this.config.defaultFn = fn;
22
+ this.config.hasDefault = true;
23
+ return this;
24
+ }
25
+ getConfig() {
26
+ return this.config;
27
+ }
28
+ getDefaultValue() {
29
+ return this.config.default ?? this.config.defaultFn?.();
30
+ }
31
+ };
32
+
33
+ // src/fields/boolean.ts
34
+ var BooleanField = class extends Field {
35
+ };
36
+ var boolean = () => {
37
+ return new BooleanField();
38
+ };
39
+ export {
40
+ boolean
41
+ };
@@ -0,0 +1,8 @@
1
+ import { Field } from "../field.ts";
2
+ declare class DateField extends Field<Date> {
3
+ }
4
+ /**
5
+ * Create a new date field
6
+ */
7
+ export declare const date: () => DateField;
8
+ export {};
@@ -0,0 +1,41 @@
1
+ // src/field.ts
2
+ var Field = class {
3
+ config;
4
+ constructor() {
5
+ this.config = {
6
+ notRequired: false,
7
+ hasDefault: false,
8
+ default: void 0
9
+ };
10
+ }
11
+ notRequired() {
12
+ this.config.notRequired = true;
13
+ return this;
14
+ }
15
+ default(value) {
16
+ this.config.default = value;
17
+ this.config.hasDefault = true;
18
+ return this;
19
+ }
20
+ defaultFn(fn) {
21
+ this.config.defaultFn = fn;
22
+ this.config.hasDefault = true;
23
+ return this;
24
+ }
25
+ getConfig() {
26
+ return this.config;
27
+ }
28
+ getDefaultValue() {
29
+ return this.config.default ?? this.config.defaultFn?.();
30
+ }
31
+ };
32
+
33
+ // src/fields/date.ts
34
+ var DateField = class extends Field {
35
+ };
36
+ var date = () => {
37
+ return new DateField();
38
+ };
39
+ export {
40
+ date
41
+ };
@@ -0,0 +1,5 @@
1
+ import { boolean } from "./boolean.ts";
2
+ import { date } from "./date.ts";
3
+ import { number } from "./number.ts";
4
+ import { string } from "./string.ts";
5
+ export { string, number, boolean, date };
@@ -0,0 +1,65 @@
1
+ // src/field.ts
2
+ var Field = class {
3
+ config;
4
+ constructor() {
5
+ this.config = {
6
+ notRequired: false,
7
+ hasDefault: false,
8
+ default: void 0
9
+ };
10
+ }
11
+ notRequired() {
12
+ this.config.notRequired = true;
13
+ return this;
14
+ }
15
+ default(value) {
16
+ this.config.default = value;
17
+ this.config.hasDefault = true;
18
+ return this;
19
+ }
20
+ defaultFn(fn) {
21
+ this.config.defaultFn = fn;
22
+ this.config.hasDefault = true;
23
+ return this;
24
+ }
25
+ getConfig() {
26
+ return this.config;
27
+ }
28
+ getDefaultValue() {
29
+ return this.config.default ?? this.config.defaultFn?.();
30
+ }
31
+ };
32
+
33
+ // src/fields/boolean.ts
34
+ var BooleanField = class extends Field {
35
+ };
36
+ var boolean = () => {
37
+ return new BooleanField();
38
+ };
39
+
40
+ // src/fields/date.ts
41
+ var DateField = class extends Field {
42
+ };
43
+ var date = () => {
44
+ return new DateField();
45
+ };
46
+
47
+ // src/fields/number.ts
48
+ var NumberField = class extends Field {
49
+ };
50
+ var number = () => {
51
+ return new NumberField();
52
+ };
53
+
54
+ // src/fields/string.ts
55
+ var StringField = class extends Field {
56
+ };
57
+ var string = () => {
58
+ return new StringField();
59
+ };
60
+ export {
61
+ boolean,
62
+ date,
63
+ number,
64
+ string
65
+ };
@@ -0,0 +1,8 @@
1
+ import { Field } from "../field.ts";
2
+ declare class NumberField extends Field<number> {
3
+ }
4
+ /**
5
+ * Create a new number field
6
+ */
7
+ export declare const number: () => NumberField;
8
+ export {};
@@ -0,0 +1,41 @@
1
+ // src/field.ts
2
+ var Field = class {
3
+ config;
4
+ constructor() {
5
+ this.config = {
6
+ notRequired: false,
7
+ hasDefault: false,
8
+ default: void 0
9
+ };
10
+ }
11
+ notRequired() {
12
+ this.config.notRequired = true;
13
+ return this;
14
+ }
15
+ default(value) {
16
+ this.config.default = value;
17
+ this.config.hasDefault = true;
18
+ return this;
19
+ }
20
+ defaultFn(fn) {
21
+ this.config.defaultFn = fn;
22
+ this.config.hasDefault = true;
23
+ return this;
24
+ }
25
+ getConfig() {
26
+ return this.config;
27
+ }
28
+ getDefaultValue() {
29
+ return this.config.default ?? this.config.defaultFn?.();
30
+ }
31
+ };
32
+
33
+ // src/fields/number.ts
34
+ var NumberField = class extends Field {
35
+ };
36
+ var number = () => {
37
+ return new NumberField();
38
+ };
39
+ export {
40
+ number
41
+ };
@@ -0,0 +1,8 @@
1
+ import { Field } from "../field.ts";
2
+ declare class StringField extends Field<string> {
3
+ }
4
+ /**
5
+ * Create a new string field
6
+ */
7
+ export declare const string: () => StringField;
8
+ export {};
@@ -0,0 +1,41 @@
1
+ // src/field.ts
2
+ var Field = class {
3
+ config;
4
+ constructor() {
5
+ this.config = {
6
+ notRequired: false,
7
+ hasDefault: false,
8
+ default: void 0
9
+ };
10
+ }
11
+ notRequired() {
12
+ this.config.notRequired = true;
13
+ return this;
14
+ }
15
+ default(value) {
16
+ this.config.default = value;
17
+ this.config.hasDefault = true;
18
+ return this;
19
+ }
20
+ defaultFn(fn) {
21
+ this.config.defaultFn = fn;
22
+ this.config.hasDefault = true;
23
+ return this;
24
+ }
25
+ getConfig() {
26
+ return this.config;
27
+ }
28
+ getDefaultValue() {
29
+ return this.config.default ?? this.config.defaultFn?.();
30
+ }
31
+ };
32
+
33
+ // src/fields/string.ts
34
+ var StringField = class extends Field {
35
+ };
36
+ var string = () => {
37
+ return new StringField();
38
+ };
39
+ export {
40
+ string
41
+ };
@@ -0,0 +1,3 @@
1
+ import { entity } from "./entity.ts";
2
+ import { boolean, date, number, string } from "./fields/index.ts";
3
+ export { entity, string, number, boolean, date };