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

Instrument @truffle/db with some initial logging #3475

Merged
merged 2 commits into from
Oct 27, 2020
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
2 changes: 1 addition & 1 deletion packages/db/.madgerc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"excludeRegExp": ["\\.\\.", "test"],
"excludeRegExp": ["\\.\\.", "test", "logger.ts"],
"fileExtensions": ["ts"],
"tsConfig": "./tsconfig.base.json"
}
5 changes: 3 additions & 2 deletions packages/db/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,9 @@
"dist": "dist"
},
"scripts": {
"build": "./bin/build",
"build": "yarn build:types && yarn build:dist",
"clean": "rm -rf ./dist ./types/schema.d.ts",
"prepare": "yarn build",
"build": "yarn build:types && yarn build:dist",
"build:stubbed": "ttsc --project tsconfig.codegen.json",
"build:types": "yarn build:stubbed && node ./bin/codegen.js",
"build:dist": "ttsc",
Expand All @@ -40,6 +39,7 @@
"@truffle/config": "^1.2.31",
"@truffle/workflow-compile": "^3.0.5",
"apollo-server": "^2.18.2",
"debug": "^4.2.0",
"fse": "^4.0.1",
"graphql": "^15.3.0",
"graphql-tag": "^2.11.0",
Expand All @@ -60,6 +60,7 @@
},
"devDependencies": {
"@gql2ts/from-schema": "^2.0.0-4",
"@types/debug": "^4.1.5",
"@types/express": "^4.16.0",
"@types/graphql": "^14.5.0",
"@types/jest": "^23.3.11",
Expand Down
3 changes: 3 additions & 0 deletions packages/db/src/connect.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { logger } from "@truffle/db/logger";
const debug = logger("db:connect");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you adding this here for later?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep - I added it to every module I could, in case we need it. We have a dedicated eslint rule to allow this as an unused variable. This is how we do things in other packages.


import { definitions } from "./definitions";
import { forDefinitions } from "./pouch";

Expand Down
3 changes: 3 additions & 0 deletions packages/db/src/db.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { logger } from "@truffle/db/logger";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto, imported but not being used.

const debug = logger("db:db");

import { GraphQLSchema, DocumentNode, parse, execute } from "graphql";
import type TruffleConfig from "@truffle/config";
import { generateCompileLoad } from "@truffle/db/loaders/commands";
Expand Down
3 changes: 3 additions & 0 deletions packages/db/src/definitions/bytecodes.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { logger } from "@truffle/db/logger";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here too.

const debug = logger("db:definitions:bytecodes");

import gql from "graphql-tag";

import { Definition } from "./types";
Expand Down
49 changes: 39 additions & 10 deletions packages/db/src/definitions/compilations.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { logger } from "@truffle/db/logger";
const debug = logger("db:definitions:compilations");

import gql from "graphql-tag";

import { Definition } from "./types";
Expand Down Expand Up @@ -66,32 +69,58 @@ export const compilations: Definition<"compilations"> = {
resolvers: {
Compilation: {
sources: {
resolve: ({ sources }, _, { workspace }) =>
Promise.all(sources.map(({ id }) => workspace.get("sources", id)))
resolve: async ({ sources }, _, { workspace }) => {
debug("Resolving Compilation.sources...");

const result = await Promise.all(
sources.map(({ id }) => workspace.get("sources", id))
);

debug("Resolved Compilation.sources.");
return result;
}
},
processedSources: {
resolve: ({ id, processedSources }, _, {}) =>
processedSources.map((processedSource, index) => ({
resolve: ({ id, processedSources }, _, {}) => {
debug("Resolving Compilation.processedSources...");

const result = processedSources.map((processedSource, index) => ({
...processedSource,
compilation: { id },
index
}))
}));

debug("Resolved Compilation.processedSources.");
return result;
}
}
},

ProcessedSource: {
source: {
resolve: ({ source: { id } }, _, { workspace }) =>
workspace.get("sources", id)
resolve: async ({ source: { id } }, _, { workspace }) => {
debug("Resolving ProcessedSource.source...");

const result = await workspace.get("sources", id);

debug("Resolved ProcessedSource.source.");
return result;
}
},
contracts: {
resolve: ({ compilation, index }, _, { workspace }) =>
workspace.find("contracts", {
resolve: async ({ compilation, index }, _, { workspace }) => {
debug("Resolving ProcessedSource.compilation...");

const result = await workspace.find("contracts", {
selector: {
"compilation.id": compilation.id,
"processedSource.index": index
}
})
});

debug("Resolved ProcessedSource.compilation.");
return result;
}
}
}
}
Expand Down
30 changes: 26 additions & 4 deletions packages/db/src/definitions/contractInstances.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { logger } from "@truffle/db/logger";
const debug = logger("db:definitions:contractInstances");

import gql from "graphql-tag";

import { Definition } from "./types";
Expand Down Expand Up @@ -76,15 +79,28 @@ export const contractInstances: Definition<"contractInstances"> = {
resolvers: {
ContractInstance: {
network: {
resolve: ({ network: { id } }, _, { workspace }) =>
workspace.get("networks", id)
resolve: async ({ network: { id } }, _, { workspace }) => {
debug("Resolving ContractInstance.network...");

const result = await workspace.get("networks", id);

debug("Resolved ContractInstance.network.");
return result;
}
},
contract: {
resolve: ({ contract: { id } }, _, { workspace }) =>
workspace.get("contracts", id)
resolve: async ({ contract: { id } }, _, { workspace }) => {
debug("Resolving ContractInstance.contract...");
const result = await workspace.get("contracts", id);

debug("Resolved ContractInstance.contract.");
return result;
}
},
callBytecode: {
resolve: async ({ callBytecode }, _, { workspace }) => {
debug("Resolving ContractInstance.callBytecode...");

const bytecode = await workspace.get(
"bytecodes",
callBytecode.bytecode.id
Expand All @@ -97,6 +113,8 @@ export const contractInstances: Definition<"contractInstances"> = {
};
}
);

debug("Resolved ContractInstance.callBytecode.");
return {
bytecode: bytecode,
linkValues: linkValues
Expand All @@ -105,6 +123,8 @@ export const contractInstances: Definition<"contractInstances"> = {
},
creation: {
resolve: async (input, _, { workspace }) => {
debug("Resolving ContractInstance.creation...");

let bytecode = await workspace.get(
"bytecodes",
input.creation.constructor.createBytecode.bytecode.id
Expand All @@ -118,6 +138,8 @@ export const contractInstances: Definition<"contractInstances"> = {
};
}
);

debug("Resolved ContractInstance.creation.");
return {
transactionHash: transactionHash,
constructor: {
Expand Down
36 changes: 30 additions & 6 deletions packages/db/src/definitions/contracts.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { logger } from "@truffle/db/logger";
const debug = logger("db:definitions:contracts");

import gql from "graphql-tag";

import { Definition } from "./types";
Expand Down Expand Up @@ -45,8 +48,14 @@ export const contracts: Definition<"contracts"> = {
resolvers: {
Contract: {
compilation: {
resolve: ({ compilation: { id } }, _, { workspace }) =>
workspace.get("compilations", id)
resolve: async ({ compilation: { id } }, _, { workspace }) => {
debug("Resolving Contract.compilation...");

const result = workspace.get("compilations", id);

debug("Resolved Contract.compilation.");
return result;
}
},
processedSource: {
fragment: `... on Contract { compilation { id } }`,
Expand All @@ -55,18 +64,33 @@ export const contracts: Definition<"contracts"> = {
_,
{ workspace }
) => {
debug("Resolving Contract.processedSource...");

const { processedSources } = await workspace.get("compilations", id);

debug("Resolved Contract.processedSource.");
return processedSources[processedSource.index];
}
},
createBytecode: {
resolve: ({ createBytecode: { id } }, _, { workspace }) =>
workspace.get("bytecodes", id)
resolve: async ({ createBytecode: { id } }, _, { workspace }) => {
debug("Resolving Contract.createBytecode...");

const result = await workspace.get("bytecodes", id);

debug("Resolved Contract.createBytecode.");
return result;
}
},
callBytecode: {
resolve: ({ callBytecode: { id } }, _, { workspace }) =>
workspace.get("bytecodes", id)
resolve: async ({ callBytecode: { id } }, _, { workspace }) => {
debug("Resolving Contract.callBytecode...");

const result = await workspace.get("bytecodes", id);

debug("Resolved Contract.callBytecode.");
return result;
}
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions packages/db/src/definitions/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { logger } from "@truffle/db/logger";
const debug = logger("db:definitions");

import { Definitions } from "./types";
export * from "./types";

Expand Down
19 changes: 17 additions & 2 deletions packages/db/src/definitions/nameRecords.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { logger } from "@truffle/db/logger";
const debug = logger("db:definitions:nameRecords");

import gql from "graphql-tag";
import camelCase from "camel-case";
import { plural } from "pluralize";
Expand Down Expand Up @@ -28,13 +31,25 @@ export const nameRecords: Definition<"nameRecords"> = {
NameRecord: {
resource: {
resolve: async ({ type, resource: { id } }, _, { workspace }) => {
debug("Resolving NameRecord.resource...");

const collectionName = camelCase(plural(type)) as CollectionName;

return await workspace.get(collectionName, id);
const result = await workspace.get(collectionName, id);

debug("Resolved NameRecord.resource.");
return result;
}
},
previous: {
resolve: ({ id }, _, { workspace }) => workspace.get("nameRecords", id)
resolve: async ({ id }, _, { workspace }) => {
debug("Resolving NameRecord.previous...");

const result = await workspace.get("nameRecords", id);

debug("Resolved NameRecord.previous.");
return result;
}
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions packages/db/src/definitions/networks.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { logger } from "@truffle/db/logger";
const debug = logger("db:definitions:networks");

import gql from "graphql-tag";

import { Definition } from "./types";
Expand Down
23 changes: 19 additions & 4 deletions packages/db/src/definitions/projectNames.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { logger } from "@truffle/db/logger";
const debug = logger("db:definitions:projectNames");

import gql from "graphql-tag";

import { Definition } from "./types";
Expand Down Expand Up @@ -35,12 +38,24 @@ export const projectNames: Definition<"projectNames"> = {
resolvers: {
ProjectName: {
project: {
resolve: ({ project: { id } }, _, { workspace }) =>
workspace.get("projects", id)
resolve: async ({ project: { id } }, _, { workspace }) => {
debug("Resolving ProjectName.project...");

const result = await workspace.get("projects", id);

debug("Resolved ProjectName.project.");
return result;
}
},
nameRecord: {
resolve: ({ nameRecord: { id } }, _, { workspace }) =>
workspace.get("nameRecords", id)
resolve: async ({ nameRecord: { id } }, _, { workspace }) => {
debug("Resolving ProjectName.nameRecord...");

const result = await workspace.get("nameRecords", id);

debug("Resolved ProjectName.nameRecord.");
return result;
}
}
}
}
Expand Down
Loading