diff --git a/ts/backend/index.tests.ts b/ts/backend/index.tests.ts index adb3e7d..79741da 100644 --- a/ts/backend/index.tests.ts +++ b/ts/backend/index.tests.ts @@ -35,7 +35,10 @@ export class FakeStorageBackend extends StorageBackend { const id = this.config.idGenerator(collection, object, options) this.createOperations.push({ object, id }) - return { object: { ...object, [pkIndex]: id } } + return { + pk: id, + object: options.incObject ? { ...object, [pkIndex]: id } : undefined, + } } async findObjects() { @@ -43,11 +46,11 @@ export class FakeStorageBackend extends StorageBackend { } async updateObjects() { - + return { count: 1 } } async deleteObjects() { - + return { count: 1 } } } diff --git a/ts/types/backend.ts b/ts/types/backend.ts index 50b3a54..2649d11 100644 --- a/ts/types/backend.ts +++ b/ts/types/backend.ts @@ -2,17 +2,17 @@ import StorageRegistry from "../registry" import { StorageBackendFeatureSupport } from "./backend-features"; import { isRelationshipReference } from "./relationships"; -export type CreateSingleOptions = DBNameOptions -export type CreateSingleResult = {object? : any} +export type CreateSingleOptions = DBNameOptions & {incObject? : boolean} +export type CreateSingleResult = {pk : PK, object? : T} export type FindSingleOptions = DBNameOptions & IgnoreCaseOptions & ReverseOptions & {fields?: string[]} export type FindManyOptions = FindSingleOptions & PaginationOptions & SortingOptions export type CountOptions = DBNameOptions & IgnoreCaseOptions -export type UpdateManyOptions = DBNameOptions -export type UpdateManyResult = any export type UpdateSingleOptions = DBNameOptions -export type UpdateSingleResult = any +export type UpdateSingleResult = {count? : number} +export type UpdateManyOptions = DBNameOptions +export type UpdateManyResult = UpdateSingleResult export type DeleteSingleOptions = DBNameOptions -export type DeleteSingleResult = any +export type DeleteSingleResult = {count? : number} export type DeleteManyOptions = DBNameOptions & {limit? : number} export type DeleteManyResult = any export type OperationBatch = Array @@ -78,7 +78,11 @@ export abstract class StorageBackend { async cleanup() : Promise {} async migrate({database} : {database?} = {}) : Promise {} - abstract async createObject(collection : string, object, options? : CreateSingleOptions) + abstract async createObject( + collection : string, + object : T, + options? : CreateSingleOptions, + ) : Promise> abstract findObjects(collection : string, query, options? : FindManyOptions) : Promise> async findObject(collection : string, query, options? : FindSingleOptions) : Promise { @@ -124,7 +128,12 @@ export abstract class StorageBackend { async deleteObject(collection : string, object, options? : DeleteSingleOptions) : Promise { const definition = this.registry.collections[collection] if (typeof definition.pkIndex === 'string') { - await this.deleteObjects(collection, {[definition.pkIndex]: object[definition.pkIndex]}, {...(options || {}), limit: 1}) + return this.deleteObjects(collection, { + [definition.pkIndex]: object[definition.pkIndex], + }, { + ...(options || {}), + limit: 1, + }) } else { throw new Error('Updating single objects with compound pks is not supported yet') } @@ -177,4 +186,4 @@ export function _validateOperationRegistration(identifier, backend : StorageBack } return true -} \ No newline at end of file +} diff --git a/ts/types/manager.ts b/ts/types/manager.ts index e8c1b1e..8d8cf28 100644 --- a/ts/types/manager.ts +++ b/ts/types/manager.ts @@ -17,7 +17,7 @@ import { } from './backend' export interface StorageCollection { - createObject(object, options?: CreateSingleOptions): Promise + createObject(object: T, options?: CreateSingleOptions): Promise> findOneObject(query, options?: FindSingleOptions): Promise findObject(query, options?: FindSingleOptions): Promise findObjects(query, options?: FindManyOptions): Promise>