This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * @param message The error message | |
| * @param value The value that violates the constraint | |
| * @param field The name of the field | |
| * @param type The expected type for the field | |
| * @param constraint The name of the constraint violated | |
| * @param code The application or module code for the error | |
| */ | |
| export class IsError extends Error { | |
| constructor( |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * @param message The error message | |
| * @param value The value that violates the constraint | |
| * @param field The name of the field | |
| * @param type The expected type for the field | |
| * @param constraint The name of the constraint violated | |
| * @param code The application or module code for the error | |
| */ | |
| export class IsError extends Error { | |
| constructor( |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* Just for code completion and compilation - defines | |
| * the interface of objects stored in the emails table. | |
| */ | |
| export interface IEmailAddress { | |
| id?: number; | |
| contactId: number; | |
| type: string; | |
| email: string; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Load navgiation properties (Email and Phone records) and | |
| * update the ocrresponding ocntact fields. | |
| */ | |
| export async function loadNavigationProperties(contact, db) { | |
| [contact.emails, contact.phones] = await Promise.all([ | |
| db.emails.where('contactId').equals(this.id).toArray(), | |
| db.phones.where('contactId').equals(this.id).toArray() | |
| ]); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import Dexie from 'dexie'; | |
| import {IEmailAddress, IPhoneNumber, Contact} from './model'; | |
| export class AppDatabase extends Dexie { | |
| public contacts: Dexie.Table<Contact, number>; | |
| public emails: Dexie.Table<IEmailAddress, number>; | |
| public phones: Dexie.Table<IPhoneNumber, number>; | |
| constructor() { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Import stylesheets | |
| import './style.css'; | |
| import Dexie from 'dexie'; | |
| import { Console } from './console'; | |
| import { db} from './db'; | |
| import { Contact} from './model'; | |
| import { loadNavigationProperties, loadContactEmails, loadContactPhones, saveContact } from './utilities'; | |
| // Write TypeScript code! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class User { | |
| id:number; | |
| name: string; | |
| groups:Group[]; | |
| constructor(name:string) {this.name = name} | |
| } | |
| class Group { | |
| id:number; | |
| name: string; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import Dexie from 'dexie'; | |
| class MyAppDatabase extends Dexie { | |
| public users: Dexie.Table<User, number>; | |
| public groups: Dexie.Table<Group, number>; | |
| public usersInGroups: Dexie.Table<UserInGroup, [number, number]>; | |
| constructor () { | |
| super("MyAppDatabase"); | |
| this.version(1).stores({ | |
| users: '++id, name', |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| async function putUser(user:User) { | |
| await db.users.put(user); | |
| } | |
| async function putGroup(group:Group) { | |
| await db.groups.put(group); | |
| } | |
| async function putUserInGroup(user:User, group:Group) { | |
| await db.usersInGroups.put({userId: user.id, groupId: group.id}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| async function test() { | |
| db.delete(); | |
| db.open(); | |
| await Promise.all([db.users.clear(), db.groups.clear(), db.usersInGroups.clear()]); | |
| /** | |
| | userid | groupid | | |
| | :----: | :-----: | | |
| | 1 | 1 | | |
| | 1 | 2 | |