s3db.js 8.0.1 → 8.0.3

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": "s3db.js",
3
- "version": "8.0.1",
3
+ "version": "8.0.3",
4
4
  "description": "Use AWS S3, the world's most reliable document storage, as a database with this ORM.",
5
5
  "main": "dist/s3db.cjs.js",
6
6
  "module": "dist/s3db.es.js",
@@ -34,7 +34,7 @@ export class Client extends EventEmitter {
34
34
  }) {
35
35
  super();
36
36
  this.verbose = verbose;
37
- this.id = id ?? idGenerator();
37
+ this.id = id ?? idGenerator(77);
38
38
  this.parallelism = parallelism;
39
39
  this.config = new ConnectionString(connectionString);
40
40
  this.httpClientOptions = {
@@ -6,13 +6,15 @@ import jsonStableStringify from "json-stable-stringify";
6
6
  import Client from "./client.class.js";
7
7
  import tryFn from "./concerns/try-fn.js";
8
8
  import Resource from "./resource.class.js";
9
- import { streamToString } from "./stream/index.js";
10
9
  import { ResourceNotFound } from "./errors.js";
10
+ import { idGenerator } from "./concerns/id.js";
11
+ import { streamToString } from "./stream/index.js";
11
12
 
12
13
  export class Database extends EventEmitter {
13
14
  constructor(options) {
14
15
  super();
15
16
 
17
+ this.id = idGenerator(7)
16
18
  this.version = "1";
17
19
  // Version is injected during build, fallback to "latest" for development
18
20
  this.s3dbVersion = (() => {
@@ -91,8 +91,8 @@ export class MemoryCache extends Cache {
91
91
  super(config);
92
92
  this.cache = {};
93
93
  this.meta = {};
94
- this.maxSize = config.maxSize || 0;
95
- this.ttl = config.ttl || 0;
94
+ this.maxSize = config.maxSize !== undefined ? config.maxSize : 1000;
95
+ this.ttl = config.ttl !== undefined ? config.ttl : 300000;
96
96
  }
97
97
 
98
98
  async _set(key, data) {
@@ -11,14 +11,26 @@ import tryFn from "../concerns/try-fn.js";
11
11
  export class CachePlugin extends Plugin {
12
12
  constructor(options = {}) {
13
13
  super(options);
14
- this.driver = options.driver;
15
- this.config = {
16
- includePartitions: options.includePartitions !== false,
17
- partitionStrategy: options.partitionStrategy || 'hierarchical',
18
- partitionAware: options.partitionAware !== false,
19
- trackUsage: options.trackUsage !== false,
20
- preloadRelated: options.preloadRelated !== false,
21
- ...options
14
+
15
+ // Extract primary configuration
16
+ this.driverName = options.driver || 's3';
17
+ this.ttl = options.ttl;
18
+ this.maxSize = options.maxSize;
19
+ this.config = options.config || {};
20
+
21
+ // Plugin-level settings
22
+ this.includePartitions = options.includePartitions !== false;
23
+ this.partitionStrategy = options.partitionStrategy || 'hierarchical';
24
+ this.partitionAware = options.partitionAware !== false;
25
+ this.trackUsage = options.trackUsage !== false;
26
+ this.preloadRelated = options.preloadRelated !== false;
27
+
28
+ // Legacy support - keep the old options for backward compatibility
29
+ this.legacyConfig = {
30
+ memoryOptions: options.memoryOptions,
31
+ filesystemOptions: options.filesystemOptions,
32
+ s3Options: options.s3Options,
33
+ driver: options.driver
22
34
  };
23
35
  }
24
36
 
@@ -28,26 +40,68 @@ export class CachePlugin extends Plugin {
28
40
 
29
41
  async onSetup() {
30
42
  // Initialize cache driver
31
- if (this.config.driver && typeof this.config.driver === 'object') {
43
+ if (this.driverName && typeof this.driverName === 'object') {
32
44
  // Use custom driver instance if provided
33
- this.driver = this.config.driver;
34
- } else if (this.config.driver === 'memory') {
35
- this.driver = new MemoryCache(this.config.memoryOptions || {});
36
- } else if (this.config.driver === 'filesystem') {
45
+ this.driver = this.driverName;
46
+ } else if (this.driverName === 'memory') {
47
+ // Build driver configuration with proper precedence
48
+ const driverConfig = {
49
+ ...this.legacyConfig.memoryOptions, // Legacy support (lowest priority)
50
+ ...this.config, // New config format (medium priority)
51
+ };
52
+
53
+ // Add global settings if defined (highest priority)
54
+ if (this.ttl !== undefined) {
55
+ driverConfig.ttl = this.ttl;
56
+ }
57
+ if (this.maxSize !== undefined) {
58
+ driverConfig.maxSize = this.maxSize;
59
+ }
60
+
61
+ this.driver = new MemoryCache(driverConfig);
62
+ } else if (this.driverName === 'filesystem') {
63
+ // Build driver configuration with proper precedence
64
+ const driverConfig = {
65
+ ...this.legacyConfig.filesystemOptions, // Legacy support (lowest priority)
66
+ ...this.config, // New config format (medium priority)
67
+ };
68
+
69
+ // Add global settings if defined (highest priority)
70
+ if (this.ttl !== undefined) {
71
+ driverConfig.ttl = this.ttl;
72
+ }
73
+ if (this.maxSize !== undefined) {
74
+ driverConfig.maxSize = this.maxSize;
75
+ }
76
+
37
77
  // Use partition-aware filesystem cache if enabled
38
- if (this.config.partitionAware) {
78
+ if (this.partitionAware) {
39
79
  this.driver = new PartitionAwareFilesystemCache({
40
- partitionStrategy: this.config.partitionStrategy,
41
- trackUsage: this.config.trackUsage,
42
- preloadRelated: this.config.preloadRelated,
43
- ...this.config.filesystemOptions
80
+ partitionStrategy: this.partitionStrategy,
81
+ trackUsage: this.trackUsage,
82
+ preloadRelated: this.preloadRelated,
83
+ ...driverConfig
44
84
  });
45
85
  } else {
46
- this.driver = new FilesystemCache(this.config.filesystemOptions || {});
86
+ this.driver = new FilesystemCache(driverConfig);
47
87
  }
48
88
  } else {
49
- // Default to S3Cache, sempre passa o client do database
50
- this.driver = new S3Cache({ client: this.database.client, ...(this.config.s3Options || {}) });
89
+ // Default to S3Cache - build driver configuration with proper precedence
90
+ const driverConfig = {
91
+ client: this.database.client, // Required for S3Cache
92
+ ...this.legacyConfig.s3Options, // Legacy support (lowest priority)
93
+ ...this.config, // New config format (medium priority)
94
+ };
95
+
96
+ // Add global settings if defined (highest priority)
97
+ if (this.ttl !== undefined) {
98
+ driverConfig.ttl = this.ttl;
99
+ }
100
+ if (this.maxSize !== undefined) {
101
+ driverConfig.maxSize = this.maxSize;
102
+ }
103
+
104
+ this.driver = new S3Cache(driverConfig);
51
105
  }
52
106
 
53
107
  // Use database hooks instead of method overwriting
@@ -7,14 +7,13 @@ import { PromisePool } from "@supercharge/promise-pool";
7
7
  import { chunk, cloneDeep, merge, isEmpty, isObject } from "lodash-es";
8
8
 
9
9
  import Schema from "./schema.class.js";
10
- import tryFn, { tryFnSync } from "./concerns/try-fn.js";
11
10
  import { streamToString } from "./stream/index.js";
12
- import { InvalidResourceItem, ResourceError, PartitionError } from "./errors.js";
11
+ import tryFn, { tryFnSync } from "./concerns/try-fn.js";
13
12
  import { ResourceReader, ResourceWriter } from "./stream/index.js"
14
13
  import { getBehavior, DEFAULT_BEHAVIOR } from "./behaviors/index.js";
15
14
  import { idGenerator as defaultIdGenerator } from "./concerns/id.js";
16
15
  import { calculateTotalSize, calculateEffectiveLimit } from "./concerns/calculator.js";
17
- import { mapAwsError } from "./errors.js";
16
+ import { mapAwsError, InvalidResourceItem, ResourceError, PartitionError } from "./errors.js";
18
17
 
19
18
 
20
19
  export class Resource extends EventEmitter {
@@ -99,12 +98,19 @@ export class Resource extends EventEmitter {
99
98
  */
100
99
  constructor(config = {}) {
101
100
  super();
102
- this._instanceId = Math.random().toString(36).slice(2, 8);
101
+ this._instanceId = defaultIdGenerator(7);
103
102
 
104
103
  // Validate configuration
105
104
  const validation = validateResourceConfig(config);
106
105
  if (!validation.isValid) {
107
- throw new ResourceError(`Invalid Resource ${config.name} configuration`, { resourceName: config.name, validation: validation.errors, operation: 'constructor', suggestion: 'Check resource config and attributes.' });
106
+ const errorDetails = validation.errors.map(err => ` ${err}`).join('\n');
107
+ throw new ResourceError(
108
+ `Invalid Resource ${config.name || '[unnamed]'} configuration:\n${errorDetails}`,
109
+ {
110
+ resourceName: config.name,
111
+ validation: validation.errors,
112
+ }
113
+ );
108
114
  }
109
115
 
110
116
  // Extract configuration with defaults - all at root level