-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
294f519
commit e8e142f
Showing
14 changed files
with
270 additions
and
10 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
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
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
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,2 @@ | ||
export * from './ticker.entity'; | ||
export * from './ticker.repository'; |
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,123 @@ | ||
import { Timestamp } from '@dripjs/types'; | ||
import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm'; | ||
|
||
import { nullableDateTransformer } from '../../common'; | ||
|
||
@Entity({ | ||
name: 'ticker', | ||
}) | ||
export class TickerEntity { | ||
@PrimaryGeneratedColumn({ | ||
type: 'bigint', | ||
name: 'id', | ||
unsigned: true, | ||
comment: 'primary auto generated id', | ||
}) | ||
readonly id!: string; | ||
|
||
@Column({ | ||
type: 'varchar', | ||
name: 'exchange', | ||
length: 20, | ||
comment: 'exchange name', | ||
}) | ||
readonly exchange!: string; | ||
|
||
@Column({ | ||
type: 'varchar', | ||
name: 'symbol', | ||
length: 20, | ||
comment: 'symbol name', | ||
}) | ||
readonly symbol!: string; | ||
|
||
@Column({ | ||
type: 'decimal', | ||
name: 'ask', | ||
precision: 21, | ||
scale: 9, | ||
unsigned: true, | ||
}) | ||
readonly ask!: string; | ||
|
||
@Column({ | ||
type: 'decimal', | ||
name: 'bid', | ||
precision: 21, | ||
scale: 9, | ||
unsigned: true, | ||
}) | ||
readonly bid!: string; | ||
|
||
@Column({ | ||
type: 'decimal', | ||
name: 'open', | ||
precision: 21, | ||
scale: 9, | ||
unsigned: true, | ||
}) | ||
readonly open!: string; | ||
|
||
@Column({ | ||
type: 'decimal', | ||
name: 'high', | ||
precision: 21, | ||
scale: 9, | ||
unsigned: true, | ||
}) | ||
readonly high!: string; | ||
|
||
@Column({ | ||
type: 'decimal', | ||
name: 'low', | ||
precision: 21, | ||
scale: 9, | ||
unsigned: true, | ||
}) | ||
readonly low!: string; | ||
|
||
@Column({ | ||
type: 'decimal', | ||
name: 'last', | ||
precision: 21, | ||
scale: 9, | ||
unsigned: true, | ||
}) | ||
readonly last!: string; | ||
|
||
@Column({ | ||
type: 'decimal', | ||
name: 'volume', | ||
precision: 21, | ||
scale: 9, | ||
unsigned: true, | ||
}) | ||
readonly volume!: string; | ||
|
||
@Column({ | ||
type: 'datetime', | ||
name: 'time', | ||
precision: 3, | ||
transformer: nullableDateTransformer, | ||
}) | ||
readonly time!: Timestamp; | ||
|
||
@Column({ | ||
type: 'datetime', | ||
name: 'created_at', | ||
precision: 3, | ||
default: /* istanbul ignore next */ () => 'NOW(3)', | ||
transformer: nullableDateTransformer, | ||
}) | ||
readonly createdAt!: Timestamp; | ||
|
||
@Column({ | ||
type: 'datetime', | ||
name: 'updated_at', | ||
precision: 3, | ||
default: /* istanbul ignore next */ () => 'NOW(3)', | ||
onUpdate: 'NOW(3)', | ||
transformer: nullableDateTransformer, | ||
}) | ||
readonly updatedAt!: Timestamp; | ||
} |
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,69 @@ | ||
import { fixByDigits } from '@dripjs/common'; | ||
import { EntityTestBed, TickerEntity, TickerEntityCreateParams, TickerRepository } from '@dripjs/models'; | ||
import { getTickerDefaultData } from '@dripjs/testing'; | ||
import { Timestamp } from '@dripjs/types'; | ||
|
||
describe('ticker.repository', () => { | ||
let tickerReposity: TickerRepository; | ||
const defaultData = getTickerDefaultData(); | ||
const exchange = 'bitmex'; | ||
const pair = 'xbtusd'; | ||
|
||
beforeAll(async () => { | ||
await EntityTestBed.setup(); | ||
tickerReposity = EntityTestBed.getRepository(TickerRepository); | ||
}); | ||
|
||
afterAll(async () => { | ||
await EntityTestBed.cleanup(); | ||
}); | ||
|
||
beforeEach(async () => { | ||
await EntityTestBed.reset(); | ||
await tickerReposity.insertNewTickers(defaultData); | ||
}); | ||
|
||
describe('insertNewTicker', () => { | ||
it('should insert new ticker', async () => { | ||
const newData = { | ||
...defaultData[0], | ||
exchange: 'bitbank', | ||
time: Date.now() as Timestamp, | ||
}; | ||
await tickerReposity.insertNewTicker(newData); | ||
const insertedTicker = await tickerReposity.find({ | ||
exchange: newData.exchange, | ||
}); | ||
expect(insertedTicker.map(getDataFromEntity)).toEqual([newData]); | ||
}); | ||
}); | ||
|
||
describe('insertNewTickers', () => { | ||
it('should insert new Tickers', async () => { | ||
const insertedTickers = await tickerReposity.find(); | ||
expect(insertedTickers.map(getDataFromEntity)).toEqual(defaultData); | ||
}); | ||
}); | ||
|
||
describe('getTickers', () => { | ||
it('should get tickers', async () => { | ||
const res = await tickerReposity.getTickers(exchange, pair); | ||
expect([getDataFromEntity(res[0])]).toEqual(defaultData); | ||
}); | ||
}); | ||
}); | ||
|
||
function getDataFromEntity(entity: TickerEntity): TickerEntityCreateParams { | ||
return { | ||
exchange: entity.exchange, | ||
symbol: entity.symbol, | ||
time: entity.time, | ||
ask: fixByDigits(entity.ask, 0), | ||
bid: fixByDigits(entity.bid, 0), | ||
open: fixByDigits(entity.open, 0), | ||
high: fixByDigits(entity.high, 0), | ||
low: fixByDigits(entity.low, 0), | ||
last: fixByDigits(entity.last, 0), | ||
volume: fixByDigits(entity.volume, 0), | ||
}; | ||
} |
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,31 @@ | ||
import { EntityRepository, Repository } from 'typeorm'; | ||
|
||
import { TickerEntity } from './ticker.entity'; | ||
|
||
export interface TickerEntityCreateParams { | ||
exchange: TickerEntity['exchange']; | ||
symbol: TickerEntity['symbol']; | ||
ask: TickerEntity['ask']; | ||
bid: TickerEntity['bid']; | ||
time: TickerEntity['time']; | ||
open: TickerEntity['open']; | ||
high: TickerEntity['high']; | ||
low: TickerEntity['low']; | ||
last: TickerEntity['last']; | ||
volume: TickerEntity['volume']; | ||
} | ||
|
||
@EntityRepository(TickerEntity) | ||
export class TickerRepository extends Repository<TickerEntity> { | ||
async insertNewTicker(params: TickerEntityCreateParams): Promise<TickerEntity> { | ||
return this.save(params as TickerEntity, { reload: false }); | ||
} | ||
|
||
async insertNewTickers(params: TickerEntityCreateParams[]): Promise<TickerEntity[]> { | ||
return this.save(params as TickerEntity[], { reload: false }); | ||
} | ||
|
||
async getTickers(exchange: string, symbol: string): Promise<TickerEntity[]> { | ||
return this.find({ where: { exchange, symbol } }); | ||
} | ||
} |
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,20 @@ | ||
import { TickerEntityCreateParams } from '@dripjs/models'; | ||
import { Timestamp } from '@dripjs/types'; | ||
|
||
// use function to avoid object mutating when insert to db | ||
export function getTickerDefaultData(): TickerEntityCreateParams[] { | ||
return [ | ||
{ | ||
exchange: 'bitmex', | ||
symbol: 'xbtusd', | ||
time: Date.now() as Timestamp, | ||
bid: '4820', | ||
ask: '4821', | ||
open: '4800', | ||
last: '4821', | ||
low: '4800', | ||
high: '4832', | ||
volume: '10', | ||
}, | ||
]; | ||
} |