@vida-global/core 1.2.1 → 1.2.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.
|
@@ -4,7 +4,7 @@ const { logger } = require('../logger');
|
|
|
4
4
|
const { Model, Op } = require('sequelize');
|
|
5
5
|
const nodeUtil = require('util');
|
|
6
6
|
const { redisClientFactory } = require('../redis');
|
|
7
|
-
const { tableize }
|
|
7
|
+
const { camelize, tableize } = require('inflection');
|
|
8
8
|
const utils = require('./utils');
|
|
9
9
|
|
|
10
10
|
|
|
@@ -53,6 +53,15 @@ class BaseRecord extends Model {
|
|
|
53
53
|
|
|
54
54
|
if (this._initialized) return;
|
|
55
55
|
|
|
56
|
+
const { schema, options } = this.initializationOptions;
|
|
57
|
+
this.init(schema, options);
|
|
58
|
+
this.initializeHooks();
|
|
59
|
+
|
|
60
|
+
this._initialized = true;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
static get initializationOptions() {
|
|
56
65
|
const schema = getActiveRecordSchema(this._tableName, this.databaseId, this.connection.dialect);
|
|
57
66
|
const options = {
|
|
58
67
|
createdAt: 'created_at',
|
|
@@ -67,11 +76,23 @@ class BaseRecord extends Model {
|
|
|
67
76
|
options.timestamps = false;
|
|
68
77
|
}
|
|
69
78
|
|
|
70
|
-
this.
|
|
79
|
+
this.configureOverridenAccessors(schema);
|
|
71
80
|
|
|
72
|
-
|
|
81
|
+
return { schema, options };
|
|
82
|
+
}
|
|
73
83
|
|
|
74
|
-
|
|
84
|
+
|
|
85
|
+
static configureOverridenAccessors(schema) {
|
|
86
|
+
for (const [colName, colOptions] of Object.entries(schema)) {
|
|
87
|
+
const getterName = `_get${camelize(colName)}`;
|
|
88
|
+
const setterName = `_set${camelize(colName)}`;
|
|
89
|
+
if (this.prototype[getterName]) {
|
|
90
|
+
colOptions.get = this.prototype[getterName]
|
|
91
|
+
}
|
|
92
|
+
if (this.prototype[setterName]) {
|
|
93
|
+
colOptions.set = this.prototype[setterName]
|
|
94
|
+
}
|
|
95
|
+
}
|
|
75
96
|
}
|
|
76
97
|
|
|
77
98
|
|
package/package.json
CHANGED
|
@@ -6,6 +6,7 @@ const importSchema = require('../../lib/active_record/db/importSc
|
|
|
6
6
|
const { Model, Sequelize } = require('sequelize');
|
|
7
7
|
const { redisClientFactory } = require('../../lib/redis');
|
|
8
8
|
const TestHelpers = require('@vida-global/test-helpers');
|
|
9
|
+
const { camelize } = require('inflection');
|
|
9
10
|
|
|
10
11
|
|
|
11
12
|
const connectionConfig = {database: ' ', host: ' ', password: ' ', username: ' '};
|
|
@@ -53,6 +54,7 @@ const dataTypeKey1 = Object.keys(Sequelize.DataTypes)[0];
|
|
|
53
54
|
const dataTypeKey2 = Object.keys(Sequelize.DataTypes)[1];
|
|
54
55
|
const dataTypeValue1 = Sequelize.DataTypes[dataTypeKey1].types.postgres[0];
|
|
55
56
|
const dataTypeValue2 = Sequelize.DataTypes[dataTypeKey2].types.postgres[0];
|
|
57
|
+
const col1Name = `${TestHelpers.Faker.Text.randomString()}_${TestHelpers.Faker.Text.randomString()}_${TestHelpers.Faker.Text.randomString()}`;
|
|
56
58
|
|
|
57
59
|
|
|
58
60
|
jest.mock('../../lib/redis', () => {
|
|
@@ -67,7 +69,12 @@ jest.mock('../../lib/redis', () => {
|
|
|
67
69
|
});
|
|
68
70
|
|
|
69
71
|
jest.mock('../../lib/active_record/db/importSchema', () => jest.fn());
|
|
70
|
-
|
|
72
|
+
const schema = {created_at: {type: dataTypeValue1},
|
|
73
|
+
updated_at: {type: dataTypeValue2},
|
|
74
|
+
[col1Name]: {type: dataTypeValue2}
|
|
75
|
+
};
|
|
76
|
+
importSchema.mockImplementation(() => structuredClone(schema));
|
|
77
|
+
beforeEach(() => importSchema.mockImplementation(() => structuredClone(schema)));
|
|
71
78
|
|
|
72
79
|
|
|
73
80
|
afterEach(() => {
|
|
@@ -88,7 +95,8 @@ describe('BaseRecord', () => {
|
|
|
88
95
|
class User extends BaseRecord {}
|
|
89
96
|
User.initialize();
|
|
90
97
|
const tableDetails = {created_at: {type: Sequelize.DataTypes[dataTypeKey1]},
|
|
91
|
-
updated_at: {type: Sequelize.DataTypes[dataTypeKey2]}
|
|
98
|
+
updated_at: {type: Sequelize.DataTypes[dataTypeKey2]},
|
|
99
|
+
[col1Name]: {type: Sequelize.DataTypes[dataTypeKey2]}
|
|
92
100
|
};
|
|
93
101
|
expect(importSchema).toHaveBeenCalledTimes(1);
|
|
94
102
|
expect(Model.init).toHaveBeenCalledTimes(1);
|
|
@@ -137,6 +145,34 @@ describe('BaseRecord', () => {
|
|
|
137
145
|
it ('will throw an error when run on BaseRecord', () => {
|
|
138
146
|
expect(() => BaseRecord.initialize()).toThrow();
|
|
139
147
|
});
|
|
148
|
+
|
|
149
|
+
it ('allows for overriding getters and setters', () => {
|
|
150
|
+
class User extends BaseRecord {}
|
|
151
|
+
const getterMethodName = `_get${camelize(col1Name)}`;
|
|
152
|
+
const setterMethodName = `_set${camelize(col1Name)}`;
|
|
153
|
+
const getter = jest.fn();
|
|
154
|
+
const setter = jest.fn();
|
|
155
|
+
User.prototype[getterMethodName] = getter;
|
|
156
|
+
User.prototype[setterMethodName] = setter;
|
|
157
|
+
User.initialize();
|
|
158
|
+
const tableDetails = {created_at: {type: Sequelize.DataTypes[dataTypeKey1]},
|
|
159
|
+
updated_at: {type: Sequelize.DataTypes[dataTypeKey2]},
|
|
160
|
+
[col1Name]: {
|
|
161
|
+
type: Sequelize.DataTypes[dataTypeKey2],
|
|
162
|
+
get: getter,
|
|
163
|
+
set: setter,
|
|
164
|
+
}};
|
|
165
|
+
expect(Model.init).toHaveBeenCalledWith(tableDetails,
|
|
166
|
+
{
|
|
167
|
+
createdAt: 'created_at',
|
|
168
|
+
deletedAt: 'deleted_at',
|
|
169
|
+
modelName: 'User',
|
|
170
|
+
sequelize: mockSequelize,
|
|
171
|
+
tableName: 'users',
|
|
172
|
+
updatedAt: 'updated_at'
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
});
|
|
140
176
|
});
|
|
141
177
|
|
|
142
178
|
|