@aneuhold/be-ts-db-lib 2.0.69 → 2.0.70

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) [2024] [Anton G Neuhold Jr]
3
+ Copyright (c) [2025] [Anton G Neuhold Jr]
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
@@ -18,4 +18,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
18
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
19
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
20
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
21
+ SOFTWARE.
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@aneuhold/be-ts-db-lib",
3
3
  "author": "Anton G. Neuhold Jr.",
4
4
  "license": "MIT",
5
- "version": "2.0.69",
5
+ "version": "2.0.70",
6
6
  "description": "A backend database library meant to actually interact with various databases in personal projects",
7
7
  "packageManager": "pnpm@10.11.1",
8
8
  "type": "module",
@@ -1,61 +0,0 @@
1
- import {
2
- DashboardTask,
3
- DashboardUserConfig,
4
- User
5
- } from '@aneuhold/core-ts-db-lib';
6
- import crypto from 'crypto';
7
- import { afterAll, expect, it } from 'vitest';
8
- import UserRepository from '../../repositories/common/UserRepository.js';
9
- import DashboardTaskRepository from '../../repositories/dashboard/DashboardTaskRepository.js';
10
- import DashboardUserConfigRepository from '../../repositories/dashboard/DashboardUserConfigRepository.js';
11
- import DocumentDb from '../../util/DocumentDb.js';
12
- import { cleanupDoc, getTestUserName } from '../testsUtil.js';
13
-
14
- it('can create a new document and delete it', async () => {
15
- const userRepository = UserRepository.getRepo();
16
- const newUser = new User(getTestUserName());
17
- const createResult = await userRepository.insertNew(newUser);
18
- expect(createResult).toBeTruthy();
19
-
20
- await cleanupDoc(userRepository, newUser);
21
- }, 10000);
22
-
23
- // -- Manual Database Operations Section -- //
24
-
25
- it.skip('can add a new test user', async () => {
26
- const userRepository = UserRepository.getRepo();
27
- const newUser = new User('someUser');
28
- newUser.auth.password = crypto.randomUUID();
29
- const createResult = await userRepository.insertNew(newUser);
30
- expect(createResult).toBeTruthy();
31
- });
32
-
33
- it.skip('can create a dashboard config for a user', async () => {
34
- const userRepo = UserRepository.getRepo();
35
- const user = await userRepo.get({ userName: 'usernameHere' });
36
- expect(user).toBeTruthy();
37
-
38
- if (user) {
39
- const configRepo = DashboardUserConfigRepository.getRepo();
40
- const newConfig = new DashboardUserConfig(user._id);
41
- newConfig.enableDevMode = true;
42
- await configRepo.insertNew(newConfig);
43
- }
44
- });
45
-
46
- it.skip(`can create a new task for a user`, async () => {
47
- const userRepo = UserRepository.getRepo();
48
- const user = await userRepo.get({ userName: 'usernameHere' });
49
- expect(user).toBeTruthy();
50
-
51
- if (user) {
52
- const taskRepo = DashboardTaskRepository.getRepo();
53
- const newTask = new DashboardTask(user._id);
54
- newTask.title = 'Test Task';
55
- await taskRepo.insertNew(newTask);
56
- }
57
- });
58
-
59
- afterAll(async () => {
60
- return DocumentDb.closeDbConnection();
61
- });
@@ -1,113 +0,0 @@
1
- import { User } from '@aneuhold/core-ts-db-lib';
2
- import { afterAll, describe, expect, it } from 'vitest';
3
- import ApiKeyRepository from '../../../repositories/common/ApiKeyRepository.js';
4
- import UserRepository from '../../../repositories/common/UserRepository.js';
5
- import DocumentDb from '../../../util/DocumentDb.js';
6
- import { cleanupDoc, expectToThrow, getTestUserName } from '../../testsUtil.js';
7
-
8
- const userRepo = UserRepository.getRepo();
9
-
10
- describe('Create operations', () => {
11
- it('can create a new user', async () => {
12
- const newUser = new User(getTestUserName());
13
- const insertResult = await userRepo.insertNew(newUser);
14
- expect(insertResult).toBeTruthy();
15
-
16
- await cleanupDoc(userRepo, newUser);
17
- });
18
-
19
- it('can create a new user and the new user gets an API key', async () => {
20
- const newUser = new User(getTestUserName());
21
- const insertResult = await userRepo.insertNew(newUser);
22
- expect(insertResult).toBeTruthy();
23
- const apiKey = await ApiKeyRepository.getRepo().get({
24
- userId: newUser._id
25
- });
26
- expect(apiKey).toBeTruthy();
27
-
28
- await cleanupDoc(userRepo, newUser);
29
- const apiKeyThatShouldNotExist = await ApiKeyRepository.getRepo().get({
30
- userId: newUser._id
31
- });
32
- expect(apiKeyThatShouldNotExist).toBeFalsy();
33
- });
34
-
35
- it('throws if the username is a duplicate username', async () => {
36
- const duplicateUserName = getTestUserName();
37
- const newUser1 = new User(duplicateUserName);
38
- const newUser2 = new User(duplicateUserName);
39
-
40
- const insertResult = await userRepo.insertNew(newUser1);
41
- expect(insertResult).toBeTruthy();
42
-
43
- await expectToThrow(async () => {
44
- await userRepo.insertNew(newUser2);
45
- });
46
-
47
- await cleanupDoc(userRepo, newUser1);
48
- });
49
- });
50
-
51
- describe('Update operations', () => {
52
- it('succeeds in updating the username if the username doesnt already exist', async () => {
53
- const userName1 = getTestUserName();
54
- const userName2 = getTestUserName();
55
- const newUser = new User(userName1);
56
-
57
- // Insert the user
58
- const insertResult = await userRepo.insertNew(newUser);
59
- expect(insertResult).toBeTruthy();
60
-
61
- // Try to update the user
62
- newUser.userName = userName2;
63
- const updateResult = await userRepo.update(newUser);
64
- expect(updateResult.acknowledged).toBeTruthy();
65
-
66
- await cleanupDoc(userRepo, newUser);
67
- });
68
-
69
- it('throws if no id is defined', async () => {
70
- const newUser = new User(getTestUserName()) as Partial<User>;
71
- delete newUser._id;
72
- await expectToThrow(async () => {
73
- await userRepo.update(newUser);
74
- });
75
- });
76
-
77
- it('throws if the username is updated and already exists', async () => {
78
- const userName1 = getTestUserName();
79
- const userName2 = getTestUserName();
80
- const newUser = new User(userName1);
81
- const userWithOtherUserName = new User(userName2);
82
-
83
- // Insert the users
84
- const insertResult1 = await userRepo.insertNew(newUser);
85
- expect(insertResult1).toBeTruthy();
86
- const insertResult2 = await userRepo.insertNew(userWithOtherUserName);
87
- expect(insertResult2).toBeTruthy();
88
-
89
- // Try to update the first user
90
- newUser.userName = userName2;
91
- await expectToThrow(async () => {
92
- await userRepo.update(newUser);
93
- });
94
-
95
- await Promise.all([
96
- cleanupDoc(userRepo, newUser),
97
- cleanupDoc(userRepo, userWithOtherUserName)
98
- ]);
99
- });
100
-
101
- it('throws if the user doesnt exist', async () => {
102
- const newUser = new User(getTestUserName());
103
-
104
- // Try to update the user
105
- await expectToThrow(async () => {
106
- await userRepo.update(newUser);
107
- });
108
- });
109
- });
110
-
111
- afterAll(async () => {
112
- await DocumentDb.closeDbConnection();
113
- });
@@ -1,55 +0,0 @@
1
- import {
2
- NonogramKatanaItem,
3
- NonogramKatanaItemName,
4
- User
5
- } from '@aneuhold/core-ts-db-lib';
6
- import crypto from 'crypto';
7
- import { describe, expect, it } from 'vitest';
8
- import UserRepository from '../../../repositories/common/UserRepository.js';
9
- import DashboardNonogramKatanaItemRepository from '../../../repositories/dashboard/DashboardNonogramKatanaItemRepository.js';
10
- import { cleanupDoc, getTestUserName } from '../../testsUtil.js';
11
-
12
- const userRepo = UserRepository.getRepo();
13
- const itemRepo = DashboardNonogramKatanaItemRepository.getRepo();
14
-
15
- describe('Create operations', () => {
16
- it('can create a new nonogram katana item', async () => {
17
- const newUser = await createNewTestUser();
18
- const newItem = new NonogramKatanaItem(
19
- newUser._id,
20
- NonogramKatanaItemName.Anchor
21
- );
22
- const insertResult = await itemRepo.insertNew(newItem);
23
- expect(insertResult).toBeTruthy();
24
-
25
- const newItemFromDb = await itemRepo.get({ _id: newItem._id });
26
- expect(newItemFromDb).toBeTruthy();
27
-
28
- await cleanupDoc(userRepo, newUser);
29
- });
30
- });
31
-
32
- /**
33
- * Deletes all Nonogram Katana Items!
34
- *
35
- * To just do a cleanup, put `only` after `it`. So `it.only('can delete all items'`
36
- */
37
- it.skip('can delete all items', async () => {
38
- const result = await itemRepo.deleteAll();
39
- expect(result.acknowledged).toBeTruthy();
40
- });
41
-
42
- /**
43
- * Create a new test user
44
- *
45
- * @returns The new user
46
- */
47
- async function createNewTestUser() {
48
- const newUser = new User(
49
- getTestUserName(`${crypto.randomUUID()}nonogramKatanaItemTest`)
50
- );
51
- newUser.projectAccess.dashboard = true;
52
- const insertResult = await userRepo.insertNew(newUser);
53
- expect(insertResult).toBeTruthy();
54
- return newUser;
55
- }
@@ -1,14 +0,0 @@
1
- import { expect, it } from 'vitest';
2
- import DashboardNonogramKatanaUpgradeRepository from '../../../repositories/dashboard/DashboardNonogramKatanaUpgradeRepository.js';
3
-
4
- const upgradeRepo = DashboardNonogramKatanaUpgradeRepository.getRepo();
5
-
6
- /**
7
- * Deletes all Nonogram Katana Upgrades!
8
- *
9
- * To just do a cleanup, put `only` after `it`. So `it.only('can delete all upgrades'`
10
- */
11
- it.skip('can delete all upgrades', async () => {
12
- const result = await upgradeRepo.deleteAll();
13
- expect(result.acknowledged).toBeTruthy();
14
- });
@@ -1,101 +0,0 @@
1
- import { DashboardTask, User } from '@aneuhold/core-ts-db-lib';
2
- import crypto from 'crypto';
3
- import { afterAll, describe, expect, it } from 'vitest';
4
- import UserRepository from '../../../repositories/common/UserRepository.js';
5
- import DashboardTaskRepository from '../../../repositories/dashboard/DashboardTaskRepository.js';
6
- import DashboardUserConfigRepository from '../../../repositories/dashboard/DashboardUserConfigRepository.js';
7
- import DocumentDb from '../../../util/DocumentDb.js';
8
- import { cleanupDoc, getTestUserName } from '../../testsUtil.js';
9
-
10
- const userRepo = UserRepository.getRepo();
11
- const taskRepo = DashboardTaskRepository.getRepo();
12
-
13
- describe('Create operations', () => {
14
- it('can create a new task', async () => {
15
- const newUser = await createNewTestUser();
16
- const newTask = new DashboardTask(newUser._id);
17
- const insertResult = await taskRepo.insertNew(newTask);
18
- expect(insertResult).toBeTruthy();
19
- const insertedTask = await taskRepo.get({ _id: newTask._id });
20
- expect(insertedTask).toBeTruthy();
21
- expect(typeof insertedTask?.createdDate).toBe('object');
22
- if (!insertedTask) {
23
- return;
24
- }
25
-
26
- await cleanupDoc(userRepo, newUser);
27
- // Task should be deleted
28
- const deletedTask = await taskRepo.get({ _id: newTask._id });
29
- expect(deletedTask).toBeFalsy();
30
- });
31
- });
32
-
33
- describe('Get operations', () => {
34
- it('can get a set of tasks for a user, without any tasks from other users', async () => {
35
- const newUser = await createNewTestUser();
36
- const newTask = new DashboardTask(newUser._id);
37
- const insertResult = await taskRepo.insertNew(newTask);
38
- expect(insertResult).toBeTruthy();
39
-
40
- const otherUser = await createNewTestUser();
41
- const otherUserTask = new DashboardTask(otherUser._id);
42
- const insertResult2 = await taskRepo.insertNew(otherUserTask);
43
- expect(insertResult2).toBeTruthy();
44
-
45
- const tasks = await taskRepo.getAllForUser(newUser._id);
46
- expect(tasks.length).toBe(1);
47
- expect(tasks[0]._id.toString()).toEqual(newTask._id.toString());
48
-
49
- await cleanupDoc(userRepo, newUser);
50
- await cleanupDoc(userRepo, otherUser);
51
- });
52
-
53
- it('can get a set of tasks for a user, including tasks shared with that user', async () => {
54
- const configRepo = DashboardUserConfigRepository.getRepo();
55
- const newUser = await createNewTestUser();
56
- const newTask = new DashboardTask(newUser._id);
57
- const insertResult = await taskRepo.insertNew(newTask);
58
- expect(insertResult).toBeTruthy();
59
-
60
- const otherUser = await createNewTestUser();
61
- const otherUserTask = new DashboardTask(otherUser._id);
62
-
63
- // Add other user as collaborator
64
- const otherUserConfig = await configRepo.get({ userId: otherUser._id });
65
- expect(otherUserConfig).toBeTruthy();
66
- if (!otherUserConfig) {
67
- return;
68
- }
69
- otherUserConfig.collaborators.push(newUser._id);
70
- await configRepo.update(otherUserConfig);
71
- otherUserTask.sharedWith.push(newUser._id);
72
- const insertResult2 = await taskRepo.insertNew(otherUserTask);
73
- expect(insertResult2).toBeTruthy();
74
-
75
- const tasks = await taskRepo.getAllForUser(newUser._id);
76
- expect(tasks.length).toBe(2);
77
- expect(tasks[0]._id.toString()).toEqual(newTask._id.toString());
78
- expect(tasks[1]._id.toString()).toEqual(otherUserTask._id.toString());
79
-
80
- await cleanupDoc(userRepo, newUser);
81
- await cleanupDoc(userRepo, otherUser);
82
- });
83
- });
84
-
85
- afterAll(async () => {
86
- return DocumentDb.closeDbConnection();
87
- });
88
-
89
- /**
90
- * Create a new test user
91
- *
92
- * @returns The new user
93
- */
94
- async function createNewTestUser(): Promise<User> {
95
- const newUser = new User(
96
- getTestUserName(`${crypto.randomUUID()}dashboardTaskTest`)
97
- );
98
- const insertResult = await userRepo.insertNew(newUser);
99
- expect(insertResult).toBeTruthy();
100
- return newUser;
101
- }
@@ -1,155 +0,0 @@
1
- import { User } from '@aneuhold/core-ts-db-lib';
2
- import crypto from 'crypto';
3
- import { afterAll, describe, expect, it } from 'vitest';
4
- import UserRepository from '../../../repositories/common/UserRepository.js';
5
- import DashboardUserConfigRepository from '../../../repositories/dashboard/DashboardUserConfigRepository.js';
6
- import DocumentDb from '../../../util/DocumentDb.js';
7
- import { cleanupDoc, getTestUserName } from '../../testsUtil.js';
8
-
9
- const userRepo = UserRepository.getRepo();
10
- const configRepo = DashboardUserConfigRepository.getRepo();
11
-
12
- describe('Create operations', () => {
13
- it('can create a new user config', async () => {
14
- // User configs are created automatically when a new user is created
15
- const newUser = await createNewTestUser();
16
- const newConfig = await configRepo.get({ userId: newUser._id });
17
- expect(newConfig).toBeTruthy();
18
- if (!newConfig) {
19
- return;
20
- }
21
-
22
- await cleanupDoc(userRepo, newUser);
23
- });
24
- });
25
-
26
- describe('Update operations', () => {
27
- it('can update an existing user config', async () => {
28
- const newUser = await createNewTestUser();
29
- const newConfig = await configRepo.get({ userId: newUser._id });
30
- expect(newConfig).toBeTruthy();
31
- if (!newConfig) {
32
- return;
33
- }
34
-
35
- newConfig.enableDevMode = true;
36
- await configRepo.update(newConfig);
37
- const updatedConfig = await configRepo.get({ _id: newConfig._id });
38
- expect(updatedConfig?.enableDevMode).toBe(true);
39
-
40
- await cleanupDoc(userRepo, newUser);
41
- });
42
-
43
- it('can add collaborators to an existing user config', async () => {
44
- const newUser = await createNewTestUser();
45
- const newConfig = await configRepo.get({ userId: newUser._id });
46
- expect(newConfig).toBeTruthy();
47
- if (!newConfig) {
48
- return;
49
- }
50
-
51
- const newCollaborator = await createNewTestUser();
52
- newConfig.collaborators.push(newCollaborator._id);
53
- await configRepo.update(newConfig);
54
- let updatedConfig = await configRepo.get({ _id: newConfig._id });
55
- expect(updatedConfig?.collaborators.map((x) => x.toString())).toContain(
56
- newCollaborator._id.toString()
57
- );
58
-
59
- await cleanupDoc(userRepo, newCollaborator);
60
- // Ensure the collaborator is deleted automatically when the user is deleted
61
- updatedConfig = await configRepo.get({ _id: newConfig._id });
62
- expect(updatedConfig?.collaborators.map((x) => x.toString())).not.toContain(
63
- newCollaborator._id.toString()
64
- );
65
-
66
- await cleanupDoc(userRepo, newUser);
67
- });
68
-
69
- /**
70
- * Pretty huge test that ensures the full range of functionality is there
71
- * for collaborators.
72
- */
73
- it('correctly updates the collaborators for groups of users', async () => {
74
- const newUser1 = await createNewTestUser();
75
- const newConfig1 = await configRepo.get({ userId: newUser1._id });
76
- expect(newConfig1).toBeTruthy();
77
- if (!newConfig1) {
78
- return;
79
- }
80
-
81
- const newUser2 = await createNewTestUser();
82
- const newConfig2 = await configRepo.get({ userId: newUser2._id });
83
- expect(newConfig2).toBeTruthy();
84
- if (!newConfig2) {
85
- return;
86
- }
87
-
88
- const newUser3 = await createNewTestUser();
89
- const newConfig3 = await configRepo.get({ userId: newUser3._id });
90
- expect(newConfig3).toBeTruthy();
91
- if (!newConfig3) {
92
- return;
93
- }
94
-
95
- newConfig1.collaborators.push(newUser2._id);
96
- const updateResult1 = await configRepo.update(newConfig1);
97
- expect(updateResult1).toBeTruthy();
98
- let updatedConfig2 = await configRepo.get({ _id: newConfig2._id });
99
- expect(updatedConfig2?.collaborators.map((x) => x.toString())).toContain(
100
- newUser1._id.toString()
101
- );
102
-
103
- newConfig3.collaborators.push(newUser1._id, newUser2._id);
104
- let updateResult3 = await configRepo.update(newConfig3);
105
- expect(updateResult3).toBeTruthy();
106
- let updatedConfig1 = await configRepo.get({ _id: newConfig1._id });
107
- expect(updatedConfig1?.collaborators.map((x) => x.toString())).toContain(
108
- newUser3._id.toString()
109
- );
110
- expect(updatedConfig1?.collaborators.map((x) => x.toString())).toContain(
111
- newUser2._id.toString()
112
- );
113
-
114
- newConfig3.collaborators = [];
115
- updateResult3 = await configRepo.update(newConfig3);
116
- expect(updateResult3).toBeTruthy();
117
- updatedConfig1 = await configRepo.get({ _id: newConfig1._id });
118
- expect(
119
- updatedConfig1?.collaborators.map((x) => x.toString())
120
- ).toContainEqual(newUser2._id.toString());
121
- expect(
122
- updatedConfig1?.collaborators.map((x) => x.toString())
123
- ).not.toContain(newUser3._id.toString());
124
- updatedConfig2 = await configRepo.get({ _id: newConfig2._id });
125
- expect(
126
- updatedConfig2?.collaborators.map((x) => x.toString())
127
- ).toContainEqual(newUser1._id.toString());
128
- expect(
129
- updatedConfig2?.collaborators.map((x) => x.toString())
130
- ).not.toContain(newUser3._id.toString());
131
-
132
- await cleanupDoc(userRepo, newUser1);
133
- await cleanupDoc(userRepo, newUser2);
134
- await cleanupDoc(userRepo, newUser3);
135
- }, 10000);
136
- });
137
-
138
- afterAll(async () => {
139
- return DocumentDb.closeDbConnection();
140
- });
141
-
142
- /**
143
- * Create a new test user
144
- *
145
- * @returns The new user
146
- */
147
- async function createNewTestUser() {
148
- const newUser = new User(
149
- getTestUserName(`${crypto.randomUUID()}userconfigtest`)
150
- );
151
- newUser.projectAccess.dashboard = true;
152
- const insertResult = await userRepo.insertNew(newUser);
153
- expect(insertResult).toBeTruthy();
154
- return newUser;
155
- }