Skip to content

Instantly share code, notes, and snippets.

View oleersoy's full-sized avatar

Ole Ersoy oleersoy

  • Firefly Semantics Corporation
  • Chicago
View GitHub Profile
/**
* @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(
@oleersoy
oleersoy / IsError.ts
Created March 7, 2019 15:50
Is Error
/**
* @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(
@oleersoy
oleersoy / model.ts
Created March 30, 2019 03:27
dexie model
/* 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;
}
@oleersoy
oleersoy / utilities.ts
Last active March 30, 2019 14:50
utilities.ts for dexie
/**
* 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()
]);
}
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() {
// 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!
class User {
id:number;
name: string;
groups:Group[];
constructor(name:string) {this.name = name}
}
class Group {
id:number;
name: string;
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',
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});
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 |