Skip to content

Commit fd1046e

Browse files
authored
chore: simplify schemas (#140)
There is no need for artificial JSON schemas - we never used any of. that, a list of properties is enough. Can even be an automated inspection based on database tables in the future (optionally)
1 parent 5c9d335 commit fd1046e

17 files changed

+417
-478
lines changed

packages/sync-engine/src/database/postgres.ts

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import pg, { QueryResult } from 'pg'
22
import { pg as sql } from 'yesql'
3-
import { JsonSchema } from '../schemas/types'
3+
import { EntitySchema } from '../schemas/types'
44

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

4343
// Max 5 in parallel to avoid exhausting connection pool
@@ -96,7 +96,7 @@ export class PostgresClient {
9696
private constructUpsertSql(
9797
schema: string,
9898
table: string,
99-
tableSchema: JsonSchema,
99+
tableSchema: EntitySchema,
100100
options?: {
101101
conflict?: string
102102
}
@@ -106,22 +106,16 @@ export class PostgresClient {
106106

107107
return `
108108
insert into "${schema}"."${table}" (
109-
${Object.keys(properties)
110-
.map((x) => `"${x}"`)
111-
.join(',')}
109+
${properties.map((x) => `"${x}"`).join(',')}
112110
)
113111
values (
114-
${Object.keys(properties)
115-
.map((x) => `:${x}`)
116-
.join(',')}
112+
${properties.map((x) => `:${x}`).join(',')}
117113
)
118114
on conflict (
119115
${conflict}
120116
)
121117
do update set
122-
${Object.keys(properties)
123-
.map((x) => `"${x}" = :${x}`)
124-
.join(',')}
118+
${properties.map((x) => `"${x}" = :${x}`).join(',')}
125119
;`
126120
}
127121

Lines changed: 41 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,43 @@
1-
import type { JsonSchema } from './types'
1+
import type { EntitySchema } from './types'
22

3-
export const chargeSchema: JsonSchema = {
4-
$id: 'chargeSchema',
5-
type: 'object',
6-
properties: {
7-
id: { type: 'string' },
8-
object: { type: 'string' },
9-
paid: { type: 'boolean' },
10-
order: { type: 'string' },
11-
amount: { type: 'number' },
12-
review: { type: 'string' },
13-
source: { type: 'object' },
14-
status: { type: 'string' },
15-
created: { type: 'number' },
16-
dispute: { type: 'string' },
17-
invoice: { type: 'string' },
18-
outcome: { type: 'object' },
19-
refunds: { type: 'object' },
20-
captured: { type: 'boolean' },
21-
currency: { type: 'string' },
22-
customer: { type: 'string' },
23-
livemode: { type: 'boolean' },
24-
metadata: { type: 'object' },
25-
refunded: { type: 'boolean' },
26-
shipping: { type: 'object' },
27-
application: { type: 'string' },
28-
description: { type: 'string' },
29-
destination: { type: 'string' },
30-
failure_code: { type: 'string' },
31-
on_behalf_of: { type: 'string' },
32-
fraud_details: { type: 'object' },
33-
receipt_email: { type: 'string' },
34-
payment_intent: { type: 'string' },
35-
receipt_number: { type: 'string' },
36-
transfer_group: { type: 'string' },
37-
amount_refunded: { type: 'number' },
38-
application_fee: { type: 'string' },
39-
failure_message: { type: 'string' },
40-
source_transfer: { type: 'string' },
41-
balance_transaction: { type: 'string' },
42-
statement_descriptor: { type: 'string' },
43-
payment_method_details: { type: 'object' },
44-
},
45-
required: ['id'],
3+
export const chargeSchema: EntitySchema = {
4+
properties: [
5+
'id',
6+
'object',
7+
'paid',
8+
'order',
9+
'amount',
10+
'review',
11+
'source',
12+
'status',
13+
'created',
14+
'dispute',
15+
'invoice',
16+
'outcome',
17+
'refunds',
18+
'captured',
19+
'currency',
20+
'customer',
21+
'livemode',
22+
'metadata',
23+
'refunded',
24+
'shipping',
25+
'application',
26+
'description',
27+
'destination',
28+
'failure_code',
29+
'on_behalf_of',
30+
'fraud_details',
31+
'receipt_email',
32+
'payment_intent',
33+
'receipt_number',
34+
'transfer_group',
35+
'amount_refunded',
36+
'application_fee',
37+
'failure_message',
38+
'source_transfer',
39+
'balance_transaction',
40+
'statement_descriptor',
41+
'payment_method_details',
42+
],
4643
} as const
Lines changed: 33 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,35 @@
1-
import type { JsonSchema } from './types'
1+
import type { EntitySchema } from './types'
22

3-
export const creditNoteSchema: JsonSchema = {
4-
$id: 'creditNoteSchema',
5-
type: 'object',
6-
properties: {
7-
id: { type: 'string' },
8-
object: { type: 'string' },
9-
amount: { type: 'number' },
10-
amount_shipping: { type: 'number' },
11-
created: { type: 'number' },
12-
currency: { type: 'string' },
13-
customer: { type: 'string' },
14-
customer_balance_transaction: { type: 'string' },
15-
discount_amount: { type: 'number' },
16-
discount_amounts: { type: 'object' },
17-
invoice: { type: 'string' },
18-
lines: { type: 'object' },
19-
livemode: { type: 'boolean' },
20-
memo: { type: 'string' },
21-
metadata: { type: 'object' },
22-
number: { type: 'string' },
23-
out_of_band_amount: { type: 'number' },
24-
pdf: { type: 'string' },
25-
reason: { type: 'string' },
26-
refund: { type: 'string' },
27-
shipping_cost: { type: 'object' },
28-
status: { type: 'string' },
29-
subtotal: { type: 'number' },
30-
subtotal_excluding_tax: { type: 'number' },
31-
tax_amounts: { type: 'object' },
32-
total: { type: 'number' },
33-
total_excluding_tax: { type: 'number' },
34-
type: { type: 'string' },
35-
voided_at: { type: 'string' },
36-
},
37-
required: ['id'],
3+
export const creditNoteSchema: EntitySchema = {
4+
properties: [
5+
'id',
6+
'object',
7+
'amount',
8+
'amount_shipping',
9+
'created',
10+
'currency',
11+
'customer',
12+
'customer_balance_transaction',
13+
'discount_amount',
14+
'discount_amounts',
15+
'invoice',
16+
'lines',
17+
'livemode',
18+
'memo',
19+
'metadata',
20+
'number',
21+
'out_of_band_amount',
22+
'pdf',
23+
'reason',
24+
'refund',
25+
'shipping_cost',
26+
'status',
27+
'subtotal',
28+
'subtotal_excluding_tax',
29+
'tax_amounts',
30+
'total',
31+
'total_excluding_tax',
32+
'type',
33+
'voided_at',
34+
],
3835
} as const
Lines changed: 27 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,31 @@
1-
import type { JsonSchema } from './types'
1+
import type { EntitySchema } from './types'
22

3-
export const customerSchema: JsonSchema = {
4-
$id: 'customerSchema',
5-
type: 'object',
6-
properties: {
7-
id: { type: 'string' },
8-
object: { type: 'string' },
9-
address: { type: 'object' },
10-
description: { type: 'string' },
11-
email: { type: 'string' },
12-
metadata: { type: 'object' },
13-
name: { type: 'string' },
14-
phone: { type: 'string' },
15-
shipping: { type: 'object' },
16-
balance: { type: 'integer' },
17-
created: { type: 'integer' },
18-
currency: { type: 'string' },
19-
default_source: { type: 'string' },
20-
delinquent: { type: 'boolean' },
21-
discount: { type: 'object' },
22-
invoice_prefix: { type: 'string' },
23-
invoice_settings: { type: 'object' },
24-
livemode: { type: 'boolean' },
25-
next_invoice_sequence: { type: 'integer' },
26-
preferred_locales: { type: 'object' },
27-
tax_exempt: { type: 'boolean' },
28-
},
29-
required: ['id'],
3+
export const customerSchema: EntitySchema = {
4+
properties: [
5+
'id',
6+
'object',
7+
'address',
8+
'description',
9+
'email',
10+
'metadata',
11+
'name',
12+
'phone',
13+
'shipping',
14+
'balance',
15+
'created',
16+
'currency',
17+
'default_source',
18+
'delinquent',
19+
'discount',
20+
'invoice_prefix',
21+
'invoice_settings',
22+
'livemode',
23+
'next_invoice_sequence',
24+
'preferred_locales',
25+
'tax_exempt',
26+
],
3027
} as const
3128

32-
export const customerDeletedSchema: JsonSchema = {
33-
$id: 'customerSchema',
34-
type: 'object',
35-
properties: {
36-
id: { type: 'string' },
37-
object: { type: 'string' },
38-
deleted: { type: 'boolean' },
39-
},
40-
required: ['id'],
29+
export const customerDeletedSchema: EntitySchema = {
30+
properties: ['id', 'object', 'deleted'],
4131
} as const
Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,21 @@
1-
import type { JsonSchema } from './types'
1+
import type { EntitySchema } from './types'
22

3-
export const disputeSchema: JsonSchema = {
4-
$id: 'disputeSchema',
5-
type: 'object',
6-
properties: {
7-
id: { type: 'string' },
8-
object: { type: 'string' },
9-
amount: { type: 'integer' },
10-
charge: { type: 'string' },
11-
created: { type: 'integer' },
12-
currency: { type: 'string' },
13-
balance_transactions: { type: 'object' },
14-
evidence: { type: 'object' },
15-
evidence_details: { type: 'object' },
16-
is_charge_refundable: { type: 'boolean' },
17-
livemode: { type: 'boolean' },
18-
metadata: { type: 'object' },
19-
payment_intent: { type: 'string' },
20-
reason: { type: 'string' },
21-
status: { type: 'string' },
22-
},
23-
required: ['id'],
3+
export const disputeSchema: EntitySchema = {
4+
properties: [
5+
'id',
6+
'object',
7+
'amount',
8+
'charge',
9+
'created',
10+
'currency',
11+
'balance_transactions',
12+
'evidence',
13+
'evidence_details',
14+
'is_charge_refundable',
15+
'livemode',
16+
'metadata',
17+
'payment_intent',
18+
'reason',
19+
'status',
20+
],
2421
} as const

0 commit comments

Comments
 (0)