npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@qfin/schema

v0.7.0

Published

The awesome repor for Quantofin schemas

Downloads

2

Readme

@qfin/schema


This module is part of the Quantofin project.

The awesome @qfin/schema for Quantofin apps ❤

💾 Install

npm install --save @qfin/schema

⚙ Module documentation

index

Remember to always validate the model before saving to database or sending over the wire.

Examples

const { User } = require('@qfin/schema');

const user = new User({
    emails: [{ email: '[email protected]' }],
    roles: [User.UserRole.ADMIN_ROLE],
});

 await user.validate({
    phones: [{ phone: '+9100000000', is_verified: true }],
});

//
// Support for Protocol Buffers:
//
// You can efficiently serialize/deserialize the data object to protocol buffers as follows
// By default all long 64bit integer values are forced to long.js formats
// Hence you will need to `npm install long` to support that

const UserProto = User.proto.User;
// encode
const encodedBuffer = UserProto.encode(UserProto.fromObject(user.props)).finish();
// decode
const userObject = UserProto.toObject(UserProto.decode(encodedBuffer));

Model

This is an abstract class which should be extended by all models

Type: module.Model

Parameters

  • schema Joi A Joi schema for the model
  • props object The model object properties

Examples

const {Model} = require('@qfin/schema');
const UserSchema = require('./userSchema');

class User extends Model {
    constructor(props) {
        super(UserSchema, props);
    }
}

getSchema

Gets the model schema reference

Returns Model

getProps

Gets the model properties as copy

Returns object Copy of the model props

toJSON

Formats the model props to json string

Parameters

  • shouldPrettify boolean Whether to prettify the json string (optional, default false)

Returns string The JSON string of the model

validate

Validate the given properties with the model schema. The given properties will be appended to the existing props and overwrite if any

Parameters

  • props object The new properties

Returns Promise<?> Resolves with the model instance or rejects with an error

update

Validates the properties and then saves the model by calling Model#save()

Parameters

  • props object The new props to validate and update

Returns Promise<(Model | Error)> Resolves with the saved model instance or rejects with an error

save

Abstract method, must be implemented by the child class

Returns Promise<(Model | Error)> Resolves with the saved model instance or rejects with an error

findAll

Abstract method, must be implemented by the child class

Parameters

  • props

Returns Promise<(Model | Error)> Resolves with the saved model instance or rejects with an error

findOne

Abstract method, must be implemented by the child class

Parameters

  • props

Returns Promise<(Model | Error)> Resolves with the saved model instance or rejects with an error

delete

Abstract method, must be implemented by the child class

Returns Promise<(Model | Error)> Resolves with the saved model instance or rejects with an error

validate

Validate the properties against the schema definition

Parameters

  • schema Joi The model schema
  • props object The model properties to validate

Returns Promise<(Joi | Error)> Resolves to the validated model properties or rejects with an error

Broker

Broker model class

Type: module.Broker

Broker.Schema

BrokerSchema

Type: Joi

Parameters

Broker.BalanceSchema

BalanceSchema

Type: Joi

Parameters

  • used number? Positive values denote the amount blocked into a Open order or position. Negative value denotes the amount being released.
  • span number? Amount blocked on futures and options towards SPAN
  • adhoc number? Payin amount credited through a manual process
  • available number? Total margin available for trading
  • exposure number? Amount blocked on futures and options towards Exposure
  • payin_amount number? Instant payin will reflect here
  • notional_cash number? The amount maintained for withdrawal

Broker.HoldingSchema

HoldingSchema

Type: Joi

Parameters

  • instrument_id string The unique id for the instrument
  • product string Shows if the order was either Intraday, Delivery, CO or OCO
  • quantity number The total holding quantity
  • t1_quantity number? Quantity on T+1 day after order execution. Stocks are usually delivered into DEMAT accounts on T+2
  • cnc_used_quantity number? Quantity either blocked towards open or completed order
  • collateral_type number? Category of collateral assigned by RMS
  • haircut number? This is the haircut percentage applied from RMS (applicable in case of collateral)
  • avg_price number? It indicates the consolidated price across all the orders placed for the scrip. Note: Corporate actions are not taken into consideration

Broker.PositionSchema

PositionSchema

Type: Joi

Parameters

  • instrument_id string The unique id for the instrument
  • product string Shows if the order was either Intraday, Delivery, CO or OCO
  • intraday object? The positions held during the day
    • intraday.buy object? The positions held during the day on the buy side
    • intraday.sell object? The positions held during the day on the sell side
  • carry_forward object? The positions carried forward from previous day
    • carry_forward.buy object? The positions carried forward from previous day on the buy side
    • carry_forward.sell object? The positions carried forward from previous day on the sell side
  • pnl object? The profit or loss
    • pnl.realized number? The profit or loss that was realized
    • pnl.unrealized number? The profit or loss that is yet to be realized

CorporateAction

CorporateAction model class

Type: module.CorporateAction

CorporateAction.Schema

CorporateActionSchema

Type: Joi

Parameters

  • id string The unique id of the instrument

Instrument

Instrument model class

Type: module.Instrument

Instrument.Schema

InstrumentSchema

Type: Joi

