mevn-orm 2.2.9 → 2.2.13

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 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) {
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.js')
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) {
@@ -49,7 +49,7 @@ class Model {
49
49
  }
50
50
  return this.stripColumns(this)
51
51
  } catch (error) {
52
- console.log(error)
52
+ throw new Error(error)
53
53
  }
54
54
  }
55
55
 
@@ -67,7 +67,22 @@ class Model {
67
67
  // console.log(fields)
68
68
  return this.stripColumns(new this.constructor(fields))
69
69
  } catch (error) {
70
- console.log(error)
70
+ throw new Error(error)
71
+ }
72
+ }
73
+
74
+ /**
75
+ * Delete a model
76
+ * @returns this
77
+ */
78
+ async delete() {
79
+ try {
80
+ await DB(this.table)
81
+ .where({ id: this.id })
82
+ .del()
83
+ return
84
+ } catch (error) {
85
+ throw new Error(error)
71
86
  }
72
87
  }
73
88
 
@@ -84,7 +99,7 @@ class Model {
84
99
  return rows
85
100
  }
86
101
  } catch (error) {
87
- console.log(error)
102
+ throw new Error(error)
88
103
  }
89
104
  }
90
105
 
@@ -99,7 +114,7 @@ class Model {
99
114
  return rows
100
115
  }
101
116
  } catch (error) {
102
- console.log(error)
117
+ throw new Error(error)
103
118
  }
104
119
  }
105
120
 
@@ -115,10 +130,12 @@ class Model {
115
130
  const fields = await DB(table)
116
131
  .where({ id })
117
132
  .first(columns)
118
- const model = new this(fields)
119
- return model.stripColumns(model)
133
+ if (fields) {
134
+ return new this(fields)
135
+ }
136
+ return null
120
137
  } catch (error) {
121
- console.log(error)
138
+ throw new Error(error)
122
139
  }
123
140
  }
124
141
 
@@ -136,7 +153,7 @@ class Model {
136
153
  const model = new this(record)
137
154
  return model.stripColumns(model)
138
155
  } catch (error) {
139
- console.log(error)
156
+ throw new Error(error)
140
157
  }
141
158
  }
142
159
 
@@ -190,12 +207,18 @@ class Model {
190
207
  try {
191
208
  if (!this.currentQuery) {
192
209
  const rows = await DB(this.table).first(columns)
193
- return new this(rows)
210
+ if (rows) {
211
+ return new this(rows)
212
+ }
213
+ return null
194
214
  }
195
215
  const rows = await this.currentQuery.first(columns)
196
- return new this(rows)
216
+ if (rows) {
217
+ return new this(rows)
218
+ }
219
+ return null
197
220
  } catch (error) {
198
- console.log(error)
221
+ throw new Error(error)
199
222
  }
200
223
  }
201
224
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mevn-orm",
3
- "version": "2.2.9",
3
+ "version": "2.2.13",
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
- "knex": "^0.95"
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.13.16",
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.13.0",
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": "^5.0.0"
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": "dist/index.js"
59
+ "main": "index.js"
55
60
  }
@@ -57,6 +57,17 @@ describe('#Model tests', () => {
57
57
  expect(farmer).to.an('Object')
58
58
  })
59
59
 
60
+ it('#Return null when not found', async () => {
61
+ const farmer = await Farmer.where({ id: 'ggggggg' }).first()
62
+ expect(farmer).to.be.null
63
+ })
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
+
60
71
  it('#Has one relationship', async () => {
61
72
  const farmer = new Farmer({
62
73
  name: faker.name.findName(),