Skip to content

Commit 3969e77

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 b32ccf3 commit 3969e77

3 files changed

Lines changed: 26 additions & 14 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/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import {
2222
export { default as StorageRegistry } from './registry'
2323

2424
export interface StorageCollection {
25-
createObject(object, options? : CreateSingleOptions) : Promise<CreateSingleResult>
25+
createObject<PK=string, T=any>(object : T, options? : CreateSingleOptions) : Promise<CreateSingleResult<PK, T>>
2626
findOneObject<T>(query, options?: FindSingleOptions) : Promise<T | null>
2727
findObjects<T>(query, options?: FindManyOptions) : Promise<Array<T>>
2828
countObjects(query, options?: CountOptions) : Promise<number>

ts/types/backend.ts

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,19 @@ 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
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}
17-
export type DeleteManyResult = any
17+
export type DeleteManyResult = DeleteSingleResult
1818

1919
export type IgnoreCaseOptions = {ignoreCase? : string[]}
2020
export type ReverseOptions = {reverse? : boolean}
@@ -71,7 +71,11 @@ export abstract class StorageBackend {
7171
async cleanup() : Promise<any> {}
7272
async migrate({database} : {database?} = {}) : Promise<any> {}
7373

74-
abstract async createObject(collection : string, object, options? : CreateSingleOptions)
74+
abstract async createObject<PK=string, T=any>(
75+
collection : string,
76+
object : T,
77+
options? : CreateSingleOptions,
78+
) : Promise<CreateSingleResult<PK, T>>
7579

7680
abstract findObjects<T>(collection : string, query, options? : FindManyOptions) : Promise<Array<T>>
7781
async findObject<T>(collection : string, query, options? : FindSingleOptions) : Promise<T | null> {
@@ -117,7 +121,12 @@ export abstract class StorageBackend {
117121
async deleteObject(collection : string, object, options? : DeleteSingleOptions) : Promise<DeleteSingleResult> {
118122
const definition = this.registry.collections[collection]
119123
if (typeof definition.pkIndex === 'string') {
120-
await this.deleteObjects(collection, {[definition.pkIndex]: object[definition.pkIndex]}, {...(options || {}), limit: 1})
124+
return this.deleteObjects(collection, {
125+
[definition.pkIndex]: object[definition.pkIndex],
126+
}, {
127+
...(options || {}),
128+
limit: 1,
129+
})
121130
} else {
122131
throw new Error('Updating single objects with compound pks is not supported yet')
123132
}
@@ -166,4 +175,4 @@ export function _validateOperationRegistration(identifier, backend : StorageBack
166175
}
167176

168177
return true
169-
}
178+
}

0 commit comments

Comments
 (0)