simplentity 1.0.0-alpha.1 → 1.0.0-alpha.3

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.
Files changed (2) hide show
  1. package/README.md +150 -49
  2. package/package.json +1 -1
package/README.md CHANGED
@@ -1,60 +1,161 @@
1
- # simplentity
1
+ # Simplentity
2
2
 
3
- The simplentity is a simple typed entity library.
4
- It provides the implementation of type-safe entities and manipulation.
3
+ A lightweight, type-safe entity factory for TypeScript and JavaScript. Define your domain entities with strongly-typed properties, default values, and optional fields—then get full IntelliSense for creating, reading, and updating your entity instances.
5
4
 
6
- > [!NOTE]
7
- > I created the library to learn TypeScript and the type system for me.
8
- > So, it may not be suitable for production use.
5
+ ## **Installation**
9
6
 
10
-
11
- ## Installation
12
- ```shell
7
+ ```bash
13
8
  npm install simplentity
9
+ # or
10
+ yarn add simplentity
11
+ ```
12
+
13
+ ## **Quickstart**
14
+
15
+ ```ts
16
+ import { createEntity, number, string, boolean } from 'simplentity';
17
+ import { randomUUID } from 'crypto'; // or any UUID library
18
+
19
+ // 1. Define your entity shape and methods:
20
+ const userFactory = createEntity(
21
+ {
22
+ id: string().defaultFn(() => randomUUID()), // auto-generated UUID
23
+ name: string(), // required string
24
+ age: number().notRequired(), // optional number
25
+ isActive: boolean().default(true), // defaults to true
26
+ },
27
+ ({ set }) => ({
28
+ activate: () => {
29
+ set('isActive', true);
30
+ },
31
+ deactivate: () => {
32
+ set('isActive', false);
33
+ }
34
+ })
35
+ );
36
+
37
+ // 2. Create an instance — properties with `.notRequired()` or defaults are optional:
38
+ const user = userFactory.create({ name: 'John Doe' });
39
+
40
+ console.log(user.toJSON());
41
+ // {
42
+ // id: 'd02daa87-3984-4c6f-be9b-a13a478354b5',
43
+ // name: 'John Doe',
44
+ // isActive: true
45
+ // }
46
+
47
+ // 3. Read and update properties with full type safety:
48
+ const currentName = user.get('name'); // type: string
49
+ user.set('age', 30); // set optional age
50
+ user.deactivate(); // custom method flips isActive to false
51
+ console.log(user.get('isActive')); // false
14
52
  ```
15
53
 
16
- ## Usage
54
+ ## **API Reference**
17
55
 
56
+ ### **`createEntity`**
57
+
58
+ Creates an entity factory function that allows defining fields and optional methods for an entity. The returned factory provides a `create` method to instantiate entities with the specified fields and methods, while also supporting default values and runtime property manipulation.
59
+
60
+ #### **Type Parameters**
61
+ - `C`: The configuration type for the entity fields.
62
+ - `D`: The type of the methods defined for the entity.
63
+
64
+ #### **Parameters**
65
+ 1. **`fields`**:
66
+ An object defining the fields of the entity. Each field should include its configuration and a method to retrieve its default value.
67
+ **Type**: `{ [key: string]: Field<unknown> }`
68
+
69
+ 2. **`methodDefinitionFunction`** *(optional)*:
70
+ A function that defines additional methods for the entity. It receives an object with `set` and `get` functions to manipulate the entity's properties.
71
+ **Type**:
72
+ ```ts
73
+ (params: {
74
+ set: <K extends keyof C>(key: K, value: EntityConfigTypeResolver<C>[K]) => void;
75
+ get: <K extends keyof C>(key: K) => EntityConfigTypeResolver<C>[K];
76
+ }) => D
77
+ ```
78
+
79
+ #### **Returns**
80
+ An object with a `create` method. The `create` method accepts an input object to initialize the entity's properties and returns an entity instance with the defined fields, methods, and utility functions (`get`, `set`, `toJSON`).
81
+
82
+ #### **Example**
18
83
  ```typescript
