@travetto/model
Advanced tools
Comparing version 5.0.14 to 5.0.15
{ | ||
"name": "@travetto/model", | ||
"version": "5.0.14", | ||
"version": "5.0.15", | ||
"description": "Datastore abstraction for core operations.", | ||
@@ -29,10 +29,10 @@ "keywords": [ | ||
"dependencies": { | ||
"@travetto/config": "^5.0.13", | ||
"@travetto/di": "^5.0.13", | ||
"@travetto/registry": "^5.0.13", | ||
"@travetto/schema": "^5.0.13" | ||
"@travetto/config": "^5.0.14", | ||
"@travetto/di": "^5.0.14", | ||
"@travetto/registry": "^5.0.14", | ||
"@travetto/schema": "^5.0.14" | ||
}, | ||
"peerDependencies": { | ||
"@travetto/cli": "^5.0.16", | ||
"@travetto/test": "^5.0.15" | ||
"@travetto/cli": "^5.0.17", | ||
"@travetto/test": "^5.0.16" | ||
}, | ||
@@ -39,0 +39,0 @@ "peerDependenciesMeta": { |
@@ -7,5 +7,5 @@ import { Class, AppError } from '@travetto/runtime'; | ||
export class NotFoundError extends AppError { | ||
constructor(cls: Class | string, id: string, details?: Record<string, unknown>) { | ||
constructor(cls: Class | string, id: string, details: Record<string, unknown> = {}) { | ||
super(`${typeof cls === 'string' ? cls : cls.name} with id ${id} not found`, { category: 'notfound', details }); | ||
} | ||
} |
@@ -6,3 +6,2 @@ import { ShutdownManager, Class, TimeSpan, TimeUtil, Util, castTo } from '@travetto/runtime'; | ||
import { ModelType } from '../../types/model'; | ||
import { NotFoundError } from '../../error/not-found'; | ||
@@ -48,20 +47,2 @@ /** | ||
} | ||
/** | ||
* Simple cull operation for a given model type | ||
* @param svc | ||
*/ | ||
static async naiveDeleteExpired<T extends ModelType>(svc: ModelExpirySupport, cls: Class<T>, suppressErrors = false): Promise<number> { | ||
const deleting = []; | ||
for await (const el of svc.list(cls)) { | ||
if (this.getExpiryState(cls, el).expired) { | ||
deleting.push(svc.delete(cls, el.id).catch(err => { | ||
if (!suppressErrors && !(err instanceof NotFoundError)) { | ||
throw err; | ||
} | ||
})); | ||
} | ||
} | ||
return (await Promise.all(deleting)).length; | ||
} | ||
} |
@@ -14,6 +14,7 @@ import assert from 'node:assert'; | ||
@Model('expiry-user') | ||
class User { | ||
export class ExpiryUser { | ||
id: string; | ||
@ExpiresAt() | ||
expiresAt?: Date; | ||
payload?: string; | ||
} | ||
@@ -37,8 +38,8 @@ | ||
const service = await this.service; | ||
const res = await service.upsert(User, User.from({ | ||
const res = await service.upsert(ExpiryUser, ExpiryUser.from({ | ||
expiresAt: this.timeFromNow('2s') | ||
})); | ||
assert(res instanceof User); | ||
assert(res instanceof ExpiryUser); | ||
const expiry = ModelExpiryUtil.getExpiryState(User, await service.get(User, res.id)); | ||
const expiry = ModelExpiryUtil.getExpiryState(ExpiryUser, await service.get(ExpiryUser, res.id)); | ||
assert(!expiry.expired); | ||
@@ -50,11 +51,11 @@ } | ||
const service = await this.service; | ||
const res = await service.upsert(User, User.from({ | ||
const res = await service.upsert(ExpiryUser, ExpiryUser.from({ | ||
expiresAt: this.timeFromNow(100) | ||
})); | ||
assert(res instanceof User); | ||
assert(res instanceof ExpiryUser); | ||
await this.wait(200); | ||
await assert.rejects(() => service.get(User, res.id), NotFoundError); | ||
await assert.rejects(() => service.get(ExpiryUser, res.id), NotFoundError); | ||
} | ||
@@ -65,11 +66,11 @@ | ||
const service = await this.service; | ||
const res = await service.upsert(User, User.from({ | ||
const res = await service.upsert(ExpiryUser, ExpiryUser.from({ | ||
expiresAt: this.timeFromNow(100) | ||
})); | ||
assert(res instanceof User); | ||
assert(res instanceof ExpiryUser); | ||
await this.wait(200); | ||
await assert.rejects(() => service.update(User, User.from({ id: res.id })), NotFoundError); | ||
await assert.rejects(() => service.update(ExpiryUser, ExpiryUser.from({ id: res.id })), NotFoundError); | ||
} | ||
@@ -80,12 +81,12 @@ | ||
const service = await this.service; | ||
const res = await service.upsert(User, User.from({ | ||
const res = await service.upsert(ExpiryUser, ExpiryUser.from({ | ||
expiresAt: this.timeFromNow('2s') | ||
})); | ||
assert(res instanceof User); | ||
assert(res instanceof ExpiryUser); | ||
await this.wait(50); | ||
assert(!ModelExpiryUtil.getExpiryState(User, (await service.get(User, res.id))).expired); | ||
assert(!ModelExpiryUtil.getExpiryState(ExpiryUser, (await service.get(ExpiryUser, res.id))).expired); | ||
await service.updatePartial(User, { | ||
await service.updatePartial(ExpiryUser, { | ||
id: res.id, | ||
@@ -97,3 +98,3 @@ expiresAt: this.timeFromNow(100) | ||
await assert.rejects(() => service.get(User, res.id), NotFoundError); | ||
await assert.rejects(() => service.get(ExpiryUser, res.id), NotFoundError); | ||
} | ||
@@ -107,3 +108,3 @@ | ||
total = await this.getSize(User); | ||
total = await this.getSize(ExpiryUser); | ||
assert(total === 0); | ||
@@ -113,3 +114,3 @@ | ||
await Promise.all( | ||
Array(10).fill(0).map((x, i) => service.upsert(User, User.from({ | ||
Array(10).fill(0).map((x, i) => service.upsert(ExpiryUser, ExpiryUser.from({ | ||
expiresAt: this.timeFromNow(1000 + i * this.delayFactor) | ||
@@ -122,3 +123,3 @@ }))) | ||
total = await this.getSize(User); | ||
total = await this.getSize(ExpiryUser); | ||
assert(total === 10); | ||
@@ -129,8 +130,8 @@ | ||
total = await this.getSize(User); | ||
total = await this.getSize(ExpiryUser); | ||
assert(total === 0); | ||
total = await this.getSize(User); | ||
total = await this.getSize(ExpiryUser); | ||
assert(total === 0); | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
159920
2500
+ Added@types/node@22.10.7(transitive)
- Removed@types/node@22.10.9(transitive)
Updated@travetto/config@^5.0.14
Updated@travetto/di@^5.0.14
Updated@travetto/registry@^5.0.14
Updated@travetto/schema@^5.0.14