@cheetah.js/orm 0.1.4 → 0.1.6

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 CHANGED
@@ -6,7 +6,9 @@ Cheetah.js ORM is a simple and powerful ORM for Cheetah.js and Bun.
6
6
  ### Menu
7
7
  - [Installation](#install)
8
8
  - [Entities](#entities)
9
+ - [Value Objects](#value-objects)
9
10
  - [Usage](#usage)
11
+ - [Migrations](#migrations)
10
12
 
11
13
  ### [Installation](#install)
12
14
  For install Cheetah.js ORM, run the command below:
@@ -148,6 +150,58 @@ export class User {
148
150
  | onUpdate | string | Define the action to be taken for this property when updating the entity in the database |
149
151
  | onInsert | string | Defines the action to be taken for this property when inserting the entity in the database |
150
152
 
153
+ #### Value Objects
154
+ A Value Object is an immutable type that is distinguishable only by the state of its properties. That is, unlike an Entity, which has a unique identifier and remains distinct even if its properties are otherwise identical, two Value Objects with the exact same properties can be considered equal.
155
+ Cheetah ORM Entities support Value Objects. To define a Value Object, extends the ValueObject class:
156
+
157
+ ```javascript
158
+ import { ValueObject } from '@cheetah.js/orm';
159
+
160
+ export class Name extends ValueObject<string, Name> { // First type is a value scalar type,
161
+ // and second is a ValueObject
162
+
163
+ validate(value): boolean {
164
+ return value.length > 0; // Any validation
165
+ }
166
+ }
167
+
168
+ const name = new Name('John Doe');
169
+ const name2 = Name.from('John Doe'); // Same as above
170
+
171
+ console.log(name.equals(name2)); // true
172
+
173
+ ```
174
+ Is Required to implement the validate method, that returns a boolean value.
175
+ To use the Value Object in the Entity, just add the ValueObject type to the property:
176
+
177
+ ```javascript
178
+ import { Entity, PrimaryKey, Property } from '@cheetah.js/orm';
179
+
180
+ @Entity()
181
+ export class User {
182
+ @PrimaryKey()
183
+ id: number;
184
+
185
+ @Property()
186
+ name: Name;
187
+ }
188
+ ```
189
+ Cheetah ORM will automatically convert the Value Object to the database type and vice versa.<br>
190
+ Important: If you value object is different from string type, you need to define the database type in the @Property decorator, because the Cheetah ORM would not know the correct type of your value object:
191
+
192
+ ```javascript
193
+ import { Entity, PrimaryKey, Property } from '@cheetah.js/orm';
194
+
195
+ @Entity()
196
+ export class User {
197
+ @PrimaryKey()
198
+ id: number;
199
+
200
+ @Property({ type: 'json' })
201
+ name: Name;
202
+ }
203
+ ```
204
+
151
205
  #### Relations
152
206
  Cheetah ORM supports relations between entities. The available relations are: OneToMany, ManyToOne.
153
207