本文整理匯總了TypeScript中knex.insert函數的典型用法代碼示例。如果您正苦於以下問題:TypeScript insert函數的具體用法?TypeScript insert怎麽用?TypeScript insert使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了insert函數的10個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。
示例1: CHECK
return knex.schema.hasColumn("markets", "validityBondSize").then(async(exists) => {
if (!exists) {
await knex.schema.table("markets", (t) => {
t.string("validityBondSize").defaultTo("0");
t.string("transactionHash", 66);
t.specificType("logIndex", "integer NOT NULL CONSTRAINT \"nonnegativelogIndex\" CHECK (\"logIndex\" >= 0)").defaultTo(0);
});
}
const etherTokenRow = await knex("tokens").where("contractAddress", "ether");
if (etherTokenRow.length === 0) {
await knex.insert({contractAddress: ETHER, symbol: ETHER}).into("tokens");
await knex.insert({token: ETHER, supply: null}).into("token_supply");
}
});
示例2: insertPayout
export async function insertPayout(db: Knex, marketId: Address, payoutNumerators: Array<string|number|null>, invalid: boolean, tentativeWinning: boolean): Promise<number> {
const payoutRow: { [index: string]: string|number|boolean|null } = {
marketId,
isInvalid: invalid,
};
payoutNumerators.forEach((value, i): void => {
if (value == null) return;
payoutRow["payout" + i] = new BigNumber(value, 10).toString();
});
const payoutIdRow: { payoutId: number }|null = await db.select("payoutId").from("payouts").where(payoutRow).first();
if (payoutIdRow != null) {
return payoutIdRow.payoutId;
} else {
const payoutRowWithTentativeWinning = Object.assign({},
payoutRow,
{ tentativeWinning: Number(tentativeWinning) },
);
let query = db.insert(payoutRowWithTentativeWinning).into("payouts");
if (db.client.config.client !== "sqlite3") {
query = query.returning("payoutId");
}
const payoutIdRow: Array<number> = await query;
if (!payoutIdRow || !payoutIdRow.length) throw new Error("No payoutId returned");
return payoutIdRow[0];
}
}
示例3: updateOutcomeValuesFromFinalization
export async function updateOutcomeValuesFromFinalization(db: Knex, augur: Augur, marketId: Address, transactionHash: string, blockNumber: number, logIndex: number): Promise<void> {
const payouts: PayoutAndMarket<BigNumber> = await db
.first(["payouts.payout0", "payouts.payout1", "payouts.payout2", "payouts.payout3", "payouts.payout4", "payouts.payout5", "payouts.payout6", "payouts.payout7", "markets.minPrice", "markets.maxPrice", "markets.numTicks"])
.from("payouts")
.where("payouts.marketId", marketId)
.join("markets", function() {
this.on("payouts.marketId", "markets.marketId");
});
const timestamp = getCurrentTime();
const maxPrice = payouts.maxPrice;
const minPrice = payouts.minPrice;
const numTicks = payouts.numTicks;
const tickSize = numTicksToTickSize(numTicks, minPrice, maxPrice);
const insertValues = [];
for (let i: number = 0; i <= 7; i++) {
const column = `payout${i}`;
const payoutValue = payouts[column as keyof PayoutAndMarket<BigNumber>];
if (payoutValue != null) {
const value = payoutValue.times(tickSize).plus(minPrice);
insertValues.push({
marketId,
transactionHash,
blockNumber,
logIndex,
outcome: i,
value: value.toString(),
timestamp,
});
}
}
await db.insert(insertValues).into("outcome_value_timeseries");
}
示例4: insertTransactionHash
export async function insertTransactionHash(db: Knex, blockNumber: number, transactionHash: string) {
if (transactionHash === null) throw new Error("Received null transactionHash from getLogs request. Your Ethereum node might be in light mode with bug: https://github.com/paritytech/parity-ethereum/issues/9929");
const txHashRows: Array<TransactionHashesRow> = await db("transactionHashes").where({ transactionHash });
if (!txHashRows || !txHashRows.length) {
await db.insert({ blockNumber, transactionHash }).into("transactionHashes");
}
}
示例5: async
return async (db: Knex) => {
const marketId = log.market;
const marketsRow: MarketsRow<BigNumber>|undefined = await db.first("minPrice", "maxPrice", "numTicks", "numOutcomes").from("markets").where({ marketId });
if (!marketsRow) throw new Error(`market not found: ${marketId}`);
const minPrice = marketsRow.minPrice;
const maxPrice = marketsRow.maxPrice;
const numTicks = marketsRow.numTicks;
const numOutcomes = marketsRow.numOutcomes;
const tickSize = numTicksToTickSize(numTicks, minPrice, maxPrice);
const numCompleteSets = augur.utils.convertOnChainAmountToDisplayAmount(new BigNumber(log.numCompleteSets, 10), tickSize);
const completeSetPurchasedData: CompleteSetsRow<string> = {
marketId,
account: log.account,
blockNumber: log.blockNumber,
universe: log.universe,
eventName: log.eventName,
transactionHash: log.transactionHash,
logIndex: log.logIndex,
numCompleteSets: numCompleteSets.toString(),
numPurchasedOrSold: numCompleteSets.toString(),
};
const eventName = log.eventName as keyof typeof SubscriptionEventNames;
await db.insert(completeSetPurchasedData).into("completeSets");
augurEmitter.emit(SubscriptionEventNames[eventName], completeSetPurchasedData);
await updateMarketOpenInterest(db, marketId);
};
示例6: async
return async(db: Knex) => {
const amount: BigNumber = new BigNumber(log.amount, 10);
const price: BigNumber = new BigNumber(log.price, 10);
const orderType: string = log.orderType;
const moneyEscrowed: BigNumber = new BigNumber(log.moneyEscrowed, 10);
const sharesEscrowed: BigNumber = new BigNumber(log.sharesEscrowed, 10);
const shareToken: Address = log.shareToken;
const tokensRow: TokensRow|undefined = await db.first("marketId", "outcome").from("tokens").where({ contractAddress: shareToken });
if (!tokensRow) throw new Error(`market and outcome not found for shareToken ${shareToken} (${log.transactionHash}`);
const marketId = tokensRow.marketId;
const outcome = tokensRow.outcome!;
const marketsRow: MarketsRow<BigNumber> = await db.first("minPrice", "maxPrice", "numTicks", "numOutcomes").from("markets").where({ marketId });
if (!marketsRow) throw new Error(`market not found: ${marketId}`);
const minPrice = marketsRow.minPrice;
const maxPrice = marketsRow.maxPrice;
const numTicks = marketsRow.numTicks;
const tickSize = numTicksToTickSize(numTicks, minPrice, maxPrice);
const numOutcomes = marketsRow.numOutcomes;
const fullPrecisionAmount = augur.utils.convertOnChainAmountToDisplayAmount(amount, tickSize);
const fullPrecisionPrice = augur.utils.convertOnChainPriceToDisplayPrice(price, minPrice, tickSize);
const orderTypeLabel = orderType === "0" ? "buy" : "sell";
const displaySharesEscrowed = augur.utils.convertOnChainAmountToDisplayAmount(sharesEscrowed, tickSize).toString();
const displayTokensEscrowed = fixedPointToDecimal(moneyEscrowed, BN_WEI_PER_ETHER).toString();
const orderData: OrdersRow<string> = {
marketId,
blockNumber: log.blockNumber,
transactionHash: log.transactionHash,
logIndex: log.logIndex,
outcome,
shareToken,
orderCreator: log.creator,
orderState: OrderState.OPEN,
tradeGroupId: log.tradeGroupId,
orderType: orderTypeLabel,
price: formatOrderPrice(orderTypeLabel, minPrice, maxPrice, fullPrecisionPrice),
amount: formatOrderAmount(fullPrecisionAmount),
originalAmount: formatOrderAmount(fullPrecisionAmount),
fullPrecisionPrice: fullPrecisionPrice.toString(),
fullPrecisionAmount: fullPrecisionAmount.toString(),
originalFullPrecisionAmount: fullPrecisionAmount.toString(),
originalTokensEscrowed: displayTokensEscrowed,
originalSharesEscrowed: displaySharesEscrowed,
tokensEscrowed: displayTokensEscrowed,
sharesEscrowed: displaySharesEscrowed,
};
const orderId = { orderId: log.orderId };
const ordersRows: Array<Partial<OrdersRow<BigNumber>>> = await db.select("marketId").from("orders").where(orderId);
let upsertOrder: QueryBuilder;
if (!ordersRows || !ordersRows.length) {
upsertOrder = db.insert(Object.assign(orderData, orderId)).into("orders");
} else {
upsertOrder = db.from("orders").where(orderId).update(orderData);
}
await upsertOrder;
await marketPendingOrphanCheck(db, orderData);
augurEmitter.emit(SubscriptionEventNames.OrderCreated, Object.assign({}, log, orderData));
};
示例7: increaseTokenSupply
export async function increaseTokenSupply(db: Knex, augur: Augur, token: Address, amount: BigNumber) {
const oldSupply: SupplyResult = await db.first("supply").from("token_supply").where({ token });
if (oldSupply == null) {
await db.insert({ token, supply: amount.toString() }).into("token_supply");
} else {
const supply = oldSupply.supply.plus(amount);
await db.update({ supply: supply.toString() }).into("token_supply").where({ token });
}
}
示例8: updateMarketState
export async function updateMarketState(db: Knex, marketId: Address, blockNumber: number, reportingState: ReportingState) {
const marketStateDataToInsert = { marketId, reportingState, blockNumber };
let query = db.insert(marketStateDataToInsert).into("market_state");
if (db.client.config.client !== "sqlite3") {
query = query.returning("marketStateId");
}
const marketStateId: Array<number> = await query;
if (!marketStateId || !marketStateId.length) throw new Error("Failed to generate new marketStateId for marketId:" + marketId);
return setMarketStateToLatest(db, marketId);
}
示例9: marketPendingOrphanCheck
async function marketPendingOrphanCheck(db: Knex, orderData: OrdersRow<string>) {
const pendingOrderData = {
marketId: orderData.marketId,
outcome: orderData.outcome,
orderType: orderData.orderType,
};
const result: { count: number } = await db.first(db.raw("count(*) as count")).from("pending_orphan_checks").where(pendingOrderData);
if (result.count > 0) return;
return await db.insert(pendingOrderData).into("pending_orphan_checks");
}
示例10: increaseTokenBalance
export async function increaseTokenBalance(db: Knex, augur: Augur, token: Address, owner: Address, amount: BigNumber, log: FormattedEventLog) {
if (isLegacyReputationToken(augur, token)) return;
const oldBalance: BalanceResult = await db.first("balance").from("balances").where({ token, owner });
let balance = amount;
if (oldBalance == null) {
await db.insert({ owner, token, balance: balance.toString() }).into("balances");
} else {
balance = oldBalance.balance.plus(amount);
await db.update({ balance: balance.toString() }).into("balances").where({ token, owner });
}
}