-
Notifications
You must be signed in to change notification settings - Fork 13
/
utils.ts
73 lines (65 loc) 路 2.15 KB
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import { Model } from 'decentraland-server'
import { utils, Log } from 'decentraland-commons'
export function omitProps<T>(obj: T, omittedProps: string[]): T
export function omitProps(obj: any, omittedProps: string[]): any {
const newObj = utils.omit(obj, omittedProps)
for (const prop in newObj) {
const value = newObj[prop]
if (value !== null && typeof value === 'object') {
if (Array.isArray(value)) {
newObj[prop] = value.map(v => omitProps(v, omittedProps))
} else {
newObj[prop] = omitProps(value, omittedProps)
}
}
}
return newObj
}
export function mockModelDbOperations(operations: { [key: string]: Function }) {
const log = new Log('Query')
const toJSON = obj => JSON.stringify(obj) // Formatting aid
const _query = Model.db.query
const _count = Model.db.count
operations = {
query: async function(query, args) {
const rows = await _query.call(this, query, args)
log.info(`Executing:\n${query} ${toJSON(args)} => ${rows.length} rows`)
return rows
},
count: async function(tableName, conditions) {
const count = await _count.call(this, tableName, conditions)
log.info(
`Counting: ${toJSON(conditions)} on ${tableName} => ${toJSON(count)}`
)
return count
},
insert: (tableName, row) => {
log.info(`Inserting: ${toJSON(row)} on ${tableName}`)
return { rows: [{}] }
},
update: (tableName, changes, conditions) =>
log.info(
`Updating: ${toJSON(conditions)} with ${toJSON(
changes
)} on ${tableName}`
),
delete: (tableName, conditions) =>
log.info(`Deleting: ${toJSON(conditions)} on ${tableName}`),
...operations
}
Model.db.query = function(...args) {
return operations.query.call(this, ...args)
}
Model.db.count = function(...args) {
return operations.count.call(this, ...args)
}
Model.db.insert = function(...args) {
return operations.insert.call(this, ...args)
}
Model.db.update = function(...args) {
return operations.update.call(this, ...args)
}
Model.db.delete = function(...args) {
return operations.delete.call(this, ...args)
}
}