Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
D
Drip
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Issue analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
backup
Drip
Commits
b07a2c8a
Commit
b07a2c8a
authored
6 years ago
by
Julia Friesel
Browse files
Options
Downloads
Patches
Plain Diff
Move db operations back to db module
parent
53328f60
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
db/index.js
+34
-2
34 additions, 2 deletions
db/index.js
lib/import-export/get-csv-column-names.js
+4
-4
4 additions, 4 deletions
lib/import-export/get-csv-column-names.js
lib/import-export/import-from-csv.js
+5
-27
5 additions, 27 deletions
lib/import-export/import-from-csv.js
with
43 additions
and
33 deletions
db/index.js
+
34
−
2
View file @
b07a2c8a
...
@@ -176,9 +176,38 @@ function getPreviousTemperature(cycleDay) {
...
@@ -176,9 +176,38 @@ function getPreviousTemperature(cycleDay) {
return
winner
.
temperature
.
value
return
winner
.
temperature
.
value
}
}
const
schema
=
db
.
schema
.
reduce
((
acc
,
curr
)
=>
{
acc
[
curr
.
name
]
=
curr
.
properties
return
acc
},
{})
function
tryToCreateCycleDay
(
day
,
i
)
{
try
{
db
.
create
(
'
CycleDay
'
,
day
)
}
catch
(
err
)
{
const
msg
=
`Line
${
i
+
1
}
(
${
day
.
date
}
):
${
err
.
message
}
`
throw
new
Error
(
msg
)
}
}
function
tryToImportWithDelete
(
cycleDays
)
{
db
.
write
(()
=>
{
db
.
delete
(
db
.
objects
(
'
CycleDay
'
))
cycleDays
.
forEach
(
tryToCreateCycleDay
)
})
}
function
tryToImportWithoutDelete
(
cycleDays
)
{
db
.
write
(()
=>
{
cycleDays
.
forEach
((
day
,
i
)
=>
{
const
existing
=
getCycleDay
(
day
.
date
)
if
(
existing
)
db
.
delete
(
existing
)
tryToCreateCycleDay
(
day
,
i
)
})
})
}
export
{
export
{
db
,
saveSymptom
,
saveSymptom
,
getOrCreateCycleDay
,
getOrCreateCycleDay
,
bleedingDaysSortedByDate
,
bleedingDaysSortedByDate
,
...
@@ -187,5 +216,8 @@ export {
...
@@ -187,5 +216,8 @@ export {
fillWithDummyData
,
fillWithDummyData
,
deleteAll
,
deleteAll
,
getPreviousTemperature
,
getPreviousTemperature
,
getCycleDay
getCycleDay
,
schema
,
tryToImportWithDelete
,
tryToImportWithoutDelete
}
}
This diff is collapsed.
Click to expand it.
lib/import-export/get-csv-column-names.js
+
4
−
4
View file @
b07a2c8a
import
{
db
}
from
'
../../db
'
import
{
schema
}
from
'
../../db
'
export
default
function
getColumnNamesForCsv
()
{
export
default
function
getColumnNamesForCsv
()
{
return
getPrefixedKeys
(
'
CycleDay
'
)
return
getPrefixedKeys
(
'
CycleDay
'
)
function
getPrefixedKeys
(
schemaName
,
prefix
)
{
function
getPrefixedKeys
(
schemaName
,
prefix
)
{
const
schema
=
db
.
schema
.
find
(
x
=>
x
.
name
===
schemaName
).
properties
const
model
=
schema
[
schemaName
]
return
Object
.
keys
(
schema
).
reduce
((
acc
,
key
)
=>
{
return
Object
.
keys
(
model
).
reduce
((
acc
,
key
)
=>
{
const
prefixedKey
=
prefix
?
[
prefix
,
key
].
join
(
'
.
'
)
:
key
const
prefixedKey
=
prefix
?
[
prefix
,
key
].
join
(
'
.
'
)
:
key
const
childSchemaName
=
schema
[
key
].
objectType
const
childSchemaName
=
model
[
key
].
objectType
if
(
!
childSchemaName
)
{
if
(
!
childSchemaName
)
{
acc
.
push
(
prefixedKey
)
acc
.
push
(
prefixedKey
)
return
acc
return
acc
...
...
This diff is collapsed.
Click to expand it.
lib/import-export/import-from-csv.js
+
5
−
27
View file @
b07a2c8a
import
csvParser
from
'
csvtojson
'
import
csvParser
from
'
csvtojson
'
import
isObject
from
'
isobject
'
import
isObject
from
'
isobject
'
import
{
db
,
getCycleDay
}
from
'
../../db
'
import
{
schema
,
tryToImportWithDelete
,
tryToImportWithoutDelete
}
from
'
../../db
'
import
getColumnNamesForCsv
from
'
./get-csv-column-names
'
import
getColumnNamesForCsv
from
'
./get-csv-column-names
'
export
default
async
function
importCsv
(
csv
,
deleteFirst
)
{
export
default
async
function
importCsv
(
csv
,
deleteFirst
)
{
...
@@ -23,12 +23,11 @@ export default async function importCsv(csv, deleteFirst) {
...
@@ -23,12 +23,11 @@ export default async function importCsv(csv, deleteFirst) {
return
Number
(
val
)
return
Number
(
val
)
}
}
const
cycleDayDbSchema
=
db
.
schema
.
find
(
x
=>
x
.
name
===
'
CycleDay
'
).
properties
const
config
=
{
const
config
=
{
ignoreEmpty
:
true
,
ignoreEmpty
:
true
,
colParser
:
getColumnNamesForCsv
().
reduce
((
acc
,
colName
)
=>
{
colParser
:
getColumnNamesForCsv
().
reduce
((
acc
,
colName
)
=>
{
const
path
=
colName
.
split
(
'
.
'
)
const
path
=
colName
.
split
(
'
.
'
)
const
dbType
=
getDbType
(
cycleDayDbSchema
,
path
)
const
dbType
=
getDbType
(
schema
.
CycleDay
,
path
)
acc
[
colName
]
=
item
=>
{
acc
[
colName
]
=
item
=>
{
if
(
item
===
''
)
return
null
if
(
item
===
''
)
return
null
return
parseFuncs
[
dbType
](
item
)
return
parseFuncs
[
dbType
](
item
)
...
@@ -45,29 +44,9 @@ export default async function importCsv(csv, deleteFirst) {
...
@@ -45,29 +44,9 @@ export default async function importCsv(csv, deleteFirst) {
putNullForEmptySymptoms
(
cycleDays
)
putNullForEmptySymptoms
(
cycleDays
)
if
(
deleteFirst
)
{
if
(
deleteFirst
)
{
db
.
write
(()
=>
{
tryToImportWithDelete
(
cycleDays
)
db
.
delete
(
db
.
objects
(
'
CycleDay
'
))
cycleDays
.
forEach
(
tryToCreateCycleDay
)
})
}
else
{
}
else
{
db
.
write
(()
=>
{
tryToImportWithoutDelete
(
cycleDays
)
cycleDays
.
forEach
((
day
,
i
)
=>
{
const
existing
=
getCycleDay
(
day
.
date
)
if
(
existing
)
{
db
.
delete
(
existing
)
}
tryToCreateCycleDay
(
day
,
i
)
})
})
}
}
function
tryToCreateCycleDay
(
day
,
i
)
{
try
{
db
.
create
(
'
CycleDay
'
,
day
)
}
catch
(
err
)
{
const
msg
=
`Line
${
i
+
1
}
(
${
day
.
date
}
):
${
err
.
message
}
`
throw
new
Error
(
msg
)
}
}
}
}
...
@@ -99,6 +78,5 @@ function putNullForEmptySymptoms(data) {
...
@@ -99,6 +78,5 @@ function putNullForEmptySymptoms(data) {
function
getDbType
(
modelProperties
,
path
)
{
function
getDbType
(
modelProperties
,
path
)
{
if
(
path
.
length
===
1
)
return
modelProperties
[
path
[
0
]].
type
if
(
path
.
length
===
1
)
return
modelProperties
[
path
[
0
]].
type
const
modelName
=
modelProperties
[
path
[
0
]].
objectType
const
modelName
=
modelProperties
[
path
[
0
]].
objectType
const
model
=
db
.
schema
.
find
(
x
=>
x
.
name
===
modelName
)
return
getDbType
(
schema
[
modelName
],
path
.
slice
(
1
))
return
getDbType
(
model
.
properties
,
path
.
slice
(
1
))
}
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment