@qfin/schemas v0.0.31
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 pushwill 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 devThis 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 prodThis 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 testBuilding
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
itemsarray<Model.instance> The array containing the found model instancescountnumber The total number of items retured after filteringscannednumber The total number of items scanned before filteringtimesQueriednumber? The total number times the items were queriedlastKeyobject? 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
propsobject? 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.htmlpropsobject 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
filterExpressionstring The filter expression as par DynamoDB documentationsattributeValuesstring The filter attribute values as par DynamoDB documentationspropsobject? 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
keystring The key to filter byvalueany The value to search for. This will be matched exactly (full and case senstive)propsobject? 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
propsobject? 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
queryobject The query objectindexstring The name of the global index to usepropsobject? The optional propertiesprops.offsetobject? The last key offset for paginatiopnprops.limitnumber The number of documents to return. Defaults to 100. (optional, default100)props.attributesArray<string>? The attributes to return from this scanprops.sortnumber 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
pathstring 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
pathstring The dot separated property pathvalueany The value to set for the property
Returns void
CorporateAction.Schema
Schema of CorporateAction
Type: object
Parameters
idNumber The unique idtimestampNumber The timestamp of the ohlc pointpurposeNumberexDateNumber 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).recordDateNumber 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.bcStartDateNumber 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.bcEndDateNumber Book closure end datendStartDateNumberndEndDateNumber
Examples
const corporateAction = {
"id": 123456,
"timestamp": 1554651528383,
"purpose": CorporateAction.PurposeType.AGM,
};CorporateAction.PurposeType
The available exchnage values
Type: object
Parameters
AGMNumber Annual General Meeting AnnouncementBONDNumber Bond AnnouncementBONUSNumber Bonus AnnouncementBUYBACKNumber Buy Back AnnouncementFIRST_CALLNumber First Call AnnouncementDIVIDENDNumber Dividend AnnouncementINT_DIVIDENDNumber Interim Dividend AnnouncementDISTRIBUTIONNumber Distribution AnnouncementINTEREST_PAYMENTNumber Interest Payment AnnouncementREDEMPTIONNumber Redemption AnnouncementSTOCK_SPLITNumber Stock Split AnnouncementCONSOLIDATIONNumber 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
valString 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
propsobject The properties used to generate the unique idprops.countrynumber The country code for this instrument Instrument.Countryprops.exchangenumber The exchange id for this instrument Instrument.Exchangeprops.symbolstring The symbol for this instrumentprops.seriesstring 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
propsobject 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
countrynumber 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
s3object The AWS S3 instancebucketstring? The upload bucket. Defaults to${process.env.NODE_ENV || 'production'}.data.quantofin.comkeystring? 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
s3object The AWS S3 instancedata(string | Buffer) The json data to uploadbucketstring? The upload bucket. Defaults to${process.env.NODE_ENV || 'production'}.data.quantofin.comkeystring? 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
s3object The AWS S3 instancebucketstring? The upload bucket. Defaults to${process.env.NODE_ENV || 'production'}.data.quantofin.comkeystring? 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
valString 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
valString 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
INDEXNumber Index Type: 10INDEX_FUTURENumber Index Future Type: 11EQNumber Equity Type: 20EQ_FUTURENumber Equity Fture Type: 21OPTION_CENumber European Call Option Type: 31OPTION_PENumber European Put Option Type: 32
Instrument.InstrumentType.fromString
Convert string value to InstrumentType value
Type: function
Parameters
valString 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
idnumber The unique instrument id for this instrument. This is autogenerated based on the exchnage and symbol fieldscountrynumber The country code to which this instrument belongsexchangenumber The exchange type Instrument.ExchangeinstrumentTypenumber The instrument type Instrument.InstrumentTypesymbolstring The symbol for the instrument - unit across a given exchangeseriesstring The series this instrument belongs to. For example: NSE has EQ, BE etc. seriesnamestring The name of the instrumentexchangeIdstring The unique token provided by the Exchnage for this instrumentbrokerIdobject The unique token provided by the given broker for this instrumentcircuitobject The upper and lower circuit limits for this instrumentisDeletedboolean? Whether or not the instrument is deleted from the ExchnageisEnabledboolean? Whether or not the instrument is enabled in the ExchnageisTradeableboolean? Whether or not the instrument is tradeable in the Exchnage. For eg. Indices are not tradeable in India, but there Future contracts areisinstring? The unique ISIN value for the instrumentfaceValuenumber? The face value of the instrumentissuedCapitalnumber? The issued capital of the instrumentstrikePricenumber? The strike price in case the instrument is an Option contracttickSizenumber? The minimum size move allowed for this instrumentlotSizenumber? The minimum amount of quantity and its multiple allowed for trading. This is relevant to Future & Option contractsexpirynumber? The expiry timestamp for the instrument contractsectorobject? The sectoral information this instrument belongs toyearlyobject? The yearly extremeties of the stockopenInterestobject? The open interest of the instrumentohlcobject? The daily ohlc data pointsohlc.openobject? The current day's open priceohlc.highobject? The current day's highohlc.lowobject? The current day's lowohlc.closeobject? The current day's closeohlc.lastTradedPriceobject? The current day's last price at which the intrument was tradedohlc.previousCloseobject? The previous day's closeohlc.volumeobject? The total traded volume or quantity tradedohlc.tradesobject? 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
idnumber The instrument idperiodnumber The period of candle Ohlc.Period (optional, defaultOhlc.Period.DAILY)offsetobject? The last key offsetlimitnumber The number of documents to return. Defaults to 100 (optional, default100)sortnumber 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_MINNumber Intraday 1 minute candles: 1INTRADAY_5_MINNumber Intraday 5 minutes candles: 5INTRADAY_10_MINNumber Intraday 10 minutes candles: 10INTRADAY_15_MINNumber Intraday 15 minutes candles: 15INTRADAY_30_MINNumber Intraday 30 minutes candles: 30INTRADAY_60_MINNumber Intraday 60 minutes candles: 60DAILYNumber Daily candles: 101
Ohlc.Period.fromString
Convert string value to Period value
Type: function
Parameters
valString 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
idNumber The unique idtimestampNumber The timestamp of the ohlc pointperiodNumber The period of the ohlc candle Ohlc.PeriodopenNumber The open value of the candlehighNumber The high value of the candlelowNumber The low value of the candlecloseNumber The close value of the candlelastTradedPriceNumber? The last traded price of the instrumentpreviousCloseNumber? The close value for the previous dayvolumeNumber? The volume tradedtradesobject? The trades related informationadjustedobject? 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
propsobject The user object properties based on User.Schema
Examples
const userProps = {
email: 'user@email.com',
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
emailstring The user's email address
Examples
await User.findOneByEmail('user@email.com');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
providerstring The provider - google, facebook, upstox, zerodhaidstring 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
rolenumber 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
passwordstring 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_EXPIREDstring Due to license expiryUSER_BLOCKEDstring Because the user is blocked by AdminIP_BLOCKEDstring Because the user's IP is blocked by Admin
User.Role
A map of available User Roles
Type: object
Parameters
SUPER_ADMINnumber The super admin roleADMINnumber The admin rolePARTNERnumber The partner rolePREMIUM_USERnumber The premium user roleUSERnumber The user role. Everyone gets this role
User.Role.fromString
Convert string value to Role value
Type: function
Parameters
valString 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
idstring The unique user id for this Authtokenstring The unique access token for this Authemailstring? The email address for this Authprofileobject? The user's profile details
Examples
const auth = {
id: 'ABC123',
token: 'someuniqueauthtoken',
email: 'user@email.com',
profile: {
avatarUrl: 'https://avatar.auth.url',
firstname: 'First',
lastname: 'Last',
phone: '+911234567890',
}
};User.Schema
Schema of User
Type: module.User.Schema
Parameters
idnumber The unique user id for this Auth (Auto generated if absent)createdAtnumber The unix timestamp when the object was created (in milliseconds)updatedAtnumber The unix timestamp when the object was updated (in milliseconds)emailstring The user's primary emailrolesarray The list of User.Rolepasswordstring? The password for user login - atleast 1 lowercase, 1 uppercase, 1 number, 1 special ch. and must be 8 or more charsreferralobject User's referral detailsprofileobject? The user profileauthobject? The user's other auth objectsauth.googleobject? The Google oauth details User.AuthSchemaauth.facebookobject? The Facebook oauth details User.AuthSchemaauth.upstoxobject? The Upstox broker oauth details User.AuthSchemaauth.zerodhaobject? The Zerodha broker oauth details User.AuthSchema
paymentobject? The user's payment informationsubscriptionobject? The user's various subscription detailscheckobject? Various type of boolean checkscheck.isVerifiedboolean? Whether the user is verified or notcheck.isSuspendedboolean? Whether the user is suspended or notcheck.isDeletedboolean? Whether the user is deleted or notcheck.suspensionReasonstring? The reason for user's account suspension User.AccountSuspensionReason
Examples
const user = {
id: 123,
email: 'user@email.com',
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_HIGHNumber : 1001OPEN_EQUALS_LOWNumber : 1002GAINERS_AND_LOSERSNumber : 1003OPEN_RANGE_BREAKOUTNumber : 1004OVER_BOUGHT_AND_SOLDNumber : 1005GAP_UP_AND_DOWNNumber : 1006CARAMILLA_POINTSNumber : 1007
Scanner.Name.fromString
Convert string value to Scanner Name value
Type: function
Parameters
valString 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
valString 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
nameNumber The name of the scanner Scanner.NametypeNumber The type of the scanner Scanner.TypedataObject The computed data of the scanner
Examples
const scanner = {
"name": 1001,
"scannerType": 1,
"data": {
"instruments": [3371985954]
},
};6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago