s3db.js 3.3.0 → 3.3.1
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.es.js
CHANGED
|
@@ -1622,10 +1622,19 @@ class Resource extends EventEmitter {
|
|
|
1622
1622
|
super();
|
|
1623
1623
|
this.name = name;
|
|
1624
1624
|
this.client = client;
|
|
1625
|
-
this.options = options;
|
|
1626
1625
|
this.observers = observers;
|
|
1627
1626
|
this.parallelism = parallelism;
|
|
1628
1627
|
this.passphrase = passphrase ?? "secret";
|
|
1628
|
+
this.options = {
|
|
1629
|
+
cache: false,
|
|
1630
|
+
autoDecrypt: true,
|
|
1631
|
+
timestamps: false,
|
|
1632
|
+
...options
|
|
1633
|
+
};
|
|
1634
|
+
if (options.timestamps) {
|
|
1635
|
+
attributes.createdAt = "string";
|
|
1636
|
+
attributes.updatedAt = "string";
|
|
1637
|
+
}
|
|
1629
1638
|
this.schema = new Schema({
|
|
1630
1639
|
name,
|
|
1631
1640
|
attributes,
|
|
@@ -1651,6 +1660,10 @@ class Resource extends EventEmitter {
|
|
|
1651
1660
|
return result;
|
|
1652
1661
|
}
|
|
1653
1662
|
async insert({ id, ...attributes }) {
|
|
1663
|
+
if (this.options.timestamps) {
|
|
1664
|
+
attributes.createdAt = (/* @__PURE__ */ new Date()).toISOString();
|
|
1665
|
+
attributes.updatedAt = (/* @__PURE__ */ new Date()).toISOString();
|
|
1666
|
+
}
|
|
1654
1667
|
const {
|
|
1655
1668
|
errors,
|
|
1656
1669
|
isValid,
|
|
@@ -1681,7 +1694,8 @@ class Resource extends EventEmitter {
|
|
|
1681
1694
|
let data = await this.schema.unmapper(request.Metadata);
|
|
1682
1695
|
data.id = id;
|
|
1683
1696
|
data._length = request.ContentLength;
|
|
1684
|
-
data.
|
|
1697
|
+
data._lastModified = request.LastModified;
|
|
1698
|
+
if (request.VersionId) data._versionId = request.VersionId;
|
|
1685
1699
|
if (request.Expiration) data._expiresAt = request.Expiration;
|
|
1686
1700
|
this.emit("get", data);
|
|
1687
1701
|
return data;
|
|
@@ -1698,6 +1712,9 @@ class Resource extends EventEmitter {
|
|
|
1698
1712
|
}
|
|
1699
1713
|
async update(id, attributes) {
|
|
1700
1714
|
const live = await this.get(id);
|
|
1715
|
+
if (this.options.timestamps) {
|
|
1716
|
+
attributes.updatedAt = (/* @__PURE__ */ new Date()).toISOString();
|
|
1717
|
+
}
|
|
1701
1718
|
const attrs = merge(live, attributes);
|
|
1702
1719
|
delete attrs.id;
|
|
1703
1720
|
const { isValid, errors, data: validated } = await this.validate(attrs);
|
|
@@ -1724,6 +1741,13 @@ class Resource extends EventEmitter {
|
|
|
1724
1741
|
this.emit("delete", id);
|
|
1725
1742
|
return response;
|
|
1726
1743
|
}
|
|
1744
|
+
async upsert({ id, ...attributes }) {
|
|
1745
|
+
const exists = await this.exists(id);
|
|
1746
|
+
if (exists) {
|
|
1747
|
+
return this.update(id, attributes);
|
|
1748
|
+
}
|
|
1749
|
+
return this.insert({ id, ...attributes });
|
|
1750
|
+
}
|
|
1727
1751
|
async count() {
|
|
1728
1752
|
const count = await this.client.count({
|
|
1729
1753
|
prefix: `resource=${this.name}`
|
|
@@ -1915,7 +1939,6 @@ class Database extends EventEmitter {
|
|
|
1915
1939
|
observers: [this],
|
|
1916
1940
|
client: this.client,
|
|
1917
1941
|
options: {
|
|
1918
|
-
autoDecrypt: true,
|
|
1919
1942
|
cache: this.cache,
|
|
1920
1943
|
...options
|
|
1921
1944
|
}
|