@@ -10,16 +10,22 @@ import { Index } from './Index.js';
1010import { IndexedColumn } from './IndexedColumn.js' ;
1111import { TableV2 } from './TableV2.js' ;
1212
13- export interface TableOptions {
13+ interface SharedTableOptions {
14+ localOnly ?: boolean ;
15+ insertOnly ?: boolean ;
16+ viewName ?: string ;
17+ includeOld ?: boolean | 'when-changed' ;
18+ includeMetadata ?: boolean ;
19+ ignoreEmptyUpdate ?: boolean ;
20+ }
21+
22+ export interface TableOptions extends SharedTableOptions {
1423 /**
1524 * The synced table name, matching sync rules
1625 */
1726 name : string ;
1827 columns : Column [ ] ;
1928 indexes ?: Index [ ] ;
20- localOnly ?: boolean ;
21- insertOnly ?: boolean ;
22- viewName ?: string ;
2329}
2430
2531export type RowType < T extends TableV2 < any > > = {
@@ -30,17 +36,17 @@ export type RowType<T extends TableV2<any>> = {
3036
3137export type IndexShorthand = Record < string , string [ ] > ;
3238
33- export interface TableV2Options {
39+ export interface TableV2Options extends SharedTableOptions {
3440 indexes ?: IndexShorthand ;
35- localOnly ?: boolean ;
36- insertOnly ?: boolean ;
37- viewName ?: string ;
3841}
3942
4043export const DEFAULT_TABLE_OPTIONS = {
4144 indexes : [ ] ,
4245 insertOnly : false ,
43- localOnly : false
46+ localOnly : false ,
47+ includeOld : false ,
48+ includeMetadata : false ,
49+ ignoreEmptyUpdate : false ,
4450} ;
4551
4652export const InvalidSQLCharacters = / [ " ' % , . # \s [ \] ] / ;
@@ -144,10 +150,9 @@ export class Table<Columns extends ColumnsType = ColumnsType> {
144150 private initTableV1 ( options : TableOptions ) {
145151 this . options = {
146152 ...options ,
147- indexes : options . indexes || [ ] ,
148- insertOnly : options . insertOnly ?? DEFAULT_TABLE_OPTIONS . insertOnly ,
149- localOnly : options . localOnly ?? DEFAULT_TABLE_OPTIONS . localOnly
153+ indexes : options . indexes || [ ]
150154 } ;
155+ this . applyDefaultOptions ( ) ;
151156 }
152157
153158 private initTableV2 ( columns : Columns , options ?: TableV2Options ) {
@@ -173,14 +178,26 @@ export class Table<Columns extends ColumnsType = ColumnsType> {
173178 name : '' ,
174179 columns : convertedColumns ,
175180 indexes : convertedIndexes ,
176- insertOnly : options ?. insertOnly ?? DEFAULT_TABLE_OPTIONS . insertOnly ,
177- localOnly : options ?. localOnly ?? DEFAULT_TABLE_OPTIONS . localOnly ,
178- viewName : options ?. viewName
181+ viewName : options ?. viewName ,
182+ insertOnly : options ?. insertOnly ,
183+ localOnly : options ?. localOnly ,
184+ includeOld : options ?. includeOld ,
185+ includeMetadata : options ?. includeMetadata ,
186+ ignoreEmptyUpdate : options ?. ignoreEmptyUpdate
179187 } ;
188+ this . applyDefaultOptions ( ) ;
180189
181190 this . _mappedColumns = columns ;
182191 }
183192
193+ private applyDefaultOptions ( ) {
194+ this . options . insertOnly ??= DEFAULT_TABLE_OPTIONS . insertOnly ;
195+ this . options . localOnly ??= DEFAULT_TABLE_OPTIONS . localOnly ;
196+ this . options . includeOld ??= DEFAULT_TABLE_OPTIONS . includeOld ;
197+ this . options . includeMetadata ??= DEFAULT_TABLE_OPTIONS . includeMetadata ;
198+ this . options . ignoreEmptyUpdate ??= DEFAULT_TABLE_OPTIONS . ignoreEmptyUpdate ;
199+ }
200+
184201 get name ( ) {
185202 return this . options . name ;
186203 }
@@ -212,11 +229,23 @@ export class Table<Columns extends ColumnsType = ColumnsType> {
212229 }
213230
214231 get localOnly ( ) {
215- return this . options . localOnly ?? false ;
232+ return this . options . localOnly ! ;
216233 }
217234
218235 get insertOnly ( ) {
219- return this . options . insertOnly ?? false ;
236+ return this . options . insertOnly ! ;
237+ }
238+
239+ get includeOld ( ) {
240+ return this . options . includeOld ! ;
241+ }
242+
243+ get includeMetadata ( ) {
244+ return this . options . includeMetadata ! ;
245+ }
246+
247+ get ignoreEmptyUpdate ( ) {
248+ return this . options . ignoreEmptyUpdate ! ;
220249 }
221250
222251 get internalName ( ) {
@@ -250,6 +279,13 @@ export class Table<Columns extends ColumnsType = ColumnsType> {
250279 throw new Error ( `Table has too many columns. The maximum number of columns is ${ MAX_AMOUNT_OF_COLUMNS } .` ) ;
251280 }
252281
282+ if ( this . includeMetadata && this . localOnly ) {
283+ throw new Error ( `Can't include metadata for local-only tables.` ) ;
284+ }
285+ if ( this . includeOld != false && this . localOnly ) {
286+ throw new Error ( `Can't include old values for local-only tables.` ) ;
287+ }
288+
253289 const columnNames = new Set < string > ( ) ;
254290 columnNames . add ( 'id' ) ;
255291 for ( const column of this . columns ) {
@@ -291,6 +327,10 @@ export class Table<Columns extends ColumnsType = ColumnsType> {
291327 view_name : this . viewName ,
292328 local_only : this . localOnly ,
293329 insert_only : this . insertOnly ,
330+ include_old : this . includeOld != false ,
331+ include_old_only_when_changed : this . includeOld == 'when-changed' ,
332+ include_metadata : this . includeMetadata ,
333+ ignore_empty_update : this . ignoreEmptyUpdate ,
294334 columns : this . columns . map ( ( c ) => c . toJSON ( ) ) ,
295335 indexes : this . indexes . map ( ( e ) => e . toJSON ( this ) )
296336 } ;
0 commit comments