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.
- package/README.md +333 -0
- package/bin/chokidar-test.js +60 -0
- package/bin/codegen.js +114 -0
- package/bin/dynamo-bao-create-table.js +136 -0
- package/bin/generators/index.js +0 -0
- package/bin/generators/model.js +428 -0
- package/bin/utils/scriptLogger.js +10 -0
- package/bin/watch.js +93 -0
- package/package.json +54 -0
- package/src/config.js +60 -0
- package/src/constants.js +35 -0
- package/src/db.js +21 -0
- package/src/fieldResolver.js +74 -0
- package/src/fields.js +781 -0
- package/src/filter-expression.js +241 -0
- package/src/index.js +127 -0
- package/src/key-condition.js +258 -0
- package/src/mixins/batch-loading-mixin.js +336 -0
- package/src/mixins/mutation-mixin.js +595 -0
- package/src/mixins/query-mixin.js +341 -0
- package/src/mixins/unique-constraint-mixin.js +131 -0
- package/src/mixins/validation-mixin.js +154 -0
- package/src/model-config.js +47 -0
- package/src/model-manager.js +106 -0
- package/src/model.js +689 -0
- package/src/object-not-found.js +44 -0
- package/src/plugin-manager.js +61 -0
- package/src/utils/logger.js +54 -0
- package/src/utils/retry-helper.js +24 -0
|
@@ -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 };
|