Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
storage-mongo-node
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Registry
Registry
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
microservices
storage-mongo-node
Commits
2dbdc34f
Commit
2dbdc34f
authored
Apr 13, 2018
by
Vadym Gidulian
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added inserting of multiple records
parent
20d3b8e2
Changes
3
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
378 additions
and
544 deletions
+378
-544
package.json
package.json
+3
-3
index.js
src/index.js
+44
-7
yarn.lock
yarn.lock
+331
-534
No files found.
package.json
View file @
2dbdc34f
...
@@ -3,10 +3,10 @@
...
@@ -3,10 +3,10 @@
"version"
:
"1.0.2"
,
"version"
:
"1.0.2"
,
"dependencies"
:
{
"dependencies"
:
{
"
express
"
:
"
^4.16.
2
"
,
"
express
"
:
"
^4.16.
3
"
,
"
mongodb
"
:
"
^3.0.
1
"
"
mongodb
"
:
"
^3.0.
7
"
},
},
"devDependencies"
:
{
"devDependencies"
:
{
"
nodemon
"
:
"
^1.1
4.8
"
"
nodemon
"
:
"
^1.1
7.4
"
}
}
}
}
src/index.js
View file @
2dbdc34f
...
@@ -44,6 +44,34 @@ app.post('/:collection', (req, res) => {
...
@@ -44,6 +44,34 @@ app.post('/:collection', (req, res) => {
});
});
});
});
});
});
app
.
put
(
'/:collection'
,
(
req
,
res
)
=>
{
if
(
isObjectEmpty
(
req
.
body
))
{
res
.
status
(
400
).
send
(
'Body is empty'
);
return
;
}
req
.
body
.
forEach
(
record
=>
{
if
(
record
.
_id
)
record
.
_id
=
toId
(
record
.
_id
);
});
mongoSafeConnect
(
res
,
client
=>
{
const
collection
=
client
.
db
(
DB_NAME
).
collection
(
req
.
params
.
collection
);
collection
.
insertMany
(
req
.
body
,
{
ordered
:
false
},
(
err
,
result
)
=>
{
if
(
err
)
{
if
(
err
.
code
==
11000
)
{
return
res
.
status
(
409
).
send
();
}
else
{
throw
err
;
}
}
const
records
=
result
.
ops
;
res
.
status
(
201
).
send
(
records
);
client
.
close
();
});
});
});
app
.
put
(
'/:collection/:id'
,
(
req
,
res
)
=>
{
app
.
put
(
'/:collection/:id'
,
(
req
,
res
)
=>
{
if
(
isObjectEmpty
(
req
.
body
))
{
if
(
isObjectEmpty
(
req
.
body
))
{
res
.
status
(
400
).
send
(
'Body is empty'
);
res
.
status
(
400
).
send
(
'Body is empty'
);
...
@@ -52,7 +80,7 @@ app.put('/:collection/:id', (req, res) => {
...
@@ -52,7 +80,7 @@ app.put('/:collection/:id', (req, res) => {
mongoSafeConnect
(
res
,
client
=>
{
mongoSafeConnect
(
res
,
client
=>
{
const
collection
=
client
.
db
(
DB_NAME
).
collection
(
req
.
params
.
collection
);
const
collection
=
client
.
db
(
DB_NAME
).
collection
(
req
.
params
.
collection
);
collection
.
findOne
({
_id
:
wrap
Id
(
req
.
params
.
id
)},
(
err
,
result
)
=>
{
collection
.
findOne
({
_id
:
to
Id
(
req
.
params
.
id
)},
(
err
,
result
)
=>
{
if
(
err
)
throw
err
;
if
(
err
)
throw
err
;
if
(
result
)
{
if
(
result
)
{
...
@@ -61,7 +89,7 @@ app.put('/:collection/:id', (req, res) => {
...
@@ -61,7 +89,7 @@ app.put('/:collection/:id', (req, res) => {
return
;
return
;
}
}
collection
.
insertOne
({...
req
.
body
,
_id
:
wrap
Id
(
req
.
params
.
id
)},
(
err
,
result
)
=>
{
collection
.
insertOne
({...
req
.
body
,
_id
:
to
Id
(
req
.
params
.
id
)},
(
err
,
result
)
=>
{
if
(
err
)
throw
err
;
if
(
err
)
throw
err
;
const
record
=
result
.
ops
[
0
];
const
record
=
result
.
ops
[
0
];
...
@@ -85,7 +113,7 @@ app.get('/:collection', (req, res) => {
...
@@ -85,7 +113,7 @@ app.get('/:collection', (req, res) => {
});
});
app
.
get
(
'/:collection/:id'
,
(
req
,
res
)
=>
{
app
.
get
(
'/:collection/:id'
,
(
req
,
res
)
=>
{
mongoSafeConnect
(
res
,
client
=>
{
mongoSafeConnect
(
res
,
client
=>
{
client
.
db
(
DB_NAME
).
collection
(
req
.
params
.
collection
).
findOne
({
_id
:
wrap
Id
(
req
.
params
.
id
)},
(
err
,
result
)
=>
{
client
.
db
(
DB_NAME
).
collection
(
req
.
params
.
collection
).
findOne
({
_id
:
to
Id
(
req
.
params
.
id
)},
(
err
,
result
)
=>
{
if
(
err
)
throw
err
;
if
(
err
)
throw
err
;
if
(
result
)
{
if
(
result
)
{
...
@@ -106,7 +134,7 @@ app.patch('/:collection/:id', (req, res) => {
...
@@ -106,7 +134,7 @@ app.patch('/:collection/:id', (req, res) => {
mongoSafeConnect
(
res
,
client
=>
{
mongoSafeConnect
(
res
,
client
=>
{
delete
req
.
body
.
_id
;
// Immutable properties shouldn't be tried to change
delete
req
.
body
.
_id
;
// Immutable properties shouldn't be tried to change
client
.
db
(
DB_NAME
).
collection
(
req
.
params
.
collection
).
findOneAndUpdate
({
_id
:
wrap
Id
(
req
.
params
.
id
)},
{
$set
:
req
.
body
},
{
returnOriginal
:
false
},
(
err
,
result
)
=>
{
client
.
db
(
DB_NAME
).
collection
(
req
.
params
.
collection
).
findOneAndUpdate
({
_id
:
to
Id
(
req
.
params
.
id
)},
{
$set
:
req
.
body
},
{
returnOriginal
:
false
},
(
err
,
result
)
=>
{
if
(
err
)
throw
err
;
if
(
err
)
throw
err
;
if
(
result
.
value
)
{
if
(
result
.
value
)
{
...
@@ -121,7 +149,7 @@ app.patch('/:collection/:id', (req, res) => {
...
@@ -121,7 +149,7 @@ app.patch('/:collection/:id', (req, res) => {
});
});
app
.
delete
(
'/:collection/:id'
,
(
req
,
res
)
=>
{
app
.
delete
(
'/:collection/:id'
,
(
req
,
res
)
=>
{
mongoSafeConnect
(
res
,
client
=>
{
mongoSafeConnect
(
res
,
client
=>
{
client
.
db
(
DB_NAME
).
collection
(
req
.
params
.
collection
).
findOneAndDelete
({
_id
:
wrap
Id
(
req
.
params
.
id
)},
(
err
,
result
)
=>
{
client
.
db
(
DB_NAME
).
collection
(
req
.
params
.
collection
).
findOneAndDelete
({
_id
:
to
Id
(
req
.
params
.
id
)},
(
err
,
result
)
=>
{
if
(
err
)
throw
err
;
if
(
err
)
throw
err
;
if
(
result
.
value
)
{
if
(
result
.
value
)
{
...
@@ -161,6 +189,15 @@ function mongoSafeConnect(serverResponse, callback) {
...
@@ -161,6 +189,15 @@ function mongoSafeConnect(serverResponse, callback) {
});
});
}
}
function
wrapId
(
id
)
{
function
toId
(
id
)
{
return
ObjectID
.
isValid
(
id
)
?
new
ObjectID
(
id
)
:
id
;
if
(
+
id
==
id
)
id
=
+
id
;
switch
(
typeof
id
)
{
case
'number'
:
return
id
;
case
'object'
:
if
(
Array
.
isArray
(
id
))
throw
new
Error
(
'Array is not a valid object id type'
);
return
id
;
case
'string'
:
return
ObjectID
.
isValid
(
id
)
?
new
ObjectID
(
id
)
:
id
;
default
:
throw
new
Error
(
'Invalid object id'
);
}
}
}
yarn.lock
View file @
2dbdc34f
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment