From 9b54c9d741f478146ddccb7f76f33eb337349d52 Mon Sep 17 00:00:00 2001 From: Alexander Belopashentsev Date: Sun, 4 Sep 2022 00:38:04 +0500 Subject: [PATCH] try to fix --- db/migrations/1662233768902-Data.js | 15 ++ package-lock.json | 214 +++++++++++----------------- package.json | 2 +- schema.graphql | 3 + src/model/generated/token.model.ts | 9 ++ src/utils/entitiesManager.ts | 23 +-- 6 files changed, 114 insertions(+), 152 deletions(-) create mode 100644 db/migrations/1662233768902-Data.js diff --git a/db/migrations/1662233768902-Data.js b/db/migrations/1662233768902-Data.js new file mode 100644 index 0000000..8a5f7a5 --- /dev/null +++ b/db/migrations/1662233768902-Data.js @@ -0,0 +1,15 @@ +module.exports = class Data1662233768902 { + name = 'Data1662233768902' + + async up(db) { + await db.query(`ALTER TABLE "token" DROP CONSTRAINT "FK_5c85dbbd108d915a13f71de39ad"`) + await db.query(`ALTER TABLE "token" ALTER COLUMN "contract_id" SET NOT NULL`) + await db.query(`ALTER TABLE "token" ADD CONSTRAINT "FK_5c85dbbd108d915a13f71de39ad" FOREIGN KEY ("contract_id") REFERENCES "contract"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`) + } + + async down(db) { + await db.query(`ALTER TABLE "token" ADD CONSTRAINT "FK_5c85dbbd108d915a13f71de39ad" FOREIGN KEY ("contract_id") REFERENCES "contract"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`) + await db.query(`ALTER TABLE "token" ALTER COLUMN "contract_id" DROP NOT NULL`) + await db.query(`ALTER TABLE "token" DROP CONSTRAINT "FK_5c85dbbd108d915a13f71de39ad"`) + } +} diff --git a/package-lock.json b/package-lock.json index c47e6b7..ef6d2ae 100644 --- a/package-lock.json +++ b/package-lock.json @@ -24,7 +24,7 @@ "devDependencies": { "@subsquid/substrate-metadata-explorer": "1.0.6", "@subsquid/substrate-typegen": "1.2.2", - "@subsquid/typeorm-codegen": "0.1.0", + "@subsquid/typeorm-codegen": "0.1.1", "@types/node": "^17.0.23", "@typescript-eslint/eslint-plugin": "^4.33.0", "eslint": "^7.32.0", @@ -2040,22 +2040,6 @@ "openreader": "bin/main.js" } }, - "node_modules/@subsquid/graphql-server/node_modules/@subsquid/typeorm-config": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/@subsquid/typeorm-config/-/typeorm-config-2.0.0.tgz", - "integrity": "sha512-TJ/ksxTLTEHqY6NiYM5S9JXFdUEROTjTY+PKQv3ixoQuuidNzIGRwwGvzMnSKNl9s1V5ADcwWIEbmcPBZjHURg==", - "dependencies": { - "@subsquid/util-naming": "^0.0.1" - }, - "peerDependencies": { - "typeorm": "^0.3.6" - }, - "peerDependenciesMeta": { - "typeorm": { - "optional": true - } - } - }, "node_modules/@subsquid/graphql-server/node_modules/dotenv": { "version": "10.0.0", "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-10.0.0.tgz", @@ -2207,22 +2191,6 @@ } } }, - "node_modules/@subsquid/substrate-processor/node_modules/@subsquid/typeorm-config": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/@subsquid/typeorm-config/-/typeorm-config-2.0.0.tgz", - "integrity": "sha512-TJ/ksxTLTEHqY6NiYM5S9JXFdUEROTjTY+PKQv3ixoQuuidNzIGRwwGvzMnSKNl9s1V5ADcwWIEbmcPBZjHURg==", - "dependencies": { - "@subsquid/util-naming": "^0.0.1" - }, - "peerDependencies": { - "typeorm": "^0.3.6" - }, - "peerDependenciesMeta": { - "typeorm": { - "optional": true - } - } - }, "node_modules/@subsquid/substrate-typegen": { "version": "1.2.2", "resolved": "https://registry.npmjs.org/@subsquid/substrate-typegen/-/substrate-typegen-1.2.2.tgz", @@ -2246,13 +2214,13 @@ } }, "node_modules/@subsquid/typeorm-codegen": { - "version": "0.1.0", - "resolved": "https://registry.npmjs.org/@subsquid/typeorm-codegen/-/typeorm-codegen-0.1.0.tgz", - "integrity": "sha512-LydNeHIxArFYFBJn2ZEpdidFIsEGy0PaIh409b083Vcoawuit+rchxoouzIrDByoa/VjSks9zJPfVYuwzzbrxQ==", + "version": "0.1.1", + "resolved": "https://registry.npmjs.org/@subsquid/typeorm-codegen/-/typeorm-codegen-0.1.1.tgz", + "integrity": "sha512-vLissbWuoJ85zccSMH99ghLq3CEKeyEMZW/AkWFNh2kp0z2eVw8iFpBpNOoLxWj9Q46zvcvTxpmLxSuqNpYw+w==", "dev": true, "dependencies": { - "@subsquid/openreader": "^1.0.3", - "@subsquid/typeorm-config": "^1.1.0", + "@subsquid/openreader": "^2.0.0", + "@subsquid/typeorm-config": "^2.0.0", "@subsquid/util-internal": "^0.0.1", "@subsquid/util-internal-code-printer": "^0.0.2", "@subsquid/util-naming": "^0.0.1", @@ -2262,35 +2230,66 @@ "squid-typeorm-codegen": "bin/run.js" } }, + "node_modules/@subsquid/typeorm-codegen/node_modules/@subsquid/graphiql-console": { + "version": "0.3.0", + "resolved": "https://registry.npmjs.org/@subsquid/graphiql-console/-/graphiql-console-0.3.0.tgz", + "integrity": "sha512-C89mus6IXnNi0xMQrZqUFBZwLj8tbuq9lye8Gq/lHmmERAUpi6UsWEyLdJLx2mneZzF3JtY8eNiiZ16jmjtvfw==", + "dev": true + }, "node_modules/@subsquid/typeorm-codegen/node_modules/@subsquid/openreader": { - "version": "1.0.3", - "resolved": "https://registry.npmjs.org/@subsquid/openreader/-/openreader-1.0.3.tgz", - "integrity": "sha512-mf12hO47I6BuU/x9dyYMW526R5if2OTGgHFw2ANRkjSIVVa0x7wjKpXRoVVY/VSAU2PKHEaSzhVSM8pTfGTK0g==", + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/@subsquid/openreader/-/openreader-2.1.0.tgz", + "integrity": "sha512-QLsB4u30EeKYXu2snjKPkeqtQqzmS/A4yBBfTAgNDBsFv5HtfJCPmJruIiU8D5wDuGhzzDLRIXn15NoJonnouA==", "dev": true, "dependencies": { "@graphql-tools/merge": "^8", - "@graphql-tools/utils": "^8", - "@subsquid/graphiql-console": "^0.2.0", + "@subsquid/graphiql-console": "^0.3.0", + "@subsquid/logger": "^0.1.0", "@subsquid/util-internal": "^0.0.1", - "@subsquid/util-internal-code-printer": "^0.0.2", + "@subsquid/util-internal-commander": "^0.0.0", "@subsquid/util-internal-hex": "^0.0.1", + "@subsquid/util-internal-http-server": "^0.1.0", "@subsquid/util-naming": "^0.0.1", "apollo-server-core": "^3.9.0", "apollo-server-express": "^3.9.0", + "commander": "^9.3.0", + "deep-equal": "^2.0.5", "express": "^4.18.1", "graphql": "^15.8.0", "graphql-parse-resolve-info": "^4.12.3", - "pg": "^8.7.3" + "graphql-ws": "^5.9.1", + "pg": "^8.7.3", + "ws": "^8.8.1" }, "bin": { "openreader": "bin/main.js" } }, - "node_modules/@subsquid/typeorm-config": { - "version": "1.1.0", - "resolved": "https://registry.npmjs.org/@subsquid/typeorm-config/-/typeorm-config-1.1.0.tgz", - "integrity": "sha512-7qIOlpodIvIhrm3pudu2jpzMXx3aCxj39FPMV38NPzbft5AEKTmfRJPPSzBDbsYnz+WFJECDkLEb2vnFUsfuDw==", + "node_modules/@subsquid/typeorm-codegen/node_modules/ws": { + "version": "8.8.1", + "resolved": "https://registry.npmjs.org/ws/-/ws-8.8.1.tgz", + "integrity": "sha512-bGy2JzvzkPowEJV++hF07hAD6niYSr0JzBNo/J29WsB57A2r7Wlc1UFcTR9IzrPvuNVO4B8LGqF8qcpsVOhJCA==", "dev": true, + "engines": { + "node": ">=10.0.0" + }, + "peerDependencies": { + "bufferutil": "^4.0.1", + "utf-8-validate": "^5.0.2" + }, + "peerDependenciesMeta": { + "bufferutil": { + "optional": true + }, + "utf-8-validate": { + "optional": true + } + } + }, + "node_modules/@subsquid/typeorm-config": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/@subsquid/typeorm-config/-/typeorm-config-2.0.0.tgz", + "integrity": "sha512-TJ/ksxTLTEHqY6NiYM5S9JXFdUEROTjTY+PKQv3ixoQuuidNzIGRwwGvzMnSKNl9s1V5ADcwWIEbmcPBZjHURg==", "dependencies": { "@subsquid/util-naming": "^0.0.1" }, @@ -2325,22 +2324,6 @@ "typeorm": "^0.3.6" } }, - "node_modules/@subsquid/typeorm-migration/node_modules/@subsquid/typeorm-config": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/@subsquid/typeorm-config/-/typeorm-config-2.0.0.tgz", - "integrity": "sha512-TJ/ksxTLTEHqY6NiYM5S9JXFdUEROTjTY+PKQv3ixoQuuidNzIGRwwGvzMnSKNl9s1V5ADcwWIEbmcPBZjHURg==", - "dependencies": { - "@subsquid/util-naming": "^0.0.1" - }, - "peerDependencies": { - "typeorm": "^0.3.6" - }, - "peerDependenciesMeta": { - "typeorm": { - "optional": true - } - } - }, "node_modules/@subsquid/typeorm-migration/node_modules/dotenv": { "version": "10.0.0", "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-10.0.0.tgz", @@ -2361,22 +2344,6 @@ "typeorm": "^0.3.6" } }, - "node_modules/@subsquid/typeorm-store/node_modules/@subsquid/typeorm-config": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/@subsquid/typeorm-config/-/typeorm-config-2.0.0.tgz", - "integrity": "sha512-TJ/ksxTLTEHqY6NiYM5S9JXFdUEROTjTY+PKQv3ixoQuuidNzIGRwwGvzMnSKNl9s1V5ADcwWIEbmcPBZjHURg==", - "dependencies": { - "@subsquid/util-naming": "^0.0.1" - }, - "peerDependencies": { - "typeorm": "^0.3.6" - }, - "peerDependenciesMeta": { - "typeorm": { - "optional": true - } - } - }, "node_modules/@subsquid/util": { "version": "0.0.5", "resolved": "https://registry.npmjs.org/@subsquid/util/-/util-0.0.5.tgz", @@ -13446,14 +13413,6 @@ "ws": "^8.8.1" } }, - "@subsquid/typeorm-config": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/@subsquid/typeorm-config/-/typeorm-config-2.0.0.tgz", - "integrity": "sha512-TJ/ksxTLTEHqY6NiYM5S9JXFdUEROTjTY+PKQv3ixoQuuidNzIGRwwGvzMnSKNl9s1V5ADcwWIEbmcPBZjHURg==", - "requires": { - "@subsquid/util-naming": "^0.0.1" - } - }, "dotenv": { "version": "10.0.0", "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-10.0.0.tgz", @@ -13574,16 +13533,6 @@ "@subsquid/util-xxhash": "^0.1.1", "blake2b": "^2.1.4", "prom-client": "^14.0.1" - }, - "dependencies": { - "@subsquid/typeorm-config": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/@subsquid/typeorm-config/-/typeorm-config-2.0.0.tgz", - "integrity": "sha512-TJ/ksxTLTEHqY6NiYM5S9JXFdUEROTjTY+PKQv3ixoQuuidNzIGRwwGvzMnSKNl9s1V5ADcwWIEbmcPBZjHURg==", - "requires": { - "@subsquid/util-naming": "^0.0.1" - } - } } }, "@subsquid/substrate-typegen": { @@ -13606,47 +13555,64 @@ } }, "@subsquid/typeorm-codegen": { - "version": "0.1.0", - "resolved": "https://registry.npmjs.org/@subsquid/typeorm-codegen/-/typeorm-codegen-0.1.0.tgz", - "integrity": "sha512-LydNeHIxArFYFBJn2ZEpdidFIsEGy0PaIh409b083Vcoawuit+rchxoouzIrDByoa/VjSks9zJPfVYuwzzbrxQ==", + "version": "0.1.1", + "resolved": "https://registry.npmjs.org/@subsquid/typeorm-codegen/-/typeorm-codegen-0.1.1.tgz", + "integrity": "sha512-vLissbWuoJ85zccSMH99ghLq3CEKeyEMZW/AkWFNh2kp0z2eVw8iFpBpNOoLxWj9Q46zvcvTxpmLxSuqNpYw+w==", "dev": true, "requires": { - "@subsquid/openreader": "^1.0.3", - "@subsquid/typeorm-config": "^1.1.0", + "@subsquid/openreader": "^2.0.0", + "@subsquid/typeorm-config": "^2.0.0", "@subsquid/util-internal": "^0.0.1", "@subsquid/util-internal-code-printer": "^0.0.2", "@subsquid/util-naming": "^0.0.1", "commander": "^9.3.0" }, "dependencies": { + "@subsquid/graphiql-console": { + "version": "0.3.0", + "resolved": "https://registry.npmjs.org/@subsquid/graphiql-console/-/graphiql-console-0.3.0.tgz", + "integrity": "sha512-C89mus6IXnNi0xMQrZqUFBZwLj8tbuq9lye8Gq/lHmmERAUpi6UsWEyLdJLx2mneZzF3JtY8eNiiZ16jmjtvfw==", + "dev": true + }, "@subsquid/openreader": { - "version": "1.0.3", - "resolved": "https://registry.npmjs.org/@subsquid/openreader/-/openreader-1.0.3.tgz", - "integrity": "sha512-mf12hO47I6BuU/x9dyYMW526R5if2OTGgHFw2ANRkjSIVVa0x7wjKpXRoVVY/VSAU2PKHEaSzhVSM8pTfGTK0g==", + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/@subsquid/openreader/-/openreader-2.1.0.tgz", + "integrity": "sha512-QLsB4u30EeKYXu2snjKPkeqtQqzmS/A4yBBfTAgNDBsFv5HtfJCPmJruIiU8D5wDuGhzzDLRIXn15NoJonnouA==", "dev": true, "requires": { "@graphql-tools/merge": "^8", - "@graphql-tools/utils": "^8", - "@subsquid/graphiql-console": "^0.2.0", + "@subsquid/graphiql-console": "^0.3.0", + "@subsquid/logger": "^0.1.0", "@subsquid/util-internal": "^0.0.1", - "@subsquid/util-internal-code-printer": "^0.0.2", + "@subsquid/util-internal-commander": "^0.0.0", "@subsquid/util-internal-hex": "^0.0.1", + "@subsquid/util-internal-http-server": "^0.1.0", "@subsquid/util-naming": "^0.0.1", "apollo-server-core": "^3.9.0", "apollo-server-express": "^3.9.0", + "commander": "^9.3.0", + "deep-equal": "^2.0.5", "express": "^4.18.1", "graphql": "^15.8.0", "graphql-parse-resolve-info": "^4.12.3", - "pg": "^8.7.3" + "graphql-ws": "^5.9.1", + "pg": "^8.7.3", + "ws": "^8.8.1" } + }, + "ws": { + "version": "8.8.1", + "resolved": "https://registry.npmjs.org/ws/-/ws-8.8.1.tgz", + "integrity": "sha512-bGy2JzvzkPowEJV++hF07hAD6niYSr0JzBNo/J29WsB57A2r7Wlc1UFcTR9IzrPvuNVO4B8LGqF8qcpsVOhJCA==", + "dev": true, + "requires": {} } } }, "@subsquid/typeorm-config": { - "version": "1.1.0", - "resolved": "https://registry.npmjs.org/@subsquid/typeorm-config/-/typeorm-config-1.1.0.tgz", - "integrity": "sha512-7qIOlpodIvIhrm3pudu2jpzMXx3aCxj39FPMV38NPzbft5AEKTmfRJPPSzBDbsYnz+WFJECDkLEb2vnFUsfuDw==", - "dev": true, + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/@subsquid/typeorm-config/-/typeorm-config-2.0.0.tgz", + "integrity": "sha512-TJ/ksxTLTEHqY6NiYM5S9JXFdUEROTjTY+PKQv3ixoQuuidNzIGRwwGvzMnSKNl9s1V5ADcwWIEbmcPBZjHURg==", "requires": { "@subsquid/util-naming": "^0.0.1" } @@ -13663,14 +13629,6 @@ "dotenv": "^10.0.0" }, "dependencies": { - "@subsquid/typeorm-config": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/@subsquid/typeorm-config/-/typeorm-config-2.0.0.tgz", - "integrity": "sha512-TJ/ksxTLTEHqY6NiYM5S9JXFdUEROTjTY+PKQv3ixoQuuidNzIGRwwGvzMnSKNl9s1V5ADcwWIEbmcPBZjHURg==", - "requires": { - "@subsquid/util-naming": "^0.0.1" - } - }, "dotenv": { "version": "10.0.0", "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-10.0.0.tgz", @@ -13685,16 +13643,6 @@ "requires": { "@subsquid/typeorm-config": "^2.0.0", "@subsquid/util-internal": "^0.0.1" - }, - "dependencies": { - "@subsquid/typeorm-config": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/@subsquid/typeorm-config/-/typeorm-config-2.0.0.tgz", - "integrity": "sha512-TJ/ksxTLTEHqY6NiYM5S9JXFdUEROTjTY+PKQv3ixoQuuidNzIGRwwGvzMnSKNl9s1V5ADcwWIEbmcPBZjHURg==", - "requires": { - "@subsquid/util-naming": "^0.0.1" - } - } } }, "@subsquid/util": { diff --git a/package.json b/package.json index 57943e0..7eb899b 100644 --- a/package.json +++ b/package.json @@ -27,7 +27,7 @@ "devDependencies": { "@subsquid/substrate-metadata-explorer": "1.0.6", "@subsquid/substrate-typegen": "1.2.2", - "@subsquid/typeorm-codegen": "0.1.0", + "@subsquid/typeorm-codegen": "0.1.1", "@types/node": "^17.0.23", "@typescript-eslint/eslint-plugin": "^4.33.0", "eslint": "^7.32.0", diff --git a/schema.graphql b/schema.graphql index fa6266d..22a6013 100644 --- a/schema.graphql +++ b/schema.graphql @@ -26,6 +26,7 @@ type Metadata @entity { type Token @entity { id: ID! numericId: BigInt! @index + ownerId: ID owner: Owner # tokenURI of contract tokenUri: String @@ -35,7 +36,9 @@ type Token @entity { updatedAt: BigInt! @index createdAt: BigInt! @index transfers: [Transfer!]! @derivedFrom(field: "token") + contractId: ID! contract: Contract! + metadataId: ID metadata: Metadata } diff --git a/src/model/generated/token.model.ts b/src/model/generated/token.model.ts index 43b2ec7..163d209 100644 --- a/src/model/generated/token.model.ts +++ b/src/model/generated/token.model.ts @@ -18,6 +18,9 @@ export class Token { @Column_("numeric", {transformer: marshal.bigintTransformer, nullable: false}) numericId!: bigint + @Column_("text", {nullable: true}) + ownerId!: string | undefined | null + @Index_() @ManyToOne_(() => Owner, {nullable: true}) owner!: Owner | undefined | null @@ -39,10 +42,16 @@ export class Token { @OneToMany_(() => Transfer, e => e.token) transfers!: Transfer[] + @Column_("text", {nullable: false}) + contractId!: string + @Index_() @ManyToOne_(() => Contract, {nullable: true}) contract!: Contract + @Column_("text", {nullable: true}) + metadataId!: string | undefined | null + @Index_() @ManyToOne_(() => Metadata, {nullable: true}) metadata!: Metadata | undefined | null diff --git a/src/utils/entitiesManager.ts b/src/utils/entitiesManager.ts index 7835298..cfb9e19 100644 --- a/src/utils/entitiesManager.ts +++ b/src/utils/entitiesManager.ts @@ -1,6 +1,6 @@ import { Store, EntityClass } from '@subsquid/typeorm-store' import { assert } from 'console' -import { FindOptionsRelations, FindOptionsWhere } from 'typeorm' +import { FindOptionsRelations, FindOptionsWhere, In, Not } from 'typeorm' import { Contract, @@ -78,14 +78,9 @@ class TokensCache extends EntitiesCache { db: Store, contractAddress: string ): Promise> { - const cachedTokens: Token[] = [] - this.cache.forEach((token, tokenId) => { - if (tokenId.startsWith(contractAddress)) { - cachedTokens.push(token) - } - }) const allTokens = await db.find(Token, { where: { + id: Not(In([...this.cache.keys()])), contract: { id: contractAddress, }, @@ -93,19 +88,11 @@ class TokensCache extends EntitiesCache { }) // Replace db tokens that exists in cache - cachedTokens.forEach((token) => { - const replaceId = allTokens.findIndex((dbToken) => dbToken.id === token.id) - // Tokens only that exist in db could be cached - assert(replaceId >= 0) - allTokens[replaceId] = token + allTokens.forEach((token) => { + this.cache.set(token.id, token) }) - // Add everything from db to in-memory cache - allTokens.forEach((token)=>{ - this.addCache(token) - }) - - return allTokens + return [...this.cache.values()] } }