You've already forked joplin
mirror of
https://github.com/laurent22/joplin.git
synced 2025-11-29 22:48:10 +02:00
Adding service to keep track of note resources associations
This commit is contained in:
35
ReactNativeClient/lib/models/ItemChange.js
Normal file
35
ReactNativeClient/lib/models/ItemChange.js
Normal file
@@ -0,0 +1,35 @@
|
||||
const BaseModel = require('lib/BaseModel.js');
|
||||
const Mutex = require('async-mutex').Mutex;
|
||||
|
||||
class ItemChange extends BaseModel {
|
||||
|
||||
static tableName() {
|
||||
return 'item_changes';
|
||||
}
|
||||
|
||||
static modelType() {
|
||||
return BaseModel.TYPE_ITEM_CHANGE;
|
||||
}
|
||||
|
||||
static async add(itemType, itemId, type) {
|
||||
const release = await ItemChange.addChangeMutex_.acquire();
|
||||
|
||||
try {
|
||||
await this.db().transactionExecBatch([
|
||||
{ sql: 'DELETE FROM item_changes WHERE item_id = ?', params: [itemId] },
|
||||
{ sql: 'INSERT INTO item_changes (item_type, item_id, type, created_time) VALUES (?, ?, ?, ?)', params: [itemType, itemId, type, Date.now()] },
|
||||
]);
|
||||
} finally {
|
||||
release();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
ItemChange.addChangeMutex_ = new Mutex();
|
||||
|
||||
ItemChange.TYPE_CREATE = 1;
|
||||
ItemChange.TYPE_UPDATE = 2;
|
||||
ItemChange.TYPE_DELETE = 3;
|
||||
|
||||
module.exports = ItemChange;
|
||||
@@ -2,6 +2,7 @@ const BaseModel = require('lib/BaseModel.js');
|
||||
const { Log } = require('lib/log.js');
|
||||
const { sprintf } = require('sprintf-js');
|
||||
const BaseItem = require('lib/models/BaseItem.js');
|
||||
const ItemChange = require('lib/models/ItemChange.js');
|
||||
const Setting = require('lib/models/Setting.js');
|
||||
const { shim } = require('lib/shim.js');
|
||||
const { time } = require('lib/time-utils.js');
|
||||
@@ -398,6 +399,8 @@ class Note extends BaseItem {
|
||||
|
||||
const note = await super.save(o, options);
|
||||
|
||||
ItemChange.add(BaseModel.TYPE_NOTE, note.id, isNew ? ItemChange.TYPE_CREATE : ItemChange.TYPE_UPDATE);
|
||||
|
||||
this.dispatch({
|
||||
type: 'NOTE_UPDATE_ONE',
|
||||
note: note,
|
||||
@@ -413,18 +416,22 @@ class Note extends BaseItem {
|
||||
return note;
|
||||
}
|
||||
|
||||
static async delete(id, options = null) {
|
||||
let r = await super.delete(id, options);
|
||||
// Not used?
|
||||
|
||||
this.dispatch({
|
||||
type: 'NOTE_DELETE',
|
||||
id: id,
|
||||
});
|
||||
}
|
||||
// static async delete(id, options = null) {
|
||||
// let r = await super.delete(id, options);
|
||||
|
||||
// this.dispatch({
|
||||
// type: 'NOTE_DELETE',
|
||||
// id: id,
|
||||
// });
|
||||
// }
|
||||
|
||||
static batchDelete(ids, options = null) {
|
||||
const result = super.batchDelete(ids, options);
|
||||
for (let i = 0; i < ids.length; i++) {
|
||||
ItemChange.add(BaseModel.TYPE_NOTE, ids[i], ItemChange.TYPE_DELETE);
|
||||
|
||||
this.dispatch({
|
||||
type: 'NOTE_DELETE',
|
||||
id: ids[i],
|
||||
|
||||
32
ReactNativeClient/lib/models/NoteResource.js
Normal file
32
ReactNativeClient/lib/models/NoteResource.js
Normal file
@@ -0,0 +1,32 @@
|
||||
const BaseModel = require('lib/BaseModel.js');
|
||||
|
||||
class NoteResource extends BaseModel {
|
||||
|
||||
static tableName() {
|
||||
return 'note_resources';
|
||||
}
|
||||
|
||||
static modelType() {
|
||||
return BaseModel.TYPE_NOTE_RESOURCE;
|
||||
}
|
||||
|
||||
static async associate(noteId, resourceIds) {
|
||||
let queries = [];
|
||||
queries.push({ sql: 'DELETE FROM note_resources WHERE note_id = ?', params: [noteId] });
|
||||
|
||||
for (let i = 0; i < resourceIds.length; i++) {
|
||||
queries.push({ sql: 'INSERT INTO note_resources (note_id, resource_id) VALUES (?, ?)', params: [noteId, resourceIds[i]] });
|
||||
}
|
||||
|
||||
await this.db().transactionExecBatch(queries);
|
||||
}
|
||||
|
||||
static async remove(noteId) {
|
||||
let queries = [];
|
||||
queries.push({ sql: 'DELETE FROM note_resources WHERE note_id = ?', params: [noteId] });
|
||||
await this.db().transactionExecBatch(queries);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
module.exports = NoteResource;
|
||||
Reference in New Issue
Block a user