@storecraft/database-mongodb 1.2.5 → 1.2.6
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/package.json +2 -2
- package/src/con.shared.js +8 -4
- package/src/con.templates.js +53 -2
- package/tests/runner.test.js +6 -8
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@storecraft/database-mongodb",
|
3
|
-
"version": "1.2.
|
3
|
+
"version": "1.2.6",
|
4
4
|
"description": "Storecraft database driver for mongodb on node / bun / deno platform",
|
5
5
|
"license": "MIT",
|
6
6
|
"author": "Tomer Shalev (https://github.com/store-craft)",
|
@@ -36,7 +36,7 @@
|
|
36
36
|
"database-mongodb:docker-compose-up": "docker compose -f ./tests/docker-compose.yml up -d ",
|
37
37
|
"database-mongodb:test": "node ./tests/runner.test.js",
|
38
38
|
"test": "npm run database-mongodb:test",
|
39
|
-
"sc-publish": "npm publish"
|
39
|
+
"sc-publish": "npm version patch --force --no-git-tag-version && npm publish"
|
40
40
|
},
|
41
41
|
"dependencies": {
|
42
42
|
"@storecraft/core": "^1.0.0",
|
package/src/con.shared.js
CHANGED
@@ -23,9 +23,10 @@ import { add_search_terms_relation_on } from './utils.relations.js'
|
|
23
23
|
* @template {Partial<withConcreteIdAndHandle<{}>>} G
|
24
24
|
* @param {MongoDB} driver
|
25
25
|
* @param {Collection<G>} col
|
26
|
+
* @param {(input: T) => T} [hook_pre_write=x=>x]
|
26
27
|
* @returns {db_crud<T, G>["upsert"]}
|
27
28
|
*/
|
28
|
-
export const upsert_regular = (driver, col) => {
|
29
|
+
export const upsert_regular = (driver, col, hook_pre_write=x=>x) => {
|
29
30
|
|
30
31
|
return async (data, search_terms=[]) => {
|
31
32
|
|
@@ -64,7 +65,7 @@ export const upsert_regular = (driver, col) => {
|
|
64
65
|
await col.insertOne(
|
65
66
|
// @ts-ignore
|
66
67
|
{
|
67
|
-
...data,
|
68
|
+
...hook_pre_write(data),
|
68
69
|
_id: data.id ? to_objid(data.id) : undefined,
|
69
70
|
},
|
70
71
|
{
|
@@ -180,9 +181,10 @@ export const expand_to_mongo_projection = (expand) => {
|
|
180
181
|
* @template T, G
|
181
182
|
* @param {MongoDB} driver
|
182
183
|
* @param {Collection<G>} col
|
184
|
+
* @param {(input: G) => G} [hook_post=x=>x]
|
183
185
|
* @returns {db_crud<T, G>["get"]}
|
184
186
|
*/
|
185
|
-
export const get_regular = (driver, col) => {
|
187
|
+
export const get_regular = (driver, col, hook_post=x=>x) => {
|
186
188
|
return async (id_or_handle, options) => {
|
187
189
|
const filter = handle_or_id(id_or_handle);
|
188
190
|
|
@@ -203,7 +205,9 @@ export const get_regular = (driver, col) => {
|
|
203
205
|
);
|
204
206
|
|
205
207
|
return /** @type {G} */(
|
206
|
-
|
208
|
+
hook_post(
|
209
|
+
/** @type {G} */(sanitize_one(res))
|
210
|
+
)
|
207
211
|
);
|
208
212
|
}
|
209
213
|
}
|
package/src/con.templates.js
CHANGED
@@ -7,6 +7,31 @@ import {
|
|
7
7
|
count_regular, get_regular, list_regular,
|
8
8
|
remove_regular, upsert_regular
|
9
9
|
} from './con.shared.js'
|
10
|
+
import { base64 } from '@storecraft/core/crypto';
|
11
|
+
|
12
|
+
/**
|
13
|
+
* @description if `base64_` prefixed then decode the postfix and return it,
|
14
|
+
* otherwise, just return the original value.
|
15
|
+
* @param {string} val
|
16
|
+
*/
|
17
|
+
const encode_base64_if_needed = val => {
|
18
|
+
if(val?.startsWith('base64_'))
|
19
|
+
return val;
|
20
|
+
|
21
|
+
return 'base64_' + base64.encode(val);
|
22
|
+
}
|
23
|
+
|
24
|
+
/**
|
25
|
+
* @description if `base64_` prefixed then decode the postfix and return it,
|
26
|
+
* otherwise, just return the original value.
|
27
|
+
* @param {string} val
|
28
|
+
*/
|
29
|
+
const decode_base64_if_needed = val => {
|
30
|
+
if(!val?.startsWith('base64_'))
|
31
|
+
return val;
|
32
|
+
|
33
|
+
return base64.decode(val.split('base64_').at(-1) ?? '');
|
34
|
+
}
|
10
35
|
|
11
36
|
/**
|
12
37
|
* @param {MongoDB} d
|
@@ -18,12 +43,38 @@ const col = (d) => d.collection('templates');
|
|
18
43
|
* @param {MongoDB} driver
|
19
44
|
* @returns {db_col["upsert"]}
|
20
45
|
*/
|
21
|
-
const upsert = (driver) => upsert_regular(
|
46
|
+
const upsert = (driver) => upsert_regular(
|
47
|
+
driver, col(driver),
|
48
|
+
(before) => {
|
49
|
+
return before && {
|
50
|
+
...before,
|
51
|
+
template_html: before?.template_html &&
|
52
|
+
encode_base64_if_needed(before?.template_html),
|
53
|
+
template_text: before?.template_text &&
|
54
|
+
encode_base64_if_needed(before?.template_text),
|
55
|
+
template_subject: before?.template_subject &&
|
56
|
+
encode_base64_if_needed(before?.template_subject),
|
57
|
+
}
|
58
|
+
},
|
59
|
+
);
|
22
60
|
|
23
61
|
/**
|
24
62
|
* @param {MongoDB} driver
|
25
63
|
*/
|
26
|
-
const get = (driver) => get_regular(
|
64
|
+
const get = (driver) => get_regular(
|
65
|
+
driver, col(driver),
|
66
|
+
(item) => {
|
67
|
+
return item && {
|
68
|
+
...item,
|
69
|
+
template_html: item?.template_html &&
|
70
|
+
decode_base64_if_needed(item?.template_html),
|
71
|
+
template_text: item?.template_text &&
|
72
|
+
decode_base64_if_needed(item?.template_text),
|
73
|
+
template_subject: item?.template_subject &&
|
74
|
+
decode_base64_if_needed(item?.template_subject),
|
75
|
+
}
|
76
|
+
}
|
77
|
+
);
|
27
78
|
|
28
79
|
/**
|
29
80
|
* @param {MongoDB} driver
|
package/tests/runner.test.js
CHANGED
@@ -9,7 +9,7 @@ import { api } from '@storecraft/core/test-runner';
|
|
9
9
|
//
|
10
10
|
|
11
11
|
export const create_app = () => {
|
12
|
-
|
12
|
+
return new App(
|
13
13
|
{
|
14
14
|
auth_admins_emails: ['admin@sc.com'],
|
15
15
|
auth_secret_access_token: 'auth_secret_access_token',
|
@@ -26,24 +26,22 @@ export const create_app = () => {
|
|
26
26
|
url: process.env.MONGODB_URL,
|
27
27
|
}
|
28
28
|
)
|
29
|
-
)
|
30
|
-
|
31
|
-
return app.init().__show_me_everything.app;
|
29
|
+
).init();
|
32
30
|
}
|
33
31
|
|
34
32
|
async function test() {
|
35
33
|
const app = create_app();
|
36
34
|
|
37
|
-
await migrateToLatest(app.
|
35
|
+
await migrateToLatest(app._.db, false);
|
38
36
|
|
39
37
|
Object.entries(api).slice(0, -1).forEach(
|
40
38
|
([name, runner]) => {
|
41
|
-
runner.create(app).run();
|
39
|
+
runner.create(app._.app).run();
|
42
40
|
}
|
43
41
|
);
|
44
42
|
|
45
|
-
const last_test = Object.values(api).at(-1).create(app);
|
46
|
-
last_test.after(async ()=>{app.
|
43
|
+
const last_test = Object.values(api).at(-1).create(app._.app);
|
44
|
+
last_test.after(async ()=>{app._.db.disconnect()});
|
47
45
|
last_test.run();
|
48
46
|
}
|
49
47
|
|