mevn-orm 2.2.13 → 2.3.2

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/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2021 StanoJs
3
+ Copyright (c) 2021 Stanley Masinde
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
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.cjs')
4
+ const { development, staging, production } = require('./knexfile.js')
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
- await DB.schema.createTable('profiles', (table) => {
40
- table.bigIncrements('id')
41
- table.bigInteger('farmer_id')
42
- table.string('bio')
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
- await DB.schema.createTable('articles', (table) => {
46
- table.bigIncrements('id')
47
- table.string('title')
48
- table.text('body')
49
- table.bigInteger('postable_id')
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
- await DB.table('Farmers').insert([
54
- {
55
- name: 'Jane Doe',
56
- email: 'jane@mail.com',
57
- password: 'pasword'
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
- await DB.table('farms').insert([
72
- {
73
- farmer_id: 1,
74
- name: 'Awesome Farm'
75
- },
76
- {
77
- farmer_id: 1,
78
- name: 'Awesome Farm two'
79
- },
80
- {
81
- farmer_id: 1,
82
- name: 'Awesome Farm three'
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
- await DB.table('profiles').insert([
87
- {
88
- farmer_id: 1,
89
- bio: 'Profile for farmer one'
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
- await DB.table('articles').insert([
94
- {
95
- title: 'Awesome Post',
96
- body: 'fffgjdfjdbdb something #1',
97
- postable_id: 1,
98
- postable_type: 'Farmer'
99
- }
100
- ])
88
+ await DB.table('profiles').insert([
89
+ {
90
+ farmer_id: 1,
91
+ bio: 'Profile for farmer one'
92
+ }
93
+ ])
101
94
 
102
- process.exit(0)
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.cjs')
2
+ const { development, staging, production } = require(process.cwd() + '/knexfile.js')
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)
@@ -176,10 +177,13 @@ class Model {
176
177
  if (!foreignKey) {
177
178
  foreignKey = `${this.modelName}_id`
178
179
  }
179
- relation[foreignKey] = localKey
180
- const res = await DB(table).where(relation).first()
181
- if (res) {
182
- return this.stripColumns(new Related(res))
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(Model) {
234
+ stripColumns(model) {
231
235
  this.#private.concat(this.hidden).forEach((h) => {
232
- delete Model[h]
236
+ delete model[h]
233
237
  })
234
- return Model
238
+ return model
235
239
  }
236
240
 
237
241
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mevn-orm",
3
- "version": "2.2.13",
3
+ "version": "2.3.2",
4
4
  "description": "simple ORM for express js",
5
5
  "type": "commonjs",
6
6
  "scripts": {
@@ -35,14 +35,14 @@
35
35
  "@babel/eslint-parser": "^7.13.14",
36
36
  "@babel/eslint-plugin": "^7.13.16",
37
37
  "@babel/plugin-proposal-class-properties": "^7.14.5",
38
+ "@vscode/sqlite3": "^5.0.7",
38
39
  "chai": "^4.2.0",
39
- "dotenv": "^8.6.0",
40
+ "dotenv": "^15.0.0",
40
41
  "eslint": "^7.25.0",
41
42
  "faker": "^5.5.3",
42
- "knex": "^0.95.11",
43
+ "knex": "^1.0.1",
43
44
  "mocha": "^9.1.2",
44
- "mysql2": "^2.3.0",
45
- "sqlite3": "^4.2.0"
45
+ "mysql2": "^2.3.0"
46
46
  },
47
47
  "directories": {
48
48
  "test": "test"
@@ -79,6 +79,7 @@ describe('#Model tests', () => {
79
79
  farmer_id: farmer.id,
80
80
  bio: faker.lorem.sentence()
81
81
  }).save()
82
+
82
83
  expect(farmer).to.an('Object')
83
84
  const farmerProfile = await farmer.profile()
84
85
  expect(farmerProfile).to.haveOwnProperty('farmer_id', farmer.id)