mevn-orm 2.2.13 → 2.2.14
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.
- package/initDb +78 -73
- package/lib/model.js +12 -8
- package/package.json +1 -1
- package/test/model.test.js +1 -0
package/initDb
CHANGED
|
@@ -20,85 +20,90 @@ default:
|
|
|
20
20
|
}
|
|
21
21
|
async function initDatabase() {
|
|
22
22
|
const DB = Knex(config)
|
|
23
|
-
await DB.schema.dropTableIfExists('farmers')
|
|
24
|
-
await DB.schema.dropTableIfExists('farms')
|
|
25
|
-
await DB.schema.dropTableIfExists('profiles')
|
|
26
|
-
await DB.schema.dropTableIfExists('articles')
|
|
27
|
-
await DB.schema.createTable('farmers', (table) => {
|
|
28
|
-
table.bigIncrements('id')
|
|
29
|
-
table.string('name')
|
|
30
|
-
table.string('email').unique()
|
|
31
|
-
table.string('password')
|
|
32
|
-
})
|
|
33
|
-
await DB.schema.createTable('farms', (table) => {
|
|
34
|
-
table.bigIncrements('id')
|
|
35
|
-
table.bigInteger('farmer_id')
|
|
36
|
-
table.string('name')
|
|
37
|
-
})
|
|
38
23
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
24
|
+
try {
|
|
25
|
+
await DB.schema.dropTableIfExists('farmers')
|
|
26
|
+
await DB.schema.dropTableIfExists('farms')
|
|
27
|
+
await DB.schema.dropTableIfExists('profiles')
|
|
28
|
+
await DB.schema.dropTableIfExists('articles')
|
|
29
|
+
await DB.schema.createTable('farmers', (table) => {
|
|
30
|
+
table.bigIncrements('id')
|
|
31
|
+
table.string('name')
|
|
32
|
+
table.string('email').unique()
|
|
33
|
+
table.string('password')
|
|
34
|
+
})
|
|
35
|
+
await DB.schema.createTable('farms', (table) => {
|
|
36
|
+
table.bigIncrements('id')
|
|
37
|
+
table.bigInteger('farmer_id')
|
|
38
|
+
table.string('name')
|
|
39
|
+
})
|
|
44
40
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
table.string('postable_type')
|
|
51
|
-
})
|
|
41
|
+
await DB.schema.createTable('profiles', (table) => {
|
|
42
|
+
table.bigIncrements('id')
|
|
43
|
+
table.bigInteger('farmer_id')
|
|
44
|
+
table.string('bio')
|
|
45
|
+
})
|
|
52
46
|
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
name: 'Ashley Doe',
|
|
61
|
-
email: 'ashley@mail.com',
|
|
62
|
-
password: 'pasword'
|
|
63
|
-
},
|
|
64
|
-
{
|
|
65
|
-
name: 'Alice Doe',
|
|
66
|
-
email: 'alice@mail.com',
|
|
67
|
-
password: 'pasword'
|
|
68
|
-
}
|
|
69
|
-
])
|
|
47
|
+
await DB.schema.createTable('articles', (table) => {
|
|
48
|
+
table.bigIncrements('id')
|
|
49
|
+
table.string('title')
|
|
50
|
+
table.text('body')
|
|
51
|
+
table.bigInteger('postable_id')
|
|
52
|
+
table.string('postable_type')
|
|
53
|
+
})
|
|
70
54
|
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
55
|
+
await DB.table('Farmers').insert([
|
|
56
|
+
{
|
|
57
|
+
name: 'Jane Doe',
|
|
58
|
+
email: 'jane@mail.com',
|
|
59
|
+
password: 'pasword'
|
|
60
|
+
},
|
|
61
|
+
{
|
|
62
|
+
name: 'Ashley Doe',
|
|
63
|
+
email: 'ashley@mail.com',
|
|
64
|
+
password: 'pasword'
|
|
65
|
+
},
|
|
66
|
+
{
|
|
67
|
+
name: 'Alice Doe',
|
|
68
|
+
email: 'alice@mail.com',
|
|
69
|
+
password: 'pasword'
|
|
70
|
+
}
|
|
71
|
+
])
|
|
85
72
|
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
73
|
+
await DB.table('farms').insert([
|
|
74
|
+
{
|
|
75
|
+
farmer_id: 1,
|
|
76
|
+
name: 'Awesome Farm'
|
|
77
|
+
},
|
|
78
|
+
{
|
|
79
|
+
farmer_id: 1,
|
|
80
|
+
name: 'Awesome Farm two'
|
|
81
|
+
},
|
|
82
|
+
{
|
|
83
|
+
farmer_id: 1,
|
|
84
|
+
name: 'Awesome Farm three'
|
|
85
|
+
}
|
|
86
|
+
])
|
|
92
87
|
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
}
|
|
100
|
-
])
|
|
88
|
+
await DB.table('profiles').insert([
|
|
89
|
+
{
|
|
90
|
+
farmer_id: 1,
|
|
91
|
+
bio: 'Profile for farmer one'
|
|
92
|
+
}
|
|
93
|
+
])
|
|
101
94
|
|
|
102
|
-
|
|
95
|
+
await DB.table('articles').insert([
|
|
96
|
+
{
|
|
97
|
+
title: 'Awesome Post',
|
|
98
|
+
body: 'fffgjdfjdbdb something #1',
|
|
99
|
+
postable_id: 1,
|
|
100
|
+
postable_type: 'Farmer'
|
|
101
|
+
}
|
|
102
|
+
])
|
|
103
|
+
|
|
104
|
+
process.exit(0)
|
|
105
|
+
} catch (error) {
|
|
106
|
+
process.exit(1)
|
|
107
|
+
}
|
|
103
108
|
}
|
|
104
109
|
initDatabase()
|
package/lib/model.js
CHANGED
|
@@ -41,12 +41,13 @@ class Model {
|
|
|
41
41
|
this.fillable.forEach((f) => {
|
|
42
42
|
rows[f] = this[f]
|
|
43
43
|
})
|
|
44
|
-
const id = await DB(this.table)
|
|
44
|
+
const [id] = await DB(this.table)
|
|
45
45
|
.insert(rows)
|
|
46
46
|
const fields = await DB(this.table).where({ id }).first()
|
|
47
47
|
for (const f in fields) {
|
|
48
48
|
this[f] = fields[f]
|
|
49
49
|
}
|
|
50
|
+
this['id'] = id
|
|
50
51
|
return this.stripColumns(this)
|
|
51
52
|
} catch (error) {
|
|
52
53
|
throw new Error(error)
|
|
@@ -176,10 +177,13 @@ class Model {
|
|
|
176
177
|
if (!foreignKey) {
|
|
177
178
|
foreignKey = `${this.modelName}_id`
|
|
178
179
|
}
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
180
|
+
|
|
181
|
+
if(localKey) {
|
|
182
|
+
relation[foreignKey] = localKey
|
|
183
|
+
const res = await DB(table).where(relation).first()
|
|
184
|
+
if (res) {
|
|
185
|
+
return this.stripColumns(new Related(res))
|
|
186
|
+
}
|
|
183
187
|
}
|
|
184
188
|
return null
|
|
185
189
|
}
|
|
@@ -227,11 +231,11 @@ class Model {
|
|
|
227
231
|
* Delete columns that are not needed
|
|
228
232
|
*
|
|
229
233
|
*/
|
|
230
|
-
stripColumns(
|
|
234
|
+
stripColumns(model) {
|
|
231
235
|
this.#private.concat(this.hidden).forEach((h) => {
|
|
232
|
-
delete
|
|
236
|
+
delete model[h]
|
|
233
237
|
})
|
|
234
|
-
return
|
|
238
|
+
return model
|
|
235
239
|
}
|
|
236
240
|
|
|
237
241
|
}
|
package/package.json
CHANGED
package/test/model.test.js
CHANGED