Skip to content

Commit

Permalink
feat: populate id arrays (#46)
Browse files Browse the repository at this point in the history
* feat: populate id arrays

* add tests
  • Loading branch information
blackmann authored Jan 4, 2024
1 parent 68cc630 commit dfc137d
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 9 deletions.
72 changes: 72 additions & 0 deletions base/src/app.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,7 @@ describe('collection service', () => {
title: { required: true, type: 'string' },
},
},
headers: { authorization: `Bearer ${devAuthToken}` },
method: 'create',
path: 'collections',
})
Expand Down Expand Up @@ -801,6 +802,77 @@ describe('collection service', () => {
error: '`create` method not allowed on detail path',
})
})

describe('relations', () => {
beforeAll(async () => {
const { statusCode } = await app.api(
context({
data: {
name: 'discography',
schema: {
songs: {
items: { relation: 'songs', type: 'id' },
type: 'array',
},
year: { required: true, type: 'number' },
},
},
headers: { authorization: `Bearer ${devAuthToken}` },
method: 'create',
path: 'collections',
})
)

assert(statusCode === 201)

const { result: album } = await app.api(
context({
data: {
streams: 0,
title: 'Mock Album',
},
method: 'create',
path: 'albums',
})
)

const { result: song } = await app.api(
context({
data: {
album: album._id,
title: 'Mock Song 2',
},
method: 'create',
path: 'songs',
})
)

await app.api(
context({
data: {
songs: [song._id],
year: 2024,
},
method: 'create',
path: 'discography',
})
)
})

describe('$populate', () => {
it('returns populated data', async () => {
const { result } = await app.api(
context({ path: 'discography', query: { $populate: 'songs' } })
)

expect(result.data[0].songs[0]).toStrictEqual(
expect.objectContaining({
title: 'Mock Song 2',
})
)
})
})
})
})

describe.todo('authentication')
Expand Down
36 changes: 29 additions & 7 deletions base/src/collection-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,21 +175,43 @@ class CollectionService implements Service {
case '$populate': {
const fields = Array.isArray(value) ? value : [value]
const schema = await this.collection.schema

filter.$populate = fields.map((field) => {
const err = new BadRequest(`Cannot populate field \`${field}\``)

if (!schema) {
return field
throw err
}

const definition = schema.schema[field] as Extract<
Definition,
{ type: 'id' | 'array' }
>

if (!definition || !['id', 'array'].includes(definition.type)) {
throw err
}

const relation = (
schema.schema[field] as Extract<Definition, { type: 'id' }>
).relation
if (definition.type === 'id') {
if (!definition.relation) {
throw err
}

return { collection: definition.relation, field }
}

if (!relation) {
return field
if (
// we can't populate tuples
Array.isArray(definition.items) ||
definition.items.type !== 'id' ||
!definition.items.relation
) {
throw err
}

return { collection: relation, field }
return { collection: definition.items.relation, field }
})

break
}

Expand Down
2 changes: 1 addition & 1 deletion base/src/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type FilterOperators = '$limit' | '$populate' | '$select' | '$skip' | '$sort'

interface Filter {
$limit?: number
$populate?: string[]
$populate?: (string | { collection: string; field: string })[]
$select?: string[]
$skip?: number
$sort?: Record<string, -1 | 1>
Expand Down
2 changes: 1 addition & 1 deletion base/src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ function context(ctx: Partial<Context>): Context {
method: 'find',
path: '',
query: {},
url: ' ',
url: '/',
...ctx,
}
}
Expand Down
1 change: 1 addition & 0 deletions mongo-db/src/mongodb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ class MongoCursor implements Cursor {
return value
})
.filter(Boolean)
.flat()

if (!ids.length) continue

Expand Down

0 comments on commit dfc137d

Please sign in to comment.