|
| 1 | +import { expect } from 'vitest'; |
| 2 | +import { column, Schema, Table } from '@powersync/common'; |
| 3 | +import { databaseTest } from './utils'; |
| 4 | + |
| 5 | +databaseTest('include metadata', async ({ database }) => { |
| 6 | + await database.init(); |
| 7 | + const schema = new Schema({ |
| 8 | + lists: new Table( |
| 9 | + { |
| 10 | + name: column.text |
| 11 | + }, |
| 12 | + { includeMetadata: true } |
| 13 | + ) |
| 14 | + }); |
| 15 | + await database.updateSchema(schema); |
| 16 | + await database.execute('INSERT INTO lists (id, name, _metadata) VALUES (uuid(), ?, ?);', ['entry', 'so meta']); |
| 17 | + |
| 18 | + const batch = await database.getNextCrudTransaction(); |
| 19 | + expect(batch?.crud[0].metadata).toBe('so meta'); |
| 20 | +}); |
| 21 | + |
| 22 | +databaseTest('include old values', async ({ database }) => { |
| 23 | + await database.init(); |
| 24 | + const schema = new Schema({ |
| 25 | + lists: new Table( |
| 26 | + { |
| 27 | + name: column.text |
| 28 | + }, |
| 29 | + { includeOld: true } |
| 30 | + ) |
| 31 | + }); |
| 32 | + await database.updateSchema(schema); |
| 33 | + await database.execute('INSERT INTO lists (id, name) VALUES (uuid(), ?);', ['entry']); |
| 34 | + await database.execute('DELETE FROM ps_crud;'); |
| 35 | + await database.execute('UPDATE lists SET name = ?', ['new name']); |
| 36 | + |
| 37 | + const batch = await database.getNextCrudTransaction(); |
| 38 | + expect(batch?.crud[0].oldData).toStrictEqual({name: 'entry'}); |
| 39 | +}); |
| 40 | + |
| 41 | +databaseTest('include old values when changed', async ({ database }) => { |
| 42 | + await database.init(); |
| 43 | + const schema = new Schema({ |
| 44 | + lists: new Table( |
| 45 | + { |
| 46 | + name: column.text, |
| 47 | + content: column.text |
| 48 | + }, |
| 49 | + { includeOld: 'when-changed' } |
| 50 | + ) |
| 51 | + }); |
| 52 | + await database.updateSchema(schema); |
| 53 | + await database.execute('INSERT INTO lists (id, name, content) VALUES (uuid(), ?, ?);', ['name', 'content']); |
| 54 | + await database.execute('DELETE FROM ps_crud;'); |
| 55 | + await database.execute('UPDATE lists SET name = ?', ['new name']); |
| 56 | + |
| 57 | + const batch = await database.getNextCrudTransaction(); |
| 58 | + expect(batch?.crud[0].oldData).toStrictEqual({name: 'name'}); |
| 59 | +}); |
| 60 | + |
| 61 | +databaseTest('ignore empty update', async ({ database }) => { |
| 62 | + await database.init(); |
| 63 | + const schema = new Schema({ |
| 64 | + lists: new Table( |
| 65 | + { |
| 66 | + name: column.text |
| 67 | + }, |
| 68 | + { ignoreEmptyUpdate: true } |
| 69 | + ) |
| 70 | + }); |
| 71 | + await database.updateSchema(schema); |
| 72 | + await database.execute('INSERT INTO lists (id, name) VALUES (uuid(), ?);', ['name']); |
| 73 | + await database.execute('DELETE FROM ps_crud;'); |
| 74 | + await database.execute('UPDATE lists SET name = ?', ['name']); |
| 75 | + |
| 76 | + const batch = await database.getNextCrudTransaction(); |
| 77 | + expect(batch).toBeNull(); |
| 78 | +}); |
0 commit comments