-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
85 lines (73 loc) · 2.09 KB
/
index.ts
1
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
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/**
* To generate client and typings: ormless -c example/ormless.config.json
*
* The purpose of this example is to show api usage.
* For real world example, please refer to https://github.com/koskimas/kysely/tree/master/example
*/
/* eslint-disable @typescript-eslint/no-non-null-assertion */
import * as dotnev from 'dotenv';
import { CamelCasePlugin, Kysely } from 'kysely';
import { CustomerRepository, Database, DatabaseSchema } from './database';
dotnev.config();
main();
async function main() {
const db = new Kysely<DatabaseSchema>({
database: process.env.DATABASE_NAME!,
host: process.env.DATABASE_HOST!,
user: process.env.DATABASE_USER!,
password: process.env.DATABASE_PASSWORD!,
dialect: 'postgres',
plugins: [new CamelCasePlugin()],
});
const customerRepo = new CustomerRepository();
const select: Database.Customer.Column[] = ['active', 'activebool', 'lastName', 'firstName'];
const createdCustomer = await customerRepo.createOne({
db,
select,
data: {
customerId: -1,
addressId: 1,
email: '[email protected]',
firstName: '',
lastName: '',
storeId: 1,
active: 0,
activebool: false,
createDate: new Date(),
lastUpdate: new Date(),
},
});
// select unique customer by primary key
const fetchedCustomer = await customerRepo.selectOne({
db,
select,
where: { customerPkey: { customerId: -1 } },
});
// update unique customer by unique key
const updatedCustomer = await customerRepo.updateOne({
db,
select,
where: { customerEmailUk: { email: '[email protected]' } },
data: { activebool: true },
});
const deletedCustomer = await customerRepo.deleteOne({
db,
select,
where: { customerPkey: { customerId: -1 } },
});
const justDeletedCustomers = await customerRepo.selectMany({
db,
select: ['customerId', 'email'],
where: [['customerId', '=', -1]],
limit: 500,
offset: 0,
});
console.log({
createdCustomer,
fetchedCustomer,
updatedCustomer,
deletedCustomer,
justDeletedCustomers,
});
await db.destroy();
}