@stemy/backend 5.0.9 → 5.1.0

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.
@@ -614,8 +614,14 @@ function gunzipPromised(data, opts) {
614
614
  });
615
615
  });
616
616
  }
617
- function deleteFromBucket(bucket, fileId) {
617
+ function deleteFromBucket(bucket, id) {
618
+ const fileId = id instanceof ObjectId ? id : new ObjectId(id);
618
619
  return new Promise(((resolve, reject) => {
620
+ if (!id) {
621
+ // We don't care about empty id
622
+ resolve(null);
623
+ return;
624
+ }
619
625
  bucket.delete(fileId, error => {
620
626
  let err = error;
621
627
  if (error) {
@@ -973,6 +979,8 @@ class BaseEntity {
973
979
  const ret = Object.assign({}, this.data);
974
980
  delete ret._id;
975
981
  ret.id = this.id;
982
+ ret.updatedAt = new Date();
983
+ ret.createdAt = ret.createdAt || ret.updatedAt;
976
984
  return ret;
977
985
  }
978
986
  }
@@ -997,6 +1005,10 @@ class Asset extends BaseEntity {
997
1005
  async unlink() {
998
1006
  return deleteFromBucket(this.bucket, this.mId);
999
1007
  }
1008
+ async setMeta(metadata) {
1009
+ metadata = Object.assign(this.metadata, metadata || {});
1010
+ await this.collection.updateOne({ _id: this.mId }, { $set: { metadata } });
1011
+ }
1000
1012
  getBuffer() {
1001
1013
  return streamToBuffer(this.stream);
1002
1014
  }
@@ -1032,6 +1044,9 @@ class TempAsset {
1032
1044
  async unlink() {
1033
1045
  throw new Error(`Temp asset '${this.id}' can not be removed!`);
1034
1046
  }
1047
+ async setMeta(meta) {
1048
+ Object.assign(this.metadata, meta || {});
1049
+ }
1035
1050
  async getBuffer() {
1036
1051
  return this.buffer;
1037
1052
  }
@@ -1210,6 +1225,12 @@ class LazyAsset extends BaseEntity {
1210
1225
  get jobQue() {
1211
1226
  return this.data.jobQue;
1212
1227
  }
1228
+ get createdAt() {
1229
+ return this.data.createdAt;
1230
+ }
1231
+ get updatedAt() {
1232
+ return this.data.updatedAt;
1233
+ }
1213
1234
  get progressId() {
1214
1235
  return this.data.progressId;
1215
1236
  }
@@ -1252,14 +1273,19 @@ class LazyAsset extends BaseEntity {
1252
1273
  return this.loadAsset();
1253
1274
  }
1254
1275
  async writeAsset(asset) {
1276
+ this.data.updatedAt = new Date();
1255
1277
  this.data.assetId = asset.id;
1278
+ await asset.setMeta({ lazyId: this.id });
1256
1279
  await this.save();
1257
1280
  return asset;
1258
1281
  }
1259
1282
  async startWorkingOnAsset(fromLoad) {
1283
+ const oldAsset = this.data.assetId;
1284
+ this.data.updatedAt = new Date();
1260
1285
  this.data.progressId = (await this.progresses.create()).id;
1261
1286
  this.data.assetId = null;
1262
1287
  await this.save();
1288
+ await deleteFromBucket(this.assets.bucket, oldAsset);
1263
1289
  const jobParams = JSON.parse(await gunzipPromised(this.data.jobParams));
1264
1290
  await this.progresses.jobMan.enqueueWithName(this.data.jobName, { ...jobParams, lazyId: this.id, fromLoad });
1265
1291
  }
@@ -1729,11 +1755,13 @@ let LazyAssets = class LazyAssets {
1729
1755
  const data = {
1730
1756
  jobName,
1731
1757
  jobParams,
1732
- jobQue
1758
+ jobQue,
1733
1759
  };
1734
1760
  const existingAsset = await this.find(data);
1735
1761
  if (existingAsset)
1736
1762
  return existingAsset;
1763
+ data.createdAt = new Date();
1764
+ data.updatedAt = data.createdAt;
1737
1765
  const res = await this.collection.insertOne(data);
1738
1766
  return new LazyAsset(res.insertedId, data, this.collection, this.logger, this.assets, this.progresses);
1739
1767
  }
@@ -1944,20 +1972,22 @@ let Fixtures = class Fixtures {
1944
1972
  constructor(container) {
1945
1973
  this.container = container;
1946
1974
  }
1947
- init() {
1975
+ init(output) {
1948
1976
  try {
1949
1977
  return this.container.resolveAll(FIXTURE);
1950
1978
  }
1951
1979
  catch (e) {
1980
+ output.writeln(e.message);
1952
1981
  return [];
1953
1982
  }
1954
1983
  }
1955
1984
  async load(output) {
1956
- this.fixtures = this.fixtures || this.init();
1957
1985
  output = output || {
1958
1986
  write: console.log,
1959
1987
  writeln: t => console.log(t + "\n")
1960
1988
  };
1989
+ this.fixtures = this.fixtures || this.init(output);
1990
+ output.write(`Loading fixtures: ${this.fixtures.length} items`);
1961
1991
  for (let fixture of this.fixtures) {
1962
1992
  await fixture.load(output);
1963
1993
  }
@@ -1966,7 +1996,7 @@ let Fixtures = class Fixtures {
1966
1996
  Fixtures = __decorate([
1967
1997
  injectable(),
1968
1998
  scoped(Lifecycle.ContainerScoped),
1969
- __param(0, injectAll(DI_CONTAINER)),
1999
+ __param(0, inject(DI_CONTAINER)),
1970
2000
  __metadata("design:paramtypes", [Object])
1971
2001
  ], Fixtures);
1972
2002
 
@@ -3791,6 +3821,28 @@ const commands = [
3791
3821
  FixturesCommand
3792
3822
  ];
3793
3823
 
3824
+ let TtlFixture = class TtlFixture {
3825
+ constructor(connector) {
3826
+ this.connector = connector;
3827
+ }
3828
+ async load() {
3829
+ const db = this.connector.database;
3830
+ if (!db)
3831
+ return null;
3832
+ const expires = { expireAfterSeconds: 3600 * 24 };
3833
+ await db.collection("progresses").createIndex({ updatedAt: 1 }, expires);
3834
+ await db.collection("lazyassets").createIndex({ updatedAt: 1 }, expires);
3835
+ }
3836
+ };
3837
+ TtlFixture = __decorate([
3838
+ injectable(),
3839
+ __metadata("design:paramtypes", [MongoConnector])
3840
+ ], TtlFixture);
3841
+
3842
+ const fixtures = [
3843
+ TtlFixture,
3844
+ ];
3845
+
3794
3846
  class BaseDoc {
3795
3847
  /**
3796
3848
  * Casts this to DocumentType<this> to allow using document methods in get/set-s
@@ -4222,8 +4274,7 @@ async function setupBackend(config, providers, parent) {
4222
4274
  providers = Array.isArray(providers) ? providers : [];
4223
4275
  parent = parent || createServices();
4224
4276
  // Create fixtures
4225
- const fixtureTypes = (config.fixtures || []);
4226
- const fixtureProviders = fixtureTypes.map(fixture => {
4277
+ const fixtureProviders = fixtures.concat(config.fixtures || []).map(fixture => {
4227
4278
  return {
4228
4279
  provide: FIXTURE,
4229
4280
  useClass: fixture
@@ -4291,7 +4342,7 @@ async function setupBackend(config, providers, parent) {
4291
4342
  });
4292
4343
  });
4293
4344
  // Add other providers
4294
- allProviders.push(...fixtureTypes, ...fixtureProviders, ...paramProviders, ...jobProviders, ...commandProviders, ...restOptions.middlewares, ...restOptions.controllers, ...socketOptions.middlewares, ...socketOptions.controllers, ...providers, {
4345
+ allProviders.push(...fixtureProviders, ...paramProviders, ...jobProviders, ...commandProviders, ...restOptions.middlewares, ...restOptions.controllers, ...socketOptions.middlewares, ...socketOptions.controllers, ...providers, {
4295
4346
  provide: EXPRESS,
4296
4347
  useFactory: (container) => {
4297
4348
  return container.resolve(BackendProvider).express;