temp.ts
/*db.notes.each(function (note: { id: string; name: string; }) {
console.log("Найдено: " + note.id + ' Название =' + note.name)
}).catch(function (error: any) {
console.error(error);
});*/
//async function haveSomeFun() {
//
// Seed Database
//
//console.log("Seeding database with some contacts...");
//await db.transaction('rw', db.contacts, async function () {
// Populate a contact
//let adamId = await db.contacts.add();
//console.log(adamId);
//let adam = new Contact('Adam', 'Tensta');
//console.log(adam);
//await saveContact(adam, db);
//db.notes.put({name: 'test'});
// Populate some emails and phone numbers for the contact
/*db.emails.add({ contactId: adamId, type: 'home', email: 'adam@tensta.se' });
db.phones.add({ contactId: adamId, type: 'work', phone: '88888888' });*/
//});
//
// For fun - add a phone number to Adam
//
// Now, just to examplify how to use the save() method as an alternative
// to db.phones.add(), we will add yet another phone number
// to an existing contact and then re-save it:
//console.log("Playing a little: adding another phone entry for Adam Tensta...");
//let adam = await db.contacts.orderBy('lastName').last();
/*console.log(`Found contact: ${adam.firstName} ${adam.lastName} (id: ${adam.id})`);
// To add another phone number to adam, the straight forward way would be this:
await db.phones.add({contactId: adam.id, type: "custom", phone: "+46 7777777"});
// But now let's do that same thing by manipulating navigation property instead:
// Load emails and phones navigation properties
await loadNavigationProperties(adam, db);
// Now, just push another phone number to adam.phones navigation property:
adam.phones.push({
contactId: adam.id,
type: 'custom',
phone: '112'
});*/
//}
//db.notes.put({name: 'test'});
//db.notes.clear();
//getAll();
/*class App extends Component<{}, State> {
state = {
newNote: {
id: 1,
name: ""
//text: ""
},
notes: []
};
render() {
return (
<div>
<h2>Hello React TS!</h2>
<FormName
note={this.state.newNote}
onAdd={this.addNote}
onChange={this.handleNoteChange}
/>
<NotesList notes={this.state.notes} onDelete={this.deleteNote} />
</div>
);
}
private addNote = (event: React.FormEvent<HTMLFormElement>) => {
event.preventDefault();
db.notes.put({name: this.state.newNote.name});
getAll();
this.setState(previousState => ({
newNote: {
id: previousState.newNote.id + 1,
name: ""
//text: ""
},
notes: [...previousState.notes, previousState.newNote]
}));
//console.log(this.state.notes);
//console.log(this.state.newNote);
};
private handleNoteChange = (event: React.ChangeEvent<HTMLInputElement>) => {
this.setState({
newNote: {
...this.state.newNote,
name: event.target.value
}
});
};
private deleteNote = (noteToDelete: Note) => {
this.setState(previousState => ({
notes: [
...previousState.notes.filter(note => note.id !== noteToDelete.id)
]
}));
};
}*/
//async function getAll() {
/*console.log("Получаем все записи");
db.notes.each(function (note) {
console.log("Найдено: " + note.id + ' Название =' + note.name)
}).catch(function (error) {
console.error(error);
});*/
/*await db.transaction('rw', db.notes, async () => {
// Transaction block
let numberOfOldFriends = await db.notes.where('id').above(0).toArray();
console.log(numberOfOldFriends);
});*/
//}
import { db } from '../services/db'
/*export async function loadNavigationProperties(contact: Contact, db: AppDatabase) {
[contact.emails, contact.phones] = await Promise.all([
db.emails.where('contactId').equals(contact.id).toArray(),
db.phones.where('contactId').equals(contact.id).toArray()
]);
}*/
/**
* Load email records and
* update the corresponding ocntact fields.
*/
/*export async function loadContactEmails(contact: Contact, db: AppDatabase) {
contact.emails =
await db.emails.where('contactId').equals(contact.id).toArray();
}*/
/**
* Load phone records and
* update the ocrresponding ocntact fields.
*/
/*export async function loadContactPhones(contact: Contact, db: AppDatabase) {
contact.phones =
await db.phones.where('contactId').equals(contact.id).toArray();
}*/
/**
* Save a contact entity. If email or phone records
* were removed from the contact, then these will also
* be deleted from the database.
*/
export async function saveContact(contact: Contact, db: AppDatabase) {
return db.transaction('rw', db.contacts, async() => {
// Add or update contact. If add, record contact.id.
contact.id = await db.contacts.put(contact);
// Save all navigation properties (arrays of emails and phones)
// Some may be new and some may be updates of existing objects.
// put() will handle both cases.
// (record the result keys from the put() operations into emailIds and phoneIds
// so that we can find local deletes)
/*let [emailIds, phoneIds] = await Promise.all ([
Promise.all(contact.emails.map(email => db.emails.put(email))),
Promise.all(contact.phones.map(phone => db.phones.put(phone)))
]);*/
// Was any email or phone number deleted from out navigation properties?
// Delete any item in DB that reference us, but is not present
// in our navigation properties:
/*await Promise.all([
db.emails.where('contactId').equals(contact.id) // references us
.and(email => emailIds.indexOf((email.id) !== undefined ? email.id : 0) === -1) // Not anymore in our array
.delete(),
db.phones.where('contactId').equals(contact.id)
.and(phone => phoneIds.indexOf((phone.id) !== undefined ? phone.id : 0) === -1)
.delete()
]);*/
});
}