s3db.js 3.3.0 → 3.3.2
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 +26 -3
- package/dist/s3db.cjs.min.js +4 -4
- package/dist/s3db.es.js +26 -3
- package/dist/s3db.es.min.js +3 -3
- package/dist/s3db.iife.js +26 -3
- package/dist/s3db.iife.min.js +4 -4
- package/package.json +11 -11
package/dist/s3db.cjs.js
CHANGED
|
@@ -1624,10 +1624,19 @@ class Resource extends EventEmitter {
|
|
|
1624
1624
|
super();
|
|
1625
1625
|
this.name = name;
|
|
1626
1626
|
this.client = client;
|
|
1627
|
-
this.options = options;
|
|
1628
1627
|
this.observers = observers;
|
|
1629
1628
|
this.parallelism = parallelism;
|
|
1630
1629
|
this.passphrase = passphrase ?? "secret";
|
|
1630
|
+
this.options = {
|
|
1631
|
+
cache: false,
|
|
1632
|
+
autoDecrypt: true,
|
|
1633
|
+
timestamps: false,
|
|
1634
|
+
...options
|
|
1635
|
+
};
|
|
1636
|
+
if (options.timestamps) {
|
|
1637
|
+
attributes.createdAt = "string|optional";
|
|
1638
|
+
attributes.updatedAt = "string|optional";
|
|
1639
|
+
}
|
|
1631
1640
|
this.schema = new Schema({
|
|
1632
1641
|
name,
|
|
1633
1642
|
attributes,
|
|
@@ -1653,6 +1662,10 @@ class Resource extends EventEmitter {
|
|
|
1653
1662
|
return result;
|
|
1654
1663
|
}
|
|
1655
1664
|
async insert({ id, ...attributes }) {
|
|
1665
|
+
if (this.options.timestamps) {
|
|
1666
|
+
attributes.createdAt = (/* @__PURE__ */ new Date()).toISOString();
|
|
1667
|
+
attributes.updatedAt = (/* @__PURE__ */ new Date()).toISOString();
|
|
1668
|
+
}
|
|
1656
1669
|
const {
|
|
1657
1670
|
errors,
|
|
1658
1671
|
isValid,
|
|
@@ -1683,7 +1696,8 @@ class Resource extends EventEmitter {
|
|
|
1683
1696
|
let data = await this.schema.unmapper(request.Metadata);
|
|
1684
1697
|
data.id = id;
|
|
1685
1698
|
data._length = request.ContentLength;
|
|
1686
|
-
data.
|
|
1699
|
+
data._lastModified = request.LastModified;
|
|
1700
|
+
if (request.VersionId) data._versionId = request.VersionId;
|
|
1687
1701
|
if (request.Expiration) data._expiresAt = request.Expiration;
|
|
1688
1702
|
this.emit("get", data);
|
|
1689
1703
|
return data;
|
|
@@ -1700,6 +1714,9 @@ class Resource extends EventEmitter {
|
|
|
1700
1714
|
}
|
|
1701
1715
|
async update(id, attributes) {
|
|
1702
1716
|
const live = await this.get(id);
|
|
1717
|
+
if (this.options.timestamps) {
|
|
1718
|
+
attributes.updatedAt = (/* @__PURE__ */ new Date()).toISOString();
|
|
1719
|
+
}
|
|
1703
1720
|
const attrs = lodashEs.merge(live, attributes);
|
|
1704
1721
|
delete attrs.id;
|
|
1705
1722
|
const { isValid, errors, data: validated } = await this.validate(attrs);
|
|
@@ -1726,6 +1743,13 @@ class Resource extends EventEmitter {
|
|
|
1726
1743
|
this.emit("delete", id);
|
|
1727
1744
|
return response;
|
|
1728
1745
|
}
|
|
1746
|
+
async upsert({ id, ...attributes }) {
|
|
1747
|
+
const exists = await this.exists(id);
|
|
1748
|
+
if (exists) {
|
|
1749
|
+
return this.update(id, attributes);
|
|
1750
|
+
}
|
|
1751
|
+
return this.insert({ id, ...attributes });
|
|
1752
|
+
}
|
|
1729
1753
|
async count() {
|
|
1730
1754
|
const count = await this.client.count({
|
|
1731
1755
|
prefix: `resource=${this.name}`
|
|
@@ -1917,7 +1941,6 @@ class Database extends EventEmitter {
|
|
|
1917
1941
|
observers: [this],
|
|
1918
1942
|
client: this.client,
|
|
1919
1943
|
options: {
|
|
1920
|
-
autoDecrypt: true,
|
|
1921
1944
|
cache: this.cache,
|
|
1922
1945
|
...options
|
|
1923
1946
|
}
|