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,106 @@
1
+ const { DynamoDBClient } = require("@aws-sdk/client-dynamodb");
2
+ const { DynamoDBDocument } = require("@aws-sdk/lib-dynamodb");
3
+ const { defaultLogger: logger } = require("./utils/logger");
4
+ const { pluginManager } = require("./plugin-manager");
5
+
6
+ class ModelManager {
7
+ static _instances = new Map();
8
+
9
+ constructor(config = {}) {
10
+ this._initialized = false;
11
+ this._docClient = null;
12
+ this._tableName = null;
13
+ this._testId = null;
14
+ this._models = new Map();
15
+ }
16
+
17
+ static getInstance(testId = null) {
18
+ const key = testId || "default";
19
+ if (!ModelManager._instances.has(key)) {
20
+ const instance = new ModelManager();
21
+ instance._testId = testId;
22
+ ModelManager._instances.set(key, instance);
23
+ }
24
+ return ModelManager._instances.get(key);
25
+ }
26
+
27
+ init(config = {}) {
28
+ const client = new DynamoDBClient({
29
+ region: config.aws.region,
30
+ });
31
+ this._docClient = DynamoDBDocument.from(client);
32
+ this._tableName = config.db.tableName;
33
+ this._testId = config.testId || this._testId;
34
+
35
+ // Initialize all registered models
36
+ for (const [_, ModelClass] of this._models) {
37
+ ModelClass._testId = this._testId;
38
+ ModelClass.documentClient = this._docClient;
39
+ ModelClass.table = this._tableName;
40
+ ModelClass._validateConfiguration();
41
+ }
42
+
43
+ this._initialized = true;
44
+ return this;
45
+ }
46
+
47
+ getModel(modelName) {
48
+ const ModelClass = this._models.get(modelName);
49
+ if (!ModelClass) {
50
+ throw new Error(`Model ${modelName} not found`);
51
+ }
52
+ return ModelClass;
53
+ }
54
+
55
+ // Registry methods
56
+ registerModel(ModelClass) {
57
+ this._models.set(ModelClass.name, ModelClass);
58
+
59
+ // Add plugin support to the model class
60
+ ModelClass.registerPlugin = function (plugin) {
61
+ pluginManager.registerPlugin(this.name, plugin);
62
+
63
+ // Apply methods directly to the prototype
64
+ if (plugin.methods) {
65
+ Object.entries(plugin.methods).forEach(([methodName, methodFn]) => {
66
+ if (ModelClass.prototype[methodName]) {
67
+ logger.warn(
68
+ `Method ${methodName} already exists for model ${this.name}`,
69
+ );
70
+ }
71
+ ModelClass.prototype[methodName] = methodFn;
72
+ });
73
+ }
74
+ };
75
+
76
+ // Initialize as before
77
+ ModelClass._testId = this._testId;
78
+ if (this._initialized) {
79
+ ModelClass.documentClient = this._docClient;
80
+ ModelClass.table = this._tableName;
81
+ ModelClass._validateConfiguration();
82
+ }
83
+
84
+ return ModelClass;
85
+ }
86
+
87
+ // Accessors
88
+ get documentClient() {
89
+ return this._docClient;
90
+ }
91
+
92
+ get tableName() {
93
+ return this._tableName;
94
+ }
95
+
96
+ getTestId() {
97
+ return this._testId;
98
+ }
99
+
100
+ // Helper method for debugging
101
+ listModels() {
102
+ return Array.from(this._models.keys());
103
+ }
104
+ }
105
+
106
+ module.exports = { ModelManager };