-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Had to turn off SWC compilation in next.js. Should be fixed in vercel/next.js#32914 Added some babel plugins needed for decoratorMetadata emit Workaround for DB connection failure when dealing with HMR in dev mode Workaround for loading Entity classes (has to have ormconfig.js config overridden in app to get around dynamic import which was causing all sorts of issues with JS module loading)
- Loading branch information
1 parent
49158b2
commit 220ec22
Showing
19 changed files
with
751 additions
and
127 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
"presets": [ | ||
"next/babel" | ||
], | ||
"plugins": [ | ||
"babel-plugin-transform-typescript-metadata", | ||
[ | ||
"@babel/plugin-proposal-decorators", | ||
{ | ||
"legacy": true | ||
} | ||
], | ||
[ | ||
"@babel/plugin-proposal-class-properties", | ||
], | ||
], | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,74 @@ | ||
import { Connection, createConnection } from 'typeorm'; | ||
import 'reflect-metadata'; | ||
import { | ||
Connection, | ||
ConnectionManager, | ||
createConnection, | ||
EntityManager, | ||
getConnectionManager, | ||
getConnectionOptions, | ||
} from 'typeorm'; | ||
import { Execution } from '../model/db/entity/Execution'; | ||
import { Trade } from '../model/db/entity/Trade'; | ||
|
||
export default async function initDb(): Promise<Connection> { | ||
export async function initDb(): Promise<Connection> { | ||
const connectionOptions = await getConnectionOptions(); | ||
Object.assign(connectionOptions, { | ||
entities: [Execution, Trade], | ||
}); | ||
return await createConnection(); | ||
} | ||
|
||
// const getConnectionOptions = () => {}; | ||
|
||
/** | ||
* Database manager class | ||
*/ | ||
class Database { | ||
private connectionManager: ConnectionManager; | ||
|
||
private hasCreatedConnection = false; | ||
|
||
constructor() { | ||
this.connectionManager = getConnectionManager(); | ||
} | ||
|
||
private async getConnection(): Promise<Connection> { | ||
const DEFAULT_CONNECTION_NAME = 'default'; | ||
const currentConnection = this.connectionManager.has( | ||
DEFAULT_CONNECTION_NAME | ||
) | ||
? this.connectionManager.get(DEFAULT_CONNECTION_NAME) | ||
: undefined; | ||
if (currentConnection && !this.hasCreatedConnection) { | ||
console.debug('recreating connection due to hot reloading'); | ||
if (currentConnection.isConnected) { | ||
await currentConnection.close(); | ||
} | ||
console.debug('done closing, making new connection..'); | ||
return this.createConnectionWithName(DEFAULT_CONNECTION_NAME); | ||
} | ||
if (currentConnection) { | ||
if (!currentConnection.isConnected) { | ||
return currentConnection.connect(); | ||
} else return currentConnection; | ||
} else { | ||
return this.createConnectionWithName(DEFAULT_CONNECTION_NAME); | ||
} | ||
} | ||
|
||
private async createConnectionWithName(name: string): Promise<Connection> { | ||
this.hasCreatedConnection = true; | ||
const connectionOptions = await getConnectionOptions(); | ||
Object.assign(connectionOptions, { | ||
entities: [Execution, Trade], | ||
}); | ||
return createConnection(connectionOptions); | ||
} | ||
|
||
public getManager(): Promise<EntityManager> { | ||
return this.getConnection().then((conn) => conn.manager); | ||
} | ||
} | ||
|
||
const db = new Database(); | ||
export default db; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { createAsyncThunk, createSlice, PayloadAction } from '@reduxjs/toolkit'; | ||
import { Execution, ExecutionJson, toExecutionJson } from '../model'; | ||
import TraderPerfApiClient from '../util/apiClient'; | ||
|
||
export interface ExeuctionsState { | ||
executions: ExecutionJson[]; | ||
} | ||
|
||
const initialState: ExeuctionsState = { | ||
executions: [], | ||
}; | ||
|
||
export const getExecutionsAsync = createAsyncThunk( | ||
'executions/fetch', | ||
async () => { | ||
const executions = await new TraderPerfApiClient().getExecutions(); | ||
return executions.data as object; | ||
} | ||
); | ||
|
||
export const saveExxecutionsAsync = createAsyncThunk( | ||
'executions/fetch', | ||
async (executions: Execution[]) => { | ||
const response = await new TraderPerfApiClient().saveExecutions( | ||
executions.map(toExecutionJson) | ||
); | ||
return response.data as object; | ||
} | ||
); | ||
|
||
export const executionsSlice = createSlice({ | ||
name: 'executions', | ||
initialState, | ||
reducers: { | ||
setExecutions: (state, action: PayloadAction<ExecutionJson[]>) => { | ||
state.executions = [...action.payload]; | ||
}, | ||
}, | ||
}); | ||
|
||
export const { setExecutions } = executionsSlice.actions; | ||
|
||
export default executionsSlice.reducer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.