@hot-updater/plugin-core 0.21.11 → 0.21.13
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/dist/createBlobDatabasePlugin.cjs +245 -245
- package/dist/createBlobDatabasePlugin.d.cts +16 -27
- package/dist/createBlobDatabasePlugin.d.ts +16 -27
- package/dist/createBlobDatabasePlugin.js +245 -245
- package/dist/createDatabasePlugin.cjs +76 -80
- package/dist/createDatabasePlugin.d.cts +48 -37
- package/dist/createDatabasePlugin.d.ts +48 -37
- package/dist/createDatabasePlugin.js +77 -81
- package/dist/createStoragePlugin.cjs +57 -0
- package/dist/createStoragePlugin.d.cts +63 -0
- package/dist/createStoragePlugin.d.ts +63 -0
- package/dist/createStoragePlugin.js +56 -0
- package/dist/index.cjs +2 -0
- package/dist/index.d.cts +4 -3
- package/dist/index.d.ts +4 -3
- package/dist/index.js +2 -1
- package/package.json +3 -3
|
@@ -4,93 +4,89 @@ es_toolkit = require_rolldown_runtime.__toESM(es_toolkit);
|
|
|
4
4
|
|
|
5
5
|
//#region src/createDatabasePlugin.ts
|
|
6
6
|
/**
|
|
7
|
-
* Creates a database plugin with
|
|
7
|
+
* Creates a database plugin with lazy initialization and automatic hook execution.
|
|
8
|
+
*
|
|
9
|
+
* This factory function abstracts the double currying pattern used by all database plugins,
|
|
10
|
+
* ensuring consistent lazy initialization behavior across different database providers.
|
|
11
|
+
* Hooks are automatically executed at appropriate times without requiring manual invocation.
|
|
12
|
+
*
|
|
13
|
+
* @param options - Configuration options for the database plugin
|
|
14
|
+
* @returns A double-curried function that lazily initializes the database plugin
|
|
8
15
|
*
|
|
9
16
|
* @example
|
|
10
|
-
* ```
|
|
11
|
-
* const
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
* // Implementation to get bundles with options using context.dbClient
|
|
22
|
-
* return bundles;
|
|
23
|
-
* },
|
|
24
|
-
* async getChannels(context) {
|
|
25
|
-
* // Implementation to get available channels using context.dbClient
|
|
26
|
-
* return channels;
|
|
27
|
-
* },
|
|
28
|
-
* async commitBundle(context, { changedSets }) {
|
|
29
|
-
* // Implementation to commit changed bundles using context.dbClient
|
|
17
|
+
* ```typescript
|
|
18
|
+
* export const postgres = createDatabasePlugin<PostgresConfig>({
|
|
19
|
+
* name: "postgres",
|
|
20
|
+
* factory: (config) => {
|
|
21
|
+
* const db = new Kysely(config);
|
|
22
|
+
* return {
|
|
23
|
+
* async getBundleById(bundleId) { ... },
|
|
24
|
+
* async getBundles(options) { ... },
|
|
25
|
+
* async getChannels() { ... },
|
|
26
|
+
* async commitBundle({ changedSets }) { ... }
|
|
27
|
+
* };
|
|
30
28
|
* }
|
|
31
29
|
* });
|
|
32
30
|
* ```
|
|
33
|
-
*
|
|
34
|
-
* @param name - The name of the database plugin
|
|
35
|
-
* @param abstractPlugin - A plugin implementation with context support
|
|
36
|
-
* @param hooks - Optional hooks for plugin lifecycle events
|
|
37
|
-
* @returns A function that creates a database plugin instance
|
|
38
31
|
*/
|
|
39
|
-
function createDatabasePlugin(
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
return abstractPlugin.getBundleById(context, bundleId);
|
|
53
|
-
},
|
|
54
|
-
async getBundles(options) {
|
|
55
|
-
const context = memoizedContext();
|
|
56
|
-
return abstractPlugin.getBundles(context, options);
|
|
57
|
-
},
|
|
58
|
-
async getChannels() {
|
|
59
|
-
const context = memoizedContext();
|
|
60
|
-
return abstractPlugin.getChannels(context);
|
|
61
|
-
},
|
|
62
|
-
async commitBundle() {
|
|
63
|
-
if (!abstractPlugin.commitBundle) throw new Error("commitBundle is not implemented");
|
|
64
|
-
const context = memoizedContext();
|
|
65
|
-
await abstractPlugin.commitBundle(context, { changedSets: Array.from(changedMap.values()) });
|
|
66
|
-
changedMap.clear();
|
|
67
|
-
hooks?.onDatabaseUpdated?.();
|
|
68
|
-
},
|
|
69
|
-
async updateBundle(targetBundleId, newBundle) {
|
|
70
|
-
const pendingChange = changedMap.get(targetBundleId);
|
|
71
|
-
if (pendingChange) {
|
|
72
|
-
const updatedData = (0, es_toolkit.merge)(pendingChange.data, newBundle);
|
|
73
|
-
changedMap.set(targetBundleId, {
|
|
74
|
-
operation: pendingChange.operation,
|
|
75
|
-
data: updatedData
|
|
32
|
+
function createDatabasePlugin(options) {
|
|
33
|
+
return (config, hooks) => {
|
|
34
|
+
return () => {
|
|
35
|
+
let cachedMethods = null;
|
|
36
|
+
const getMethods = () => {
|
|
37
|
+
if (!cachedMethods) cachedMethods = options.factory(config);
|
|
38
|
+
return cachedMethods;
|
|
39
|
+
};
|
|
40
|
+
const changedMap = /* @__PURE__ */ new Map();
|
|
41
|
+
const markChanged = (operation, data) => {
|
|
42
|
+
changedMap.set(data.id, {
|
|
43
|
+
operation,
|
|
44
|
+
data
|
|
76
45
|
});
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
46
|
+
};
|
|
47
|
+
return {
|
|
48
|
+
name: options.name,
|
|
49
|
+
async getBundleById(bundleId) {
|
|
50
|
+
return getMethods().getBundleById(bundleId);
|
|
51
|
+
},
|
|
52
|
+
async getBundles(options$1) {
|
|
53
|
+
return getMethods().getBundles(options$1);
|
|
54
|
+
},
|
|
55
|
+
async getChannels() {
|
|
56
|
+
return getMethods().getChannels();
|
|
57
|
+
},
|
|
58
|
+
async onUnmount() {
|
|
59
|
+
const methods = getMethods();
|
|
60
|
+
if (methods.onUnmount) return methods.onUnmount();
|
|
61
|
+
},
|
|
62
|
+
async commitBundle() {
|
|
63
|
+
await getMethods().commitBundle({ changedSets: Array.from(changedMap.values()) });
|
|
64
|
+
await hooks?.onDatabaseUpdated?.();
|
|
65
|
+
changedMap.clear();
|
|
66
|
+
},
|
|
67
|
+
async updateBundle(targetBundleId, newBundle) {
|
|
68
|
+
const pendingChange = changedMap.get(targetBundleId);
|
|
69
|
+
if (pendingChange) {
|
|
70
|
+
const updatedData = (0, es_toolkit.merge)(pendingChange.data, newBundle);
|
|
71
|
+
changedMap.set(targetBundleId, {
|
|
72
|
+
operation: pendingChange.operation,
|
|
73
|
+
data: updatedData
|
|
74
|
+
});
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
const currentBundle = await getMethods().getBundleById(targetBundleId);
|
|
78
|
+
if (!currentBundle) throw new Error("targetBundleId not found");
|
|
79
|
+
markChanged("update", (0, es_toolkit.merge)(currentBundle, newBundle));
|
|
80
|
+
},
|
|
81
|
+
async appendBundle(inputBundle) {
|
|
82
|
+
markChanged("insert", inputBundle);
|
|
83
|
+
},
|
|
84
|
+
async deleteBundle(deleteBundle) {
|
|
85
|
+
markChanged("delete", deleteBundle);
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
};
|
|
89
|
+
};
|
|
94
90
|
}
|
|
95
91
|
|
|
96
92
|
//#endregion
|
|
@@ -2,10 +2,9 @@ import { DatabasePlugin, DatabasePluginHooks, PaginationInfo } from "./types/ind
|
|
|
2
2
|
import { Bundle } from "@hot-updater/core";
|
|
3
3
|
|
|
4
4
|
//#region src/createDatabasePlugin.d.ts
|
|
5
|
-
interface AbstractDatabasePlugin
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
getBundles: (context: TContext, options: {
|
|
5
|
+
interface AbstractDatabasePlugin {
|
|
6
|
+
getBundleById: (bundleId: string) => Promise<Bundle | null>;
|
|
7
|
+
getBundles: (options: {
|
|
9
8
|
where?: {
|
|
10
9
|
channel?: string;
|
|
11
10
|
platform?: string;
|
|
@@ -16,11 +15,9 @@ interface AbstractDatabasePlugin<TContext = object> {
|
|
|
16
15
|
data: Bundle[];
|
|
17
16
|
pagination: PaginationInfo;
|
|
18
17
|
}>;
|
|
19
|
-
getChannels: (
|
|
20
|
-
onUnmount?: (
|
|
21
|
-
commitBundle: (
|
|
22
|
-
changedSets
|
|
23
|
-
}: {
|
|
18
|
+
getChannels: () => Promise<string[]>;
|
|
19
|
+
onUnmount?: () => Promise<void>;
|
|
20
|
+
commitBundle: (params: {
|
|
24
21
|
changedSets: {
|
|
25
22
|
operation: "insert" | "update" | "delete";
|
|
26
23
|
data: Bundle;
|
|
@@ -28,38 +25,52 @@ interface AbstractDatabasePlugin<TContext = object> {
|
|
|
28
25
|
}) => Promise<void>;
|
|
29
26
|
}
|
|
30
27
|
/**
|
|
31
|
-
*
|
|
28
|
+
* Database plugin methods without name
|
|
29
|
+
*/
|
|
30
|
+
type DatabasePluginMethods = Omit<AbstractDatabasePlugin, never>;
|
|
31
|
+
/**
|
|
32
|
+
* Factory function that creates database plugin methods
|
|
33
|
+
*/
|
|
34
|
+
type DatabasePluginFactory<TConfig> = (config: TConfig) => DatabasePluginMethods;
|
|
35
|
+
/**
|
|
36
|
+
* Configuration options for creating a database plugin
|
|
37
|
+
*/
|
|
38
|
+
interface CreateDatabasePluginOptions<TConfig> {
|
|
39
|
+
/**
|
|
40
|
+
* The name of the database plugin (e.g., "postgres", "d1Database")
|
|
41
|
+
*/
|
|
42
|
+
name: string;
|
|
43
|
+
/**
|
|
44
|
+
* Function that creates the database plugin methods
|
|
45
|
+
*/
|
|
46
|
+
factory: DatabasePluginFactory<TConfig>;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Creates a database plugin with lazy initialization and automatic hook execution.
|
|
50
|
+
*
|
|
51
|
+
* This factory function abstracts the double currying pattern used by all database plugins,
|
|
52
|
+
* ensuring consistent lazy initialization behavior across different database providers.
|
|
53
|
+
* Hooks are automatically executed at appropriate times without requiring manual invocation.
|
|
54
|
+
*
|
|
55
|
+
* @param options - Configuration options for the database plugin
|
|
56
|
+
* @returns A double-curried function that lazily initializes the database plugin
|
|
32
57
|
*
|
|
33
58
|
* @example
|
|
34
|
-
* ```
|
|
35
|
-
* const
|
|
36
|
-
*
|
|
37
|
-
*
|
|
38
|
-
*
|
|
39
|
-
*
|
|
40
|
-
*
|
|
41
|
-
*
|
|
42
|
-
*
|
|
43
|
-
*
|
|
44
|
-
*
|
|
45
|
-
* // Implementation to get bundles with options using context.dbClient
|
|
46
|
-
* return bundles;
|
|
47
|
-
* },
|
|
48
|
-
* async getChannels(context) {
|
|
49
|
-
* // Implementation to get available channels using context.dbClient
|
|
50
|
-
* return channels;
|
|
51
|
-
* },
|
|
52
|
-
* async commitBundle(context, { changedSets }) {
|
|
53
|
-
* // Implementation to commit changed bundles using context.dbClient
|
|
59
|
+
* ```typescript
|
|
60
|
+
* export const postgres = createDatabasePlugin<PostgresConfig>({
|
|
61
|
+
* name: "postgres",
|
|
62
|
+
* factory: (config) => {
|
|
63
|
+
* const db = new Kysely(config);
|
|
64
|
+
* return {
|
|
65
|
+
* async getBundleById(bundleId) { ... },
|
|
66
|
+
* async getBundles(options) { ... },
|
|
67
|
+
* async getChannels() { ... },
|
|
68
|
+
* async commitBundle({ changedSets }) { ... }
|
|
69
|
+
* };
|
|
54
70
|
* }
|
|
55
71
|
* });
|
|
56
72
|
* ```
|
|
57
|
-
*
|
|
58
|
-
* @param name - The name of the database plugin
|
|
59
|
-
* @param abstractPlugin - A plugin implementation with context support
|
|
60
|
-
* @param hooks - Optional hooks for plugin lifecycle events
|
|
61
|
-
* @returns A function that creates a database plugin instance
|
|
62
73
|
*/
|
|
63
|
-
declare function createDatabasePlugin<
|
|
74
|
+
declare function createDatabasePlugin<TConfig>(options: CreateDatabasePluginOptions<TConfig>): (config: TConfig, hooks?: DatabasePluginHooks) => (() => DatabasePlugin);
|
|
64
75
|
//#endregion
|
|
65
|
-
export { AbstractDatabasePlugin, createDatabasePlugin };
|
|
76
|
+
export { AbstractDatabasePlugin, CreateDatabasePluginOptions, createDatabasePlugin };
|
|
@@ -2,10 +2,9 @@ import { DatabasePlugin, DatabasePluginHooks, PaginationInfo } from "./types/ind
|
|
|
2
2
|
import { Bundle } from "@hot-updater/core";
|
|
3
3
|
|
|
4
4
|
//#region src/createDatabasePlugin.d.ts
|
|
5
|
-
interface AbstractDatabasePlugin
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
getBundles: (context: TContext, options: {
|
|
5
|
+
interface AbstractDatabasePlugin {
|
|
6
|
+
getBundleById: (bundleId: string) => Promise<Bundle | null>;
|
|
7
|
+
getBundles: (options: {
|
|
9
8
|
where?: {
|
|
10
9
|
channel?: string;
|
|
11
10
|
platform?: string;
|
|
@@ -16,11 +15,9 @@ interface AbstractDatabasePlugin<TContext = object> {
|
|
|
16
15
|
data: Bundle[];
|
|
17
16
|
pagination: PaginationInfo;
|
|
18
17
|
}>;
|
|
19
|
-
getChannels: (
|
|
20
|
-
onUnmount?: (
|
|
21
|
-
commitBundle: (
|
|
22
|
-
changedSets
|
|
23
|
-
}: {
|
|
18
|
+
getChannels: () => Promise<string[]>;
|
|
19
|
+
onUnmount?: () => Promise<void>;
|
|
20
|
+
commitBundle: (params: {
|
|
24
21
|
changedSets: {
|
|
25
22
|
operation: "insert" | "update" | "delete";
|
|
26
23
|
data: Bundle;
|
|
@@ -28,38 +25,52 @@ interface AbstractDatabasePlugin<TContext = object> {
|
|
|
28
25
|
}) => Promise<void>;
|
|
29
26
|
}
|
|
30
27
|
/**
|
|
31
|
-
*
|
|
28
|
+
* Database plugin methods without name
|
|
29
|
+
*/
|
|
30
|
+
type DatabasePluginMethods = Omit<AbstractDatabasePlugin, never>;
|
|
31
|
+
/**
|
|
32
|
+
* Factory function that creates database plugin methods
|
|
33
|
+
*/
|
|
34
|
+
type DatabasePluginFactory<TConfig> = (config: TConfig) => DatabasePluginMethods;
|
|
35
|
+
/**
|
|
36
|
+
* Configuration options for creating a database plugin
|
|
37
|
+
*/
|
|
38
|
+
interface CreateDatabasePluginOptions<TConfig> {
|
|
39
|
+
/**
|
|
40
|
+
* The name of the database plugin (e.g., "postgres", "d1Database")
|
|
41
|
+
*/
|
|
42
|
+
name: string;
|
|
43
|
+
/**
|
|
44
|
+
* Function that creates the database plugin methods
|
|
45
|
+
*/
|
|
46
|
+
factory: DatabasePluginFactory<TConfig>;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Creates a database plugin with lazy initialization and automatic hook execution.
|
|
50
|
+
*
|
|
51
|
+
* This factory function abstracts the double currying pattern used by all database plugins,
|
|
52
|
+
* ensuring consistent lazy initialization behavior across different database providers.
|
|
53
|
+
* Hooks are automatically executed at appropriate times without requiring manual invocation.
|
|
54
|
+
*
|
|
55
|
+
* @param options - Configuration options for the database plugin
|
|
56
|
+
* @returns A double-curried function that lazily initializes the database plugin
|
|
32
57
|
*
|
|
33
58
|
* @example
|
|
34
|
-
* ```
|
|
35
|
-
* const
|
|
36
|
-
*
|
|
37
|
-
*
|
|
38
|
-
*
|
|
39
|
-
*
|
|
40
|
-
*
|
|
41
|
-
*
|
|
42
|
-
*
|
|
43
|
-
*
|
|
44
|
-
*
|
|
45
|
-
* // Implementation to get bundles with options using context.dbClient
|
|
46
|
-
* return bundles;
|
|
47
|
-
* },
|
|
48
|
-
* async getChannels(context) {
|
|
49
|
-
* // Implementation to get available channels using context.dbClient
|
|
50
|
-
* return channels;
|
|
51
|
-
* },
|
|
52
|
-
* async commitBundle(context, { changedSets }) {
|
|
53
|
-
* // Implementation to commit changed bundles using context.dbClient
|
|
59
|
+
* ```typescript
|
|
60
|
+
* export const postgres = createDatabasePlugin<PostgresConfig>({
|
|
61
|
+
* name: "postgres",
|
|
62
|
+
* factory: (config) => {
|
|
63
|
+
* const db = new Kysely(config);
|
|
64
|
+
* return {
|
|
65
|
+
* async getBundleById(bundleId) { ... },
|
|
66
|
+
* async getBundles(options) { ... },
|
|
67
|
+
* async getChannels() { ... },
|
|
68
|
+
* async commitBundle({ changedSets }) { ... }
|
|
69
|
+
* };
|
|
54
70
|
* }
|
|
55
71
|
* });
|
|
56
72
|
* ```
|
|
57
|
-
*
|
|
58
|
-
* @param name - The name of the database plugin
|
|
59
|
-
* @param abstractPlugin - A plugin implementation with context support
|
|
60
|
-
* @param hooks - Optional hooks for plugin lifecycle events
|
|
61
|
-
* @returns A function that creates a database plugin instance
|
|
62
73
|
*/
|
|
63
|
-
declare function createDatabasePlugin<
|
|
74
|
+
declare function createDatabasePlugin<TConfig>(options: CreateDatabasePluginOptions<TConfig>): (config: TConfig, hooks?: DatabasePluginHooks) => (() => DatabasePlugin);
|
|
64
75
|
//#endregion
|
|
65
|
-
export { AbstractDatabasePlugin, createDatabasePlugin };
|
|
76
|
+
export { AbstractDatabasePlugin, CreateDatabasePluginOptions, createDatabasePlugin };
|
|
@@ -1,94 +1,90 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { merge } from "es-toolkit";
|
|
2
2
|
|
|
3
3
|
//#region src/createDatabasePlugin.ts
|
|
4
4
|
/**
|
|
5
|
-
* Creates a database plugin with
|
|
5
|
+
* Creates a database plugin with lazy initialization and automatic hook execution.
|
|
6
|
+
*
|
|
7
|
+
* This factory function abstracts the double currying pattern used by all database plugins,
|
|
8
|
+
* ensuring consistent lazy initialization behavior across different database providers.
|
|
9
|
+
* Hooks are automatically executed at appropriate times without requiring manual invocation.
|
|
10
|
+
*
|
|
11
|
+
* @param options - Configuration options for the database plugin
|
|
12
|
+
* @returns A double-curried function that lazily initializes the database plugin
|
|
6
13
|
*
|
|
7
14
|
* @example
|
|
8
|
-
* ```
|
|
9
|
-
* const
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
* // Implementation to get bundles with options using context.dbClient
|
|
20
|
-
* return bundles;
|
|
21
|
-
* },
|
|
22
|
-
* async getChannels(context) {
|
|
23
|
-
* // Implementation to get available channels using context.dbClient
|
|
24
|
-
* return channels;
|
|
25
|
-
* },
|
|
26
|
-
* async commitBundle(context, { changedSets }) {
|
|
27
|
-
* // Implementation to commit changed bundles using context.dbClient
|
|
15
|
+
* ```typescript
|
|
16
|
+
* export const postgres = createDatabasePlugin<PostgresConfig>({
|
|
17
|
+
* name: "postgres",
|
|
18
|
+
* factory: (config) => {
|
|
19
|
+
* const db = new Kysely(config);
|
|
20
|
+
* return {
|
|
21
|
+
* async getBundleById(bundleId) { ... },
|
|
22
|
+
* async getBundles(options) { ... },
|
|
23
|
+
* async getChannels() { ... },
|
|
24
|
+
* async commitBundle({ changedSets }) { ... }
|
|
25
|
+
* };
|
|
28
26
|
* }
|
|
29
27
|
* });
|
|
30
28
|
* ```
|
|
31
|
-
*
|
|
32
|
-
* @param name - The name of the database plugin
|
|
33
|
-
* @param abstractPlugin - A plugin implementation with context support
|
|
34
|
-
* @param hooks - Optional hooks for plugin lifecycle events
|
|
35
|
-
* @returns A function that creates a database plugin instance
|
|
36
29
|
*/
|
|
37
|
-
function createDatabasePlugin(
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
return abstractPlugin.getBundleById(context, bundleId);
|
|
51
|
-
},
|
|
52
|
-
async getBundles(options) {
|
|
53
|
-
const context = memoizedContext();
|
|
54
|
-
return abstractPlugin.getBundles(context, options);
|
|
55
|
-
},
|
|
56
|
-
async getChannels() {
|
|
57
|
-
const context = memoizedContext();
|
|
58
|
-
return abstractPlugin.getChannels(context);
|
|
59
|
-
},
|
|
60
|
-
async commitBundle() {
|
|
61
|
-
if (!abstractPlugin.commitBundle) throw new Error("commitBundle is not implemented");
|
|
62
|
-
const context = memoizedContext();
|
|
63
|
-
await abstractPlugin.commitBundle(context, { changedSets: Array.from(changedMap.values()) });
|
|
64
|
-
changedMap.clear();
|
|
65
|
-
hooks?.onDatabaseUpdated?.();
|
|
66
|
-
},
|
|
67
|
-
async updateBundle(targetBundleId, newBundle) {
|
|
68
|
-
const pendingChange = changedMap.get(targetBundleId);
|
|
69
|
-
if (pendingChange) {
|
|
70
|
-
const updatedData = merge(pendingChange.data, newBundle);
|
|
71
|
-
changedMap.set(targetBundleId, {
|
|
72
|
-
operation: pendingChange.operation,
|
|
73
|
-
data: updatedData
|
|
30
|
+
function createDatabasePlugin(options) {
|
|
31
|
+
return (config, hooks) => {
|
|
32
|
+
return () => {
|
|
33
|
+
let cachedMethods = null;
|
|
34
|
+
const getMethods = () => {
|
|
35
|
+
if (!cachedMethods) cachedMethods = options.factory(config);
|
|
36
|
+
return cachedMethods;
|
|
37
|
+
};
|
|
38
|
+
const changedMap = /* @__PURE__ */ new Map();
|
|
39
|
+
const markChanged = (operation, data) => {
|
|
40
|
+
changedMap.set(data.id, {
|
|
41
|
+
operation,
|
|
42
|
+
data
|
|
74
43
|
});
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
44
|
+
};
|
|
45
|
+
return {
|
|
46
|
+
name: options.name,
|
|
47
|
+
async getBundleById(bundleId) {
|
|
48
|
+
return getMethods().getBundleById(bundleId);
|
|
49
|
+
},
|
|
50
|
+
async getBundles(options$1) {
|
|
51
|
+
return getMethods().getBundles(options$1);
|
|
52
|
+
},
|
|
53
|
+
async getChannels() {
|
|
54
|
+
return getMethods().getChannels();
|
|
55
|
+
},
|
|
56
|
+
async onUnmount() {
|
|
57
|
+
const methods = getMethods();
|
|
58
|
+
if (methods.onUnmount) return methods.onUnmount();
|
|
59
|
+
},
|
|
60
|
+
async commitBundle() {
|
|
61
|
+
await getMethods().commitBundle({ changedSets: Array.from(changedMap.values()) });
|
|
62
|
+
await hooks?.onDatabaseUpdated?.();
|
|
63
|
+
changedMap.clear();
|
|
64
|
+
},
|
|
65
|
+
async updateBundle(targetBundleId, newBundle) {
|
|
66
|
+
const pendingChange = changedMap.get(targetBundleId);
|
|
67
|
+
if (pendingChange) {
|
|
68
|
+
const updatedData = merge(pendingChange.data, newBundle);
|
|
69
|
+
changedMap.set(targetBundleId, {
|
|
70
|
+
operation: pendingChange.operation,
|
|
71
|
+
data: updatedData
|
|
72
|
+
});
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
const currentBundle = await getMethods().getBundleById(targetBundleId);
|
|
76
|
+
if (!currentBundle) throw new Error("targetBundleId not found");
|
|
77
|
+
markChanged("update", merge(currentBundle, newBundle));
|
|
78
|
+
},
|
|
79
|
+
async appendBundle(inputBundle) {
|
|
80
|
+
markChanged("insert", inputBundle);
|
|
81
|
+
},
|
|
82
|
+
async deleteBundle(deleteBundle) {
|
|
83
|
+
markChanged("delete", deleteBundle);
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
};
|
|
87
|
+
};
|
|
92
88
|
}
|
|
93
89
|
|
|
94
90
|
//#endregion
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
|
|
2
|
+
//#region src/createStoragePlugin.ts
|
|
3
|
+
/**
|
|
4
|
+
* Creates a storage plugin with lazy initialization and automatic hook execution.
|
|
5
|
+
*
|
|
6
|
+
* This factory function abstracts the double currying pattern used by all storage plugins,
|
|
7
|
+
* ensuring consistent lazy initialization behavior across different storage providers.
|
|
8
|
+
* Hooks are automatically executed at appropriate times without requiring manual invocation.
|
|
9
|
+
*
|
|
10
|
+
* @param options - Configuration options for the storage plugin
|
|
11
|
+
* @returns A double-curried function that lazily initializes the storage plugin
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```typescript
|
|
15
|
+
* export const s3Storage = createStoragePlugin<S3StorageConfig>({
|
|
16
|
+
* name: "s3Storage",
|
|
17
|
+
* supportedProtocol: "s3",
|
|
18
|
+
* factory: (config) => {
|
|
19
|
+
* const client = new S3Client(config);
|
|
20
|
+
* return {
|
|
21
|
+
* async upload(key, filePath) { ... },
|
|
22
|
+
* async delete(storageUri) { ... },
|
|
23
|
+
* async getDownloadUrl(storageUri) { ... }
|
|
24
|
+
* };
|
|
25
|
+
* }
|
|
26
|
+
* });
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
const createStoragePlugin = (options) => {
|
|
30
|
+
return (config, hooks) => {
|
|
31
|
+
return () => {
|
|
32
|
+
let cachedMethods = null;
|
|
33
|
+
const getMethods = () => {
|
|
34
|
+
if (!cachedMethods) cachedMethods = options.factory(config);
|
|
35
|
+
return cachedMethods;
|
|
36
|
+
};
|
|
37
|
+
return {
|
|
38
|
+
name: options.name,
|
|
39
|
+
supportedProtocol: options.supportedProtocol,
|
|
40
|
+
async upload(key, filePath) {
|
|
41
|
+
const result = await getMethods().upload(key, filePath);
|
|
42
|
+
await hooks?.onStorageUploaded?.();
|
|
43
|
+
return result;
|
|
44
|
+
},
|
|
45
|
+
async delete(storageUri) {
|
|
46
|
+
return getMethods().delete(storageUri);
|
|
47
|
+
},
|
|
48
|
+
async getDownloadUrl(storageUri) {
|
|
49
|
+
return getMethods().getDownloadUrl(storageUri);
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
};
|
|
53
|
+
};
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
//#endregion
|
|
57
|
+
exports.createStoragePlugin = createStoragePlugin;
|