simplentity 1.0.0-alpha.0 → 1.0.0-alpha.2
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/README.md +10 -11
- package/dist/bun/entity.d.ts +13 -5
- package/dist/bun/index.d.ts +3 -3
- package/dist/bun/index.js +1 -1
- package/dist/cjs/entity.cjs +41 -34
- package/dist/cjs/entity.d.ts +13 -5
- package/dist/cjs/fields/entity.cjs +68 -0
- package/dist/cjs/fields/index.cjs +9 -0
- package/dist/cjs/index.cjs +48 -32
- package/dist/cjs/index.d.ts +3 -3
- package/dist/entity.d.ts +13 -5
- package/dist/entity.js +40 -33
- package/dist/fields/entity.d.ts +5 -0
- package/dist/fields/entity.js +41 -0
- package/dist/fields/index.d.ts +2 -1
- package/dist/fields/index.js +8 -0
- package/dist/index.d.ts +3 -3
- package/dist/index.js +47 -32
- package/package.json +1 -1
package/README.md
CHANGED
@@ -16,28 +16,27 @@ npm install simplentity
|
|
16
16
|
## Usage
|
17
17
|
|
18
18
|
```typescript
|
19
|
-
import {
|
19
|
+
import {createEntity, number, string, boolean} from 'simplentity';
|
20
20
|
|
21
21
|
// Define a user entity
|
22
|
-
|
22
|
+
const userFactory = createEntity({
|
23
23
|
id: string().defaultFn(() => randomUUID()), // randomUUID is a third-party library. Not included.
|
24
24
|
name: string(),
|
25
25
|
age: number().notRequired(),
|
26
26
|
isActive: boolean().default(true),
|
27
|
-
}) {
|
28
|
-
activate()
|
27
|
+
}, ({ set }) => ({
|
28
|
+
activate: () => {
|
29
29
|
// You can get suggestions for the properties of the entity.
|
30
|
-
|
30
|
+
set('isActive', true)
|
31
|
+
},
|
32
|
+
deactivate: () => {
|
33
|
+
set('isActive', false)
|
31
34
|
}
|
32
|
-
|
33
|
-
deactivate(): void {
|
34
|
-
this.set('isActive', false);
|
35
|
-
}
|
36
|
-
}
|
35
|
+
}))
|
37
36
|
|
38
37
|
// Properties that have NotRequired or Default(Fn) are optional.
|
39
38
|
// If a property has a default value, it is automatically assigned to the property when you create the entity instance.
|
40
|
-
const user =
|
39
|
+
const user = userFactory.create({
|
41
40
|
name: 'John Doe',
|
42
41
|
})
|
43
42
|
/*
|
package/dist/bun/entity.d.ts
CHANGED
@@ -21,6 +21,16 @@ interface EntityInterface<C extends EntityConfig> {
|
|
21
21
|
get: <K extends keyof C>(key: K) => EntityConfigTypeResolver<C>[K];
|
22
22
|
toJSON: () => EntityConfigTypeResolver<C>;
|
23
23
|
}
|
24
|
+
declare class EntityFactory<C extends EntityConfig, D extends MethodDefinition> {
|
25
|
+
private readonly fields;
|
26
|
+
private readonly methodDefinitionFunction?;
|
27
|
+
readonly $infer: EntityInterface<C> & D;
|
28
|
+
constructor(fields: C, methodDefinitionFunction?: (params: {
|
29
|
+
set: <K extends keyof C>(key: K, value: EntityConfigTypeResolver<C>[K]) => void;
|
30
|
+
get: <K extends keyof C>(key: K) => EntityConfigTypeResolver<C>[K];
|
31
|
+
}) => D);
|
32
|
+
create(props: EntityPropInputResolver<C>): EntityInterface<C> & D;
|
33
|
+
}
|
24
34
|
/**
|
25
35
|
* Creates an entity factory function that allows defining fields and optional methods for an entity.
|
26
36
|
* The returned factory provides a `create` method to instantiate entities with the specified fields
|
@@ -41,7 +51,7 @@ interface EntityInterface<C extends EntityConfig> {
|
|
41
51
|
*
|
42
52
|
* @example
|
43
53
|
* ```typescript
|
44
|
-
* const userFactory =
|
54
|
+
* const userFactory = createEntity({
|
45
55
|
* name: string(),
|
46
56
|
* age: number().default(18),
|
47
57
|
* isActive: boolean().default(true),
|
@@ -55,10 +65,8 @@ interface EntityInterface<C extends EntityConfig> {
|
|
55
65
|
* console.log(user.props.age); // 19
|
56
66
|
* ```
|
57
67
|
*/
|
58
|
-
export declare function
|
68
|
+
export declare function createEntity<C extends EntityConfig, D extends MethodDefinition>(fields: C, methodDefinitionFunction?: (params: {
|
59
69
|
set: <K extends keyof C>(key: K, value: EntityConfigTypeResolver<C>[K]) => void;
|
60
70
|
get: <K extends keyof C>(key: K) => EntityConfigTypeResolver<C>[K];
|
61
|
-
}) => D):
|
62
|
-
create(props: EntityPropInputResolver<C>): EntityInterface<C> & D;
|
63
|
-
};
|
71
|
+
}) => D): EntityFactory<C, D>;
|
64
72
|
export {};
|
package/dist/bun/index.d.ts
CHANGED
@@ -1,3 +1,3 @@
|
|
1
|
-
import {
|
2
|
-
import { boolean, date, number, string } from "./fields/index.ts";
|
3
|
-
export {
|
1
|
+
import { createEntity } from "./entity.ts";
|
2
|
+
import { boolean, date, number, string, entity } from "./fields/index.ts";
|
3
|
+
export { createEntity, string, number, boolean, date, entity };
|
package/dist/bun/index.js
CHANGED
@@ -1,2 +1,2 @@
|
|
1
1
|
// @bun
|
2
|
-
|
2
|
+
class _{fields;methodDefinitionFunction;constructor(u,R){this.fields=u,this.methodDefinitionFunction=R}create(u){let R=Object.entries(this.fields).reduce((q,[x,C])=>{let D=u[x]??C.getDefaultValue();if(C.getConfig().hasDefault&&D===void 0)q[x]=C.getDefaultValue();else q[x]=D;return q},{}),W=(q,x)=>{R[q]=x},H=(q)=>{return R[q]},X=()=>{return R},Y=this.methodDefinitionFunction?.({set:W,get:H})??{};return{get:H,toJSON:X,...Y}}}function Z(u,R){return new _(u,R)}class T{config;constructor(){this.config={notRequired:!1,hasDefault:!1,default:void 0}}notRequired(){return this.config.notRequired=!0,this}default(u){return this.config.default=u,this.config.hasDefault=!0,this}defaultFn(u){return this.config.defaultFn=u,this.config.hasDefault=!0,this}getConfig(){return this.config}getDefaultValue(){return this.config.default??this.config.defaultFn?.()}}class b extends T{}var f=()=>{return new b};class B extends T{}var V=()=>{return new B};class G extends T{}var K=()=>{return new G};class L extends T{}var M=()=>{return new L};class Q extends T{}var U=()=>{return new Q};export{M as string,K as number,U as entity,V as date,Z as createEntity,f as boolean};
|
package/dist/cjs/entity.cjs
CHANGED
@@ -20,43 +20,50 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
20
20
|
// src/entity.ts
|
21
21
|
var entity_exports = {};
|
22
22
|
__export(entity_exports, {
|
23
|
-
|
23
|
+
createEntity: () => createEntity
|
24
24
|
});
|
25
25
|
module.exports = __toCommonJS(entity_exports);
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
{
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
}
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
26
|
+
var EntityFactory = class {
|
27
|
+
fields;
|
28
|
+
methodDefinitionFunction;
|
29
|
+
constructor(fields, methodDefinitionFunction) {
|
30
|
+
this.fields = fields;
|
31
|
+
this.methodDefinitionFunction = methodDefinitionFunction;
|
32
|
+
}
|
33
|
+
create(props) {
|
34
|
+
const assignedProps = Object.entries(this.fields).reduce(
|
35
|
+
(acc, [key, field]) => {
|
36
|
+
const value = props[key] ?? field.getDefaultValue();
|
37
|
+
if (field.getConfig().hasDefault && value === void 0) {
|
38
|
+
acc[key] = field.getDefaultValue();
|
39
|
+
} else {
|
40
|
+
acc[key] = value;
|
41
|
+
}
|
42
|
+
return acc;
|
43
|
+
},
|
44
|
+
{}
|
45
|
+
);
|
46
|
+
const set = (key, value) => {
|
47
|
+
assignedProps[key] = value;
|
48
|
+
};
|
49
|
+
const get = (key) => {
|
50
|
+
return assignedProps[key];
|
51
|
+
};
|
52
|
+
const toJSON = () => {
|
53
|
+
return assignedProps;
|
54
|
+
};
|
55
|
+
const methods = this.methodDefinitionFunction?.({ set, get }) ?? {};
|
56
|
+
return {
|
57
|
+
get,
|
58
|
+
toJSON,
|
59
|
+
...methods
|
60
|
+
};
|
61
|
+
}
|
62
|
+
};
|
63
|
+
function createEntity(fields, methodDefinitionFunction) {
|
64
|
+
return new EntityFactory(fields, methodDefinitionFunction);
|
58
65
|
}
|
59
66
|
// Annotate the CommonJS export names for ESM import in node:
|
60
67
|
0 && (module.exports = {
|
61
|
-
|
68
|
+
createEntity
|
62
69
|
});
|
package/dist/cjs/entity.d.ts
CHANGED
@@ -21,6 +21,16 @@ interface EntityInterface<C extends EntityConfig> {
|
|
21
21
|
get: <K extends keyof C>(key: K) => EntityConfigTypeResolver<C>[K];
|
22
22
|
toJSON: () => EntityConfigTypeResolver<C>;
|
23
23
|
}
|
24
|
+
declare class EntityFactory<C extends EntityConfig, D extends MethodDefinition> {
|
25
|
+
private readonly fields;
|
26
|
+
private readonly methodDefinitionFunction?;
|
27
|
+
readonly $infer: EntityInterface<C> & D;
|
28
|
+
constructor(fields: C, methodDefinitionFunction?: (params: {
|
29
|
+
set: <K extends keyof C>(key: K, value: EntityConfigTypeResolver<C>[K]) => void;
|
30
|
+
get: <K extends keyof C>(key: K) => EntityConfigTypeResolver<C>[K];
|
31
|
+
}) => D);
|
32
|
+
create(props: EntityPropInputResolver<C>): EntityInterface<C> & D;
|
33
|
+
}
|
24
34
|
/**
|
25
35
|
* Creates an entity factory function that allows defining fields and optional methods for an entity.
|
26
36
|
* The returned factory provides a `create` method to instantiate entities with the specified fields
|
@@ -41,7 +51,7 @@ interface EntityInterface<C extends EntityConfig> {
|
|
41
51
|
*
|
42
52
|
* @example
|
43
53
|
* ```typescript
|
44
|
-
* const userFactory =
|
54
|
+
* const userFactory = createEntity({
|
45
55
|
* name: string(),
|
46
56
|
* age: number().default(18),
|
47
57
|
* isActive: boolean().default(true),
|
@@ -55,10 +65,8 @@ interface EntityInterface<C extends EntityConfig> {
|
|
55
65
|
* console.log(user.props.age); // 19
|
56
66
|
* ```
|
57
67
|
*/
|
58
|
-
export declare function
|
68
|
+
export declare function createEntity<C extends EntityConfig, D extends MethodDefinition>(fields: C, methodDefinitionFunction?: (params: {
|
59
69
|
set: <K extends keyof C>(key: K, value: EntityConfigTypeResolver<C>[K]) => void;
|
60
70
|
get: <K extends keyof C>(key: K) => EntityConfigTypeResolver<C>[K];
|
61
|
-
}) => D):
|
62
|
-
create(props: EntityPropInputResolver<C>): EntityInterface<C> & D;
|
63
|
-
};
|
71
|
+
}) => D): EntityFactory<C, D>;
|
64
72
|
export {};
|
@@ -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/entity.ts
|
21
|
+
var entity_exports = {};
|
22
|
+
__export(entity_exports, {
|
23
|
+
entity: () => entity
|
24
|
+
});
|
25
|
+
module.exports = __toCommonJS(entity_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/entity.ts
|
60
|
+
var EntityField = class extends Field {
|
61
|
+
};
|
62
|
+
var entity = () => {
|
63
|
+
return new EntityField();
|
64
|
+
};
|
65
|
+
// Annotate the CommonJS export names for ESM import in node:
|
66
|
+
0 && (module.exports = {
|
67
|
+
entity
|
68
|
+
});
|
@@ -22,6 +22,7 @@ var fields_exports = {};
|
|
22
22
|
__export(fields_exports, {
|
23
23
|
boolean: () => boolean,
|
24
24
|
date: () => date,
|
25
|
+
entity: () => entity,
|
25
26
|
number: () => number,
|
26
27
|
string: () => string
|
27
28
|
});
|
@@ -86,10 +87,18 @@ var StringField = class extends Field {
|
|
86
87
|
var string = () => {
|
87
88
|
return new StringField();
|
88
89
|
};
|
90
|
+
|
91
|
+
// src/fields/entity.ts
|
92
|
+
var EntityField = class extends Field {
|
93
|
+
};
|
94
|
+
var entity = () => {
|
95
|
+
return new EntityField();
|
96
|
+
};
|
89
97
|
// Annotate the CommonJS export names for ESM import in node:
|
90
98
|
0 && (module.exports = {
|
91
99
|
boolean,
|
92
100
|
date,
|
101
|
+
entity,
|
93
102
|
number,
|
94
103
|
string
|
95
104
|
});
|
package/dist/cjs/index.cjs
CHANGED
@@ -21,6 +21,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
21
21
|
var index_exports = {};
|
22
22
|
__export(index_exports, {
|
23
23
|
boolean: () => boolean,
|
24
|
+
createEntity: () => createEntity,
|
24
25
|
date: () => date,
|
25
26
|
entity: () => entity,
|
26
27
|
number: () => number,
|
@@ -29,38 +30,45 @@ __export(index_exports, {
|
|
29
30
|
module.exports = __toCommonJS(index_exports);
|
30
31
|
|
31
32
|
// src/entity.ts
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
{
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
}
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
33
|
+
var EntityFactory = class {
|
34
|
+
fields;
|
35
|
+
methodDefinitionFunction;
|
36
|
+
constructor(fields, methodDefinitionFunction) {
|
37
|
+
this.fields = fields;
|
38
|
+
this.methodDefinitionFunction = methodDefinitionFunction;
|
39
|
+
}
|
40
|
+
create(props) {
|
41
|
+
const assignedProps = Object.entries(this.fields).reduce(
|
42
|
+
(acc, [key, field]) => {
|
43
|
+
const value = props[key] ?? field.getDefaultValue();
|
44
|
+
if (field.getConfig().hasDefault && value === void 0) {
|
45
|
+
acc[key] = field.getDefaultValue();
|
46
|
+
} else {
|
47
|
+
acc[key] = value;
|
48
|
+
}
|
49
|
+
return acc;
|
50
|
+
},
|
51
|
+
{}
|
52
|
+
);
|
53
|
+
const set = (key, value) => {
|
54
|
+
assignedProps[key] = value;
|
55
|
+
};
|
56
|
+
const get = (key) => {
|
57
|
+
return assignedProps[key];
|
58
|
+
};
|
59
|
+
const toJSON = () => {
|
60
|
+
return assignedProps;
|
61
|
+
};
|
62
|
+
const methods = this.methodDefinitionFunction?.({ set, get }) ?? {};
|
63
|
+
return {
|
64
|
+
get,
|
65
|
+
toJSON,
|
66
|
+
...methods
|
67
|
+
};
|
68
|
+
}
|
69
|
+
};
|
70
|
+
function createEntity(fields, methodDefinitionFunction) {
|
71
|
+
return new EntityFactory(fields, methodDefinitionFunction);
|
64
72
|
}
|
65
73
|
|
66
74
|
// src/field.ts
|
@@ -122,9 +130,17 @@ var StringField = class extends Field {
|
|
122
130
|
var string = () => {
|
123
131
|
return new StringField();
|
124
132
|
};
|
133
|
+
|
134
|
+
// src/fields/entity.ts
|
135
|
+
var EntityField = class extends Field {
|
136
|
+
};
|
137
|
+
var entity = () => {
|
138
|
+
return new EntityField();
|
139
|
+
};
|
125
140
|
// Annotate the CommonJS export names for ESM import in node:
|
126
141
|
0 && (module.exports = {
|
127
142
|
boolean,
|
143
|
+
createEntity,
|
128
144
|
date,
|
129
145
|
entity,
|
130
146
|
number,
|
package/dist/cjs/index.d.ts
CHANGED
@@ -1,3 +1,3 @@
|
|
1
|
-
import {
|
2
|
-
import { boolean, date, number, string } from "./fields/index.ts";
|
3
|
-
export {
|
1
|
+
import { createEntity } from "./entity.ts";
|
2
|
+
import { boolean, date, number, string, entity } from "./fields/index.ts";
|
3
|
+
export { createEntity, string, number, boolean, date, entity };
|
package/dist/entity.d.ts
CHANGED
@@ -21,6 +21,16 @@ interface EntityInterface<C extends EntityConfig> {
|
|
21
21
|
get: <K extends keyof C>(key: K) => EntityConfigTypeResolver<C>[K];
|
22
22
|
toJSON: () => EntityConfigTypeResolver<C>;
|
23
23
|
}
|
24
|
+
declare class EntityFactory<C extends EntityConfig, D extends MethodDefinition> {
|
25
|
+
private readonly fields;
|
26
|
+
private readonly methodDefinitionFunction?;
|
27
|
+
readonly $infer: EntityInterface<C> & D;
|
28
|
+
constructor(fields: C, methodDefinitionFunction?: (params: {
|
29
|
+
set: <K extends keyof C>(key: K, value: EntityConfigTypeResolver<C>[K]) => void;
|
30
|
+
get: <K extends keyof C>(key: K) => EntityConfigTypeResolver<C>[K];
|
31
|
+
}) => D);
|
32
|
+
create(props: EntityPropInputResolver<C>): EntityInterface<C> & D;
|
33
|
+
}
|
24
34
|
/**
|
25
35
|
* Creates an entity factory function that allows defining fields and optional methods for an entity.
|
26
36
|
* The returned factory provides a `create` method to instantiate entities with the specified fields
|
@@ -41,7 +51,7 @@ interface EntityInterface<C extends EntityConfig> {
|
|
41
51
|
*
|
42
52
|
* @example
|
43
53
|
* ```typescript
|
44
|
-
* const userFactory =
|
54
|
+
* const userFactory = createEntity({
|
45
55
|
* name: string(),
|
46
56
|
* age: number().default(18),
|
47
57
|
* isActive: boolean().default(true),
|
@@ -55,10 +65,8 @@ interface EntityInterface<C extends EntityConfig> {
|
|
55
65
|
* console.log(user.props.age); // 19
|
56
66
|
* ```
|
57
67
|
*/
|
58
|
-
export declare function
|
68
|
+
export declare function createEntity<C extends EntityConfig, D extends MethodDefinition>(fields: C, methodDefinitionFunction?: (params: {
|
59
69
|
set: <K extends keyof C>(key: K, value: EntityConfigTypeResolver<C>[K]) => void;
|
60
70
|
get: <K extends keyof C>(key: K) => EntityConfigTypeResolver<C>[K];
|
61
|
-
}) => D):
|
62
|
-
create(props: EntityPropInputResolver<C>): EntityInterface<C> & D;
|
63
|
-
};
|
71
|
+
}) => D): EntityFactory<C, D>;
|
64
72
|
export {};
|
package/dist/entity.js
CHANGED
@@ -1,37 +1,44 @@
|
|
1
1
|
// src/entity.ts
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
{
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
}
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
2
|
+
var EntityFactory = class {
|
3
|
+
fields;
|
4
|
+
methodDefinitionFunction;
|
5
|
+
constructor(fields, methodDefinitionFunction) {
|
6
|
+
this.fields = fields;
|
7
|
+
this.methodDefinitionFunction = methodDefinitionFunction;
|
8
|
+
}
|
9
|
+
create(props) {
|
10
|
+
const assignedProps = Object.entries(this.fields).reduce(
|
11
|
+
(acc, [key, field]) => {
|
12
|
+
const value = props[key] ?? field.getDefaultValue();
|
13
|
+
if (field.getConfig().hasDefault && value === void 0) {
|
14
|
+
acc[key] = field.getDefaultValue();
|
15
|
+
} else {
|
16
|
+
acc[key] = value;
|
17
|
+
}
|
18
|
+
return acc;
|
19
|
+
},
|
20
|
+
{}
|
21
|
+
);
|
22
|
+
const set = (key, value) => {
|
23
|
+
assignedProps[key] = value;
|
24
|
+
};
|
25
|
+
const get = (key) => {
|
26
|
+
return assignedProps[key];
|
27
|
+
};
|
28
|
+
const toJSON = () => {
|
29
|
+
return assignedProps;
|
30
|
+
};
|
31
|
+
const methods = this.methodDefinitionFunction?.({ set, get }) ?? {};
|
32
|
+
return {
|
33
|
+
get,
|
34
|
+
toJSON,
|
35
|
+
...methods
|
36
|
+
};
|
37
|
+
}
|
38
|
+
};
|
39
|
+
function createEntity(fields, methodDefinitionFunction) {
|
40
|
+
return new EntityFactory(fields, methodDefinitionFunction);
|
34
41
|
}
|
35
42
|
export {
|
36
|
-
|
43
|
+
createEntity
|
37
44
|
};
|
@@ -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/entity.ts
|
34
|
+
var EntityField = class extends Field {
|
35
|
+
};
|
36
|
+
var entity = () => {
|
37
|
+
return new EntityField();
|
38
|
+
};
|
39
|
+
export {
|
40
|
+
entity
|
41
|
+
};
|
package/dist/fields/index.d.ts
CHANGED
@@ -2,4 +2,5 @@ import { boolean } from "./boolean.ts";
|
|
2
2
|
import { date } from "./date.ts";
|
3
3
|
import { number } from "./number.ts";
|
4
4
|
import { string } from "./string.ts";
|
5
|
-
|
5
|
+
import { entity } from "./entity.ts";
|
6
|
+
export { string, number, boolean, date, entity };
|
package/dist/fields/index.js
CHANGED
@@ -57,9 +57,17 @@ var StringField = class extends Field {
|
|
57
57
|
var string = () => {
|
58
58
|
return new StringField();
|
59
59
|
};
|
60
|
+
|
61
|
+
// src/fields/entity.ts
|
62
|
+
var EntityField = class extends Field {
|
63
|
+
};
|
64
|
+
var entity = () => {
|
65
|
+
return new EntityField();
|
66
|
+
};
|
60
67
|
export {
|
61
68
|
boolean,
|
62
69
|
date,
|
70
|
+
entity,
|
63
71
|
number,
|
64
72
|
string
|
65
73
|
};
|
package/dist/index.d.ts
CHANGED
@@ -1,3 +1,3 @@
|
|
1
|
-
import {
|
2
|
-
import { boolean, date, number, string } from "./fields/index.ts";
|
3
|
-
export {
|
1
|
+
import { createEntity } from "./entity.ts";
|
2
|
+
import { boolean, date, number, string, entity } from "./fields/index.ts";
|
3
|
+
export { createEntity, string, number, boolean, date, entity };
|
package/dist/index.js
CHANGED
@@ -1,36 +1,43 @@
|
|
1
1
|
// src/entity.ts
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
{
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
}
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
2
|
+
var EntityFactory = class {
|
3
|
+
fields;
|
4
|
+
methodDefinitionFunction;
|
5
|
+
constructor(fields, methodDefinitionFunction) {
|
6
|
+
this.fields = fields;
|
7
|
+
this.methodDefinitionFunction = methodDefinitionFunction;
|
8
|
+
}
|
9
|
+
create(props) {
|
10
|
+
const assignedProps = Object.entries(this.fields).reduce(
|
11
|
+
(acc, [key, field]) => {
|
12
|
+
const value = props[key] ?? field.getDefaultValue();
|
13
|
+
if (field.getConfig().hasDefault && value === void 0) {
|
14
|
+
acc[key] = field.getDefaultValue();
|
15
|
+
} else {
|
16
|
+
acc[key] = value;
|
17
|
+
}
|
18
|
+
return acc;
|
19
|
+
},
|
20
|
+
{}
|
21
|
+
);
|
22
|
+
const set = (key, value) => {
|
23
|
+
assignedProps[key] = value;
|
24
|
+
};
|
25
|
+
const get = (key) => {
|
26
|
+
return assignedProps[key];
|
27
|
+
};
|
28
|
+
const toJSON = () => {
|
29
|
+
return assignedProps;
|
30
|
+
};
|
31
|
+
const methods = this.methodDefinitionFunction?.({ set, get }) ?? {};
|
32
|
+
return {
|
33
|
+
get,
|
34
|
+
toJSON,
|
35
|
+
...methods
|
36
|
+
};
|
37
|
+
}
|
38
|
+
};
|
39
|
+
function createEntity(fields, methodDefinitionFunction) {
|
40
|
+
return new EntityFactory(fields, methodDefinitionFunction);
|
34
41
|
}
|
35
42
|
|
36
43
|
// src/field.ts
|
@@ -92,8 +99,16 @@ var StringField = class extends Field {
|
|
92
99
|
var string = () => {
|
93
100
|
return new StringField();
|
94
101
|
};
|
102
|
+
|
103
|
+
// src/fields/entity.ts
|
104
|
+
var EntityField = class extends Field {
|
105
|
+
};
|
106
|
+
var entity = () => {
|
107
|
+
return new EntityField();
|
108
|
+
};
|
95
109
|
export {
|
96
110
|
boolean,
|
111
|
+
createEntity,
|
97
112
|
date,
|
98
113
|
entity,
|
99
114
|
number,
|