@vida-global/core 1.2.5 → 1.3.0

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.
Files changed (46) hide show
  1. package/.github/workflows/npm-test.yml +24 -0
  2. package/index.js +1 -1
  3. package/lib/{active_record → activeRecord}/README.md +3 -3
  4. package/lib/{active_record → activeRecord}/baseRecord.js +11 -2
  5. package/lib/http/README.md +2 -2
  6. package/lib/server/README.md +181 -20
  7. package/lib/server/errors.js +28 -0
  8. package/lib/server/index.js +3 -3
  9. package/lib/server/server.js +7 -53
  10. package/lib/server/serverController.js +147 -20
  11. package/package.json +1 -1
  12. package/scripts/{active_record → activeRecord}/migrate.js +1 -1
  13. package/test/{active_record → activeRecord}/baseRecord.test.js +46 -90
  14. package/test/activeRecord/db/connection.test.js +149 -0
  15. package/test/activeRecord/db/connectionConfiguration.test.js +128 -0
  16. package/test/activeRecord/db/migrator.test.js +144 -0
  17. package/test/activeRecord/db/queryInterface.test.js +48 -0
  18. package/test/activeRecord/helpers/baseRecord.js +32 -0
  19. package/test/activeRecord/helpers/baseRecordMocks.js +59 -0
  20. package/test/activeRecord/helpers/connection.js +28 -0
  21. package/test/activeRecord/helpers/connectionConfiguration.js +32 -0
  22. package/test/activeRecord/helpers/fixtures.js +39 -0
  23. package/test/activeRecord/helpers/migrator.js +78 -0
  24. package/test/activeRecord/helpers/queryInterface.js +29 -0
  25. package/test/http/client.test.js +61 -239
  26. package/test/http/error.test.js +23 -47
  27. package/test/http/helpers/client.js +80 -0
  28. package/test/http/helpers/error.js +31 -0
  29. package/test/server/helpers/autoload/TmpWithHelpersController.js +17 -0
  30. package/test/server/helpers/serverController.js +13 -0
  31. package/test/server/serverController.test.js +319 -6
  32. package/test/active_record/db/connection.test.js +0 -221
  33. package/test/active_record/db/connectionConfiguration.test.js +0 -184
  34. package/test/active_record/db/migrator.test.js +0 -266
  35. package/test/active_record/db/queryInterface.test.js +0 -66
  36. /package/lib/{active_record → activeRecord}/db/connection.js +0 -0
  37. /package/lib/{active_record → activeRecord}/db/connectionConfiguration.js +0 -0
  38. /package/lib/{active_record → activeRecord}/db/importSchema.js +0 -0
  39. /package/lib/{active_record → activeRecord}/db/migration.js +0 -0
  40. /package/lib/{active_record → activeRecord}/db/migrationTemplate.js +0 -0
  41. /package/lib/{active_record → activeRecord}/db/migrationVersion.js +0 -0
  42. /package/lib/{active_record → activeRecord}/db/migrator.js +0 -0
  43. /package/lib/{active_record → activeRecord}/db/queryInterface.js +0 -0
  44. /package/lib/{active_record → activeRecord}/db/schema.js +0 -0
  45. /package/lib/{active_record → activeRecord}/index.js +0 -0
  46. /package/lib/{active_record → activeRecord}/utils.js +0 -0
