@loomcore/api 0.0.59 → 0.1.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.
- package/LICENSE +201 -0
- package/README.md +50 -0
- package/dist/__tests__/common-test.utils.d.ts +29 -60
- package/dist/__tests__/common-test.utils.js +88 -166
- package/dist/__tests__/index.d.ts +6 -0
- package/dist/__tests__/index.js +6 -0
- package/dist/__tests__/models/category.model.d.ts +8 -0
- package/dist/__tests__/models/category.model.js +6 -0
- package/dist/__tests__/models/product.model.d.ts +17 -0
- package/dist/__tests__/models/product.model.js +10 -0
- package/dist/__tests__/models/test-entity.model.d.ts +11 -0
- package/dist/__tests__/models/test-entity.model.js +10 -0
- package/dist/__tests__/models/test-item.model.d.ts +12 -0
- package/dist/__tests__/models/test-item.model.js +9 -0
- package/dist/__tests__/mongo-db.test-database.d.ts +15 -0
- package/dist/__tests__/mongo-db.test-database.js +74 -0
- package/dist/__tests__/postgres-test-migrations/001-create-test-entities-table.migration.d.ts +11 -0
- package/dist/__tests__/postgres-test-migrations/001-create-test-entities-table.migration.js +59 -0
- package/dist/__tests__/postgres-test-migrations/002-create-categories-table.migration.d.ts +11 -0
- package/dist/__tests__/postgres-test-migrations/002-create-categories-table.migration.js +52 -0
- package/dist/__tests__/postgres-test-migrations/003-create-products-table.migration.d.ts +11 -0
- package/dist/__tests__/postgres-test-migrations/003-create-products-table.migration.js +62 -0
- package/dist/__tests__/postgres-test-migrations/004-create-test-users-table.migration.d.ts +11 -0
- package/dist/__tests__/postgres-test-migrations/004-create-test-users-table.migration.js +66 -0
- package/dist/__tests__/postgres-test-migrations/005-create-test-items-table.migration.d.ts +11 -0
- package/dist/__tests__/postgres-test-migrations/005-create-test-items-table.migration.js +50 -0
- package/dist/__tests__/postgres-test-migrations/run-test-migrations.d.ts +2 -0
- package/dist/__tests__/postgres-test-migrations/run-test-migrations.js +22 -0
- package/dist/__tests__/postgres.test-database.d.ts +13 -0
- package/dist/__tests__/postgres.test-database.js +85 -0
- package/dist/__tests__/test-database.interface.d.ts +7 -0
- package/dist/__tests__/test-express-app.d.ts +9 -7
- package/dist/__tests__/test-express-app.js +38 -48
- package/dist/__tests__/test-objects.d.ts +23 -0
- package/dist/__tests__/test-objects.js +45 -0
- package/dist/config/base-api-config.d.ts +2 -2
- package/dist/config/base-api-config.js +2 -2
- package/dist/controllers/api.controller.d.ts +1 -5
- package/dist/controllers/api.controller.js +4 -11
- package/dist/controllers/auth.controller.d.ts +2 -2
- package/dist/controllers/auth.controller.js +4 -5
- package/dist/controllers/organizations.controller.d.ts +2 -2
- package/dist/controllers/organizations.controller.js +4 -4
- package/dist/controllers/users.controller.d.ts +2 -2
- package/dist/controllers/users.controller.js +2 -2
- package/dist/databases/index.d.ts +1 -0
- package/dist/databases/index.js +1 -0
- package/dist/databases/models/constants.d.ts +1 -0
- package/dist/databases/models/constants.js +1 -0
- package/dist/databases/models/database.d.ts +3 -0
- package/dist/databases/models/database.interface.d.ts +28 -0
- package/dist/databases/models/delete-result.d.ts +5 -0
- package/dist/databases/models/delete-result.js +8 -0
- package/dist/databases/models/index.d.ts +5 -0
- package/dist/databases/models/index.js +5 -0
- package/dist/databases/models/update-result.d.ts +5 -0
- package/dist/databases/models/update-result.js +8 -0
- package/dist/databases/mongo-db/commands/index.d.ts +8 -0
- package/dist/databases/mongo-db/commands/index.js +8 -0
- package/dist/databases/mongo-db/commands/mongo-batch-update.command.d.ts +4 -0
- package/dist/databases/mongo-db/commands/mongo-batch-update.command.js +41 -0
- package/dist/databases/mongo-db/commands/mongo-create-many.command.d.ts +5 -0
- package/dist/databases/mongo-db/commands/mongo-create-many.command.js +17 -0
- package/dist/databases/mongo-db/commands/mongo-create.command.d.ts +5 -0
- package/dist/databases/mongo-db/commands/mongo-create.command.js +17 -0
- package/dist/databases/mongo-db/commands/mongo-delete-by-id.command.d.ts +3 -0
- package/dist/databases/mongo-db/commands/mongo-delete-by-id.command.js +14 -0
- package/dist/databases/mongo-db/commands/mongo-delete-many.command.d.ts +4 -0
- package/dist/databases/mongo-db/commands/mongo-delete-many.command.js +9 -0
- package/dist/databases/mongo-db/commands/mongo-full-updateby-id.command.d.ts +3 -0
- package/dist/databases/mongo-db/commands/mongo-full-updateby-id.command.js +25 -0
- package/dist/databases/mongo-db/commands/mongo-partial-update-by-id.command.d.ts +3 -0
- package/dist/databases/mongo-db/commands/mongo-partial-update-by-id.command.js +25 -0
- package/dist/databases/mongo-db/commands/mongo-update.command.d.ts +4 -0
- package/dist/databases/mongo-db/commands/mongo-update.command.js +19 -0
- package/dist/databases/mongo-db/index.d.ts +4 -0
- package/dist/databases/mongo-db/index.js +4 -0
- package/dist/databases/mongo-db/models/no-sql-pipeline.d.ts +15 -0
- package/dist/databases/mongo-db/models/no-sql-pipeline.interface.d.ts +11 -0
- package/dist/databases/mongo-db/models/no-sql-pipeline.js +43 -0
- package/dist/databases/mongo-db/mongo-db.database.d.ts +32 -0
- package/dist/databases/mongo-db/mongo-db.database.js +65 -0
- package/dist/databases/mongo-db/queries/index.d.ts +6 -0
- package/dist/databases/mongo-db/queries/index.js +6 -0
- package/dist/databases/mongo-db/queries/mongo-find-one.query.d.ts +3 -0
- package/dist/databases/mongo-db/queries/mongo-find-one.query.js +9 -0
- package/dist/databases/mongo-db/queries/mongo-find.query.d.ts +3 -0
- package/dist/databases/mongo-db/queries/mongo-find.query.js +9 -0
- package/dist/databases/mongo-db/queries/mongo-get-all.query.d.ts +3 -0
- package/dist/databases/mongo-db/queries/mongo-get-all.query.js +17 -0
- package/dist/databases/mongo-db/queries/mongo-get-by-id.query.d.ts +4 -0
- package/dist/databases/mongo-db/queries/mongo-get-by-id.query.js +17 -0
- package/dist/databases/mongo-db/queries/mongo-get-count.query.d.ts +2 -0
- package/dist/databases/mongo-db/queries/mongo-get-count.query.js +5 -0
- package/dist/databases/mongo-db/queries/mongo-get.query.d.ts +4 -0
- package/dist/databases/mongo-db/queries/mongo-get.query.js +14 -0
- package/dist/databases/mongo-db/utils/build-find-options.util.d.ts +3 -0
- package/dist/databases/mongo-db/utils/build-find-options.util.js +15 -0
- package/dist/databases/mongo-db/utils/build-no-sql-match.util.d.ts +3 -0
- package/dist/databases/mongo-db/utils/build-no-sql-match.util.js +59 -0
- package/dist/databases/mongo-db/utils/convert-object-ids-to-strings.util.d.ts +1 -0
- package/dist/databases/mongo-db/utils/convert-object-ids-to-strings.util.js +32 -0
- package/dist/databases/mongo-db/utils/convert-operations-to-pipeline.util.d.ts +3 -0
- package/dist/databases/mongo-db/utils/convert-operations-to-pipeline.util.js +68 -0
- package/dist/databases/mongo-db/utils/convert-query-options-to-pipeline.util.d.ts +3 -0
- package/dist/databases/mongo-db/utils/convert-query-options-to-pipeline.util.js +31 -0
- package/dist/databases/mongo-db/utils/convert-strings-to-object-ids.util.d.ts +3 -0
- package/dist/databases/mongo-db/utils/convert-strings-to-object-ids.util.js +72 -0
- package/dist/databases/mongo-db/utils/index.d.ts +7 -0
- package/dist/databases/mongo-db/utils/index.js +7 -0
- package/dist/databases/operations/join.operation.d.ts +7 -0
- package/dist/databases/operations/join.operation.js +12 -0
- package/dist/databases/operations/operation.d.ts +2 -0
- package/dist/databases/postgres/commands/postgres-batch-update.command.d.ts +4 -0
- package/dist/databases/postgres/commands/postgres-batch-update.command.js +56 -0
- package/dist/databases/postgres/commands/postgres-create-many.command.d.ts +6 -0
- package/dist/databases/postgres/commands/postgres-create-many.command.js +63 -0
- package/dist/databases/postgres/commands/postgres-create.command.d.ts +6 -0
- package/dist/databases/postgres/commands/postgres-create.command.js +29 -0
- package/dist/databases/postgres/commands/postgres-delete-by-id.command.d.ts +3 -0
- package/dist/databases/postgres/commands/postgres-delete-by-id.command.js +6 -0
- package/dist/databases/postgres/commands/postgres-delete-many.command.d.ts +4 -0
- package/dist/databases/postgres/commands/postgres-delete-many.command.js +13 -0
- package/dist/databases/postgres/commands/postgres-full-update-by-id.command.d.ts +4 -0
- package/dist/databases/postgres/commands/postgres-full-update-by-id.command.js +72 -0
- package/dist/databases/postgres/commands/postgres-partial-update-by-id.command.d.ts +4 -0
- package/dist/databases/postgres/commands/postgres-partial-update-by-id.command.js +42 -0
- package/dist/databases/postgres/commands/postgres-update.command.d.ts +5 -0
- package/dist/databases/postgres/commands/postgres-update.command.js +48 -0
- package/dist/databases/postgres/migrations/001-create-migrations-table.migration.d.ts +11 -0
- package/dist/databases/postgres/migrations/001-create-migrations-table.migration.js +52 -0
- package/dist/databases/postgres/migrations/002-create-organizations-table.migration.d.ts +11 -0
- package/dist/databases/postgres/migrations/002-create-organizations-table.migration.js +55 -0
- package/dist/databases/postgres/migrations/003-create-users-table.migration.d.ts +11 -0
- package/dist/databases/postgres/migrations/003-create-users-table.migration.js +65 -0
- package/dist/databases/postgres/migrations/004-create-refresh-token-table.migration.d.ts +11 -0
- package/dist/databases/postgres/migrations/004-create-refresh-token-table.migration.js +57 -0
- package/dist/databases/postgres/migrations/index.d.ts +3 -0
- package/dist/databases/postgres/migrations/index.js +3 -0
- package/dist/databases/postgres/migrations/migration.interface.d.ts +6 -0
- package/dist/databases/postgres/migrations/migration.interface.js +1 -0
- package/dist/databases/postgres/migrations/setup-for-auth.migration.d.ts +2 -0
- package/dist/databases/postgres/migrations/setup-for-auth.migration.js +18 -0
- package/dist/databases/postgres/migrations/setup-for-multitenant.migration.d.ts +2 -0
- package/dist/databases/postgres/migrations/setup-for-multitenant.migration.js +16 -0
- package/dist/databases/postgres/postgres.database.d.ts +31 -0
- package/dist/databases/postgres/postgres.database.js +69 -0
- package/dist/databases/postgres/queries/postgres-find-one.query.d.ts +3 -0
- package/dist/databases/postgres/queries/postgres-find-one.query.js +13 -0
- package/dist/databases/postgres/queries/postgres-find.query.d.ts +3 -0
- package/dist/databases/postgres/queries/postgres-find.query.js +11 -0
- package/dist/databases/postgres/queries/postgres-get-all.query.d.ts +3 -0
- package/dist/databases/postgres/queries/postgres-get-all.query.js +14 -0
- package/dist/databases/postgres/queries/postgres-get-by-id.query.d.ts +4 -0
- package/dist/databases/postgres/queries/postgres-get-by-id.query.js +26 -0
- package/dist/databases/postgres/queries/postgres-get-count.query.d.ts +2 -0
- package/dist/databases/postgres/queries/postgres-get-count.query.js +4 -0
- package/dist/databases/postgres/queries/postgres-get.query.d.ts +4 -0
- package/dist/databases/postgres/queries/postgres-get.query.js +26 -0
- package/dist/databases/postgres/utils/build-count-query.d.ts +3 -0
- package/dist/databases/postgres/utils/build-count-query.js +7 -0
- package/dist/databases/postgres/utils/build-join-clauses.d.ts +2 -0
- package/dist/databases/postgres/utils/build-join-clauses.js +12 -0
- package/dist/databases/postgres/utils/build-order-by-clause.d.ts +2 -0
- package/dist/databases/postgres/utils/build-order-by-clause.js +8 -0
- package/dist/databases/postgres/utils/build-pagination-clause.d.ts +2 -0
- package/dist/databases/postgres/utils/build-pagination-clause.js +9 -0
- package/dist/databases/postgres/utils/build-select-clause.d.ts +3 -0
- package/dist/databases/postgres/utils/build-select-clause.js +28 -0
- package/dist/databases/postgres/utils/build-where-clause.d.ts +5 -0
- package/dist/databases/postgres/utils/build-where-clause.js +50 -0
- package/dist/databases/postgres/utils/columns-and-values-from-entity.d.ts +5 -0
- package/dist/databases/postgres/utils/columns-and-values-from-entity.js +9 -0
- package/dist/databases/postgres/utils/convert-null-to-undefined.util.d.ts +2 -0
- package/dist/databases/postgres/utils/convert-null-to-undefined.util.js +70 -0
- package/dist/databases/postgres/utils/transform-join-results.d.ts +2 -0
- package/dist/databases/postgres/utils/transform-join-results.js +33 -0
- package/dist/databases/utils/get-property-schema.util.d.ts +2 -0
- package/dist/databases/utils/get-property-schema.util.js +15 -0
- package/dist/databases/utils/index.d.ts +1 -0
- package/dist/databases/utils/index.js +1 -0
- package/dist/models/base-api-config.interface.d.ts +3 -2
- package/dist/models/index.d.ts +1 -1
- package/dist/models/index.js +1 -1
- package/dist/models/refresh-token.model.d.ts +18 -0
- package/dist/models/refresh-token.model.js +13 -0
- package/dist/services/auth.service.d.ts +11 -18
- package/dist/services/auth.service.js +29 -50
- package/dist/services/generic-api-service/generic-api-service.interface.d.ts +29 -0
- package/dist/services/generic-api-service/generic-api-service.interface.js +1 -0
- package/dist/services/generic-api-service/generic-api.service.d.ts +37 -0
- package/dist/services/generic-api-service/generic-api.service.js +178 -0
- package/dist/services/index.d.ts +2 -2
- package/dist/services/index.js +2 -2
- package/dist/services/multi-tenant-api.service.d.ts +9 -6
- package/dist/services/multi-tenant-api.service.js +10 -18
- package/dist/services/organization.service.d.ts +5 -5
- package/dist/services/organization.service.js +9 -6
- package/dist/services/password-reset-token.service.d.ts +3 -3
- package/dist/services/password-reset-token.service.js +5 -5
- package/dist/services/tenant-query-decorator.d.ts +1 -1
- package/dist/services/tenant-query-decorator.js +1 -1
- package/dist/services/user.service.d.ts +4 -6
- package/dist/services/user.service.js +4 -10
- package/dist/services/utils/audit-for-create.util.d.ts +2 -0
- package/dist/services/utils/audit-for-create.util.js +9 -0
- package/dist/services/utils/audit-for-update.util.d.ts +2 -0
- package/dist/services/utils/audit-for-update.util.js +7 -0
- package/dist/services/utils/strip-sender-provided-system-properties.util.d.ts +2 -0
- package/dist/services/utils/strip-sender-provided-system-properties.util.js +15 -0
- package/dist/utils/api.utils.js +2 -1
- package/dist/utils/index.d.ts +0 -1
- package/dist/utils/index.js +0 -1
- package/package.json +17 -6
- package/dist/services/generic-api-service.interface.d.ts +0 -27
- package/dist/services/generic-api.service.d.ts +0 -50
- package/dist/services/generic-api.service.js +0 -424
- package/dist/utils/db.utils.d.ts +0 -27
- package/dist/utils/db.utils.js +0 -318
- /package/dist/{controllers/api-controller.utils.d.ts → __tests__/test-database.interface.js} +0 -0
- /package/dist/{controllers/api-controller.utils.js → databases/models/database.interface.js} +0 -0
- /package/dist/{models/types/index.d.ts → databases/models/database.js} +0 -0
- /package/dist/{models/types/index.js → databases/mongo-db/models/no-sql-pipeline.interface.js} +0 -0
- /package/dist/{services/generic-api-service.interface.js → databases/operations/operation.js} +0 -0
|
@@ -1,20 +1,23 @@
|
|
|
1
|
-
import { GenericApiService } from './generic-api.service.js';
|
|
1
|
+
import { GenericApiService } from './generic-api-service/generic-api.service.js';
|
|
2
2
|
import { OrganizationSpec } from '@loomcore/common/models';
|
|
3
3
|
export class OrganizationService extends GenericApiService {
|
|
4
|
-
constructor(
|
|
5
|
-
super(
|
|
4
|
+
constructor(database) {
|
|
5
|
+
super(database, 'organizations', 'organization', OrganizationSpec);
|
|
6
6
|
}
|
|
7
7
|
async getAuthTokenByRepoCode(userContext, orgId) {
|
|
8
8
|
const org = await this.getById(userContext, orgId);
|
|
9
|
-
return org
|
|
9
|
+
return org?.authToken ?? null;
|
|
10
10
|
}
|
|
11
11
|
async validateRepoAuthToken(userContext, orgCode, authToken) {
|
|
12
|
-
const org = await this.findOne(userContext, { code: orgCode });
|
|
12
|
+
const org = await this.findOne(userContext, { filters: { code: { eq: orgCode } } });
|
|
13
|
+
if (!org) {
|
|
14
|
+
return null;
|
|
15
|
+
}
|
|
13
16
|
const orgId = org.authToken === authToken ? org._id.toString() : null;
|
|
14
17
|
return orgId;
|
|
15
18
|
}
|
|
16
19
|
async getMetaOrg(userContext) {
|
|
17
|
-
const org = await this.findOne(userContext, { isMetaOrg: true });
|
|
20
|
+
const org = await this.findOne(userContext, { filters: { isMetaOrg: { eq: true } } });
|
|
18
21
|
return org;
|
|
19
22
|
}
|
|
20
23
|
}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { Db } from 'mongodb';
|
|
2
1
|
import { IPasswordResetToken } from '@loomcore/common/models';
|
|
3
|
-
import { GenericApiService } from './generic-api.service.js';
|
|
2
|
+
import { GenericApiService } from './generic-api-service/generic-api.service.js';
|
|
3
|
+
import { IDatabase } from '../databases/models/index.js';
|
|
4
4
|
export declare class PasswordResetTokenService extends GenericApiService<IPasswordResetToken> {
|
|
5
|
-
constructor(
|
|
5
|
+
constructor(database: IDatabase);
|
|
6
6
|
createPasswordResetToken(email: string, expiresOn: number): Promise<IPasswordResetToken | null>;
|
|
7
7
|
getByEmail(email: string): Promise<IPasswordResetToken | null>;
|
|
8
8
|
}
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import crypto from 'crypto';
|
|
2
2
|
import { EmptyUserContext, PasswordResetTokenSpec } from '@loomcore/common/models';
|
|
3
|
-
import { GenericApiService } from './generic-api.service.js';
|
|
3
|
+
import { GenericApiService } from './generic-api-service/generic-api.service.js';
|
|
4
4
|
export class PasswordResetTokenService extends GenericApiService {
|
|
5
|
-
constructor(
|
|
6
|
-
super(
|
|
5
|
+
constructor(database) {
|
|
6
|
+
super(database, 'passwordResetTokens', 'passwordResetToken', PasswordResetTokenSpec);
|
|
7
7
|
}
|
|
8
8
|
async createPasswordResetToken(email, expiresOn) {
|
|
9
9
|
const lowerCaseEmail = email.toLowerCase();
|
|
10
|
-
await this.
|
|
10
|
+
await this.deleteMany(EmptyUserContext, { filters: { email: { eq: lowerCaseEmail } } });
|
|
11
11
|
const passwordResetToken = {
|
|
12
12
|
email: lowerCaseEmail,
|
|
13
13
|
token: crypto.randomBytes(40).toString('hex'),
|
|
@@ -16,6 +16,6 @@ export class PasswordResetTokenService extends GenericApiService {
|
|
|
16
16
|
return super.create(EmptyUserContext, passwordResetToken);
|
|
17
17
|
}
|
|
18
18
|
async getByEmail(email) {
|
|
19
|
-
return await super.findOne(EmptyUserContext, { email: email.toLowerCase() });
|
|
19
|
+
return await super.findOne(EmptyUserContext, { filters: { email: { eq: email.toLowerCase() } } });
|
|
20
20
|
}
|
|
21
21
|
}
|
|
@@ -7,7 +7,7 @@ export declare const DEFAULT_TENANT_OPTIONS: ITenantQueryOptions;
|
|
|
7
7
|
export declare class TenantQueryDecorator {
|
|
8
8
|
private options;
|
|
9
9
|
constructor(options?: Partial<ITenantQueryOptions>);
|
|
10
|
-
applyTenantToQuery(userContext: IUserContext, queryObject:
|
|
10
|
+
applyTenantToQuery(userContext: IUserContext, queryObject: IQueryOptions, collectionName: string): IQueryOptions;
|
|
11
11
|
applyTenantToQueryOptions(userContext: IUserContext, queryOptions: IQueryOptions, collectionName: string): IQueryOptions;
|
|
12
12
|
applyTenantToEntity<T extends IEntity>(userContext: IUserContext, entity: T, collectionName: string): T;
|
|
13
13
|
getOrgIdField(): string;
|
|
@@ -14,7 +14,7 @@ export class TenantQueryDecorator {
|
|
|
14
14
|
userContext?._orgId;
|
|
15
15
|
if (shouldApplyTenantFilter) {
|
|
16
16
|
const orgIdField = this.options.orgIdField || '_orgId';
|
|
17
|
-
result = { ...queryObject, [orgIdField]: userContext._orgId };
|
|
17
|
+
result = { ...queryObject, filters: { ...queryObject.filters, [orgIdField]: { eq: userContext._orgId } } };
|
|
18
18
|
}
|
|
19
19
|
else if (!userContext?._orgId) {
|
|
20
20
|
if (!this.options.excludedCollections?.includes(collectionName)) {
|
|
@@ -1,10 +1,8 @@
|
|
|
1
|
-
import { Db } from 'mongodb';
|
|
2
1
|
import { IUser, IUserContext } from '@loomcore/common/models';
|
|
3
2
|
import { MultiTenantApiService } from '../services/index.js';
|
|
3
|
+
import { IDatabase } from '../databases/models/index.js';
|
|
4
4
|
export declare class UserService extends MultiTenantApiService<IUser> {
|
|
5
|
-
constructor(
|
|
6
|
-
fullUpdateById(userContext: IUserContext, id: string, entity: IUser): Promise<
|
|
7
|
-
|
|
8
|
-
transformList(users: IUser[]): IUser[];
|
|
9
|
-
transformSingle(user: IUser): IUser;
|
|
5
|
+
constructor(database: IDatabase);
|
|
6
|
+
fullUpdateById(userContext: IUserContext, id: string, entity: IUser): Promise<IUser>;
|
|
7
|
+
preprocessEntity(userContext: IUserContext, entity: Partial<IUser>, isCreate: boolean, allowId?: boolean): Promise<Partial<IUser>>;
|
|
10
8
|
}
|
|
@@ -3,14 +3,14 @@ import { UserSpec, PublicUserSchema } from '@loomcore/common/models';
|
|
|
3
3
|
import { MultiTenantApiService } from '../services/index.js';
|
|
4
4
|
import { ServerError } from '../errors/index.js';
|
|
5
5
|
export class UserService extends MultiTenantApiService {
|
|
6
|
-
constructor(
|
|
7
|
-
super(
|
|
6
|
+
constructor(database) {
|
|
7
|
+
super(database, 'users', 'user', UserSpec);
|
|
8
8
|
}
|
|
9
9
|
async fullUpdateById(userContext, id, entity) {
|
|
10
10
|
throw new ServerError('Cannot full update a user. Either use PATCH or /auth/change-password to update password.');
|
|
11
11
|
}
|
|
12
|
-
async
|
|
13
|
-
const preparedEntity = await super.
|
|
12
|
+
async preprocessEntity(userContext, entity, isCreate, allowId = false) {
|
|
13
|
+
const preparedEntity = await super.preprocessEntity(userContext, entity, isCreate);
|
|
14
14
|
if (preparedEntity.email) {
|
|
15
15
|
preparedEntity.email = preparedEntity.email.toLowerCase();
|
|
16
16
|
}
|
|
@@ -19,10 +19,4 @@ export class UserService extends MultiTenantApiService {
|
|
|
19
19
|
}
|
|
20
20
|
return preparedEntity;
|
|
21
21
|
}
|
|
22
|
-
transformList(users) {
|
|
23
|
-
return super.transformList(users);
|
|
24
|
-
}
|
|
25
|
-
transformSingle(user) {
|
|
26
|
-
return super.transformSingle(user);
|
|
27
|
-
}
|
|
28
22
|
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import moment from "moment";
|
|
2
|
+
export function auditForCreate(userContext, doc) {
|
|
3
|
+
const now = moment().utc().toDate();
|
|
4
|
+
const userId = userContext.user?._id?.toString() ?? 'system';
|
|
5
|
+
doc._created = now;
|
|
6
|
+
doc._createdBy = userId;
|
|
7
|
+
doc._updated = now;
|
|
8
|
+
doc._updatedBy = userId;
|
|
9
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export function stripSenderProvidedSystemProperties(userContext, entity, allowId = false) {
|
|
2
|
+
const isSystemUser = userContext.user?._id === 'system';
|
|
3
|
+
if (isSystemUser) {
|
|
4
|
+
return;
|
|
5
|
+
}
|
|
6
|
+
const propertiesToIgnore = ['_orgId'];
|
|
7
|
+
if (allowId) {
|
|
8
|
+
propertiesToIgnore.push('_id');
|
|
9
|
+
}
|
|
10
|
+
for (const key in entity) {
|
|
11
|
+
if (Object.prototype.hasOwnProperty.call(entity, key) && key.startsWith('_') && !propertiesToIgnore.includes(key)) {
|
|
12
|
+
delete entity[key];
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
}
|
package/dist/utils/api.utils.js
CHANGED
|
@@ -14,7 +14,8 @@ function apiResponse(response, status, options = {}, modelSpec, publicSchema) {
|
|
|
14
14
|
options.data = pagedResult;
|
|
15
15
|
}
|
|
16
16
|
else {
|
|
17
|
-
|
|
17
|
+
const encodedData = specForEncoding.encode(options.data);
|
|
18
|
+
options.data = encodedData;
|
|
18
19
|
}
|
|
19
20
|
}
|
|
20
21
|
if (success) {
|
package/dist/utils/index.d.ts
CHANGED
package/dist/utils/index.js
CHANGED
package/package.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@loomcore/api",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.1.2",
|
|
4
4
|
"private": false,
|
|
5
|
-
"description": "Loom Core Api - An opinionated Node.js api using Typescript, Express, and MongoDb",
|
|
5
|
+
"description": "Loom Core Api - An opinionated Node.js api using Typescript, Express, and MongoDb or PostgreSQL",
|
|
6
6
|
"scripts": {
|
|
7
7
|
"clean": "rm -rf dist",
|
|
8
8
|
"tsc": "tsc --project tsconfig.prod.json",
|
|
@@ -17,10 +17,16 @@
|
|
|
17
17
|
"install-updated-libs": "npm i @loomcore/models",
|
|
18
18
|
"update-libs": "npm-run-all -s update-lib-versions install-updated-libs",
|
|
19
19
|
"typecheck": "tsc",
|
|
20
|
-
"test": "
|
|
21
|
-
"test:
|
|
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",
|
|
22
26
|
"test:watch": "cross-env NODE_ENV=test vitest",
|
|
23
|
-
"coverage": "
|
|
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"
|
|
24
30
|
},
|
|
25
31
|
"author": "Tim Hardy",
|
|
26
32
|
"license": "Apache 2.0",
|
|
@@ -31,8 +37,10 @@
|
|
|
31
37
|
"dist/**/*"
|
|
32
38
|
],
|
|
33
39
|
"exports": {
|
|
40
|
+
"./__tests__": "./dist/__tests__/index.js",
|
|
34
41
|
"./config": "./dist/config/index.js",
|
|
35
42
|
"./controllers": "./dist/controllers/index.js",
|
|
43
|
+
"./database": "./dist/database/index.js",
|
|
36
44
|
"./errors": "./dist/errors/index.js",
|
|
37
45
|
"./middleware": "./dist/middleware/index.js",
|
|
38
46
|
"./models": "./dist/models/index.js",
|
|
@@ -45,7 +53,7 @@
|
|
|
45
53
|
"qs": "^6.14.0"
|
|
46
54
|
},
|
|
47
55
|
"peerDependencies": {
|
|
48
|
-
"@loomcore/common": "^0.0.
|
|
56
|
+
"@loomcore/common": "^0.0.19",
|
|
49
57
|
"@sinclair/typebox": "0.34.33",
|
|
50
58
|
"cookie-parser": "^1.4.6",
|
|
51
59
|
"cors": "^2.8.5",
|
|
@@ -53,6 +61,7 @@
|
|
|
53
61
|
"lodash": "^4.17.21",
|
|
54
62
|
"moment": "^2.30.1",
|
|
55
63
|
"mongodb": "^6.16.0",
|
|
64
|
+
"pg": "^8.15.6",
|
|
56
65
|
"rxjs": "^7.8.0"
|
|
57
66
|
},
|
|
58
67
|
"devDependencies": {
|
|
@@ -63,6 +72,7 @@
|
|
|
63
72
|
"@types/lodash": "^4.17.13",
|
|
64
73
|
"@types/qs": "^6.14.0",
|
|
65
74
|
"@types/supertest": "^6.0.3",
|
|
75
|
+
"@types/pg": "^8.15.6",
|
|
66
76
|
"@vitest/coverage-v8": "^3.0.9",
|
|
67
77
|
"cross-env": "^7.0.3",
|
|
68
78
|
"mongodb-memory-server": "^9.3.0",
|
|
@@ -70,6 +80,7 @@
|
|
|
70
80
|
"rxjs": "^7.8.0",
|
|
71
81
|
"supertest": "^7.1.0",
|
|
72
82
|
"typescript": "^5.8.3",
|
|
83
|
+
"pg-mem": "^3.0.5",
|
|
73
84
|
"vite": "^6.2.5",
|
|
74
85
|
"vitest": "^3.0.9"
|
|
75
86
|
}
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import { DeleteResult } from 'mongodb';
|
|
2
|
-
import { ValueError } from '@sinclair/typebox/errors';
|
|
3
|
-
import { IUserContext, IEntity, IPagedResult, IQueryOptions } from '@loomcore/common/models';
|
|
4
|
-
export interface IGenericApiService<T extends IEntity> {
|
|
5
|
-
validate(doc: any, isPartial?: boolean): ValueError[] | null;
|
|
6
|
-
validateMany(docs: any[], isPartial?: boolean): ValueError[] | null;
|
|
7
|
-
prepareDataForDb(userContext: IUserContext, entity: T, isCreate?: boolean): Promise<T>;
|
|
8
|
-
prepareDataForDb(userContext: IUserContext, entity: Partial<T>, isCreate?: boolean): Promise<Partial<T>>;
|
|
9
|
-
prepareDataForDb(userContext: IUserContext, entity: T[], isCreate?: boolean): Promise<T[]>;
|
|
10
|
-
prepareDataForDb(userContext: IUserContext, entity: Partial<T>[], isCreate?: boolean): Promise<Partial<T>[]>;
|
|
11
|
-
prepareDataForBatchUpdate(userContext: IUserContext, entities: Partial<T>[]): Promise<Partial<T>[]>;
|
|
12
|
-
getAll(userContext: IUserContext): Promise<T[]>;
|
|
13
|
-
get(userContext: IUserContext, queryOptions: IQueryOptions): Promise<IPagedResult<T>>;
|
|
14
|
-
getById(userContext: IUserContext, id: string): Promise<T>;
|
|
15
|
-
getCount(userContext: IUserContext): Promise<number>;
|
|
16
|
-
create(userContext: IUserContext, entity: T | Partial<T>): Promise<T | null>;
|
|
17
|
-
createMany(userContext: IUserContext, entities: T[]): Promise<T[]>;
|
|
18
|
-
batchUpdate(userContext: IUserContext, entities: Partial<T>[]): Promise<T[]>;
|
|
19
|
-
fullUpdateById(userContext: IUserContext, id: string, entity: T): Promise<T>;
|
|
20
|
-
partialUpdateById(userContext: IUserContext, id: string, entity: Partial<T>): Promise<T>;
|
|
21
|
-
partialUpdateByIdWithoutBeforeAndAfter(userContext: IUserContext, id: string, entity: T): Promise<T>;
|
|
22
|
-
update(userContext: IUserContext, queryObject: any, entity: Partial<T>): Promise<T[]>;
|
|
23
|
-
deleteById(userContext: IUserContext, id: string): Promise<DeleteResult>;
|
|
24
|
-
deleteMany(userContext: IUserContext, queryObject: any): Promise<DeleteResult>;
|
|
25
|
-
find(userContext: IUserContext, mongoQueryObject: any, options?: any): Promise<T[]>;
|
|
26
|
-
findOne(userContext: IUserContext, mongoQueryObject: any, options?: any): Promise<T>;
|
|
27
|
-
}
|
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
import { Db, Collection, DeleteResult, Document, FindOptions } from 'mongodb';
|
|
2
|
-
import { ValueError } from '@sinclair/typebox/errors';
|
|
3
|
-
import { IUserContext, IEntity, IQueryOptions, IPagedResult, IModelSpec } from '@loomcore/common/models';
|
|
4
|
-
import { IGenericApiService } from './generic-api-service.interface.js';
|
|
5
|
-
export declare class GenericApiService<T extends IEntity> implements IGenericApiService<T> {
|
|
6
|
-
protected db: Db;
|
|
7
|
-
protected pluralResourceName: string;
|
|
8
|
-
protected singularResourceName: string;
|
|
9
|
-
protected collection: Collection;
|
|
10
|
-
protected modelSpec?: IModelSpec;
|
|
11
|
-
constructor(db: Db, pluralResourceName: string, singularResourceName: string, modelSpec?: IModelSpec);
|
|
12
|
-
validate(doc: any, isPartial?: boolean): ValueError[] | null;
|
|
13
|
-
validateMany(docs: any[], isPartial?: boolean): ValueError[] | null;
|
|
14
|
-
protected getAdditionalPipelineStages(): any[];
|
|
15
|
-
protected createAggregationPipeline(userContext: IUserContext, query: any, queryOptions?: IQueryOptions): any[];
|
|
16
|
-
getAll(userContext: IUserContext): Promise<T[]>;
|
|
17
|
-
get(userContext: IUserContext, queryOptions?: IQueryOptions): Promise<IPagedResult<T>>;
|
|
18
|
-
getById(userContext: IUserContext, id: string): Promise<T>;
|
|
19
|
-
getCount(userContext: IUserContext): Promise<number>;
|
|
20
|
-
create(userContext: IUserContext, preparedEntity: T | Partial<T>): Promise<T | null>;
|
|
21
|
-
createMany(userContext: IUserContext, preparedEntities: T[]): Promise<T[]>;
|
|
22
|
-
batchUpdate(userContext: IUserContext, preparedEntities: Partial<T>[]): Promise<T[]>;
|
|
23
|
-
fullUpdateById(userContext: IUserContext, id: string, preparedEntity: T): Promise<T>;
|
|
24
|
-
partialUpdateById(userContext: IUserContext, id: string, preparedEntity: Partial<T>): Promise<T>;
|
|
25
|
-
partialUpdateByIdWithoutBeforeAndAfter(userContext: IUserContext, id: string, preparedEntity: T): Promise<T>;
|
|
26
|
-
update(userContext: IUserContext, queryObject: any, preparedEntity: Partial<T>): Promise<T[]>;
|
|
27
|
-
deleteById(userContext: IUserContext, id: string): Promise<DeleteResult>;
|
|
28
|
-
deleteMany(userContext: IUserContext, queryObject: any): Promise<DeleteResult>;
|
|
29
|
-
find(userContext: IUserContext, mongoQueryObject: any, options?: FindOptions<Document> | undefined): Promise<T[]>;
|
|
30
|
-
findOne(userContext: IUserContext, mongoQueryObject: any, options?: FindOptions<Document> | undefined): Promise<T>;
|
|
31
|
-
auditForCreate(userContext: IUserContext, doc: any): void;
|
|
32
|
-
auditForUpdate(userContext: IUserContext, doc: any): void;
|
|
33
|
-
onBeforeCreate<E extends T | T[] | Partial<T> | Partial<T>[]>(userContext: IUserContext, entities: E): Promise<E | E[]>;
|
|
34
|
-
onAfterCreate<E extends T | T[]>(userContext: IUserContext, entities: E): Promise<E | E[]>;
|
|
35
|
-
onBeforeUpdate<E extends T | T[] | Partial<T> | Partial<T>[]>(userContext: IUserContext, entities: E): Promise<E | E[]>;
|
|
36
|
-
onAfterUpdate<E extends T | T[] | Partial<T> | Partial<T>[]>(userContext: IUserContext | undefined, entities: E): Promise<E>;
|
|
37
|
-
onBeforeDelete(userContext: IUserContext, queryObject: any): Promise<any>;
|
|
38
|
-
onAfterDelete(userContext: IUserContext, queryObject: any): Promise<any>;
|
|
39
|
-
transformList(list: any[]): T[];
|
|
40
|
-
transformSingle(single: any): T;
|
|
41
|
-
private stripSenderProvidedSystemProperties;
|
|
42
|
-
prepareDataForDb(userContext: IUserContext, entity: T, isCreate?: boolean): Promise<T>;
|
|
43
|
-
prepareDataForDb(userContext: IUserContext, entity: Partial<T>, isCreate?: boolean): Promise<Partial<T>>;
|
|
44
|
-
prepareDataForDb(userContext: IUserContext, entity: T[], isCreate?: boolean): Promise<T[]>;
|
|
45
|
-
prepareDataForDb(userContext: IUserContext, entity: Partial<T>[], isCreate?: boolean): Promise<Partial<T>[]>;
|
|
46
|
-
prepareDataForBatchUpdate(userContext: IUserContext, entities: Partial<T>[]): Promise<Partial<T>[]>;
|
|
47
|
-
protected prepareEntity(userContext: IUserContext, entity: T | Partial<T>, isCreate: boolean, allowId?: boolean): Promise<T | Partial<T>>;
|
|
48
|
-
protected prepareQuery(userContext: IUserContext | undefined, query: any): any;
|
|
49
|
-
protected prepareQueryOptions(userContext: IUserContext | undefined, queryOptions: IQueryOptions): IQueryOptions;
|
|
50
|
-
}
|