forked from colmena/colmena
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path03-load-content.js
More file actions
91 lines (83 loc) · 2.48 KB
/
03-load-content.js
File metadata and controls
91 lines (83 loc) · 2.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
'use strict'
import { default as debug } from 'debug'
// to enable these logs set `DEBUG=boot:03-load-content` or `DEBUG=boot:*`
const log = debug('boot:03-load-content')
module.exports = function (app) {
// Do not run if we are in codegen mode.
if (process.env.ENV === 'codegen') return
if (app.dataSources.db.name !== 'Memory' && !process.env.INITDB) {
return
}
log('Creating categories and products')
const Category = app.models.Category
const Product = app.models.Product
Category.findOrCreate(
{where: {name: 'Beer'}}, // find
{name: 'Beer'}, // create
(err, category, created) => {
if (err) {
console.error('err', err)
}
(created) ? log('created Category', category.name)
: log('found Category', category.name)
Product.findOrCreate(
{where: {name: 'Draft beer'}}, // find
{
name: 'Draft beer',
price: '250',
categoryId: category.id,
}, // create
(err, data, created) => {
if (err) {
console.error('err', err)
}
(created) ? log('created Product', data.name)
: log('found Product', data.name)
})
Product.findOrCreate(
{where: {name: 'Bottled beer'}}, // find
{
name: 'Bottled beer',
price: '350',
categoryId: category.id,
}, // create
(err, data, created) => {
if (err) {
console.error('err', err)
}
(created) ? log('created Product', data.name)
: log('found Product', data.name)
})
})
Category.findOrCreate({where: {name: 'Wine'}}, {
name: 'Wine',
}, (err, category, created) => {
if (err) {
console.error('err', err)
}
(created) ? log('created Category', category.name)
: log('found Category', category.name)
Product.findOrCreate({where: {name: 'Red wine'}}, {
name: 'Red wine',
price: '350',
categoryId: category.id,
}, (err, data, created) => {
if (err) {
console.error('err', err)
}
(created) ? log('created Product', data.name)
: log('found Product', data.name)
})
Product.findOrCreate({where: {name: 'White wine'}}, {
name: 'White wine',
price: '350',
categoryId: category.id,
}, (err, data, created) => {
if (err) {
console.error('err', err)
}
(created) ? log('created Product', data.name)
: log('found Product', data.name)
})
})
}