@@ -0,0 +1,48 @@
1
+ const { ConnectionConfiguration } = require('../../../lib/activeRecord/db/connectionConfiguration');
2
+ const { Connection } = require('../../../lib/activeRecord/db/connection');
3
+ const { QueryInterface } = require('../../../lib/activeRecord/db/queryInterface');
4
+ const { Sequelize } = require('sequelize');
5
+ const Helpers = require('../helpers/queryInterface');
6
+
7
+
8
+ const connection = new Connection();
9
+ const queryInterface = new QueryInterface(connection);
10
+
11
+ jest.mock('sequelize', () => {
12
+ const { MockSequelize } = require('../helpers/fixtures');
13
+ return { Sequelize: MockSequelize };
14
+ });
15
+
16
+ afterEach(() => {
17
+ jest.clearAllMocks();
18
+ });
19
+
20
+
21
+ describe('QueryInterface', () => {
22
+ describe('QueryInterface#allTables', () => {
23
+ it('loads all table details from sequelize query interface', async () => {
24
+ const tables = await queryInterface.allTables();
25
+
26
+ expect(Helpers.mockQueryInterface.showAllTables).toHaveBeenCalledTimes(1);
27
+ expect(Helpers.mockQueryInterface.describeTable).toHaveBeenCalledTimes(2);
28
+ expect(Helpers.mockQueryInterface.describeTable).toHaveBeenCalledWith(Helpers.tableName1);
29
+ expect(Helpers.mockQueryInterface.describeTable).toHaveBeenCalledWith(Helpers.tableName2);
30
+ expect(tables).toEqual({
31
+ [Helpers.tableName1]: { name: Helpers.tableName1 },
32
+ [Helpers.tableName2]: { name: Helpers.tableName2 },
33
+ });
34
+ });
35
+ });
36
+
37
+
38
+ describe('QueryInterface#transaction', () => {
39
+ it('delegates transaction callback to sequelize', async () => {
40
+ const callback = jest.fn();
41
+
42
+ await queryInterface.transaction(callback);
43
+
44
+ expect(connection._sequelize.transaction).toHaveBeenCalledTimes(1);
45
+ expect(connection._sequelize.transaction).toHaveBeenCalledWith(callback);
46
+ });
47
+ });
48
+ });
@@ -0,0 +1,32 @@
1
+ const { Connection} = require('../../../lib/activeRecord/db/connection');
2
+ const { ConnectionConfiguration } = require('../../../lib/activeRecord/db/connectionConfiguration');
3
+ const Mocks = require('./baseRecordMocks');
4
+ const TestHelpers = require('@vida-global/test-helpers');
5
+
6
+
7
+ /***************************************************************************************************
8
+ * VARIABLES / VARIABLE GENERATION
9
+ ***************************************************************************************************/
10
+ const colName = `${TestHelpers.Faker.Text.randomString()}_${TestHelpers.Faker.Text.randomString()}_${TestHelpers.Faker.Text.randomString()}`;
11
+ const schema = {created_at: {type: Mocks.dataTypeValue1},
12
+ updated_at: {type: Mocks.dataTypeValue2},
13
+ [colName]: {type: Mocks.dataTypeValue2}
14
+ };
15
+
16
+
17
+ /***************************************************************************************************
18
+ * MOCKS
19
+ ***************************************************************************************************/
20
+ const connectionConfig = {database: ' ', host: ' ', password: ' ', username: ' '};
21
+ const configSpy = jest.spyOn(ConnectionConfiguration, '_fetchAllConfigs');
22
+ configSpy.mockImplementation(() => ({default: {test: connectionConfig}}));
23
+
24
+ const connectionSpy = jest.spyOn(Connection.prototype, '_sequelize', 'get');
25
+ connectionSpy.mockImplementation(() => new Mocks.MockSequelize());
26
+
27
+
28
+ module.exports = {
29
+ colName,
30
+ ...Mocks,
31
+ schema,
32
+ }
@@ -0,0 +1,59 @@
1
+ const TestHelpers = require('@vida-global/test-helpers');
2
+
3
+
4
+ /***************************************************************************************************
5
+ * VARIABLES / VARIABLE GENERATION
6
+ ***************************************************************************************************/
7
+ const dataTypeKey1 = TestHelpers.Faker.Text.randomString();
8
+ const dataTypeValue1 = TestHelpers.Faker.Text.randomString();
9
+ const dataTypeKey2 = TestHelpers.Faker.Text.randomString();
10
+ const dataTypeValue2 = TestHelpers.Faker.Text.randomString();
11
+
12
+
13
+ /***************************************************************************************************
14
+ * MOCKS
15
+ ***************************************************************************************************/
16
+ class MockModel {
17
+ constructor(data, opts={}) {
18
+ this.dataValues = data || {}
19
+ this.options = opts;
20
+ for (const [k,v] of Object.entries(this.dataValues)) {
21
+ this[k] = v;
22
+ }
23
+ };
24
+ static addHook = jest.fn()
25
+ static init = jest.fn()
26
+ static findByPk = jest.fn();
27
+ static findAll = jest.fn();
28
+ static primaryKeyAttribute = TestHelpers.Faker.Text.randomString();
29
+ toJSON() { return this.dataValues }
30
+ }
31
+ MockModel.findAll.mockImplementation(() => []);
32
+
33
+
34
+ class MockSequelize {
35
+ static DataTypes = {
36
+ [dataTypeKey1]: {types: {postgres: [dataTypeValue1]}, key: dataTypeKey1},
37
+ [dataTypeKey2]: {types: {postgres: [dataTypeValue2]}, key: dataTypeKey2},
38
+ postgres: {}
39
+ }
40
+ }
41
+
42
+
43
+ const redisClient = {
44
+ del: jest.fn(),
45
+ mGet: jest.fn(),
46
+ mSet: jest.fn(),
47
+ }
48
+ const mockRedisClientFactory = () => redisClient;
49
+
50
+
51
+ module.exports = {
52
+ dataTypeKey1,
53
+ dataTypeValue1,
54
+ dataTypeKey2,
55
+ dataTypeValue2,
56
+ MockModel,
57
+ mockRedisClientFactory,
58
+ MockSequelize,
59
+ }
@@ -0,0 +1,28 @@
1
+ const TestHelpers = require('@vida-global/test-helpers');
2
+ const Fixtures = require('./fixtures');
3
+
4
+ const defaultConfig = Fixtures.buildPostgresConfig({ pool: { min: Math.random(), max: Math.random() } });
5
+ const secondaryConfig = Fixtures.buildPostgresConfig();
6
+ const sslConfig = Fixtures.buildPostgresConfig({ ssl: true });
7
+ const sqliteConfig = { dialect: 'sqlite' };
8
+
9
+ const originalNodeEnv = process.env.NODE_ENV;
10
+
11
+
12
+ function buildConfigs() {
13
+ return {
14
+ default: { test: defaultConfig, development: defaultConfig, production: defaultConfig },
15
+ [Fixtures.databaseId1]: { test: secondaryConfig, production: {} },
16
+ [Fixtures.databaseId2]: { test: sslConfig, production: {} },
17
+ sqlite: { test: sqliteConfig },
18
+ };
19
+ }
20
+
21
+
22
+ module.exports = {
23
+ ...Fixtures,
24
+ buildConfigs,
25
+ defaultConfig,
26
+ originalNodeEnv,
27
+ secondaryConfig,
28
+ };
@@ -0,0 +1,32 @@
1
+ const { ConnectionConfiguration } = require('../../../lib/activeRecord/db/connectionConfiguration');
2
+ const Fixtures = require('./fixtures');
3
+
4
+
5
+ const testConfig = Fixtures.buildPostgresConfig();
6
+ const productionConfig = Fixtures.buildPostgresConfig();
7
+ const sqliteConfig = { dialect: 'sqlite' };
8
+
9
+
10
+ const originalEnv = {
11
+ dbPoolMin: process.env.DB_POOL_MIN,
12
+ dbPoolMax: process.env.DB_POOL_MAX,
13
+ nodeEnv: process.env.NODE_ENV,
14
+ };
15
+
16
+
17
+ const fetchConfigsSpy = jest.spyOn(ConnectionConfiguration, '_fetchAllConfigs');
18
+ function mockConfigs(configs) {
19
+ fetchConfigsSpy.mockImplementation(() => configs);
20
+ }
21
+
22
+
23
+ module.exports = {
24
+ fetchConfigsSpy,
25
+ ...Fixtures,
26
+ mockConfigs,
27
+ originalEnv,
28
+ productionConfig,
29
+ sqliteConfig,
30
+ testConfig,
31
+ }
32
+
@@ -0,0 +1,39 @@
1
+ const TestHelpers = require('@vida-global/test-helpers');
2
+
3
+ /***************************************************************************************************
4
+ * VARIABLES / VARIABLE GENERATION
5
+ ***************************************************************************************************/
6
+ function buildPostgresConfig(overrides = {}) {
7
+ return {
8
+ database: TestHelpers.Faker.Text.randomString(),
9
+ host: TestHelpers.Faker.Text.randomString(),
10
+ password: TestHelpers.Faker.Text.randomString(),
11
+ port: TestHelpers.Faker.Math.randomNumber(),
12
+ username: TestHelpers.Faker.Text.randomString(),
13
+ ...overrides,
14
+ };
15
+ }
16
+
17
+ const databaseId1 = TestHelpers.Faker.Text.randomString();
18
+ const databaseId2 = TestHelpers.Faker.Text.randomString();
19
+
20
+
21
+ /***************************************************************************************************
22
+ * MOCKS
23
+ ***************************************************************************************************/
24
+ class MockSequelize {
25
+ constructor(_db, _user, _pass, options) {
26
+ this.options = options;
27
+ }
28
+
29
+ transaction = jest.fn(callback => callback());
30
+ close = jest.fn();
31
+ }
32
+
33
+
34
+ module.exports = {
35
+ buildPostgresConfig,
36
+ databaseId1,
37
+ databaseId2,
38
+ MockSequelize,
39
+ };
@@ -0,0 +1,78 @@
1
+ const { Migrator } = require('../../../lib/activeRecord/db/migrator');
2
+ const { ConnectionConfiguration } = require('../../../lib/activeRecord/db/connectionConfiguration');
3
+ const Fixtures = require('./fixtures');
4
+ const { QueryInterface } = require('../../../lib/activeRecord/db/queryInterface');
5
+ const { Sequelize } = require('sequelize');
6
+ const TestHelpers = require('@vida-global/test-helpers');
7
+
8
+
9
+ /***************************************************************************************************
10
+ * VARIABLES / VARIABLE GENERATION
11
+ ***************************************************************************************************/
12
+ const connectionConfig = { database: ' ', host: ' ', password: ' ', username: ' ' };
13
+ const migrationModule = { up: jest.fn(), down: jest.fn() };
14
+ const randomNumber = TestHelpers.Faker.Math.randomNumber
15
+ const randomString = TestHelpers.Faker.Text.randomString;
16
+ const mockQueryInterface = {
17
+ createTable: jest.fn(),
18
+ dropTable: jest.fn(),
19
+ addColumn: jest.fn(),
20
+ removeColumn: jest.fn(),
21
+ addIndex: jest.fn(),
22
+ removeIndex: jest.fn(),
23
+ renameColumn: jest.fn(),
24
+ changeColumn: jest.fn(),
25
+ };
26
+
27
+
28
+ function randomColumnDetails() {
29
+ return { [randomString()]: randomNumber() };
30
+ }
31
+
32
+
33
+ /***************************************************************************************************
34
+ * MOCKS
35
+ ***************************************************************************************************/
36
+ jest.mock('sequelize', () => {
37
+ class MockSequelize {
38
+ static DataTypes = { INTEGER: 'INTEGER', DATE: 'DATE' };
39
+ }
40
+ return { Sequelize: MockSequelize };
41
+ });
42
+ Sequelize.prototype.getQueryInterface = () => mockQueryInterface;
43
+
44
+
45
+ jest.spyOn(ConnectionConfiguration, '_fetchAllConfigs').mockImplementation(() => ({
46
+ default: { test: connectionConfig },
47
+ }));
48
+
49
+
50
+ const migrator = new Migrator();
51
+ jest.spyOn(migrator, 'migrationModule', 'get').mockImplementation(() => migrationModule);
52
+
53
+
54
+ const transactionSpy = jest.spyOn(QueryInterface.prototype, 'transaction');
55
+ transactionSpy.mockImplementation(callback => callback());
56
+
57
+
58
+ /***************************************************************************************************
59
+ * ASSERTIONS
60
+ ***************************************************************************************************/
61
+ async function expectQueryInterfaceCall(methodName, args) {
62
+ await migrator[methodName](...args);
63
+ expect(mockQueryInterface[methodName]).toHaveBeenCalledTimes(1);
64
+ expect(mockQueryInterface[methodName]).toHaveBeenCalledWith(...args);
65
+ }
66
+
67
+
68
+ module.exports = {
69
+ expectQueryInterfaceCall,
70
+ ...Fixtures,
71
+ migrationModule,
72
+ migrator,
73
+ mockQueryInterface,
74
+ randomColumnDetails,
75
+ randomNumber,
76
+ randomString,
77
+ transactionSpy,
78
+ };
@@ -0,0 +1,29 @@
1
+ const { Connection } = require('../../../lib/activeRecord/db/connection');
2
+ const { ConnectionConfiguration } = require('../../../lib/activeRecord/db/connectionConfiguration');
3
+ const Fixtures = require('../helpers/fixtures');
4
+ const { QueryInterface } = require('../../../lib/activeRecord/db/queryInterface');
5
+ const { Sequelize } = require('sequelize');
6
+ const TestHelpers = require('@vida-global/test-helpers');
7
+
8
+
9
+ const tableName1 = TestHelpers.Faker.Text.randomString();
10
+ const tableName2 = TestHelpers.Faker.Text.randomString();
11
+ const mockQueryInterface = {
12
+ showAllTables: jest.fn(() => [tableName1, tableName2]),
13
+ describeTable: jest.fn(tableName => ({ name: tableName })),
14
+ };
15
+
16
+
17
+ const connectionConfig = { database: ' ', host: ' ', password: ' ', username: ' ' };
18
+ const configSpy = jest.spyOn(ConnectionConfiguration, '_fetchAllConfigs');
19
+ configSpy.mockImplementation(() => ({ default: { test: connectionConfig } }));
20
+
21
+ Sequelize.prototype.getQueryInterface = () => mockQueryInterface;
22
+
23
+
24
+ module.exports = {
25
+ ...Fixtures,
26
+ mockQueryInterface,
27
+ tableName1,
28
+ tableName2,
29
+ }