Skip to content
This repository has been archived by the owner on Sep 26, 2024. It is now read-only.

Refactor: strict DB types #1173

Merged
merged 1 commit into from
Mar 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 82 additions & 0 deletions backend/kanel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,68 @@ import {
TypeImport,
defaultGenerateIdentifierType,
PreRenderHook,
defaultGetPropertyMetadata,
} from "kanel";
import path from "path";

import { config } from "@explorer/backend/config";
import { notNullGuard } from "@explorer/common/utils/utils";

type DatabaseName = string;
type TableName = string;
type TableField = string;

const ACTIONS_TYPE = `
| {
public_key: string;
access_key: {
nonce: number;
permission:
| {
permission_kind: "FUNCTION_CALL";
permission_details: {
allowance: string;
receiver_id: string;
method_names: string[];
};
}
| {
permission_kind: "FULL_ACCESS";
};
};
}
| {}
| { beneficiary_id: string; }
| { public_key: string; }
| { code_sha256: string; }
| {
gas: number;
deposit: string;
method_name: string;
args_json?: Record<string, unknown>;
args_base64: string;
}
| {
public_key: string;
stake: string;
}
| { deposit: string; }
`;

const TYPE_OVERRIDES: Record<
DatabaseName,
Record<TableName, Record<TableField, string>>
> = {
readOnlyIndexer: {
action_receipt_actions: {
args: ACTIONS_TYPE,
},
transaction_actions: {
args: ACTIONS_TYPE,
},
},
};

type TableDetails = Extract<Details, { kind: "table" }>;

