Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 6 additions & 12 deletions packages/sync-engine/src/database/postgres.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pg, { QueryResult } from 'pg'
import { pg as sql } from 'yesql'
import { JsonSchema } from '../schemas/types'
import { EntitySchema } from '../schemas/types'

type PostgresConfig = {
databaseUrl: string
Expand Down Expand Up @@ -37,7 +37,7 @@ export class PostgresClient {
T extends {
[Key: string]: any // eslint-disable-line @typescript-eslint/no-explicit-any
},
>(entries: T[], table: string, tableSchema: JsonSchema): Promise<T[]> {
>(entries: T[], table: string, tableSchema: EntitySchema): Promise<T[]> {
if (!entries.length) return []

// Max 5 in parallel to avoid exhausting connection pool
Expand Down Expand Up @@ -96,7 +96,7 @@ export class PostgresClient {
private constructUpsertSql(
schema: string,
table: string,
tableSchema: JsonSchema,
tableSchema: EntitySchema,
options?: {
conflict?: string
}
Expand All @@ -106,22 +106,16 @@ export class PostgresClient {

return `
insert into "${schema}"."${table}" (
${Object.keys(properties)
.map((x) => `"${x}"`)
.join(',')}
${properties.map((x) => `"${x}"`).join(',')}
)
values (
${Object.keys(properties)
.map((x) => `:${x}`)
.join(',')}
${properties.map((x) => `:${x}`).join(',')}
)
on conflict (
${conflict}
)
do update set
${Object.keys(properties)
.map((x) => `"${x}" = :${x}`)
.join(',')}
${properties.map((x) => `"${x}" = :${x}`).join(',')}
;`
}

Expand Down
85 changes: 41 additions & 44 deletions packages/sync-engine/src/schemas/charge.ts
Original file line number Diff line number Diff line change
@@ -1,46 +1,43 @@
import type { JsonSchema } from './types'
import type { EntitySchema } from './types'

export const chargeSchema: JsonSchema = {
$id: 'chargeSchema',
type: 'object',
properties: {
id: { type: 'string' },
object: { type: 'string' },
paid: { type: 'boolean' },
order: { type: 'string' },
amount: { type: 'number' },
review: { type: 'string' },
source: { type: 'object' },
status: { type: 'string' },
created: { type: 'number' },
dispute: { type: 'string' },
invoice: { type: 'string' },
outcome: { type: 'object' },
refunds: { type: 'object' },
captured: { type: 'boolean' },
currency: { type: 'string' },
customer: { type: 'string' },
livemode: { type: 'boolean' },
metadata: { type: 'object' },
refunded: { type: 'boolean' },
shipping: { type: 'object' },
application: { type: 'string' },
description: { type: 'string' },
destination: { type: 'string' },
failure_code: { type: 'string' },
on_behalf_of: { type: 'string' },
fraud_details: { type: 'object' },
receipt_email: { type: 'string' },
payment_intent: { type: 'string' },
receipt_number: { type: 'string' },
transfer_group: { type: 'string' },
amount_refunded: { type: 'number' },
application_fee: { type: 'string' },
failure_message: { type: 'string' },
source_transfer: { type: 'string' },
balance_transaction: { type: 'string' },
statement_descriptor: { type: 'string' },
payment_method_details: { type: 'object' },
},
required: ['id'],
export const chargeSchema: EntitySchema = {
properties: [
'id',
'object',
'paid',
'order',
'amount',
'review',
'source',
'status',
'created',
'dispute',
'invoice',
'outcome',
'refunds',
'captured',
'currency',
'customer',
'livemode',
'metadata',
'refunded',
'shipping',
'application',
'description',
'destination',
'failure_code',
'on_behalf_of',
'fraud_details',
'receipt_email',
'payment_intent',
'receipt_number',
'transfer_group',
'amount_refunded',
'application_fee',
'failure_message',
'source_transfer',
'balance_transaction',
'statement_descriptor',
'payment_method_details',
],
} as const
69 changes: 33 additions & 36 deletions packages/sync-engine/src/schemas/credit_note.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,35 @@
import type { JsonSchema } from './types'
import type { EntitySchema } from './types'

export const creditNoteSchema: JsonSchema = {
$id: 'creditNoteSchema',
type: 'object',
properties: {
id: { type: 'string' },
object: { type: 'string' },
amount: { type: 'number' },
amount_shipping: { type: 'number' },
created: { type: 'number' },
currency: { type: 'string' },
customer: { type: 'string' },
customer_balance_transaction: { type: 'string' },
discount_amount: { type: 'number' },
discount_amounts: { type: 'object' },
invoice: { type: 'string' },
lines: { type: 'object' },
livemode: { type: 'boolean' },
memo: { type: 'string' },
metadata: { type: 'object' },
number: { type: 'string' },
out_of_band_amount: { type: 'number' },
pdf: { type: 'string' },
reason: { type: 'string' },
refund: { type: 'string' },
shipping_cost: { type: 'object' },
status: { type: 'string' },
subtotal: { type: 'number' },
subtotal_excluding_tax: { type: 'number' },
tax_amounts: { type: 'object' },
total: { type: 'number' },
total_excluding_tax: { type: 'number' },
type: { type: 'string' },
voided_at: { type: 'string' },
},
required: ['id'],
export const creditNoteSchema: EntitySchema = {
properties: [
'id',
'object',
'amount',
'amount_shipping',
'created',
'currency',
'customer',
'customer_balance_transaction',
'discount_amount',
'discount_amounts',
'invoice',
'lines',
'livemode',
'memo',
'metadata',
'number',
'out_of_band_amount',
'pdf',
'reason',
'refund',
'shipping_cost',
'status',
'subtotal',
'subtotal_excluding_tax',
'tax_amounts',
'total',
'total_excluding_tax',
'type',
'voided_at',
],
} as const
64 changes: 27 additions & 37 deletions packages/sync-engine/src/schemas/customer.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,31 @@
import type { JsonSchema } from './types'
import type { EntitySchema } from './types'

export const customerSchema: JsonSchema = {
$id: 'customerSchema',
type: 'object',
properties: {
id: { type: 'string' },
object: { type: 'string' },
address: { type: 'object' },
description: { type: 'string' },
email: { type: 'string' },
metadata: { type: 'object' },
name: { type: 'string' },
phone: { type: 'string' },
shipping: { type: 'object' },
balance: { type: 'integer' },
created: { type: 'integer' },
currency: { type: 'string' },
default_source: { type: 'string' },
delinquent: { type: 'boolean' },
discount: { type: 'object' },
invoice_prefix: { type: 'string' },
invoice_settings: { type: 'object' },
livemode: { type: 'boolean' },
next_invoice_sequence: { type: 'integer' },
preferred_locales: { type: 'object' },
tax_exempt: { type: 'boolean' },
},
required: ['id'],
export const customerSchema: EntitySchema = {
properties: [
'id',
'object',
'address',
'description',
'email',
'metadata',
'name',
'phone',
'shipping',
'balance',
'created',
'currency',
'default_source',
'delinquent',
'discount',
'invoice_prefix',
'invoice_settings',
'livemode',
'next_invoice_sequence',
'preferred_locales',
'tax_exempt',
],
} as const

export const customerDeletedSchema: JsonSchema = {
$id: 'customerSchema',
type: 'object',
properties: {
id: { type: 'string' },
object: { type: 'string' },
deleted: { type: 'boolean' },
},
required: ['id'],
export const customerDeletedSchema: EntitySchema = {
properties: ['id', 'object', 'deleted'],
} as const
41 changes: 19 additions & 22 deletions packages/sync-engine/src/schemas/dispute.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
import type { JsonSchema } from './types'
import type { EntitySchema } from './types'

export const disputeSchema: JsonSchema = {
$id: 'disputeSchema',
type: 'object',
properties: {
id: { type: 'string' },
object: { type: 'string' },
amount: { type: 'integer' },
charge: { type: 'string' },
created: { type: 'integer' },
currency: { type: 'string' },
balance_transactions: { type: 'object' },
evidence: { type: 'object' },
evidence_details: { type: 'object' },
is_charge_refundable: { type: 'boolean' },
livemode: { type: 'boolean' },
metadata: { type: 'object' },
payment_intent: { type: 'string' },
reason: { type: 'string' },
status: { type: 'string' },
},
required: ['id'],
export const disputeSchema: EntitySchema = {
properties: [
'id',
'object',
'amount',
'charge',
'created',
'currency',
'balance_transactions',
'evidence',
'evidence_details',
'is_charge_refundable',
'livemode',
'metadata',
'payment_intent',
'reason',
'status',
],
} as const
Loading