Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow empty array param #630

Merged
merged 1 commit into from
Mar 19, 2023
Merged

Allow empty array param #630

merged 1 commit into from
Mar 19, 2023

Conversation

Meldiron
Copy link
Contributor

@Meldiron Meldiron commented Mar 17, 2023

What does this PR do?

Makes it possible to set array attribute to empty array in Appwrite CLI.

Generated code:

databases
    .command(`createDocument`)
    .description(`Create a new Document. Before using this route, you should create a new collection resource using either a [server integration](/docs/server/databases#databasesCreateCollection) API or directly from your database console.`)
    .requiredOption(`--databaseId <databaseId>`, `Database ID.`)
    .requiredOption(`--collectionId <collectionId>`, `Collection ID. You can create a new collection using the Database service [server integration](https://appwrite.io/docs/server/databases#databasesCreateCollection). Make sure to define attributes before creating documents.`)
    .requiredOption(`--documentId <documentId>`, `Document ID. Choose a custom ID or generate a random ID with 'ID.unique()'. Valid chars are a-z, A-Z, 0-9, period, hyphen, and underscore. Can't start with a special char. Max length is 36 chars.`)
    .requiredOption(`--data <data>`, `Document data as JSON object.`)
    .option(`--permissions [permissions...]`, `An array of permissions strings. By default, only the current user is granted all permissions. [Learn more about permissions](/docs/permissions).`)
    .action(actionRunner(databasesCreateDocument))
const databasesCreateDocument = async ({ databaseId, collectionId, documentId, data, permissions, parseOutput = true, sdk = undefined}) => {
    /* @param {string} databaseId */
    /* @param {string} collectionId */
    /* @param {string} documentId */
    /* @param {object} data */
    /* @param {string[]} permissions */

    let client = !sdk ? await sdkForProject() : sdk;
    let path = '/databases/{databaseId}/collections/{collectionId}/documents'.replace('{databaseId}', databaseId).replace('{collectionId}', collectionId);
    let payload = {};
    
    /** Body Params */

    if (typeof documentId !== 'undefined') {
        payload['documentId'] = documentId;
    }

    if (typeof data !== 'undefined') {
        payload['data'] = JSON.parse(data);
    }

    permissions = permissions === true ? [] : permissions;

    if (typeof permissions !== 'undefined') {
        payload['permissions'] = permissions;
    }

    let response = undefined;
    response = await client.call('post', path, {
        'content-type': 'application/json',
    }, payload);
    
    if (parseOutput) {
        parse(response)
        success()
    }
    return response;
}

Test Plan

  • Manual QA:
appwrite databases createDocument --permissions --databaseId 'default' --collectionId 'categories' --documentId 'cryptocurrency' --data '{"name":"Crypto","description":"Cryptocurrency coverage and news on Bitcoin, Ethereum and the blockchain startups building the future of crypto, web3 using tokens and NFTs."}'

CleanShot 2023-03-17 at 12 19 37@2x

Related PRs and Issues

x

Have you read the Contributing Guidelines on issues?

Yes

@lohanidamodar lohanidamodar merged commit 6bca0dd into master Mar 19, 2023
@lohanidamodar lohanidamodar deleted the feat-cli-empty-array branch March 19, 2023 10:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants