|
| 1 | +import axios from 'axios' |
1 | 2 | import { Item } from '@wix-velo/velo-external-db-types' |
| 3 | +import { dataSpi } from '@wix-velo/velo-external-db-core' |
| 4 | +import { streamToArray } from '@wix-velo/test-commons' |
2 | 5 |
|
3 | | -const axios = require('axios').create({ |
| 6 | +const axiosInstance = axios.create({ |
4 | 7 | baseURL: 'http://localhost:8080' |
5 | 8 | }) |
6 | 9 |
|
7 | | -export const givenItems = async(items: Item[], collectionName: string, auth: any) => await axios.post('/data/insert/bulk', { collectionName: collectionName, items: items }, auth) |
| 10 | +export const insertRequest = (collectionName: string, items: Item[], overwriteExisting: boolean): dataSpi.InsertRequest => ({ |
| 11 | + collectionId: collectionName, |
| 12 | + items: items, |
| 13 | + overwriteExisting, |
| 14 | + options: { |
| 15 | + consistentRead: false, |
| 16 | + appOptions: {}, |
| 17 | + } |
| 18 | +}) |
| 19 | + |
| 20 | +export const updateRequest = (collectionName: string, items: Item[]): dataSpi.UpdateRequest => ({ |
| 21 | + collectionId: collectionName, |
| 22 | + items: items, |
| 23 | + options: { |
| 24 | + consistentRead: false, |
| 25 | + appOptions: {}, |
| 26 | + } |
| 27 | +}) |
| 28 | + |
| 29 | +export const countRequest = (collectionName: string): dataSpi.CountRequest => ({ |
| 30 | + collectionId: collectionName, |
| 31 | + filter: '', |
| 32 | + options: { |
| 33 | + consistentRead: false, |
| 34 | + appOptions: {}, |
| 35 | + }, |
| 36 | +}) |
| 37 | + |
| 38 | +export const queryRequest = (collectionName: string, sort: dataSpi.Sorting[], fields: string[], filter?: dataSpi.Filter): dataSpi.QueryRequest => ({ |
| 39 | + collectionId: collectionName, |
| 40 | + query: { |
| 41 | + filter: filter ?? '', |
| 42 | + sort: sort, |
| 43 | + fields: fields, |
| 44 | + fieldsets: undefined, |
| 45 | + paging: { |
| 46 | + limit: 25, |
| 47 | + offset: 0, |
| 48 | + }, |
| 49 | + cursorPaging: null |
| 50 | + }, |
| 51 | + includeReferencedItems: [], |
| 52 | + options: { |
| 53 | + consistentRead: false, |
| 54 | + appOptions: {}, |
| 55 | + }, |
| 56 | + omitTotalCount: false |
| 57 | +}) |
| 58 | + |
| 59 | + |
| 60 | +export const queryCollectionAsArray = (collectionName: string, sort: dataSpi.Sorting[], fields: string[], auth: any, filter?: dataSpi.Filter) => |
| 61 | + axiosInstance.post('/data/query', |
| 62 | + queryRequest(collectionName, sort, fields, filter), { responseType: 'stream', transformRequest: auth.transformRequest }) |
| 63 | + .then(response => streamToArray(response.data)) |
| 64 | + |
| 65 | + |
| 66 | +export const pagingMetadata = (total: number, count: number): dataSpi.QueryResponsePart => ({ pagingMetadata: { count: count, offset: 0, total: total, tooManyToCount: false } }) |
| 67 | + |
8 | 68 |
|
9 | | -export const expectAllDataIn = async(collectionName: string, auth: any) => (await axios.post('/data/find', { collectionName: collectionName, filter: '', sort: '', skip: 0, limit: 25 }, auth)).data |
| 69 | +export const givenItems = async(items: Item[], collectionName: string, auth: any) => |
| 70 | + await axiosInstance.post('/data/insert', insertRequest(collectionName, items, false), { responseType: 'stream', transformRequest: auth.transformRequest }) |
0 commit comments