const generateInterfaceDeclaration = (
Expand Down Expand Up @@ -135,6 +191,7 @@ const run = async () => {
) {
return;
}
const databaseTypesOverride = TYPE_OVERRIDES[database];
console.log(`\n> Generating types for ${database}...`);
try {
await processDatabase({
Expand Down Expand Up @@ -166,6 +223,31 @@ const run = async () => {
typeDefinition: [`${match[1]} & { __flavor?: '${match[2]}' }`],
};
},
getPropertyMetadata: (
property,
details,
generateFor,
instantiatedConfig
) => {
const metadata = defaultGetPropertyMetadata(
property,
details,
generateFor,
instantiatedConfig
);
if (!databaseTypesOverride) {
return metadata;
}
const tableTypesOverride = databaseTypesOverride[details.name];
if (!tableTypesOverride) {
return metadata;
}
const columnTypeOverride = tableTypesOverride[property.name];
if (!columnTypeOverride) {
return metadata;
}
return { ...metadata, typeOverride: columnTypeOverride };
},
typeFilter: (type) =>
type.kind !== "table" || type.name !== "__diesel_schema_migrations",
enumStyle: "type",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,41 @@ export default interface ActionReceiptActions {

action_kind: ActionKind;

args: unknown;
args:
| {
public_key: string;
access_key: {
nonce: number;
permission:
| {
permission_kind: "FUNCTION_CALL";
permission_details: {
allowance: string;
receiver_id: string;
method_names: string[];
};
}
| {
permission_kind: "FULL_ACCESS";
};
};
}
| {}
| { beneficiary_id: string }
| { public_key: string }
| { code_sha256: string }
| {
gas: number;
deposit: string;
method_name: string;
args_json?: Record<string, unknown>;
args_base64: string;
}
| {
public_key: string;
stake: string;
}
| { deposit: string };

receipt_predecessor_account_id: string;

Expand All @@ -34,7 +68,41 @@ export interface ActionReceiptActionsInitializer {

action_kind: ActionKind;

args: unknown;
args:
| {
public_key: string;
access_key: {
nonce: number;
permission:
| {
permission_kind: "FUNCTION_CALL";
permission_details: {
allowance: string;
receiver_id: string;
method_names: string[];
};
}
| {
permission_kind: "FULL_ACCESS";
};
};
}
| {}
| { beneficiary_id: string }
| { public_key: string }
| { code_sha256: string }
| {
gas: number;
deposit: string;
method_name: string;
args_json?: Record<string, unknown>;
args_base64: string;
}
| {
public_key: string;
stake: string;
}
| { deposit: string };

receipt_predecessor_account_id: string;

Expand All @@ -51,7 +119,41 @@ export interface ActionReceiptActionsMutator {

action_kind?: ActionKind;

args?: unknown;
args?:
| {
public_key: string;
access_key: {
nonce: number;
permission:
| {
permission_kind: "FUNCTION_CALL";
permission_details: {
allowance: string;
receiver_id: string;
method_names: string[];
};
}
| {
permission_kind: "FULL_ACCESS";
};
};
}
| {}
| { beneficiary_id: string }
| { public_key: string }
| { code_sha256: string }
| {
gas: number;
deposit: string;
method_name: string;
args_json?: Record<string, unknown>;
args_base64: string;
}
| {
public_key: string;
stake: string;
}
| { deposit: string };

receipt_predecessor_account_id?: string;

Expand Down
108 changes: 105 additions & 3 deletions backend/src/database/models/readOnlyIndexer/transaction-actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,41 @@ export default interface TransactionActions {

action_kind: ActionKind;

args: unknown;
args:
| {
public_key: string;
access_key: {
nonce: number;
permission:
| {
permission_kind: "FUNCTION_CALL";
permission_details: {
allowance: string;
receiver_id: string;
method_names: string[];
};
}
| {
permission_kind: "FULL_ACCESS";
};
};
}
| {}
| { beneficiary_id: string }
| { public_key: string }
| { code_sha256: string }
| {
gas: number;
deposit: string;
method_name: string;
args_json?: Record<string, unknown>;
args_base64: string;
}
| {
public_key: string;
stake: string;
}
| { deposit: string };
}

/** Represents the initializer for the table public.transaction_actions */
Expand All @@ -28,7 +62,41 @@ export interface TransactionActionsInitializer {

action_kind: ActionKind;

args: unknown;
args:
| {
public_key: string;
access_key: {
nonce: number;
permission:
| {
permission_kind: "FUNCTION_CALL";
permission_details: {
allowance: string;
receiver_id: string;
method_names: string[];
};
}
| {
permission_kind: "FULL_ACCESS";
};
};
}
| {}
| { beneficiary_id: string }
| { public_key: string }
| { code_sha256: string }
| {
gas: number;
deposit: string;
method_name: string;
args_json?: Record<string, unknown>;
args_base64: string;
}
| {
public_key: string;
stake: string;
}
| { deposit: string };
}

/** Represents the mutator for the table public.transaction_actions */
Expand All @@ -39,5 +107,39 @@ export interface TransactionActionsMutator {

action_kind?: ActionKind;

args?: unknown;
args?:
| {
public_key: string;
access_key: {
nonce: number;
permission:
| {
permission_kind: "FUNCTION_CALL";
permission_details: {
allowance: string;
receiver_id: string;
method_names: string[];
};
}
| {
permission_kind: "FULL_ACCESS";
};
};
}
| {}
| { beneficiary_id: string }
| { public_key: string }
| { code_sha256: string }
| {
gas: number;
deposit: string;
method_name: string;
args_json?: Record<string, unknown>;
args_base64: string;
}
| {
public_key: string;
stake: string;
}
| { deposit: string };
}
11 changes: 3 additions & 8 deletions backend/src/router/account/activity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ import { div } from "@explorer/backend/database/utils";
import { validators } from "@explorer/backend/router/validators";
import {
Action,
DatabaseAction,
mapDatabaseActionToAction,
mapForceDatabaseActionToAction,
} from "@explorer/backend/utils/actions";
import { nanosecondsToMilliseconds } from "@explorer/backend/utils/bigint";
import {
Expand Down Expand Up @@ -323,11 +322,7 @@ const getReceiptMapping = (
initialMapping: Map<string, ReceiptPreview> = new Map()
) =>
receiptRows.reduce((mapping, receipt) => {
const action = mapDatabaseActionToAction({
hash: receipt.originatedFromTransactionHash,
kind: receipt.kind,
args: receipt.args,
} as DatabaseAction);
const action = mapForceDatabaseActionToAction(receipt);
const existingReceipt = mapping.get(receipt.id);
if (!existingReceipt) {
return mapping.set(receipt.id, {
Expand Down Expand Up @@ -454,7 +449,7 @@ const getTransactionsByHashes = async (
(mapping, action) =>
mapping.set(action.hash, [
...(mapping.get(action.hash) || []),
mapDatabaseActionToAction(action as DatabaseAction),
mapForceDatabaseActionToAction(action),
]),
new Map<string, Action[]>()
);
Expand Down
Loading