Note on Sequelize
Sequelize is a promise-based ORM for Node.
Example12345678910111213141516const Sequelize = require('sequelize')const sequelize = new Sequeslize('database', 'username', 'password')const User = sequelize.define('user', { username: Sequelize.STRING, birthday: Sequelize.DATE,})sequelize.sync().then(() => { User.create({ username: 'Jane', birthday: new Date(1980, 5, 2), })}).then(jane => { ...
Build GraphQL Server With Express
Step 1 - Connect to Server123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566// db.jsconst Sequelize = require('sequelize')const _ = require('lodash')const Faker = require('faker')const Conn = new Sequelize( 'test', 'postgres', 'postgres', { dialect: 'postgres', host: 'localhost', })const Person = Conn.define('person ...
Koa-Graphql
Create a GraphQL HTTP server with Koa.
Usage with Koa-Router12345678910111213const Koa = require('koa')const Router = require('koa-router')const GraphqlHTTP = require('koa-graphql')const app = new Koa()const router = new Router()router.all('/graphql', graphqlHTTP({ schema: MyGraphQLSchema, graphiql: ture,}))app.use(router.routes()).use(router.allowedMethods())
OptionsThe GraphqlHTTP function accpets the following options:
schema: A GraphQLSch ...
Note on GraphQL(2)
ExecutionAfter being validated, a GraphQL query is executed by a GraphQL server which returns a result that mirrors the shape of the requested query, typically as JSON.
GraphQL cannot execute a query without a type system, let’s use an example type system to illustrate executing a query.
12345678910111213141516171819type Query { human(id: ID!): Human}type Human { name: String appearsIn: [Episode] starships: [Starship]}enum Episode { NEWHOPE EMPIRE JEDI}type S ...
Note on GraphQL(1)
GraphQL is a query language for your API, and a server-side runtime for executing queries by using a type system you define for your data.
A GraphQL service is created by defining types and fields on those types, then providing functions for each field on each type.
12345678type Query { me: User}type User { id: ID name: String}
Along with functions for each field on each type:
1234567function Query_me(request) { return request.auth.user;}function User_name(user ...
List of HTTP Status Code
1xx
100 (continue)
101 (Switching Protocols)
2xx
200 (OK)
201 (Created): respond to POST Request in RESTFul.
202 (Accepted): Server fetch the request, but not respond immediately.
203 (Non-Authoritative Information).
204 (No Content): Server has handled the request, but no content need to be respond.
205 (Reset Content): Server has handled the request, and no content need to be respond. But server request client to reset the document(recover to that before submission).
206 (Partial Cont ...
Short Clip of RxJS
CreationRx.Observable.create is an alias for the Observable constructor, and it takes one argument: the subscribe function.
1234var observable = Rx.Observable.create(function subscribe (observer) { observer.next(1) // ...})
Observable can be created with create, but usually we use the so-called creation operators, like, of, from, interval, etc.
Subscription12345observable.subscribe({ next: console.log, error: console.error, complete: () => console.log('done'), ...
Http Cache: Etag, Last-Modified, Cache-Control
Cache-ControlCache-Control general-header field is used to specify deirectives that MUST be obeyed by all caching mechanisms along the request/response chain.
When the Cache-Control allow to cache, like Cache-Control: public, max-age: 86400, it means that in this day the browser can use the cahce directly without any request to server.a
1234567import http: 'http'let server = http.createServer((req, res) => { res.setHeader('Cache-Control', 'public', 'ma ...
WS on Node
Create WS Server12345678// get WS Objectconst WebSocket = require('ws')// Create new Server Instanceconst ws = new WebSocket.Server({ port: 80, // port verifyClient: socketverify // (optional)function to verify connection})
new WebSocket.Server(options[, callback])12345678910111213options: { host: string, port: number, backlog: number, // the maximum length of the queue of pending connection. server: http.Server | https.Server, // a pre-created ...
Short Clip of TypeScript(1)
Basic TypesBoolean, Number, String, Array, Tuple, Enum, Any, Void, Null, Undefined, Never
12345678910111213141516171819202122232425262728293031323334// Booleanlet isDone: boolean: false// Numberlet decimal: number = 6// Stringlet color: string = 'red'// Arraylet list: number[] = [1, 2, 3]let list: Array<number> = [1, 2, 3]// Tuplelet x: [string, number] = ['hello', 10]// Enumenum Color {Red, Green, Blue}let c: Color = Color.Green // 1// Anylet notSure = 5notSu ...
Web App Manifest
The web app manifest provides information about an application(such as name, author, icon and description) in a JSON text file. The purpose of the manifest is to install web application to the homescreen of a device, providing users with quicker access and a richer experience.
Web app manifest are part of a collection of web technologies called progressive web apps, which are web applications that can be installed to the homescreen of a device without needing the user to go through an app store, ...
Cache Object
The Cache interface provides a storage mechanism for Request / Response object pairs that are cahced, for example as part of the ServiceWorker lifecycle. Note that the Cache Interface is exposed to windowed scopes as well as workers. You don’t have to use it in conjunction with service workers, even though it is defined in the service worker spec.
An orgin can have multiple Cache Objects. You are responsible for implementing how your script handles Cache Update. Items in a Cache do not get updat ...
Some Concepts on BlockChain
Getting BlockChain Data:
Blocks;
Transactions;
Sending Transactions;
Cryptocurrency Functions:
Generating Private/Public keys, Hashing, Address Encoding etc;
Creating transactions;
Signing transactions;
Support functions;
Build A Block
Receive the transaction broadcase;
Verify the crypto in the transaction;
Add it to the unconfirmed Pool;
Do some hard maths on all the transactions in the pool;
Broadcast the Block to the network;
The Block is added to the blockchain;
Blockchains a ...
Some Note on Blockchain
Getting BlockChain Data:
Blocks;
Transactions;
Sending Transactions;
Cryptocurrency Functions:
Generating Private/Public keys, Hashing, Address Encoding etc;
Creating transactions;
Signing transactions;
Support functions;
Build A Block
Receive the transaction broadcase;
Verify the crypto in the transaction;
Add it to the unconfirmed Pool;
Do some hard maths on all the transactions in the pool;
Broadcast the Block to the network;
The Block is added to the blockchain;
Blockchains a ...
Five Models in Smart Contract
Database ContractsThese are used only as data storage. The only logic they need is functions that allow other contracts to write, update and get data, and some simple way of checking caller permissions.
Controller ContractsThese contracts operate on the storage contracts. In a flexible system, both controllers and databases can be replaced by other, similar contracts that share the same public api(although this is not always needed).) Controllers can be advanced, and could for example do batched ...
Tutorial Part 4 of Dapp
Calling ContractsContract API basically comes in three pieces.
Firstly, there’s state-changing transaction like tranferring tokens to a counter-party.
Secondly, there’s event reception and reporting that happen when such a state change occurs.
Finally, there is inspection of the contract state through calling constant function.
Our first contractThe first contract we will deal with is the global(name) registry. If you are not yet familiar, this is a registry that exists on all sensible bloc ...
Tutorial Part 3 of Dapp
Parity BondsIt’s time to get down and dirty with the blockchain.
For this we will introduce the oo7-parity package, which provides a high-level reactive Bond API.
To set this up, all we need do is import bonds from the oo7-parity module, so ensure you have this at the top of your file:
1import { bonds } from 'oo7-parity'
Watch the blockFor our first task, we will introduce the simplest of all bonds: bonds.height. This evaluates to the number of the latest block, expressed as ...
Tutorial Part 2 of Dapp
oo7 BondsNow we have our basic dapp harness, we can start introducing more interesting functionality. Without too much ado, let’s get started. Head in to src/client/scripts/app.jsx. You will see our basic file:
123456789import React from 'react'export class App extends React.Component { render () { return ( <div>Hello World</div> ) }}
This is a JSX file. Baiscally means that can handle embedded HTML when describing how React components shou ...
Tutorial Part 1 of Dapp
Spawning a new DappThe dapp will use modern JS technologies: NPM, Webpack2, React, Babel, ES6, JSX and oo7.
Clone a skeleton repo from parity:
1git clone https://github.com/paritytech/skeleton mydapp
This will make your a new repo with everything ready.
cd in to it and remove the origin repository lest it confuse Git.
It’s liberally licensed (Apache 2.0) so you don’t have to worry about open sourcing your own code(though obviously you’ll be enlightened and want to do that anyway.)
The next stag ...
D3-Shape
d3.shapeVisualization typically consist of discrete graphical marks, such as Symbols, arcs, lines and areas. While the ractangles of a bar chart may be easy enough to generate directly using SVG or Canvas, other shapes are complex, such as roudned annular sectors and centripetal Catmull-Rom splines. This module provides a variety of shape generators for your convenience.
As with other aspects of D3, these shapes are driven by data: each shape generator exposes accessors that control how the inp ...