19
- import {createEntity, number, string, boolean} from 'simplentity';
20
-
21
- // Define a user entity
22
- const userFactory = createEntity({
23
- id: string().defaultFn(() => randomUUID()), // randomUUID is a third-party library. Not included.
24
- name: string(),
25
- age: number().notRequired(),
26
- isActive: boolean().default(true),
27
- }, ({ set }) => ({
28
- activate: () => {
29
- // You can get suggestions for the properties of the entity.
30
- set('isActive', true)
84
+ const userFactory = createEntity(
85
+ {
86
+ name: string(),
87
+ age: number().default(18),
88
+ isActive: boolean().default(true),
31
89
  },
32
- deactivate: () => {
33
- set('isActive', false)
34
- }
35
- }))
36
-
37
- // Properties that have NotRequired or Default(Fn) are optional.
38
- // If a property has a default value, it is automatically assigned to the property when you create the entity instance.
39
- const user = userFactory.create({
40
- name: 'John Doe',
41
- })
42
- /*
43
- {
44
- id: 'd02daa87-3984-4c6f-be9b-a13a478354b5',
45
- name: 'John Doe',
46
- isActive: true,
47
- }
48
- */
49
-
50
- // You can get property values via the get method.
51
- // Of course, you can get suggestions for the properties of the entity.
52
- const name = user.get('name'); // 'John Doe'
90
+ ({ set, get }) => ({
91
+ incrementAge: () => set("age", get("age") + 1),
92
+ })
93
+ );
94
+
95
+ const user = userFactory.create({ name: "John" });
96
+ console.log(user.toJSON()); // { name: "John", age: 18, isActive: true }
97
+ user.incrementAge();
98
+ console.log(user.get("age")); // 19
53
99
  ```
54
100
 
55
- ## Todo
56
- - [ ] Add more built-in types
57
- - [ ] Validation
58
- - [ ] Custom field types
59
- - [ ] Custom field validation
60
- - [ ] Custom field serialization
101
+ ---
102
+
103
+ ### **Field Types**
104
+
105
+ #### **`string()`**
106
+ Creates a string field.
107
+ **Returns**: `Field<string>`
108
+
109
+ #### **`number()`**
110
+ Creates a number field.
111
+ **Returns**: `Field<number>`
112
+
113
+ #### **`boolean()`**
114
+ Creates a boolean field.
115
+ **Returns**: `Field<boolean>`
116
+
117
+ #### **`date()`**
118
+ Creates a date field.
119
+ **Returns**: `Field<Date>`
120
+
121
+ #### **`entity<T>()`**
122
+ Creates an entity field that references another entity.
123
+ **Returns**: `Field<T>`
124
+
125
+ #### **`array<T>()`**
126
+ Creates an array field.
127
+ **Returns**: `Field<T[]>`
128
+
129
+ ---
130
+
131
+ ### **Field Methods**
132
+
133
+ All fields inherit the following methods:
134
+
135
+ #### **`.notRequired()`**
136
+ Marks the field as optional.
137
+ **Returns**: `Field<T>`
138
+
139
+ #### **`.default(value: T)`**
140
+ Sets a default value for the field.
141
+ **Returns**: `Field<T>`
142
+
143
+ #### **`.defaultFn(fn: () => T)`**
144
+ Sets a default value for the field using a function.
145
+ **Returns**: `Field<T>`
146
+
147
+ ---
148
+
149
+ ### **Entity Instance Methods**
150
+
151
+ Entities created by `createEntity` have the following methods:
152
+
153
+ #### **`.get(key: string)`**
154
+ Retrieves the value of a property.
155
+ **Parameters**:
156
+ - `key`: The property name.
157
+ **Returns**: The value of the property.
158
+
159
+ #### **`.toJSON()`**
160
+ Serializes the entity to a plain object.
161
+ **Returns**: `{ [key: string]: any }`
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "simplentity",
3
- "version": "1.0.0-alpha.1",
3
+ "version": "1.0.0-alpha.3",
4
4
  "description": "A type-safe entity library for TypeScript.",
5
5
  "type": "module",
6
6
  "module": "./dist/index.js",