• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

monolithst / functional-models-orm-elastic / 13060420650

30 Jan 2025 08:11PM UTC coverage: 39.512%. First build
13060420650

Pull #1

github

web-flow
Merge 3e1ea2ca8 into 4942d1bc0
Pull Request #1: feat(3.0): add support for 3.0

22 of 69 branches covered (31.88%)

Branch coverage included in aggregate %.

32 of 87 new or added lines in 2 files covered. (36.78%)

59 of 136 relevant lines covered (43.38%)

4.35 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/datastoreAdapter.ts
1
import get from 'lodash/get'
2
import merge from 'lodash/merge'
3
import {
4
  DatastoreAdapter,
5
  OrmSearch,
6
  DataDescription,
7
  PrimaryKeyType,
8
  ModelType,
9
  ModelInstance,
10
  OrmModel,
11
} from 'functional-models'
12
import * as types from './types'
13
import { toElasticSearch } from './lib'
14

15
type WithRequired<T, K extends keyof T> = T & { [P in K]-?: T[P] }
NEW
16
const DEFAULT_TAKE = 10000
×
17

NEW
18
export const defaultGetIndexForModel = <T extends DataDescription>(
×
19
  model: ModelType<T>
20
) => {
NEW
21
  const x = model.getName().toLowerCase()
×
NEW
22
  return x
×
23
}
24

NEW
25
const create = ({
×
26
  client,
27
  getIndexForModel = defaultGetIndexForModel,
×
28
}: types.DatastoreAdapterInputs): WithRequired<
29
  DatastoreAdapter,
30
  'bulkInsert'
31
> => {
NEW
32
  const retrieve = async <T extends DataDescription>(
×
33
    model: ModelType<T>,
34
    id: PrimaryKeyType
35
  ) => {
NEW
36
    const index = getIndexForModel(model)
×
NEW
37
    const { body } = await client.get({
×
38
      index,
39
      id,
40
    })
41

NEW
42
    return body._source
×
43
  }
44

NEW
45
  const search = <T extends DataDescription>(
×
46
    model: ModelType<T>,
47
    ormQuery: OrmSearch
48
  ) => {
NEW
49
    return Promise.resolve().then(async () => {
×
NEW
50
      const index = getIndexForModel(model)
×
NEW
51
      const updatedSearch = !ormQuery.take
×
52
        ? merge(ormQuery, { take: DEFAULT_TAKE })
53
        : ormQuery
NEW
54
      const search = toElasticSearch(index, updatedSearch)
×
NEW
55
      const response = await client.search(search)
×
NEW
56
      const toMap = get(response, 'body.hits.hits', [])
×
57

58
      // We have to build the paging ourselves.
NEW
59
      const took = toMap.length
×
NEW
60
      const total = response.body.hits.total.value
×
NEW
61
      const isMore = total - took > 0
×
NEW
62
      const page = isMore ? { from: took } : undefined
×
63

NEW
64
      const instances = toMap.map((raw: any) => raw._source)
×
NEW
65
      return {
×
66
        instances,
67
        page,
68
      }
69
    })
70
  }
71

NEW
72
  const save = async <T extends DataDescription>(
×
73
    instance: ModelInstance<T>
74
  ) => {
NEW
75
    const index = getIndexForModel(instance.getModel())
×
NEW
76
    const data = await instance.toObj<T>()
×
NEW
77
    const id = instance.getPrimaryKey()
×
NEW
78
    await client.index({
×
79
      id,
80
      index,
81
      body: data,
82
    })
NEW
83
    return data
×
84
  }
85

NEW
86
  const bulkInsert = async <T extends DataDescription>(
×
87
    model: OrmModel<T>,
88
    instances: readonly ModelInstance<T>[]
89
  ) => {
NEW
90
    if (instances.length < 1) {
×
NEW
91
      return
×
92
    }
NEW
93
    const index = getIndexForModel(instances[0].getModel())
×
NEW
94
    const operations = await instances.reduce(
×
95
      async (accP: Promise<any[]>, instance: ModelInstance<T>) => {
NEW
96
        const acc = await accP
×
NEW
97
        const data = await instance.toObj()
×
NEW
98
        const id = instance.getPrimaryKey()
×
NEW
99
        return acc.concat([
×
100
          {
101
            index: { _index: index, _id: id },
102
          },
103
          data,
104
        ])
NEW
105
        return acc.concat(data)
×
106
      },
107
      Promise.resolve([] as any[])
108
    )
NEW
109
    await client.bulk({
×
110
      index,
111
      refresh: true,
112
      body: operations,
113
    })
114
    //TODO: Handle exceptions
NEW
115
    return
×
116
  }
117

NEW
118
  const deleteObj = async <T extends DataDescription>(
×
119
    model: OrmModel<T>,
120
    primarykey: PrimaryKeyType
121
  ) => {
NEW
122
    const index = getIndexForModel(model)
×
NEW
123
    await client.delete({
×
124
      index,
125
      id: primarykey,
126
    })
NEW
127
    return
×
128
  }
129

NEW
130
  return {
×
131
    bulkInsert,
132
    //@ts-ignore
133
    search,
134
    retrieve,
135
    save,
136
    delete: deleteObj,
137
  }
138
}
139

140
export { create }
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc