Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions ts/backend/index.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,22 @@ 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() {
return []
}

async updateObjects() {

return { count: 1 }
}

async deleteObjects() {

return { count: 1 }
}
}

27 changes: 18 additions & 9 deletions ts/types/backend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, T> = {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<CreateObjectBatchOperation | UpdateObjectsBatchOperation | DeleteObjectsBatchOperation>
Expand Down Expand Up @@ -78,7 +78,11 @@ export abstract class StorageBackend {
async cleanup() : Promise<any> {}
async migrate({database} : {database?} = {}) : Promise<any> {}

abstract async createObject(collection : string, object, options? : CreateSingleOptions)
abstract async createObject<PK=string, T=any>(
collection : string,
object : T,
options? : CreateSingleOptions,
) : Promise<CreateSingleResult<PK, T>>

abstract findObjects<T>(collection : string, query, options? : FindManyOptions) : Promise<Array<T>>
async findObject<T>(collection : string, query, options? : FindSingleOptions) : Promise<T | null> {
Expand Down Expand Up @@ -124,7 +128,12 @@ export abstract class StorageBackend {
async deleteObject(collection : string, object, options? : DeleteSingleOptions) : Promise<DeleteSingleResult> {
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')
}
Expand Down Expand Up @@ -177,4 +186,4 @@ export function _validateOperationRegistration(identifier, backend : StorageBack
}

return true
}
}
2 changes: 1 addition & 1 deletion ts/types/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
} from './backend'

export interface StorageCollection {
createObject(object, options?: CreateSingleOptions): Promise<CreateSingleResult>
createObject<PK=string, T=any>(object: T, options?: CreateSingleOptions): Promise<CreateSingleResult<PK, T>>
findOneObject<T>(query, options?: FindSingleOptions): Promise<T | null>
findObject<T>(query, options?: FindSingleOptions): Promise<T | null>
findObjects<T>(query, options?: FindManyOptions): Promise<Array<T>>
Expand Down