Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Server
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
web-et-numerique
Factory
Resin
Server
Commits
cb75392e
Commit
cb75392e
authored
2 years ago
by
Etienne LOUPIAS
Browse files
Options
Downloads
Patches
Plain Diff
feat(init-script): refacto to exit after structures creation
parent
989b4cd3
No related branches found
No related tags found
4 merge requests
!247
V2.1.0
,
!242
V2.0
,
!210
feat(init-script): exit after structures creation
,
!127
V2.0
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
scripts/data/users.js
+1
-3
1 addition, 3 deletions
scripts/data/users.js
scripts/init-db.js
+70
-66
70 additions, 66 deletions
scripts/init-db.js
with
71 additions
and
69 deletions
scripts/data/users.js
+
1
−
3
View file @
cb75392e
...
...
@@ -17,13 +17,11 @@ module.exports = {
emailVerified
:
true
,
email
:
'
admin@admin.com
'
,
structureOutdatedMailSent
:
[],
employer
:
mongoose
.
Types
.
ObjectId
(
'
627b6ca899862168705ca836
'
),
job
:
mongoose
.
Types
.
ObjectId
(
'
627b6ca899862168705ca846
'
),
},
{
structureOutdatedMailSent
:
[],
pendingStructuresLink
:
[],
structuresLink
:
[
mongoose
.
Types
.
ObjectId
(
'
6001a39c16b08100062e4164
'
)
],
structuresLink
:
[],
newEmail
:
null
,
changeEmailToken
:
null
,
role
:
0
,
...
...
This diff is collapsed.
Click to expand it.
scripts/init-db.js
+
70
−
66
View file @
cb75392e
...
...
@@ -36,14 +36,6 @@ function hashPassword() {
return
bcrypt
.
hashSync
(
process
.
env
.
USER_PWD
,
process
.
env
.
SALT
);
}
const
handleError
=
async
(
name
,
err
)
=>
{
/* show messages */
if
(
err
)
{
if
(
err
.
code
===
26
)
console
.
error
(
`--
${
name
}
collection does not exists --`
);
else
throw
err
;
}
else
console
.
log
(
`--
${
name
}
collection dropped --`
);
};
// define Schema
const
usersSchema
=
mongoose
.
Schema
({
name
:
String
,
...
...
@@ -89,6 +81,7 @@ const structuresTypeSchema = mongoose.Schema(
},
{
collection
:
'
structuretype
'
}
);
const
categoriesOthersSchema
=
mongoose
.
Schema
({
name
:
String
,
id
:
String
,
...
...
@@ -151,16 +144,35 @@ const structuresSchema = mongoose.Schema({
// compile schema to model
const
user
=
mongoose
.
model
(
'
users
'
,
usersSchema
);
const
structuresType
=
mongoose
.
model
(
'
structureType
'
,
structuresTypeSchema
);
const
categoriesOthers
=
mongoose
.
model
(
'
categoriesOthers
'
,
categoriesOthersSchema
);
const
categoriesAccompanements
=
mongoose
.
model
(
'
CategoriesAccompagnement
'
,
categoriesAccompanementsSchema
);
const
categoriesFormation
=
mongoose
.
model
(
'
categoriesFormation
'
,
categoriesFormationSchema
);
//
const categoriesOthers = mongoose.model('categoriesOthers', categoriesOthersSchema);
//
const categoriesAccompanements = mongoose.model('CategoriesAccompagnement', categoriesAccompanementsSchema);
//
const categoriesFormation = mongoose.model('categoriesFormation', categoriesFormationSchema);
const
structures
=
mongoose
.
model
(
'
structures
'
,
structuresSchema
);
const
jobs
=
mongoose
.
model
(
'
jobs
'
,
jobsSchema
);
const
employers
=
mongoose
.
model
(
'
employers
'
,
employersSchema
);
/* drop users collections */
mongoose
.
connection
.
dropCollection
(
'
users
'
,
async
(
err
)
=>
{
await
handleError
(
'
Users
'
,
err
);
async
function
deleteData
()
{
/* drop users collections */
let
usersPromise
=
mongoose
.
connection
.
dropCollection
(
'
users
'
);
/* Create structures ref */
let
structureTypePromise
=
mongoose
.
connection
.
dropCollection
(
'
structuretype
'
);
/* Create structures */
let
structuresPromise
=
mongoose
.
connection
.
dropCollection
(
'
structures
'
);
/* Create jobs */
let
jobPromise
=
mongoose
.
connection
.
dropCollection
(
'
jobs
'
);
/* Create employers */
let
employersPromise
=
mongoose
.
connection
.
dropCollection
(
'
employers
'
);
await
Promise
.
all
([
employersPromise
,
structureTypePromise
,
jobPromise
,
structuresPromise
,
usersPromise
]);
return
true
;
}
async
function
createData
()
{
let
employersPromise
=
employers
.
create
(
employersData
.
data
);
let
structureTypePromise
=
structuresType
.
create
(
structuresTypeData
.
data
);
let
structuresPromise
=
structures
.
create
(
structuresData
.
data
);
let
jobPromise
=
jobs
.
create
(
jobsData
.
data
);
// Init passsword
console
.
log
(
'
-- Users password encryption based on .env --
'
);
...
...
@@ -168,60 +180,52 @@ mongoose.connection.dropCollection('users', async (err) => {
user
.
password
=
hashPassword
();
});
// save model to database
user
.
create
(
userData
.
data
,
(
error
)
=>
{
if
(
error
)
return
console
.
error
(
error
);
console
.
log
(
'
-- Users collection initialized --
'
);
});
});
let
userPromise
=
user
.
create
(
userData
.
data
);
await
Promise
.
all
([
employersPromise
,
structureTypePromise
,
jobPromise
,
structuresPromise
,
userPromise
]);
return
true
;
}
/
* Create structures ref */
mongoose
.
connection
.
dropCollection
(
'
structuretype
'
,
async
(
err
)
=>
{
await
handleError
(
'
structureType
'
,
err
);
structuresType
.
create
(
structuresType
Data
.
data
,
(
error
)
=>
{
if
(
error
)
return
console
.
error
(
error
);
});
});
/
/ This will part will be updated after repository update US222
//
mongoose.connection.dropCollection('
categoriesothers
', async (err) => {
//
await handleError('
categoriesOthers
', err);
// categoriesOthers.create(categoriesOthers
Data.data, (error) => {
//
if (error) return console.error(error);
//
});
//
});
mongoose
.
connection
.
dropCollection
(
'
categories
other
s
'
,
async
(
err
)
=>
{
await
handleError
(
'
categories
Other
s
'
,
err
);
categories
Other
s
.
create
(
categories
Other
sData
.
data
,
(
error
)
=>
{
if
(
error
)
return
console
.
error
(
error
);
});
});
//
mongoose.connection.dropCollection('categories
accompagnement
s', async (err) => {
//
await handleError('categories
Accompanement
s', err);
//
categories
Accompanement
s.create(categories
Accompanement
sData.data, (error) => {
//
if (error) return console.error(error);
//
});
//
});
mongoose
.
connection
.
dropCollection
(
'
categories
accompagnement
s
'
,
async
(
err
)
=>
{
await
handleError
(
'
categories
Accompanement
s
'
,
err
);
categories
Accompanements
.
create
(
categories
Accompanements
Data
.
data
,
(
error
)
=>
{
if
(
error
)
return
console
.
error
(
error
);
});
});
//
mongoose.connection.dropCollection('categories
formation
s', async (err) => {
//
await handleError('categories
Formation
s', err);
//
categories
Formation
.create(categories
Formation
Data.data, (error) => {
//
if (error) return console.error(error);
//
});
//
});
mongoose
.
connection
.
dropCollection
(
'
categoriesformations
'
,
async
(
err
)
=>
{
await
handleError
(
'
categoriesFormations
'
,
err
);
categoriesFormation
.
create
(
categoriesFormationData
.
data
,
(
error
)
=>
{
if
(
error
)
return
console
.
error
(
error
);
});
});
function
main
()
{
deleteData
()
.
then
(()
=>
{
console
.
log
(
'
-- Deleting data done --
'
);
})
.
catch
((
err
)
=>
{
if
(
err
.
code
===
26
)
console
.
warn
(
`-- collection(s) does not exists --`
);
else
console
.
error
(
err
);
})
.
then
(()
=>
{
createData
()
.
then
(()
=>
{
console
.
log
(
'
-- Init db done--
'
);
process
.
exit
(
0
);
})
.
catch
((
err
)
=>
{
console
.
error
(
err
);
});
});
}
/* Create structures */
mongoose
.
connection
.
dropCollection
(
'
structures
'
,
async
(
err
)
=>
{
await
handleError
(
'
structures
'
,
err
);
structures
.
create
(
structuresData
.
data
,
(
error
)
=>
{
if
(
error
)
return
console
.
error
(
error
);
});
});
/* Create structures */
mongoose
.
connection
.
dropCollection
(
'
jobs
'
,
async
(
err
)
=>
{
await
handleError
(
'
jobs
'
,
err
);
jobs
.
create
(
jobsData
.
data
,
(
error
)
=>
{
if
(
error
)
return
console
.
error
(
error
);
});
});
/* Create structures */
mongoose
.
connection
.
dropCollection
(
'
employers
'
,
async
(
err
)
=>
{
await
handleError
(
'
employers
'
,
err
);
employers
.
create
(
employersData
.
data
,
(
error
)
=>
{
if
(
error
)
return
console
.
error
(
error
);
process
.
exit
(
0
);
});
});
main
();
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