@cheetah.js/orm 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/README.md +228 -0
- package/build.ts +14 -0
- package/cheetah.config.ts +14 -0
- package/dist/SqlBuilder.d.ts +57 -0
- package/dist/SqlBuilder.js +436 -0
- package/dist/SqlBuilder.js.map +1 -0
- package/dist/bun/index.d.ts +13 -0
- package/dist/bun/index.js +224319 -0
- package/dist/bun/index.js.map +306 -0
- package/dist/cheetah.d.ts +1 -0
- package/dist/cheetah.js +24 -0
- package/dist/cheetah.js.map +1 -0
- package/dist/constants.d.ts +4 -0
- package/dist/constants.js +5 -0
- package/dist/constants.js.map +1 -0
- package/dist/decorators/entity.decorator.d.ts +3 -0
- package/dist/decorators/entity.decorator.js +10 -0
- package/dist/decorators/entity.decorator.js.map +1 -0
- package/dist/decorators/index.decorator.d.ts +3 -0
- package/dist/decorators/index.decorator.js +17 -0
- package/dist/decorators/index.decorator.js.map +1 -0
- package/dist/decorators/one-many.decorator.d.ts +3 -0
- package/dist/decorators/one-many.decorator.js +17 -0
- package/dist/decorators/one-many.decorator.js.map +1 -0
- package/dist/decorators/primary-key.decorator.d.ts +2 -0
- package/dist/decorators/primary-key.decorator.js +6 -0
- package/dist/decorators/primary-key.decorator.js.map +1 -0
- package/dist/decorators/property.decorator.d.ts +15 -0
- package/dist/decorators/property.decorator.js +25 -0
- package/dist/decorators/property.decorator.js.map +1 -0
- package/dist/domain/base-entity.d.ts +42 -0
- package/dist/domain/base-entity.js +106 -0
- package/dist/domain/base-entity.js.map +1 -0
- package/dist/domain/collection.d.ts +6 -0
- package/dist/domain/collection.js +11 -0
- package/dist/domain/collection.js.map +1 -0
- package/dist/domain/entities.d.ts +40 -0
- package/dist/domain/entities.js +137 -0
- package/dist/domain/entities.js.map +1 -0
- package/dist/domain/reference.d.ts +4 -0
- package/dist/domain/reference.js +7 -0
- package/dist/domain/reference.js.map +1 -0
- package/dist/driver/driver.interface.d.ts +270 -0
- package/dist/driver/driver.interface.js +2 -0
- package/dist/driver/driver.interface.js.map +1 -0
- package/dist/driver/pg-driver.d.ts +43 -0
- package/dist/driver/pg-driver.js +255 -0
- package/dist/driver/pg-driver.js.map +1 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.js +16 -0
- package/dist/index.js.map +1 -0
- package/dist/migration/diff-calculator.d.ts +17 -0
- package/dist/migration/diff-calculator.js +230 -0
- package/dist/migration/diff-calculator.js.map +1 -0
- package/dist/migration/migrator.d.ts +18 -0
- package/dist/migration/migrator.js +233 -0
- package/dist/migration/migrator.js.map +1 -0
- package/dist/orm.d.ts +14 -0
- package/dist/orm.js +23 -0
- package/dist/orm.js.map +1 -0
- package/dist/orm.service.d.ts +8 -0
- package/dist/orm.service.js +115 -0
- package/dist/orm.service.js.map +1 -0
- package/dist/utils.d.ts +1 -0
- package/dist/utils.js +16 -0
- package/dist/utils.js.map +1 -0
- package/package.json +50 -0
- package/src/SqlBuilder.ts +542 -0
- package/src/cheetah.ts +28 -0
- package/src/constants.ts +4 -0
- package/src/decorators/entity.decorator.ts +10 -0
- package/src/decorators/index.decorator.ts +18 -0
- package/src/decorators/one-many.decorator.ts +19 -0
- package/src/decorators/primary-key.decorator.ts +6 -0
- package/src/decorators/property.decorator.ts +41 -0
- package/src/domain/base-entity.ts +149 -0
- package/src/domain/collection.ts +10 -0
- package/src/domain/entities.ts +159 -0
- package/src/domain/reference.ts +5 -0
- package/src/driver/driver.interface.ts +331 -0
- package/src/driver/pg-driver.ts +308 -0
- package/src/index.ts +17 -0
- package/src/migration/diff-calculator.ts +258 -0
- package/src/migration/migrator.ts +278 -0
- package/src/orm.service.ts +115 -0
- package/src/orm.ts +30 -0
- package/src/utils.ts +18 -0
- package/test/domain/base-entity.spec.ts +463 -0
- package/test/migration/.sql +5 -0
- package/test/migration/cheetah.config.ts +13 -0
- package/test/migration/migator.spec.ts +251 -0
- package/test/migration/test.sql +5 -0
- package/test/node-database.ts +32 -0
package/README.md
ADDED
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
# Cheetah.js ORM
|
|
2
|
+
Cheetah.js ORM is a simple and powerful ORM for Cheetah.js and Bun.
|
|
3
|
+
<br>We don't use any query builder like knex, we have our own query builder making us faster.
|
|
4
|
+
**In development.**
|
|
5
|
+
|
|
6
|
+
### Menu
|
|
7
|
+
- [Installation](#install)
|
|
8
|
+
- [Entities](#entities)
|
|
9
|
+
- [Usage](#usage)
|
|
10
|
+
|
|
11
|
+
### [Installation](#install)
|
|
12
|
+
For install Cheetah.js ORM, run the command below:
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
bun install @cheetah.js/orm
|
|
16
|
+
```
|
|
17
|
+
Create a configuration file for the ORM in the root of the project called "cheetah.config.ts" and configure the database connection, providers and entities:
|
|
18
|
+
|
|
19
|
+
```javascript
|
|
20
|
+
import { PgDriver } from '@cheetah.js/orm';
|
|
21
|
+
import { ConnectionSettings } from '@cheetah.js/orm/driver/driver.interface';
|
|
22
|
+
|
|
23
|
+
const config: ConnectionSettings<any> = {
|
|
24
|
+
host: 'localhost',
|
|
25
|
+
port: 5432,
|
|
26
|
+
database: 'postgres',
|
|
27
|
+
username: 'postgres',
|
|
28
|
+
password: 'postgres',
|
|
29
|
+
driver: PgDriver,
|
|
30
|
+
migrationPath: 'path_migrations',
|
|
31
|
+
entities: 'entity/*.ts' // or [User, Post, ...]
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
export default config;
|
|
35
|
+
```
|
|
36
|
+
Actually, the ORM only supports PostgreSQL, but in the future it will support other databases.
|
|
37
|
+
- Entities: Path to entities. Accepts glob patterns or an array of Entity classes.
|
|
38
|
+
- MigrationPath: Path to migrations. Accepts glob patterns. Is optional.
|
|
39
|
+
<br/>
|
|
40
|
+
<br/>
|
|
41
|
+
After that, you need to import the ORM into the project and add it to the Cheetah.js instance:
|
|
42
|
+
|
|
43
|
+
```javascript
|
|
44
|
+
import { Cheetah } from '@cheetah.js/core';
|
|
45
|
+
import { CheetahOrm } from '@cheetah.js/orm';
|
|
46
|
+
|
|
47
|
+
new Cheetah().use(CheetahOrm).listen();
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
### [Entities](#entities)
|
|
51
|
+
Entities are classes that map to database tables. Each entity must have a primary key.
|
|
52
|
+
|
|
53
|
+
#### Example:
|
|
54
|
+
```javascript
|
|
55
|
+
import { Entity, PrimaryKey, Property } from '@cheetah.js/orm';
|
|
56
|
+
|
|
57
|
+
@Entity()
|
|
58
|
+
export class User {
|
|
59
|
+
@PrimaryKey()
|
|
60
|
+
id: number;
|
|
61
|
+
|
|
62
|
+
@Property()
|
|
63
|
+
name: string;
|
|
64
|
+
}
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
#### PrimaryKey
|
|
68
|
+
The @PrimaryKey decorator is used to define the primary key of the entity.
|
|
69
|
+
|
|
70
|
+
#### Nullable property
|
|
71
|
+
For define a nullable property, add a parameter to the @Property decorator:
|
|
72
|
+
|
|
73
|
+
```javascript
|
|
74
|
+
@Entity()
|
|
75
|
+
export class User {
|
|
76
|
+
@PrimaryKey()
|
|
77
|
+
id: number;
|
|
78
|
+
|
|
79
|
+
@Property({ nullable: true })
|
|
80
|
+
name: string;
|
|
81
|
+
}
|
|
82
|
+
```
|
|
83
|
+
Cheetah ORM can also distinguish nullable properties automatically by adding the question mark to the end of the property name:
|
|
84
|
+
|
|
85
|
+
```javascript
|
|
86
|
+
export class User {
|
|
87
|
+
@PrimaryKey()
|
|
88
|
+
id: number;
|
|
89
|
+
|
|
90
|
+
@Property()
|
|
91
|
+
name?:string;
|
|
92
|
+
}
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
#### Unique property
|
|
96
|
+
For define a unique property, add a parameter to the @Property decorator:
|
|
97
|
+
|
|
98
|
+
```javascript
|
|
99
|
+
@Entity()
|
|
100
|
+
export class User {
|
|
101
|
+
@PrimaryKey()
|
|
102
|
+
id: number;
|
|
103
|
+
|
|
104
|
+
@Property({ unique: true })
|
|
105
|
+
name: string;
|
|
106
|
+
}
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
#### Index property
|
|
110
|
+
For define a index for a unique property, add a parameter to the @Property decorator:
|
|
111
|
+
|
|
112
|
+
```javascript
|
|
113
|
+
@Entity()
|
|
114
|
+
export class User {
|
|
115
|
+
@PrimaryKey()
|
|
116
|
+
id: number;
|
|
117
|
+
|
|
118
|
+
@Property({ index: true })
|
|
119
|
+
name: string;
|
|
120
|
+
}
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
For define a index for a multiple properties, add the @Index decorator:
|
|
124
|
+
|
|
125
|
+
```javascript
|
|
126
|
+
@Entity()
|
|
127
|
+
export class User {
|
|
128
|
+
@PrimaryKey()
|
|
129
|
+
id: number;
|
|
130
|
+
|
|
131
|
+
@Property()
|
|
132
|
+
name: string;
|
|
133
|
+
|
|
134
|
+
@Index(['name', 'email'])
|
|
135
|
+
@Property()
|
|
136
|
+
email: string;
|
|
137
|
+
}
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
#### Property options
|
|
141
|
+
| Option | Type | Description |
|
|
142
|
+
| ------ | ---- |--------------------------------------------------------------------------------------------|
|
|
143
|
+
| nullable | boolean | Defines if the property is nullable. |
|
|
144
|
+
| unique | boolean | Defines if the property is unique. |
|
|
145
|
+
| index | boolean | Defines if the property is index. |
|
|
146
|
+
| default | any | Defines the default value of the property. |
|
|
147
|
+
| length | number | Defines the length of the property. |
|
|
148
|
+
| onUpdate | string | Define the action to be taken for this property when updating the entity in the database |
|
|
149
|
+
| onInsert | string | Defines the action to be taken for this property when inserting the entity in the database |
|
|
150
|
+
|
|
151
|
+
#### Relations
|
|
152
|
+
Cheetah ORM supports relations between entities. The available relations are: OneToMany, ManyToOne.
|
|
153
|
+
|
|
154
|
+
##### OneToMany
|
|
155
|
+
The OneToMany relation is used to define a one-to-many relationship between two entities. For example, a user can have multiple posts, but a post can only have one user.
|
|
156
|
+
|
|
157
|
+
```javascript
|
|
158
|
+
@Entity()
|
|
159
|
+
export class User {
|
|
160
|
+
@PrimaryKey()
|
|
161
|
+
id: number;
|
|
162
|
+
|
|
163
|
+
@Property()
|
|
164
|
+
name: string;
|
|
165
|
+
|
|
166
|
+
@OneToMany(() => Post, (post) => post.user)
|
|
167
|
+
posts: Post[];
|
|
168
|
+
}
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
#### ManyToOne
|
|
172
|
+
The owner side of the relation is the side that has the @ManyToOne decorator. The inverse side is the side that has the @OneToMany decorator. The owner side is always the side that has the foreign key.
|
|
173
|
+
|
|
174
|
+
```javascript
|
|
175
|
+
@Entity()
|
|
176
|
+
export class Post {
|
|
177
|
+
@PrimaryKey()
|
|
178
|
+
id: number;
|
|
179
|
+
|
|
180
|
+
@Property()
|
|
181
|
+
title: string;
|
|
182
|
+
|
|
183
|
+
@ManyToOne(() => User)
|
|
184
|
+
user: User;
|
|
185
|
+
}
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
### [Usage](#usage)
|
|
189
|
+
#### Create a new entity
|
|
190
|
+
```javascript
|
|
191
|
+
import { User } from './entity/user';
|
|
192
|
+
|
|
193
|
+
const user = User.create({ name: 'John Doe' });
|
|
194
|
+
|
|
195
|
+
// OR
|
|
196
|
+
const user = new User();
|
|
197
|
+
user.name = 'John Doe';
|
|
198
|
+
await user.save();
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
#### Find a entity
|
|
202
|
+
```javascript
|
|
203
|
+
import { User } from './entity/user';
|
|
204
|
+
|
|
205
|
+
const user = await User.findOne({
|
|
206
|
+
name: 'John Doe',
|
|
207
|
+
old: { $gte: 16, $lte: 30 }
|
|
208
|
+
});
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
#### List of supported operators
|
|
212
|
+
| Operator |Name | Description |
|
|
213
|
+
| ------ | ---- |--------------------------------------------------------------------------------------------|
|
|
214
|
+
| $eq | Equal | Matches values that are equal to a specified value. |
|
|
215
|
+
| $gt | Greater Than | Matches values that are greater than a specified value. |
|
|
216
|
+
| $gte | Greater Than or Equal | Matches values that are greater than or equal to a specified value. |
|
|
217
|
+
| $in | In | Matches any of the values specified in an array. |
|
|
218
|
+
| $lt | Less Than | Matches values that are less than a specified value. |
|
|
219
|
+
| $lte | Less Than or Equal | Matches values that are less than or equal to a specified value. |
|
|
220
|
+
| $ne | Not Equal | Matches all values that are not equal to a specified value. |
|
|
221
|
+
| $nin | Not In | Matches none of the values specified in an array. |
|
|
222
|
+
| $and | And | Joins query clauses with a logical AND returns all documents that match the conditions of both clauses. |
|
|
223
|
+
| $or | Or | Joins query clauses with a logical OR returns all documents that match the conditions of either clause. |
|
|
224
|
+
| $not | Not | Inverts the effect of a query expression and returns documents that do not match the query expression. |
|
|
225
|
+
|
|
226
|
+
|
|
227
|
+
|
|
228
|
+
|
package/build.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { copyFileSync } from 'fs'
|
|
2
|
+
|
|
3
|
+
await Bun.build({
|
|
4
|
+
entrypoints: ['./src/index.ts'],
|
|
5
|
+
outdir: './dist/bun',
|
|
6
|
+
minify: false,
|
|
7
|
+
target: 'bun',
|
|
8
|
+
sourcemap: 'external',
|
|
9
|
+
splitting: true
|
|
10
|
+
})
|
|
11
|
+
|
|
12
|
+
copyFileSync('dist/index.d.ts', 'dist/bun/index.d.ts')
|
|
13
|
+
|
|
14
|
+
export {}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { PgDriver } from '@cheetah.js/orm';
|
|
2
|
+
import { ConnectionSettings } from '@cheetah.js/orm/driver/driver.interface';
|
|
3
|
+
|
|
4
|
+
const config: ConnectionSettings<any> = {
|
|
5
|
+
host: 'localhost',
|
|
6
|
+
port: 5432,
|
|
7
|
+
database: 'postgres',
|
|
8
|
+
username: 'postgres',
|
|
9
|
+
password: 'postgres',
|
|
10
|
+
driver: PgDriver,
|
|
11
|
+
migrationPath: '/packages/orm/test/migration'
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export default config;
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { AutoPath, FilterQuery, QueryOrderMap } from './driver/driver.interface';
|
|
2
|
+
export declare class SqlBuilder<T> {
|
|
3
|
+
private readonly driver;
|
|
4
|
+
private entityStorage;
|
|
5
|
+
private statements;
|
|
6
|
+
private entity;
|
|
7
|
+
private model;
|
|
8
|
+
private aliases;
|
|
9
|
+
private lastKeyNotOperator;
|
|
10
|
+
private logger;
|
|
11
|
+
private updatedColumns;
|
|
12
|
+
constructor(model: new () => T);
|
|
13
|
+
select(columns?: AutoPath<T, never, '*'>[]): SqlBuilder<T>;
|
|
14
|
+
insert(values: Partial<T>): SqlBuilder<T>;
|
|
15
|
+
update(values: Partial<T>): SqlBuilder<T>;
|
|
16
|
+
where(where: FilterQuery<T>): SqlBuilder<T>;
|
|
17
|
+
orderBy(orderBy: (QueryOrderMap<T> & {
|
|
18
|
+
0?: never;
|
|
19
|
+
}) | QueryOrderMap<T>[]): SqlBuilder<T>;
|
|
20
|
+
limit(limit: number | undefined): SqlBuilder<T>;
|
|
21
|
+
offset(offset: number | undefined): SqlBuilder<T>;
|
|
22
|
+
executeAndReturnFirst(): Promise<T | undefined>;
|
|
23
|
+
executeAndReturnFirstOrFail(): Promise<T>;
|
|
24
|
+
executeAndReturnAll(): Promise<T[]>;
|
|
25
|
+
execute(): Promise<{
|
|
26
|
+
query: any;
|
|
27
|
+
startTime: number;
|
|
28
|
+
sql: string;
|
|
29
|
+
}>;
|
|
30
|
+
startTransaction(): Promise<void>;
|
|
31
|
+
commit(): Promise<void>;
|
|
32
|
+
rollback(): Promise<void>;
|
|
33
|
+
inTransaction<T>(callback: (builder: SqlBuilder<T>) => Promise<T>): Promise<T>;
|
|
34
|
+
private objectToStringMap;
|
|
35
|
+
private discoverColumnAlias;
|
|
36
|
+
private getTableName;
|
|
37
|
+
private addSimpleConditionToSql;
|
|
38
|
+
private addInConditionToSql;
|
|
39
|
+
private addLogicalOperatorToSql;
|
|
40
|
+
private conditionToSql;
|
|
41
|
+
private t;
|
|
42
|
+
private applyJoin;
|
|
43
|
+
private getFkKey;
|
|
44
|
+
private getEntity;
|
|
45
|
+
private transformToModel;
|
|
46
|
+
/**
|
|
47
|
+
* Retrieves an alias for a given table name.
|
|
48
|
+
*
|
|
49
|
+
* @param {string} tableName - The name of the table.
|
|
50
|
+
* @private
|
|
51
|
+
* @returns {string} - The alias for the table name.
|
|
52
|
+
*/
|
|
53
|
+
private getAlias;
|
|
54
|
+
private getColumnsEntity;
|
|
55
|
+
private withDefaultValues;
|
|
56
|
+
private withUpdatedValues;
|
|
57
|
+
}
|