Compare commits

...

7 Commits

Author SHA1 Message Date
standardci
51c8b20506 chore(release): publish new version
- @standardnotes/api-gateway@1.63.1
 - @standardnotes/home-server@1.8.3
 - @standardnotes/revisions-server@1.21.2
2023-06-02 11:26:26 +00:00
Karol Sójko
1e62a3760e fix(home-server): displaying the port on which it is running 2023-06-02 13:08:23 +02:00
Karol Sójko
2f569d4104 fix(home-server): add default for VERSION environment variable 2023-06-02 12:52:51 +02:00
standardci
f23e444ed0 chore(release): publish new version
- @standardnotes/home-server@1.8.2
2023-06-02 10:43:32 +00:00
Karol Sójko
e6e9a32f03 fix(home-server): default configuration variables 2023-06-02 12:29:24 +02:00
standardci
8237df33a7 chore(release): publish new version
- @standardnotes/auth-server@1.116.1
 - @standardnotes/home-server@1.8.1
 - @standardnotes/revisions-server@1.21.1
 - @standardnotes/syncing-server@1.42.1
2023-06-02 09:37:30 +00:00
Karol Sójko
624b574013 fix: initializing data source with already configured environment 2023-06-02 11:20:34 +02:00
19 changed files with 349 additions and 256 deletions

View File

