Skip to content

Commit

Permalink
Merge pull request #247 from kodadot/test-in-prod
Browse files Browse the repository at this point in the history
test in prod
  • Loading branch information
vikiival authored Apr 16, 2023
2 parents 115c951 + 8a4c876 commit 6fb420c
Show file tree
Hide file tree
Showing 15 changed files with 169 additions and 69 deletions.
15 changes: 15 additions & 0 deletions db/migrations/1681650285646-Data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module.exports = class Data1681650285646 {
name = 'Data1681650285646'

async up(db) {
await db.query(`CREATE TABLE "property" ("id" character varying NOT NULL, "key" text NOT NULL, "value" text NOT NULL, "type" text NOT NULL, "mutable" boolean NOT NULL, "nft_id" character varying, CONSTRAINT "PK_d80743e6191258a5003d5843b4f" PRIMARY KEY ("id"))`)
await db.query(`CREATE INDEX "IDX_e8ead80cf8ed86716aa80ef45e" ON "property" ("nft_id") `)
await db.query(`ALTER TABLE "property" ADD CONSTRAINT "FK_e8ead80cf8ed86716aa80ef45e7" FOREIGN KEY ("nft_id") REFERENCES "nft_entity"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`)
}

async down(db) {
await db.query(`DROP TABLE "property"`)
await db.query(`DROP INDEX "public"."IDX_e8ead80cf8ed86716aa80ef45e"`)
await db.query(`ALTER TABLE "property" DROP CONSTRAINT "FK_e8ead80cf8ed86716aa80ef45e7"`)
}
}
11 changes: 11 additions & 0 deletions db/migrations/1681650453802-Data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module.exports = class Data1681650453802 {
name = 'Data1681650453802'

async up(db) {
await db.query(`ALTER TABLE "part" ALTER COLUMN "z" DROP NOT NULL`)
}

async down(db) {
await db.query(`ALTER TABLE "part" ALTER COLUMN "z" SET NOT NULL`)
}
}
40 changes: 14 additions & 26 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
"@kodadot1/metasquid": "^0.1.5-rc.0",
"@kodadot1/minimark": "0.1.7-rc.3",
"@kodadot1/minipfs": "^0.3.0-rc.0",
"@subsquid/archive-registry": "2.1.10",
"@subsquid/archive-registry": "2.1.12",
"@subsquid/big-decimal": "^0.0.0",
"@subsquid/cli": "^2.2.1",
"@subsquid/cli": "^2.2.2",
"@subsquid/graphql-server": "^3.3.2",
"@subsquid/ss58": "^0.1.4",
"@subsquid/substrate-processor": "2.4.1",
Expand Down
2 changes: 1 addition & 1 deletion schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ type Part @entity {
thumb: String # RMRK GraphQL does not have this
# resources: [ResourcePart!] @derivedFrom (field: "resource")
type: PartType!
z: Int!
z: Int
}


Expand Down
10 changes: 8 additions & 2 deletions src/mappings/v2/addResource.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { burned, plsNotBe } from '@kodadot1/metasquid/consolidator'
import { getOrCreate, getOrFail as get, getOptional } from '@kodadot1/metasquid/entity'
import { getOrFail as get, getOptional, getOrCreate } from '@kodadot1/metasquid/entity'
import { Optional } from '@kodadot1/metasquid/types'
import { Resadd } from '@kodadot1/minimark/v2'

Expand All @@ -8,7 +8,7 @@ import { handleMetadata } from '../shared'
import { createEvent } from '../shared/event'
import { unwrap } from '../utils'
import { isIssuerOrElseError } from '../utils/consolidator'
import logger, { error, success } from '../utils/logger'
import logger, { error, success, warn } from '../utils/logger'
import { Action, Context } from '../utils/types'
import { getAddRes } from './getters'

