@unchainedshop/core-bookmarks 4.3.4 → 4.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +74 -2
- package/lib/bookmarks-index.d.ts +2 -3
- package/lib/bookmarks-index.js +2 -3
- package/lib/db/BookmarksCollection.d.ts +1 -2
- package/lib/db/BookmarksCollection.js +1 -1
- package/lib/module/configureBookmarksModule.d.ts +1 -2
- package/lib/module/configureBookmarksModule.js +2 -5
- package/package.json +11 -9
- package/lib/bookmarks-index.d.ts.map +0 -1
- package/lib/bookmarks-index.js.map +0 -1
- package/lib/db/BookmarksCollection.d.ts.map +0 -1
- package/lib/db/BookmarksCollection.js.map +0 -1
- package/lib/module/configureBookmarksModule.d.ts.map +0 -1
- package/lib/module/configureBookmarksModule.js.map +0 -1
- package/src/bookmarks-index.ts +0 -2
- package/src/db/BookmarksCollection.ts +0 -11
- package/src/module/configureBookmarksModule.ts +0 -96
- package/tests/bookmarks-index.test.ts +0 -8
- package/tsconfig.json +0 -10
package/README.md
CHANGED
|
@@ -1,3 +1,75 @@
|
|
|
1
|
-
|
|
1
|
+
[](https://npmjs.com/package/@unchainedshop/core-bookmarks)
|
|
2
|
+
[](https://opensource.org/licenses/EUPL-1.2)
|
|
2
3
|
|
|
3
|
-
|
|
4
|
+
# @unchainedshop/core-bookmarks
|
|
5
|
+
|
|
6
|
+
Bookmark management module for the Unchained Engine. Allows users to bookmark products for later reference.
|
|
7
|
+
|
|
8
|
+
## Installation
|
|
9
|
+
|
|
10
|
+
```bash
|
|
11
|
+
npm install @unchainedshop/core-bookmarks
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
## Usage
|
|
15
|
+
|
|
16
|
+
```typescript
|
|
17
|
+
import { configureBookmarksModule } from '@unchainedshop/core-bookmarks';
|
|
18
|
+
|
|
19
|
+
const bookmarksModule = await configureBookmarksModule({ db });
|
|
20
|
+
|
|
21
|
+
// Create a bookmark
|
|
22
|
+
const bookmarkId = await bookmarksModule.create({
|
|
23
|
+
userId: 'user-123',
|
|
24
|
+
productId: 'product-456',
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
// Find bookmarks by user
|
|
28
|
+
const bookmarks = await bookmarksModule.findBookmarksByUserId('user-123');
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## API Overview
|
|
32
|
+
|
|
33
|
+
### Module Configuration
|
|
34
|
+
|
|
35
|
+
| Export | Description |
|
|
36
|
+
|--------|-------------|
|
|
37
|
+
| `configureBookmarksModule` | Configure and return the bookmarks module |
|
|
38
|
+
|
|
39
|
+
### Queries
|
|
40
|
+
|
|
41
|
+
| Method | Description |
|
|
42
|
+
|--------|-------------|
|
|
43
|
+
| `findBookmarkById` | Find a bookmark by its ID |
|
|
44
|
+
| `findBookmarksByUserId` | Find all bookmarks for a user |
|
|
45
|
+
| `findBookmarks` | Find bookmarks with custom query |
|
|
46
|
+
|
|
47
|
+
### Mutations
|
|
48
|
+
|
|
49
|
+
| Method | Description |
|
|
50
|
+
|--------|-------------|
|
|
51
|
+
| `create` | Create a new bookmark |
|
|
52
|
+
| `update` | Update an existing bookmark |
|
|
53
|
+
| `delete` | Delete a bookmark |
|
|
54
|
+
| `deleteByUserId` | Delete all bookmarks for a user |
|
|
55
|
+
| `deleteByProductId` | Delete all bookmarks for a product |
|
|
56
|
+
| `replaceUserId` | Transfer bookmarks between users |
|
|
57
|
+
|
|
58
|
+
### Types
|
|
59
|
+
|
|
60
|
+
| Export | Description |
|
|
61
|
+
|--------|-------------|
|
|
62
|
+
| `Bookmark` | Bookmark document type |
|
|
63
|
+
| `BookmarksModule` | Module interface type |
|
|
64
|
+
|
|
65
|
+
## Events
|
|
66
|
+
|
|
67
|
+
| Event | Description |
|
|
68
|
+
|-------|-------------|
|
|
69
|
+
| `BOOKMARK_CREATE` | Emitted when a bookmark is created |
|
|
70
|
+
| `BOOKMARK_UPDATE` | Emitted when a bookmark is updated |
|
|
71
|
+
| `BOOKMARK_REMOVE` | Emitted when a bookmark is removed |
|
|
72
|
+
|
|
73
|
+
## License
|
|
74
|
+
|
|
75
|
+
EUPL-1.2
|
package/lib/bookmarks-index.d.ts
CHANGED
|
@@ -1,3 +1,2 @@
|
|
|
1
|
-
export * from './module/configureBookmarksModule.
|
|
2
|
-
export * from './db/BookmarksCollection.
|
|
3
|
-
//# sourceMappingURL=bookmarks-index.d.ts.map
|
|
1
|
+
export * from './module/configureBookmarksModule.ts';
|
|
2
|
+
export * from './db/BookmarksCollection.ts';
|
package/lib/bookmarks-index.js
CHANGED
|
@@ -1,3 +1,2 @@
|
|
|
1
|
-
export * from
|
|
2
|
-
export * from
|
|
3
|
-
//# sourceMappingURL=bookmarks-index.js.map
|
|
1
|
+
export * from "./module/configureBookmarksModule.js";
|
|
2
|
+
export * from "./db/BookmarksCollection.js";
|
|
@@ -1,4 +1,3 @@
|
|
|
1
1
|
import { mongodb } from '@unchainedshop/mongodb';
|
|
2
|
-
import { Bookmark } from '../bookmarks-index.
|
|
2
|
+
import type { Bookmark } from '../bookmarks-index.ts';
|
|
3
3
|
export declare const BookmarksCollection: (db: mongodb.Db) => Promise<mongodb.Collection<Bookmark>>;
|
|
4
|
-
//# sourceMappingURL=BookmarksCollection.d.ts.map
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
+
import { mongodb } from '@unchainedshop/mongodb';
|
|
1
2
|
export const BookmarksCollection = async (db) => {
|
|
2
3
|
const Bookmarks = db.collection('bookmarks');
|
|
3
4
|
await Bookmarks.createIndex({ userId: 1 });
|
|
4
5
|
await Bookmarks.createIndex({ productId: 1 });
|
|
5
6
|
return Bookmarks;
|
|
6
7
|
};
|
|
7
|
-
//# sourceMappingURL=BookmarksCollection.js.map
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { mongodb, TimestampFields, ModuleInput } from '@unchainedshop/mongodb';
|
|
1
|
+
import { mongodb, type TimestampFields, type ModuleInput } from '@unchainedshop/mongodb';
|
|
2
2
|
export type Bookmark = {
|
|
3
3
|
_id: string;
|
|
4
4
|
userId: string;
|
|
@@ -17,4 +17,3 @@ export declare const configureBookmarksModule: ({ db }: ModuleInput<Record<strin
|
|
|
17
17
|
delete: (bookmarkId: string) => Promise<number>;
|
|
18
18
|
}>;
|
|
19
19
|
export type BookmarksModule = Awaited<ReturnType<typeof configureBookmarksModule>>;
|
|
20
|
-
//# sourceMappingURL=configureBookmarksModule.d.ts.map
|
|
@@ -1,19 +1,17 @@
|
|
|
1
1
|
import { emit, registerEvents } from '@unchainedshop/events';
|
|
2
|
-
import { generateDbFilterById, generateDbObjectId, } from '@unchainedshop/mongodb';
|
|
3
|
-
import { BookmarksCollection } from
|
|
2
|
+
import { generateDbFilterById, generateDbObjectId, mongodb, } from '@unchainedshop/mongodb';
|
|
3
|
+
import { BookmarksCollection } from "../db/BookmarksCollection.js";
|
|
4
4
|
const BOOKMARK_EVENTS = ['BOOKMARK_CREATE', 'BOOKMARK_UPDATE', 'BOOKMARK_REMOVE'];
|
|
5
5
|
export const configureBookmarksModule = async ({ db }) => {
|
|
6
6
|
registerEvents(BOOKMARK_EVENTS);
|
|
7
7
|
const Bookmarks = await BookmarksCollection(db);
|
|
8
8
|
return {
|
|
9
|
-
// Queries
|
|
10
9
|
findBookmarksByUserId: async (userId) => Bookmarks.find({ userId }).toArray(),
|
|
11
10
|
findBookmarkById: async (bookmarkId) => {
|
|
12
11
|
const filter = generateDbFilterById(bookmarkId);
|
|
13
12
|
return Bookmarks.findOne(filter, {});
|
|
14
13
|
},
|
|
15
14
|
findBookmarks: async (query) => Bookmarks.find(query).toArray(),
|
|
16
|
-
// Mutations
|
|
17
15
|
replaceUserId: async (fromUserId, toUserId, bookmarkIds) => {
|
|
18
16
|
const selector = { userId: fromUserId };
|
|
19
17
|
if (bookmarkIds) {
|
|
@@ -65,4 +63,3 @@ export const configureBookmarksModule = async ({ db }) => {
|
|
|
65
63
|
},
|
|
66
64
|
};
|
|
67
65
|
};
|
|
68
|
-
//# sourceMappingURL=configureBookmarksModule.js.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@unchainedshop/core-bookmarks",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.4.0",
|
|
4
4
|
"main": "lib/bookmarks-index.js",
|
|
5
5
|
"types": "lib/bookmarks-index.d.ts",
|
|
6
6
|
"type": "module",
|
|
@@ -9,9 +9,10 @@
|
|
|
9
9
|
"build": "tsc -b",
|
|
10
10
|
"prepublishOnly": "npm run clean && npm run build",
|
|
11
11
|
"watch": "tsc -w",
|
|
12
|
-
"test": "
|
|
13
|
-
"test:watch": "
|
|
12
|
+
"test": "node --test",
|
|
13
|
+
"test:watch": "node --test --watch"
|
|
14
14
|
},
|
|
15
|
+
"sideEffects": false,
|
|
15
16
|
"repository": {
|
|
16
17
|
"type": "git",
|
|
17
18
|
"url": "git+https://github.com/unchainedshop/unchained.git"
|
|
@@ -22,8 +23,10 @@
|
|
|
22
23
|
"core"
|
|
23
24
|
],
|
|
24
25
|
"authors": [
|
|
25
|
-
"
|
|
26
|
-
"
|
|
26
|
+
"Pascal Kaufmann",
|
|
27
|
+
"Mikael Araya",
|
|
28
|
+
"Vedran Rudelj",
|
|
29
|
+
"Joël Meiller"
|
|
27
30
|
],
|
|
28
31
|
"license": "EUPL-1.2",
|
|
29
32
|
"bugs": {
|
|
@@ -31,12 +34,11 @@
|
|
|
31
34
|
},
|
|
32
35
|
"homepage": "https://github.com/unchainedshop/unchained#readme",
|
|
33
36
|
"dependencies": {
|
|
34
|
-
"@unchainedshop/events": "^4.
|
|
35
|
-
"@unchainedshop/mongodb": "^4.
|
|
37
|
+
"@unchainedshop/events": "^4.4.0",
|
|
38
|
+
"@unchainedshop/mongodb": "^4.4.0"
|
|
36
39
|
},
|
|
37
40
|
"devDependencies": {
|
|
38
|
-
"@types/node": "^
|
|
39
|
-
"tsx": "^4.20.3",
|
|
41
|
+
"@types/node": "^25.0.0",
|
|
40
42
|
"typescript": "^5.8.3"
|
|
41
43
|
}
|
|
42
44
|
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"bookmarks-index.d.ts","sourceRoot":"","sources":["../src/bookmarks-index.ts"],"names":[],"mappings":"AAAA,cAAc,sCAAsC,CAAC;AACrD,cAAc,6BAA6B,CAAC"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"bookmarks-index.js","sourceRoot":"","sources":["../src/bookmarks-index.ts"],"names":[],"mappings":"AAAA,cAAc,sCAAsC,CAAC;AACrD,cAAc,6BAA6B,CAAC"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"BookmarksCollection.d.ts","sourceRoot":"","sources":["../../src/db/BookmarksCollection.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,wBAAwB,CAAC;AACjD,OAAO,EAAE,QAAQ,EAAE,MAAM,uBAAuB,CAAC;AAEjD,eAAO,MAAM,mBAAmB,GAAU,IAAI,OAAO,CAAC,EAAE,0CAOvD,CAAC"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"BookmarksCollection.js","sourceRoot":"","sources":["../../src/db/BookmarksCollection.ts"],"names":[],"mappings":"AAGA,MAAM,CAAC,MAAM,mBAAmB,GAAG,KAAK,EAAE,EAAc,EAAE,EAAE;IAC1D,MAAM,SAAS,GAAG,EAAE,CAAC,UAAU,CAAW,WAAW,CAAC,CAAC;IAEvD,MAAM,SAAS,CAAC,WAAW,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,CAAC,CAAC;IAC3C,MAAM,SAAS,CAAC,WAAW,CAAC,EAAE,SAAS,EAAE,CAAC,EAAE,CAAC,CAAC;IAE9C,OAAO,SAAS,CAAC;AACnB,CAAC,CAAC"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"configureBookmarksModule.d.ts","sourceRoot":"","sources":["../../src/module/configureBookmarksModule.ts"],"names":[],"mappings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,EAAE,GAAsB,EAAE,EAAE;YAC3D,MAAM,SAAS,CAAC,SAAS,CACvB,EAAE,GAAG,EAAE,UAAU,EAAE,EACnB;gBACE,IAAI,EAAE;oBACJ,OAAO,EAAE,IAAI,IAAI,EAAE;oBACnB,GAAG,GAAG;iBACP;aACF,CACF,CAAC;YACF,MAAM,IAAI,CAAC,iBAAiB,EAAE,EAAE,UAAU,EAAE,CAAC,CAAC;YAC9C,OAAO,UAAU,CAAC;QACpB,CAAC;QACD,MAAM,EAAE,KAAK,EAAE,UAAkB,EAAE,EAAE;YACnC,MAAM,EAAE,YAAY,EAAE,GAAG,MAAM,SAAS,CAAC,SAAS,CAAC,EAAE,GAAG,EAAE,UAAU,EAAE,CAAC,CAAC;YACxE,MAAM,IAAI,CAAC,iBAAiB,EAAE,EAAE,UAAU,EAAE,CAAC,CAAC;YAC9C,OAAO,YAAY,CAAC;QACtB,CAAC;KACF,CAAC;AACJ,CAAC,CAAC"}
|
package/src/bookmarks-index.ts
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import { mongodb } from '@unchainedshop/mongodb';
|
|
2
|
-
import { Bookmark } from '../bookmarks-index.js';
|
|
3
|
-
|
|
4
|
-
export const BookmarksCollection = async (db: mongodb.Db) => {
|
|
5
|
-
const Bookmarks = db.collection<Bookmark>('bookmarks');
|
|
6
|
-
|
|
7
|
-
await Bookmarks.createIndex({ userId: 1 });
|
|
8
|
-
await Bookmarks.createIndex({ productId: 1 });
|
|
9
|
-
|
|
10
|
-
return Bookmarks;
|
|
11
|
-
};
|
|
@@ -1,96 +0,0 @@
|
|
|
1
|
-
import { emit, registerEvents } from '@unchainedshop/events';
|
|
2
|
-
import {
|
|
3
|
-
generateDbFilterById,
|
|
4
|
-
generateDbObjectId,
|
|
5
|
-
mongodb,
|
|
6
|
-
TimestampFields,
|
|
7
|
-
ModuleInput,
|
|
8
|
-
} from '@unchainedshop/mongodb';
|
|
9
|
-
import { BookmarksCollection } from '../db/BookmarksCollection.js';
|
|
10
|
-
|
|
11
|
-
const BOOKMARK_EVENTS: string[] = ['BOOKMARK_CREATE', 'BOOKMARK_UPDATE', 'BOOKMARK_REMOVE'];
|
|
12
|
-
|
|
13
|
-
export type Bookmark = {
|
|
14
|
-
_id: string;
|
|
15
|
-
userId: string;
|
|
16
|
-
productId: string;
|
|
17
|
-
meta?: any;
|
|
18
|
-
} & TimestampFields;
|
|
19
|
-
|
|
20
|
-
export const configureBookmarksModule = async ({ db }: ModuleInput<Record<string, never>>) => {
|
|
21
|
-
registerEvents(BOOKMARK_EVENTS);
|
|
22
|
-
|
|
23
|
-
const Bookmarks = await BookmarksCollection(db);
|
|
24
|
-
|
|
25
|
-
return {
|
|
26
|
-
// Queries
|
|
27
|
-
findBookmarksByUserId: async (userId: string): Promise<Bookmark[]> =>
|
|
28
|
-
Bookmarks.find({ userId }).toArray(),
|
|
29
|
-
findBookmarkById: async (bookmarkId: string) => {
|
|
30
|
-
const filter = generateDbFilterById(bookmarkId);
|
|
31
|
-
return Bookmarks.findOne(filter, {});
|
|
32
|
-
},
|
|
33
|
-
findBookmarks: async (query: mongodb.Filter<Bookmark>): Promise<Bookmark[]> =>
|
|
34
|
-
Bookmarks.find(query).toArray(),
|
|
35
|
-
|
|
36
|
-
// Mutations
|
|
37
|
-
replaceUserId: async (
|
|
38
|
-
fromUserId: string,
|
|
39
|
-
toUserId: string,
|
|
40
|
-
bookmarkIds?: string[],
|
|
41
|
-
): Promise<number> => {
|
|
42
|
-
const selector: mongodb.Filter<Bookmark> = { userId: fromUserId };
|
|
43
|
-
if (bookmarkIds) {
|
|
44
|
-
selector._id = { $in: bookmarkIds };
|
|
45
|
-
}
|
|
46
|
-
const result = await Bookmarks.updateMany(selector, {
|
|
47
|
-
$set: {
|
|
48
|
-
userId: toUserId,
|
|
49
|
-
updated: new Date(),
|
|
50
|
-
},
|
|
51
|
-
});
|
|
52
|
-
return result.upsertedCount;
|
|
53
|
-
},
|
|
54
|
-
deleteByUserId: async (userId: string) => {
|
|
55
|
-
const bookmarks = await Bookmarks.find({ userId }, { projection: { _id: true } }).toArray();
|
|
56
|
-
const result = await Bookmarks.deleteMany({ userId });
|
|
57
|
-
await Promise.all(bookmarks.map(async (b) => emit('BOOKMARK_REMOVE', { bookmarkId: b._id })));
|
|
58
|
-
return result.deletedCount;
|
|
59
|
-
},
|
|
60
|
-
deleteByProductId: async (productId: string) => {
|
|
61
|
-
const bookmarks = await Bookmarks.find({ productId }, { projection: { _id: true } }).toArray();
|
|
62
|
-
const result = await Bookmarks.deleteMany({ productId });
|
|
63
|
-
await Promise.all(bookmarks.map(async (b) => emit('BOOKMARK_REMOVE', { bookmarkId: b._id })));
|
|
64
|
-
return result.deletedCount;
|
|
65
|
-
},
|
|
66
|
-
create: async (doc) => {
|
|
67
|
-
const { insertedId: bookmarkId } = await Bookmarks.insertOne({
|
|
68
|
-
_id: generateDbObjectId(),
|
|
69
|
-
created: new Date(),
|
|
70
|
-
...doc,
|
|
71
|
-
});
|
|
72
|
-
await emit('BOOKMARK_CREATE', { bookmarkId });
|
|
73
|
-
return bookmarkId;
|
|
74
|
-
},
|
|
75
|
-
update: async (bookmarkId: string, doc: Partial<Bookmark>) => {
|
|
76
|
-
await Bookmarks.updateOne(
|
|
77
|
-
{ _id: bookmarkId },
|
|
78
|
-
{
|
|
79
|
-
$set: {
|
|
80
|
-
updated: new Date(),
|
|
81
|
-
...doc,
|
|
82
|
-
},
|
|
83
|
-
},
|
|
84
|
-
);
|
|
85
|
-
await emit('BOOKMARK_UPDATE', { bookmarkId });
|
|
86
|
-
return bookmarkId;
|
|
87
|
-
},
|
|
88
|
-
delete: async (bookmarkId: string) => {
|
|
89
|
-
const { deletedCount } = await Bookmarks.deleteOne({ _id: bookmarkId });
|
|
90
|
-
await emit('BOOKMARK_REMOVE', { bookmarkId });
|
|
91
|
-
return deletedCount;
|
|
92
|
-
},
|
|
93
|
-
};
|
|
94
|
-
};
|
|
95
|
-
|
|
96
|
-
export type BookmarksModule = Awaited<ReturnType<typeof configureBookmarksModule>>;
|