dip-dobit-shared-model 1.0.3 → 1.0.6

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/index.js ADDED
@@ -0,0 +1,3 @@
1
+ const initModels = require("./models");
2
+
3
+ module.exports = initModels;
@@ -0,0 +1,75 @@
1
+ "use strict";
2
+
3
+ const { STATUS_ID } = require("../service/constants");
4
+
5
+ module.exports = {
6
+ async up(queryInterface, Sequelize) {
7
+ await queryInterface.createTable("portalUsers", {
8
+ portalUserId: {
9
+ type: Sequelize.INTEGER.UNSIGNED,
10
+ primaryKey: true,
11
+ autoIncrement: true,
12
+ allowNull: false,
13
+ },
14
+ firstName: {
15
+ type: Sequelize.STRING,
16
+ allowNull: false,
17
+ },
18
+ lastName: {
19
+ type: Sequelize.STRING,
20
+ allowNull: false,
21
+ },
22
+ email: {
23
+ type: Sequelize.STRING,
24
+ allowNull: false,
25
+ unique: true,
26
+ },
27
+ username: {
28
+ type: Sequelize.STRING,
29
+ allowNull: false,
30
+ unique: true,
31
+ },
32
+ phoneNumber: {
33
+ type: Sequelize.STRING,
34
+ allowNull: true,
35
+ },
36
+ statusId: {
37
+ type: Sequelize.INTEGER.UNSIGNED,
38
+ allowNull: false,
39
+ defaultValue: STATUS_ID.ACTIVE,
40
+ },
41
+ createdBy: {
42
+ type: Sequelize.INTEGER.UNSIGNED,
43
+ allowNull: true,
44
+ references: {
45
+ model: "PortalUsers",
46
+ key: "portalUserId",
47
+ },
48
+ },
49
+ updatedBy: {
50
+ type: Sequelize.INTEGER.UNSIGNED,
51
+ allowNull: true,
52
+ references: {
53
+ model: "PortalUsers",
54
+ key: "portalUserId",
55
+ },
56
+ },
57
+ createdAt: {
58
+ allowNull: false,
59
+ type: Sequelize.DATE,
60
+ },
61
+ updatedAt: {
62
+ allowNull: false,
63
+ type: Sequelize.DATE,
64
+ },
65
+ deletedAt: {
66
+ type: Sequelize.DATE,
67
+ allowNull: true,
68
+ },
69
+ });
70
+ },
71
+
72
+ async down(queryInterface, Sequelize) {
73
+ await queryInterface.dropTable("PortalUsers");
74
+ },
75
+ };
@@ -48,6 +48,22 @@ module.exports = {
48
48
  allowNull: false,
49
49
  defaultValue: STATUS_ID.ACTIVE,
50
50
  },
