-
Notifications
You must be signed in to change notification settings - Fork 3
/
database.ts
222 lines (214 loc) · 8.75 KB
/
database.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/**
* SudoSOS back-end API service.
* Copyright (C) 2024 Study association GEWIS
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* @license
*/
/**
* This is the module page of the database.
*
* @module internal/database
*/
import {
createConnection, DataSource,
} from 'typeorm';
import User from '../entity/user/user';
import Product from '../entity/product/product';
import SubTransaction from '../entity/transactions/sub-transaction';
import Transaction from '../entity/transactions/transaction';
import ProductCategory from '../entity/product/product-category';
import SubTransactionRow from '../entity/transactions/sub-transaction-row';
import PointOfSale from '../entity/point-of-sale/point-of-sale';
import Container from '../entity/container/container';
import VoucherGroup from '../entity/user/voucher-group';
import LocalUser from '../entity/user/local-user';
import GewisUser from '../gewis/entity/gewis-user';
import UserVoucherGroup from '../entity/user/user-voucher-group';
import EanAuthenticator from '../entity/authenticator/ean-authenticator';
import MemberAuthenticator from '../entity/authenticator/member-authenticator';
import NfcAuthenticator from '../entity/authenticator/nfc-authenticator';
import PinAuthenticator from '../entity/authenticator/pin-authenticator';
import Banner from '../entity/banner';
import Transfer from '../entity/transactions/transfer';
import ProductRevision from '../entity/product/product-revision';
import ContainerRevision from '../entity/container/container-revision';
import PointOfSaleRevision from '../entity/point-of-sale/point-of-sale-revision';
import ProductOrdering from '../entity/point-of-sale/product-ordering';
import Balance from '../entity/transactions/balance';
import InvoiceUser from '../entity/user/invoice-user';
import Invoice from '../entity/invoices/invoice';
import InvoiceStatus from '../entity/invoices/invoice-status';
import BaseFile from '../entity/file/base-file';
import ProductImage from '../entity/file/product-image';
import BannerImage from '../entity/file/banner-image';
import StripeDeposit from '../entity/stripe/stripe-deposit';
import StripePaymentIntentStatus from '../entity/stripe/stripe-payment-intent-status';
import PayoutRequest from '../entity/transactions/payout/payout-request';
import PayoutRequestStatus from '../entity/transactions/payout/payout-request-status';
import LDAPAuthenticator from '../entity/authenticator/ldap-authenticator';
import AssignedRole from '../entity/rbac/assigned-role';
import VatGroup from '../entity/vat-group';
import LocalAuthenticator from '../entity/authenticator/local-authenticator';
import ResetToken from '../entity/authenticator/reset-token';
import { DataSourceOptions } from 'typeorm/data-source/DataSourceOptions';
import KeyAuthenticator from '../entity/authenticator/key-authenticator';
import Fine from '../entity/fine/fine';
import FineHandoutEvent from '../entity/fine/fineHandoutEvent';
import UserFineGroup from '../entity/fine/userFineGroup';
import Event from '../entity/event/event';
import EventShiftAnswer from '../entity/event/event-shift-answer';
import EventShift from '../entity/event/event-shift';
import { TransactionSubscriber, TransferSubscriber } from '../subscriber';
import InvoicePdf from '../entity/file/invoice-pdf';
import { InvoiceRefactor1707251162194 } from '../migrations/1707251162194-invoice-refactor';
import dotenv from 'dotenv';
import PayoutRequestPdf from '../entity/file/payout-request-pdf';
import { PayoutRequestPdf1720610649657 } from '../migrations/1720610649657-payout-request-pdf';
import { SoftDeletes1720608140757 } from '../migrations/1720608140757-soft-deletes';
import Role from '../entity/rbac/role';
import Permission from '../entity/rbac/permission';
import { DatabaseRbac1720624912620 } from '../migrations/1720624912260-database-rbac';
import RoleUserType from '../entity/rbac/role-user-type';
import { TransfersVat1721916495084 } from '../migrations/1721916495084-transfers-vat';
import { PosCashiers1722022351000 } from '../migrations/1722022351000-pos-cashiers';
import WriteOff from '../entity/transactions/write-off';
import { WriteOffs1722004753128 } from '../migrations/1722004753128-write-offs';
import ServerSetting from '../entity/server-setting';
import { ServerSettings1722083254200 } from '../migrations/1722083254200-server-settings';
import { PosUsers1722084520361 } from '../migrations/1722084520361-pos-users';
import { InvoiceRework1622118077157 } from '../migrations/1722118077157-invoice-rework';
import StripePaymentIntent from '../entity/stripe/stripe-payment-intent';
import { StripePaymentIntents1722869409448 } from '../migrations/1722869409448-stripe-payment-intents';
import { NestedProductCategories1722517212441 } from '../migrations/1722517212441-nested-product-categories';
import SellerPayout from '../entity/transactions/payout/seller-payout';
import { InvoiceAsTopups1724506999318 } from '../migrations/1724506999318-invoice-as-topups';
import { SellerPayouts1724855153990 } from '../migrations/1724855153990-seller-payouts';
import SellerPayoutPdf from '../entity/file/seller-payout-pdf';
import { UserTypeEnums1725196803203 } from '../migrations/1725196803203-user-type-enums';
import { CustomInvoiceEntries1725388477226 } from '../migrations/1725388477226-custom-invoice-entries';
import { SellerPayoutPdf1726066600389 } from '../migrations/1726066600389-seller-payout-pdf';
import { LDAPObjectGUID1726689003147 } from '../migrations/1726689003147-ldap-objectguid';
// We need to load the dotenv to prevent the env from being undefined.
dotenv.config();
if (process.env.NODE_ENV === 'test') {
console.log('TYPEORM_CONNECTION:', process.env.TYPEORM_CONNECTION);
}
const options: DataSourceOptions = {
host: process.env.TYPEORM_HOST,
port: parseInt(process.env.TYPEORM_PORT || '3001'),
database: process.env.TYPEORM_DATABASE,
type: process.env.TYPEORM_CONNECTION as 'postgres' | 'mariadb' | 'mysql',
username: process.env.TYPEORM_USERNAME,
password: process.env.TYPEORM_PASSWORD,
synchronize: process.env.TYPEORM_SYNCHRONIZE === 'true',
logging: process.env.TYPEORM_LOGGING === 'true',
migrations: [
InvoiceRefactor1707251162194,
SoftDeletes1720608140757,
PayoutRequestPdf1720610649657,
DatabaseRbac1720624912620,
TransfersVat1721916495084,
PosCashiers1722022351000,
ServerSettings1722083254200,
PosUsers1722084520361,
WriteOffs1722004753128,
InvoiceRework1622118077157,
StripePaymentIntents1722869409448,
NestedProductCategories1722517212441,
InvoiceAsTopups1724506999318,
SellerPayouts1724855153990,
UserTypeEnums1725196803203,
CustomInvoiceEntries1725388477226,
SellerPayoutPdf1726066600389,
LDAPObjectGUID1726689003147,
],
extra: {
authPlugins: {
mysql_clear_password: () => () => Buffer.from(`${process.env.TYPEORM_PASSWORD}\0`),
},
},
poolSize: 4,
entities: [
ServerSetting,
ProductCategory,
VatGroup,
Product,
ProductRevision,
Container,
ContainerRevision,
PointOfSale,
PointOfSaleRevision,
Transfer,
StripeDeposit,
StripePaymentIntent,
StripePaymentIntentStatus,
PayoutRequest,
PayoutRequestPdf,
PayoutRequestStatus,
SellerPayout,
SellerPayoutPdf,
Fine,
FineHandoutEvent,
UserFineGroup,
Transaction,
SubTransaction,
SubTransactionRow,
VoucherGroup,
User,
LocalUser,
GewisUser,
UserVoucherGroup,
EanAuthenticator,
MemberAuthenticator,
NfcAuthenticator,
KeyAuthenticator,
PinAuthenticator,
LocalAuthenticator,
LDAPAuthenticator,
Banner,
ProductOrdering,
Balance,
InvoiceUser,
Invoice,
InvoiceStatus,
InvoicePdf,
BaseFile,
ProductImage,
BannerImage,
Role,
RoleUserType,
Permission,
AssignedRole,
ResetToken,
Event,
EventShift,
EventShiftAnswer,
WriteOff,
],
subscribers: [
TransactionSubscriber,
TransferSubscriber,
],
};
export let AppDataSource = new DataSource(options);
const Database = {
initialize: async () => {
if (AppDataSource && AppDataSource.isInitialized) return AppDataSource;
AppDataSource = await createConnection(options);
return AppDataSource;
},
};
export default Database;