Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for sanity::documentsOf() #254

Merged
merged 3 commits into from
Aug 8, 2024
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
34 changes: 33 additions & 1 deletion src/evaluator/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ sanity['versionOf'] = async function (args, scope, execute) {
const baseId = value.data

// All the document are a version of the given ID if:
// 1. Document ID is of the ford bundleId.documentGroupId
// 1. Document ID is of the form bundleId.documentGroupId
// 2. And, they have a field called _version which is an object.
const versionIds: string[] = []
for await (const value of scope.source) {
Expand All @@ -458,6 +458,38 @@ sanity['versionOf'] = async function (args, scope, execute) {
}
sanity['versionOf'].arity = 1

// eslint-disable-next-line require-await
sanity['documentsOf'] = async function (args, scope, execute) {
if (!scope.source.isArray()) return NULL_VALUE

const value = await execute(args[0], scope)
if (value.type !== 'string') return NULL_VALUE
const baseId = value.data

// A document belongs to a bundle ID if:
// 1. Document ID is of the form bundleId.documentGroupId
// 2. And, they have a field called _version which is an object.
const documentIdsInBundle: string[] = []
for await (const value of scope.source) {
if (getType(value) === 'object') {
const val = await value.get()
if (
val &&
'_id' in val &&
val._id.split('.').length === 2 &&
val._id.startsWith(`${baseId}.`) &&
'_version' in val &&
typeof val._version === 'object'
) {
documentIdsInBundle.push(val._id)
}
}
}

return fromJS(documentIdsInBundle)
}
sanity['documentsOf'].arity = 1

export type GroqPipeFunction = (
base: Value,
args: ExprNode[],
Expand Down
9 changes: 9 additions & 0 deletions src/typeEvaluator/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,15 @@ export function handleFuncCallNode(node: FuncCallNode, scope: Scope): TypeNode {
return {type: 'array', of: {type: 'string'}}
})
}
case 'sanity.documentsOf': {
const typeNode = walk({node: node.args[0], scope})
return mapConcrete(typeNode, scope, (typeNode) => {
if (typeNode.type !== 'string') {
return {type: 'null'}
}
return {type: 'array', of: {type: 'string'}}
})
}
default: {
return {type: 'unknown'}
}
Expand Down
17 changes: 17 additions & 0 deletions test/evaluate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,23 @@ t.test('Basic parsing', async (t) => {
const data = await value.get()
t.same(data, {versions: ['drafts.doc1', 'sale.doc1']})
})

t.test('sanity::documentsOf()', async (t) => {
const dataset = [
{_id: 'doc1', _version: {}},
{_id: 'drafts.doc1', _version: {}},
{_id: 'sale.doc1', _version: {}},
{_id: 'sale.doc2', _version: {}},
{_id: 'sale.doc3'},
{_id: 'weekend.sale.doc1', _version: {}},
{_id: 'doc2', _version: {}},
]

const tree = parse('{"documentsInBundle": sanity::documentsOf("sale")}')
const value = await evaluate(tree, {dataset})
const data = await value.get()
t.same(data, {documentsInBundle: ['sale.doc1', 'sale.doc2']})
})
})

t.test('Custom dereference function', async (t) => {
Expand Down
27 changes: 27 additions & 0 deletions test/typeEvaluate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3137,6 +3137,33 @@ t.test('function: sanity::versionOf', (t) => {
t.end()
})

t.test('function: sanity::documentsOf', (t) => {
const query = `*[_type == "author"] {
"saleBundleDocuments": sanity::documentsOf("sale")
}`
const ast = parse(query)
const res = typeEvaluate(ast, schemas)

t.strictSame(res, {
type: 'array',
of: {
type: 'object',
attributes: {
saleBundleDocuments: {
type: 'objectAttribute',
value: {
type: 'array',
of: {
type: 'string',
},
},
},
},
},
})
t.end()
})

function findSchemaType(name: string): TypeNode {
const type = schemas.find((s) => s.name === name)
if (!type) {
Expand Down