-
-
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
80d871b
commit 1112cec
Showing
12 changed files
with
538 additions
and
25 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 |
---|---|---|
|
@@ -9,8 +9,8 @@ | |
.history | ||
|
||
# source | ||
projects | ||
modules | ||
/projects | ||
/modules | ||
index.ts | ||
|
||
# configuration | ||
|
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 @@ | ||
export * from './transformers'; |
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,81 @@ | ||
type StringTransformerFunction = (value?: string | null | undefined) => string | null | undefined; | ||
|
||
interface StringTransformer { | ||
from: StringTransformerFunction; | ||
to: StringTransformerFunction; | ||
} | ||
|
||
export function getFloorByDigitsTransformer(digit: number): StringTransformer { | ||
return { | ||
from: (value?: string | null | undefined) => value, | ||
to: (value?: string | null | undefined) => { | ||
if (value === undefined || value === null) { | ||
return value; | ||
} | ||
|
||
const [integer, fraction] = value.split('.'); | ||
if (fraction === undefined) { | ||
return value; | ||
} | ||
|
||
const fractionReduced = fraction.substr(0, digit); | ||
|
||
return `${integer}.${fractionReduced}`; | ||
}, | ||
}; | ||
} | ||
|
||
export const nullableDateTransformer = { | ||
from: (value?: Date | null | undefined | number) => { | ||
if (value === undefined || value === null) { | ||
return value; | ||
} | ||
|
||
if (value instanceof Date) { | ||
return value.getTime(); | ||
} | ||
|
||
return value; | ||
}, | ||
to: (value?: number | null | undefined) => { | ||
if (value === undefined || value === null) { | ||
return value; | ||
} | ||
|
||
return new Date(value); | ||
}, | ||
}; | ||
|
||
/** | ||
* Use this transformer for the following case. | ||
* - we want to use a value as `boolean` in application, but store it as `tinyint` into DB. | ||
*/ | ||
export const booleanTinyintTransformer = { | ||
from: (val: 0 | 1): boolean => !!val, | ||
to: (val: boolean) => { | ||
if (val === undefined || val === null) { | ||
return val; | ||
} | ||
const n = Number(val); | ||
if (Number.isNaN(n)) { | ||
throw new Error(`Invalid boolean value ${val}`); | ||
} | ||
|
||
return n as 0 | 1; | ||
}, | ||
}; | ||
|
||
/** | ||
* Use this when tinyint has a default value i.e. '0' or '1'. | ||
* @see MasterPairEntity | ||
*/ | ||
export const nullableBooleanTinyintTransformer = { | ||
from: (val: 0 | 1): boolean => !!val, | ||
to: (val: boolean | null | undefined) => { | ||
if (val === undefined || val === null) { | ||
return val; | ||
} | ||
|
||
return Number(val) as 0 | 1; | ||
}, | ||
}; |
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 @@ | ||
export * from './master-pair.entity'; |
136 changes: 136 additions & 0 deletions
136
modules/models/entity/master-pair/master-pair.entity.ts
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,136 @@ | ||
import { Pair, Timestamp } from '@dripjs/types'; | ||
import { Column, Entity, Index, PrimaryGeneratedColumn } from 'typeorm'; | ||
|
||
import { getFloorByDigitsTransformer, nullableBooleanTinyintTransformer, nullableDateTransformer } from '../../common'; | ||
|
||
@Entity({ | ||
name: 'master_pair', | ||
}) | ||
@Index('unique_base_asset_quote_asset', ['baseAsset', 'quoteAsset'], { unique: true }) | ||
export class MasterPairEntity { | ||
@PrimaryGeneratedColumn({ | ||
type: 'bigint', | ||
name: 'id', | ||
unsigned: true, | ||
comment: 'primary auto generated id', | ||
}) | ||
readonly id!: string; | ||
|
||
@Column({ | ||
type: 'varchar', | ||
name: 'name', | ||
length: 10, | ||
comment: 'special pair name', | ||
}) | ||
readonly name!: Pair['name']; | ||
|
||
@Column({ | ||
type: 'varchar', | ||
name: 'exchange', | ||
length: 10, | ||
comment: 'exchange of special pair name', | ||
}) | ||
readonly exchange!: Pair['exchange']; | ||
|
||
@Column({ | ||
type: 'varchar', | ||
name: 'base_asset', | ||
length: 10, | ||
comment: 'base asset name', | ||
}) | ||
readonly baseAsset!: Pair['baseAsset']; | ||
|
||
@Column({ | ||
type: 'varchar', | ||
name: 'quote_asset', | ||
length: 10, | ||
comment: 'quote asset name', | ||
}) | ||
readonly quoteAsset!: Pair['quoteAsset']; | ||
|
||
@Column({ | ||
type: 'int', | ||
name: 'price_precision', | ||
unsigned: true, | ||
comment: 'pair price precision', | ||
}) | ||
readonly pricePrecision!: Pair['pricePrecision']; | ||
|
||
@Column({ | ||
type: 'int', | ||
name: 'amount_precision', | ||
unsigned: true, | ||
comment: 'pair amount precision', | ||
}) | ||
readonly amountPrecision!: Pair['amountPrecision']; | ||
|
||
@Column({ | ||
type: 'decimal', | ||
name: 'min_order_amount', | ||
precision: 9, | ||
scale: 9, | ||
unsigned: true, | ||
comment: 'min Order amount', | ||
default: () => '0.0', | ||
transformer: getFloorByDigitsTransformer(9), | ||
}) | ||
readonly minOrderAmount!: Pair['minOrderAmount']; | ||
|
||
@Column({ | ||
type: 'int', | ||
name: 'max_order_amount', | ||
unsigned: true, | ||
comment: 'max Order amount', | ||
}) | ||
readonly maxOrderAmount!: Pair['maxOrderAmount']; | ||
|
||
@Column({ | ||
type: 'decimal', | ||
name: 'min_order_price', | ||
precision: 9, | ||
scale: 9, | ||
unsigned: true, | ||
comment: 'min Order price', | ||
default: () => '0.0', | ||
transformer: getFloorByDigitsTransformer(9), | ||
}) | ||
readonly minOrderPrice!: Pair['minOrderAmount']; | ||
|
||
@Column({ | ||
type: 'int', | ||
name: 'max_order_price', | ||
unsigned: true, | ||
comment: 'max Order price', | ||
}) | ||
readonly maxOrderPrice!: Pair['maxOrderAmount']; | ||
|
||
@Column({ | ||
type: 'tinyint', | ||
name: 'is_enabled', | ||
unsigned: true, | ||
width: 1, | ||
comment: 'whether pair is enable status', | ||
default: '0', | ||
transformer: nullableBooleanTinyintTransformer, | ||
}) | ||
readonly isEnabled!: Pair['isEnabled']; | ||
|
||
@Column({ | ||
type: 'datetime', | ||
name: 'created_at', | ||
precision: 3, | ||
default: () => 'NOW(3)', | ||
transformer: nullableDateTransformer, | ||
}) | ||
readonly createdAt!: Timestamp; | ||
|
||
@Column({ | ||
type: 'datetime', | ||
name: 'updated_at', | ||
precision: 3, | ||
default: () => '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
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
Oops, something went wrong.