@qfin/schemas
v0.0.31
Published
The awesome database schemas for QFin
Downloads
2
Readme
The awesome database schemas for QFin 😎 ❤
📦 Features
- Support for ES6 modules
- Unit testing using Jest
- All the usual goodies: ESLint, Babel, Webpack etc.
- Auto code formatting using Prettier
- Automated versioning
- Automated changelog generator
- Automated readme file and source documentation generator
- Git Hooks integrations -
git push
will trigger auto build, versioning, documentation generation etc. - Watch for file changes in development mode
- Local in-memory DynamoDB database support
💾 Install
Latest Release: @qfin/schemas:0.0.31
npm install @qfin/schemas
💻 Development
Most of the things are automated so as a developer you will have less things to worry or care about. Simply write code and let us take care of the rest.
Running
Development Mode
You can run the development mode of the application by simply running the following command:
npm run dev
This will trigger a development
build and the build system will skip testing, generating documentations etc. thereby reducing the build time significantly.
Once the build is completed, the application will start in development mode.
NOTE:
In development
mode only, the system will watch for file changes. Any further changes to the source files will trigger a rebuild and will restart the application automatically and also the local database.
Production Mode
The production mode of the application can be started as follows:
npm run prod
This will trigger a production
build and will do all the relevant automation of testing, generating documentations etc.
Once the build process is completed, the application will start in production mode. Changes to the filesystem will not be watched under this mode.
Testing
Tests will be automatically run when you do git push
.
Test files should end with .spec.js
filename and should be kept in a __tests__/
directory under respective places as a good convention.
You can always manually run the test as follows:
npm run test
Building
The project will be automatically built when you do git push
.
Production build may take a couple of minutes as several steps are involved in the process such as running test coverage, generating documentations, building docker image etc.
Ideally you dont need to build the project manually unless you are testing the build system itself.
npm run build
🔗 Git
Git Commit
All git commit
messages will be logged in the CHANGELOG file automatically. Hence it is advisable to commit your code often with small changes and give meaningful commit message.
Try to follow git flow branching model.
The seven rules of a great Git commit message (adapted from here):
- Separate subject from body with a blank line
- Limit the subject line to 50 characters
- Capitalize the subject line
- Do not end the subject line with a period
- Use the imperative mood in the subject line
- Wrap the body at 72 characters
- Use the body to explain what and why vs. how
Git Push
Doing git push
will trigger the git-hooks which will do a bunch of stuffs for you automatically:
- Increment the patch version of the project
- Trigger a production build
- Running tests
- Generating project documentations
- Update CHANGELOG with all git commits until the point of last push
- Update the README file
- Update the version in docker-compose.yml file
- Generating new versioned docker image
NOTE: If you wish to skip these automations, then you should do git push --no-verify
.
One usecase would be to skip incrementing the package version when merging two branches and pushing the merged code.
⚙ QFin Schemas Documentation
PurposeType
@qfin/schemas
defines various object schemas and corresponding helper methods that can be used to do CRUD operations across DynamoDB databases.
Type: object
Examples
const Schemas = require('@qfin/schemas');
// OR
import Schemas from '@qfin/schemas';
const {Announcement, Instrument, Ohlc, User} = Schemas;
Model.formatDynamooseResults
This is the general format of the returned results from the model query or scan methods.
Type: object
Parameters
items
array<Model.instance> The array containing the found model instancescount
number The total number of items retured after filteringscanned
number The total number of items scanned before filteringtimesQueried
number? The total number times the items were queriedlastKey
object? The last key used for pagination
Model.createNew
Create a new model object Every new model instance will have a unique random id which will be autogenerated if absent from the props.
NOTE:
All models implements this method and can have its unique behavior. Check model specific documentation for more details (if any).
Type: function
Parameters
props
object? The model object properties based on the model's schema
Returns Promise<Model.instance> A promise containing the newly created model object
Model.scanWithFilter
Scan a given model with a given set of filter object
NOTE:
All models implements this method and can have its unique behavior. Check model specific documentation for more details (if any).
Type: function
Parameters
filter
(object | undefined) The filter object https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Query.htmlprops
object The scan properties
Examples
const { items, lastKey} = await Schema.scanWithFilter({
FilterExpression: filterExpression,
ExpressionAttributeValues: attributeValues,
});
Returns Promise<Model.formatDynamooseResults> A promise containing the model result Model.formatDynamooseResults
Model.scanWithFilterExpression
Scan a given model with a given set of filter expression and value
NOTE:
All models implements this method and can have its unique behavior. Check model specific documentation for more details (if any).
Type: function
Parameters
filterExpression
string The filter expression as par DynamoDB documentationsattributeValues
string The filter attribute values as par DynamoDB documentationsprops
object? The optional scan properties
Examples
const { items, lastKey} = await Schema.scanWithFilterExpression(`${key} = :prop`, { ':prop': value });
Returns Promise<Model.formatDynamooseResults> A promise containing the model result Model.formatDynamooseResults
Model.scanWithKeyValue
Scan a given model with a given set of key-value condition. Match will look for equality.
NOTE:
All models implements this method and can have its unique behavior. Check model specific documentation for more details (if any).
Type: function
Parameters
key
string The key to filter byvalue
any The value to search for. This will be matched exactly (full and case senstive)props
object? The optional scan properties
Examples
const { items, lastKey} = await Schema.scanWithKeyValue('xyz', 10);
Returns Promise<Model.formatDynamooseResults> A promise containing the model result Model.formatDynamooseResults
Model.scanAll
Scan a given model to find all instances in a paginated way.
NOTE:
All models implements this method and can have its unique behavior. Check model specific documentation for more details (if any).
Type: function
Parameters
props
object? The optional scan properties
Returns Promise<Model.formatDynamooseResults> A promise containing the model result Model.formatDynamooseResults
Model.findByIndex
Queries a given model to search by a global index.
NOTE:
All models implements this method and can have its unique behavior. Check model specific documentation for more details (if any).
Type: function
Parameters
query
object The query objectindex
string The name of the global index to useprops
object? The optional propertiesprops.offset
object? The last key offset for paginatiopnprops.limit
number The number of documents to return. Defaults to 100. (optional, default100
)props.attributes
Array<string>? The attributes to return from this scanprops.sort
number Sort the data. Positive sorts in ascending order, negative in descending. (optional, default-1
)
Returns Promise<Model.formatDynamooseResults> A promise containing the model result Model.formatDynamooseResults
Model.insance.get
Get the property value of a model instance using string dot path. https://github.com/aheckmann/mpath
NOTE:
This is an insance level method. All models implements this method and can have its unique behavior. Check model specific documentation for more details (if any).
Type: function
Parameters
path
string The dot separated property path
Returns any The value of the property
Model.insance.set
Set the property value of a model instance using string dot path. https://github.com/aheckmann/mpath
The model is NOT saved at this point, you need to handle saving to database separately by calling the save
method.
NOTE:
This is an insance level method. All models implements this method and can have its unique behavior. Check model specific documentation for more details (if any).
Type: function
Parameters
path
string The dot separated property pathvalue
any The value to set for the property
Returns void
CorporateAction.Schema
Schema of CorporateAction
Type: object
Parameters
id
Number The unique idtimestamp
Number The timestamp of the ohlc pointpurpose
NumberexDate
Number The date at which a stock will trade "cum ex" (without entitlement). So for example in a normal cash dividend, if the exdate is 25.11.2008 then the stock will trade without the right to the cash dividendfrom the 25.11.2008 onwards. Cum (latin for with) and Ex (latin for without).recordDate
Number The date at which your positions will be recorded in order to calculate your entitlements. So for example; if the positions in your account on record date are 100,000 shares and a cash dividend pays EUR 0.25 per share then your entitlement will be calculated as 100,000 x EUR 0.25 = EUR 25,000.bcStartDate
Number Book closure is a time period during which a company will not handle adjustments to the register, or requests to transfer shares. Companies will often use the book closure date to identify the cut-off date for determining which investors of record will receive a particular dividend payment.bcEndDate
Number Book closure end datendStartDate
NumberndEndDate
Number
Examples
const corporateAction = {
"id": 123456,
"timestamp": 1554651528383,
"purpose": CorporateAction.PurposeType.AGM,
};
CorporateAction.PurposeType
The available exchnage values
Type: object
Parameters
AGM
Number Annual General Meeting AnnouncementBOND
Number Bond AnnouncementBONUS
Number Bonus AnnouncementBUYBACK
Number Buy Back AnnouncementFIRST_CALL
Number First Call AnnouncementDIVIDEND
Number Dividend AnnouncementINT_DIVIDEND
Number Interim Dividend AnnouncementDISTRIBUTION
Number Distribution AnnouncementINTEREST_PAYMENT
Number Interest Payment AnnouncementREDEMPTION
Number Redemption AnnouncementSTOCK_SPLIT
Number Stock Split AnnouncementCONSOLIDATION
Number A reverse stock split is a type of corporate action which consolidates the number of existing shares of stock into fewer, proportionally more valuable, shares. The process involves a company reducing the total number of its outstanding shares in the open market, and often signals a company in distress.
CorporateAction.PurposeType.fromString
Convert string value to InstrumentType value
Type: function
Parameters
val
String The string value such as AGM, DIVIDEND etc.
Returns Number The PurposeType value {@see CorporateAction.PurposeType}
Instrument.generateId
Generate instrument id out of exchange and symbol
Type: function
Parameters
props
object The properties used to generate the unique idprops.country
number The country code for this instrument Instrument.Countryprops.exchange
number The exchange id for this instrument Instrument.Exchangeprops.symbol
string The symbol for this instrumentprops.series
string The series this instrument belongs to
Examples
const id = Instrument.generateId({
country: Instrument.Country.INDIA,
exchange: Instrument.Exchange.INDIA_NSE,
series: 'EQ',
symbol: 'RELIANCE',
});
Returns number The generated unique id for this instrument
Instrument.createNew
Create a new instrument Every new instrument will have a unique id which will be autogenerated based on the exchnage and symbol fields
Type: function
Parameters
props
object The instrument object properties based on Instrument.Schema
Examples
const instrument = await Instrument.createNew({
// id: 3371985954, auto-generated constant id for this isntrument
country: Instrument.Country.INDIA,
exchange: Instrument.Exchange.INDIA_NSE,
instrumentType: Instrument.InstrumentType.EQ,
series: 'EQ',
symbol: 'RELIANCE',
name: 'Reliance Industries Limited',
exchangeId: '2885',
brokerId: {
zerodha: 1012012,
upstox: '2885',
},
});
Returns Promise<Instrument.instance> A promise containing the newly created instrument
Instrument.getAllContracts
Get all contracts
Type: function
Parameters
country
number The country for which all contracts are needed (optional, defaultInstrument.Country.INDIA
)
Returns Promise<Array<Instrument.instance>> A promise containing the contracts array
Instrument.getAllContractsCopy
Get all the contracts data from AWS S3 copy
Type: function
Parameters
s3
object The AWS S3 instancebucket
string? The upload bucket. Defaults to${process.env.NODE_ENV || 'production'}.data.quantofin.com
key
string? The upload file key. Defaults tocontracts.json
Returns Promise<Object> Contracts data
Instrument.saveAllContractsCopy
Save a copy of all the contracts as file to AWS S3
Type: function
Parameters
s3
object The AWS S3 instancedata
(string | Buffer) The json data to uploadbucket
string? The upload bucket. Defaults to${process.env.NODE_ENV || 'production'}.data.quantofin.com
key
string? The upload file key. Defaults tocontracts.json
Returns Promise<Object> AWS S3 promise object
Instrument.deleteContracts
Delete the contract file from AWS S3
Type: function
Parameters
s3
object The AWS S3 instancebucket
string? The upload bucket. Defaults to${process.env.NODE_ENV || 'production'}.data.quantofin.com
key
string? The upload file key. Defaults tocontracts.json
Returns Promise<Object> AWS S3 promise object
Instrument.Country
The available Countries
Type: object
Parameters
Instrument.Country.fromString
Convert string value to Country value
Type: function
Parameters
val
String The string value such as INDIA, IND etc.
Returns Number The Country value {@see Instrument.Country}
Instrument.Exchange
The available Exchnages
Type: object
Parameters
Instrument.Exchange.fromString
Convert string value to Exchange value
Type: function
Parameters
val
String The string value such as NSE, BSE, MCX etc.
Returns Number The Exchange value {@see Instrument.Exchange}
Instrument.InstrumentType
The available exchnage values
Type: object
Parameters
INDEX
Number Index Type: 10INDEX_FUTURE
Number Index Future Type: 11EQ
Number Equity Type: 20EQ_FUTURE
Number Equity Fture Type: 21OPTION_CE
Number European Call Option Type: 31OPTION_PE
Number European Put Option Type: 32
Instrument.InstrumentType.fromString
Convert string value to InstrumentType value
Type: function
Parameters
val
String The string value such as EQ, FUT, CE, PE etc.
Returns Number The InstrumentType value {@see Instrument.InstrumentType}
Instrument.Schema
Schema of Instrument
Type: object
Parameters
id
number The unique instrument id for this instrument. This is autogenerated based on the exchnage and symbol fieldscountry
number The country code to which this instrument belongsexchange
number The exchange type Instrument.ExchangeinstrumentType
number The instrument type Instrument.InstrumentTypesymbol
string The symbol for the instrument - unit across a given exchangeseries
string The series this instrument belongs to. For example: NSE has EQ, BE etc. seriesname
string The name of the instrumentexchangeId
string The unique token provided by the Exchnage for this instrumentbrokerId
object The unique token provided by the given broker for this instrumentcircuit
object The upper and lower circuit limits for this instrumentisDeleted
boolean? Whether or not the instrument is deleted from the ExchnageisEnabled
boolean? Whether or not the instrument is enabled in the ExchnageisTradeable
boolean? Whether or not the instrument is tradeable in the Exchnage. For eg. Indices are not tradeable in India, but there Future contracts areisin
string? The unique ISIN value for the instrumentfaceValue
number? The face value of the instrumentissuedCapital
number? The issued capital of the instrumentstrikePrice
number? The strike price in case the instrument is an Option contracttickSize
number? The minimum size move allowed for this instrumentlotSize
number? The minimum amount of quantity and its multiple allowed for trading. This is relevant to Future & Option contractsexpiry
number? The expiry timestamp for the instrument contractsector
object? The sectoral information this instrument belongs toyearly
object? The yearly extremeties of the stockopenInterest
object? The open interest of the instrumentohlc
object? The daily ohlc data pointsohlc.open
object? The current day's open priceohlc.high
object? The current day's highohlc.low
object? The current day's lowohlc.close
object? The current day's closeohlc.lastTradedPrice
object? The current day's last price at which the intrument was tradedohlc.previousClose
object? The previous day's closeohlc.volume
object? The total traded volume or quantity tradedohlc.trades
object? The trades related information
Examples
const instrument = {
// id: 3371985954, auto-generated constant id for this isntrument
country: Instrument.Country.INDIA,
exchange: Instrument.Exchange.INDIA_NSE,
instrumentType: Instrument.InstrumentType.EQ,
series: Instrument.Series.EQ,
symbol: 'RELIANCE',
name: 'Reliance Industries Limited',
exchangeId: '2885',
brokerId: {
zerodha: 1012012,
upstox: '2885',
},
};
Ohlc.getCandles
Get all candles for a given instrument and period
Type: module.Ohlc.getCandles
Parameters
id
number The instrument idperiod
number The period of candle Ohlc.Period (optional, defaultOhlc.Period.DAILY
)offset
object? The last key offsetlimit
number The number of documents to return. Defaults to 100 (optional, default100
)sort
number Sort the data. Positive sorts in ascending order, negative in descending. (optional, default-1
)
Examples
const candles = await Model.getCandles(123456, {
period: Model.Period.INTRADAY_1_MIN,
start: Date.now() - 9000000,
limit: 200,
sort: 1,
});
Returns Promise<Model.formatDynamooseResults> A promise containing the model result Model.formatDynamooseResults
Ohlc.Period
The available ohlc periods
Type: module.Ohlc.Period
Parameters
INTRADAY_1_MIN
Number Intraday 1 minute candles: 1INTRADAY_5_MIN
Number Intraday 5 minutes candles: 5INTRADAY_10_MIN
Number Intraday 10 minutes candles: 10INTRADAY_15_MIN
Number Intraday 15 minutes candles: 15INTRADAY_30_MIN
Number Intraday 30 minutes candles: 30INTRADAY_60_MIN
Number Intraday 60 minutes candles: 60DAILY
Number Daily candles: 101
Ohlc.Period.fromString
Convert string value to Period value
Type: function
Parameters
val
String The string value such as DAILY, 1_MIN, 30_MIN etc.
Returns Number The Period value {@see Ohlc.Period}
Ohlc.Schema
Schema of Ohlc Canlde Data
Type: object
Parameters
id
Number The unique idtimestamp
Number The timestamp of the ohlc pointperiod
Number The period of the ohlc candle Ohlc.Periodopen
Number The open value of the candlehigh
Number The high value of the candlelow
Number The low value of the candleclose
Number The close value of the candlelastTradedPrice
Number? The last traded price of the instrumentpreviousClose
Number? The close value for the previous dayvolume
Number? The volume tradedtrades
object? The trades related informationadjusted
object? The adjusted values
Examples
const ohlc = {
"id": 123456,
"timestamp": 1554651528383,
"open": 627.52,
"high": 627.52,
"low": 621.83,
"close": 622.98,
};
User.createNew
Create a new user Every new user will have a unique random id and a unique referral.id and these will be autogenerated if absent from the props. NOTE: If a password field is present, it will be treated as plain text and will be auto encrypted before creating the user.
Type: function
Parameters
props
object The user object properties based on User.Schema
Examples
const userProps = {
email: '[email protected]',
password: 'Password@123',
roles: [ 'user' ],
referral: {
id: 'ABCD',
},
profile: {
firstname: 'First',
lastname: 'Last',
avatarUrl: 'https://avatar.auth.url',
}
};
await User.createNew(userProps);
Returns Promise<User.instance> A promise containing the newly created user
User.findOneByEmail
Search a user by her email address. NOTE: The provided email must be an exact case-insensitive match. This can't match against partial email address.
Type: function
Parameters
email
string The user's email address
Examples
await User.findOneByEmail('[email protected]');
Returns Promise<User.instance> A promise containing the found user
User.findOneByOAuthId
Search a user by her OAuth provider and id. NOTE: This is a scan and not a query hence will be slow and is not recommended to call often
Type: function
Parameters
provider
string The provider - google, facebook, upstox, zerodhaid
string The user's id as given by the provider
Examples
await User.findOneByOAuthId('google', 'G-1234');
Returns Promise<User.instance> A promise containing the found user
User.instance.hasRole
Check if the user has a given role or not NOTE: This is an instance method
Type: function
Parameters
role
number The role to check User.Roles
Examples
user.hasRole(User.Roles.ADMIN);
Returns boolean Whether or not the user has the given role
User.instance.isValidPassword
Check if the given plain text password matches with the user's encrypted password NOTE: This is an instance method
Type: function
Parameters
password
string The password to check as plain text
Examples
user.isValidPassword('abcd');
Returns boolean Whether or not the given password matches against the user's set password
User.AccountSuspensionReason
Various reasons for account suspension
Type: object
Parameters
LICENSE_EXPIRED
string Due to license expiryUSER_BLOCKED
string Because the user is blocked by AdminIP_BLOCKED
string Because the user's IP is blocked by Admin
User.Role
A map of available User Roles
Type: object
Parameters
SUPER_ADMIN
number The super admin roleADMIN
number The admin rolePARTNER
number The partner rolePREMIUM_USER
number The premium user roleUSER
number The user role. Everyone gets this role
User.Role.fromString
Convert string value to Role value
Type: function
Parameters
val
String The string value such as admin, user etc.
Returns Number The Role value {@see User.Role}
User.AuthSchema
AuthSchema of User Generally this schema is used for OAuth authorization and login
Type: module.User.AuthSchema
Parameters
id
string The unique user id for this Authtoken
string The unique access token for this Authemail
string? The email address for this Authprofile
object? The user's profile details
Examples
const auth = {
id: 'ABC123',
token: 'someuniqueauthtoken',
email: '[email protected]',
profile: {
avatarUrl: 'https://avatar.auth.url',
firstname: 'First',
lastname: 'Last',
phone: '+911234567890',
}
};
User.Schema
Schema of User
Type: module.User.Schema
Parameters
id
number The unique user id for this Auth (Auto generated if absent)createdAt
number The unix timestamp when the object was created (in milliseconds)updatedAt
number The unix timestamp when the object was updated (in milliseconds)email
string The user's primary emailroles
array The list of User.Rolepassword
string? The password for user login - atleast 1 lowercase, 1 uppercase, 1 number, 1 special ch. and must be 8 or more charsreferral
object User's referral detailsprofile
object? The user profileauth
object? The user's other auth objectsauth.google
object? The Google oauth details User.AuthSchemaauth.facebook
object? The Facebook oauth details User.AuthSchemaauth.upstox
object? The Upstox broker oauth details User.AuthSchemaauth.zerodha
object? The Zerodha broker oauth details User.AuthSchema
payment
object? The user's payment informationsubscription
object? The user's various subscription detailscheck
object? Various type of boolean checkscheck.isVerified
boolean? Whether the user is verified or notcheck.isSuspended
boolean? Whether the user is suspended or notcheck.isDeleted
boolean? Whether the user is deleted or notcheck.suspensionReason
string? The reason for user's account suspension User.AccountSuspensionReason
Examples
const user = {
id: 123,
email: '[email protected]',
password: 'Password@123',
roles: [User.Role.USER],
referral: {
id: 'ABCD',
},
auth: {
google: {#User.AuthSchema}
},
profile: {
firstname: 'First',
lastname: 'Last',
avatarUrl: 'https://avatar.auth.url',
}
};
Scanner.Name
The available Scanners
Type: object
Parameters
OPEN_EQUALS_HIGH
Number : 1001OPEN_EQUALS_LOW
Number : 1002GAINERS_AND_LOSERS
Number : 1003OPEN_RANGE_BREAKOUT
Number : 1004OVER_BOUGHT_AND_SOLD
Number : 1005GAP_UP_AND_DOWN
Number : 1006CARAMILLA_POINTS
Number : 1007
Scanner.Name.fromString
Convert string value to Scanner Name value
Type: function
Parameters
val
String The string value such as OPEN_EQUALS_HIGH, GAINERS_AND_LOSERS, OVER_BOUGHT_AND_SOLD etc.
Returns Number The Scanner Name value {@see Scanner.Name}
Scanner.Type
The available Scanner Types
Type: object
Parameters
Scanner.Type.fromString
Convert string value to Scanner Type value
Type: function
Parameters
val
String The string value such as FEED, CANDLE etc.
Returns Number The Scanner Type value {@see Scanner.Type}
Scanner.Schema
Schema of Scanner Data
Type: Object
Parameters
name
Number The name of the scanner Scanner.Nametype
Number The type of the scanner Scanner.Typedata
Object The computed data of the scanner
Examples
const scanner = {
"name": 1001,
"scannerType": 1,
"data": {
"instruments": [3371985954]
},
};