@travetto/model
Advanced tools
Comparing version 5.0.15 to 5.0.16
{ | ||
"name": "@travetto/model", | ||
"version": "5.0.15", | ||
"version": "5.0.16", | ||
"description": "Datastore abstraction for core operations.", | ||
@@ -29,10 +29,10 @@ "keywords": [ | ||
"dependencies": { | ||
"@travetto/config": "^5.0.14", | ||
"@travetto/di": "^5.0.14", | ||
"@travetto/registry": "^5.0.14", | ||
"@travetto/schema": "^5.0.14" | ||
"@travetto/config": "^5.0.15", | ||
"@travetto/di": "^5.0.15", | ||
"@travetto/registry": "^5.0.15", | ||
"@travetto/schema": "^5.0.15" | ||
}, | ||
"peerDependencies": { | ||
"@travetto/cli": "^5.0.17", | ||
"@travetto/test": "^5.0.16" | ||
"@travetto/cli": "^5.0.18", | ||
"@travetto/test": "^5.0.17" | ||
}, | ||
@@ -39,0 +39,0 @@ "peerDependenciesMeta": { |
@@ -145,2 +145,9 @@ import { castTo, Class, Util, asConstructable, AppError } from '@travetto/runtime'; | ||
} | ||
const keys = Object.keys(item); | ||
if ((keys.length === 1 && item.id) || keys.length === 0) { | ||
throw new AppError('No fields to update'); | ||
} else { | ||
item = { ...item }; | ||
delete item.id; | ||
} | ||
const res = await this.prePersist(cls, castTo(item), 'partial'); | ||
@@ -147,0 +154,0 @@ await SchemaValidator.validatePartial(cls, item, view); |
@@ -20,2 +20,4 @@ import { castTo, Class, DeepPartial, TypedObject } from '@travetto/runtime'; | ||
const DEFAULT_SEP = '\u8203'; | ||
/** | ||
@@ -111,3 +113,3 @@ * Utils for working with indexed model services | ||
const { fields, sorted } = this.computeIndexParts(cls, idx, item, { ...(opts ?? {}), includeSortInFields: false }); | ||
const key = fields.map(({ value }) => value).map(x => `${x}`).join(opts?.sep ?? 'Ⲑ'); | ||
const key = fields.map(({ value }) => value).map(x => `${x}`).join(opts?.sep ?? DEFAULT_SEP); | ||
const cfg = typeof idx === 'string' ? ModelRegistry.getIndex(cls, idx) : idx; | ||
@@ -114,0 +116,0 @@ return !sorted ? { type: cfg.type, key } : { type: cfg.type, key, sort: sorted.value }; |
@@ -1,2 +0,2 @@ | ||
import { asConstructable, castTo, Class } from '@travetto/runtime'; | ||
import { AppError, asConstructable, castTo, Class } from '@travetto/runtime'; | ||
import { SchemaRegistry } from '@travetto/schema'; | ||
@@ -28,2 +28,5 @@ | ||
export function Index<T extends ModelType>(...indices: IndexConfig<T>[]) { | ||
if (indices.some(x => x.fields.some(f => f === 'id'))) { | ||
throw new AppError('Cannot create an index with the id field'); | ||
} | ||
return function (target: Class<T>): void { | ||
@@ -75,3 +78,2 @@ ModelRegistry.getOrCreatePending(target).indices!.push(...indices); | ||
/** | ||
@@ -78,0 +80,0 @@ * Model class decorator for post-load behavior |
@@ -5,3 +5,3 @@ import { SchemaRegistry } from '@travetto/schema'; | ||
import { AppError, castTo, Class, describeFunction, asFull } from '@travetto/runtime'; | ||
import { AllViewⲐ } from '@travetto/schema/src/internal/types'; | ||
import { AllViewSymbol } from '@travetto/schema/src/internal/types'; | ||
@@ -78,3 +78,3 @@ import { IndexConfig, IndexType, ModelOptions } from './types'; | ||
const schema = SchemaRegistry.get(cls); | ||
const view = schema.views[AllViewⲐ].schema; | ||
const view = schema.views[AllViewSymbol].schema; | ||
delete view.id.required; // Allow ids to be optional | ||
@@ -81,0 +81,0 @@ |
@@ -47,3 +47,2 @@ import assert from 'node:assert'; | ||
@Test('create, read, delete') | ||
@@ -50,0 +49,0 @@ async createRaw() { |
@@ -122,3 +122,2 @@ import assert from 'node:assert'; | ||
@Test() | ||
@@ -138,3 +137,2 @@ async writeAndGet() { | ||
@Test() | ||
@@ -158,3 +156,2 @@ async metadataUpdate() { | ||
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions | ||
@@ -165,3 +162,2 @@ @Test({ skip: (x: unknown) => !(x as ModelBlobSuite).serviceClass.prototype.getBlobWriteUrl }) | ||
const buffer = Buffer.alloc(1.5 * 10000); | ||
@@ -168,0 +164,0 @@ for (let i = 0; i < buffer.length; i++) { |
@@ -37,3 +37,2 @@ import assert from 'node:assert'; | ||
@Model() | ||
@@ -61,2 +60,4 @@ class SimpleList { | ||
value?: string; | ||
@PersistValue(v => v ?? new Date(), 'full') | ||
@@ -159,3 +160,3 @@ @Required(false) | ||
const service = await this.service; | ||
await assert.rejects(() => service.updatePartial(User2, { id: '-1' }), NotFoundError); | ||
await assert.rejects(() => service.updatePartial(User2, { id: '-1', name: 'bob' }), NotFoundError); | ||
} | ||
@@ -233,3 +234,3 @@ | ||
const final = await service.updatePartial(Dated, { id: res.id }); | ||
const final = await service.updatePartial(Dated, { id: res.id, value: 'random' }); | ||
assert(final.createdDate instanceof Date); | ||
@@ -241,3 +242,2 @@ assert(final.createdDate.getTime() === created?.getTime()); | ||
@Test('verify list') | ||
@@ -290,3 +290,2 @@ async list() { | ||
@Test('Verify update') | ||
@@ -293,0 +292,0 @@ async testRawUpdate() { |
@@ -108,3 +108,3 @@ import assert from 'node:assert'; | ||
Array(10).fill(0).map((x, i) => service.upsert(ExpiryUser, ExpiryUser.from({ | ||
expiresAt: this.timeFromNow(1000 + i * this.delayFactor) | ||
expiresAt: this.timeFromNow(300 + i * this.delayFactor) | ||
}))) | ||
@@ -120,3 +120,3 @@ ); | ||
// Let expire | ||
await this.wait(1100); | ||
await this.wait(400); | ||
@@ -123,0 +123,0 @@ total = await this.getSize(ExpiryUser); |
@@ -114,3 +114,2 @@ import assert from 'node:assert'; | ||
@Test() | ||
@@ -117,0 +116,0 @@ async queryList() { |
@@ -54,19 +54,11 @@ import { Class } from '@travetto/runtime'; | ||
if (isStorageSupported(service)) { | ||
if (service.truncateModel || service.deleteModel) { | ||
for (const m of ModelRegistry.getClasses()) { | ||
if (m === ModelRegistry.getBaseModel(m)) { | ||
if (service.truncateModel) { | ||
await service.truncateModel(m); | ||
} else if (service.deleteModel) { | ||
await service.deleteModel(m); | ||
} | ||
} | ||
} | ||
if (isBlobSupported(service)) { | ||
if (service.truncateModel) { | ||
await service.truncateModel(MODEL_BLOB); | ||
} else if (service.deleteModel) { | ||
await service.deleteModel(MODEL_BLOB); | ||
} | ||
} | ||
const models = ModelRegistry.getClasses().filter(m => m === ModelRegistry.getBaseModel(m)); | ||
if (isBlobSupported(service)) { | ||
models.push(MODEL_BLOB); | ||
} | ||
if (service.truncateModel) { | ||
await Promise.all(models.map(x => service.truncateModel!(x))); | ||
} else if (service.deleteModel) { | ||
await Promise.all(models.map(x => service.deleteModel!(x))); | ||
} else { | ||
@@ -73,0 +65,0 @@ await service.deleteStorage(); // Purge it all |
160086
2503
Updated@travetto/config@^5.0.15
Updated@travetto/di@^5.0.15
Updated@travetto/registry@^5.0.15
Updated@travetto/schema@^5.0.15