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

Feat: Support for bulk insertion #931

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion __tests__/server/plural-with-custom-foreign-key.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ describe('Server with custom foreign key', () => {
.post('/posts/1/comments')
.send({ body: 'foo' })
.expect('Content-Type', /json/)
.expect({ id: 4, post_id: '1', body: 'foo' })
.expect({ id: 4, post_id: 1, body: 'foo' })
.expect(201))
})

Expand Down
87 changes: 86 additions & 1 deletion __tests__/server/plural.js
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,13 @@ describe('Server', () => {
.expect('Content-Type', /json/)
.expect(db.comments.slice(1))
.expect(200))

test.skip('should accept multiple parameters', () =>
request(server)
.get('/comments?id_ne=1&id_ne=2')
.expect('Content-Type', /json/)
.expect(db.comments.slice(1))
.expect(200))
})

describe('GET /:resource?attr_like=', () => {
Expand Down Expand Up @@ -526,6 +533,32 @@ describe('Server', () => {
assert.strictEqual(db.posts.length, 3)
})

test('should support bulk insertion', async () => {
await request(server)
.post('/posts')
.send([
{
body: 'foo bar'
},
{
body: 'foo baz'
}
])
.expect('Content-Type', /json/)
.expect([
{
id: 3,
body: 'foo bar'
},
{
id: 4,
body: 'foo baz'
}
])
.expect(201)
assert.strictEqual(db.posts.length, 4)
})

test('should support x-www-form-urlencoded', async () => {
await request(server)
.post('/posts')
Expand Down Expand Up @@ -554,7 +587,59 @@ describe('Server', () => {
.post('/posts/1/comments')
.send({ body: 'foo' })
.expect('Content-Type', /json/)
.expect({ id: 6, postId: '1', body: 'foo' })
.expect({ id: 6, postId: 1, body: 'foo' })
.expect(201))

test('should support bulk insertion', () =>
request(server)
.post('/posts/1/comments')
.send([
{
body: 'foo'
},
{
body: 'bar'
}
])
.expect('Content-Type', /json/)
.expect([
{
id: 6,
postId: 1,
body: 'foo'
},
{
id: 7,
postId: 1,
body: 'bar'
}
])
.expect(201))

test('should support bulk insertion with non numeric id', () =>
request(server)
.post('/refs/abcd-1234/posts')
.send([
{
body: 'foo'
},
{
body: 'bar'
}
])
.expect('Content-Type', /json/)
.expect([
{
id: 3,
refId: 'abcd-1234',
body: 'foo'
},
{
id: 4,
refId: 'abcd-1234',
body: 'bar'
}
])
.expect(201))
})

Expand Down
14 changes: 12 additions & 2 deletions src/server/router/nested.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const _ = require('lodash')
const express = require('express')
const pluralize = require('pluralize')
const delay = require('./delay')
const utils = require('../utils')

module.exports = opts => {
const router = express.Router()
Expand All @@ -16,8 +18,16 @@ module.exports = opts => {

// Rewrite URL (/:resource/:id/:nested -> /:nested) and request body
function post(req, res, next) {
const prop = pluralize.singular(req.params.resource)
req.body[`${prop}${opts.foreignKeySuffix}`] = req.params.id
const id = utils.parseID(req.params.id)
const prop = pluralize.singular(req.params.resource) + opts.foreignKeySuffix
if (_.isArray(req.body)) {
req.body = req.body.map(r => ({
...r,
[prop]: id
}))
} else {
req.body[prop] = id
}
req.url = `/${req.params.nested}`
next()
}
Expand Down
48 changes: 34 additions & 14 deletions src/server/router/plural.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ module.exports = (db, name, opts) => {
delete req.query[query]
})

// Full-text search
if (q) {
// Full-text search
if (Array.isArray(q)) {
q = q[0]
}
Expand Down Expand Up @@ -256,21 +256,41 @@ module.exports = (db, name, opts) => {
// POST /name
function create(req, res, next) {
let resource
if (opts._isFake) {
const id = db
.get(name)
.createId()
.value()
resource = { ...req.body, id }
const body = req.body

if (_.isArray(body)) {
resource = body.map(r => {
if (opts._isFake) {
return {
...r,
id: db
.get(name)
.createId()
.value()
}
}
return db
.get(name)
.insert(r)
.value()
})
} else {
resource = db
.get(name)
.insert(req.body)
.value()
}
if (opts._isFake) {
const id = db
.get(name)
.createId()
.value()
resource = { ...body, id }
} else {
resource = db
.get(name)
.insert(body)
.value()
}

res.setHeader('Access-Control-Expose-Headers', 'Location')
res.location(`${getFullURL(req)}/${resource.id}`)
res.setHeader('Access-Control-Expose-Headers', 'Location')
res.location(`${getFullURL(req)}/${resource.id}`)
}

res.status(201)
res.locals.data = resource
Expand Down
7 changes: 6 additions & 1 deletion src/server/utils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
module.exports = {
getPage
getPage,
parseID
}

function parseID(id) {
return /^\d+$/.test(id) ? +id : id
}

function getPage(array, page, perPage) {
Expand Down