@cheetah.js/orm 0.1.28 → 0.1.30
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/dist/SqlBuilder.js +76 -23
- package/dist/SqlBuilder.js.map +1 -1
- package/dist/bun/index.js +1898 -1537
- package/dist/bun/index.js.map +26 -18
- package/dist/decorators/one-many.decorator.js +9 -2
- package/dist/decorators/one-many.decorator.js.map +1 -1
- package/dist/decorators/property.decorator.d.ts +1 -0
- package/dist/decorators/property.decorator.js +2 -1
- package/dist/decorators/property.decorator.js.map +1 -1
- package/dist/domain/entities.d.ts +1 -0
- package/dist/domain/entities.js +12 -5
- package/dist/domain/entities.js.map +1 -1
- package/dist/driver/driver.interface.d.ts +1 -0
- package/dist/driver/pg-driver.js +1 -1
- package/dist/driver/pg-driver.js.map +1 -1
- package/dist/utils.d.ts +1 -0
- package/dist/utils.js +4 -0
- package/dist/utils.js.map +1 -1
- package/package.json +1 -1
- package/test/domain/base-entity.spec.ts +0 -548
- package/test/domain/enum.spec.ts +0 -55
- package/test/domain/hooks.spec.ts +0 -160
- package/test/domain/relationship.spec.ts +0 -173
- package/test/domain/serialize.spec.ts +0 -44
- package/test/migration/cheetah.config.ts +0 -13
- package/test/migration/migrator.spec.ts +0 -412
- package/test/node-database.ts +0 -30
|
@@ -1,160 +0,0 @@
|
|
|
1
|
-
import { afterEach, beforeEach, describe, expect, it, jest } from 'bun:test';
|
|
2
|
-
import { execute, mockLogger, purgeDatabase, startDatabase } from '../node-database';
|
|
3
|
-
import {
|
|
4
|
-
AfterCreate, AfterUpdate,
|
|
5
|
-
BaseEntity,
|
|
6
|
-
BeforeCreate,
|
|
7
|
-
BeforeUpdate,
|
|
8
|
-
Entity,
|
|
9
|
-
ManyToOne,
|
|
10
|
-
OneToMany,
|
|
11
|
-
PrimaryKey,
|
|
12
|
-
Property,
|
|
13
|
-
} from '../../src';
|
|
14
|
-
|
|
15
|
-
describe('hooks', () => {
|
|
16
|
-
|
|
17
|
-
beforeEach(async () => {
|
|
18
|
-
await startDatabase();
|
|
19
|
-
await execute(DLL);
|
|
20
|
-
await execute(DDL_ADDRESS);
|
|
21
|
-
await execute(DDL_STREET);
|
|
22
|
-
})
|
|
23
|
-
|
|
24
|
-
afterEach(async () => {
|
|
25
|
-
await purgeDatabase();
|
|
26
|
-
(mockLogger as jest.Mock).mockClear();
|
|
27
|
-
})
|
|
28
|
-
|
|
29
|
-
const DLL = `
|
|
30
|
-
CREATE TABLE "user"
|
|
31
|
-
(
|
|
32
|
-
"id" SERIAL PRIMARY KEY,
|
|
33
|
-
"email" varchar(255) NOT NULL
|
|
34
|
-
);
|
|
35
|
-
`;
|
|
36
|
-
|
|
37
|
-
const DDL_ADDRESS = `
|
|
38
|
-
CREATE TABLE "address"
|
|
39
|
-
(
|
|
40
|
-
"id" SERIAL PRIMARY KEY,
|
|
41
|
-
"address" varchar(255) NOT NULL,
|
|
42
|
-
"user" integer REFERENCES "user" ("id")
|
|
43
|
-
);
|
|
44
|
-
`;
|
|
45
|
-
|
|
46
|
-
const DDL_STREET = `
|
|
47
|
-
CREATE TABLE "street"
|
|
48
|
-
(
|
|
49
|
-
"id" SERIAL PRIMARY KEY,
|
|
50
|
-
"street" varchar(255) NOT NULL,
|
|
51
|
-
"address" integer REFERENCES "address" ("id")
|
|
52
|
-
);
|
|
53
|
-
`;
|
|
54
|
-
|
|
55
|
-
@Entity()
|
|
56
|
-
class User extends BaseEntity {
|
|
57
|
-
@PrimaryKey()
|
|
58
|
-
id: number;
|
|
59
|
-
|
|
60
|
-
@Property()
|
|
61
|
-
email: string;
|
|
62
|
-
|
|
63
|
-
@OneToMany(() => Address, (address) => address.user)
|
|
64
|
-
addresses: Address[];
|
|
65
|
-
|
|
66
|
-
@BeforeCreate()
|
|
67
|
-
onBeforeCreate() {
|
|
68
|
-
this.id = 10
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
@BeforeUpdate()
|
|
72
|
-
onBeforeUpdate() {
|
|
73
|
-
this.email = 'otherEmail@test.com'
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
@Entity({tableName: 'user'})
|
|
78
|
-
class User2 extends BaseEntity {
|
|
79
|
-
@PrimaryKey()
|
|
80
|
-
id: number;
|
|
81
|
-
|
|
82
|
-
@Property()
|
|
83
|
-
email: string;
|
|
84
|
-
|
|
85
|
-
@OneToMany(() => Address, (address) => address.user)
|
|
86
|
-
addresses: Address[];
|
|
87
|
-
|
|
88
|
-
@AfterCreate()
|
|
89
|
-
hook() {
|
|
90
|
-
this.id = 10
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
@AfterUpdate()
|
|
94
|
-
hook2() {
|
|
95
|
-
this.email = 'AfterUpdate@test.com';
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
@Entity()
|
|
100
|
-
class Address extends BaseEntity {
|
|
101
|
-
@PrimaryKey()
|
|
102
|
-
id: number;
|
|
103
|
-
|
|
104
|
-
@Property()
|
|
105
|
-
address: string;
|
|
106
|
-
|
|
107
|
-
@ManyToOne(() => User)
|
|
108
|
-
user: User;
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
@Entity()
|
|
112
|
-
class Street extends BaseEntity {
|
|
113
|
-
@PrimaryKey()
|
|
114
|
-
id: number;
|
|
115
|
-
|
|
116
|
-
@Property()
|
|
117
|
-
street: string;
|
|
118
|
-
|
|
119
|
-
@ManyToOne(() => Address)
|
|
120
|
-
address: Address;
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
it('beforeCreate', async () => {
|
|
124
|
-
Entity()(User);
|
|
125
|
-
|
|
126
|
-
const user = await User.create({email: 'test@test.com', id: 1})
|
|
127
|
-
|
|
128
|
-
expect(user.id).toBe(10)
|
|
129
|
-
})
|
|
130
|
-
|
|
131
|
-
it('should afterCreate', async () => {
|
|
132
|
-
Entity({tableName: 'user'})(User2);
|
|
133
|
-
|
|
134
|
-
const user = await User2.create({email: 'test@test.com', id: 1})
|
|
135
|
-
|
|
136
|
-
expect(user.id).toBe(10)
|
|
137
|
-
});
|
|
138
|
-
|
|
139
|
-
it('should beforeUpdate', async () => {
|
|
140
|
-
Entity()(User);
|
|
141
|
-
|
|
142
|
-
const user = await User.create({email: 'test@test.com', id: 1})
|
|
143
|
-
expect(user.id).toBe(10)
|
|
144
|
-
|
|
145
|
-
user.email = 'testUpdated@test.com'
|
|
146
|
-
await user.save()
|
|
147
|
-
expect(user.email).toBe('otherEmail@test.com')
|
|
148
|
-
});
|
|
149
|
-
|
|
150
|
-
it('should afterUpdate', async () => {
|
|
151
|
-
Entity({tableName: 'user'})(User2);
|
|
152
|
-
|
|
153
|
-
const user = await User2.create({email: 'test@test.com', id: 1})
|
|
154
|
-
expect(user.id).toBe(10)
|
|
155
|
-
|
|
156
|
-
user.email = 'updateEmail@test.com';
|
|
157
|
-
await user.save()
|
|
158
|
-
expect(user.email).toBe('AfterUpdate@test.com');
|
|
159
|
-
});
|
|
160
|
-
});
|
|
@@ -1,173 +0,0 @@
|
|
|
1
|
-
import { afterEach, beforeEach, describe, expect, it, jest } from 'bun:test';
|
|
2
|
-
import { BaseEntity, Entity, ManyToOne, OneToMany, PrimaryKey, Property } from '../../src';
|
|
3
|
-
import { execute, mockLogger, purgeDatabase, startDatabase } from '../node-database';
|
|
4
|
-
|
|
5
|
-
describe('Relationship entities', () => {
|
|
6
|
-
|
|
7
|
-
beforeEach(async () => {
|
|
8
|
-
await startDatabase();
|
|
9
|
-
await execute(DLL);
|
|
10
|
-
await execute(DDL_ADDRESS);
|
|
11
|
-
await execute(DDL_STREET);
|
|
12
|
-
})
|
|
13
|
-
|
|
14
|
-
afterEach(async () => {
|
|
15
|
-
await purgeDatabase();
|
|
16
|
-
(mockLogger as jest.Mock).mockClear();
|
|
17
|
-
})
|
|
18
|
-
|
|
19
|
-
const DLL = `
|
|
20
|
-
CREATE TABLE "user"
|
|
21
|
-
(
|
|
22
|
-
"id" SERIAL PRIMARY KEY,
|
|
23
|
-
"email" varchar(255) NOT NULL
|
|
24
|
-
);
|
|
25
|
-
`;
|
|
26
|
-
|
|
27
|
-
const DDL_ADDRESS = `
|
|
28
|
-
CREATE TABLE "address"
|
|
29
|
-
(
|
|
30
|
-
"id" SERIAL PRIMARY KEY,
|
|
31
|
-
"address" varchar(255) NOT NULL,
|
|
32
|
-
"user" integer REFERENCES "user" ("id")
|
|
33
|
-
);
|
|
34
|
-
`;
|
|
35
|
-
|
|
36
|
-
const DDL_STREET = `
|
|
37
|
-
CREATE TABLE "street"
|
|
38
|
-
(
|
|
39
|
-
"id" SERIAL PRIMARY KEY,
|
|
40
|
-
"street" varchar(255) NOT NULL,
|
|
41
|
-
"address" integer REFERENCES "address" ("id")
|
|
42
|
-
);
|
|
43
|
-
`;
|
|
44
|
-
|
|
45
|
-
@Entity()
|
|
46
|
-
class User extends BaseEntity {
|
|
47
|
-
@PrimaryKey()
|
|
48
|
-
id: number;
|
|
49
|
-
|
|
50
|
-
@Property()
|
|
51
|
-
email: string;
|
|
52
|
-
|
|
53
|
-
@OneToMany(() => Address, (address) => address.user)
|
|
54
|
-
addresses: Address[];
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
@Entity()
|
|
58
|
-
class Address extends BaseEntity {
|
|
59
|
-
@PrimaryKey()
|
|
60
|
-
id: number;
|
|
61
|
-
|
|
62
|
-
@Property()
|
|
63
|
-
address: string;
|
|
64
|
-
|
|
65
|
-
@ManyToOne(() => User)
|
|
66
|
-
user: User;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
@Entity()
|
|
70
|
-
class Street extends BaseEntity {
|
|
71
|
-
@PrimaryKey()
|
|
72
|
-
id: number;
|
|
73
|
-
|
|
74
|
-
@Property()
|
|
75
|
-
street: string;
|
|
76
|
-
|
|
77
|
-
@ManyToOne(() => Address)
|
|
78
|
-
address: Address;
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
it('should create a new user with address', async () => {
|
|
82
|
-
|
|
83
|
-
Entity()(User)
|
|
84
|
-
Entity()(Address)
|
|
85
|
-
Entity()(Street)
|
|
86
|
-
|
|
87
|
-
const user = new User();
|
|
88
|
-
user.email = 'test@test.com';
|
|
89
|
-
user.id = 1;
|
|
90
|
-
await user.save();
|
|
91
|
-
|
|
92
|
-
const address = await Address.create({
|
|
93
|
-
id: 1,
|
|
94
|
-
address: 'test address',
|
|
95
|
-
user,
|
|
96
|
-
})
|
|
97
|
-
const street = await Street.create({
|
|
98
|
-
id: 1,
|
|
99
|
-
street: 'test street',
|
|
100
|
-
address,
|
|
101
|
-
})
|
|
102
|
-
|
|
103
|
-
const find = await Street.find({
|
|
104
|
-
address: {
|
|
105
|
-
user: {
|
|
106
|
-
id: 1,
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
});
|
|
110
|
-
|
|
111
|
-
const findWithLoadSelect = await Street.find({
|
|
112
|
-
address: {
|
|
113
|
-
user: {
|
|
114
|
-
id: 1,
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
}, {
|
|
118
|
-
loadStrategy: 'select'
|
|
119
|
-
});
|
|
120
|
-
|
|
121
|
-
expect(find).toHaveLength(1);
|
|
122
|
-
expect(find[0].id).toBe(1);
|
|
123
|
-
expect(find[0].street).toBe('test street');
|
|
124
|
-
expect(find[0].address).toBeInstanceOf(Address);
|
|
125
|
-
expect(find[0].address.user).toBeInstanceOf(User);
|
|
126
|
-
expect(find[0].address.user.email).toBe('test@test.com');
|
|
127
|
-
expect(findWithLoadSelect).toHaveLength(1);
|
|
128
|
-
expect(findWithLoadSelect[0].id).toBe(1);
|
|
129
|
-
expect(findWithLoadSelect[0].street).toBe('test street');
|
|
130
|
-
expect(findWithLoadSelect[0].address).toBeInstanceOf(Address);
|
|
131
|
-
expect(findWithLoadSelect[0].address.user).toBeInstanceOf(User);
|
|
132
|
-
expect(findWithLoadSelect[0].address.user.email).toBe('test@test.com');
|
|
133
|
-
expect(mockLogger).toHaveBeenCalledTimes(7)
|
|
134
|
-
expect((mockLogger as jest.Mock).mock.calls[3][0]).toStartWith("SQL: SELECT s1.\"id\" as \"s1_id\", s1.\"street\" as \"s1_street\", s1.\"address\" as \"s1_address\", u1.\"id\" as \"u1_id\", u1.\"email\" as \"u1_email\", a1.\"id\" as \"a1_id\", a1.\"address\" as \"a1_address\", a1.\"user\" as \"a1_user\" FROM \"public\".\"street\" s1 LEFT JOIN public.address a1 ON s1.\"address\" = a1.\"id\" LEFT JOIN public.user u1 ON a1.\"user\" = u1.\"id\" WHERE (((u1.id = 1)))");
|
|
135
|
-
});
|
|
136
|
-
|
|
137
|
-
it('should load relationship', async () => {
|
|
138
|
-
|
|
139
|
-
Entity()(User)
|
|
140
|
-
Entity()(Address)
|
|
141
|
-
Entity()(Street)
|
|
142
|
-
|
|
143
|
-
const user = new User();
|
|
144
|
-
user.email = 'test@test.com';
|
|
145
|
-
user.id = 1;
|
|
146
|
-
await user.save();
|
|
147
|
-
|
|
148
|
-
const address = await Address.create({
|
|
149
|
-
id: 1,
|
|
150
|
-
address: 'test address',
|
|
151
|
-
user,
|
|
152
|
-
})
|
|
153
|
-
await Street.create({
|
|
154
|
-
id: 1,
|
|
155
|
-
street: 'test street',
|
|
156
|
-
address,
|
|
157
|
-
})
|
|
158
|
-
|
|
159
|
-
const find = await Street.findOneOrFail({}, {
|
|
160
|
-
load: ['address', 'address.user'],
|
|
161
|
-
});
|
|
162
|
-
const find2 = await Street.findOneOrFail({}, {
|
|
163
|
-
load: ['address', 'address.user'],
|
|
164
|
-
loadStrategy: 'select',
|
|
165
|
-
});
|
|
166
|
-
|
|
167
|
-
expect(find).toBeInstanceOf(Street);
|
|
168
|
-
expect(find.id).toBe(find2.id);
|
|
169
|
-
expect(find.street).toBe(find2.street);
|
|
170
|
-
expect(find.address.id).toBe(find2.address.id);
|
|
171
|
-
expect(find.address.user.id).toBe(find2.address.user.id);
|
|
172
|
-
});
|
|
173
|
-
});
|
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
import { afterEach, beforeEach, describe, expect, it, jest } from 'bun:test';
|
|
2
|
-
import { execute, mockLogger, purgeDatabase, startDatabase } from '../node-database';
|
|
3
|
-
import { BaseEntity, Entity, PrimaryKey, Property } from '../../src';
|
|
4
|
-
|
|
5
|
-
describe('serialize', () => {
|
|
6
|
-
beforeEach(async () => {
|
|
7
|
-
await startDatabase();
|
|
8
|
-
await execute(DLL);
|
|
9
|
-
})
|
|
10
|
-
|
|
11
|
-
afterEach(async () => {
|
|
12
|
-
await purgeDatabase();
|
|
13
|
-
(mockLogger as jest.Mock).mockClear();
|
|
14
|
-
})
|
|
15
|
-
|
|
16
|
-
const DLL = `
|
|
17
|
-
CREATE TABLE "user"
|
|
18
|
-
(
|
|
19
|
-
"id" SERIAL PRIMARY KEY,
|
|
20
|
-
"email" varchar(255) NOT NULL
|
|
21
|
-
);
|
|
22
|
-
`;
|
|
23
|
-
|
|
24
|
-
@Entity()
|
|
25
|
-
class User extends BaseEntity {
|
|
26
|
-
@PrimaryKey()
|
|
27
|
-
id: number;
|
|
28
|
-
|
|
29
|
-
@Property({ hidden: true })
|
|
30
|
-
email: string;
|
|
31
|
-
|
|
32
|
-
propertyHidden = 'propertyHidden';
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
it('should serialize', async () => {
|
|
36
|
-
Entity()(User);
|
|
37
|
-
|
|
38
|
-
const user = new User();
|
|
39
|
-
user.email = 'test@test.com';
|
|
40
|
-
user.id = 1;
|
|
41
|
-
|
|
42
|
-
expect(JSON.stringify(user)).toEqual('{"id":1}')
|
|
43
|
-
});
|
|
44
|
-
});
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import { PgDriver } from '@cheetah.js/orm';
|
|
2
|
-
|
|
3
|
-
const config = {
|
|
4
|
-
host: 'localhost',
|
|
5
|
-
port: 5432,
|
|
6
|
-
database: 'postgres',
|
|
7
|
-
username: 'postgres',
|
|
8
|
-
password: 'postgres',
|
|
9
|
-
driver: PgDriver,
|
|
10
|
-
migrationPath: '/packages/orm/test/migration'
|
|
11
|
-
};
|
|
12
|
-
|
|
13
|
-
export default config;
|