mevn-orm 2.2.3 → 2.2.7
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/lib/model.js +90 -3
- package/package.json +1 -1
- package/test/model.test.js +28 -1
package/lib/model.js
CHANGED
|
@@ -19,7 +19,7 @@ default:
|
|
|
19
19
|
const DB = knex(config)
|
|
20
20
|
class Model {
|
|
21
21
|
#private
|
|
22
|
-
static
|
|
22
|
+
static currentTable = pluralize(this.name.toLowerCase())
|
|
23
23
|
static currentQuery
|
|
24
24
|
constructor(properties) {
|
|
25
25
|
for (const key in properties) {
|
|
@@ -27,9 +27,9 @@ class Model {
|
|
|
27
27
|
}
|
|
28
28
|
this.fillable = []
|
|
29
29
|
this.hidden = []
|
|
30
|
-
this.#private = ['fillable', 'hidden'
|
|
30
|
+
this.#private = ['fillable', 'hidden']
|
|
31
31
|
this.modelName = this.constructor.name.toLowerCase()
|
|
32
|
-
this.table = pluralize(this.
|
|
32
|
+
this.table = pluralize(this.constructor.name.toLowerCase())
|
|
33
33
|
}
|
|
34
34
|
|
|
35
35
|
/**
|
|
@@ -53,6 +53,93 @@ class Model {
|
|
|
53
53
|
}
|
|
54
54
|
}
|
|
55
55
|
|
|
56
|
+
/**
|
|
57
|
+
* Update a model
|
|
58
|
+
* @param {*} properties
|
|
59
|
+
* @returns this
|
|
60
|
+
*/
|
|
61
|
+
async update(properties) {
|
|
62
|
+
try {
|
|
63
|
+
const id = await DB(this.table)
|
|
64
|
+
.where({ id: this.id })
|
|
65
|
+
.update(properties)
|
|
66
|
+
const fields = await DB(this.table).where({ id }).first()
|
|
67
|
+
// console.log(fields)
|
|
68
|
+
return this.stripColumns(new this.constructor(fields))
|
|
69
|
+
} catch (error) {
|
|
70
|
+
console.log(error)
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Update a model
|
|
77
|
+
* @param {*} properties
|
|
78
|
+
* @returns
|
|
79
|
+
*/
|
|
80
|
+
static async update(properties) {
|
|
81
|
+
try {
|
|
82
|
+
if (!this.currentQuery) {
|
|
83
|
+
const rows = await DB(this.table).update(properties)
|
|
84
|
+
return rows
|
|
85
|
+
}
|
|
86
|
+
} catch (error) {
|
|
87
|
+
console.log(error)
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Delete a model
|
|
93
|
+
* @returns
|
|
94
|
+
*/
|
|
95
|
+
static async destroy() {
|
|
96
|
+
try {
|
|
97
|
+
if (!this.currentQuery) {
|
|
98
|
+
const rows = await DB(this.table).delete()
|
|
99
|
+
return rows
|
|
100
|
+
}
|
|
101
|
+
} catch (error) {
|
|
102
|
+
console.log(error)
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Find a model
|
|
108
|
+
* @param {*} id
|
|
109
|
+
* @param {*} columns
|
|
110
|
+
* @returns this
|
|
111
|
+
*/
|
|
112
|
+
static async find(id, columns = '*') {
|
|
113
|
+
const table = pluralize(this.name.toLowerCase())
|
|
114
|
+
try {
|
|
115
|
+
const fields = await DB(table)
|
|
116
|
+
.where({ id })
|
|
117
|
+
.first(columns)
|
|
118
|
+
const model = new this(fields)
|
|
119
|
+
return model.stripColumns(model)
|
|
120
|
+
} catch (error) {
|
|
121
|
+
console.log(error)
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Create a new model
|
|
127
|
+
* @param {*} properties
|
|
128
|
+
* @returns this
|
|
129
|
+
*/
|
|
130
|
+
static async create(properties) {
|
|
131
|
+
const table = pluralize(this.name.toLowerCase())
|
|
132
|
+
try {
|
|
133
|
+
const fields = await DB(table)
|
|
134
|
+
.insert(properties)
|
|
135
|
+
const record = await DB(table).where({ id: fields[0] }).first()
|
|
136
|
+
const model = new this(record)
|
|
137
|
+
return model.stripColumns(model)
|
|
138
|
+
} catch (error) {
|
|
139
|
+
console.log(error)
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
56
143
|
/**
|
|
57
144
|
* --------------|
|
|
58
145
|
* Relationships |
|
package/package.json
CHANGED
package/test/model.test.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/* eslint-disable no-undef */
|
|
2
|
-
const { Model } = require('../index')
|
|
2
|
+
const { Model, DB } = require('../index')
|
|
3
3
|
const faker = require('faker')
|
|
4
4
|
const { expect } = require('chai')
|
|
5
5
|
class Profile extends Model {
|
|
@@ -14,6 +14,8 @@ class Farmer extends Model {
|
|
|
14
14
|
}
|
|
15
15
|
}
|
|
16
16
|
|
|
17
|
+
DB(Farmer)
|
|
18
|
+
|
|
17
19
|
describe('#Model tests', () => {
|
|
18
20
|
it('#Model instance', async () => {
|
|
19
21
|
const farmer = new Farmer({
|
|
@@ -25,6 +27,31 @@ describe('#Model tests', () => {
|
|
|
25
27
|
expect(farmer).to.an('Object')
|
|
26
28
|
})
|
|
27
29
|
|
|
30
|
+
it('#find a model', async () => {
|
|
31
|
+
const farmer = await Farmer.find(1)
|
|
32
|
+
expect(farmer).to.an('Object')
|
|
33
|
+
})
|
|
34
|
+
|
|
35
|
+
it('#create a model', async () => {
|
|
36
|
+
const farmer = await Farmer.create({
|
|
37
|
+
name: faker.name.findName(),
|
|
38
|
+
email: faker.internet.email(),
|
|
39
|
+
password: faker.internet.password()
|
|
40
|
+
})
|
|
41
|
+
expect(farmer).to.an('Object')
|
|
42
|
+
expect(farmer.id).to.be.a('number')
|
|
43
|
+
})
|
|
44
|
+
|
|
45
|
+
it('#Update a model with a new instance', async () => {
|
|
46
|
+
const farmer = await Farmer.find(1)
|
|
47
|
+
await farmer.update({
|
|
48
|
+
name: 'new name',
|
|
49
|
+
email: faker.internet.email(),
|
|
50
|
+
password: faker.internet.password()
|
|
51
|
+
})
|
|
52
|
+
expect(farmer).to.an('Object')
|
|
53
|
+
})
|
|
54
|
+
|
|
28
55
|
it('#Has one relationship', async () => {
|
|
29
56
|
const farmer = new Farmer({
|
|
30
57
|
name: faker.name.findName(),
|