dynamo-bao 0.2.6 → 0.2.8

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dynamo-bao",
3
- "version": "0.2.6",
3
+ "version": "0.2.8",
4
4
  "description": "Simple powerful data modeling primitives for DynamoDB in JavaScript.",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
@@ -45,12 +45,13 @@
45
45
  "jest": "^29.7.0",
46
46
  "jsdoc": "^4.0.4",
47
47
  "prettier": "3.4.2",
48
- "serverless": "^3.38.0"
48
+ "serverless": "^3.38.0",
49
+ "eslint-plugin-promise": "^6.1.1"
49
50
  },
50
51
  "bin": {
51
52
  "bao-init": "bin/dynamo-bao-create-table.js",
52
- "bao-codegen": "bin/codegen.js",
53
- "bao-watch": "bin/watch.js",
53
+ "bao-codegen": "./bin/codegen.js",
54
+ "bao-watch": "./bin/watch.js",
54
55
  "bao-delete": "bin/dynamo-bao-delete-table.js"
55
56
  }
56
57
  }
package/src/config.js CHANGED
@@ -18,8 +18,10 @@ function findConfig() {
18
18
 
19
19
  const possibleNames = [
20
20
  "dynamo-bao.config.js",
21
+ "dynamo-bao.config.cjs",
21
22
  ".dynamo-bao/config.js",
22
23
  "config.js",
24
+ "config.cjs",
23
25
  ];
24
26
  const searchPaths = [
25
27
  process.cwd(), // Project root
@@ -63,6 +65,7 @@ function findConfig() {
63
65
  function normalizeConfig(rawConfig, configDir) {
64
66
  return {
65
67
  ...rawConfig,
68
+ models: rawConfig.models || null,
66
69
  paths: {
67
70
  ...(rawConfig.paths || {}),
68
71
  modelsDir: rawConfig.paths?.modelsDir
package/src/index.js CHANGED
@@ -56,9 +56,14 @@ function _registerModelsFromManifest(manager, manifestPath) {
56
56
  }
57
57
 
58
58
  function _registerModelsFromDirectory(manager, modelsDir) {
59
- if (!modelsDir || !fs.existsSync(modelsDir)) {
59
+ if (!modelsDir) {
60
60
  throw new ConfigurationError(
61
- `modelsDir is not defined or does not exist: ${modelsDir}`,
61
+ "modelsDir is required when no direct models are provided. Either specify a modelsDir or provide models directly in the config.",
62
+ );
63
+ }
64
+ if (!fs.existsSync(modelsDir)) {
65
+ throw new ConfigurationError(
66
+ `modelsDir does not exist: ${modelsDir}`,
62
67
  );
63
68
  }
64
69
  const modelFiles = findModelFiles(modelsDir);
@@ -75,7 +80,46 @@ function _registerModelsFromDirectory(manager, modelsDir) {
75
80
  return models;
76
81
  }
77
82
 
83
+ function _registerModelsFromDirectImports(manager, modelsObject) {
84
+ if (!modelsObject || typeof modelsObject !== 'object') {
85
+ throw new ConfigurationError(
86
+ "models must be an object containing model classes",
87
+ );
88
+ }
89
+
90
+ const models = {};
91
+ Object.entries(modelsObject).forEach(([name, ModelClass]) => {
92
+ if (!ModelClass || typeof ModelClass !== 'function') {
93
+ throw new ConfigurationError(
94
+ `Invalid model "${name}": must be a constructor function`,
95
+ );
96
+ }
97
+ if (!(ModelClass.prototype instanceof BaoModel)) {
98
+ throw new ConfigurationError(
99
+ `Model "${name}" must extend BaoModel`,
100
+ );
101
+ }
102
+ manager.registerModel(ModelClass);
103
+ models[name] = ModelClass;
104
+ });
105
+ return models;
106
+ }
107
+
78
108
  function _registerModels(manager, config) {
109
+ // If models are provided directly, use them
110
+ if (config.models) {
111
+ return _registerModelsFromDirectImports(manager, config.models);
112
+ }
113
+
114
+ // Check if filesystem is available
115
+ const fsAvailable = typeof fs !== 'undefined' && fs.existsSync;
116
+
117
+ if (!fsAvailable) {
118
+ throw new ConfigurationError(
119
+ "Filesystem not available. Please provide models directly using the 'models' config option.",
120
+ );
121
+ }
122
+
79
123
  const manifestPath = config.paths.generatedModelsManifest;
80
124
 
81
125
  // Try loading from manifest first
@@ -122,6 +166,7 @@ function initModels(userConfig = {}) {
122
166
  ...config.tenancy,
123
167
  ...(userConfig.tenancy || {}),
124
168
  },
169
+ models: userConfig.models || config.models || null,
125
170
  };
126
171
 
127
172
  // Validate tenant context if tenancy enabled
@@ -120,6 +120,10 @@ class ModelManager {
120
120
  listModels() {
121
121
  return Array.from(this._models.keys());
122
122
  }
123
+
124
+ getModels() {
125
+ return Array.from(this._models.values());
126
+ }
123
127
  }
124
128
 
125
129
  module.exports = { ModelManager };