Skip to content

Commit

Permalink
feat: remove referenced files
Browse files Browse the repository at this point in the history
  • Loading branch information
enguerran committed May 3, 2017
1 parent 3bcf704 commit 0a57bfe
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 18 deletions.
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const dataProto = {
defineIndex: mango.defineIndex,
query: mango.query,
addReferencedFiles: relations.addReferencedFiles,
removeReferencedFiles: relations.removeReferencedFiles,
listReferencedFiles: relations.listReferencedFiles,
destroy: function (...args) {
warn('destroy is deprecated, use cozy.data.delete instead.')
Expand Down
15 changes: 10 additions & 5 deletions src/relations.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
import {cozyFetchJSON} from './fetch'
import { DOCTYPE_FILES } from './doctypes'

export function addReferencedFiles (cozy, doc, ids) {
if (!doc) throw new Error('missing doc argument')
if (!Array.isArray(ids)) ids = [ids]
function updateRelations (verb) {
return function (cozy, doc, ids) {
if (!doc) throw new Error('missing doc argument')
if (!Array.isArray(ids)) ids = [ids]

const refs = ids.map((id) => ({type: DOCTYPE_FILES, id: id}))
const refs = ids.map((id) => ({type: DOCTYPE_FILES, id}))

return cozyFetchJSON(cozy, 'POST', makeReferencesPath(doc), {data: refs})
return cozyFetchJSON(cozy, verb, makeReferencesPath(doc), {data: refs})
}
}

export const addReferencedFiles = updateRelations('POST')
export const removeReferencedFiles = updateRelations('DELETE')

export function listReferencedFiles (cozy, doc) {
if (!doc) throw new Error('missing doc argument')
return cozyFetchJSON(cozy, 'GET', makeReferencesPath(doc))
Expand Down
38 changes: 25 additions & 13 deletions test/integration/relations.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,20 @@ const COZY_STACK_URL = process.env && process.env.COZY_STACK_URL || ''
const COZY_STACK_VERSION = process.env && process.env.COZY_STACK_VERSION
const COZY_STACK_TOKEN = process.env && process.env.COZY_STACK_TOKEN

async function createRandomFile (cozy, random) {
return cozy.client.files.create('datastring1', {
name: 'foo_' + random(),
contentType: 'application/json'
})
}

describe('references', async function () {
let random
const cozy = {}
let specialDoc
let ids = []

beforeEach(function () {
before(async function () {
if (COZY_STACK_VERSION === '2') {
this.skip()
}
Expand All @@ -23,25 +32,28 @@ describe('references', async function () {
token: COZY_STACK_TOKEN
})
random = randomGenerator()
})

it('bind files to a doc', async function () {
const ids = []
for (let i = 0; i < 3; i++) {
let file = await cozy.client.files.create('datastring1', {
name: 'foo_' + random(),
contentType: 'application/json'
})
let file = await createRandomFile(cozy, random)
ids.push(file._id)
}

const doc = await cozy.client.data.create('io.cozy.testreferencer', {
specialDoc = await cozy.client.data.create('io.cozy.testreferencer', {
name: 'foo_' + random()
})

await cozy.client.data.addReferencedFiles(doc, ids)
await cozy.client.data.addReferencedFiles(specialDoc, ids)
})

it('bind files to a doc', async function () {
const anotherDoc = await createRandomFile(cozy, random)
await cozy.client.data.addReferencedFiles(specialDoc, anotherDoc._id)
const ids2 = await cozy.client.data.listReferencedFiles(specialDoc)
ids2.should.eql(ids.concat(anotherDoc._id))
})

const ids2 = await cozy.client.data.listReferencedFiles(doc)
ids2.should.eql(ids)
it('remove references', async function () {
await cozy.client.data.removeReferencedFiles(specialDoc, ids[0])
const ids2 = await cozy.client.data.listReferencedFiles(specialDoc)
ids2.should.not.containEql(ids[0])
})
})

0 comments on commit 0a57bfe

Please sign in to comment.