@loomcore/api 0.1.60 → 0.1.63

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 (39) hide show
  1. package/LICENSE +201 -201
  2. package/README.md +49 -49
  3. package/dist/__tests__/common-test.utils.js +10 -8
  4. package/dist/__tests__/postgres-test-migrations/postgres-test-schema.js +52 -52
  5. package/dist/__tests__/postgres.test-database.js +8 -8
  6. package/dist/__tests__/test-email-client.d.ts +4 -0
  7. package/dist/__tests__/test-email-client.js +6 -0
  8. package/dist/controllers/auth.controller.d.ts +2 -1
  9. package/dist/controllers/auth.controller.js +2 -3
  10. package/dist/databases/migrations/migration-runner.d.ts +3 -1
  11. package/dist/databases/migrations/migration-runner.js +27 -25
  12. package/dist/databases/mongo-db/migrations/mongo-initial-schema.d.ts +3 -2
  13. package/dist/databases/mongo-db/migrations/mongo-initial-schema.js +119 -110
  14. package/dist/databases/postgres/commands/postgres-batch-update.command.js +7 -7
  15. package/dist/databases/postgres/commands/postgres-create-many.command.js +4 -4
  16. package/dist/databases/postgres/commands/postgres-create.command.js +4 -4
  17. package/dist/databases/postgres/commands/postgres-full-update-by-id.command.js +13 -13
  18. package/dist/databases/postgres/commands/postgres-partial-update-by-id.command.js +7 -7
  19. package/dist/databases/postgres/commands/postgres-update.command.js +7 -7
  20. package/dist/databases/postgres/migrations/__tests__/test-migration-helper.js +2 -1
  21. package/dist/databases/postgres/migrations/postgres-initial-schema.d.ts +2 -1
  22. package/dist/databases/postgres/migrations/postgres-initial-schema.js +276 -266
  23. package/dist/databases/postgres/postgres.database.js +17 -17
  24. package/dist/databases/postgres/utils/build-select-clause.js +6 -6
  25. package/dist/databases/postgres/utils/does-table-exist.util.js +4 -4
  26. package/dist/models/base-api-config.interface.d.ts +7 -10
  27. package/dist/models/email-client.interface.d.ts +3 -0
  28. package/dist/models/email-client.interface.js +1 -0
  29. package/dist/models/email-config.interface.d.ts +4 -0
  30. package/dist/models/email-config.interface.js +1 -0
  31. package/dist/models/index.d.ts +1 -0
  32. package/dist/models/index.js +1 -0
  33. package/dist/models/multi-tenant-config.interface.d.ts +4 -0
  34. package/dist/models/multi-tenant-config.interface.js +1 -0
  35. package/dist/services/auth.service.d.ts +2 -1
  36. package/dist/services/auth.service.js +2 -2
  37. package/dist/services/email.service.d.ts +5 -4
  38. package/dist/services/email.service.js +12 -20
  39. package/package.json +88 -88
@@ -68,23 +68,23 @@ export class PostgresDatabase {
68
68
  }
69
69
  async getUserAuthorizations(userId, orgId) {
70
70
  const now = new Date();
71
- let query = `
72
- SELECT DISTINCT
73
- ur."userId" as "userId",
74
- r."name" as "role",
75
- f."name" as "feature",
76
- a."config",
77
- a."_id",
78
- a."_orgId"
79
- FROM "user_roles" ur
80
- INNER JOIN "roles" r ON ur."roleId" = r."_id"
81
- INNER JOIN "authorizations" a ON r."_id" = a."roleId"
82
- INNER JOIN "features" f ON a."featureId" = f."_id"
83
- WHERE ur."userId" = $1
84
- AND ur."_deleted" IS NULL
85
- AND a."_deleted" IS NULL
86
- AND (a."startDate" IS NULL OR a."startDate" <= $2)
87
- AND (a."endDate" IS NULL OR a."endDate" >= $2)
71
+ let query = `
72
+ SELECT DISTINCT
73
+ ur."userId" as "userId",
74
+ r."name" as "role",
75
+ f."name" as "feature",
76
+ a."config",
77
+ a."_id",
78
+ a."_orgId"
79
+ FROM "user_roles" ur
80
+ INNER JOIN "roles" r ON ur."roleId" = r."_id"
81
+ INNER JOIN "authorizations" a ON r."_id" = a."roleId"
82
+ INNER JOIN "features" f ON a."featureId" = f."_id"
83
+ WHERE ur."userId" = $1
84
+ AND ur."_deleted" IS NULL
85
+ AND a."_deleted" IS NULL
86
+ AND (a."startDate" IS NULL OR a."startDate" <= $2)
87
+ AND (a."endDate" IS NULL OR a."endDate" >= $2)
88
88
  `;
89
89
  const values = [userId, now];
90
90
  if (orgId) {
@@ -1,11 +1,11 @@
1
1
  import { Join } from '../../operations/join.operation.js';
2
2
  async function getTableColumns(client, tableName) {
3
- const result = await client.query(`
4
- SELECT column_name
5
- FROM information_schema.columns
6
- WHERE table_schema = current_schema()
7
- AND table_name = $1
8
- ORDER BY ordinal_position
3
+ const result = await client.query(`
4
+ SELECT column_name
5
+ FROM information_schema.columns
6
+ WHERE table_schema = current_schema()
7
+ AND table_name = $1
8
+ ORDER BY ordinal_position
9
9
  `, [tableName]);
10
10
  return result.rows.map(row => row.column_name);
11
11
  }