@@ -3,6 +3,12 @@
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
## [1.63.1](https://github.com/standardnotes/api-gateway/compare/@standardnotes/api-gateway@1.63.0...@standardnotes/api-gateway@1.63.1) (2023-06-02)
### Bug Fixes
* **home-server:** add default for VERSION environment variable ([2f569d4](https://github.com/standardnotes/api-gateway/commit/2f569d41047a802eb72ef1a3618ffe4df28a709c))
# [1.63.0](https://github.com/standardnotes/api-gateway/compare/@standardnotes/api-gateway@1.62.4...@standardnotes/api-gateway@1.63.0) (2023-06-02)
### Features

View File

@@ -1,6 +1,6 @@
{
"name": "@standardnotes/api-gateway",
"version": "1.63.0",
"version": "1.63.1",
"engines": {
"node": ">=18.0.0 <21.0.0"
},

View File

@@ -83,7 +83,7 @@ export class ContainerConfigLoader {
container
.bind(TYPES.HTTP_CALL_TIMEOUT)
.toConstantValue(env.get('HTTP_CALL_TIMEOUT', true) ? +env.get('HTTP_CALL_TIMEOUT', true) : 60_000)
container.bind(TYPES.VERSION).toConstantValue(env.get('VERSION'))
container.bind(TYPES.VERSION).toConstantValue(env.get('VERSION', true) ?? 'development')
container.bind(TYPES.CROSS_SERVICE_TOKEN_CACHE_TTL).toConstantValue(+env.get('CROSS_SERVICE_TOKEN_CACHE_TTL', true))
// Middleware

View File

@@ -3,6 +3,12 @@
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
## [1.116.1](https://github.com/standardnotes/server/compare/@standardnotes/auth-server@1.116.0...@standardnotes/auth-server@1.116.1) (2023-06-02)
### Bug Fixes
* initializing data source with already configured environment ([624b574](https://github.com/standardnotes/server/commit/624b574013157e9e044d4a8ed53cadb7fcc567ae))
# [1.116.0](https://github.com/standardnotes/server/compare/@standardnotes/auth-server@1.115.5...@standardnotes/auth-server@1.116.0) (2023-06-02)
### Features

View File

@@ -1,6 +1,6 @@
{
"name": "@standardnotes/auth-server",
"version": "1.116.0",
"version": "1.116.1",
"engines": {
"node": ">=18.0.0 <21.0.0"
},

View File

@@ -267,7 +267,8 @@ export class ContainerConfigLoader {
const container = new Container()
await AppDataSource.initialize()
const appDataSource = new AppDataSource(env)
await appDataSource.initialize()
const isConfiguredForHomeServer = env.get('DB_TYPE') === 'sqlite'
@@ -359,42 +360,42 @@ export class ContainerConfigLoader {
// ORM
container
.bind<Repository<OfflineSetting>>(TYPES.Auth_ORMOfflineSettingRepository)
.toConstantValue(AppDataSource.getRepository(OfflineSetting))
.toConstantValue(appDataSource.getRepository(OfflineSetting))
container
.bind<Repository<OfflineUserSubscription>>(TYPES.Auth_ORMOfflineUserSubscriptionRepository)
.toConstantValue(AppDataSource.getRepository(OfflineUserSubscription))
.toConstantValue(appDataSource.getRepository(OfflineUserSubscription))
container
.bind<Repository<RevokedSession>>(TYPES.Auth_ORMRevokedSessionRepository)
.toConstantValue(AppDataSource.getRepository(RevokedSession))
container.bind<Repository<Role>>(TYPES.Auth_ORMRoleRepository).toConstantValue(AppDataSource.getRepository(Role))
.toConstantValue(appDataSource.getRepository(RevokedSession))
container.bind<Repository<Role>>(TYPES.Auth_ORMRoleRepository).toConstantValue(appDataSource.getRepository(Role))
container
.bind<Repository<Session>>(TYPES.Auth_ORMSessionRepository)
.toConstantValue(AppDataSource.getRepository(Session))
.toConstantValue(appDataSource.getRepository(Session))
container
.bind<Repository<Setting>>(TYPES.Auth_ORMSettingRepository)
.toConstantValue(AppDataSource.getRepository(Setting))
.toConstantValue(appDataSource.getRepository(Setting))
container
.bind<Repository<SharedSubscriptionInvitation>>(TYPES.Auth_ORMSharedSubscriptionInvitationRepository)
.toConstantValue(AppDataSource.getRepository(SharedSubscriptionInvitation))
.toConstantValue(appDataSource.getRepository(SharedSubscriptionInvitation))
container
.bind<Repository<SubscriptionSetting>>(TYPES.Auth_ORMSubscriptionSettingRepository)
.toConstantValue(AppDataSource.getRepository(SubscriptionSetting))
container.bind<Repository<User>>(TYPES.Auth_ORMUserRepository).toConstantValue(AppDataSource.getRepository(User))
.toConstantValue(appDataSource.getRepository(SubscriptionSetting))
container.bind<Repository<User>>(TYPES.Auth_ORMUserRepository).toConstantValue(appDataSource.getRepository(User))
container
.bind<Repository<UserSubscription>>(TYPES.Auth_ORMUserSubscriptionRepository)
.toConstantValue(AppDataSource.getRepository(UserSubscription))
.toConstantValue(appDataSource.getRepository(UserSubscription))
container
.bind<Repository<TypeORMSessionTrace>>(TYPES.Auth_ORMSessionTraceRepository)
.toConstantValue(AppDataSource.getRepository(TypeORMSessionTrace))
.toConstantValue(appDataSource.getRepository(TypeORMSessionTrace))
container
.bind<Repository<TypeORMAuthenticator>>(TYPES.Auth_ORMAuthenticatorRepository)
.toConstantValue(AppDataSource.getRepository(TypeORMAuthenticator))
.toConstantValue(appDataSource.getRepository(TypeORMAuthenticator))
container
.bind<Repository<TypeORMAuthenticatorChallenge>>(TYPES.Auth_ORMAuthenticatorChallengeRepository)
.toConstantValue(AppDataSource.getRepository(TypeORMAuthenticatorChallenge))
.toConstantValue(appDataSource.getRepository(TypeORMAuthenticatorChallenge))
container
.bind<Repository<TypeORMCacheEntry>>(TYPES.Auth_ORMCacheEntryRepository)
.toConstantValue(AppDataSource.getRepository(TypeORMCacheEntry))
.toConstantValue(appDataSource.getRepository(TypeORMCacheEntry))
// Repositories
container.bind<SessionRepositoryInterface>(TYPES.Auth_SessionRepository).to(TypeORMSessionRepository)

View File

@@ -1,4 +1,4 @@
import { DataSource, LoggerOptions } from 'typeorm'
import { DataSource, EntityTarget, LoggerOptions, ObjectLiteral, Repository } from 'typeorm'
import { MysqlConnectionOptions } from 'typeorm/driver/mysql/MysqlConnectionOptions'
import { Permission } from '../Domain/Permission/Permission'
import { Role } from '../Domain/Role/Role'
@@ -19,88 +19,102 @@ import { TypeORMSessionTrace } from '../Infra/TypeORM/TypeORMSessionTrace'
import { Env } from './Env'
import { SqliteConnectionOptions } from 'typeorm/driver/sqlite/SqliteConnectionOptions'
const env: Env = new Env()
env.load()
export class AppDataSource {
private dataSource: DataSource | undefined
const isConfiguredForMySQL = env.get('DB_TYPE') === 'mysql'
constructor(private env: Env) {}
const maxQueryExecutionTime = env.get('DB_MAX_QUERY_EXECUTION_TIME', true)
? +env.get('DB_MAX_QUERY_EXECUTION_TIME', true)
: 45_000
getRepository<Entity extends ObjectLiteral>(target: EntityTarget<Entity>): Repository<Entity> {
if (!this.dataSource) {
throw new Error('DataSource not initialized')
}
const commonDataSourceOptions = {
maxQueryExecutionTime,
entities: [
User,
UserSubscription,
OfflineUserSubscription,
Session,
RevokedSession,
Role,
Permission,
Setting,
OfflineSetting,
SharedSubscriptionInvitation,
SubscriptionSetting,
TypeORMSessionTrace,
TypeORMAuthenticator,
TypeORMAuthenticatorChallenge,
TypeORMEmergencyAccessInvitation,
TypeORMCacheEntry,
],
migrations: [`${__dirname}/../../migrations/${isConfiguredForMySQL ? 'mysql' : 'sqlite'}/*.js`],
migrationsRun: true,
logging: <LoggerOptions>env.get('DB_DEBUG_LEVEL', true) ?? 'info',
return this.dataSource.getRepository(target)
}
async initialize(): Promise<void> {
this.env.load()
const isConfiguredForMySQL = this.env.get('DB_TYPE') === 'mysql'
const maxQueryExecutionTime = this.env.get('DB_MAX_QUERY_EXECUTION_TIME', true)
? +this.env.get('DB_MAX_QUERY_EXECUTION_TIME', true)
: 45_000
const commonDataSourceOptions = {
maxQueryExecutionTime,
entities: [
User,
UserSubscription,
OfflineUserSubscription,
Session,
RevokedSession,
Role,
Permission,
Setting,
OfflineSetting,
SharedSubscriptionInvitation,
SubscriptionSetting,
TypeORMSessionTrace,
TypeORMAuthenticator,
TypeORMAuthenticatorChallenge,
TypeORMEmergencyAccessInvitation,
TypeORMCacheEntry,
],
migrations: [`${__dirname}/../../migrations/${isConfiguredForMySQL ? 'mysql' : 'sqlite'}/*.js`],
migrationsRun: true,
logging: <LoggerOptions>this.env.get('DB_DEBUG_LEVEL', true) ?? 'info',
}
if (isConfiguredForMySQL) {
const inReplicaMode = this.env.get('DB_REPLICA_HOST', true) ? true : false
const replicationConfig = {
master: {
host: this.env.get('DB_HOST'),
port: parseInt(this.env.get('DB_PORT')),
username: this.env.get('DB_USERNAME'),
password: this.env.get('DB_PASSWORD'),
database: this.env.get('DB_DATABASE'),
},
slaves: [
{
host: this.env.get('DB_REPLICA_HOST', true),
port: parseInt(this.env.get('DB_PORT')),
username: this.env.get('DB_USERNAME'),
password: this.env.get('DB_PASSWORD'),
database: this.env.get('DB_DATABASE'),
},
],
removeNodeErrorCount: 10,
restoreNodeTimeout: 5,
}
const mySQLDataSourceOptions: MysqlConnectionOptions = {
...commonDataSourceOptions,
type: 'mysql',
charset: 'utf8mb4',
supportBigNumbers: true,
bigNumberStrings: false,
replication: inReplicaMode ? replicationConfig : undefined,
host: inReplicaMode ? undefined : this.env.get('DB_HOST'),
port: inReplicaMode ? undefined : parseInt(this.env.get('DB_PORT')),
username: inReplicaMode ? undefined : this.env.get('DB_USERNAME'),
password: inReplicaMode ? undefined : this.env.get('DB_PASSWORD'),
database: inReplicaMode ? undefined : this.env.get('DB_DATABASE'),
}
this.dataSource = new DataSource(mySQLDataSourceOptions)
} else {
const sqliteDataSourceOptions: SqliteConnectionOptions = {
...commonDataSourceOptions,
type: 'sqlite',
database: `data/${this.env.get('DB_DATABASE')}.sqlite`,
}
this.dataSource = new DataSource(sqliteDataSourceOptions)
}
await this.dataSource.initialize()
}
}
let dataSource: DataSource
if (isConfiguredForMySQL) {
const inReplicaMode = env.get('DB_REPLICA_HOST', true) ? true : false
const replicationConfig = {
master: {
host: env.get('DB_HOST'),
port: parseInt(env.get('DB_PORT')),
username: env.get('DB_USERNAME'),
password: env.get('DB_PASSWORD'),
database: env.get('DB_DATABASE'),
},
slaves: [
{
host: env.get('DB_REPLICA_HOST', true),
port: parseInt(env.get('DB_PORT')),
username: env.get('DB_USERNAME'),
password: env.get('DB_PASSWORD'),
database: env.get('DB_DATABASE'),
},
],
removeNodeErrorCount: 10,
restoreNodeTimeout: 5,
}
const mySQLDataSourceOptions: MysqlConnectionOptions = {
...commonDataSourceOptions,
type: 'mysql',
charset: 'utf8mb4',
supportBigNumbers: true,
bigNumberStrings: false,
replication: inReplicaMode ? replicationConfig : undefined,
host: inReplicaMode ? undefined : env.get('DB_HOST'),
port: inReplicaMode ? undefined : parseInt(env.get('DB_PORT')),
username: inReplicaMode ? undefined : env.get('DB_USERNAME'),
password: inReplicaMode ? undefined : env.get('DB_PASSWORD'),
database: inReplicaMode ? undefined : env.get('DB_DATABASE'),
}
dataSource = new DataSource(mySQLDataSourceOptions)
} else {
const sqliteDataSourceOptions: SqliteConnectionOptions = {
...commonDataSourceOptions,
type: 'sqlite',
database: `data/${env.get('DB_DATABASE')}.sqlite`,
}
dataSource = new DataSource(sqliteDataSourceOptions)
}
export const AppDataSource = dataSource

View File

@@ -1,15 +1,5 @@
LOG_LEVEL=debug
NODE_ENV=development
VERSION=development
# (Optional) New Relic Setup
NEW_RELIC_ENABLED=false
NEW_RELIC_APP_NAME="Home Server"
CACHE_TYPE=memory
DB_TYPE=sqlite
DB_DATABASE=home_server
JWT_SECRET=
AUTH_JWT_SECRET=

View File

@@ -3,6 +3,23 @@
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
## [1.8.3](https://github.com/standardnotes/server/compare/@standardnotes/home-server@1.8.2...@standardnotes/home-server@1.8.3) (2023-06-02)
### Bug Fixes
* **home-server:** add default for VERSION environment variable ([2f569d4](https://github.com/standardnotes/server/commit/2f569d41047a802eb72ef1a3618ffe4df28a709c))
* **home-server:** displaying the port on which it is running ([1e62a37](https://github.com/standardnotes/server/commit/1e62a3760e3b9601478c851cf33db2f2b348d7fb))
## [1.8.2](https://github.com/standardnotes/server/compare/@standardnotes/home-server@1.8.1...@standardnotes/home-server@1.8.2) (2023-06-02)
### Bug Fixes
* **home-server:** default configuration variables ([e6e9a32](https://github.com/standardnotes/server/commit/e6e9a32f0385789e5e772e5cabcc0da0b8ccbb01))
## [1.8.1](https://github.com/standardnotes/server/compare/@standardnotes/home-server@1.8.0...@standardnotes/home-server@1.8.1) (2023-06-02)
**Note:** Version bump only for package @standardnotes/home-server
# [1.8.0](https://github.com/standardnotes/server/compare/@standardnotes/home-server@1.7.5...@standardnotes/home-server@1.8.0) (2023-06-02)
### Features

View File

@@ -1,6 +1,6 @@
{
"name": "@standardnotes/home-server",
"version": "1.8.0",
"version": "1.8.3",
"engines": {
"node": ">=18.0.0 <21.0.0"
},

View File

@@ -30,7 +30,16 @@ export class HomeServer implements HomeServerInterface {
const serviceContainer = new ServiceContainer()
const directCallDomainEventPublisher = new DirectCallDomainEventPublisher()
const env: Env = new Env(configuration?.environment)
const environmentOverrides = {
...configuration?.environment,
NEW_RELIC_ENABLED: 'false',
NEW_RELIC_APP_NAME: 'Home Server',
CACHE_TYPE: 'memory',
DB_TYPE: 'sqlite',
DB_DATABASE: 'home_server',
}
const env: Env = new Env(environmentOverrides)
env.load()
this.configureLoggers(env)
@@ -44,23 +53,23 @@ export class HomeServer implements HomeServerInterface {
const container = Container.merge(
(await apiGatewayService.getContainer({
logger: winston.loggers.get('api-gateway'),
environmentOverrides: configuration?.environment,
environmentOverrides,
})) as Container,
(await authService.getContainer({
logger: winston.loggers.get('auth-server'),
environmentOverrides: configuration?.environment,
environmentOverrides,
})) as Container,
(await syncingService.getContainer({
logger: winston.loggers.get('syncing-server'),
environmentOverrides: configuration?.environment,
environmentOverrides,
})) as Container,
(await revisionsService.getContainer({
logger: winston.loggers.get('revisions-server'),
environmentOverrides: configuration?.environment,
environmentOverrides,
})) as Container,
(await filesService.getContainer({
logger: winston.loggers.get('files-server'),
environmentOverrides: configuration?.environment,
environmentOverrides,
})) as Container,
)
@@ -119,9 +128,11 @@ export class HomeServer implements HomeServerInterface {
})
})
this.serverInstance = server.build().listen(env.get('PORT', true) ? +env.get('PORT', true) : 3000)
const port = env.get('PORT', true) ? +env.get('PORT', true) : 3000
logger.info(`Server started on port ${process.env.PORT}`)
this.serverInstance = server.build().listen(port)
logger.info(`Server started on port ${port}`)
}
async stop(): Promise<void> {

View File

@@ -3,6 +3,18 @@
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
## [1.21.2](https://github.com/standardnotes/server/compare/@standardnotes/revisions-server@1.21.1...@standardnotes/revisions-server@1.21.2) (2023-06-02)
### Bug Fixes
* **home-server:** add default for VERSION environment variable ([2f569d4](https://github.com/standardnotes/server/commit/2f569d41047a802eb72ef1a3618ffe4df28a709c))
## [1.21.1](https://github.com/standardnotes/server/compare/@standardnotes/revisions-server@1.21.0...@standardnotes/revisions-server@1.21.1) (2023-06-02)
### Bug Fixes
* initializing data source with already configured environment ([624b574](https://github.com/standardnotes/server/commit/624b574013157e9e044d4a8ed53cadb7fcc567ae))
# [1.21.0](https://github.com/standardnotes/server/compare/@standardnotes/revisions-server@1.20.4...@standardnotes/revisions-server@1.21.0) (2023-06-02)
### Features

View File

@@ -1,6 +1,6 @@
{
"name": "@standardnotes/revisions-server",
"version": "1.21.0",
"version": "1.21.2",
"engines": {
"node": ">=18.0.0 <21.0.0"
},

View File

@@ -67,7 +67,8 @@ export class ContainerConfigLoader {
defaultScope: 'Singleton',
})
await AppDataSource.initialize()
const appDataSource = new AppDataSource(env)
await appDataSource.initialize()
container.bind<Env>(TYPES.Revisions_Env).toConstantValue(env)
@@ -94,7 +95,7 @@ export class ContainerConfigLoader {
}
container.bind(TYPES.Revisions_NEW_RELIC_ENABLED).toConstantValue(env.get('NEW_RELIC_ENABLED', true))
container.bind(TYPES.Revisions_VERSION).toConstantValue(env.get('VERSION'))
container.bind(TYPES.Revisions_VERSION).toConstantValue(env.get('VERSION', true) ?? 'development')
// Map
container
@@ -107,7 +108,7 @@ export class ContainerConfigLoader {
// ORM
container
.bind<Repository<TypeORMRevision>>(TYPES.Revisions_ORMRevisionRepository)
.toDynamicValue(() => AppDataSource.getRepository(TypeORMRevision))
.toDynamicValue(() => appDataSource.getRepository(TypeORMRevision))
// Repositories
container

View File

@@ -1,4 +1,4 @@
import { DataSource, LoggerOptions } from 'typeorm'
import { DataSource, EntityTarget, LoggerOptions, ObjectLiteral, Repository } from 'typeorm'
import { MysqlConnectionOptions } from 'typeorm/driver/mysql/MysqlConnectionOptions'
import { TypeORMRevision } from '../Infra/TypeORM/TypeORMRevision'
@@ -6,71 +6,85 @@ import { TypeORMRevision } from '../Infra/TypeORM/TypeORMRevision'
import { Env } from './Env'
import { SqliteConnectionOptions } from 'typeorm/driver/sqlite/SqliteConnectionOptions'
const env: Env = new Env()
env.load()
export class AppDataSource {
private dataSource: DataSource | undefined
const isConfiguredForMySQL = env.get('DB_TYPE') === 'mysql'
constructor(private env: Env) {}
const maxQueryExecutionTime = env.get('DB_MAX_QUERY_EXECUTION_TIME', true)
? +env.get('DB_MAX_QUERY_EXECUTION_TIME', true)
: 45_000
getRepository<Entity extends ObjectLiteral>(target: EntityTarget<Entity>): Repository<Entity> {
if (!this.dataSource) {
throw new Error('DataSource not initialized')
}
const commonDataSourceOptions = {
maxQueryExecutionTime,
entities: [TypeORMRevision],
migrations: [`${__dirname}/../../migrations/${isConfiguredForMySQL ? 'mysql' : 'sqlite'}/*.js`],
migrationsRun: true,
logging: <LoggerOptions>env.get('DB_DEBUG_LEVEL', true) ?? 'info',
return this.dataSource.getRepository(target)
}
async initialize(): Promise<void> {
this.env.load()
const isConfiguredForMySQL = this.env.get('DB_TYPE') === 'mysql'
const maxQueryExecutionTime = this.env.get('DB_MAX_QUERY_EXECUTION_TIME', true)
? +this.env.get('DB_MAX_QUERY_EXECUTION_TIME', true)
: 45_000
const commonDataSourceOptions = {
maxQueryExecutionTime,
entities: [TypeORMRevision],
migrations: [`${__dirname}/../../migrations/${isConfiguredForMySQL ? 'mysql' : 'sqlite'}/*.js`],
migrationsRun: true,
logging: <LoggerOptions>this.env.get('DB_DEBUG_LEVEL', true) ?? 'info',
}
if (isConfiguredForMySQL) {
const inReplicaMode = this.env.get('DB_REPLICA_HOST', true) ? true : false
const replicationConfig = {
master: {
host: this.env.get('DB_HOST'),
port: parseInt(this.env.get('DB_PORT')),
username: this.env.get('DB_USERNAME'),
password: this.env.get('DB_PASSWORD'),
database: this.env.get('DB_DATABASE'),
},
slaves: [
{
host: this.env.get('DB_REPLICA_HOST', true),
port: parseInt(this.env.get('DB_PORT')),
username: this.env.get('DB_USERNAME'),
password: this.env.get('DB_PASSWORD'),
database: this.env.get('DB_DATABASE'),
},
],
removeNodeErrorCount: 10,
restoreNodeTimeout: 5,
}
const mySQLDataSourceOptions: MysqlConnectionOptions = {
...commonDataSourceOptions,
type: 'mysql',
charset: 'utf8mb4',
supportBigNumbers: true,
bigNumberStrings: false,
replication: inReplicaMode ? replicationConfig : undefined,
host: inReplicaMode ? undefined : this.env.get('DB_HOST'),
port: inReplicaMode ? undefined : parseInt(this.env.get('DB_PORT')),
username: inReplicaMode ? undefined : this.env.get('DB_USERNAME'),
password: inReplicaMode ? undefined : this.env.get('DB_PASSWORD'),
database: inReplicaMode ? undefined : this.env.get('DB_DATABASE'),
}
this.dataSource = new DataSource(mySQLDataSourceOptions)
} else {
const sqliteDataSourceOptions: SqliteConnectionOptions = {
...commonDataSourceOptions,
type: 'sqlite',
database: `data/${this.env.get('DB_DATABASE')}.sqlite`,
}
this.dataSource = new DataSource(sqliteDataSourceOptions)
}
await this.dataSource.initialize()
}
}
let dataSource: DataSource
if (isConfiguredForMySQL) {
const inReplicaMode = env.get('DB_REPLICA_HOST', true) ? true : false
const replicationConfig = {
master: {
host: env.get('DB_HOST'),
port: parseInt(env.get('DB_PORT')),
username: env.get('DB_USERNAME'),
password: env.get('DB_PASSWORD'),
database: env.get('DB_DATABASE'),
},
slaves: [
{
host: env.get('DB_REPLICA_HOST', true),
port: parseInt(env.get('DB_PORT')),
username: env.get('DB_USERNAME'),
password: env.get('DB_PASSWORD'),
database: env.get('DB_DATABASE'),
},
],
removeNodeErrorCount: 10,
restoreNodeTimeout: 5,
}
const mySQLDataSourceOptions: MysqlConnectionOptions = {
...commonDataSourceOptions,
type: 'mysql',
charset: 'utf8mb4',
supportBigNumbers: true,
bigNumberStrings: false,
replication: inReplicaMode ? replicationConfig : undefined,
host: inReplicaMode ? undefined : env.get('DB_HOST'),
port: inReplicaMode ? undefined : parseInt(env.get('DB_PORT')),
username: inReplicaMode ? undefined : env.get('DB_USERNAME'),
password: inReplicaMode ? undefined : env.get('DB_PASSWORD'),
database: inReplicaMode ? undefined : env.get('DB_DATABASE'),
}
dataSource = new DataSource(mySQLDataSourceOptions)
} else {
const sqliteDataSourceOptions: SqliteConnectionOptions = {
...commonDataSourceOptions,
type: 'sqlite',
database: `data/${env.get('DB_DATABASE')}.sqlite`,
}
dataSource = new DataSource(sqliteDataSourceOptions)
}
export const AppDataSource = dataSource

View File

@@ -3,6 +3,12 @@
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
## [1.42.1](https://github.com/standardnotes/syncing-server-js/compare/@standardnotes/syncing-server@1.42.0...@standardnotes/syncing-server@1.42.1) (2023-06-02)
### Bug Fixes
* initializing data source with already configured environment ([624b574](https://github.com/standardnotes/syncing-server-js/commit/624b574013157e9e044d4a8ed53cadb7fcc567ae))
# [1.42.0](https://github.com/standardnotes/syncing-server-js/compare/@standardnotes/syncing-server@1.41.4...@standardnotes/syncing-server@1.42.0) (2023-06-02)
### Features

View File

@@ -1,6 +1,6 @@
{
"name": "@standardnotes/syncing-server",
"version": "1.42.0",
"version": "1.42.1",
"engines": {
"node": ">=18.0.0 <21.0.0"
},

View File

@@ -95,7 +95,8 @@ export class ContainerConfigLoader {
defaultScope: 'Singleton',
})
await AppDataSource.initialize()
const appDataSource = new AppDataSource(env)
await appDataSource.initialize()
const isConfiguredForHomeServer = env.get('DB_TYPE') === 'sqlite'
@@ -206,7 +207,7 @@ export class ContainerConfigLoader {
// ORM
container
.bind<Repository<Item>>(TYPES.Sync_ORMItemRepository)
.toDynamicValue(() => AppDataSource.getRepository(Item))
.toDynamicValue(() => appDataSource.getRepository(Item))
// Projectors
container

View File

@@ -1,74 +1,88 @@
import { DataSource, LoggerOptions } from 'typeorm'
import { DataSource, EntityTarget, LoggerOptions, ObjectLiteral, Repository } from 'typeorm'
import { MysqlConnectionOptions } from 'typeorm/driver/mysql/MysqlConnectionOptions'
import { Item } from '../Domain/Item/Item'
import { Env } from './Env'
import { SqliteConnectionOptions } from 'typeorm/driver/sqlite/SqliteConnectionOptions'
const env: Env = new Env()
env.load()
export class AppDataSource {
private dataSource: DataSource | undefined
const isConfiguredForMySQL = env.get('DB_TYPE') === 'mysql'
constructor(private env: Env) {}
const maxQueryExecutionTime = env.get('DB_MAX_QUERY_EXECUTION_TIME', true)
? +env.get('DB_MAX_QUERY_EXECUTION_TIME', true)
: 45_000
getRepository<Entity extends ObjectLiteral>(target: EntityTarget<Entity>): Repository<Entity> {
if (!this.dataSource) {
throw new Error('DataSource not initialized')
}
const commonDataSourceOptions = {
maxQueryExecutionTime,
entities: [Item],
migrations: [`${__dirname}/../../migrations/${isConfiguredForMySQL ? 'mysql' : 'sqlite'}/*.js`],
migrationsRun: true,
logging: <LoggerOptions>env.get('DB_DEBUG_LEVEL', true) ?? 'info',
return this.dataSource.getRepository(target)
}
async initialize(): Promise<void> {
this.env.load()
const isConfiguredForMySQL = this.env.get('DB_TYPE') === 'mysql'
const maxQueryExecutionTime = this.env.get('DB_MAX_QUERY_EXECUTION_TIME', true)
? +this.env.get('DB_MAX_QUERY_EXECUTION_TIME', true)
: 45_000
const commonDataSourceOptions = {
maxQueryExecutionTime,
entities: [Item],
migrations: [`${__dirname}/../../migrations/${isConfiguredForMySQL ? 'mysql' : 'sqlite'}/*.js`],
migrationsRun: true,
logging: <LoggerOptions>this.env.get('DB_DEBUG_LEVEL', true) ?? 'info',
}
if (isConfiguredForMySQL) {
const inReplicaMode = this.env.get('DB_REPLICA_HOST', true) ? true : false
const replicationConfig = {
master: {
host: this.env.get('DB_HOST'),
port: parseInt(this.env.get('DB_PORT')),
username: this.env.get('DB_USERNAME'),
password: this.env.get('DB_PASSWORD'),
database: this.env.get('DB_DATABASE'),
},
slaves: [
{
host: this.env.get('DB_REPLICA_HOST', true),
port: parseInt(this.env.get('DB_PORT')),
username: this.env.get('DB_USERNAME'),
password: this.env.get('DB_PASSWORD'),
database: this.env.get('DB_DATABASE'),
},
],
removeNodeErrorCount: 10,
restoreNodeTimeout: 5,
}
const mySQLDataSourceOptions: MysqlConnectionOptions = {
...commonDataSourceOptions,
type: 'mysql',
charset: 'utf8mb4',
supportBigNumbers: true,
bigNumberStrings: false,
replication: inReplicaMode ? replicationConfig : undefined,
host: inReplicaMode ? undefined : this.env.get('DB_HOST'),
port: inReplicaMode ? undefined : parseInt(this.env.get('DB_PORT')),
username: inReplicaMode ? undefined : this.env.get('DB_USERNAME'),
password: inReplicaMode ? undefined : this.env.get('DB_PASSWORD'),
database: inReplicaMode ? undefined : this.env.get('DB_DATABASE'),
}
this.dataSource = new DataSource(mySQLDataSourceOptions)
} else {
const sqliteDataSourceOptions: SqliteConnectionOptions = {
...commonDataSourceOptions,
type: 'sqlite',
database: `data/${this.env.get('DB_DATABASE')}.sqlite`,
}
this.dataSource = new DataSource(sqliteDataSourceOptions)
}
await this.dataSource.initialize()
}
}
let dataSource: DataSource
if (isConfiguredForMySQL) {
const inReplicaMode = env.get('DB_REPLICA_HOST', true) ? true : false
const replicationConfig = {
master: {
host: env.get('DB_HOST'),
port: parseInt(env.get('DB_PORT')),
username: env.get('DB_USERNAME'),
password: env.get('DB_PASSWORD'),
database: env.get('DB_DATABASE'),
},
slaves: [
{
host: env.get('DB_REPLICA_HOST', true),
port: parseInt(env.get('DB_PORT')),
username: env.get('DB_USERNAME'),
password: env.get('DB_PASSWORD'),
database: env.get('DB_DATABASE'),
},
],
removeNodeErrorCount: 10,
restoreNodeTimeout: 5,
}
const mySQLDataSourceOptions: MysqlConnectionOptions = {
...commonDataSourceOptions,
type: 'mysql',
charset: 'utf8mb4',
supportBigNumbers: true,
bigNumberStrings: false,
replication: inReplicaMode ? replicationConfig : undefined,
host: inReplicaMode ? undefined : env.get('DB_HOST'),
port: inReplicaMode ? undefined : parseInt(env.get('DB_PORT')),
username: inReplicaMode ? undefined : env.get('DB_USERNAME'),
password: inReplicaMode ? undefined : env.get('DB_PASSWORD'),
database: inReplicaMode ? undefined : env.get('DB_DATABASE'),
}
dataSource = new DataSource(mySQLDataSourceOptions)
} else {
const sqliteDataSourceOptions: SqliteConnectionOptions = {
...commonDataSourceOptions,
type: 'sqlite',
database: `data/${env.get('DB_DATABASE')}.sqlite`,
}
dataSource = new DataSource(sqliteDataSourceOptions)
}
export const AppDataSource = dataSource