feat(firebase): add create cloud firestore document action

This commit is contained in:
Rıdvan Akca
2024-03-01 16:19:35 +03:00
parent dff1c5e387
commit d6b9410932
5 changed files with 134 additions and 0 deletions

View File

@@ -0,0 +1,116 @@
import defineAction from '../../../../helpers/define-action.js';
export default defineAction({
name: 'Create Cloud Firestore document',
key: 'createCloudFirestoreDocument',
description: 'Creates a new document within a Cloud Firestore collection.',
arguments: [
{
label: 'Collection',
key: 'collectionId',
type: 'dropdown',
required: true,
description: '',
variables: true,
source: {
type: 'query',
name: 'getDynamicData',
arguments: [
{
name: 'key',
value: 'listFirestoreCollections',
},
],
},
},
{
label: 'Convert Numerics',
key: 'convertNumerics',
type: 'dropdown',
required: false,
description:
"If any value represents a valid numerical value, whether it's an integer or a floating-point number, this field directs the database to store it as a numeric data type instead of a string.",
variables: true,
options: [
{ label: 'Yes', value: true },
{ label: 'No', value: false },
],
},
{
label: 'Document ID',
key: 'documentId',
type: 'string',
required: false,
description:
'The document ID to use for this document. If not specified, an ID will be assigned.',
variables: true,
},
{
label: 'Document Data',
key: 'documentData',
type: 'dynamic',
required: false,
description: '',
fields: [
{
label: 'Key',
key: 'key',
type: 'string',
required: false,
variables: true,
},
{
label: 'Value',
key: 'value',
type: 'string',
required: false,
variables: true,
},
],
},
],
async run($) {
const projectId = $.auth.data.projectId;
const { collectionId, documentId, documentData, convertNumerics } =
$.step.parameters;
const documentDataObject = documentData.reduce((result, entry) => {
const key = entry.key?.toLowerCase();
const value = entry.value;
const isNumber = !isNaN(parseFloat(value));
if (key && value) {
const formattedValue =
convertNumerics && isNumber
? { integerValue: parseFloat(value) }
: { stringValue: value };
return {
...result,
[key]: formattedValue,
};
}
return result;
}, {});
const body = {
fields: documentDataObject,
};
const { data } = await $.http.post(
`/v1/projects/${projectId}/databases/(default)/documents/${collectionId}?documentId=${documentId}`,
body,
{
additionalProperties: {
setFirestoreBaseUrl: true,
},
}
);
$.setActionItem({
raw: data,
});
},
});

View File

@@ -0,0 +1,3 @@
import createCloudFirestoreDocument from './create-cloud-firestore-document/index.js';
export default [createCloudFirestoreDocument];

View File

@@ -4,6 +4,7 @@ import auth from './auth/index.js';
import setBaseUrl from './common/set-base-url.js';
import triggers from './triggers/index.js';
import dynamicData from './dynamic-data/index.js';
import actions from './actions/index.js';
export default defineApp({
name: 'Firebase',
@@ -18,4 +19,5 @@ export default defineApp({
auth,
triggers,
dynamicData,
actions,
});

View File

@@ -100,6 +100,7 @@ export default defineConfig({
collapsible: true,
collapsed: true,
items: [
{ text: 'Actions', link: '/apps/firebase/actions' },
{ text: 'Triggers', link: '/apps/firebase/triggers' },
{ text: 'Connection', link: '/apps/firebase/connection' },
],

View File

@@ -0,0 +1,12 @@
---
favicon: /favicons/firebase.svg
items:
- name: Create Cloud Firestore document
desc: Creates a new document within a Cloud Firestore collection.
---
<script setup>
import CustomListing from '../../components/CustomListing.vue'
</script>
<CustomListing />