dynamo-bao 0.1.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.
@@ -0,0 +1,44 @@
1
+ class ObjectNotFound {
2
+ /**
3
+ * @constructor
4
+ * @param {Object} consumedCapacity - The consumed capacity for the operation.
5
+ */
6
+ constructor(consumedCapacity) {
7
+ this.consumedCapacity = consumedCapacity;
8
+ }
9
+
10
+ /**
11
+ * @memberof ObjectNotFound
12
+ * @description
13
+ * Returns false, since the object was not found.
14
+ * @returns {boolean} False.
15
+ */
16
+ exists() {
17
+ return false;
18
+ }
19
+
20
+ toString() {
21
+ return `ObjectNotFound (consumed ${this.consumedCapacity} capacity units)`;
22
+ }
23
+
24
+ /**
25
+ * @memberof ObjectNotFound
26
+ * @description
27
+ * Returns the numeric consumed capacity for the operation. For compatibility this
28
+ * method supports an includeRelated parameter, but it is ignored, since there are
29
+ * no related objects to consider.
30
+ * @param {string} type - The type of operation.
31
+ * @param {boolean} [includeRelated=false] - Whether to include related capacity.
32
+ * @returns {number} The numeric consumed capacity.
33
+ */
34
+ getNumericConsumedCapacity(type, includeRelated = false) {
35
+ if (this.consumedCapacity && type === "read") {
36
+ return this.consumedCapacity.CapacityUnits;
37
+ }
38
+ return 0;
39
+ }
40
+ }
41
+
42
+ module.exports = {
43
+ ObjectNotFound,
44
+ };
@@ -0,0 +1,61 @@
1
+ const { defaultLogger: logger } = require("./utils/logger");
2
+
3
+ class PluginManager {
4
+ constructor() {
5
+ this.plugins = new Map(); // modelName -> plugins[]
6
+ this.methods = new Map(); // modelName -> Map<methodName, function>
7
+ }
8
+
9
+ registerPlugin(modelName, plugin) {
10
+ if (!this.plugins.has(modelName)) {
11
+ this.plugins.set(modelName, []);
12
+ }
13
+ logger.debug(`Registering plugin for model ${modelName}`, plugin);
14
+ this.plugins.get(modelName).push(plugin);
15
+
16
+ // Register methods if they exist
17
+ if (plugin.methods) {
18
+ this.registerPluginMethods(modelName, plugin.methods);
19
+ }
20
+ }
21
+
22
+ async executeHooks(modelName, hookName, ...args) {
23
+ const plugins = this.plugins.get(modelName) || [];
24
+ for (const plugin of plugins) {
25
+ if (typeof plugin[hookName] === "function") {
26
+ logger.debug(`Executing ${hookName} hook for ${modelName}`);
27
+ await plugin[hookName](...args);
28
+ }
29
+ }
30
+ }
31
+
32
+ applyMethodsToInstance(modelName, instance) {
33
+ const modelMethods = this.methods.get(modelName);
34
+ if (!modelMethods) return;
35
+
36
+ for (const [methodName, methodFn] of modelMethods) {
37
+ // Bind the method to the instance
38
+ instance[methodName] = methodFn.bind(instance);
39
+ }
40
+ }
41
+
42
+ registerPluginMethods(modelName, methods) {
43
+ if (!this.methods.has(modelName)) {
44
+ this.methods.set(modelName, new Map());
45
+ }
46
+
47
+ const modelMethods = this.methods.get(modelName);
48
+ Object.entries(methods).forEach(([methodName, methodFn]) => {
49
+ if (modelMethods.has(methodName)) {
50
+ logger.warn(
51
+ `Method ${methodName} already exists for model ${modelName}. It will be overwritten.`,
52
+ );
53
+ }
54
+ modelMethods.set(methodName, methodFn);
55
+ });
56
+ }
57
+ }
58
+
59
+ // Singleton instance
60
+ const pluginManager = new PluginManager();
61
+ module.exports = { pluginManager };
@@ -0,0 +1,54 @@
1
+ // require("dotenv").config();
2
+
3
+ const userConfig = require("../config");
4
+
5
+ const LOG_LEVELS = {
6
+ OFF: -1,
7
+ ERROR: 0,
8
+ WARN: 1,
9
+ INFO: 2,
10
+ DEBUG: 3,
11
+ };
12
+
13
+ class Logger {
14
+ constructor(context) {
15
+ this.context = context;
16
+ this.currentLevel = userConfig.logging.level.toUpperCase();
17
+ }
18
+
19
+ log(level, ...args) {
20
+ if (
21
+ this.currentLevel !== "OFF" &&
22
+ LOG_LEVELS[level] <= LOG_LEVELS[this.currentLevel]
23
+ ) {
24
+ const prefix = `[${level}] ${this.context}:`;
25
+ console.log(prefix, ...args);
26
+ }
27
+ }
28
+
29
+ debug(...args) {
30
+ this.log("DEBUG", ...args);
31
+ }
32
+ info(...args) {
33
+ this.log("INFO", ...args);
34
+ }
35
+ warn(...args) {
36
+ this.log("WARN", ...args);
37
+ }
38
+ error(...args) {
39
+ this.log("ERROR", ...args);
40
+ }
41
+
42
+ // Create a logger instance with a fixed context
43
+ static getLogger(context) {
44
+ return new Logger(context);
45
+ }
46
+ }
47
+
48
+ // Export a default logger with a generic context
49
+ const defaultLogger = new Logger("Default");
50
+
51
+ module.exports = {
52
+ Logger,
53
+ defaultLogger,
54
+ };
@@ -0,0 +1,24 @@
1
+ const retryOperation = async (operation, maxRetries = 3) => {
2
+ for (let attempt = 0; attempt < maxRetries; attempt++) {
3
+ try {
4
+ return await operation();
5
+ } catch (error) {
6
+ if (attempt === maxRetries - 1) throw error;
7
+
8
+ // Check if it's a network-related error
9
+ if (
10
+ error.name === "TimeoutError" ||
11
+ error.code === "NetworkingError" ||
12
+ error.message.includes("getaddrinfo ENOTFOUND")
13
+ ) {
14
+ const delay = Math.pow(2, attempt) * 100; // exponential backoff
15
+ await new Promise((resolve) => setTimeout(resolve, delay));
16
+ continue;
17
+ }
18
+
19
+ throw error; // rethrow non-network errors immediately
20
+ }
21
+ }
22
+ };
23
+
24
+ module.exports = { retryOperation };