Skip to content

Commit

Permalink
Add option to ignore deterministic docs with empty contents (#215)
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulLeCam authored Feb 26, 2024
1 parent efd8385 commit f0f146e
Show file tree
Hide file tree
Showing 8 changed files with 525 additions and 713 deletions.
14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@
},
"pnpm": {
"overrides": {
"@ceramicnetwork/3id-did-resolver": "^5.0.1-rc.1",
"@ceramicnetwork/cli": "^5.1.0-rc.1",
"@ceramicnetwork/common": "^5.1.0-rc.0",
"@ceramicnetwork/core": "^5.1.0-rc.1",
"@ceramicnetwork/http-client": "^5.0.1-rc.1",
"@ceramicnetwork/stream-model": "^4.0.1-rc.1",
"@ceramicnetwork/stream-model-instance": "^4.0.1-rc.1",
"@ceramicnetwork/3id-did-resolver": "^5.2.0-rc.0",
"@ceramicnetwork/cli": "^5.2.0-rc.0",
"@ceramicnetwork/common": "^5.1.0",
"@ceramicnetwork/core": "^5.2.0-rc.0",
"@ceramicnetwork/http-client": "^5.2.0-rc.0",
"@ceramicnetwork/stream-model": "^4.1.0",
"@ceramicnetwork/stream-model-instance": "^4.2.0-rc.0",
"@ceramicnetwork/streamid": "^5.0.0"
}
}
Expand Down
4 changes: 1 addition & 3 deletions packages/loader/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,7 @@
"dependencies": {
"@ceramicnetwork/stream-model-instance": "^4.0.1-rc.1",
"@ceramicnetwork/streamid": "^5.0.0",
"@ipld/dag-cbor": "^9.2.0",
"dataloader": "^2.2.2",
"multiformats": "^13.1.0"
"dataloader": "^2.2.2"
},
"devDependencies": {
"@ceramicnetwork/common": "^5.1.0-rc.0",
Expand Down
16 changes: 2 additions & 14 deletions packages/loader/src/deterministic.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import type { GenesisCommit, GenesisHeader } from '@ceramicnetwork/common'
import { ModelInstanceDocument } from '@ceramicnetwork/stream-model-instance'
import { StreamID } from '@ceramicnetwork/streamid'
import * as codec from '@ipld/dag-cbor'
import { CID } from 'multiformats/cid'
import { sha256 } from 'multiformats/hashes/sha2'

import type { LoadKey } from './types.js'

Expand All @@ -25,22 +22,13 @@ export function createDeterministicGenesis(meta: GenesisMetadata): GenesisCommit
return { header, data: null }
}

/**
* Create a StreamID from a GenesisCommit.
*/
export async function createGenesisID(genesis: GenesisCommit): Promise<StreamID> {
const bytes = codec.encode(genesis)
const digest = await sha256.digest(bytes)
const cid = CID.createV1(codec.code, digest)
return new StreamID(ModelInstanceDocument.STREAM_TYPE_ID, cid)
}

/**
* Create a LoadKey for a deterministic stream.
*/
export async function createDeterministicKey(meta: GenesisMetadata): Promise<LoadKey> {
const genesis = createDeterministicGenesis(meta)
return { id: await createGenesisID(genesis), genesis }
const id = await StreamID.fromGenesis(ModelInstanceDocument.STREAM_TYPE_ID, genesis)
return { id, genesis }
}

/**
Expand Down
8 changes: 6 additions & 2 deletions packages/loader/src/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ export type CreateOptions = CreateOpts & {
shouldIndex?: boolean
}

export type DeterministicLoadOptions = CreateOptions & {
export type DeterministicLoadOptions = CreateOpts & {
ignoreEmpty?: boolean
onlyIndexed?: boolean
}

Expand Down Expand Up @@ -197,7 +198,10 @@ export class DocumentLoader extends DataLoader<LoadKey, ModelInstanceDocument, s
const opts = { ...DEFAULT_DETERMINISTIC_OPTIONS, ...options }
const key = await this._getDeterministicKey(meta)
const doc = await this.load<T>({ ...key, opts })
return options.onlyIndexed === false || doc.metadata.shouldIndex !== false ? doc : null
return (options.onlyIndexed === false || doc.metadata.shouldIndex !== false) &&
(!options.ignoreEmpty || doc.content != null)
? doc
: null
}

/**
Expand Down
4 changes: 2 additions & 2 deletions packages/loader/test/loader.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ describe('loader', () => {
expect(multiQuery).not.toHaveBeenCalled()
})

test('loadSingle() method calls ModelInstanceDocument.single() and adds the stream to the cache', async () => {
test('loadSingle() method loads deterministic documents using the SINGLE account relation', async () => {
const metadata = { controller: 'did:test:123', model: testStreamID }
const loadKey = await createDeterministicKey(metadata)
const { id, genesis } = loadKey
Expand Down Expand Up @@ -252,7 +252,7 @@ describe('loader', () => {
expect(multiQuery).toHaveBeenCalledTimes(1)
})

test('loadSet() method calls ModelInstanceDocument.set() and adds the stream to the cache', async () => {
test('loadSet() method loads deterministic documents using the SET account relation', async () => {
const metadata = { controller: 'did:test:123', model: testStreamID }
const unique = ['foo']
const loadKey = await createDeterministicKey({ ...metadata, unique })
Expand Down
20 changes: 16 additions & 4 deletions packages/runtime/src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,10 @@ class SchemaBuilder {
config[alias] = {
type: this.#types[reference.name],
resolve: async (account, _, ctx): Promise<ModelInstanceDocument | null> => {
return await ctx.loader.loadSingle(account, model.id)
return await ctx.loader.loadSingle(account, model.id, {
ignoreEmpty: true,
onlyIndexed: true,
})
},
}
break
Expand Down Expand Up @@ -413,7 +416,10 @@ class SchemaBuilder {
const value = args.with[field]
return value ? String(value) : ''
})
return await ctx.loader.loadSet(account, model.id, unique)
return await ctx.loader.loadSet(account, model.id, unique, {
ignoreEmpty: true,
onlyIndexed: true,
})
},
}
break
Expand Down Expand Up @@ -459,7 +465,10 @@ class SchemaBuilder {
const value = args.with[field]
return value ? String(value) : ''
})
return await ctx.loader.loadSet(refAccount, model.id, unique)
return await ctx.loader.loadSet(refAccount, model.id, unique, {
ignoreEmpty: true,
onlyIndexed: true,
})
},
}

Expand Down Expand Up @@ -956,7 +965,10 @@ class SchemaBuilder {
const value = args.with[field]
return value ? String(value) : ''
})
return await ctx.loader.loadSet(account, model.id, unique)
return await ctx.loader.loadSet(account, model.id, unique, {
ignoreEmpty: true,
onlyIndexed: true,
})
},
}
}
Expand Down
Loading

0 comments on commit f0f146e

Please sign in to comment.