51
+ userCreatedby: {
52
+ type: Sequelize.INTEGER.UNSIGNED,
53
+ allowNull: true,
54
+ references: {
55
+ model: "PortalUsers",
56
+ key: "portalUserId",
57
+ },
58
+ },
59
+ userUpdatedby: {
60
+ type: Sequelize.INTEGER.UNSIGNED,
61
+ allowNull: true,
62
+ references: {
63
+ model: "PortalUsers",
64
+ key: "portalUserId",
65
+ },
66
+ },
51
67
  createdAt: {
52
68
  allowNull: false,
53
69
  type: Sequelize.DATE,
@@ -4,13 +4,21 @@ const { Model, DataTypes } = require("sequelize");
4
4
  const { STATUS_ID } = require("../service/constants");
5
5
 
6
6
  module.exports = (sequelize) => {
7
- class DobitUser extends Model {
7
+ class DobitUsers extends Model {
8
8
  static associate(models) {
9
9
  // define association here
10
+ DobitUsers.belongsTo(models.PortalUsers, {
11
+ as: "createdBy",
12
+ foreignKey: "userCreatedby",
13
+ });
14
+ DobitUsers.belongsTo(models.PortalUsers, {
15
+ as: "updatedBy",
16
+ foreignKey: "userUpdatedby",
17
+ });
10
18
  }
11
19
  }
12
20
 
13
- DobitUser.init(
21
+ DobitUsers.init(
14
22
  {
15
23
  dobitUserId: {
16
24
  type: DataTypes.INTEGER.UNSIGNED,
@@ -58,18 +66,34 @@ module.exports = (sequelize) => {
58
66
  allowNull: false,
59
67
  defaultValue: STATUS_ID.ACTIVE,
60
68
  },
69
+ userCreatedby: {
70
+ type: DataTypes.INTEGER.UNSIGNED,
71
+ allowNull: true,
72
+ references: {
73
+ model: "PortalUser",
74
+ key: "portalUserId",
75
+ },
76
+ },
77
+ userUpdatedby: {
78
+ type: DataTypes.INTEGER.UNSIGNED,
79
+ allowNull: true,
80
+ references: {
81
+ model: "PortalUser",
82
+ key: "portalUserId",
83
+ },
84
+ },
61
85
  },
62
86
  {
63
87
  sequelize,
64
- modelName: "DobitUser",
65
- tableName: "dobitUsers",
88
+ modelName: "DobitUsers",
89
+ tableName: "dobitusers",
66
90
  timestamps: true,
67
91
  paranoid: true,
68
- }
92
+ },
69
93
  );
70
94
 
71
- DobitUser.removeAttribute("id");
72
- return DobitUser;
95
+ DobitUsers.removeAttribute("id");
96
+ return DobitUsers;
73
97
  };
74
98
 
75
99
  // firstName, lastName, email, username, phoneNumber, password, nin
package/models/index.js CHANGED
@@ -2,46 +2,22 @@
2
2
 
3
3
  const fs = require("fs");
4
4
  const path = require("path");
5
- const Sequelize = require("sequelize");
6
- const process = require("process");
7
5
  const basename = path.basename(__filename);
8
- const env = process.env.NODE_ENV || "development";
9
- const config = require(__dirname + "/../config/config.js")[env];
10
- const db = {};
11
6
 
12
- let sequelize;
13
- if (config.use_env_variable) {
14
- sequelize = new Sequelize(process.env[config.use_env_variable], config);
15
- } else {
16
- sequelize = new Sequelize(
17
- config.database,
18
- config.username,
19
- config.password,
20
- config
21
- );
22
- }
7
+ module.exports = (sequelize) => {
8
+ const db = {};
23
9
 
24
- fs.readdirSync(__dirname)
25
- .filter((file) => {
26
- return (
27
- file.indexOf(".") !== 0 && file !== basename && file.slice(-3) === ".js"
28
- );
29
- })
30
- .forEach((file) => {
31
- const model = require(path.join(__dirname, file))(
32
- sequelize,
33
- Sequelize.DataTypes
34
- );
35
- db[model.name] = model;
36
- });
37
-
38
- Object.keys(db).forEach((modelName) => {
39
- if (db[modelName].associate) {
40
- db[modelName].associate(db);
41
- }
42
- });
10
+ fs.readdirSync(__dirname)
11
+ .filter((file) => file !== basename && file.endsWith(".js"))
12
+ .forEach((file) => {
13
+ const model = require(path.join(__dirname, file))(sequelize);
14
+ db[model.name] = model;
15
+ });
43
16
 
44
- db.sequelize = sequelize;
45
- db.Sequelize = Sequelize;
17
+ // Setup associations
18
+ Object.keys(db).forEach((name) => {
19
+ if (db[name].associate) db[name].associate(db);
20
+ });
46
21
 
47
- module.exports = db;
22
+ return db;
23
+ };
@@ -0,0 +1,81 @@
1
+ "use strict";
2
+
3
+ const { Model, DataTypes } = require("sequelize");
4
+ const { STATUS_ID } = require("../service/constants");
5
+
6
+ module.exports = (sequelize) => {
7
+ class PortalUsers extends Model {
8
+ static associate(models) {
9
+ // define association here
10
+ PortalUsers.belongsTo(models.PortalUsers, {
11
+ foreignKey: "createdBy",
12
+ as: "creator",
13
+ allowNull: true,
14
+ });
15
+ PortalUsers.belongsTo(models.PortalUsers, {
16
+ foreignKey: "updatedBy",
17
+ as: "editor",
18
+ allowNull: true,
19
+ });
20
+ }
21
+ }
22
+
23
+ PortalUsers.init(
24
+ {
25
+ portalUserId: {
26
+ type: DataTypes.INTEGER.UNSIGNED,
27
+ primaryKey: true,
28
+ autoIncrement: true,
29
+ allowNull: false,
30
+ },
31
+ firstName: {
32
+ type: DataTypes.STRING,
33
+ allowNull: false,
34
+ },
35
+ lastName: {
36
+ type: DataTypes.STRING,
37
+ allowNull: false,
38
+ },
39
+ email: {
40
+ type: DataTypes.STRING,
41
+ allowNull: false,
42
+ unique: true,
43
+ validate: {
44
+ isEmail: true,
45
+ },
46
+ },
47
+ username: {
48
+ type: DataTypes.STRING,
49
+ allowNull: false,
50
+ unique: true,
51
+ },
52
+ phoneNumber: {
53
+ type: DataTypes.STRING,
54
+ allowNull: true,
55
+ },
56
+ statusId: {
57
+ type: DataTypes.INTEGER.UNSIGNED,
58
+ allowNull: false,
59
+ defaultValue: STATUS_ID.ACTIVE,
60
+ },
61
+ createdBy: {
62
+ type: DataTypes.INTEGER.UNSIGNED,
63
+ allowNull: true,
64
+ },
65
+ updatedBy: {
66
+ type: DataTypes.INTEGER.UNSIGNED,
67
+ allowNull: true,
68
+ },
69
+ },
70
+ {
71
+ sequelize,
72
+ modelName: "PortalUsers",
73
+ tableName: "portalusers",
74
+ timestamps: true,
75
+ paranoid: true,
76
+ },
77
+ );
78
+
79
+ PortalUsers.removeAttribute("id");
80
+ return PortalUsers;
81
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dip-dobit-shared-model",
3
- "version": "1.0.3",
3
+ "version": "1.0.6",
4
4
  "main": "models/index.js",
5
5
  "scripts": {
6
6
  "migrate:up": "npx sequelize-cli db:migrate",