@@ -1,8 +1,8 @@
1
1
  export async function doesTableExist(client, tableName) {
2
- const result = await client.query(`
3
- SELECT EXISTS (
4
- SELECT 1 FROM information_schema.tables WHERE table_schema = current_schema() AND table_name = $1
5
- )
2
+ const result = await client.query(`
3
+ SELECT EXISTS (
4
+ SELECT 1 FROM information_schema.tables WHERE table_schema = current_schema() AND table_name = $1
5
+ )
6
6
  `, [tableName]);
7
7
  return result.rows[0].exists;
8
8
  }
@@ -1,16 +1,17 @@
1
1
  import { DbType } from "../databases/db-type.type.js";
2
2
  import { IAuthConfig } from "./auth-config.interface.js";
3
+ import { IEmailConfig } from "./email-config.interface.js";
4
+ import { IMultiTenantConfig } from "./multi-tenant-config.interface.js";
3
5
  export interface IBaseApiConfig {
4
6
  app: {
5
- dbType?: DbType;
7
+ dbType: DbType;
6
8
  isMultiTenant: boolean;
7
- metaOrgCode?: string;
8
- metaOrgName?: string;
9
+ isAuthEnabled: boolean;
9
10
  name: string;
10
11
  primaryTimezone?: string;
11
12
  };
12
13
  auth?: IAuthConfig;
13
- database?: {
14
+ database: {
14
15
  host: string;
15
16
  name: string;
16
17
  password: string;
@@ -20,13 +21,9 @@ export interface IBaseApiConfig {
20
21
  debug?: {
21
22
  showErrors?: boolean;
22
23
  };
23
- email?: {
24
- emailApiKey: string;
25
- emailApiSecret: string;
26
- fromAddress: string;
27
- systemEmailAddress: string;
28
- };
24
+ email?: IEmailConfig;
29
25
  env: string;
26
+ multiTenant?: IMultiTenantConfig;
30
27
  network: {
31
28
  corsAllowedOrigins: string[];
32
29
  externalPort?: number;
@@ -0,0 +1,3 @@
1
+ export interface IEmailClient {
2
+ sendHtmlEmail(toEmailAddress: string, subject: string, body: string): Promise<void>;
3
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,4 @@
1
+ export interface IEmailConfig {
2
+ fromAddress: string;
3
+ systemEmailAddress: string;
4
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -1,2 +1,3 @@
1
1
  export * from './base-api-config.interface.js';
2
2
  export * from './refresh-token.model.js';
3
+ export * from './email-client.interface.js';
@@ -1,2 +1,3 @@
1
1
  export * from './base-api-config.interface.js';
2
2
  export * from './refresh-token.model.js';
3
+ export * from './email-client.interface.js';
@@ -0,0 +1,4 @@
1
+ export interface IMultiTenantConfig {
2
+ metaOrgCode: string;
3
+ metaOrgName: string;
4
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -5,13 +5,14 @@ import { MultiTenantApiService } from './multi-tenant-api.service.js';
5
5
  import { UpdateResult } from '../databases/models/update-result.js';
6
6
  import { IRefreshToken } from '../models/refresh-token.model.js';
7
7
  import { IDatabase } from '../databases/models/index.js';
8
+ import { IEmailClient } from '../models/email-client.interface.js';
8
9
  export declare class AuthService extends MultiTenantApiService<IUser> {
9
10
  private refreshTokenService;
10
11
  private passwordResetTokenService;
11
12
  private emailService;
12
13
  private organizationService;
13
14
  private authConfig;
14
- constructor(database: IDatabase);
15
+ constructor(database: IDatabase, emailClient: IEmailClient);
15
16
  attemptLogin(req: Request, res: Response, email: string, password: string): Promise<ILoginResponse | null>;
16
17
  logUserIn(userContext: IUserContext, deviceId: string): Promise<{
17
18
  tokens: {
@@ -18,11 +18,11 @@ export class AuthService extends MultiTenantApiService {
18
18
  emailService;
19
19
  organizationService;
20
20
  authConfig;
21
- constructor(database) {
21
+ constructor(database, emailClient) {
22
22
  super(database, 'users', 'user', UserSpec);
23
23
  this.refreshTokenService = new GenericApiService(database, 'refreshTokens', 'refreshToken', refreshTokenModelSpec);
24
24
  this.passwordResetTokenService = new PasswordResetTokenService(database);
25
- this.emailService = new EmailService();
25
+ this.emailService = new EmailService(emailClient);
26
26
  this.organizationService = new OrganizationService(database);
27
27
  if (!config.auth) {
28
28
  throw new ServerError('Auth configuration is not set');
@@ -1,6 +1,7 @@
1
- import * as Mailjet from 'node-mailjet';
1
+ import { IEmailClient } from '../models/email-client.interface.js';
2
2
  export declare class EmailService {
3
- private mailjet;
4
- constructor();
5
- sendHtmlEmail(emailAddress: string, subject: string, body: string): Promise<Mailjet.LibraryResponse<import("node-mailjet/declarations/request/Request.js").RequestData>>;
3
+ private emailConfig;
4
+ private emailClient;
5
+ constructor(emailClient: IEmailClient);
6
+ sendHtmlEmail(emailAddress: string, subject: string, body: string): Promise<void>;
6
7
  }
@@ -1,29 +1,24 @@
1
- import * as Mailjet from 'node-mailjet';
2
1
  import { ServerError } from '../errors/index.js';
3
2
  import { config } from '../config/index.js';
4
3
  export class EmailService {
5
- mailjet = null;
6
- constructor() {
7
- if (config && config.email?.emailApiKey && config.email?.emailApiSecret) {
8
- this.mailjet = new Mailjet.default({
9
- apiKey: config.email.emailApiKey,
10
- apiSecret: config.email.emailApiSecret
11
- });
4
+ emailConfig;
5
+ emailClient;
6
+ constructor(emailClient) {
7
+ if (config.email) {
8
+ this.emailConfig = config.email;
12
9
  }
10
+ else {
11
+ throw new ServerError('Email configuration is not available. Email API credentials are not set in the config.');
12
+ }
13
+ this.emailClient = emailClient;
13
14
  }
14
15
  async sendHtmlEmail(emailAddress, subject, body) {
15
- if (!config || !config.email?.fromAddress) {
16
- throw new ServerError('Email configuration is not available. From address is not set in the config.');
17
- }
18
- if (!this.mailjet) {
19
- throw new ServerError('Email service is not configured. Email API credentials are not set in the config.');
20
- }
21
16
  const messageData = {
22
17
  Messages: [
23
18
  {
24
19
  From: {
25
- Email: config.email?.fromAddress,
26
- Name: config.app.name || 'Application'
20
+ Email: this.emailConfig.fromAddress,
21
+ Name: config.app.name
27
22
  },
28
23
  To: [
29
24
  {
@@ -36,11 +31,8 @@ export class EmailService {
36
31
  ]
37
32
  };
38
33
  try {
39
- const result = await this.mailjet
40
- .post('send', { version: 'v3.1' })
41
- .request(messageData);
34
+ await this.emailClient.sendHtmlEmail(emailAddress, subject, body);
42
35
  console.log(`Email sent to ${emailAddress} with subject ${subject}`);
43
- return result;
44
36
  }
45
37
  catch (error) {
46
38
  console.error('Error sending email:', error);
package/package.json CHANGED
@@ -1,88 +1,88 @@
1
- {
2
- "name": "@loomcore/api",
3
- "version": "0.1.60",
4
- "private": false,
5
- "description": "Loom Core Api - An opinionated Node.js api using Typescript, Express, and MongoDb or PostgreSQL",
6
- "scripts": {
7
- "clean": "rm -rf dist",
8
- "tsc": "tsc --project tsconfig.prod.json",
9
- "build": "npm-run-all -s clean tsc",
10
- "add": "git add .",
11
- "commit": "git commit -m \"Updates\"",
12
- "patch": "npm version patch",
13
- "push": "git push",
14
- "publishMe": "npm publish --access public",
15
- "pub": "npm-run-all -s add commit patch build push publishMe",
16
- "update-lib-versions": "npx --yes npm-check-updates -u -f @loomcore/common",
17
- "install-updated-libs": "npm i @loomcore/common",
18
- "update-libs": "npm-run-all -s update-lib-versions install-updated-libs",
19
- "typecheck": "tsc",
20
- "test": "npm-run-all -s test:postgres test:mongodb",
21
- "test:postgres": "cross-env NODE_ENV=test TEST_DATABASE=postgres vitest run",
22
- "test:mongodb": "cross-env NODE_ENV=test TEST_DATABASE=mongodb vitest run",
23
- "test:ci": "npm-run-all -s test:ci:postgres test:ci:mongodb",
24
- "test:ci:postgres": "cross-env NODE_ENV=test TEST_DATABASE=postgres vitest run --reporter=json --outputFile=test-results-postgres.json",
25
- "test:ci:mongodb": "cross-env NODE_ENV=test TEST_DATABASE=mongodb vitest run --reporter=json --outputFile=test-results-mongodb.json",
26
- "test:watch": "cross-env NODE_ENV=test vitest",
27
- "coverage": "npm-run-all -s coverage:postgres coverage:mongodb",
28
- "coverage:postgres": "cross-env NODE_ENV=test TEST_DATABASE=postgres vitest run --coverage",
29
- "coverage:mongodb": "cross-env NODE_ENV=test TEST_DATABASE=mongodb vitest run --coverage"
30
- },
31
- "author": "Tim Hardy",
32
- "license": "Apache 2.0",
33
- "main": "dist/index.js",
34
- "type": "module",
35
- "types": "dist/index.d.ts",
36
- "files": [
37
- "dist/**/*"
38
- ],
39
- "exports": {
40
- "./__tests__": "./dist/__tests__/index.js",
41
- "./config": "./dist/config/index.js",
42
- "./controllers": "./dist/controllers/index.js",
43
- "./databases": "./dist/databases/index.js",
44
- "./errors": "./dist/errors/index.js",
45
- "./middleware": "./dist/middleware/index.js",
46
- "./models": "./dist/models/index.js",
47
- "./services": "./dist/services/index.js",
48
- "./utils": "./dist/utils/index.js"
49
- },
50
- "dependencies": {
51
- "jsonwebtoken": "^9.0.2",
52
- "node-mailjet": "^6.0.8",
53
- "qs": "^6.14.1"
54
- },
55
- "peerDependencies": {
56
- "@loomcore/common": "^0.0.43",
57
- "@sinclair/typebox": "0.34.33",
58
- "cookie-parser": "^1.4.6",
59
- "cors": "^2.8.5",
60
- "express": "^5.1.0",
61
- "lodash": "^4.17.21",
62
- "moment": "^2.30.1",
63
- "mongodb": "^6.16.0",
64
- "pg": "^8.15.6",
65
- "rxjs": "^7.8.0",
66
- "umzug": "^3.8.2"
67
- },
68
- "devDependencies": {
69
- "@types/cookie-parser": "^1.4.7",
70
- "@types/cors": "^2.8.18",
71
- "@types/express": "^5.0.1",
72
- "@types/jsonwebtoken": "^9.0.9",
73
- "@types/lodash": "^4.17.13",
74
- "@types/pg": "^8.15.6",
75
- "@types/qs": "^6.14.0",
76
- "@types/supertest": "^6.0.3",
77
- "@vitest/coverage-v8": "^3.0.9",
78
- "cross-env": "^7.0.3",
79
- "mongodb-memory-server": "^9.3.0",
80
- "npm-run-all": "^4.1.5",
81
- "pg-mem": "^3.0.5",
82
- "rxjs": "^7.8.0",
83
- "supertest": "^7.1.0",
84
- "typescript": "^5.8.3",
85
- "vite": "^6.2.5",
86
- "vitest": "^3.0.9"
87
- }
88
- }
1
+ {
2
+ "name": "@loomcore/api",
3
+ "version": "0.1.63",
4
+ "private": false,
5
+ "description": "Loom Core Api - An opinionated Node.js api using Typescript, Express, and MongoDb or PostgreSQL",
6
+ "scripts": {
7
+ "clean": "rm -rf dist",
8
+ "tsc": "tsc --project tsconfig.prod.json",
9
+ "build": "npm-run-all -s clean tsc",
10
+ "add": "git add .",
11
+ "commit": "git commit -m \"Updates\"",
12
+ "patch": "npm version patch",
13
+ "push": "git push",
14
+ "publishMe": "npm publish --access public",
15
+ "pub": "npm-run-all -s add commit patch build push publishMe",
16
+ "update-lib-versions": "npx --yes npm-check-updates -u -f @loomcore/common",
17
+ "install-updated-libs": "npm i @loomcore/common",
18
+ "update-libs": "npm-run-all -s update-lib-versions install-updated-libs",
19
+ "typecheck": "tsc",
20
+ "test": "npm-run-all -s test:postgres test:mongodb",
21
+ "test:postgres": "cross-env NODE_ENV=test TEST_DATABASE=postgres vitest run",
22
+ "test:mongodb": "cross-env NODE_ENV=test TEST_DATABASE=mongodb vitest run",
23
+ "test:ci": "npm-run-all -s test:ci:postgres test:ci:mongodb",
24
+ "test:ci:postgres": "cross-env NODE_ENV=test TEST_DATABASE=postgres vitest run --reporter=json --outputFile=test-results-postgres.json",
25
+ "test:ci:mongodb": "cross-env NODE_ENV=test TEST_DATABASE=mongodb vitest run --reporter=json --outputFile=test-results-mongodb.json",
26
+ "test:watch": "cross-env NODE_ENV=test vitest",
27
+ "coverage": "npm-run-all -s coverage:postgres coverage:mongodb",
28
+ "coverage:postgres": "cross-env NODE_ENV=test TEST_DATABASE=postgres vitest run --coverage",
29
+ "coverage:mongodb": "cross-env NODE_ENV=test TEST_DATABASE=mongodb vitest run --coverage"
30
+ },
31
+ "author": "Tim Hardy",
32
+ "license": "Apache 2.0",
33
+ "main": "dist/index.js",
34
+ "type": "module",
35
+ "types": "dist/index.d.ts",
36
+ "files": [
37
+ "dist/**/*"
38
+ ],
39
+ "exports": {
40
+ "./__tests__": "./dist/__tests__/index.js",
41
+ "./config": "./dist/config/index.js",
42
+ "./controllers": "./dist/controllers/index.js",
43
+ "./databases": "./dist/databases/index.js",
44
+ "./errors": "./dist/errors/index.js",
45
+ "./middleware": "./dist/middleware/index.js",
46
+ "./models": "./dist/models/index.js",
47
+ "./services": "./dist/services/index.js",
48
+ "./utils": "./dist/utils/index.js"
49
+ },
50
+ "dependencies": {
51
+ "jsonwebtoken": "^9.0.2",
52
+ "node-mailjet": "^6.0.8",
53
+ "qs": "^6.14.1"
54
+ },
55
+ "peerDependencies": {
56
+ "@loomcore/common": "^0.0.43",
57
+ "@sinclair/typebox": "0.34.33",
58
+ "cookie-parser": "^1.4.6",
59
+ "cors": "^2.8.5",
60
+ "express": "^5.1.0",
61
+ "lodash": "^4.17.21",
62
+ "moment": "^2.30.1",
63
+ "mongodb": "^6.16.0",
64
+ "pg": "^8.15.6",
65
+ "rxjs": "^7.8.0",
66
+ "umzug": "^3.8.2"
67
+ },
68
+ "devDependencies": {
69
+ "@types/cookie-parser": "^1.4.7",
70
+ "@types/cors": "^2.8.18",
71
+ "@types/express": "^5.0.1",
72
+ "@types/jsonwebtoken": "^9.0.9",
73
+ "@types/lodash": "^4.17.13",
74
+ "@types/pg": "^8.15.6",
75
+ "@types/qs": "^6.14.0",
76
+ "@types/supertest": "^6.0.3",
77
+ "@vitest/coverage-v8": "^3.0.9",
78
+ "cross-env": "^7.0.3",
79
+ "mongodb-memory-server": "^9.3.0",
80
+ "npm-run-all": "^4.1.5",
81
+ "pg-mem": "^3.0.5",
82
+ "rxjs": "^7.8.0",
83
+ "supertest": "^7.1.0",
84
+ "typescript": "^5.8.3",
85
+ "vite": "^6.2.5",
86
+ "vitest": "^3.0.9"
87
+ }
88
+ }