Expand Down Expand Up @@ -43,11 +43,17 @@ export async function addResource(context: Context) {

if (interaction.value.base) {
const base = await getOptional<Base>(context.store, Base, interaction.value.base)
if (!base) {
warn(OPERATION, `Base ${interaction.value.base} not found`)
}
final.base = base
}

if (interaction.value.slot) {
const part = await getOptional<Part>(context.store, Part, interaction.value.slot)
if (!part) {
warn(OPERATION, `Base ${interaction.value.base} not found`)
}
final.slot = part
}

Expand Down
13 changes: 11 additions & 2 deletions src/mappings/v2/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Base, BaseType, Part, PartType } from '../../model'
import { handleMetadata } from '../shared'
import { unwrap } from '../utils/extract'
import { baseId } from '../utils/helper'
import logger, { error } from '../utils/logger'
import logger, { error, success } from '../utils/logger'
import { Action, Context } from '../utils/types'
import { createUnlessNotExist } from '../utils/verbose'
import { getCreateBase } from './getters'
Expand All @@ -17,7 +17,7 @@ export async function base(context: Context) {
try {
const { value: interaction, caller, timestamp, blockNumber, version } = unwrap(context, getCreateBase)
const base = interaction.value as CreatedBase
const id = baseId(blockNumber, base.symbol)
const id = baseId(base.symbol, blockNumber)
const final = await createUnlessNotExist(id, Base, context)
final.issuer = caller
final.currentOwner = caller
Expand All @@ -43,6 +43,13 @@ export async function base(context: Context) {
part.id = partId
part.metadata = basePart.metadata
part.type = basePart.type as PartType
if (basePart.type === PartType.slot) {
part.equippable = Array.isArray(basePart.equippable) ? basePart.equippable : ['*']
part.z = basePart.z
}

part.src = basePart.src
part.thumb = basePart.thumb

if (basePart.metadata) {
const metadata = await handleMetadata(basePart.metadata, '', context.store)
Expand All @@ -52,6 +59,8 @@ export async function base(context: Context) {
await context.store.save(part)
}
}

success(OPERATION, `${final.id} from ${caller}`)
// TODO: themes
// if (base.themes) {
// const keys = ['theme_color_1', 'theme_color_2', 'theme_color_3', 'theme_color_4']
Expand Down
4 changes: 2 additions & 2 deletions src/model/generated/part.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,6 @@ export class Part {
@Column_("varchar", {length: 5, nullable: false})
type!: PartType

@Column_("int4", {nullable: false})
z!: number
@Column_("int4", {nullable: true})
z!: number | undefined | null
}
32 changes: 32 additions & 0 deletions src/server-extension/model/child.model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { Field, ObjectType } from 'type-graphql';

@ObjectType()
export class ChildItemEntity {
@Field(() => String, { nullable: false })
id!: string

@Field(() => String, { nullable: true, defaultValue: '' })
name!: string

@Field(() => String, { nullable: true, defaultValue: '' })
image!: string

@Field(() => String, { nullable: true, defaultValue: '' })
media!: string

@Field(() => Boolean, { nullable: false })
pending!: boolean

@Field(() => String, { nullable: true, defaultValue: '', name: 'resourceMetadata' })
resource_metadata!: string

@Field(() => String, { nullable: true, defaultValue: '', name: 'resourceSrc' })
resource_src!: string

@Field(() => String, { nullable: true, defaultValue: '', name: 'resourceThumb' })
resource_thumb!: string

constructor(props: Partial<ChildItemEntity>) {
Object.assign(this, props);
}
}
28 changes: 14 additions & 14 deletions src/server-extension/model/event.model.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Field, Int, ObjectType } from "type-graphql";
import { Field, Int, ObjectType } from "type-graphql"

@ObjectType()
export class EventEntity {
Expand Down Expand Up @@ -31,28 +31,28 @@ export class HistoryEntity {
@ObjectType()
export class LastEventEntity {
@Field(() => String, { nullable: false })
id!: String;
id!: string;

@Field(() => String, { nullable: false })
name!: String;
name!: string;

@Field(() => String, { nullable: false })
issuer!: String;
issuer!: string;

@Field(() => Date, { nullable: false })
timestamp!: Date;

@Field(() => String, { nullable: false })
metadata!: String;
metadata!: string;

@Field(() => String, { nullable: false })
value!: String;
value!: string;

@Field(() => String, { nullable: false, name: "currentOwner" })
current_owner!: String;
current_owner!: string;

@Field(() => String, { nullable: true })
image!: String;
image!: string;

@Field(() => String, { nullable: true, name: "animationUrl" })
animation_url!: string | undefined | null;
Expand All @@ -63,16 +63,16 @@ export class LastEventEntity {
@Field(() => String, { nullable: false, name: "collectionName" })
collection_name!: string;

@Field(() => [Resource], { nullable: true })
resources!: Resource[];
@Field(() => [ResourceEntity], { nullable: true })
resources!: ResourceEntity[];

constructor(props: Partial<LastEventEntity>) {
Object.assign(this, props);
}
}

@ObjectType()
export class Resource {
export class ResourceEntity {
@Field(() => String, { nullable: false })
id!: string;

Expand All @@ -82,8 +82,8 @@ export class Resource {
@Field(() => String, { nullable: true })
metadata!: string;

@Field(() => String, { nullable: true })
slot!: string;
@Field(() => String, { nullable: true, name: "slotId" })
slot_id!: string;

@Field(() => String, { nullable: true })
thumb!: string;
Expand All @@ -96,7 +96,7 @@ export class Resource {

nftId!: string;

constructor(props: Partial<Resource>) {
constructor(props: Partial<ResourceEntity>) {
Object.assign(this, props);
}
}
2 changes: 1 addition & 1 deletion src/server-extension/query/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export const resourcesByNFT = (whereCondition: string) => `
SELECT r.id as id,
r.src as src,
r.metadata as metadata,
r.slot as slot,
r.slot_id,
r.thumb as thumb,
r.priority as priority,
r.pending as pending,
Expand Down
16 changes: 16 additions & 0 deletions src/server-extension/query/nft.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,20 @@ export const parentBaseResouceQuery = `
SELECT * FROM resource r
WHERE r.nft_id = $1
AND r.base_id = $2
`

export const childItemsQuery = `SELECT
ne.id,
ne.name,
ne.image,
ne.media,
ne.pending,
r.metadata as resource_metadata,
r.thumb as resource_thumb,
r.src as resource_src
FROM nft_entity ne
LEFT JOIN resource r
ON ne.id = r.nft_id
AND r.slot_id = ne.equipped_id
WHERE ne.parent_id = $1
`
21 changes: 21 additions & 0 deletions src/server-extension/resolvers/child.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Arg, Query, Resolver } from 'type-graphql'
import type { EntityManager } from 'typeorm'
import { NFTEntity } from '../../model'
import { ChildItemEntity } from '../model/child.model'
import { childItemsQuery } from '../query/nft'
import { makeQuery } from '../utils'



@Resolver()
export class ChildResolver {
constructor(private tx: () => Promise<EntityManager>) {}

@Query(() => [ChildItemEntity])
async childListByNftId(
@Arg('id', { nullable: false }) id: string,
): Promise<ChildItemEntity[]> {
const result: ChildItemEntity[] = await makeQuery(this.tx, NFTEntity, childItemsQuery, [id])
return result
}
}
Loading

0 comments on commit 6fb420c

Please sign in to comment.