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

WIP: Feat/Stripe: Added more object of the stripe products categorie #101

Closed
wants to merge 14 commits into from
74 changes: 49 additions & 25 deletions docs/stripe.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,31 +58,37 @@ We need to provide Postgres with the credentials to connect to Stripe, and any a

The Stripe Wrapper supports data read and modify from Stripe API.

| Object | Select | Insert | Update | Delete | Truncate |
| ----------- | :----: | :----: | :----: | :----: | :----: |
| [Accounts](https://stripe.com/docs/api/accounts/list) | :white_check_mark:| :x: | :x: | :x: | :x: |
| [Balance](https://stripe.com/docs/api/balance) | :white_check_mark:| :x: | :x: | :x: | :x: |
| [Balance Transactions](https://stripe.com/docs/api/balance_transactions/list) | :white_check_mark:| :x: | :x: | :x: | :x: |
| [Charges](https://stripe.com/docs/api/charges/list) | :white_check_mark:| :x: | :x: | :x: | :x: |
| [Checkout Sessions](https://stripe.com/docs/api/checkout/sessions/list) | :white_check_mark:| :x: | :x: | :x: | :x: |
| [Customers](https://stripe.com/docs/api/customers/list) | :white_check_mark:| :white_check_mark:| :white_check_mark:| :white_check_mark:| :x: |
| [Disputes](https://stripe.com/docs/api/disputes/list) | :white_check_mark:| :x: | :x: | :x: | :x: |
| [Events](https://stripe.com/docs/api/events/list) | :white_check_mark:| :x: | :x: | :x: | :x: |
| [Files](https://stripe.com/docs/api/files/list) | :white_check_mark:| :x: | :x: | :x: | :x: |
| [File Links](https://stripe.com/docs/api/file_links/list) | :white_check_mark:| :x: | :x: | :x: | :x: |
| [Invoices](https://stripe.com/docs/api/invoices/list) | :white_check_mark:| :x: | :x: | :x: | :x: |
| [Mandates](https://stripe.com/docs/api/mandates) | :white_check_mark:| :x: | :x: | :x: | :x: |
| [PaymentIntents](https://stripe.com/docs/api/payment_intents/list) | :white_check_mark:| :x: | :x: | :x: | :x: |
| [Payouts](https://stripe.com/docs/api/payouts/list) | :white_check_mark:| :x: | :x: | :x: | :x: |
| [Prices](https://stripe.com/docs/api/prices/list) | :white_check_mark:| :x: | :x: | :x: | :x: |
| [Products](https://stripe.com/docs/api/products/list) | :white_check_mark:| :white_check_mark:| :white_check_mark:| :white_check_mark:| :x: |
| [Refunds](https://stripe.com/docs/api/refunds/list) | :white_check_mark:| :x: | :x: | :x: | :x: |
| [SetupAttempts](https://stripe.com/docs/api/setup_attempts/list) | :white_check_mark:| :x: | :x: | :x: | :x: |
| [SetupIntents](https://stripe.com/docs/api/setup_intents/list) | :white_check_mark:| :x: | :x: | :x: | :x: |
| [Subscriptions](https://stripe.com/docs/api/subscriptions/list) | :white_check_mark:| :white_check_mark:| :white_check_mark:| :white_check_mark:| :x: |
| [Tokens](https://stripe.com/docs/api/tokens) | :white_check_mark:| :x: | :x: | :x: | :x: |
| [Topups](https://stripe.com/docs/api/topups/list) | :white_check_mark:| :x: | :x: | :x: | :x: |
| [Transfers](https://stripe.com/docs/api/transfers/list) | :white_check_mark:| :x: | :x: | :x: | :x: |
| Object | Select | Insert | Update | Delete | Truncate |
|-------------------------------------------------------------------------------|-:-:----------------|-:-:----------------|-:-:----------------|-:-:----------------|-:-:------|
| [Accounts](https://stripe.com/docs/api/accounts/list) | :white_check_mark: | :x: | :x: | :x: | :x: |
| [Balance](https://stripe.com/docs/api/balance) | :white_check_mark: | :x: | :x: | :x: | :x: |
| [Balance Transactions](https://stripe.com/docs/api/balance_transactions/list) | :white_check_mark: | :x: | :x: | :x: | :x: |
| [Charges](https://stripe.com/docs/api/charges/list) | :white_check_mark: | :x: | :x: | :x: | :x: |
| [Checkout Sessions](https://stripe.com/docs/api/checkout/sessions/list) | :white_check_mark: | :x: | :x: | :x: | :x: |
| [Coupons](https://stripe.com/docs/api/coupons/list) | :white_check_mark: | :x: | :x: | :x: | :x: |
| [Customers](https://stripe.com/docs/api/customers/list) | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :x: |
| [Disputes](https://stripe.com/docs/api/disputes/list) | :white_check_mark: | :x: | :x: | :x: | :x: |
| [Events](https://stripe.com/docs/api/events/list) | :white_check_mark: | :x: | :x: | :x: | :x: |
| [Files](https://stripe.com/docs/api/files/list) | :white_check_mark: | :x: | :x: | :x: | :x: |
| [File Links](https://stripe.com/docs/api/file_links/list) | :white_check_mark: | :x: | :x: | :x: | :x: |
| [Invoices](https://stripe.com/docs/api/invoices/list) | :white_check_mark: | :x: | :x: | :x: | :x: |
| [Mandates](https://stripe.com/docs/api/mandates) | :white_check_mark: | :x: | :x: | :x: | :x: |
| [PaymentIntents](https://stripe.com/docs/api/payment_intents/list) | :white_check_mark: | :x: | :x: | :x: | :x: |
| [Payouts](https://stripe.com/docs/api/payouts/list) | :white_check_mark: | :x: | :x: | :x: | :x: |
| [Prices](https://stripe.com/docs/api/prices/list) | :white_check_mark: | :x: | :x: | :x: | :x: |
| [Products](https://stripe.com/docs/api/products/list) | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :x: |
| [Promotion Codes](https://stripe.com/docs/api/promotion_codes/list) | :white_check_mark: | :x: | :x: | :x: | :x: |
| [Refunds](https://stripe.com/docs/api/refunds/list) | :white_check_mark: | :x: | :x: | :x: | :x: |
| [SetupAttempts](https://stripe.com/docs/api/setup_attempts/list) | :white_check_mark: | :x: | :x: | :x: | :x: |
| [SetupIntents](https://stripe.com/docs/api/setup_intents/list) | :white_check_mark: | :x: | :x: | :x: | :x: |
| [Shipping Rates](https://stripe.com/docs/api/shipping_rates/list) | :white_check_mark: | :x: | :x: | :x: | :x: |
| [Subscriptions](https://stripe.com/docs/api/subscriptions/list) | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :x: |
| [Tax Codes](https://stripe.com/docs/api/tax_codes/list) | :white_check_mark: | :x: | :x: | :x: | :x: |
| [Tax Rates](https://stripe.com/docs/api/tax_rates/list) | :white_check_mark: | :x: | :x: | :x: | :x: |
| [Tokens](https://stripe.com/docs/api/tokens) | :white_check_mark: | :x: | :x: | :x: | :x: |
| [Topups](https://stripe.com/docs/api/topups/list) | :white_check_mark: | :x: | :x: | :x: | :x: |
| [Transfers](https://stripe.com/docs/api/transfers/list) | :white_check_mark: | :x: | :x: | :x: | :x: |


The Stripe foreign tables mirror Stripe's API. We can create a schema to hold all the Stripe tables.

Expand Down Expand Up @@ -249,6 +255,8 @@ While any column is allowed in a where clause, it is most efficient to filter by
- payment_intent
- subscription

// TODO: Add coupons docs

### Customers
*read and modify*

Expand Down Expand Up @@ -564,6 +572,8 @@ While any column is allowed in a where clause, it is most efficient to filter by
- id
- active

// TODO: Add promotion_codes docs

### Refunds
*read only*

Expand Down Expand Up @@ -657,6 +667,8 @@ While any column is allowed in a where clause, it is most efficient to filter by
- customer
- payment_method

// TODO: Add shipping_rates docs

### Subscriptions
*read and modify*

Expand Down Expand Up @@ -688,6 +700,10 @@ While any column is allowed in a where clause, it is most efficient to filter by
- price
- status

// TODO: Add tax_codes docs

// TODO: Add tax_rates docs

### Tokens
*read only*

Expand Down Expand Up @@ -766,6 +782,14 @@ While any column is allowed in a where clause, it is most efficient to filter by
- id
- destination

## Removing foreign tables

If you would like to remove a foreign table because you do not use it anymore this can be done easily with the following command

```sql
drop foreign table stripe.customers, stripe.invoices, stripe.subscriptions;
```

## Examples

Some examples on how to use Stripe foreign tables.
Expand Down
21 changes: 11 additions & 10 deletions wrappers/src/fdw/stripe_fdw/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ This is a foreign data wrapper for [Stripe](https://stripe.com/) developed using

## Changelog

| Version | Date | Notes |
| ------- | ---------- | ---------------------------------------------------- |
| 0.1.7 | 2023-07-13 | Added fdw stats collection |
| 0.1.6 | 2023-05-30 | Added Checkout Session object |
| 0.1.5 | 2023-05-01 | Added 'prices' object and empty result improvement |
| 0.1.4 | 2023-02-21 | Added Connect objects |
| 0.1.3 | 2022-12-21 | Added more core objects |
| 0.1.2 | 2022-12-04 | Added 'products' objects support |
| 0.1.1 | 2022-12-03 | Added quals pushdown support |
| 0.1.0 | 2022-12-01 | Initial version |
| Version | Date | Notes |
| ------- | ---------- | ---------------------------------------------------------------------------------------------------- |
| 0.1.8 | 2023-09-13 | Added objects for Coupons, Promotion Codes, Tax codes, Tax rates and Shipping rates |
| 0.1.7 | 2023-07-13 | Added fdw stats collection |
| 0.1.6 | 2023-05-30 | Added Checkout Session object |
| 0.1.5 | 2023-05-01 | Added 'prices' object and empty result improvement |
| 0.1.4 | 2023-02-21 | Added Connect objects |
| 0.1.3 | 2022-12-21 | Added more core objects |
| 0.1.2 | 2022-12-04 | Added 'products' objects support |
| 0.1.1 | 2022-12-03 | Added quals pushdown support |
| 0.1.0 | 2022-12-01 | Initial version |
97 changes: 84 additions & 13 deletions wrappers/src/fdw/stripe_fdw/stripe_fdw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ fn body_to_rows(
.and_then(|v| match *col_type {
"bool" => v.as_bool().map(Cell::Bool),
"i64" => v.as_i64().map(Cell::I64),
"f64" => v.as_f64().map(Cell::F64),
"json" => v.as_i64().map(Cell::Json),
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if this is correct... maybe I should first learn how rust works...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is correct for f64, but for json I think it is better to use string convert in between.

"string" => v.as_str().map(|a| Cell::String(a.to_owned())),
"timestamp" => v.as_i64().map(|a| {
let ts = to_timestamp(a as f64);
Expand Down Expand Up @@ -147,6 +149,9 @@ fn row_to_body(row: &Row) -> JsonValue {
Cell::I64(v) => {
map.insert(col_name, JsonValue::Number(Number::from(*v)));
}
Cell::F64(v) => {
map.insert(col_name, JsonValue::Number(Decimal::from(*v)));
}
Cell::String(v) => {
map.insert(col_name, JsonValue::String(v.to_string()));
}
Expand All @@ -155,6 +160,8 @@ fn row_to_body(row: &Row) -> JsonValue {
if let Some(m) = v.0.clone().as_object_mut() {
map.append(m)
}
} else {
map.insert(col_name, v);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Curious if someone can shed some light on this code if this is the correct implementation for normal json values

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks correct, but that requires the col_name matches with Stripe json field, may need have some testing on this.

}
}
_ => {
Expand Down Expand Up @@ -254,7 +261,7 @@ macro_rules! report_request_error {
}

#[wrappers_fdw(
version = "0.1.7",
version = "0.1.8",
author = "Supabase",
website = "https://github.com/supabase/wrappers/tree/main/wrappers/src/fdw/stripe_fdw",
error_type = "StripeFdwError"
Expand Down Expand Up @@ -287,6 +294,8 @@ impl StripeFdw {
"balance" => vec![],
"balance_transactions" => vec!["type"],
"charges" => vec!["customer"],
"checkout/sessions" => vec!["customer", "payment_intent", "subscription"],
"coupons" => vec![],
"customers" => vec!["email"],
"disputes" => vec!["charge", "payment_intent"],
"events" => vec!["type"],
Expand All @@ -298,14 +307,17 @@ impl StripeFdw {
"payouts" => vec!["status"],
"prices" => vec!["active", "currency", "product", "type"],
"products" => vec!["active"],
"promotion_codes" => vec![],
"refunds" => vec!["charge", "payment_intent"],
"setup_attempts" => vec!["setup_intent"],
"setup_intents" => vec!["customer", "payment_method"],
"shipping_rates" => vec!["active", "created", "currency"],
"subscriptions" => vec!["customer", "price", "status"],
"tax_codes" => vec![],
"tax_rates" => vec!["active"],
"tokens" => vec![],
"topups" => vec!["status"],
"transfers" => vec!["destination"],
"checkout/sessions" => vec!["customer", "payment_intent", "subscription"],
_ => {
report_error(
PgSqlErrorCode::ERRCODE_FDW_TABLE_NOT_FOUND,
Expand Down Expand Up @@ -378,6 +390,32 @@ impl StripeFdw {
],
tgt_cols,
),
"checkout/sessions" => body_to_rows(
resp_body,
vec![
("id", "string"),
("customer", "string"),
("payment_intent", "string"),
("subscription", "string"),
("created", "timestamp"),
],
tgt_cols,
),
"coupons" => body_to_rows(
resp_body,
vec![
("id", "string"),
("amount_off", "i64"),
("currency", "string"),
("duration", "string"),
("duration_in_months", "i64"),
("max_redemptions", "i64"),
("name", "string"),
("percent_off", "f64"),
("created", "timestamp"),
],
tgt_cols,
),
"customers" => body_to_rows(
resp_body,
vec![
Expand Down Expand Up @@ -516,6 +554,17 @@ impl StripeFdw {
],
tgt_cols,
),
"promotion_codes" => body_to_rows(
resp_body,
vec![
("id", "string"),
("code", "string"),
("coupon", "string"),
("active", "bool"),
("created", "timestamp"),
],
tgt_cols,
),
"refunds" => body_to_rows(
resp_body,
vec![
Expand Down Expand Up @@ -559,6 +608,18 @@ impl StripeFdw {
],
tgt_cols,
),
"shipping_rates" => body_to_rows(
resp_body,
vec![
("id", "string"),
("active", "bool"),
("display_name", "string"),
("amount", "string"),
("type", "string"),
("created", "timestamp"),
],
tgt_cols,
),
"subscriptions" => body_to_rows(
resp_body,
vec![
Expand All @@ -570,6 +631,27 @@ impl StripeFdw {
],
tgt_cols,
),
"tax_codes" => body_to_rows(
resp_body,
vec![
("id", "string"),
("description", "string"),
("name", "string"),
],
tgt_cols,
),
"tax_rates" => body_to_rows(
resp_body,
vec![
("id", "string"),
("active", "bool"),
("country", "string"),
("description", "string"),
("display_name", "string"),
("inclusive", "bool"),
("percentage", "f64"),
("created", "timestamp"),
),
"tokens" => body_to_rows(
resp_body,
vec![
Expand Down Expand Up @@ -605,17 +687,6 @@ impl StripeFdw {
],
tgt_cols,
),
"checkout/sessions" => body_to_rows(
resp_body,
vec![
("id", "string"),
("customer", "string"),
("payment_intent", "string"),
("subscription", "string"),
("created", "timestamp"),
],
tgt_cols,
),
_ => {
report_error(
PgSqlErrorCode::ERRCODE_FDW_TABLE_NOT_FOUND,
Expand Down
Loading
Loading