Skip to content

Commit

Permalink
feat(keystone): DOMA-10804 some fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
vovaaxeapolla committed Jan 24, 2025
1 parent 8c22dc5 commit da8fefe
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 18 deletions.
19 changes: 19 additions & 0 deletions packages/keystone/fields/CloudStorageText/Implementation.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,25 @@ class CloudStorageTextImplementation extends Text.implementation {
super(...arguments)
this.fileAdapter = adapter
this.isMultiline = true
this.isOrderable = false
}
equalityInputFields () {
return []
}
equalityInputFieldsInsensitive () {
return []
}
inInputFields () {
return []
}
orderingInputFields () {
return []
}
stringInputFields () {
return []
}
stringInputFieldsInsensitive () {
return []
}
}

Expand Down
29 changes: 11 additions & 18 deletions packages/keystone/fields/CloudStorageText/adapters/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
const { Text } = require('@keystonejs/fields')
const cuid = require('cuid')
const isNil = require('lodash/isNil')

const { CLOUD_STORAGE_TEXT_MIMETYPE, CLOUD_STORAGE_TEXT_ENCODING } = require('@open-condo/keystone/fields/CloudStorageText/constants')
const { bufferToStream, readFileFromStream, getObjectStream } = require('@open-condo/keystone/file')


const CommonInterface = superclass => class extends superclass {

constructor () {
Expand All @@ -16,7 +17,7 @@ const CommonInterface = superclass => class extends superclass {

setupHooks ({ addPreSaveHook, addPostReadHook }) {
addPreSaveHook(async (item) => {
if (this._isFieldDefined(item)) {
if (item[this.path]) {
item[this.path] = await this._saveFileToAdapter(item[this.path])
}
return item
Expand All @@ -30,30 +31,24 @@ const CommonInterface = superclass => class extends superclass {
})
}

_isFieldDefined (item) {
return item && !isNil(item[this.path])
}

async _saveFileToAdapter (content) {
const stream = bufferToStream(content)
const originalFilename = this._generateFilename()
const mimetype = 'text/plain'
const encoding = 'utf8'
const originalFilename = cuid()

const { id, filename, _meta } = await this.fileAdapter.save({
stream,
filename: originalFilename,
mimetype,
encoding,
mimetype: CLOUD_STORAGE_TEXT_MIMETYPE,
encoding: CLOUD_STORAGE_TEXT_ENCODING,
id: cuid(),
})

return {
id,
filename,
originalFilename,
mimetype,
encoding,
mimetype: CLOUD_STORAGE_TEXT_MIMETYPE,
encoding: CLOUD_STORAGE_TEXT_ENCODING,
_meta,
}
}
Expand All @@ -64,12 +59,10 @@ const CommonInterface = superclass => class extends superclass {
return fileContent.toString()
}

_generateFilename () {
return `${new Date().toISOString()}`
}

addToTableSchema (table) {
table.jsonb(this.path)
const column = table.jsonb(this.path)
if (this.isNotNullable) column.notNullable()
if (this.defaultTo) column.defaultTo(this.defaultTo)
}
}

Expand Down
7 changes: 7 additions & 0 deletions packages/keystone/fields/CloudStorageText/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const CLOUD_STORAGE_TEXT_MIMETYPE = 'text/plain'
const CLOUD_STORAGE_TEXT_ENCODING = 'utf8'

module.exports = {
CLOUD_STORAGE_TEXT_MIMETYPE,
CLOUD_STORAGE_TEXT_ENCODING,
}

0 comments on commit da8fefe

Please sign in to comment.