Skip to content
This repository has been archived by the owner on Jul 14, 2023. It is now read-only.

Stream #389

Merged
merged 3 commits into from
Mar 16, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat: upload and download stream file
  • Loading branch information
ryegrost8 committed Mar 16, 2023
commit cedde8ef6f894728d7c204c054f478158b9322aa
22 changes: 22 additions & 0 deletions __tests__/lib/pg/service.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const cds = require('@sap/cds')
const deploy = require('@sap/cds/lib/dbs/cds-deploy')
const { fs } = require('@sap/cds/lib/utils/cds-utils')
// const path = require('path')

// mock (package|.cds'rc).json entries
Expand Down Expand Up @@ -236,6 +237,27 @@ describe.each(suiteEnvironments)(
ibu: 20
})
})

test('odata: stream -> sql: upload/dowload stream Media Content', async () => {
//Create a media
const response = await request.post('/beershop/TypeChecks').send({
type_mediaType: 'text/plain'
})

const entry = JSON.parse(response.text)

const mediaPath = `/beershop/TypeChecks(${entry.ID})/type_mediaContent`

const buffer = fs.readFileSync('__tests__/__assets__/test.sql')

// Upload file
await request.put(mediaPath).attach('file', buffer).expect(204)

// Download file
const mediaResponse = await request.get(mediaPath).send().responseType('blob').expect(200)

expect(mediaResponse.body.toString().includes(buffer.toString())).toEqual(true)
})
})

describe('odata: GET on Draft enabled Entity -> sql: SELECT', () => {
Expand Down
60 changes: 57 additions & 3 deletions lib/pg/execute.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ const { postProcess, getPostProcessMapper } = require('@sap/cds/libx/_runtime/db
const { PG_TYPE_CONVERSION_MAP } = require('./converters/conversion')
const { flattenArray } = require('./utils/deep')
const { remapColumnNames } = require('./utils/columns')
const { Readable } = require('stream')

const DEBUG = cds.debug('cds-pg|sql')

/*
Expand All @@ -23,14 +25,17 @@ const DEBUG = cds.debug('cds-pg|sql')
* @param {*} txTimestamp
* @return {import('pg').QueryArrayResult}
*/
const executeGenericCQN = (model, dbc, query, user, locale, txTimestamp) => {
const executeGenericCQN = async (model, dbc, query, user, locale, txTimestamp) => {
const { sql, values = [] } = _cqnToSQL(model, query, user, locale, txTimestamp)

const vals = await _convertStreamValues(values)

if (/^\s*insert/i.test(sql)) {
return executeInsertCQN(model, dbc, query, user, locale, txTimestamp)
}
const isOne = query.SELECT && query.SELECT.one
const postPropertyMapper = getPostProcessMapper(PG_TYPE_CONVERSION_MAP, model, query)
return _executeSQLReturningRows(dbc, sql, values, isOne, postPropertyMapper)
return _executeSQLReturningRows(dbc, sql, vals, isOne, postPropertyMapper)
}

/**
Expand Down Expand Up @@ -231,12 +236,61 @@ const executeUpdateCQN = async (model, dbc, cqn, user, locale, txTimestamp) => {
return Array.isArray(result) ? result.length : result
}

/**
* Processes a SELECT CQN statement and executes the query against the database.
* The result rows are processed and returned.
* @param {Object} model
* @param {import('pg').PoolClient} dbc
* @param {Object} query
* @param {*} user
* @param {String} locale
* @param {*} txTimestamp
* @return {import('pg-query-stream').QueryStream}
*/
const executeSelectStreamCQN = async (model, dbc, query, user, locale, txTimestamp) => {
const result = await executeSelectCQN(model, dbc, query, user, locale, txTimestamp)

if (result == null || result.length === 0) {
return
}

let val = Array.isArray(result) ? Object.values(result[0])[0] : Object.values(result)[0]
if (val === null) {
return null
}
if (typeof val === 'number') {
val = val.toString()
}

const stream_ = Readable.from(val)

return { value: stream_ }
}

const _convertStreamValues = async (values) => {
let any
values.forEach((v, i) => {
if (v && typeof v.pipe === 'function') {
any = values[i] = new Promise((resolve) => {
const chunks = []
v.on('data', (chunk) => chunks.push(chunk))
v.on('end', () => resolve(Buffer.concat(chunks)))
v.on('error', () => {
v.removeAllListeners('error')
v.push(null)
})
})
}
})
return any ? Promise.all(values) : values
}

module.exports = {
delete: executeGenericCQN,
insert: executeInsertCQN,
update: executeUpdateCQN,
read: executeSelectCQN,
//stream: executeSelectStreamCQN,
stream: executeSelectStreamCQN,
cqn: executeGenericCQN,
sql: executePlainSQL
}