5am-code / laravel-notion-api

Effortless Notion integrations with Laravel
https://notionforlaravel.com
MIT License
410 stars 50 forks source link

Database Creation #135

Closed johguentner closed 1 year ago

johguentner commented 1 year ago

Adding multiple ways to create a new database within the Notion API

$scheme = PropertyBuilder::bulk()
                ->title('MyTitle') // required
                ->richText('MyText')
                ->checkbox('...')
                ->status()
                ->number()
                ->date()
                ->url()
                ->email()
                ->phoneNumber()
                ->people()
                ->files()
                ->createdBy()
                ->createdTime()
                ->lastEditedBy()
                ->lastEditedTime()
                ->formula('Testing', 'prop("MultiSelect")')
                ->select('Select', [
                    [
                        'name' => 'option 1 select',
                        'color' => 'blue'
                    ]
                ])->multiSelect('MultiSelect', [
                    [
                        'name' => 'option 1 multiselect',
                        'color' => 'yellow'
                    ]
                ]);

 $databaseEntity = Notion::databases()
                  ->build()
                  ->inline()
                  ->title('Inline Test DB 1')
                  ->add($scheme)
                  ->createInPage('1c682e7371ec4399be1cc015686c67c6');

To keep it a bit more similar to Laravel migrations, it's also possible to do it like this:


$databaseEntity = Notion::databases()
                  ->build()
                  ->title('Test DB 2')
                  ->scheme(function ($table){
                      $table->title('My Title');
                      $table->richText('My Text');
                      $table->date('My Date');
                  })
                  ->createInPage('1c682e7371ec4399be1cc015686c67c6');

To keep things simple, it's also possible to use the PropertyBuilder::class.

$props = collect([
     PropertyBuilder::title(),
     PropertyBuilder::richText(),
     ...
]);

$databaseEntity = Notion::databases()
                  ->build()
                  ->title('Test DB 3')
                  ->add($props) //add multiple props
                  ->add(PropertyBuilder::date('My Date')) //add single props
                  ->createInPage('1c682e7371ec4399be1cc015686c67c6');

Polish of PestHttpRecorder::class

what-the-diff[bot] commented 1 year ago

PR Summary

johguentner commented 1 year ago

Hey @farez,

just wanted to double check, if this goes into the direction you hoped regarding database creation (example of current possibilities with these new changes):

$databaseEntity = Notion::databases()
                ->build()
                ->inline() // just don't use, if you want it to be as subpage
                ->title('Title of Database')
                ->addTitleProperty() // required (that's why it's an extra function for ease of use)
                ->addProperty('Description', PropertyBuilder::richText())
                ->addProperty('Number', PropertyBuilder::number('dollar'))
                ->addRawProperty('Special Property', [ /* raw for unsupported/future properties */ ])
                ->createInPage('2c682e7372ec4399be1cc015686c69c6');
farez commented 1 year ago

Hello Johannes!

Ah that looks awesome. Should work well.

An idea: have a shortcut method called addProperties() that takes an array of property definitions. Saves calling addProperty lots of times.

Thanks man. Look forward to using this.

Farez

On Fri, 28 Apr 2023, 22:58 JohGuentner, @.***> wrote:

Hey @farez https://github.com/farez,

just wanted to double check, if this goes into the direction you hoped regarding database creation (example of current possibilities with these new changes):

$databaseEntity = Notion::databases() ->build() ->inline() // just don't use, if you want it to be as subpage ->title('Title of Database') ->addTitleProperty() // required (that's why it's an extra function for ease of use) ->addProperty('Description', PropertyBuilder::richText()) ->addProperty('Number', PropertyBuilder::number('dollar')) ->addRawProperty('Special Property', [ / raw for unsupported/future properties / ]) ->createInPage('2c682e7372ec4399be1cc015686c69c6');

— Reply to this email directly, view it on GitHub https://github.com/5am-code/laravel-notion-api/pull/135#issuecomment-1527695438, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAAJVUPT2C6OJGD52DV4YBDXDPLHVANCNFSM6AAAAAAXPK57TA . You are receiving this because you were mentioned.Message ID: @.***>

johguentner commented 1 year ago

Hello Johannes! Ah that looks awesome. Should work well. An idea: have a shortcut method called addProperties() that takes an array of property definitions. Saves calling addProperty lots of times. Thanks man. Look forward to using this. Farez

Thank you for your input @farez ! I updated the description above to show all possibilities. After I add the final touches + especially testing, I'll push this as v1.1.0