Skip to content

Commit 6ef298f

Browse files
committed
fix generateData
1 parent deb0362 commit 6ef298f

File tree

3 files changed

+168
-152
lines changed

3 files changed

+168
-152
lines changed

packages/core/thunks/dbWrite.ts

+75-63
Original file line numberDiff line numberDiff line change
@@ -16,85 +16,97 @@ export const dbSaveOptions: SaveOptions = {
1616
reload: false,
1717
}
1818

19-
export const dbWrite = (changes: DbChange[]): CoreThunk =>
19+
export const dbWrite = (dbChanges: DbChange[]): CoreThunk =>
2020
async function _dbWrite(dispatch, getState) {
21-
log('dbWrite %o', changes)
21+
log('dbWrite %o', dbChanges)
2222
const { connection } = selectors.appDb(getState())
2323
const loadEntities: LoadEntities[] = []
2424

2525
await connection.transaction(async manager => {
2626
const crr = manager.getRepository(ChangeRecord)
2727

28-
const records = ChangeRecord.fromDbChange(connection, changes)
29-
await crr.save(records, dbSaveOptions)
30-
31-
const recordsByTable = R.groupBy((a: ChangeRecord) => a.table, records)
32-
for (const table of Object.keys(recordsByTable) as AppTable[]) {
33-
const entity = appTable[table]
34-
const entityMetadata = connection.entityMetadatas.find(emd => emd.target === entity)
35-
if (!entityMetadata) {
36-
throw new Error('no entityMetadata for table ' + table)
37-
}
28+
const changesByTable = R.groupBy(a => a.table, dbChanges)
3829

30+
for (const table of Object.keys(changesByTable) as AppTable[]) {
3931
const le: LoadEntities = { table, deletes: [], entities: [] }
40-
for (const { id, t } of recordsByTable[table]) {
41-
const prev = await crr
42-
.createQueryBuilder('e')
43-
.where('e.table = :table', { table })
44-
.andWhere('e.id = :id', { id })
45-
.andWhere('e.t < :t', { t })
46-
.orderBy('e.t', 'DESC')
47-
.take(1)
48-
.getOne()
49-
50-
const base = prev && prev.value ? simplifyEntity(prev.value, entityMetadata) : undefined
51-
let ent: object | undefined = base
52-
let deleted = false
53-
54-
const next = await crr
55-
.createQueryBuilder('e')
56-
.where('e.table = :table', { table })
57-
.andWhere('e.id = :id', { id })
58-
.andWhere('e.t >= :t', { t })
59-
.orderBy('e.t', 'DESC')
60-
.getMany()
61-
62-
for (const n of next) {
63-
switch (n.type) {
64-
case 'add':
65-
deleted = false
66-
assert(n.value)
67-
ent = n.value
68-
break
69-
70-
case 'delete':
71-
deleted = true
72-
break
73-
74-
case 'edit':
75-
assert(ent)
76-
deleted = false
77-
ent = n.edit && ent ? iupdate(ent, n.edit) : ent
78-
n.value = ent
79-
break
32+
const entity = appTable[table]
33+
const tableChanges = changesByTable[table]
34+
if (table === 'image') {
35+
for (const change of tableChanges) {
36+
if (!change.adds) {
37+
throw new Error('images can only be added')
8038
}
39+
const saved = await manager.save(entity, change.adds)
40+
le.entities.push(...saved)
41+
}
42+
} else {
43+
const records = ChangeRecord.fromDbChange(connection, tableChanges)
44+
await crr.save(records, dbSaveOptions)
8145

82-
crr.save(next, dbSaveOptions)
46+
const entityMetadata = connection.entityMetadatas.find(emd => emd.target === entity)
47+
if (!entityMetadata) {
48+
throw new Error('no entityMetadata for table ' + table)
49+
}
50+
51+
for (const { id, t } of records) {
52+
const prev = await crr
53+
.createQueryBuilder('e')
54+
.where('e.table = :table', { table })
55+
.andWhere('e.id = :id', { id })
56+
.andWhere('e.t < :t', { t })
57+
.orderBy('e.t', 'DESC')
58+
.take(1)
59+
.getOne()
60+
61+
const base = prev && prev.value ? simplifyEntity(prev.value, entityMetadata) : undefined
62+
let ent: object | undefined = base
63+
let deleted = false
64+
65+
const next = await crr
66+
.createQueryBuilder('e')
67+
.where('e.table = :table', { table })
68+
.andWhere('e.id = :id', { id })
69+
.andWhere('e.t >= :t', { t })
70+
.orderBy('e.t', 'DESC')
71+
.getMany()
72+
73+
for (const n of next) {
74+
switch (n.type) {
75+
case 'add':
76+
deleted = false
77+
assert(n.value)
78+
ent = n.value
79+
break
80+
81+
case 'delete':
82+
deleted = true
83+
break
84+
85+
case 'edit':
86+
assert(ent)
87+
deleted = false
88+
ent = n.edit && ent ? iupdate(ent, n.edit) : ent
89+
n.value = ent
90+
break
91+
}
8392

84-
if (deleted) {
85-
await manager.delete(entity, { id })
86-
le.deletes.push(id)
87-
} else {
88-
if (ent) {
89-
const values = unsimplifyEntity(ent, entityMetadata)
90-
const saved = await manager.save(entity, { id, ...values })
91-
le.entities.push(saved)
93+
crr.save(next, dbSaveOptions)
94+
95+
if (deleted) {
96+
await manager.delete(entity, { id })
97+
le.deletes.push(id)
98+
} else {
99+
if (ent) {
100+
const values = unsimplifyEntity(ent, entityMetadata)
101+
const saved = await manager.save(entity, { id, ...values })
102+
le.entities.push(saved)
103+
}
92104
}
93105
}
94106
}
95-
}
96107

97-
loadEntities.push(le)
108+
loadEntities.push(le)
109+
}
98110
}
99111
})
100112

packages/db/export.ts

+4
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ const fixExport = (
103103
const path = `${entityMetadata.tableName}/${object.id}_${key}${ext}`
104104
zip.file(path, object[key] as Buffer)
105105
object[key] = path
106+
} else if (col.type === 'simple-json') {
107+
object[key] = object[key] && JSON.stringify(object[key])
106108
} else if (key === '_history' && object._history) {
107109
try {
108110
const value = JSON.stringify(hydrate(object._history), null, ' ')
@@ -129,6 +131,8 @@ const fixImport = async (
129131
const path = row[key] as string
130132
const data: Buffer = await zip.file(path).async('nodebuffer')
131133
row[key] = data
134+
} else if (col.type === 'simple-json') {
135+
row[key] = row[key] && JSON.parse(row[key])
132136
} else if (isDate(col.type)) {
133137
row[key] = DateTime.fromISO(row[key]).toJSDate()
134138
} else if (key === '_history' && row[key]) {

packages/storybook/scripts/generateExport.ts

+89-89
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ async function main() {
6262
faker.seed(seed)
6363

6464
const changes: DbChange[] = []
65-
const txs = 10 // 1000
65+
const txs = 1000
6666

6767
window.SQL = await require('sql.js/dist/sql-asm.js')()
6868

@@ -71,38 +71,38 @@ async function main() {
7171
fi: getBank('American Express', 'https://www.americanexpress.com/'),
7272
accounts: [{ name: 'American Express', cardImage: cardImages.amex, txs: 1 }],
7373
},
74-
// chase: {
75-
// fi: getBank('Chase', 'https://chase.com'),
76-
// accounts: [
77-
// { name: 'Chase Sapphire', cardImage: cardImages.chaseSapphire, txs },
78-
// { name: 'Chase Freedom', cardImage: cardImages.chaseFreedom, txs },
79-
// { name: 'Checking', txs },
80-
// { name: 'Vacation Savings', txs },
81-
// { name: 'House Savings', txs },
82-
// { name: 'College Savings', txs },
83-
// { name: 'Kids Savings', txs },
84-
// { name: 'Shared Savings', txs },
85-
// { name: 'Mortgage', txs },
86-
// { name: 'Home Equity Line of Credit', txs },
87-
// { name: 'Checking 2', txs },
88-
// ],
89-
// },
90-
// citi: {
91-
// fi: getBank('Citi Cards', 'https://online.citi.com/'),
92-
// accounts: [
93-
// { name: 'Citi Savings', txs: 0 },
94-
// { name: 'Citi Checking', txs },
95-
// { name: 'Citi Credit', cardImage: cardImages.citi, txs },
96-
// ],
97-
// },
98-
// discover: {
99-
// fi: getBank('Discover Card'),
100-
// accounts: [{ name: 'Discover', currencyCode: 'EUR', cardImage: cardImages.discover, txs }],
101-
// },
102-
// etrade: {
103-
// fi: getBank('E*TRADE Financial'),
104-
// accounts: [{ name: 'E*Trade Checking', txs }],
105-
// },
74+
chase: {
75+
fi: getBank('Chase', 'https://chase.com'),
76+
accounts: [
77+
{ name: 'Chase Sapphire', cardImage: cardImages.chaseSapphire, txs },
78+
{ name: 'Chase Freedom', cardImage: cardImages.chaseFreedom, txs },
79+
{ name: 'Checking', txs },
80+
{ name: 'Vacation Savings', txs },
81+
{ name: 'House Savings', txs },
82+
{ name: 'College Savings', txs },
83+
{ name: 'Kids Savings', txs },
84+
{ name: 'Shared Savings', txs },
85+
{ name: 'Mortgage', txs },
86+
{ name: 'Home Equity Line of Credit', txs },
87+
{ name: 'Checking 2', txs },
88+
],
89+
},
90+
citi: {
91+
fi: getBank('Citi Cards', 'https://online.citi.com/'),
92+
accounts: [
93+
{ name: 'Citi Savings', txs: 0 },
94+
{ name: 'Citi Checking', txs },
95+
{ name: 'Citi Credit', cardImage: cardImages.citi, txs },
96+
],
97+
},
98+
discover: {
99+
fi: getBank('Discover Card'),
100+
accounts: [{ name: 'Discover', currencyCode: 'EUR', cardImage: cardImages.discover, txs }],
101+
},
102+
etrade: {
103+
fi: getBank('E*TRADE Financial'),
104+
accounts: [{ name: 'E*Trade Checking', txs }],
105+
},
106106
}
107107

108108
const s = createStore({ online, ui: {} as any, sys: { ...sys, openDb } })
@@ -220,62 +220,62 @@ async function main() {
220220
recur: 'monthly',
221221
start: makeDate({ year: 2012, month: 7, day: 12 }),
222222
},
223-
// {
224-
// payee: 'Hulu',
225-
// description: '',
226-
// amount: -9.99,
227-
// url: 'http://hulu.com',
228-
// recur: 'monthly',
229-
// start: makeDate({ year: 2015, month: 2, day: 3 }),
230-
// },
231-
// {
232-
// payee: 'Amazon',
233-
// description: 'Amazon Prime',
234-
// amount: -120.0,
235-
// url: 'http://amazon.com',
236-
// recur: 'yearly',
237-
// start: makeDate({ year: 2017, month: 11, day: 1 }),
238-
// },
239-
// {
240-
// payee: 'Cal Water',
241-
// description: 'Water',
242-
// amount: -28.55,
243-
// url: 'https://www.calwater.com/',
244-
// recur: 'bimonthly',
245-
// start: makeDate({ year: 2015, month: 3, day: 15 }),
246-
// },
247-
// {
248-
// payee: 'National Fuel',
249-
// description: 'Utilities',
250-
// amount: -55.0,
251-
// url: 'https://natfuel.com/',
252-
// recur: 'monthly',
253-
// start: makeDate({ year: 2017, month: 11, day: 1 }),
254-
// },
255-
// {
256-
// payee: 'AT&T',
257-
// description: '',
258-
// amount: -89.99,
259-
// url: 'https://att.com/',
260-
// recur: 'monthly',
261-
// start: makeDate({ year: 2017, month: 3, day: 1 }),
262-
// },
263-
// {
264-
// payee: 'IBM',
265-
// description: '',
266-
// amount: 1000.0,
267-
// url: 'https://ibm.com/',
268-
// recur: 'biweekly',
269-
// start: makeDate({ year: 2013, month: 4, day: 1 }),
270-
// },
271-
// {
272-
// payee: 'Bank of America',
273-
// description: 'Mortgage',
274-
// amount: -1500.0,
275-
// url: 'https://www.bankofamerica.com',
276-
// recur: 'monthly',
277-
// start: makeDate({ year: 2014, month: 4, day: 1 }),
278-
// },
223+
{
224+
payee: 'Hulu',
225+
description: '',
226+
amount: -9.99,
227+
url: 'http://hulu.com',
228+
recur: 'monthly',
229+
start: makeDate({ year: 2015, month: 2, day: 3 }),
230+
},
231+
{
232+
payee: 'Amazon',
233+
description: 'Amazon Prime',
234+
amount: -120.0,
235+
url: 'http://amazon.com',
236+
recur: 'yearly',
237+
start: makeDate({ year: 2017, month: 11, day: 1 }),
238+
},
239+
{
240+
payee: 'Cal Water',
241+
description: 'Water',
242+
amount: -28.55,
243+
url: 'https://www.calwater.com/',
244+
recur: 'bimonthly',
245+
start: makeDate({ year: 2015, month: 3, day: 15 }),
246+
},
247+
{
248+
payee: 'National Fuel',
249+
description: 'Utilities',
250+
amount: -55.0,
251+
url: 'https://natfuel.com/',
252+
recur: 'monthly',
253+
start: makeDate({ year: 2017, month: 11, day: 1 }),
254+
},
255+
{
256+
payee: 'AT&T',
257+
description: '',
258+
amount: -89.99,
259+
url: 'https://att.com/',
260+
recur: 'monthly',
261+
start: makeDate({ year: 2017, month: 3, day: 1 }),
262+
},
263+
{
264+
payee: 'IBM',
265+
description: '',
266+
amount: 1000.0,
267+
url: 'https://ibm.com/',
268+
recur: 'biweekly',
269+
start: makeDate({ year: 2013, month: 4, day: 1 }),
270+
},
271+
{
272+
payee: 'Bank of America',
273+
description: 'Mortgage',
274+
amount: -1500.0,
275+
url: 'https://www.bankofamerica.com',
276+
recur: 'monthly',
277+
start: makeDate({ year: 2014, month: 4, day: 1 }),
278+
},
279279
]
280280

281281
for (const billInfo of billInfos) {

0 commit comments

Comments
 (0)