Skip to content
Snippets Groups Projects
Commit 7fcf0885 authored by Jure's avatar Jure
Browse files

feat(db-manager): remove addCollection and addFragment functions

BREAKING CHANGE: If you were relying on db-manager to add collections and fragments, this will no
longer work. Since Collections and Fragments are now merely model components (previously part of
core), on equal footing with the rest of the model components, the recommended migration path is to
use your own seed script, e.g.
https://gitlab.coko.foundation/pubsweet/pubsweet-starter/blob/master/scripts/seed.js to perform
database setup/seed.
parent 4204318b
No related branches found
No related tags found
No related merge requests found
const logger = require('@pubsweet/logger')
module.exports = async (collectionData, fragment = null) => {
const { User, Collection } = require('@pubsweet/models')
logger.info('Creating collection')
const collection = await new Collection(collectionData).save()
const [user] = await User.all()
if (user) collection.setOwners([user.id])
await collection.save()
if (fragment) collection.addFragment(fragment)
await collection.save()
logger.info(
`Successfully created collection ${
user ? `and set ${user.id} as owner` : 'with no owner'
}`,
)
return collection
}
const logger = require('@pubsweet/logger')
module.exports = async fragmentData => {
const { Fragment, User } = require('@pubsweet/models')
logger.info('Creating fragment')
const fragment = await new Fragment(fragmentData).save()
const [user] = await User.all()
if (user) fragment.setOwners([user.id])
await fragment.save()
logger.info(
`Successfully created fragment ${
user ? `and set ${user.id} as owner` : 'with no owner'
}`,
)
return fragment
}
......@@ -3,6 +3,4 @@ module.exports.createTables = require('./commands/create-tables')
module.exports.setupDb = require('./commands/setup-db')
module.exports.migrate = require('./commands/migrate')
module.exports.addUser = require('./commands/add-user')
module.exports.addCollection = require('./commands/add-collection')
module.exports.addFragment = require('./commands/add-fragment')
module.exports.dbExists = require('./helpers/db-exists')
const { addCollection, createTables } = require('../../src')
const { Collection, Fragment, User } = require('@pubsweet/models')
describe('add-collection', () => {
beforeEach(() => createTables(true))
it('adds a collection to the database', async () => {
await addCollection({})
const [actualCollection] = await Collection.all()
expect(actualCollection).toMatchObject({
type: 'collection',
owners: [],
})
})
it('adds user as owner', async () => {
const user = await new User({
username: 'user',
email: 'test@example.com',
password: 'password',
}).save()
await addCollection({})
const [actualCollection] = await Collection.all()
expect(actualCollection).toMatchObject({
type: 'collection',
owners: [user.id],
})
})
it('adds fragment to collection', async () => {
const fragment = await new Fragment({
fragmentType: 'version',
}).save()
await addCollection({}, fragment)
const [actualCollection] = await Collection.all()
expect(actualCollection).toMatchObject({
type: 'collection',
owners: [],
fragments: [fragment.id],
})
})
})
const { addFragment, createTables } = require('../../src')
const { Fragment, User } = require('@pubsweet/models')
describe('add-fragment', () => {
beforeEach(() => createTables(true))
it('adds a fragment to the database', async () => {
await addFragment({ fragmentType: 'version' })
const [actualFragment] = await Fragment.all()
expect(actualFragment).toMatchObject({
type: 'fragment',
fragmentType: 'version',
owners: [],
})
})
it('adds user as owner', async () => {
const user = await new User({
username: 'user',
email: 'test@example.com',
password: 'password',
}).save()
await addFragment({ fragmentType: 'version' })
const [actualFragment] = await Fragment.all()
expect(actualFragment).toMatchObject({
type: 'fragment',
fragmentType: 'version',
owners: [user.id],
})
})
})
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment