Skip to content

Commit

Permalink
feat: add getMany method to member api (#263)
Browse files Browse the repository at this point in the history
  • Loading branch information
achou11 authored Sep 18, 2023
1 parent d035a83 commit 350ccc5
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 11 deletions.
10 changes: 9 additions & 1 deletion src/member-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export class MemberApi extends TypedEmitter {
* @param {Object} opts.queries
* @param {() => Promise<import('./generated/rpc.js').Invite_ProjectInfo>} opts.queries.getProjectInfo
* @param {Object} opts.dataTypes
* @param {Pick<DeviceInfoDataType, 'getByDocId'>} opts.dataTypes.deviceInfo
* @param {Pick<DeviceInfoDataType, 'getByDocId' | 'getMany'>} opts.dataTypes.deviceInfo
*/
constructor({
capabilities,
Expand Down Expand Up @@ -74,4 +74,12 @@ export class MemberApi extends TypedEmitter {
const { name } = await this.#dataTypes.deviceInfo.getByDocId(deviceId)
return { deviceId, name }
}

/**
* @returns {Promise<Array<MemberInfo>>}
*/
async getMany() {
const devices = await this.#dataTypes.deviceInfo.getMany()
return devices.map(({ docId, name }) => ({ deviceId: docId, name }))
}
}
78 changes: 68 additions & 10 deletions tests/member-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,16 +149,7 @@ test('getById() works', async (t) => {

const deviceId = randomBytes(32).toString('hex')

/** @type {import('@mapeo/schema').DeviceInfo} */
const deviceInfo = {
schemaName: 'deviceInfo',
docId: deviceId,
name: 'mapeo',
versionId: `${deviceId}/0`,
createdAt: new Date().toISOString(),
updatedAt: new Date().toISOString(),
links: [],
}
const deviceInfo = createDeviceInfoRecord({ deviceId, name: 'member' })

const deviceInfoRecords = [deviceInfo]

Expand Down Expand Up @@ -195,3 +186,70 @@ test('getById() works', async (t) => {
await memberApi.getById(randomId)
}, 'throws when no match')
})

test('getMany() works', async (t) => {
const projectKey = KeyManager.generateProjectKeypair().publicKey
const encryptionKeys = { auth: randomBytes(32) }

const rpc = new MapeoRPC()

const deviceInfoRecords = []

const memberApi = new MemberApi({
capabilities: { async assignRole() {} },
encryptionKeys,
projectKey,
rpc,
queries: { getProjectInfo: async () => {} },
dataTypes: {
deviceInfo: {
async getMany() {
return deviceInfoRecords
},
},
},
})

const initialMembers = await memberApi.getMany()

t.is(initialMembers.length, 0, 'no initial members')

deviceInfoRecords.push(
createDeviceInfoRecord({ name: 'member1' }),
createDeviceInfoRecord({ name: 'member2' }),
createDeviceInfoRecord({ name: 'member3' })
)

const members = await memberApi.getMany()

t.is(members.length, 3)

for (const member of members) {
const { deviceId, name } = member

const deviceInfo = deviceInfoRecords.find(({ docId }) => docId === deviceId)

t.ok(deviceInfo)
t.is(name, deviceInfo.name)
}
})

/**
* @param {Object} opts
* @param {string} [opts.deviceId]
* @param {string} opts.name
* @returns {import('@mapeo/schema').DeviceInfo}
*/
function createDeviceInfoRecord({ deviceId, name }) {
const docId = deviceId || randomBytes(32).toString('hex')

return {
schemaName: 'deviceInfo',
docId,
name,
versionId: `${docId}/0`,
createdAt: new Date().toISOString(),
updatedAt: new Date().toISOString(),
links: [],
}
}

0 comments on commit 350ccc5

Please sign in to comment.