mevn-orm 2.2.10 → 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 +79 -74
- package/{knexfile.js → knexfile.cjs} +0 -0
- package/lib/model.js +28 -9
- package/package.json +13 -8
- package/test/model.test.js +7 -0
package/initDb
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
#!/bin/env node
|
|
2
2
|
require('dotenv').config()
|
|
3
3
|
const Knex = require('knex')
|
|
4
|
-
const { development, staging, production } = require('./knexfile')
|
|
4
|
+
const { development, staging, production } = require('./knexfile.cjs')
|
|
5
5
|
let config
|
|
6
6
|
|
|
7
7
|
switch (process.env.NODE_ENV) {
|
|
@@ -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()
|
|
File without changes
|
package/lib/model.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
const knex = require('knex').knex
|
|
2
|
-
const { development, staging, production } = require(process.cwd() + '/knexfile.
|
|
2
|
+
const { development, staging, production } = require(process.cwd() + '/knexfile.cjs')
|
|
3
3
|
const pluralize = require('pluralize')
|
|
4
4
|
let config
|
|
5
5
|
switch (process.env.NODE_ENV) {
|
|
@@ -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)
|
|
@@ -71,6 +72,21 @@ class Model {
|
|
|
71
72
|
}
|
|
72
73
|
}
|
|
73
74
|
|
|
75
|
+
/**
|
|
76
|
+
* Delete a model
|
|
77
|
+
* @returns this
|
|
78
|
+
*/
|
|
79
|
+
async delete() {
|
|
80
|
+
try {
|
|
81
|
+
await DB(this.table)
|
|
82
|
+
.where({ id: this.id })
|
|
83
|
+
.del()
|
|
84
|
+
return
|
|
85
|
+
} catch (error) {
|
|
86
|
+
throw new Error(error)
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
74
90
|
|
|
75
91
|
/**
|
|
76
92
|
* Update a model
|
|
@@ -161,10 +177,13 @@ class Model {
|
|
|
161
177
|
if (!foreignKey) {
|
|
162
178
|
foreignKey = `${this.modelName}_id`
|
|
163
179
|
}
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
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
|
+
}
|
|
168
187
|
}
|
|
169
188
|
return null
|
|
170
189
|
}
|
|
@@ -212,11 +231,11 @@ class Model {
|
|
|
212
231
|
* Delete columns that are not needed
|
|
213
232
|
*
|
|
214
233
|
*/
|
|
215
|
-
stripColumns(
|
|
234
|
+
stripColumns(model) {
|
|
216
235
|
this.#private.concat(this.hidden).forEach((h) => {
|
|
217
|
-
delete
|
|
236
|
+
delete model[h]
|
|
218
237
|
})
|
|
219
|
-
return
|
|
238
|
+
return model
|
|
220
239
|
}
|
|
221
240
|
|
|
222
241
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mevn-orm",
|
|
3
|
-
"version": "2.2.
|
|
3
|
+
"version": "2.2.14",
|
|
4
4
|
"description": "simple ORM for express js",
|
|
5
5
|
"type": "commonjs",
|
|
6
6
|
"scripts": {
|
|
@@ -11,7 +11,9 @@
|
|
|
11
11
|
"lint": "eslint --ext .js ./"
|
|
12
12
|
},
|
|
13
13
|
"peerDependencies": {
|
|
14
|
-
"
|
|
14
|
+
"dotenv": "^15.0.0",
|
|
15
|
+
"knex": "^1.0.1",
|
|
16
|
+
"mysql": "^2.18.1"
|
|
15
17
|
},
|
|
16
18
|
"keywords": [
|
|
17
19
|
"ORM",
|
|
@@ -20,24 +22,27 @@
|
|
|
20
22
|
"mevn"
|
|
21
23
|
],
|
|
22
24
|
"author": "Stanley Masinde",
|
|
25
|
+
"contributors": [
|
|
26
|
+
"Stanley Masinde <stanmasinde@gmail.com>"
|
|
27
|
+
],
|
|
23
28
|
"license": "MIT",
|
|
24
29
|
"dependencies": {
|
|
25
|
-
"dotenv": "^8.2.0",
|
|
26
|
-
"knex": "^0.95",
|
|
27
30
|
"pluralize": "^8.0.0"
|
|
28
31
|
},
|
|
29
32
|
"devDependencies": {
|
|
30
33
|
"@babel/cli": "^7.13.16",
|
|
31
|
-
"@babel/core": "^7.
|
|
34
|
+
"@babel/core": "^7.15.8",
|
|
32
35
|
"@babel/eslint-parser": "^7.13.14",
|
|
33
36
|
"@babel/eslint-plugin": "^7.13.16",
|
|
34
|
-
"@babel/plugin-proposal-class-properties": "^7.
|
|
37
|
+
"@babel/plugin-proposal-class-properties": "^7.14.5",
|
|
35
38
|
"chai": "^4.2.0",
|
|
39
|
+
"dotenv": "^8.6.0",
|
|
36
40
|
"eslint": "^7.25.0",
|
|
37
41
|
"faker": "^5.5.3",
|
|
42
|
+
"knex": "^0.95.11",
|
|
38
43
|
"mocha": "^9.1.2",
|
|
39
44
|
"mysql2": "^2.3.0",
|
|
40
|
-
"sqlite3": "^
|
|
45
|
+
"sqlite3": "^4.2.0"
|
|
41
46
|
},
|
|
42
47
|
"directories": {
|
|
43
48
|
"test": "test"
|
|
@@ -51,5 +56,5 @@
|
|
|
51
56
|
"url": "https://github.com/StanleyMasinde/mevn-orm/issues"
|
|
52
57
|
},
|
|
53
58
|
"homepage": "https://github.com/StanleyMasinde/mevn-orm#readme",
|
|
54
|
-
"main": "
|
|
59
|
+
"main": "index.js"
|
|
55
60
|
}
|
package/test/model.test.js
CHANGED
|
@@ -62,6 +62,12 @@ describe('#Model tests', () => {
|
|
|
62
62
|
expect(farmer).to.be.null
|
|
63
63
|
})
|
|
64
64
|
|
|
65
|
+
it('#Delete a model', async () => {
|
|
66
|
+
const farmer = await Farmer.find(1)
|
|
67
|
+
await farmer.delete()
|
|
68
|
+
expect(await Farmer.find(1)).to.be.null
|
|
69
|
+
})
|
|
70
|
+
|
|
65
71
|
it('#Has one relationship', async () => {
|
|
66
72
|
const farmer = new Farmer({
|
|
67
73
|
name: faker.name.findName(),
|
|
@@ -73,6 +79,7 @@ describe('#Model tests', () => {
|
|
|
73
79
|
farmer_id: farmer.id,
|
|
74
80
|
bio: faker.lorem.sentence()
|
|
75
81
|
}).save()
|
|
82
|
+
|
|
76
83
|
expect(farmer).to.an('Object')
|
|
77
84
|
const farmerProfile = await farmer.profile()
|
|
78
85
|
expect(farmerProfile).to.haveOwnProperty('farmer_id', farmer.id)
|