create-sip 1.3.8 → 1.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,9 +2,9 @@ import Sequelize from 'sequelize'
2
2
  import dotenvFlow from 'dotenv-flow'
3
3
 
4
4
  if (process.env.NODE_ENV === 'test') {
5
- dotenv.config({ path: '.env.test' })
5
+ dotenvFlow.config({ path: '.env.test' })
6
6
  }else {
7
- dotenv.config()
7
+ dotenvFlow.config()
8
8
  }
9
9
 
10
10
  const sequelize = new Sequelize(
@@ -1,7 +1,7 @@
1
1
  import app from './app.js'
2
2
  import dotenvFlow from 'dotenv-flow'
3
3
 
4
- dotenv.config()
4
+ dotenvFlow.config()
5
5
 
6
6
  const PORT = process.env.APP_PORT || 8000
7
7
 
@@ -1,6 +1,6 @@
1
1
  import jwt from 'jsonwebtoken';
2
2
  import dotenvFlow from 'dotenv-flow';
3
- dotenv.config()
3
+ dotenvFlow.config()
4
4
 
5
5
  const verifyToken = (req, res, next) => {
6
6
  let authData = req.headers.authorization;
@@ -25,3 +25,7 @@ npm run dev
25
25
  ```
26
26
 
27
27
  Restarting the application when saving is done by nodemon.
28
+
29
+ ## Migartaions and seeders
30
+
31
+ The project uses umzug for migrations and seeders.
@@ -208,34 +208,60 @@ Run all migration:
208
208
 
209
209
  ```bash
210
210
  node op migration:run
211
+ node op migrate
211
212
  ```
212
213
 
213
214
  Run a migration:
214
215
 
215
216
  ```bash
216
217
  node op migration:run <migration_name>
218
+ node op migrate <migration_name>
217
219
  ```
218
220
 
219
221
  Rollback a migration:
220
222
 
221
223
  ```bash
222
224
  node op migration:rollback
225
+ node op migrate:rollback
223
226
  ```
224
227
 
225
228
  Rollback two migrations:
226
229
 
227
230
  ```bash
228
231
  node op migration:rollback 2
232
+ node op migrate:rollback 2
229
233
  ```
230
234
 
231
235
  Reset the database:
232
236
 
233
237
  ```bash
234
238
  node op migration:reset
239
+ node op migrate:reset
235
240
  ```
236
241
 
237
242
  Reset the database and run all migrations:
238
243
 
239
244
  ```bash
240
245
  node op migration:fresh
246
+ node op migrate:fresh
247
+ ```
248
+
249
+ ### Database seed
250
+
251
+ Generate a seeder:
252
+
253
+ ```bash
254
+ node op make/seeder thing
255
+ ```
256
+
257
+ Run all seeders:
258
+
259
+ ```bash
260
+ node op db:seed
261
+ ```
262
+
263
+ Run a seeder:
264
+
265
+ ```bash
266
+ node op db:seed path_thing
241
267
  ```
package/expressapi/op CHANGED
@@ -14,72 +14,79 @@ yargs(hideBin(process.argv))
14
14
  .usage('Használat: node op <command> [name]')
15
15
  .help()
16
16
  .demandCommand(2, 'Not enough arguments!')
17
- .command('make:model <name>',
18
- 'Generates a new Sequelize model',
19
- (yargs) => {
17
+ .command({
18
+ command: 'make:model <name>',
19
+ description: 'Generates a new Sequelize model',
20
+ builder: (yargs) => {
20
21
  return yargs
21
22
  .positional('name', {
22
23
  type: 'string',
23
24
  description: 'Name of the model'
24
25
  })
25
26
  },
26
- async (argv) => {
27
+ handler: async (argv) => {
27
28
  const { name } = argv;
28
29
  await copyModel(name);
29
30
  }
30
- )
31
- .command('make:controller <name>',
32
- 'Generates a new controller',
33
- (yargs) => {
31
+ })
32
+ .command({
33
+ command: 'make:controller <name>',
34
+ description: 'Generates a new controller',
35
+ builder: (yargs) => {
34
36
  return yargs
35
37
  .positional('name', {
36
38
  type: 'string',
37
39
  description: 'Name of the controller'
38
40
  })
39
41
  },
40
- async (argv) => {
42
+ handler: async (argv) => {
41
43
  const { name } = argv;
42
44
  await copyController(name);
43
45
  }
44
- )
45
- .command('key:generate',
46
- 'Generates a new API key',
47
- (yargs) => {
46
+ })
47
+ .command({
48
+ command: 'key:generate',
49
+ description:'Generates a new API key',
50
+ builder: (yargs) => {
48
51
  return yargs
49
52
  },
50
- async (argv) => {
53
+ handler: async (argv) => {
51
54
  startGenerateKey();
52
55
  }
53
- )
54
- .command('conf:generate',
55
- 'Generate a new config file: .env',
56
- (yargs) => {
56
+ })
57
+ .command({
58
+ command: 'conf:generate',
59
+ description:'Generate a new config file: .env',
60
+ builder: (yargs) => {
57
61
  return yargs
58
62
  },
59
- async (argv) => {
63
+ handler: async (argv) => {
60
64
  startGenerateConf();
61
65
  }
62
- )
63
- .command('testconf:generate',
64
- 'Generate a new config file: .env.test',
65
- (yargs) => {
66
+ })
67
+ .command({
68
+ command: 'testconf:generate',
69
+ description:'Generate a new config file: .env.test',
70
+ builder: (yargs) => {
66
71
  return yargs
67
72
  },
68
- async (argv) => {
73
+ handler: async (argv) => {
69
74
  startGenerateTestConf();
70
75
  }
71
- )
72
- .command('admin:generate',
73
- 'Generates a new admin user in database table: users',
74
- (yargs) => {
76
+ })
77
+ .command({
78
+ command: 'admin:generate',
79
+ description:'Generates a new admin user in database table: users',
80
+ builder: (yargs) => {
75
81
  return yargs
76
82
  },
77
- async (argv) => {
83
+ handler: async (argv) => {
78
84
  startGenerateAdmin();
79
85
  }
80
- )
81
- .command('db:import <model> <filePath> [sep]',
82
- `Import CSV or JSON file to database table
86
+ })
87
+ .command({
88
+ command: 'db:import <model> <filePath> [sep]',
89
+ description: `Import CSV or JSON file to database table
83
90
 
84
91
  Examples:
85
92
  node op db:import thing somethings.json
@@ -90,7 +97,7 @@ node op db:import thing somethings.csv ";"
90
97
 
91
98
  In CSV file the field names must match the model fields.
92
99
  `,
93
- (yargs) => {
100
+ builder: (yargs) => {
94
101
  return yargs
95
102
  .positional('model', {
96
103
  type: 'string',
@@ -105,42 +112,47 @@ In CSV file the field names must match the model fields.
105
112
  description: 'Separator for the CSV file'
106
113
  })
107
114
  },
108
- async (argv) => {
115
+ handler: async (argv) => {
109
116
  const { model, filePath, sep } = argv;
110
117
  await runImportData(model, filePath, sep);
111
118
  }
112
- )
113
- .command('make:migration <name>',
114
- 'Generates a new migration file',
115
- (yargs) => {
119
+ })
120
+ .command({
121
+ command: 'make:migration <name>',
122
+ description:'Generates a new migration file',
123
+ builder: (yargs) => {
116
124
  return yargs
117
125
  .positional('name', {
118
126
  type: 'string',
119
127
  description: 'Name of the migration'
120
128
  })
121
129
  },
122
- async (argv) => {
130
+ handler: async (argv) => {
123
131
  const { name } = argv;
124
132
  await createMigration(name);
125
133
  }
126
- )
127
- .command('migration:run [migrationName]',
128
- 'Runs migrations',
129
- (yargs) => {
134
+ })
135
+ .command({
136
+ command: 'migrate [migrationName]',
137
+ description:'Runs migrations',
138
+ aliases: ['migration:run'],
139
+ builder: (yargs) => {
130
140
  return yargs
131
141
  .positional('migrationName', {
132
142
  type: 'string',
133
143
  description: 'Name of the migration'
134
144
  })
135
145
  },
136
- async (argv) => {
146
+ handler: async (argv) => {
137
147
  const { migrationName } = argv;
138
148
  await startMigrations(migrationName);
139
149
  }
140
- )
141
- .command('migration:rollback',
142
- 'Rolls back migrations',
143
- (yargs) => {
150
+ })
151
+ .command({
152
+ command: 'migrate:rollback',
153
+ description:'Rolls back migrations',
154
+ aliases: ['migration:rollback'],
155
+ builder: (yargs) => {
144
156
  return yargs
145
157
  .options({
146
158
  step: {
@@ -150,29 +162,64 @@ In CSV file the field names must match the model fields.
150
162
  }
151
163
  })
152
164
  },
153
- async (argv) => {
165
+ handler: async (argv) => {
154
166
  const { step } = argv;
155
167
  await rollbackMigrations(step);
156
168
  }
157
- )
158
- .command('migration:fresh',
159
- 'Rolls back all migrations',
160
- (yargs) => {
169
+ })
170
+ .command({
171
+ command: 'migrate:fresh',
172
+ description:'Rolls back all migrations',
173
+ aliases: ['migration:fresh'],
174
+ builder: (yargs) => {
161
175
  return yargs
162
176
  },
163
- async (argv) => {
177
+ handler: async (argv) => {
164
178
  await freshMigrations();
165
179
  }
166
- )
167
- .command('migration:reset',
168
- 'Rolls back all migrations',
169
- (yargs) => {
180
+ })
181
+ .command({
182
+ command: 'migrate:reset',
183
+ description:'Rolls back all migrations',
184
+ aliases: ['migration:reset'],
185
+ builder: (yargs) => {
170
186
  return yargs
171
187
  },
172
- async (argv) => {
188
+ handler: async (argv) => {
173
189
  await resetMigrations();
174
190
  }
175
- )
191
+ })
192
+ .command({
193
+ command: 'make:seeder <seederName>',
194
+ description:'Make seeder...',
195
+ builder: (yargs) => {
196
+ return yargs
197
+ .positional('seederName', {
198
+ type: 'string',
199
+ description: 'Name of the seeder'
200
+ })
201
+ },
202
+ handler: async (argv) => {
203
+ const { seederName } = argv;
204
+ await createSeeder(seederName);
205
+ }
206
+ })
207
+ .command({
208
+ command: 'db:seed [seederName]',
209
+ description:'Runs seeders',
210
+ aliases: ['seeder:run'],
211
+ builder: (yargs) => {
212
+ return yargs
213
+ .positional('seederName', {
214
+ type: 'string',
215
+ description: 'Name of the seeder'
216
+ })
217
+ },
218
+ handler: async (argv) => {
219
+ const { seederName } = argv;
220
+ await startSeeders(seederName);
221
+ }
222
+ })
176
223
  .parse();
177
224
 
178
225
  async function copyController(className) {
@@ -459,12 +506,12 @@ async function createMigration(name) {
459
506
 
460
507
  }
461
508
 
462
- async function getUmzug() {
509
+ async function getUmzug(directory) {
463
510
  const { default: sequelize } = await import('./app/database/database.js')
464
511
  const { Umzug, SequelizeStorage } = await import('umzug')
465
512
 
466
513
  const umzug = new Umzug({
467
- migrations: { glob: './database/migrations/*.js' },
514
+ migrations: { glob: directory },
468
515
  context: sequelize.getQueryInterface(),
469
516
  storage: new SequelizeStorage({ sequelize }),
470
517
  logger: console
@@ -487,26 +534,31 @@ async function runOneMigration(name) {
487
534
  if(!name.endsWith('.js')) {
488
535
  name += '.js'
489
536
  }
490
-
491
- const migrationPath = `database/migrations/${name}`
537
+ let migrationPath = '';
538
+ if(!name.startsWith('database/migrations/')) {
539
+ migrationPath = `database/migrations/${name}`
540
+ }else {
541
+ migrationPath = name
542
+ }
543
+
492
544
  if(!await startCheckIfFileExists(migrationPath)) {
493
545
  console.log(`The migration file ${migrationPath} not already exists.`)
494
546
  process.exit(1)
495
547
  }
496
548
 
497
- const umzug = await getUmzug()
549
+ const umzug = await getUmzug(migrationPath)
498
550
  await umzug.up()
499
551
  }
500
552
 
501
553
  async function runMigrations() {
502
554
  console.log('Run migrations...')
503
- const umzug = await getUmzug()
555
+ const umzug = await getUmzug('./database/migrations/*.js')
504
556
  await umzug.up()
505
557
  }
506
558
 
507
559
  async function resetMigrations() {
508
560
  console.log('Reset migrations...')
509
- const umzug = await getUmzug()
561
+ const umzug = await getUmzug('./database/migrations/*.js')
510
562
  await umzug.down({ to: 0 })
511
563
  }
512
564
 
@@ -518,6 +570,79 @@ async function freshMigrations() {
518
570
 
519
571
  async function rollbackMigrations(step = 1) {
520
572
  console.log('Rollback migrations...')
521
- const umzug = await getUmzug()
573
+ const umzug = await getUmzug('./database/migrations/*.js')
522
574
  await umzug.down({ step: step })
523
575
  }
576
+
577
+ async function createSeeder(name) {
578
+ console.log('Create a new seeder...', name)
579
+
580
+ const lowerName = name.toLowerCase()
581
+ const date = new Date()
582
+ const year = date.getFullYear()
583
+ const month = String(date.getMonth() + 1).padStart(2, '0')
584
+ const day = String(date.getDate()).padStart(2, '0')
585
+ const hours = String(date.getHours()).padStart(2, '0')
586
+ const minutes = String(date.getMinutes()).padStart(2, '0')
587
+ const seconds = String(date.getSeconds()).padStart(2, '0')
588
+ const timestamp = `${year}_${month}_${day}_${hours}${minutes}${seconds}`
589
+
590
+ const migrationName = `${timestamp}_${lowerName}`
591
+
592
+ const src = 'templates/seederTemplate.js'
593
+ const dest = `database/seeders/${migrationName}.js`
594
+
595
+ if(await startCheckIfFileExists(dest)) {
596
+ process.exit(1);
597
+ }
598
+
599
+ await fse.copy(src, dest)
600
+
601
+ replace({
602
+ regex: 'Thing',
603
+ replacement: capitalizeFirstLetter(name),
604
+ paths: [dest]
605
+ })
606
+ replace({
607
+ regex: 'thing',
608
+ replacement: name,
609
+ paths: [dest]
610
+ })
611
+
612
+ }
613
+
614
+ async function startSeeders(name) {
615
+ if(name) {
616
+ await runOneSeeder(name)
617
+ } else {
618
+ await runSeeders()
619
+ }
620
+ }
621
+
622
+ async function runOneSeeder(name) {
623
+ console.log('Run one seeder...', name)
624
+
625
+ if(!name.endsWith('.js')) {
626
+ name += '.js'
627
+ }
628
+ let seederPath = ''
629
+ if(!name.startsWith('database/seeders/')) {
630
+ seederPath = `database/seeders/${name}`
631
+ }else {
632
+ seederPath = name
633
+ }
634
+
635
+ if(!await startCheckIfFileExists(seederPath)) {
636
+ console.log(`The seeder file ${seederPath} not already exists.`)
637
+ process.exit(1)
638
+ }
639
+
640
+ const umzug = await getUmzug('./database/seeders/*.js')
641
+ await umzug.up()
642
+ }
643
+
644
+ async function runSeeders() {
645
+ console.log('Run seeders...')
646
+ const umzug = await getUmzug('./database/seeders/*.js')
647
+ await umzug.up()
648
+ }
@@ -0,0 +1,20 @@
1
+ import db from '../../app/models/modrels.js';
2
+
3
+ async function up({context: QueryInterface}) {
4
+ if(db.Thing) {
5
+ await db.Thing.bulkCreate([
6
+
7
+ ]);
8
+ }else {
9
+ await QueryInterface.bulkInsert('things', [
10
+
11
+ ]);
12
+ }
13
+
14
+ }
15
+
16
+ async function down({context: QueryInterface}) {
17
+ await QueryInterface.bulkDelete('things');
18
+ }
19
+
20
+ export { up, down }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-sip",
3
- "version": "1.3.8",
3
+ "version": "1.4.0",
4
4
  "main": "index.js",
5
5
  "bin": {
6
6
  "create-sip": "create-sip.js"