@loomcore/api 0.2.42 → 0.2.45
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 +201 -201
- package/dist/__tests__/common-test.utils.d.ts +13 -13
- package/dist/__tests__/common-test.utils.js +72 -72
- package/dist/__tests__/models/category.model.d.ts +1 -1
- package/dist/__tests__/models/category.model.js +2 -2
- package/dist/__tests__/models/product-with-category.model.d.ts +3 -3
- package/dist/__tests__/models/product-with-category.model.js +4 -4
- package/dist/__tests__/models/product.model.d.ts +2 -2
- package/dist/__tests__/models/product.model.js +2 -2
- package/dist/__tests__/models/test-entity.model.d.ts +2 -2
- package/dist/__tests__/models/test-entity.model.js +2 -2
- package/dist/__tests__/models/test-item.model.d.ts +1 -1
- package/dist/__tests__/models/test-item.model.js +3 -3
- package/dist/__tests__/mongo-db.test-database.js +1 -1
- package/dist/__tests__/postgres-test-migrations/postgres-test-schema.d.ts +2 -2
- package/dist/__tests__/postgres-test-migrations/postgres-test-schema.js +38 -38
- package/dist/__tests__/postgres.test-database.d.ts +2 -2
- package/dist/__tests__/postgres.test-database.js +25 -25
- package/dist/__tests__/test-database.interface.d.ts +1 -1
- package/dist/__tests__/test-email-client.d.ts +1 -1
- package/dist/__tests__/test-express-app.d.ts +3 -3
- package/dist/__tests__/test-express-app.js +12 -12
- package/dist/__tests__/test-objects.d.ts +1 -1
- package/dist/__tests__/test-objects.js +26 -26
- package/dist/config/base-api-config.d.ts +2 -2
- package/dist/config/base-api-config.js +17 -17
- package/dist/controllers/auth.controller.d.ts +4 -4
- package/dist/controllers/auth.controller.js +32 -32
- package/dist/controllers/authorizations.controller.d.ts +4 -4
- package/dist/controllers/authorizations.controller.js +4 -4
- package/dist/controllers/features.controller.d.ts +4 -4
- package/dist/controllers/features.controller.js +4 -4
- package/dist/controllers/index.d.ts +10 -10
- package/dist/controllers/index.js +10 -10
- package/dist/controllers/organizations.controller.d.ts +5 -5
- package/dist/controllers/organizations.controller.js +10 -10
- package/dist/controllers/roles.controller.d.ts +4 -4
- package/dist/controllers/roles.controller.js +4 -4
- package/dist/controllers/types.d.ts +2 -2
- package/dist/controllers/user-roles.controller.d.ts +4 -4
- package/dist/controllers/user-roles.controller.js +4 -4
- package/dist/controllers/users.controller.d.ts +5 -5
- package/dist/controllers/users.controller.js +4 -4
- package/dist/databases/models/database.interface.d.ts +5 -5
- package/dist/databases/mongo-db/commands/mongo-batch-update.command.d.ts +3 -3
- package/dist/databases/mongo-db/commands/mongo-batch-update.command.js +5 -5
- package/dist/databases/mongo-db/commands/mongo-create-many.command.d.ts +1 -1
- package/dist/databases/mongo-db/commands/mongo-create-many.command.js +1 -1
- package/dist/databases/mongo-db/commands/mongo-create.command.d.ts +1 -1
- package/dist/databases/mongo-db/commands/mongo-create.command.js +1 -1
- package/dist/databases/mongo-db/commands/mongo-delete-by-id.command.d.ts +3 -3
- package/dist/databases/mongo-db/commands/mongo-delete-by-id.command.js +4 -4
- package/dist/databases/mongo-db/commands/mongo-delete-many.command.d.ts +3 -3
- package/dist/databases/mongo-db/commands/mongo-delete-many.command.js +2 -2
- package/dist/databases/mongo-db/commands/mongo-full-updateby-id.command.d.ts +3 -3
- package/dist/databases/mongo-db/commands/mongo-full-updateby-id.command.js +4 -4
- package/dist/databases/mongo-db/commands/mongo-partial-update-by-id.command.d.ts +3 -3
- package/dist/databases/mongo-db/commands/mongo-partial-update-by-id.command.js +4 -4
- package/dist/databases/mongo-db/commands/mongo-update.command.d.ts +3 -3
- package/dist/databases/mongo-db/commands/mongo-update.command.js +3 -3
- package/dist/databases/mongo-db/migrations/mongo-initial-schema.d.ts +2 -2
- package/dist/databases/mongo-db/migrations/mongo-initial-schema.js +108 -108
- package/dist/databases/mongo-db/models/no-sql-pipeline.d.ts +4 -4
- package/dist/databases/mongo-db/models/no-sql-pipeline.interface.d.ts +3 -3
- package/dist/databases/mongo-db/models/no-sql-pipeline.js +1 -1
- package/dist/databases/mongo-db/mongo-db.database.d.ts +7 -7
- package/dist/databases/mongo-db/mongo-db.database.js +5 -5
- package/dist/databases/mongo-db/queries/mongo-find-one.query.d.ts +2 -2
- package/dist/databases/mongo-db/queries/mongo-find-one.query.js +1 -1
- package/dist/databases/mongo-db/queries/mongo-find.query.d.ts +2 -2
- package/dist/databases/mongo-db/queries/mongo-find.query.js +1 -1
- package/dist/databases/mongo-db/queries/mongo-get-all.query.d.ts +2 -2
- package/dist/databases/mongo-db/queries/mongo-get-all.query.js +1 -1
- package/dist/databases/mongo-db/queries/mongo-get-by-id.query.d.ts +4 -4
- package/dist/databases/mongo-db/queries/mongo-get-by-id.query.js +3 -3
- package/dist/databases/mongo-db/queries/mongo-get-count.query.d.ts +1 -1
- package/dist/databases/mongo-db/queries/mongo-get.query.d.ts +3 -3
- package/dist/databases/mongo-db/queries/mongo-get.query.js +2 -2
- package/dist/databases/mongo-db/utils/build-find-options.util.d.ts +2 -2
- package/dist/databases/mongo-db/utils/build-mongo-url.util.d.ts +1 -1
- package/dist/databases/mongo-db/utils/build-mongo-url.util.js +4 -4
- package/dist/databases/operations/__tests__/models/test-agent.model.d.ts +2 -2
- package/dist/databases/operations/__tests__/models/test-agent.model.js +3 -3
- package/dist/databases/operations/__tests__/models/test-client-report.model.d.ts +4 -4
- package/dist/databases/operations/__tests__/models/test-client-report.model.js +5 -5
- package/dist/databases/operations/__tests__/models/test-district.model.d.ts +2 -2
- package/dist/databases/operations/__tests__/models/test-district.model.js +2 -2
- package/dist/databases/operations/__tests__/models/test-email-address.model.d.ts +1 -1
- package/dist/databases/operations/__tests__/models/test-email-address.model.js +2 -2
- package/dist/databases/operations/__tests__/models/test-person.model.d.ts +4 -4
- package/dist/databases/operations/__tests__/models/test-person.model.js +4 -4
- package/dist/databases/operations/__tests__/models/test-phone-number.model.d.ts +1 -1
- package/dist/databases/operations/__tests__/models/test-phone-number.model.js +2 -2
- package/dist/databases/operations/__tests__/models/test-policy.model.d.ts +3 -3
- package/dist/databases/operations/__tests__/models/test-policy.model.js +4 -4
- package/dist/databases/operations/__tests__/models/test-premium.model.d.ts +1 -1
- package/dist/databases/operations/__tests__/models/test-premium.model.js +2 -2
- package/dist/databases/operations/__tests__/models/test-school.model.d.ts +2 -2
- package/dist/databases/operations/__tests__/models/test-school.model.js +2 -2
- package/dist/databases/operations/__tests__/models/test-state.model.d.ts +1 -1
- package/dist/databases/operations/__tests__/models/test-state.model.js +2 -2
- package/dist/databases/operations/operation.d.ts +3 -3
- package/dist/databases/postgres/commands/postgres-batch-update.command.d.ts +1 -1
- package/dist/databases/postgres/commands/postgres-batch-update.command.js +11 -11
- package/dist/databases/postgres/commands/postgres-create-many.command.js +5 -5
- package/dist/databases/postgres/commands/postgres-create.command.d.ts +3 -3
- package/dist/databases/postgres/commands/postgres-create.command.js +5 -5
- package/dist/databases/postgres/commands/postgres-delete-by-id.command.d.ts +1 -1
- package/dist/databases/postgres/commands/postgres-delete-by-id.command.js +1 -1
- package/dist/databases/postgres/commands/postgres-delete-many.command.d.ts +2 -2
- package/dist/databases/postgres/commands/postgres-delete-many.command.js +1 -1
- package/dist/databases/postgres/commands/postgres-full-update-by-id.command.d.ts +1 -1
- package/dist/databases/postgres/commands/postgres-full-update-by-id.command.js +4 -4
- package/dist/databases/postgres/commands/postgres-partial-update-by-id.command.d.ts +1 -1
- package/dist/databases/postgres/commands/postgres-partial-update-by-id.command.js +11 -11
- package/dist/databases/postgres/commands/postgres-update.command.d.ts +2 -2
- package/dist/databases/postgres/commands/postgres-update.command.js +11 -11
- package/dist/databases/postgres/migrations/postgres-initial-schema.d.ts +2 -2
- package/dist/databases/postgres/migrations/postgres-initial-schema.js +69 -69
- package/dist/databases/postgres/postgres.database.d.ts +6 -6
- package/dist/databases/postgres/postgres.database.js +18 -18
- package/dist/databases/postgres/queries/postgres-find-one.query.d.ts +1 -1
- package/dist/databases/postgres/queries/postgres-find-one.query.js +2 -2
- package/dist/databases/postgres/queries/postgres-find.query.d.ts +1 -1
- package/dist/databases/postgres/queries/postgres-find.query.js +2 -2
- package/dist/databases/postgres/queries/postgres-get-all.query.d.ts +1 -1
- package/dist/databases/postgres/queries/postgres-get-all.query.js +3 -3
- package/dist/databases/postgres/queries/postgres-get-by-id.query.d.ts +1 -1
- package/dist/databases/postgres/queries/postgres-get-by-id.query.js +5 -5
- package/dist/databases/postgres/queries/postgres-get-count.query.js +1 -1
- package/dist/databases/postgres/queries/postgres-get.query.d.ts +2 -2
- package/dist/databases/postgres/queries/postgres-get.query.js +9 -9
- package/dist/databases/postgres/utils/build-count-query.d.ts +1 -1
- package/dist/databases/postgres/utils/build-count-query.js +1 -1
- package/dist/databases/postgres/utils/build-join-clauses.d.ts +1 -1
- package/dist/databases/postgres/utils/build-join-clauses.js +7 -7
- package/dist/databases/postgres/utils/build-order-by-clause.d.ts +1 -1
- package/dist/databases/postgres/utils/build-pagination-clause.d.ts +1 -1
- package/dist/databases/postgres/utils/build-postgres-url.util.d.ts +1 -1
- package/dist/databases/postgres/utils/build-postgres-url.util.js +2 -2
- package/dist/databases/postgres/utils/build-select-clause.js +9 -9
- package/dist/databases/postgres/utils/build-where-clause.d.ts +1 -1
- package/dist/databases/postgres/utils/build-where-clause.js +1 -1
- package/dist/databases/postgres/utils/columns-and-values-from-entity.d.ts +1 -1
- package/dist/databases/postgres/utils/does-table-exist.util.d.ts +1 -1
- package/dist/databases/postgres/utils/does-table-exist.util.js +5 -5
- package/dist/decorators/index.d.ts +1 -0
- package/dist/decorators/index.js +1 -0
- package/dist/middleware/authorize/authenticate-request.d.ts +2 -2
- package/dist/middleware/authorize/authenticate-request.js +5 -5
- package/dist/middleware/index.d.ts +4 -4
- package/dist/middleware/index.js +4 -4
- package/dist/models/app-config.interface.d.ts +1 -1
- package/dist/models/base-api-config.interface.d.ts +6 -6
- package/dist/models/initial-database-config.interface.d.ts +3 -3
- package/dist/models/log-level.type.d.ts +1 -1
- package/dist/services/authorizations.service.d.ts +3 -3
- package/dist/services/authorizations.service.js +7 -7
- package/dist/services/email.service.js +5 -5
- package/dist/services/features.service.d.ts +3 -3
- package/dist/services/features.service.js +4 -4
- package/dist/services/generic-api-service/generic-api-service.interface.d.ts +6 -6
- package/dist/services/generic-api-service/generic-api.service.d.ts +8 -8
- package/dist/services/generic-api-service/generic-api.service.js +7 -7
- package/dist/services/index.d.ts +16 -16
- package/dist/services/index.js +16 -16
- package/dist/services/multi-tenant-api.service.d.ts +4 -4
- package/dist/services/multi-tenant-api.service.js +9 -9
- package/dist/services/organization-domain.service.d.ts +3 -3
- package/dist/services/organization-domain.service.js +3 -3
- package/dist/services/organization.service.d.ts +4 -4
- package/dist/services/organization.service.js +7 -7
- package/dist/services/password-reset-token.service.d.ts +3 -3
- package/dist/services/password-reset-token.service.js +5 -5
- package/dist/services/refresh-token.service.d.ts +3 -3
- package/dist/services/refresh-token.service.js +3 -3
- package/dist/services/roles.service.d.ts +3 -3
- package/dist/services/roles.service.js +4 -4
- package/dist/services/tenant-query-decorator.d.ts +1 -1
- package/dist/services/tenant-query-decorator.js +8 -8
- package/dist/services/user-roles.service.d.ts +3 -3
- package/dist/services/user-roles.service.js +4 -4
- package/dist/services/user.service.d.ts +4 -4
- package/dist/services/user.service.js +11 -11
- package/dist/services/utils/audit-for-create.util.d.ts +1 -1
- package/dist/services/utils/audit-for-create.util.js +2 -2
- package/dist/services/utils/audit-for-update.util.d.ts +1 -1
- package/dist/services/utils/audit-for-update.util.js +2 -2
- package/dist/services/utils/strip-sender-provided-system-properties.util.d.ts +1 -1
- package/dist/services/utils/strip-sender-provided-system-properties.util.js +4 -4
- package/dist/utils/auth/assert-admin-or-feature.util.d.ts +1 -1
- package/dist/utils/auth/assert-admin-or-feature.util.js +2 -2
- package/dist/utils/auth/assert-admin.util.d.ts +1 -1
- package/dist/utils/auth/assert-admin.util.js +2 -2
- package/dist/utils/auth/assert-owner-or-admin.util.d.ts +1 -1
- package/dist/utils/auth/assert-owner-or-admin.util.js +2 -2
- package/dist/utils/auth/assert-user-has-feature.util.d.ts +1 -1
- package/dist/utils/auth/assert-user-has-feature.util.js +1 -1
- package/dist/utils/auth/assert-user-or-admin.util.d.ts +2 -2
- package/dist/utils/auth/assert-user-or-admin.util.js +2 -2
- package/dist/utils/auth/attempt-login.util.d.ts +4 -4
- package/dist/utils/auth/attempt-login.util.js +8 -8
- package/dist/utils/auth/auth-user-specs.util.d.ts +1 -1
- package/dist/utils/auth/auth-user-specs.util.js +3 -3
- package/dist/utils/auth/change-password.util.d.ts +4 -4
- package/dist/utils/auth/change-password.util.js +2 -2
- package/dist/utils/auth/create-new-refresh-token.util.d.ts +3 -3
- package/dist/utils/auth/create-new-refresh-token.util.js +8 -8
- package/dist/utils/auth/create-new-tokens.util.d.ts +1 -1
- package/dist/utils/auth/create-new-tokens.util.js +3 -3
- package/dist/utils/auth/generate-device-id.util.js +2 -2
- package/dist/utils/auth/generate-refresh-token.util.js +2 -2
- package/dist/utils/auth/get-active-refresh-token.util.d.ts +2 -2
- package/dist/utils/auth/get-active-refresh-token.util.js +1 -1
- package/dist/utils/auth/get-and-set-device-id-cookie.util.d.ts +1 -1
- package/dist/utils/auth/get-and-set-device-id-cookie.util.js +4 -4
- package/dist/utils/auth/get-auth-config.util.d.ts +1 -1
- package/dist/utils/auth/get-auth-config.util.js +3 -3
- package/dist/utils/auth/get-device-id-from-cookie.util.d.ts +1 -1
- package/dist/utils/auth/index.d.ts +26 -26
- package/dist/utils/auth/index.js +26 -26
- package/dist/utils/auth/is-admin.util.d.ts +1 -1
- package/dist/utils/auth/is-admin.util.js +1 -1
- package/dist/utils/auth/log-user-in.util.d.ts +3 -3
- package/dist/utils/auth/log-user-in.util.js +5 -5
- package/dist/utils/auth/request-token-using-refresh-token.util.d.ts +4 -4
- package/dist/utils/auth/request-token-using-refresh-token.util.js +7 -7
- package/dist/utils/auth/reset-password.util.d.ts +4 -4
- package/dist/utils/auth/reset-password.util.js +8 -8
- package/dist/utils/auth/send-reset-password-email.util.d.ts +2 -2
- package/dist/utils/auth/send-reset-password-email.util.js +5 -5
- package/dist/utils/auth/update-last-logged-in.util.d.ts +3 -3
- package/dist/utils/auth/update-last-logged-in.util.js +3 -3
- package/dist/utils/create-public-spec.utils.d.ts +1 -1
- package/dist/utils/create-public-spec.utils.js +2 -2
- package/dist/utils/express.utils.d.ts +4 -4
- package/dist/utils/express.utils.js +5 -5
- package/dist/utils/index.d.ts +10 -10
- package/dist/utils/index.js +10 -10
- package/dist/utils/jwt.utils.d.ts +1 -1
- package/dist/utils/jwt.utils.js +3 -3
- package/dist/utils/logger.utils.d.ts +1 -1
- package/dist/utils/logger.utils.js +10 -10
- package/dist/utils/password.utils.js +7 -7
- package/package.json +2 -1
|
@@ -1,25 +1,25 @@
|
|
|
1
|
-
import { EmptyUserContext, getSystemUserContext, initializeSystemUserContext, isSystemUserContextInitialized, } from
|
|
2
|
-
import { OrganizationService } from
|
|
3
|
-
import { passwordUtils } from
|
|
4
|
-
import { PostgresDatabase } from
|
|
1
|
+
import { EmptyUserContext, getSystemUserContext, initializeSystemUserContext, isSystemUserContextInitialized, } from '@loomcore/common/models';
|
|
2
|
+
import { OrganizationService } from '../../../services/index.js';
|
|
3
|
+
import { passwordUtils } from '../../../utils/index.js';
|
|
4
|
+
import { PostgresDatabase } from '../postgres.database.js';
|
|
5
5
|
export const getPostgresInitialSchema = (dbConfig) => {
|
|
6
6
|
const migrations = [];
|
|
7
7
|
const isMultiTenant = dbConfig.app.isMultiTenant;
|
|
8
8
|
if (isMultiTenant && !dbConfig.multiTenant) {
|
|
9
|
-
throw new Error(
|
|
9
|
+
throw new Error('Multi-tenant configuration is enabled but multi-tenant configuration is not provided');
|
|
10
10
|
}
|
|
11
11
|
const isAuthEnabled = dbConfig.app.isAuthEnabled;
|
|
12
12
|
const dbName = dbConfig.database.name.replace(/"/g, '""');
|
|
13
13
|
migrations.push({
|
|
14
|
-
name:
|
|
14
|
+
name: '00000000000001_system-configurations',
|
|
15
15
|
up: async ({ context: pool }) => {
|
|
16
|
-
if (dbConfig.env ===
|
|
16
|
+
if (dbConfig.env === 'test') {
|
|
17
17
|
return;
|
|
18
18
|
}
|
|
19
19
|
await pool.query(`ALTER DATABASE "${dbName}" SET statement_timeout = '60s'`);
|
|
20
20
|
},
|
|
21
21
|
down: async ({ context: pool }) => {
|
|
22
|
-
if (dbConfig.env ===
|
|
22
|
+
if (dbConfig.env === 'test') {
|
|
23
23
|
return;
|
|
24
24
|
}
|
|
25
25
|
await pool.query(`ALTER DATABASE "${dbName}" RESET statement_timeout`);
|
|
@@ -27,7 +27,7 @@ export const getPostgresInitialSchema = (dbConfig) => {
|
|
|
27
27
|
});
|
|
28
28
|
if (isMultiTenant) {
|
|
29
29
|
migrations.push({
|
|
30
|
-
name:
|
|
30
|
+
name: '00000000000002_schema-organizations',
|
|
31
31
|
up: async ({ context: pool }) => {
|
|
32
32
|
await pool.query(`
|
|
33
33
|
CREATE TABLE IF NOT EXISTS "organizations" (
|
|
@@ -52,23 +52,23 @@ export const getPostgresInitialSchema = (dbConfig) => {
|
|
|
52
52
|
},
|
|
53
53
|
});
|
|
54
54
|
migrations.push({
|
|
55
|
-
name:
|
|
55
|
+
name: '00000000000002_schema-organizations-domains',
|
|
56
56
|
up: async ({ context: pool }) => {
|
|
57
57
|
await pool.query(`
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
58
|
+
CREATE TABLE IF NOT EXISTS "organization_domains" (
|
|
59
|
+
"_id" INTEGER GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
|
|
60
|
+
"organization_id" INTEGER NOT NULL,
|
|
61
|
+
"domain" VARCHAR(255) NOT NULL UNIQUE,
|
|
62
|
+
"_created" TIMESTAMPTZ NOT NULL,
|
|
63
|
+
"_createdBy" INTEGER NOT NULL,
|
|
64
|
+
"_updated" TIMESTAMPTZ,
|
|
65
|
+
"_updatedBy" INTEGER,
|
|
66
|
+
"_deleted" TIMESTAMPTZ,
|
|
67
|
+
"_deletedBy" INTEGER,
|
|
68
|
+
CONSTRAINT "fk_organization_domains_organization"
|
|
69
|
+
FOREIGN KEY("organization_id") REFERENCES "organizations"("_id") ON DELETE CASCADE
|
|
70
|
+
)
|
|
71
|
+
`);
|
|
72
72
|
},
|
|
73
73
|
down: async ({ context: pool }) => {
|
|
74
74
|
await pool.query('DROP TABLE IF EXISTS "organization_domains"');
|
|
@@ -77,9 +77,9 @@ export const getPostgresInitialSchema = (dbConfig) => {
|
|
|
77
77
|
}
|
|
78
78
|
if (isAuthEnabled)
|
|
79
79
|
migrations.push({
|
|
80
|
-
name:
|
|
80
|
+
name: '00000000000003_schema-users',
|
|
81
81
|
up: async ({ context: pool }) => {
|
|
82
|
-
const orgColumnDef = isMultiTenant ? '"_orgId" INTEGER NOT NULL,' :
|
|
82
|
+
const orgColumnDef = isMultiTenant ? '"_orgId" INTEGER NOT NULL,' : '';
|
|
83
83
|
const uniqueConstraint = isMultiTenant
|
|
84
84
|
? 'CONSTRAINT "uk_users_email" UNIQUE ("_orgId", "email")'
|
|
85
85
|
: 'CONSTRAINT "uk_users_email" UNIQUE ("email")';
|
|
@@ -87,7 +87,7 @@ export const getPostgresInitialSchema = (dbConfig) => {
|
|
|
87
87
|
CREATE TABLE IF NOT EXISTS "users"(
|
|
88
88
|
"_id" INTEGER GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
|
|
89
89
|
${orgColumnDef}
|
|
90
|
-
|
|
90
|
+
"external_id" VARCHAR(255) UNIQUE,
|
|
91
91
|
"email" VARCHAR(255) NOT NULL,
|
|
92
92
|
"display_name" VARCHAR(255),
|
|
93
93
|
"password" VARCHAR(255) NOT NULL,
|
|
@@ -112,9 +112,9 @@ export const getPostgresInitialSchema = (dbConfig) => {
|
|
|
112
112
|
});
|
|
113
113
|
if (isAuthEnabled)
|
|
114
114
|
migrations.push({
|
|
115
|
-
name:
|
|
115
|
+
name: '00000000000004_schema-refresh-tokens',
|
|
116
116
|
up: async ({ context: pool }) => {
|
|
117
|
-
const orgColumnDef = isMultiTenant ? '"_orgId" INTEGER NOT NULL,' :
|
|
117
|
+
const orgColumnDef = isMultiTenant ? '"_orgId" INTEGER NOT NULL,' : '';
|
|
118
118
|
await pool.query(`
|
|
119
119
|
CREATE TABLE IF NOT EXISTS "refresh_tokens"(
|
|
120
120
|
"_id" INTEGER GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
|
|
@@ -135,9 +135,9 @@ export const getPostgresInitialSchema = (dbConfig) => {
|
|
|
135
135
|
});
|
|
136
136
|
if (isAuthEnabled)
|
|
137
137
|
migrations.push({
|
|
138
|
-
name:
|
|
138
|
+
name: '00000000000005_schema-password-reset-tokens',
|
|
139
139
|
up: async ({ context: pool }) => {
|
|
140
|
-
const orgColumnDef = isMultiTenant ? '"_orgId" INTEGER NOT NULL,' :
|
|
140
|
+
const orgColumnDef = isMultiTenant ? '"_orgId" INTEGER NOT NULL,' : '';
|
|
141
141
|
const uniqueConstraint = isMultiTenant
|
|
142
142
|
? 'CONSTRAINT "uk_passwordResetTokens_email" UNIQUE ("_orgId", "email")'
|
|
143
143
|
: 'CONSTRAINT "uk_passwordResetTokens_email" UNIQUE ("email")';
|
|
@@ -164,9 +164,9 @@ export const getPostgresInitialSchema = (dbConfig) => {
|
|
|
164
164
|
});
|
|
165
165
|
if (isAuthEnabled)
|
|
166
166
|
migrations.push({
|
|
167
|
-
name:
|
|
167
|
+
name: '00000000000006_schema-roles',
|
|
168
168
|
up: async ({ context: pool }) => {
|
|
169
|
-
const orgColumnDef = isMultiTenant ? '"_orgId" INTEGER NOT NULL,' :
|
|
169
|
+
const orgColumnDef = isMultiTenant ? '"_orgId" INTEGER NOT NULL,' : '';
|
|
170
170
|
const uniqueConstraint = isMultiTenant
|
|
171
171
|
? 'CONSTRAINT "uk_roles_name" UNIQUE ("_orgId", "name")'
|
|
172
172
|
: 'CONSTRAINT "uk_roles_name" UNIQUE ("name")';
|
|
@@ -186,9 +186,9 @@ export const getPostgresInitialSchema = (dbConfig) => {
|
|
|
186
186
|
});
|
|
187
187
|
if (isAuthEnabled)
|
|
188
188
|
migrations.push({
|
|
189
|
-
name:
|
|
189
|
+
name: '00000000000007_schema-user-roles',
|
|
190
190
|
up: async ({ context: pool }) => {
|
|
191
|
-
const orgColumnDef = isMultiTenant ? '"_orgId" INTEGER NOT NULL,' :
|
|
191
|
+
const orgColumnDef = isMultiTenant ? '"_orgId" INTEGER NOT NULL,' : '';
|
|
192
192
|
const uniqueConstraint = isMultiTenant
|
|
193
193
|
? 'CONSTRAINT "uk_user_roles" UNIQUE ("_orgId", "user_id", "role_id")'
|
|
194
194
|
: 'CONSTRAINT "uk_user_roles" UNIQUE ("user_id", "role_id")';
|
|
@@ -216,9 +216,9 @@ export const getPostgresInitialSchema = (dbConfig) => {
|
|
|
216
216
|
});
|
|
217
217
|
if (isAuthEnabled)
|
|
218
218
|
migrations.push({
|
|
219
|
-
name:
|
|
219
|
+
name: '00000000000008_schema-features',
|
|
220
220
|
up: async ({ context: pool }) => {
|
|
221
|
-
const orgColumnDef = isMultiTenant ? '"_orgId" INTEGER NOT NULL,' :
|
|
221
|
+
const orgColumnDef = isMultiTenant ? '"_orgId" INTEGER NOT NULL,' : '';
|
|
222
222
|
const uniqueConstraint = isMultiTenant
|
|
223
223
|
? 'CONSTRAINT "uk_features" UNIQUE ("_orgId", "name")'
|
|
224
224
|
: 'CONSTRAINT "uk_features" UNIQUE ("name")';
|
|
@@ -238,9 +238,9 @@ export const getPostgresInitialSchema = (dbConfig) => {
|
|
|
238
238
|
});
|
|
239
239
|
if (isAuthEnabled)
|
|
240
240
|
migrations.push({
|
|
241
|
-
name:
|
|
241
|
+
name: '00000000000009_schema-authorizations',
|
|
242
242
|
up: async ({ context: pool }) => {
|
|
243
|
-
const orgColumnDef = isMultiTenant ? '"_orgId" INTEGER NOT NULL,' :
|
|
243
|
+
const orgColumnDef = isMultiTenant ? '"_orgId" INTEGER NOT NULL,' : '';
|
|
244
244
|
const uniqueConstraint = isMultiTenant
|
|
245
245
|
? 'CONSTRAINT "uk_authorizations" UNIQUE ("_orgId", "role_id", "feature_id")'
|
|
246
246
|
: 'CONSTRAINT "uk_authorizations" UNIQUE ("role_id", "feature_id")';
|
|
@@ -271,28 +271,28 @@ export const getPostgresInitialSchema = (dbConfig) => {
|
|
|
271
271
|
});
|
|
272
272
|
if (isMultiTenant) {
|
|
273
273
|
migrations.push({
|
|
274
|
-
name:
|
|
274
|
+
name: '00000000000010_data-meta-org',
|
|
275
275
|
up: async ({ context: pool }) => {
|
|
276
276
|
const result = await pool.query(`
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
277
|
+
INSERT INTO "organizations"("name", "code", "status", "is_meta_org", "_created", "_createdBy")
|
|
278
|
+
VALUES($1, $2, 1, true, NOW(), 0)
|
|
279
|
+
RETURNING "_id", "name", "code", "status", "is_meta_org", "_created", "_createdBy"
|
|
280
|
+
`, [
|
|
281
281
|
dbConfig.multiTenant?.metaOrgName,
|
|
282
282
|
dbConfig.multiTenant?.metaOrgCode,
|
|
283
283
|
]);
|
|
284
284
|
if (result.rowCount === 0) {
|
|
285
|
-
throw new Error(
|
|
285
|
+
throw new Error('Failed to create meta organization');
|
|
286
286
|
}
|
|
287
287
|
const metaOrg = result.rows[0];
|
|
288
288
|
const metaOrgDomains = dbConfig.multiTenant?.metaOrgDomains ?? [];
|
|
289
289
|
for (const domain of metaOrgDomains) {
|
|
290
290
|
await pool.query(`
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
291
|
+
INSERT INTO "organization_domains"("organization_id", "domain", "_created", "_createdBy")
|
|
292
|
+
VALUES($1, $2, NOW(), 0)
|
|
293
|
+
`, [metaOrg._id, domain]);
|
|
294
294
|
}
|
|
295
|
-
initializeSystemUserContext(dbConfig.email?.systemEmailAddress ||
|
|
295
|
+
initializeSystemUserContext(dbConfig.email?.systemEmailAddress || 'system@example.com', metaOrg);
|
|
296
296
|
},
|
|
297
297
|
down: async ({ context: pool }) => {
|
|
298
298
|
await pool.query(`DELETE FROM "organizations" WHERE "is_meta_org" = TRUE`);
|
|
@@ -301,14 +301,14 @@ export const getPostgresInitialSchema = (dbConfig) => {
|
|
|
301
301
|
}
|
|
302
302
|
if (isAuthEnabled && dbConfig.adminUser) {
|
|
303
303
|
migrations.push({
|
|
304
|
-
name:
|
|
304
|
+
name: '00000000000011_data-admin-user',
|
|
305
305
|
up: async ({ context: pool }) => {
|
|
306
306
|
if (!isSystemUserContextInitialized()) {
|
|
307
307
|
const errorMessage = isMultiTenant
|
|
308
|
-
?
|
|
309
|
-
|
|
310
|
-
:
|
|
311
|
-
console.error(
|
|
308
|
+
? 'SystemUserContext has not been initialized. The meta-org migration (00000000000010_data-meta-org) should have run before this migration. ' +
|
|
309
|
+
'Please ensure metaOrgName and metaOrgCode are provided in your dbConfig.'
|
|
310
|
+
: 'BUG: SystemUserContext has not been initialized. For non-multi-tenant setups, SystemUserContext should be initialized before migrations run.';
|
|
311
|
+
console.error('❌ Migration Error:', errorMessage);
|
|
312
312
|
throw new Error(errorMessage);
|
|
313
313
|
}
|
|
314
314
|
const systemUserContext = getSystemUserContext();
|
|
@@ -321,11 +321,11 @@ export const getPostgresInitialSchema = (dbConfig) => {
|
|
|
321
321
|
try {
|
|
322
322
|
if (isMultiTenant) {
|
|
323
323
|
await client.query(`INSERT INTO "users"("_orgId", "email", "display_name", "password", "_created", "_createdBy")
|
|
324
|
-
|
|
324
|
+
VALUES($1, $2, 'Admin User', $3, NOW(), 0)`, [orgId, email, hashedPassword]);
|
|
325
325
|
}
|
|
326
326
|
else {
|
|
327
327
|
await client.query(`INSERT INTO "users"("email", "display_name", "password", "_created", "_createdBy")
|
|
328
|
-
|
|
328
|
+
VALUES($1, 'Admin User', $2, NOW(), 0)`, [email, hashedPassword]);
|
|
329
329
|
}
|
|
330
330
|
}
|
|
331
331
|
finally {
|
|
@@ -343,7 +343,7 @@ export const getPostgresInitialSchema = (dbConfig) => {
|
|
|
343
343
|
}
|
|
344
344
|
if (isAuthEnabled && dbConfig.adminUser) {
|
|
345
345
|
migrations.push({
|
|
346
|
-
name:
|
|
346
|
+
name: '00000000000012_data-admin-authorizations',
|
|
347
347
|
up: async ({ context: pool }) => {
|
|
348
348
|
const client = await pool.connect();
|
|
349
349
|
try {
|
|
@@ -353,16 +353,16 @@ export const getPostgresInitialSchema = (dbConfig) => {
|
|
|
353
353
|
? await organizationService.getMetaOrg(EmptyUserContext)
|
|
354
354
|
: undefined;
|
|
355
355
|
if (isMultiTenant && !metaOrg) {
|
|
356
|
-
throw new Error(
|
|
356
|
+
throw new Error('Meta organization not found. Ensure meta-org migration ran successfully.');
|
|
357
357
|
}
|
|
358
358
|
const email = dbConfig.adminUser.email.toLowerCase();
|
|
359
359
|
const userResult = await client.query(`SELECT "_id" FROM "users" WHERE "email" = $1`, [email]);
|
|
360
360
|
const adminUserRow = userResult.rows[0];
|
|
361
361
|
if (!adminUserRow) {
|
|
362
|
-
throw new Error(
|
|
362
|
+
throw new Error('Admin user not found. Ensure admin-user migration ran successfully.');
|
|
363
363
|
}
|
|
364
364
|
const adminUserId = adminUserRow._id;
|
|
365
|
-
await client.query(
|
|
365
|
+
await client.query('BEGIN');
|
|
366
366
|
try {
|
|
367
367
|
const roleResult = isMultiTenant
|
|
368
368
|
? await client.query(`
|
|
@@ -376,7 +376,7 @@ export const getPostgresInitialSchema = (dbConfig) => {
|
|
|
376
376
|
RETURNING "_id"
|
|
377
377
|
`);
|
|
378
378
|
if (roleResult.rowCount === 0) {
|
|
379
|
-
throw new Error(
|
|
379
|
+
throw new Error('Failed to create admin role');
|
|
380
380
|
}
|
|
381
381
|
const roleId = roleResult.rows[0]._id;
|
|
382
382
|
const userRoleResult = isMultiTenant
|
|
@@ -389,7 +389,7 @@ export const getPostgresInitialSchema = (dbConfig) => {
|
|
|
389
389
|
VALUES($1, $2, NOW(), 0)
|
|
390
390
|
`, [adminUserId, roleId]);
|
|
391
391
|
if (userRoleResult.rowCount === 0) {
|
|
392
|
-
throw new Error(
|
|
392
|
+
throw new Error('Failed to create user role');
|
|
393
393
|
}
|
|
394
394
|
const featureResult = isMultiTenant
|
|
395
395
|
? await client.query(`
|
|
@@ -403,7 +403,7 @@ export const getPostgresInitialSchema = (dbConfig) => {
|
|
|
403
403
|
RETURNING "_id"
|
|
404
404
|
`);
|
|
405
405
|
if (featureResult.rowCount === 0) {
|
|
406
|
-
throw new Error(
|
|
406
|
+
throw new Error('Failed to create admin feature');
|
|
407
407
|
}
|
|
408
408
|
const featureId = featureResult.rows[0]._id;
|
|
409
409
|
const authorizationResult = isMultiTenant
|
|
@@ -422,12 +422,12 @@ export const getPostgresInitialSchema = (dbConfig) => {
|
|
|
422
422
|
VALUES($1, $2, NOW(), 0)
|
|
423
423
|
`, [roleId, featureId]);
|
|
424
424
|
if (authorizationResult.rowCount === 0) {
|
|
425
|
-
throw new Error(
|
|
425
|
+
throw new Error('Failed to create admin authorization');
|
|
426
426
|
}
|
|
427
|
-
await client.query(
|
|
427
|
+
await client.query('COMMIT');
|
|
428
428
|
}
|
|
429
429
|
catch (error) {
|
|
430
|
-
await client.query(
|
|
430
|
+
await client.query('ROLLBACK');
|
|
431
431
|
throw error;
|
|
432
432
|
}
|
|
433
433
|
}
|
|
@@ -445,7 +445,7 @@ export const getPostgresInitialSchema = (dbConfig) => {
|
|
|
445
445
|
: undefined;
|
|
446
446
|
if (isMultiTenant && !metaOrg)
|
|
447
447
|
return;
|
|
448
|
-
await client.query(
|
|
448
|
+
await client.query('BEGIN');
|
|
449
449
|
try {
|
|
450
450
|
if (isMultiTenant) {
|
|
451
451
|
await client.query(`
|
|
@@ -505,10 +505,10 @@ export const getPostgresInitialSchema = (dbConfig) => {
|
|
|
505
505
|
WHERE "name" = 'admin'
|
|
506
506
|
`);
|
|
507
507
|
}
|
|
508
|
-
await client.query(
|
|
508
|
+
await client.query('COMMIT');
|
|
509
509
|
}
|
|
510
510
|
catch (error) {
|
|
511
|
-
await client.query(
|
|
511
|
+
await client.query('ROLLBACK');
|
|
512
512
|
throw error;
|
|
513
513
|
}
|
|
514
514
|
}
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { IEntity, IModelSpec, IPagedResult, IQueryOptions } from
|
|
2
|
-
import type { AppIdType } from
|
|
3
|
-
import { TSchema } from
|
|
4
|
-
import { DeleteResult, IDatabase } from
|
|
5
|
-
import { Operation } from
|
|
6
|
-
import type { PostgresConnection } from
|
|
1
|
+
import { IEntity, IModelSpec, IPagedResult, IQueryOptions } from '@loomcore/common/models';
|
|
2
|
+
import type { AppIdType } from '@loomcore/common/types';
|
|
3
|
+
import { TSchema } from '@sinclair/typebox';
|
|
4
|
+
import { DeleteResult, IDatabase } from '../models/index.js';
|
|
5
|
+
import { Operation } from '../operations/operation.js';
|
|
6
|
+
import type { PostgresConnection } from './postgres-connection.js';
|
|
7
7
|
export declare class PostgresDatabase implements IDatabase {
|
|
8
8
|
private connection;
|
|
9
9
|
constructor(connection: PostgresConnection);
|
|
@@ -1,19 +1,19 @@
|
|
|
1
|
-
import { batchUpdate as batchUpdateCommand } from
|
|
2
|
-
import { create as createCommand } from
|
|
3
|
-
import { createMany as createManyCommand } from
|
|
4
|
-
import { deleteById as deleteByIdCommand } from
|
|
5
|
-
import { deleteMany as deleteManyCommand } from
|
|
6
|
-
import { fullUpdateById as fullUpdateByIdCommand } from
|
|
7
|
-
import { partialUpdateById as partialUpdateByIdCommand } from
|
|
8
|
-
import { update as updateCommand } from
|
|
9
|
-
import { find as findQuery } from
|
|
10
|
-
import { findOne as findOneQuery } from
|
|
11
|
-
import { get as getQuery } from
|
|
12
|
-
import { getAll as getAllQuery } from
|
|
13
|
-
import { getById as getByIdQuery } from
|
|
14
|
-
import { getCount as getCountQuery } from
|
|
15
|
-
import { convertKeysToCamelCase, convertKeysToSnakeCase, toPostgresStoreName, } from
|
|
16
|
-
import { convertNullToUndefined } from
|
|
1
|
+
import { batchUpdate as batchUpdateCommand } from './commands/postgres-batch-update.command.js';
|
|
2
|
+
import { create as createCommand } from './commands/postgres-create.command.js';
|
|
3
|
+
import { createMany as createManyCommand } from './commands/postgres-create-many.command.js';
|
|
4
|
+
import { deleteById as deleteByIdCommand } from './commands/postgres-delete-by-id.command.js';
|
|
5
|
+
import { deleteMany as deleteManyCommand } from './commands/postgres-delete-many.command.js';
|
|
6
|
+
import { fullUpdateById as fullUpdateByIdCommand } from './commands/postgres-full-update-by-id.command.js';
|
|
7
|
+
import { partialUpdateById as partialUpdateByIdCommand } from './commands/postgres-partial-update-by-id.command.js';
|
|
8
|
+
import { update as updateCommand } from './commands/postgres-update.command.js';
|
|
9
|
+
import { find as findQuery } from './queries/postgres-find.query.js';
|
|
10
|
+
import { findOne as findOneQuery } from './queries/postgres-find-one.query.js';
|
|
11
|
+
import { get as getQuery } from './queries/postgres-get.query.js';
|
|
12
|
+
import { getAll as getAllQuery } from './queries/postgres-get-all.query.js';
|
|
13
|
+
import { getById as getByIdQuery } from './queries/postgres-get-by-id.query.js';
|
|
14
|
+
import { getCount as getCountQuery } from './queries/postgres-get-count.query.js';
|
|
15
|
+
import { convertKeysToCamelCase, convertKeysToSnakeCase, toPostgresStoreName, } from './utils/convert-keys.util.js';
|
|
16
|
+
import { convertNullToUndefined } from './utils/convert-null-to-undefined.util.js';
|
|
17
17
|
export class PostgresDatabase {
|
|
18
18
|
connection;
|
|
19
19
|
constructor(connection) {
|
|
@@ -21,10 +21,10 @@ export class PostgresDatabase {
|
|
|
21
21
|
}
|
|
22
22
|
async close() {
|
|
23
23
|
const connection = this.connection;
|
|
24
|
-
if (typeof connection.release ===
|
|
24
|
+
if (typeof connection.release === 'function') {
|
|
25
25
|
return;
|
|
26
26
|
}
|
|
27
|
-
if (typeof connection.end ===
|
|
27
|
+
if (typeof connection.end === 'function') {
|
|
28
28
|
await connection.end();
|
|
29
29
|
}
|
|
30
30
|
}
|
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import type { PostgresConnection } from '../postgres-connection.js';
|
|
2
|
-
import { IQueryOptions } from
|
|
2
|
+
import { IQueryOptions } from '@loomcore/common/models';
|
|
3
3
|
export declare function findOne<T>(client: PostgresConnection, queryObject: IQueryOptions, pluralResourceName: string): Promise<T | null>;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { buildWhereClause } from
|
|
2
|
-
import { buildOrderByClause } from
|
|
1
|
+
import { buildWhereClause } from '../utils/build-where-clause.js';
|
|
2
|
+
import { buildOrderByClause } from '../utils/build-order-by-clause.js';
|
|
3
3
|
export async function findOne(client, queryObject, pluralResourceName) {
|
|
4
4
|
const { whereClause, values } = buildWhereClause(queryObject);
|
|
5
5
|
const orderByClause = buildOrderByClause(queryObject);
|
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import type { PostgresConnection } from '../postgres-connection.js';
|
|
2
|
-
import { IQueryOptions } from
|
|
2
|
+
import { IQueryOptions } from '@loomcore/common/models';
|
|
3
3
|
export declare function find<T>(client: PostgresConnection, queryObject: IQueryOptions, pluralResourceName: string): Promise<T[]>;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { buildWhereClause } from
|
|
2
|
-
import { buildOrderByClause } from
|
|
1
|
+
import { buildWhereClause } from '../utils/build-where-clause.js';
|
|
2
|
+
import { buildOrderByClause } from '../utils/build-order-by-clause.js';
|
|
3
3
|
import { buildPaginationClause } from '../utils/build-pagination-clause.js';
|
|
4
4
|
export async function find(client, queryObject, pluralResourceName) {
|
|
5
5
|
const { whereClause, values } = buildWhereClause(queryObject);
|
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import type { PostgresConnection } from '../postgres-connection.js';
|
|
2
|
-
import { Operation } from
|
|
2
|
+
import { Operation } from '../../operations/operation.js';
|
|
3
3
|
export declare function getAll<T>(client: PostgresConnection, operations: Operation[], pluralResourceName: string): Promise<T[]>;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { LeftJoin } from
|
|
2
|
-
import { InnerJoin } from
|
|
3
|
-
import { LeftJoinMany } from
|
|
1
|
+
import { LeftJoin } from '../../operations/left-join.operation.js';
|
|
2
|
+
import { InnerJoin } from '../../operations/inner-join.operation.js';
|
|
3
|
+
import { LeftJoinMany } from '../../operations/left-join-many.operation.js';
|
|
4
4
|
import { buildJoinClauses } from '../utils/build-join-clauses.js';
|
|
5
5
|
import { buildSelectClause } from '../utils/build-select-clause.js';
|
|
6
6
|
export async function getAll(client, operations, pluralResourceName) {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { PostgresConnection } from '../postgres-connection.js';
|
|
2
|
-
import { Operation } from
|
|
2
|
+
import { Operation } from '../../operations/operation.js';
|
|
3
3
|
import { IQueryOptions } from '@loomcore/common/models';
|
|
4
4
|
import type { AppIdType } from '@loomcore/common/types';
|
|
5
5
|
export declare function getById<T>(client: PostgresConnection, operations: Operation[], queryObject: IQueryOptions, id: AppIdType, pluralResourceName: string): Promise<T | null>;
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { LeftJoin } from
|
|
2
|
-
import { InnerJoin } from
|
|
3
|
-
import { LeftJoinMany } from
|
|
4
|
-
import { buildJoinClauses } from
|
|
5
|
-
import { buildSelectClause } from
|
|
1
|
+
import { LeftJoin } from '../../operations/left-join.operation.js';
|
|
2
|
+
import { InnerJoin } from '../../operations/inner-join.operation.js';
|
|
3
|
+
import { LeftJoinMany } from '../../operations/left-join-many.operation.js';
|
|
4
|
+
import { buildJoinClauses } from '../utils/build-join-clauses.js';
|
|
5
|
+
import { buildSelectClause } from '../utils/build-select-clause.js';
|
|
6
6
|
import { BadRequestError } from '../../../errors/index.js';
|
|
7
7
|
import { buildWhereClause } from '../utils/build-where-clause.js';
|
|
8
8
|
export async function getById(client, operations, queryObject, id, pluralResourceName) {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import type { PostgresConnection } from '../postgres-connection.js';
|
|
2
|
-
import { IQueryOptions, IPagedResult } from
|
|
3
|
-
import { Operation } from
|
|
2
|
+
import { IQueryOptions, IPagedResult } from '@loomcore/common/models';
|
|
3
|
+
import { Operation } from '../../operations/operation.js';
|
|
4
4
|
export declare function get<T>(client: PostgresConnection, operations: Operation[], queryOptions: IQueryOptions, pluralResourceName: string): Promise<IPagedResult<T>>;
|
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import { LeftJoin } from
|
|
2
|
-
import { InnerJoin } from
|
|
3
|
-
import { LeftJoinMany } from
|
|
4
|
-
import { buildWhereClause } from
|
|
5
|
-
import { buildOrderByClause } from
|
|
6
|
-
import { buildJoinClauses } from
|
|
7
|
-
import { buildSelectClause } from
|
|
8
|
-
import { executeCountQuery } from
|
|
9
|
-
import { apiUtils } from
|
|
1
|
+
import { LeftJoin } from '../../operations/left-join.operation.js';
|
|
2
|
+
import { InnerJoin } from '../../operations/inner-join.operation.js';
|
|
3
|
+
import { LeftJoinMany } from '../../operations/left-join-many.operation.js';
|
|
4
|
+
import { buildWhereClause } from '../utils/build-where-clause.js';
|
|
5
|
+
import { buildOrderByClause } from '../utils/build-order-by-clause.js';
|
|
6
|
+
import { buildJoinClauses } from '../utils/build-join-clauses.js';
|
|
7
|
+
import { buildSelectClause } from '../utils/build-select-clause.js';
|
|
8
|
+
import { executeCountQuery } from '../utils/build-count-query.js';
|
|
9
|
+
import { apiUtils } from '../../../utils/api.utils.js';
|
|
10
10
|
import { buildPaginationClause } from '../utils/build-pagination-clause.js';
|
|
11
11
|
export async function get(client, operations, queryOptions, pluralResourceName) {
|
|
12
12
|
const hasJoins = operations.some(op => op instanceof LeftJoin || op instanceof InnerJoin || op instanceof LeftJoinMany);
|
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import type { PostgresConnection } from '../postgres-connection.js';
|
|
2
|
-
import { IQueryOptions } from
|
|
2
|
+
import { IQueryOptions } from '@loomcore/common/models';
|
|
3
3
|
export declare function executeCountQuery(client: PostgresConnection, pluralResourceName: string, queryOptions?: IQueryOptions): Promise<number>;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { buildWhereClause } from
|
|
1
|
+
import { buildWhereClause } from './build-where-clause.js';
|
|
2
2
|
export async function executeCountQuery(client, pluralResourceName, queryOptions = {}) {
|
|
3
3
|
const { whereClause, values } = buildWhereClause(queryOptions);
|
|
4
4
|
const countQuery = `SELECT COUNT(*) as total FROM "${pluralResourceName}" ${whereClause}`;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { LeftJoin } from
|
|
2
|
-
import { InnerJoin } from
|
|
3
|
-
import { LeftJoinMany } from
|
|
4
|
-
import { toPostgresStoreName } from
|
|
1
|
+
import { LeftJoin } from '../../operations/left-join.operation.js';
|
|
2
|
+
import { InnerJoin } from '../../operations/inner-join.operation.js';
|
|
3
|
+
import { LeftJoinMany } from '../../operations/left-join-many.operation.js';
|
|
4
|
+
import { toPostgresStoreName } from './convert-keys.util.js';
|
|
5
5
|
function getParentAlias(localField) {
|
|
6
6
|
if (!localField.includes('.'))
|
|
7
7
|
return null;
|
|
@@ -37,10 +37,10 @@ export function buildJoinClauses(operations, mainTableName, options) {
|
|
|
37
37
|
}
|
|
38
38
|
if (operation instanceof LeftJoin || operation instanceof InnerJoin || operation instanceof LeftJoinMany) {
|
|
39
39
|
const useLeftJoin = oneToOneOnly && operation instanceof InnerJoin && throughTableAliases.has(operation.as);
|
|
40
|
-
const joinType = useLeftJoin || operation instanceof LeftJoin || operation instanceof LeftJoinMany ?
|
|
40
|
+
const joinType = useLeftJoin || operation instanceof LeftJoin || operation instanceof LeftJoinMany ? 'LEFT JOIN' : 'INNER JOIN';
|
|
41
41
|
let leftSide;
|
|
42
|
-
if (operation.localField.includes(
|
|
43
|
-
const [alias, column] = operation.localField.split(
|
|
42
|
+
if (operation.localField.includes('.')) {
|
|
43
|
+
const [alias, column] = operation.localField.split('.');
|
|
44
44
|
leftSide = `"${alias}"."${column}"`;
|
|
45
45
|
}
|
|
46
46
|
else {
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { IQueryOptions } from
|
|
1
|
+
import { IQueryOptions } from '@loomcore/common/models';
|
|
2
2
|
export declare function buildPaginationClause(queryObject: IQueryOptions): string;
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
export function buildPostgresUrl(config) {
|
|
2
2
|
const { database } = config;
|
|
3
3
|
if (!database) {
|
|
4
|
-
throw new Error(
|
|
4
|
+
throw new Error('Database configuration is required to build the PostgreSQL URL.');
|
|
5
5
|
}
|
|
6
6
|
const { username, password, host, port, name } = database;
|
|
7
7
|
if (!username || !password || !host || !port || !name) {
|
|
8
|
-
throw new Error(
|
|
8
|
+
throw new Error('Database configuration must include user, password, host, port, and name to build the PostgreSQL URL.');
|
|
9
9
|
}
|
|
10
10
|
const encodedUsername = encodeURIComponent(username);
|
|
11
11
|
const encodedPassword = encodeURIComponent(password);
|