Skip to content

Commit cf0847c

Browse files
committed
Better define main CRUD ops' return types
Based on what we discussed this a while back on slack: https://worldbrain.slack.com/archives/CBLN20LCU/p1545279785004100 I think it's probably better to decide on these return types early on. Not sure exactly about the choices; what do you think?
1 parent b253736 commit cf0847c

3 files changed

Lines changed: 25 additions & 13 deletions

File tree

ts/backend/index.tests.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,22 @@ export class FakeStorageBackend extends StorageBackend {
3535

3636
const id = this.config.idGenerator(collection, object, options)
3737
this.createOperations.push({ object, id })
38-
return { object: { ...object, [pkIndex]: id } }
38+
return {
39+
pk: id,
40+
object: options.incObject ? { ...object, [pkIndex]: id } : undefined,
41+
}
3942
}
4043

4144
async findObjects() {
4245
return []
4346
}
4447

4548
async updateObjects() {
46-
49+
return { count: 1 }
4750
}
4851

4952
async deleteObjects() {
50-
53+
return { count: 1 }
5154
}
5255
}
5356

ts/types/backend.ts

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@ import StorageRegistry from "../registry"
22
import { StorageBackendFeatureSupport } from "./backend-features";
33
import { isRelationshipReference } from "./relationships";
44

5-
export type CreateSingleOptions = DBNameOptions
6-
export type CreateSingleResult = {object? : any}
5+
export type CreateSingleOptions = DBNameOptions & {incObject? : boolean}
6+
export type CreateSingleResult<PK, T> = {pk : PK, object? : T}
77
export type FindSingleOptions = DBNameOptions & IgnoreCaseOptions & ReverseOptions & {fields?: string[]}
88
export type FindManyOptions = FindSingleOptions & PaginationOptions & SortingOptions
99
export type CountOptions = DBNameOptions & IgnoreCaseOptions
10-
export type UpdateManyOptions = DBNameOptions
11-
export type UpdateManyResult = any
1210
export type UpdateSingleOptions = DBNameOptions
13-
export type UpdateSingleResult = any
11+
export type UpdateSingleResult = {count? : number}
12+
export type UpdateManyOptions = DBNameOptions
13+
export type UpdateManyResult = UpdateSingleResult
1414
export type DeleteSingleOptions = DBNameOptions
15-
export type DeleteSingleResult = any
15+
export type DeleteSingleResult = {count? : number}
1616
export type DeleteManyOptions = DBNameOptions & {limit? : number}
1717
export type DeleteManyResult = any
1818
export type OperationBatch = Array<CreateObjectBatchOperation | UpdateObjectsBatchOperation | DeleteObjectsBatchOperation>
@@ -78,7 +78,11 @@ export abstract class StorageBackend {
7878
async cleanup() : Promise<any> {}
7979
async migrate({database} : {database?} = {}) : Promise<any> {}
8080

81-
abstract async createObject(collection : string, object, options? : CreateSingleOptions)
81+
abstract async createObject<PK=string, T=any>(
82+
collection : string,
83+
object : T,
84+
options? : CreateSingleOptions,
85+
) : Promise<CreateSingleResult<PK, T>>
8286

8387
abstract findObjects<T>(collection : string, query, options? : FindManyOptions) : Promise<Array<T>>
8488
async findObject<T>(collection : string, query, options? : FindSingleOptions) : Promise<T | null> {
@@ -124,7 +128,12 @@ export abstract class StorageBackend {
124128
async deleteObject(collection : string, object, options? : DeleteSingleOptions) : Promise<DeleteSingleResult> {
125129
const definition = this.registry.collections[collection]
126130
if (typeof definition.pkIndex === 'string') {
127-
await this.deleteObjects(collection, {[definition.pkIndex]: object[definition.pkIndex]}, {...(options || {}), limit: 1})
131+
return this.deleteObjects(collection, {
132+
[definition.pkIndex]: object[definition.pkIndex],
133+
}, {
134+
...(options || {}),
135+
limit: 1,
136+
})
128137
} else {
129138
throw new Error('Updating single objects with compound pks is not supported yet')
130139
}
@@ -177,4 +186,4 @@ export function _validateOperationRegistration(identifier, backend : StorageBack
177186
}
178187

179188
return true
180-
}
189+
}

ts/types/manager.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import {
1717
} from './backend'
1818

1919
export interface StorageCollection {
20-
createObject(object, options?: CreateSingleOptions): Promise<CreateSingleResult>
20+
createObject<PK=string, T=any>(object: T, options?: CreateSingleOptions): Promise<CreateSingleResult<PK, T>>
2121
findOneObject<T>(query, options?: FindSingleOptions): Promise<T | null>
2222
findObject<T>(query, options?: FindSingleOptions): Promise<T | null>
2323
findObjects<T>(query, options?: FindManyOptions): Promise<Array<T>>

0 commit comments

Comments
 (0)