Parameters

  • id string The unique id for the instrument
  • exchange string The exchange the instrument belongs to
  • symbol string The symbol of the instrument
  • exchange_token string The unique token of the instrument at exchange level
  • isin string? The unique isin value of the instrument
  • tick_size number? The tick size of the instrument
  • strike_price number? The strike price of the instrument in case of an option
  • lot_size number? The lot size of the instrument in case of a future/option
  • expiry number? The expiry timestamp of the instrument in case of a future/option
  • is_enabled boolean? Whether the instrument is enabled or not (Defaults to true)
  • sector string? The sector to which the instrument belongs

News

News model class

Type: module.News

News.Schema

NewsSchema

Type: Joi

Parameters

  • id string The unique id of the instrument

Ohlc

Ohlc model class

Type: module.Ohlc

Ohlc.Schema

OhlcSchema

Type: Joi

Parameters

  • id string The unique id for the instrument
  • timestamp number The timestamp when the tick was last updated
  • open number The open price of the instrument
  • high number The high price of the instrument
  • low number The low price of the instrument
  • close number The close price of the instrument
  • volume number? The volume of the instrument that was transacted at last tick

Strategy

Strategy model class

Type: module.Strategy

Strategy.Schema

StrategySchema

Type: Joi

Parameters

  • id string The unique id of the strategy

Tick.Schema

TickSchema

Type: Joi

Parameters

  • timestamp number The timestamp when the tick was last updated
  • price number The price of the instrument for bid or ask
  • size number The size or quantity of the instrument for bid or ask

Tick.Schema

TickSchema

Type: Joi

Parameters

  • id string The unique id for the tick, should correspond to the unique instrument id
  • last_timestamp number? The timestamp when the tick was last updated
  • last_price number? The last price of the instrument
  • last_size number? The last contract size that was transacted
  • avg_price number? The average price of instrument at the given time
  • tbq number? The total buy quantities of the instrument
  • tsq number? The total sell quantities of the instrument
  • ohlc Object<Ohlc.Schema>? The OHLC data point for the instrument at the given time Ohlc.Schema
  • oi number? The open interest standing at the given time
  • oi_day_high number? The day's high value of open interest for the instrument
  • oi_day_low number? The day's low value of open interest for the instrument
  • bids Array<Tick.DepthSchema>? The bid side of the depth of book for the instrument Tick.DepthSchema
  • asks Array<Tick.DepthSchema>? The ask side of the depth of book for the instrument Tick.DepthSchema

User

User model class

Type: module.User

Examples

const {User} = require('@qfin/schema');
const user = new User({

})

hasRole

Check if the user has a given role

Parameters

Returns boolean true if role is present

validate

Validates the props and if valid returns the user object with updated props

Parameters

  • props object The user properties (optional, default {})

Returns Promise<(User | Error)> Promise resolves with user data or error

User.AuthSchema

AuthSchema of User Generally this schema is used for OAuth authorization and login

Type: Joi

Parameters

  • id string The unique user id for this Auth
  • token string The unique access token for this Auth
  • email string? The email address for this Auth
  • profile object? The user's profile details
    • profile.first_name string? The user's first name
    • profile.last_name string? The user's last name
    • profile.phone string? The user's phone number
    • profile.avatar_url string? The user's avatar URL
  • broker_details object? Additional user's details if the Auth is of Broker Auth type

Examples

const auth = {
    id: 'ABC123',
    token: 'someuniqueauthtoken',
    email: '[email protected]',
    profile: {
        first_name: 'First',
        last_name: 'Last',
        phone: '+911234567890',
        avatar_url: 'https://avatar.auth.url',
    }
};

User.Schema

Schema of User

Type: Joi

Parameters

  • id string The unique user id for this Auth
  • created_at number The unix timestamp when the object was created (in milliseconds)
  • updated_at number The unix timestamp when the object was updated (in milliseconds)
  • emails array A list of emails and whether it is verified or not
  • password string The password for user login
  • roles array The list of User.UserRole
  • check object? Various type of boolean checks
    • check.is_verified boolean? Whether the user is verified or not
    • check.is_suspended boolean? Whether the user is suspended or not
    • check.is_deleted boolean? Whether the user is deleted or not
    • check.suspension_reason string? The reason for user's account suspension User.AccountSuspensionReason
  • first_name string? The user's first name
  • last_name string? The user's last name
  • avatar_url string? The user's avatar URL
  • auth object? The user's other auth objects

Examples

const user = {
    id: 'ABC123',
    emails: [
      { email: '[email protected]', is_verified: true },
    ],
    phones: [
      { phone: '+911234567890' },
    ],
    password: 'Password@123',
    roles: [ 'USER_ROLE' ],
    first_name: 'First',
    last_name: 'Last',
    avatar_url: 'https://avatar.auth.url',
    auth: {
        google: {#User.AuthSchema}
    }
};

User.UserRole

A map of available User Roles

Type: module.User.UserRole

Parameters

  • SUPER_ADMIN_ROLE string The Super Admin Role
  • ADMIN_ROLE string The Admin Role
  • USER_ROLE string The normal User Role

User.AccountSuspensionReason

Various reasons for account suspension

Type: object

Parameters

  • LICENSE_EXPIRED string Due to license expiry
  • USER_BLOCKED string Because the user is blocked by Admin
  • IP_BLOCKED string Because the user's IP is blocked by Admin