s3db.js 4.1.6 → 4.1.7
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/dist/s3db.cjs.js +438 -111
- package/dist/s3db.cjs.min.js +9 -9
- package/dist/s3db.es.js +438 -111
- package/dist/s3db.es.min.js +9 -9
- package/dist/s3db.iife.js +438 -111
- package/dist/s3db.iife.min.js +9 -9
- package/package.json +1 -1
package/dist/s3db.es.js
CHANGED
|
@@ -8566,6 +8566,14 @@ function calculateUTF8Bytes(str) {
|
|
|
8566
8566
|
}
|
|
8567
8567
|
return bytes;
|
|
8568
8568
|
}
|
|
8569
|
+
function calculateAttributeNamesSize(mappedObject) {
|
|
8570
|
+
let totalSize = 0;
|
|
8571
|
+
for (const key of Object.keys(mappedObject)) {
|
|
8572
|
+
if (key === "_v") continue;
|
|
8573
|
+
totalSize += calculateUTF8Bytes(key);
|
|
8574
|
+
}
|
|
8575
|
+
return totalSize;
|
|
8576
|
+
}
|
|
8569
8577
|
function transformValue(value) {
|
|
8570
8578
|
if (value === null || value === void 0) {
|
|
8571
8579
|
return "";
|
|
@@ -8600,49 +8608,31 @@ function calculateAttributeSizes(mappedObject) {
|
|
|
8600
8608
|
return sizes;
|
|
8601
8609
|
}
|
|
8602
8610
|
function calculateTotalSize(mappedObject) {
|
|
8603
|
-
const
|
|
8604
|
-
|
|
8611
|
+
const valueSizes = calculateAttributeSizes(mappedObject);
|
|
8612
|
+
const valueTotal = Object.values(valueSizes).reduce((total, size) => total + size, 0);
|
|
8613
|
+
const namesSize = calculateAttributeNamesSize(mappedObject);
|
|
8614
|
+
return valueTotal + namesSize;
|
|
8605
8615
|
}
|
|
8606
8616
|
|
|
8607
|
-
const S3_METADATA_LIMIT_BYTES
|
|
8617
|
+
const S3_METADATA_LIMIT_BYTES = 2048;
|
|
8608
8618
|
async function handleInsert$3({ resource, data, mappedData }) {
|
|
8609
8619
|
const totalSize = calculateTotalSize(mappedData);
|
|
8610
|
-
if (totalSize > S3_METADATA_LIMIT_BYTES
|
|
8611
|
-
|
|
8612
|
-
operation: "insert",
|
|
8613
|
-
totalSize,
|
|
8614
|
-
limit: S3_METADATA_LIMIT_BYTES$3,
|
|
8615
|
-
excess: totalSize - S3_METADATA_LIMIT_BYTES$3,
|
|
8616
|
-
data
|
|
8617
|
-
});
|
|
8620
|
+
if (totalSize > S3_METADATA_LIMIT_BYTES) {
|
|
8621
|
+
throw new Error(`S3 metadata size exceeds 2KB limit. Current size: ${totalSize} bytes, limit: ${S3_METADATA_LIMIT_BYTES} bytes`);
|
|
8618
8622
|
}
|
|
8619
8623
|
return { mappedData, body: "" };
|
|
8620
8624
|
}
|
|
8621
8625
|
async function handleUpdate$3({ resource, id, data, mappedData }) {
|
|
8622
8626
|
const totalSize = calculateTotalSize(mappedData);
|
|
8623
|
-
if (totalSize > S3_METADATA_LIMIT_BYTES
|
|
8624
|
-
|
|
8625
|
-
operation: "update",
|
|
8626
|
-
id,
|
|
8627
|
-
totalSize,
|
|
8628
|
-
limit: S3_METADATA_LIMIT_BYTES$3,
|
|
8629
|
-
excess: totalSize - S3_METADATA_LIMIT_BYTES$3,
|
|
8630
|
-
data
|
|
8631
|
-
});
|
|
8627
|
+
if (totalSize > S3_METADATA_LIMIT_BYTES) {
|
|
8628
|
+
throw new Error(`S3 metadata size exceeds 2KB limit. Current size: ${totalSize} bytes, limit: ${S3_METADATA_LIMIT_BYTES} bytes`);
|
|
8632
8629
|
}
|
|
8633
8630
|
return { mappedData, body: "" };
|
|
8634
8631
|
}
|
|
8635
8632
|
async function handleUpsert$3({ resource, id, data, mappedData }) {
|
|
8636
8633
|
const totalSize = calculateTotalSize(mappedData);
|
|
8637
|
-
if (totalSize > S3_METADATA_LIMIT_BYTES
|
|
8638
|
-
|
|
8639
|
-
operation: "upsert",
|
|
8640
|
-
id,
|
|
8641
|
-
totalSize,
|
|
8642
|
-
limit: S3_METADATA_LIMIT_BYTES$3,
|
|
8643
|
-
excess: totalSize - S3_METADATA_LIMIT_BYTES$3,
|
|
8644
|
-
data
|
|
8645
|
-
});
|
|
8634
|
+
if (totalSize > S3_METADATA_LIMIT_BYTES) {
|
|
8635
|
+
throw new Error(`S3 metadata size exceeds 2KB limit. Current size: ${totalSize} bytes, limit: ${S3_METADATA_LIMIT_BYTES} bytes`);
|
|
8646
8636
|
}
|
|
8647
8637
|
return { mappedData, body: "" };
|
|
8648
8638
|
}
|
|
@@ -8650,33 +8640,53 @@ async function handleGet$3({ resource, metadata, body }) {
|
|
|
8650
8640
|
return { metadata, body };
|
|
8651
8641
|
}
|
|
8652
8642
|
|
|
8653
|
-
var
|
|
8643
|
+
var enforceLimits = /*#__PURE__*/Object.freeze({
|
|
8654
8644
|
__proto__: null,
|
|
8645
|
+
S3_METADATA_LIMIT_BYTES: S3_METADATA_LIMIT_BYTES,
|
|
8655
8646
|
handleGet: handleGet$3,
|
|
8656
8647
|
handleInsert: handleInsert$3,
|
|
8657
8648
|
handleUpdate: handleUpdate$3,
|
|
8658
8649
|
handleUpsert: handleUpsert$3
|
|
8659
8650
|
});
|
|
8660
8651
|
|
|
8661
|
-
const S3_METADATA_LIMIT_BYTES$2 = 2e3;
|
|
8662
8652
|
async function handleInsert$2({ resource, data, mappedData }) {
|
|
8663
8653
|
const totalSize = calculateTotalSize(mappedData);
|
|
8664
|
-
if (totalSize > S3_METADATA_LIMIT_BYTES
|
|
8665
|
-
|
|
8654
|
+
if (totalSize > S3_METADATA_LIMIT_BYTES) {
|
|
8655
|
+
resource.emit("exceedsLimit", {
|
|
8656
|
+
operation: "insert",
|
|
8657
|
+
totalSize,
|
|
8658
|
+
limit: S3_METADATA_LIMIT_BYTES,
|
|
8659
|
+
excess: totalSize - S3_METADATA_LIMIT_BYTES,
|
|
8660
|
+
data
|
|
8661
|
+
});
|
|
8666
8662
|
}
|
|
8667
8663
|
return { mappedData, body: "" };
|
|
8668
8664
|
}
|
|
8669
8665
|
async function handleUpdate$2({ resource, id, data, mappedData }) {
|
|
8670
8666
|
const totalSize = calculateTotalSize(mappedData);
|
|
8671
|
-
if (totalSize > S3_METADATA_LIMIT_BYTES
|
|
8672
|
-
|
|
8667
|
+
if (totalSize > S3_METADATA_LIMIT_BYTES) {
|
|
8668
|
+
resource.emit("exceedsLimit", {
|
|
8669
|
+
operation: "update",
|
|
8670
|
+
id,
|
|
8671
|
+
totalSize,
|
|
8672
|
+
limit: S3_METADATA_LIMIT_BYTES,
|
|
8673
|
+
excess: totalSize - S3_METADATA_LIMIT_BYTES,
|
|
8674
|
+
data
|
|
8675
|
+
});
|
|
8673
8676
|
}
|
|
8674
8677
|
return { mappedData, body: "" };
|
|
8675
8678
|
}
|
|
8676
8679
|
async function handleUpsert$2({ resource, id, data, mappedData }) {
|
|
8677
8680
|
const totalSize = calculateTotalSize(mappedData);
|
|
8678
|
-
if (totalSize > S3_METADATA_LIMIT_BYTES
|
|
8679
|
-
|
|
8681
|
+
if (totalSize > S3_METADATA_LIMIT_BYTES) {
|
|
8682
|
+
resource.emit("exceedsLimit", {
|
|
8683
|
+
operation: "upsert",
|
|
8684
|
+
id,
|
|
8685
|
+
totalSize,
|
|
8686
|
+
limit: S3_METADATA_LIMIT_BYTES,
|
|
8687
|
+
excess: totalSize - S3_METADATA_LIMIT_BYTES,
|
|
8688
|
+
data
|
|
8689
|
+
});
|
|
8680
8690
|
}
|
|
8681
8691
|
return { mappedData, body: "" };
|
|
8682
8692
|
}
|
|
@@ -8684,7 +8694,7 @@ async function handleGet$2({ resource, metadata, body }) {
|
|
|
8684
8694
|
return { metadata, body };
|
|
8685
8695
|
}
|
|
8686
8696
|
|
|
8687
|
-
var
|
|
8697
|
+
var userManagement = /*#__PURE__*/Object.freeze({
|
|
8688
8698
|
__proto__: null,
|
|
8689
8699
|
handleGet: handleGet$2,
|
|
8690
8700
|
handleInsert: handleInsert$2,
|
|
@@ -8692,7 +8702,6 @@ var enforceLimits = /*#__PURE__*/Object.freeze({
|
|
|
8692
8702
|
handleUpsert: handleUpsert$2
|
|
8693
8703
|
});
|
|
8694
8704
|
|
|
8695
|
-
const S3_METADATA_LIMIT_BYTES$1 = 2e3;
|
|
8696
8705
|
const TRUNCATE_SUFFIX = "...";
|
|
8697
8706
|
const TRUNCATE_SUFFIX_BYTES = calculateUTF8Bytes(TRUNCATE_SUFFIX);
|
|
8698
8707
|
async function handleInsert$1({ resource, data, mappedData }) {
|
|
@@ -8713,7 +8722,7 @@ function handleTruncate({ resource, data, mappedData }) {
|
|
|
8713
8722
|
const result = {};
|
|
8714
8723
|
let currentSize = 0;
|
|
8715
8724
|
for (const [key, size] of sortedAttributes) {
|
|
8716
|
-
const availableSpace = S3_METADATA_LIMIT_BYTES
|
|
8725
|
+
const availableSpace = S3_METADATA_LIMIT_BYTES - currentSize;
|
|
8717
8726
|
if (size <= availableSpace) {
|
|
8718
8727
|
result[key] = mappedData[key];
|
|
8719
8728
|
currentSize += size;
|
|
@@ -8733,7 +8742,7 @@ function handleTruncate({ resource, data, mappedData }) {
|
|
|
8733
8742
|
}
|
|
8734
8743
|
}
|
|
8735
8744
|
result[key] = truncatedValue + TRUNCATE_SUFFIX;
|
|
8736
|
-
currentSize = S3_METADATA_LIMIT_BYTES
|
|
8745
|
+
currentSize = S3_METADATA_LIMIT_BYTES;
|
|
8737
8746
|
break;
|
|
8738
8747
|
} else {
|
|
8739
8748
|
break;
|
|
@@ -8750,7 +8759,6 @@ var dataTruncate = /*#__PURE__*/Object.freeze({
|
|
|
8750
8759
|
handleUpsert: handleUpsert$1
|
|
8751
8760
|
});
|
|
8752
8761
|
|
|
8753
|
-
const S3_METADATA_LIMIT_BYTES = 2e3;
|
|
8754
8762
|
const OVERFLOW_FLAG = "$overflow";
|
|
8755
8763
|
const OVERFLOW_FLAG_VALUE = "true";
|
|
8756
8764
|
const OVERFLOW_FLAG_BYTES = calculateUTF8Bytes(OVERFLOW_FLAG) + calculateUTF8Bytes(OVERFLOW_FLAG_VALUE);
|
|
@@ -9093,13 +9101,29 @@ class Resource extends EventEmitter {
|
|
|
9093
9101
|
return join(`resource=${this.name}`, `v=${this.version}`, `id=${id}`);
|
|
9094
9102
|
}
|
|
9095
9103
|
/**
|
|
9096
|
-
*
|
|
9097
|
-
* @param {
|
|
9098
|
-
* @param {string}
|
|
9099
|
-
* @param {
|
|
9100
|
-
* @
|
|
9104
|
+
* Generate partition key for a resource in a specific partition
|
|
9105
|
+
* @param {Object} params - Partition key parameters
|
|
9106
|
+
* @param {string} params.partitionName - Name of the partition
|
|
9107
|
+
* @param {string} params.id - Resource ID
|
|
9108
|
+
* @param {Object} params.data - Resource data for partition value extraction
|
|
9109
|
+
* @returns {string|null} The partition key path or null if required fields are missing
|
|
9110
|
+
* @example
|
|
9111
|
+
* const partitionKey = resource.getPartitionKey({
|
|
9112
|
+
* partitionName: 'byUtmSource',
|
|
9113
|
+
* id: 'user-123',
|
|
9114
|
+
* data: { utm: { source: 'google' } }
|
|
9115
|
+
* });
|
|
9116
|
+
* // Returns: 'resource=users/partition=byUtmSource/utm.source=google/id=user-123'
|
|
9117
|
+
*
|
|
9118
|
+
* // Returns null if required field is missing
|
|
9119
|
+
* const nullKey = resource.getPartitionKey({
|
|
9120
|
+
* partitionName: 'byUtmSource',
|
|
9121
|
+
* id: 'user-123',
|
|
9122
|
+
* data: { name: 'John' } // Missing utm.source
|
|
9123
|
+
* });
|
|
9124
|
+
* // Returns: null
|
|
9101
9125
|
*/
|
|
9102
|
-
getPartitionKey(partitionName, id, data) {
|
|
9126
|
+
getPartitionKey({ partitionName, id, data }) {
|
|
9103
9127
|
const partition = this.options.partitions[partitionName];
|
|
9104
9128
|
if (!partition) {
|
|
9105
9129
|
throw new Error(`Partition '${partitionName}' not found`);
|
|
@@ -9130,15 +9154,36 @@ class Resource extends EventEmitter {
|
|
|
9130
9154
|
return data[fieldPath];
|
|
9131
9155
|
}
|
|
9132
9156
|
const keys = fieldPath.split(".");
|
|
9133
|
-
let
|
|
9157
|
+
let currentLevel = data;
|
|
9134
9158
|
for (const key of keys) {
|
|
9135
|
-
if (
|
|
9159
|
+
if (!currentLevel || typeof currentLevel !== "object" || !(key in currentLevel)) {
|
|
9136
9160
|
return void 0;
|
|
9137
9161
|
}
|
|
9138
|
-
|
|
9162
|
+
currentLevel = currentLevel[key];
|
|
9139
9163
|
}
|
|
9140
|
-
return
|
|
9164
|
+
return currentLevel;
|
|
9141
9165
|
}
|
|
9166
|
+
/**
|
|
9167
|
+
* Insert a new resource object
|
|
9168
|
+
* @param {Object} params - Insert parameters
|
|
9169
|
+
* @param {string} [params.id] - Resource ID (auto-generated if not provided)
|
|
9170
|
+
* @param {...Object} params - Resource attributes (any additional properties)
|
|
9171
|
+
* @returns {Promise<Object>} The inserted resource object with all attributes and generated ID
|
|
9172
|
+
* @example
|
|
9173
|
+
* // Insert with auto-generated ID
|
|
9174
|
+
* const user = await resource.insert({
|
|
9175
|
+
* name: 'John Doe',
|
|
9176
|
+
* email: 'john@example.com',
|
|
9177
|
+
* age: 30
|
|
9178
|
+
* });
|
|
9179
|
+
*
|
|
9180
|
+
* // Insert with custom ID
|
|
9181
|
+
* const user = await resource.insert({
|
|
9182
|
+
* id: 'custom-id-123',
|
|
9183
|
+
* name: 'Jane Smith',
|
|
9184
|
+
* email: 'jane@example.com'
|
|
9185
|
+
* });
|
|
9186
|
+
*/
|
|
9142
9187
|
async insert({ id, ...attributes }) {
|
|
9143
9188
|
if (this.options.timestamps) {
|
|
9144
9189
|
attributes.createdAt = (/* @__PURE__ */ new Date()).toISOString();
|
|
@@ -9177,6 +9222,16 @@ class Resource extends EventEmitter {
|
|
|
9177
9222
|
this.emit("insert", final);
|
|
9178
9223
|
return final;
|
|
9179
9224
|
}
|
|
9225
|
+
/**
|
|
9226
|
+
* Retrieve a resource object by ID
|
|
9227
|
+
* @param {string} id - Resource ID
|
|
9228
|
+
* @returns {Promise<Object>} The resource object with all attributes and metadata
|
|
9229
|
+
* @example
|
|
9230
|
+
* const user = await resource.get('user-123');
|
|
9231
|
+
* console.log(user.name); // 'John Doe'
|
|
9232
|
+
* console.log(user._lastModified); // Date object
|
|
9233
|
+
* console.log(user._hasContent); // boolean
|
|
9234
|
+
*/
|
|
9180
9235
|
async get(id) {
|
|
9181
9236
|
const key = this.getResourceKey(id);
|
|
9182
9237
|
const request = await this.client.headObject(key);
|
|
@@ -9210,6 +9265,16 @@ class Resource extends EventEmitter {
|
|
|
9210
9265
|
this.emit("get", data);
|
|
9211
9266
|
return data;
|
|
9212
9267
|
}
|
|
9268
|
+
/**
|
|
9269
|
+
* Check if a resource exists by ID
|
|
9270
|
+
* @param {string} id - Resource ID
|
|
9271
|
+
* @returns {Promise<boolean>} True if resource exists, false otherwise
|
|
9272
|
+
* @example
|
|
9273
|
+
* const exists = await resource.exists('user-123');
|
|
9274
|
+
* if (exists) {
|
|
9275
|
+
* console.log('User exists');
|
|
9276
|
+
* }
|
|
9277
|
+
*/
|
|
9213
9278
|
async exists(id) {
|
|
9214
9279
|
try {
|
|
9215
9280
|
const key = this.getResourceKey(id);
|
|
@@ -9219,6 +9284,24 @@ class Resource extends EventEmitter {
|
|
|
9219
9284
|
return false;
|
|
9220
9285
|
}
|
|
9221
9286
|
}
|
|
9287
|
+
/**
|
|
9288
|
+
* Update an existing resource object
|
|
9289
|
+
* @param {string} id - Resource ID
|
|
9290
|
+
* @param {Object} attributes - Attributes to update (partial update supported)
|
|
9291
|
+
* @returns {Promise<Object>} The updated resource object with all attributes
|
|
9292
|
+
* @example
|
|
9293
|
+
* // Update specific fields
|
|
9294
|
+
* const updatedUser = await resource.update('user-123', {
|
|
9295
|
+
* name: 'John Updated',
|
|
9296
|
+
* age: 31
|
|
9297
|
+
* });
|
|
9298
|
+
*
|
|
9299
|
+
* // Update with timestamps (if enabled)
|
|
9300
|
+
* const updatedUser = await resource.update('user-123', {
|
|
9301
|
+
* email: 'newemail@example.com'
|
|
9302
|
+
* });
|
|
9303
|
+
* console.log(updatedUser.updatedAt); // ISO timestamp
|
|
9304
|
+
*/
|
|
9222
9305
|
async update(id, attributes) {
|
|
9223
9306
|
const live = await this.get(id);
|
|
9224
9307
|
if (this.options.timestamps) {
|
|
@@ -9275,6 +9358,14 @@ class Resource extends EventEmitter {
|
|
|
9275
9358
|
this.emit("update", preProcessedData, validated);
|
|
9276
9359
|
return validated;
|
|
9277
9360
|
}
|
|
9361
|
+
/**
|
|
9362
|
+
* Delete a resource object by ID
|
|
9363
|
+
* @param {string} id - Resource ID
|
|
9364
|
+
* @returns {Promise<Object>} S3 delete response
|
|
9365
|
+
* @example
|
|
9366
|
+
* await resource.delete('user-123');
|
|
9367
|
+
* console.log('User deleted successfully');
|
|
9368
|
+
*/
|
|
9278
9369
|
async delete(id) {
|
|
9279
9370
|
let objectData;
|
|
9280
9371
|
try {
|
|
@@ -9289,6 +9380,20 @@ class Resource extends EventEmitter {
|
|
|
9289
9380
|
this.emit("delete", id);
|
|
9290
9381
|
return response;
|
|
9291
9382
|
}
|
|
9383
|
+
/**
|
|
9384
|
+
* Insert or update a resource object (upsert operation)
|
|
9385
|
+
* @param {Object} params - Upsert parameters
|
|
9386
|
+
* @param {string} params.id - Resource ID (required for upsert)
|
|
9387
|
+
* @param {...Object} params - Resource attributes (any additional properties)
|
|
9388
|
+
* @returns {Promise<Object>} The inserted or updated resource object
|
|
9389
|
+
* @example
|
|
9390
|
+
* // Will insert if doesn't exist, update if exists
|
|
9391
|
+
* const user = await resource.upsert({
|
|
9392
|
+
* id: 'user-123',
|
|
9393
|
+
* name: 'John Doe',
|
|
9394
|
+
* email: 'john@example.com'
|
|
9395
|
+
* });
|
|
9396
|
+
*/
|
|
9292
9397
|
async upsert({ id, ...attributes }) {
|
|
9293
9398
|
const exists = await this.exists(id);
|
|
9294
9399
|
if (exists) {
|
|
@@ -9296,6 +9401,28 @@ class Resource extends EventEmitter {
|
|
|
9296
9401
|
}
|
|
9297
9402
|
return this.insert({ id, ...attributes });
|
|
9298
9403
|
}
|
|
9404
|
+
/**
|
|
9405
|
+
* Count resources with optional partition filtering
|
|
9406
|
+
* @param {Object} [params] - Count parameters
|
|
9407
|
+
* @param {string} [params.partition] - Partition name to count in
|
|
9408
|
+
* @param {Object} [params.partitionValues] - Partition field values to filter by
|
|
9409
|
+
* @returns {Promise<number>} Total count of matching resources
|
|
9410
|
+
* @example
|
|
9411
|
+
* // Count all resources
|
|
9412
|
+
* const total = await resource.count();
|
|
9413
|
+
*
|
|
9414
|
+
* // Count in specific partition
|
|
9415
|
+
* const googleUsers = await resource.count({
|
|
9416
|
+
* partition: 'byUtmSource',
|
|
9417
|
+
* partitionValues: { 'utm.source': 'google' }
|
|
9418
|
+
* });
|
|
9419
|
+
*
|
|
9420
|
+
* // Count in multi-field partition
|
|
9421
|
+
* const usElectronics = await resource.count({
|
|
9422
|
+
* partition: 'byCategoryRegion',
|
|
9423
|
+
* partitionValues: { category: 'electronics', region: 'US' }
|
|
9424
|
+
* });
|
|
9425
|
+
*/
|
|
9299
9426
|
async count({ partition = null, partitionValues = {} } = {}) {
|
|
9300
9427
|
let prefix;
|
|
9301
9428
|
if (partition && Object.keys(partitionValues).length > 0) {
|
|
@@ -9326,6 +9453,19 @@ class Resource extends EventEmitter {
|
|
|
9326
9453
|
this.emit("count", count);
|
|
9327
9454
|
return count;
|
|
9328
9455
|
}
|
|
9456
|
+
/**
|
|
9457
|
+
* Insert multiple resources in parallel
|
|
9458
|
+
* @param {Object[]} objects - Array of resource objects to insert
|
|
9459
|
+
* @returns {Promise<Object[]>} Array of inserted resource objects
|
|
9460
|
+
* @example
|
|
9461
|
+
* const users = [
|
|
9462
|
+
* { name: 'John', email: 'john@example.com' },
|
|
9463
|
+
* { name: 'Jane', email: 'jane@example.com' },
|
|
9464
|
+
* { name: 'Bob', email: 'bob@example.com' }
|
|
9465
|
+
* ];
|
|
9466
|
+
* const insertedUsers = await resource.insertMany(users);
|
|
9467
|
+
* console.log(`Inserted ${insertedUsers.length} users`);
|
|
9468
|
+
*/
|
|
9329
9469
|
async insertMany(objects) {
|
|
9330
9470
|
const { results } = await PromisePool.for(objects).withConcurrency(this.parallelism).handleError(async (error, content) => {
|
|
9331
9471
|
this.emit("error", error, content);
|
|
@@ -9337,6 +9477,15 @@ class Resource extends EventEmitter {
|
|
|
9337
9477
|
this.emit("insertMany", objects.length);
|
|
9338
9478
|
return results;
|
|
9339
9479
|
}
|
|
9480
|
+
/**
|
|
9481
|
+
* Delete multiple resources by their IDs in parallel
|
|
9482
|
+
* @param {string[]} ids - Array of resource IDs to delete
|
|
9483
|
+
* @returns {Promise<Object[]>} Array of S3 delete responses
|
|
9484
|
+
* @example
|
|
9485
|
+
* const deletedIds = ['user-1', 'user-2', 'user-3'];
|
|
9486
|
+
* const results = await resource.deleteMany(deletedIds);
|
|
9487
|
+
* console.log(`Deleted ${deletedIds.length} users`);
|
|
9488
|
+
*/
|
|
9340
9489
|
async deleteMany(ids) {
|
|
9341
9490
|
const packages = chunk(
|
|
9342
9491
|
ids.map((id) => this.getResourceKey(id)),
|
|
@@ -9395,6 +9544,28 @@ class Resource extends EventEmitter {
|
|
|
9395
9544
|
});
|
|
9396
9545
|
return { deletedCount, resource: this.name };
|
|
9397
9546
|
}
|
|
9547
|
+
/**
|
|
9548
|
+
* List resource IDs with optional partition filtering
|
|
9549
|
+
* @param {Object} [params] - List parameters
|
|
9550
|
+
* @param {string} [params.partition] - Partition name to list from
|
|
9551
|
+
* @param {Object} [params.partitionValues] - Partition field values to filter by
|
|
9552
|
+
* @returns {Promise<string[]>} Array of resource IDs (strings)
|
|
9553
|
+
* @example
|
|
9554
|
+
* // List all IDs
|
|
9555
|
+
* const allIds = await resource.listIds();
|
|
9556
|
+
*
|
|
9557
|
+
* // List IDs from specific partition
|
|
9558
|
+
* const googleUserIds = await resource.listIds({
|
|
9559
|
+
* partition: 'byUtmSource',
|
|
9560
|
+
* partitionValues: { 'utm.source': 'google' }
|
|
9561
|
+
* });
|
|
9562
|
+
*
|
|
9563
|
+
* // List IDs from multi-field partition
|
|
9564
|
+
* const usElectronicsIds = await resource.listIds({
|
|
9565
|
+
* partition: 'byCategoryRegion',
|
|
9566
|
+
* partitionValues: { category: 'electronics', region: 'US' }
|
|
9567
|
+
* });
|
|
9568
|
+
*/
|
|
9398
9569
|
async listIds({ partition = null, partitionValues = {} } = {}) {
|
|
9399
9570
|
let prefix;
|
|
9400
9571
|
if (partition && Object.keys(partitionValues).length > 0) {
|
|
@@ -9431,13 +9602,36 @@ class Resource extends EventEmitter {
|
|
|
9431
9602
|
return ids;
|
|
9432
9603
|
}
|
|
9433
9604
|
/**
|
|
9434
|
-
* List objects
|
|
9435
|
-
* @param {Object}
|
|
9436
|
-
* @param {
|
|
9437
|
-
* @
|
|
9605
|
+
* List resource objects with optional partition filtering and pagination
|
|
9606
|
+
* @param {Object} [params] - List parameters
|
|
9607
|
+
* @param {string} [params.partition] - Partition name to list from
|
|
9608
|
+
* @param {Object} [params.partitionValues] - Partition field values to filter by
|
|
9609
|
+
* @param {number} [params.limit] - Maximum number of results to return
|
|
9610
|
+
* @param {number} [params.offset=0] - Offset for pagination
|
|
9611
|
+
* @returns {Promise<Object[]>} Array of resource objects with all attributes
|
|
9612
|
+
* @example
|
|
9613
|
+
* // List all resources
|
|
9614
|
+
* const allUsers = await resource.list();
|
|
9615
|
+
*
|
|
9616
|
+
* // List with pagination
|
|
9617
|
+
* const firstPage = await resource.list({ limit: 10, offset: 0 });
|
|
9618
|
+
* const secondPage = await resource.list({ limit: 10, offset: 10 });
|
|
9619
|
+
*
|
|
9620
|
+
* // List from specific partition
|
|
9621
|
+
* const googleUsers = await resource.list({
|
|
9622
|
+
* partition: 'byUtmSource',
|
|
9623
|
+
* partitionValues: { 'utm.source': 'google' }
|
|
9624
|
+
* });
|
|
9625
|
+
*
|
|
9626
|
+
* // List from partition with pagination
|
|
9627
|
+
* const googleUsersPage = await resource.list({
|
|
9628
|
+
* partition: 'byUtmSource',
|
|
9629
|
+
* partitionValues: { 'utm.source': 'google' },
|
|
9630
|
+
* limit: 5,
|
|
9631
|
+
* offset: 0
|
|
9632
|
+
* });
|
|
9438
9633
|
*/
|
|
9439
|
-
async
|
|
9440
|
-
const { limit, offset = 0 } = options;
|
|
9634
|
+
async list({ partition = null, partitionValues = {}, limit, offset = 0 } = {}) {
|
|
9441
9635
|
if (!partition) {
|
|
9442
9636
|
const ids2 = await this.listIds({ partition, partitionValues });
|
|
9443
9637
|
let filteredIds2 = ids2.slice(offset);
|
|
@@ -9447,7 +9641,7 @@ class Resource extends EventEmitter {
|
|
|
9447
9641
|
const { results: results2 } = await PromisePool.for(filteredIds2).withConcurrency(this.parallelism).process(async (id) => {
|
|
9448
9642
|
return await this.get(id);
|
|
9449
9643
|
});
|
|
9450
|
-
this.emit("
|
|
9644
|
+
this.emit("list", { partition, partitionValues, count: results2.length });
|
|
9451
9645
|
return results2;
|
|
9452
9646
|
}
|
|
9453
9647
|
const partitionDef = this.options.partitions[partition];
|
|
@@ -9480,11 +9674,19 @@ class Resource extends EventEmitter {
|
|
|
9480
9674
|
filteredIds = filteredIds.slice(0, limit);
|
|
9481
9675
|
}
|
|
9482
9676
|
const { results } = await PromisePool.for(filteredIds).withConcurrency(this.parallelism).process(async (id) => {
|
|
9483
|
-
return await this.getFromPartition(id, partition, partitionValues);
|
|
9677
|
+
return await this.getFromPartition({ id, partitionName: partition, partitionValues });
|
|
9484
9678
|
});
|
|
9485
|
-
this.emit("
|
|
9679
|
+
this.emit("list", { partition, partitionValues, count: results.length });
|
|
9486
9680
|
return results;
|
|
9487
9681
|
}
|
|
9682
|
+
/**
|
|
9683
|
+
* Get multiple resources by their IDs
|
|
9684
|
+
* @param {string[]} ids - Array of resource IDs
|
|
9685
|
+
* @returns {Promise<Object[]>} Array of resource objects
|
|
9686
|
+
* @example
|
|
9687
|
+
* const users = await resource.getMany(['user-1', 'user-2', 'user-3']);
|
|
9688
|
+
* users.forEach(user => console.log(user.name));
|
|
9689
|
+
*/
|
|
9488
9690
|
async getMany(ids) {
|
|
9489
9691
|
const { results } = await PromisePool.for(ids).withConcurrency(this.client.parallelism).process(async (id) => {
|
|
9490
9692
|
this.emit("id", id);
|
|
@@ -9495,6 +9697,13 @@ class Resource extends EventEmitter {
|
|
|
9495
9697
|
this.emit("getMany", ids.length);
|
|
9496
9698
|
return results;
|
|
9497
9699
|
}
|
|
9700
|
+
/**
|
|
9701
|
+
* Get all resources (equivalent to list() without pagination)
|
|
9702
|
+
* @returns {Promise<Object[]>} Array of all resource objects
|
|
9703
|
+
* @example
|
|
9704
|
+
* const allUsers = await resource.getAll();
|
|
9705
|
+
* console.log(`Total users: ${allUsers.length}`);
|
|
9706
|
+
*/
|
|
9498
9707
|
async getAll() {
|
|
9499
9708
|
let ids = await this.listIds();
|
|
9500
9709
|
if (ids.length === 0) return [];
|
|
@@ -9505,18 +9714,41 @@ class Resource extends EventEmitter {
|
|
|
9505
9714
|
this.emit("getAll", results.length);
|
|
9506
9715
|
return results;
|
|
9507
9716
|
}
|
|
9508
|
-
|
|
9509
|
-
|
|
9510
|
-
|
|
9717
|
+
/**
|
|
9718
|
+
* Get a page of resources with pagination metadata
|
|
9719
|
+
* @param {Object} [params] - Page parameters
|
|
9720
|
+
* @param {number} [params.offset=0] - Offset for pagination
|
|
9721
|
+
* @param {number} [params.size=100] - Page size
|
|
9722
|
+
* @param {string} [params.partition] - Partition name to page from
|
|
9723
|
+
* @param {Object} [params.partitionValues] - Partition field values to filter by
|
|
9724
|
+
* @returns {Promise<Object>} Page result with items and pagination info
|
|
9725
|
+
* @example
|
|
9726
|
+
* // Get first page of all resources
|
|
9727
|
+
* const page = await resource.page({ offset: 0, size: 10 });
|
|
9728
|
+
* console.log(`Page ${page.page + 1} of ${page.totalPages}`);
|
|
9729
|
+
* console.log(`Showing ${page.items.length} of ${page.totalItems} total`);
|
|
9730
|
+
*
|
|
9731
|
+
* // Get page from specific partition
|
|
9732
|
+
* const googlePage = await resource.page({
|
|
9733
|
+
* partition: 'byUtmSource',
|
|
9734
|
+
* partitionValues: { 'utm.source': 'google' },
|
|
9735
|
+
* offset: 0,
|
|
9736
|
+
* size: 5
|
|
9737
|
+
* });
|
|
9738
|
+
*/
|
|
9739
|
+
async page({ offset = 0, size = 100, partition = null, partitionValues = {} } = {}) {
|
|
9740
|
+
const ids = await this.listIds({ partition, partitionValues });
|
|
9741
|
+
const totalItems = ids.length;
|
|
9511
9742
|
const totalPages = Math.ceil(totalItems / size);
|
|
9512
|
-
const
|
|
9743
|
+
const page = Math.floor(offset / size);
|
|
9744
|
+
const pageIds = ids.slice(offset, offset + size);
|
|
9513
9745
|
const items = await Promise.all(
|
|
9514
|
-
|
|
9746
|
+
pageIds.map((id) => this.get(id))
|
|
9515
9747
|
);
|
|
9516
9748
|
const result = {
|
|
9517
9749
|
items,
|
|
9518
9750
|
totalItems,
|
|
9519
|
-
page
|
|
9751
|
+
page,
|
|
9520
9752
|
pageSize: size,
|
|
9521
9753
|
totalPages
|
|
9522
9754
|
};
|
|
@@ -9532,36 +9764,62 @@ class Resource extends EventEmitter {
|
|
|
9532
9764
|
return stream.build();
|
|
9533
9765
|
}
|
|
9534
9766
|
/**
|
|
9535
|
-
*
|
|
9536
|
-
* @param {
|
|
9537
|
-
* @param {
|
|
9538
|
-
* @param {string}
|
|
9767
|
+
* Set binary content for a resource
|
|
9768
|
+
* @param {Object} params - Content parameters
|
|
9769
|
+
* @param {string} params.id - Resource ID
|
|
9770
|
+
* @param {Buffer|string} params.buffer - Content buffer or string
|
|
9771
|
+
* @param {string} [params.contentType='application/octet-stream'] - Content type
|
|
9772
|
+
* @returns {Promise<Object>} Updated resource data
|
|
9773
|
+
* @example
|
|
9774
|
+
* // Set image content
|
|
9775
|
+
* const imageBuffer = fs.readFileSync('image.jpg');
|
|
9776
|
+
* await resource.setContent({
|
|
9777
|
+
* id: 'user-123',
|
|
9778
|
+
* buffer: imageBuffer,
|
|
9779
|
+
* contentType: 'image/jpeg'
|
|
9780
|
+
* });
|
|
9781
|
+
*
|
|
9782
|
+
* // Set text content
|
|
9783
|
+
* await resource.setContent({
|
|
9784
|
+
* id: 'document-456',
|
|
9785
|
+
* buffer: 'Hello World',
|
|
9786
|
+
* contentType: 'text/plain'
|
|
9787
|
+
* });
|
|
9539
9788
|
*/
|
|
9540
|
-
async setContent(id, buffer, contentType = "application/octet-stream") {
|
|
9541
|
-
|
|
9542
|
-
|
|
9543
|
-
|
|
9544
|
-
|
|
9545
|
-
|
|
9546
|
-
|
|
9547
|
-
|
|
9548
|
-
|
|
9549
|
-
|
|
9550
|
-
}
|
|
9551
|
-
|
|
9552
|
-
key,
|
|
9789
|
+
async setContent({ id, buffer, contentType = "application/octet-stream" }) {
|
|
9790
|
+
const currentData = await this.get(id);
|
|
9791
|
+
if (!currentData) {
|
|
9792
|
+
throw new Error(`Resource with id '${id}' not found`);
|
|
9793
|
+
}
|
|
9794
|
+
const updatedData = {
|
|
9795
|
+
...currentData,
|
|
9796
|
+
_hasContent: true,
|
|
9797
|
+
_contentLength: buffer.length,
|
|
9798
|
+
_mimeType: contentType
|
|
9799
|
+
};
|
|
9800
|
+
await this.client.putObject({
|
|
9801
|
+
key: this.getResourceKey(id),
|
|
9802
|
+
metadata: await this.schema.mapper(updatedData),
|
|
9553
9803
|
body: buffer,
|
|
9554
|
-
contentType
|
|
9555
|
-
metadata: existingMetadata
|
|
9556
|
-
// Preserve existing metadata
|
|
9804
|
+
contentType
|
|
9557
9805
|
});
|
|
9558
|
-
this.emit("setContent", id, buffer.length
|
|
9559
|
-
return
|
|
9806
|
+
this.emit("setContent", { id, contentType, contentLength: buffer.length });
|
|
9807
|
+
return updatedData;
|
|
9560
9808
|
}
|
|
9561
9809
|
/**
|
|
9562
9810
|
* Retrieve binary content associated with a resource
|
|
9563
9811
|
* @param {string} id - Resource ID
|
|
9564
|
-
* @returns {Object} Object with buffer and contentType
|
|
9812
|
+
* @returns {Promise<Object>} Object with buffer and contentType
|
|
9813
|
+
* @example
|
|
9814
|
+
* const content = await resource.content('user-123');
|
|
9815
|
+
* if (content.buffer) {
|
|
9816
|
+
* console.log('Content type:', content.contentType);
|
|
9817
|
+
* console.log('Content size:', content.buffer.length);
|
|
9818
|
+
* // Save to file
|
|
9819
|
+
* fs.writeFileSync('output.jpg', content.buffer);
|
|
9820
|
+
* } else {
|
|
9821
|
+
* console.log('No content found');
|
|
9822
|
+
* }
|
|
9565
9823
|
*/
|
|
9566
9824
|
async content(id) {
|
|
9567
9825
|
const key = this.getResourceKey(id);
|
|
@@ -9656,7 +9914,7 @@ class Resource extends EventEmitter {
|
|
|
9656
9914
|
return;
|
|
9657
9915
|
}
|
|
9658
9916
|
for (const [partitionName, partition] of Object.entries(partitions)) {
|
|
9659
|
-
const partitionKey = this.getPartitionKey(partitionName, data.id, data);
|
|
9917
|
+
const partitionKey = this.getPartitionKey({ partitionName, id: data.id, data });
|
|
9660
9918
|
if (partitionKey) {
|
|
9661
9919
|
const mappedData = await this.schema.mapper(data);
|
|
9662
9920
|
const behaviorImpl = getBehavior(this.behavior);
|
|
@@ -9688,7 +9946,7 @@ class Resource extends EventEmitter {
|
|
|
9688
9946
|
}
|
|
9689
9947
|
const keysToDelete = [];
|
|
9690
9948
|
for (const [partitionName, partition] of Object.entries(partitions)) {
|
|
9691
|
-
const partitionKey = this.getPartitionKey(partitionName, data.id, data);
|
|
9949
|
+
const partitionKey = this.getPartitionKey({ partitionName, id: data.id, data });
|
|
9692
9950
|
if (partitionKey) {
|
|
9693
9951
|
keysToDelete.push(partitionKey);
|
|
9694
9952
|
}
|
|
@@ -9702,20 +9960,72 @@ class Resource extends EventEmitter {
|
|
|
9702
9960
|
}
|
|
9703
9961
|
}
|
|
9704
9962
|
/**
|
|
9705
|
-
* Query
|
|
9706
|
-
* @param {Object} filter - Filter criteria
|
|
9707
|
-
* @
|
|
9963
|
+
* Query resources with simple filtering and pagination
|
|
9964
|
+
* @param {Object} [filter={}] - Filter criteria (exact field matches)
|
|
9965
|
+
* @param {Object} [options] - Query options
|
|
9966
|
+
* @param {number} [options.limit=100] - Maximum number of results
|
|
9967
|
+
* @param {number} [options.offset=0] - Offset for pagination
|
|
9968
|
+
* @param {string} [options.partition] - Partition name to query from
|
|
9969
|
+
* @param {Object} [options.partitionValues] - Partition field values to filter by
|
|
9970
|
+
* @returns {Promise<Object[]>} Array of filtered resource objects
|
|
9971
|
+
* @example
|
|
9972
|
+
* // Query all resources (no filter)
|
|
9973
|
+
* const allUsers = await resource.query();
|
|
9974
|
+
*
|
|
9975
|
+
* // Query with simple filter
|
|
9976
|
+
* const activeUsers = await resource.query({ status: 'active' });
|
|
9977
|
+
*
|
|
9978
|
+
* // Query with multiple filters
|
|
9979
|
+
* const usElectronics = await resource.query({
|
|
9980
|
+
* category: 'electronics',
|
|
9981
|
+
* region: 'US'
|
|
9982
|
+
* });
|
|
9983
|
+
*
|
|
9984
|
+
* // Query with pagination
|
|
9985
|
+
* const firstPage = await resource.query(
|
|
9986
|
+
* { status: 'active' },
|
|
9987
|
+
* { limit: 10, offset: 0 }
|
|
9988
|
+
* );
|
|
9989
|
+
*
|
|
9990
|
+
* // Query within partition
|
|
9991
|
+
* const googleUsers = await resource.query(
|
|
9992
|
+
* { status: 'active' },
|
|
9993
|
+
* {
|
|
9994
|
+
* partition: 'byUtmSource',
|
|
9995
|
+
* partitionValues: { 'utm.source': 'google' },
|
|
9996
|
+
* limit: 5
|
|
9997
|
+
* }
|
|
9998
|
+
* );
|
|
9708
9999
|
*/
|
|
9709
|
-
async query(filter = {}) {
|
|
9710
|
-
const allDocuments = await this.getAll();
|
|
10000
|
+
async query(filter = {}, { limit = 100, offset = 0, partition = null, partitionValues = {} } = {}) {
|
|
9711
10001
|
if (Object.keys(filter).length === 0) {
|
|
9712
|
-
return
|
|
9713
|
-
}
|
|
9714
|
-
|
|
9715
|
-
|
|
9716
|
-
|
|
10002
|
+
return await this.list({ partition, partitionValues, limit, offset });
|
|
10003
|
+
}
|
|
10004
|
+
const results = [];
|
|
10005
|
+
let currentOffset = offset;
|
|
10006
|
+
const batchSize = Math.min(limit, 50);
|
|
10007
|
+
while (results.length < limit) {
|
|
10008
|
+
const batch = await this.list({
|
|
10009
|
+
partition,
|
|
10010
|
+
partitionValues,
|
|
10011
|
+
limit: batchSize,
|
|
10012
|
+
offset: currentOffset
|
|
9717
10013
|
});
|
|
9718
|
-
|
|
10014
|
+
if (batch.length === 0) {
|
|
10015
|
+
break;
|
|
10016
|
+
}
|
|
10017
|
+
const filteredBatch = batch.filter((doc) => {
|
|
10018
|
+
return Object.entries(filter).every(([key, value]) => {
|
|
10019
|
+
return doc[key] === value;
|
|
10020
|
+
});
|
|
10021
|
+
});
|
|
10022
|
+
results.push(...filteredBatch);
|
|
10023
|
+
currentOffset += batchSize;
|
|
10024
|
+
if (batch.length < batchSize) {
|
|
10025
|
+
break;
|
|
10026
|
+
}
|
|
10027
|
+
}
|
|
10028
|
+
return results.slice(0, limit);
|
|
9719
10029
|
}
|
|
9720
10030
|
/**
|
|
9721
10031
|
* Update partition objects to keep them in sync
|
|
@@ -9727,7 +10037,7 @@ class Resource extends EventEmitter {
|
|
|
9727
10037
|
return;
|
|
9728
10038
|
}
|
|
9729
10039
|
for (const [partitionName, partition] of Object.entries(partitions)) {
|
|
9730
|
-
const partitionKey = this.getPartitionKey(partitionName, data.id, data);
|
|
10040
|
+
const partitionKey = this.getPartitionKey({ partitionName, id: data.id, data });
|
|
9731
10041
|
if (partitionKey) {
|
|
9732
10042
|
const mappedData = await this.schema.mapper(data);
|
|
9733
10043
|
const behaviorImpl = getBehavior(this.behavior);
|
|
@@ -9754,13 +10064,30 @@ class Resource extends EventEmitter {
|
|
|
9754
10064
|
}
|
|
9755
10065
|
}
|
|
9756
10066
|
/**
|
|
9757
|
-
* Get object directly from a specific partition
|
|
9758
|
-
* @param {
|
|
9759
|
-
* @param {string}
|
|
9760
|
-
* @param {
|
|
9761
|
-
* @
|
|
10067
|
+
* Get a resource object directly from a specific partition
|
|
10068
|
+
* @param {Object} params - Partition parameters
|
|
10069
|
+
* @param {string} params.id - Resource ID
|
|
10070
|
+
* @param {string} params.partitionName - Name of the partition
|
|
10071
|
+
* @param {Object} params.partitionValues - Values for partition fields
|
|
10072
|
+
* @returns {Promise<Object>} The resource object with partition metadata
|
|
10073
|
+
* @example
|
|
10074
|
+
* // Get user from UTM source partition
|
|
10075
|
+
* const user = await resource.getFromPartition({
|
|
10076
|
+
* id: 'user-123',
|
|
10077
|
+
* partitionName: 'byUtmSource',
|
|
10078
|
+
* partitionValues: { 'utm.source': 'google' }
|
|
10079
|
+
* });
|
|
10080
|
+
* console.log(user._partition); // 'byUtmSource'
|
|
10081
|
+
* console.log(user._partitionValues); // { 'utm.source': 'google' }
|
|
10082
|
+
*
|
|
10083
|
+
* // Get product from multi-field partition
|
|
10084
|
+
* const product = await resource.getFromPartition({
|
|
10085
|
+
* id: 'product-456',
|
|
10086
|
+
* partitionName: 'byCategoryRegion',
|
|
10087
|
+
* partitionValues: { category: 'electronics', region: 'US' }
|
|
10088
|
+
* });
|
|
9762
10089
|
*/
|
|
9763
|
-
async getFromPartition(id, partitionName, partitionValues = {}) {
|
|
10090
|
+
async getFromPartition({ id, partitionName, partitionValues = {} }) {
|
|
9764
10091
|
const partition = this.options.partitions[partitionName];
|
|
9765
10092
|
if (!partition) {
|
|
9766
10093
|
throw new Error(`Partition '${partitionName}' not found`);
|
|
@@ -9819,7 +10146,7 @@ class Database extends EventEmitter {
|
|
|
9819
10146
|
this.version = "1";
|
|
9820
10147
|
this.s3dbVersion = (() => {
|
|
9821
10148
|
try {
|
|
9822
|
-
return true ? "4.1.
|
|
10149
|
+
return true ? "4.1.6" : "latest";
|
|
9823
10150
|
} catch (e) {
|
|
9824
10151
|
return "latest";
|
|
9825
10152
|
}
|