| 36 | }; |
| 37 | |
| 38 | export class Colors implements ICollection { |
| 39 | name = "colors"; |
| 40 | readonly collection: SQLCollection<"colors", Color>; |
| 41 | constructor(private readonly db: Database) { |
| 42 | this.collection = new SQLCollection( |
| 43 | db.sql, |
| 44 | db.transaction, |
| 45 | "colors", |
| 46 | db.eventManager, |
| 47 | db.sanitizer |
| 48 | ); |
| 49 | } |
| 50 | |
| 51 | init() { |
| 52 | return this.collection.init(); |
| 53 | } |
| 54 | |
| 55 | color(id: string) { |
| 56 | return this.collection.get(id); |
| 57 | } |
| 58 | |
| 59 | find(colorCode: string) { |
| 60 | return this.all.find((eb) => eb.and([eb("colorCode", "==", colorCode)])); |
| 61 | } |
| 62 | |
| 63 | // async merge(remoteColor: MaybeDeletedItem<Color>) { |
| 64 | // if (!remoteColor) return; |
| 65 | |
| 66 | // const localColor = this.collection.get(remoteColor.id); |
| 67 | // if (!localColor || remoteColor.dateModified > localColor.dateModified) |
| 68 | // await this.collection.add(remoteColor); |
| 69 | // } |
| 70 | |
| 71 | async add(item: Partial<Color>) { |
| 72 | item.title = item.title ? sanitizeTag(item.title) : item.title; |
| 73 | const oldColor = item.id |
| 74 | ? await this.color(item.id) |
| 75 | : item.colorCode |
| 76 | ? await this.find(item.colorCode) |
| 77 | : undefined; |
| 78 | |
| 79 | if (!item.title && !oldColor?.title) throw new Error("Title is required."); |
| 80 | if (!item.colorCode && !oldColor?.colorCode) |
| 81 | throw new Error("Color code is required."); |
| 82 | |
| 83 | if (oldColor) { |
| 84 | await this.collection.update([oldColor.id], item); |
| 85 | return oldColor.id; |
| 86 | } |
| 87 | |
| 88 | const id = item.id || getId(item.dateCreated); |
| 89 | await this.collection.upsert({ |
| 90 | id, |
| 91 | dateCreated: item.dateCreated || Date.now(), |
| 92 | dateModified: item.dateModified || Date.now(), |
| 93 | title: item.title || "", |
| 94 | colorCode: item.colorCode || "", |
| 95 | type: "color", |
nothing calls this directly
no outgoing calls
no test coverage detected