4b60c6ca27
This changes the way item types, item fields, creator types, and CSL mappings are defined and handled, in preparation for updated types and fields. Instead of being predefined in SQL files or code, type/field info is read from a bundled JSON file shared with other parts of the Zotero ecosystem [1], referred to as the "global schema". Updates to the bundled schema file are automatically applied to the database at first run, allowing changes to be made consistently across apps. When syncing, invalid JSON properties are now rejected instead of being ignored and processed later, which will allow for schema changes to be made without causing problems in existing clients. We considered many alternative approaches, but this approach is by far the simplest, safest, and most transparent to the user. For now, there are no actual changes to types and fields, since we'll first need to do a sync cut-off for earlier versions that don't reject invalid properties. For third-party code, the main change is that type and field IDs should no longer be hard-coded, since they may not be consistent in new installs. For example, code should use `Zotero.ItemTypes.getID('note')` instead of hard-coding `1`. [1] https://github.com/zotero/zotero-schema
98 lines
2.2 KiB
JavaScript
98 lines
2.2 KiB
JavaScript
/*
|
|
***** BEGIN LICENSE BLOCK *****
|
|
|
|
Copyright © 2016 Center for History and New Media
|
|
George Mason University, Fairfax, Virginia, USA
|
|
https://www.zotero.org
|
|
|
|
This file is part of Zotero.
|
|
|
|
Zotero is free software: you can redistribute it and/or modify
|
|
it under the terms of the GNU Affero General Public License as published by
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
(at your option) any later version.
|
|
|
|
Zotero is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU Affero General Public License for more details.
|
|
|
|
You should have received a copy of the GNU Affero General Public License
|
|
along with Zotero. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
***** END LICENSE BLOCK *****
|
|
*/
|
|
|
|
Zotero.ID_Tracker = function () {
|
|
var _tables = [
|
|
'collections',
|
|
'creators',
|
|
'creatorTypes',
|
|
'customFields',
|
|
'customItemTypes',
|
|
'fields',
|
|
'itemDataValues',
|
|
'itemTypes',
|
|
'items',
|
|
'libraries',
|
|
'proxies',
|
|
'savedSearches',
|
|
'tags'
|
|
];
|
|
var _nextIDs = {};
|
|
|
|
|
|
this.init = Zotero.Promise.coroutine(function* () {
|
|
for (let table of _tables) {
|
|
_nextIDs[table] = yield _getNext(table);
|
|
}
|
|
});
|
|
|
|
|
|
/**
|
|
* Gets an unused primary key id for a DB table
|
|
*/
|
|
this.get = function (table) {
|
|
if (!_nextIDs[table]) {
|
|
throw new Error("IDs not loaded for table '" + table + "'");
|
|
}
|
|
|
|
return _nextIDs[table]++;
|
|
};
|
|
|
|
|
|
function _getTableColumn(table) {
|
|
switch (table) {
|
|
case 'libraries':
|
|
return 'libraryID';
|
|
|
|
case 'itemDataValues':
|
|
return 'valueID';
|
|
|
|
case 'savedSearches':
|
|
return 'savedSearchID';
|
|
|
|
case 'creatorData':
|
|
return 'creatorDataID';
|
|
|
|
case 'proxies':
|
|
return 'proxyID';
|
|
|
|
default:
|
|
return table.substr(0, table.length - 1) + 'ID';
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* Get MAX(id) + 1 from table
|
|
*
|
|
* @return {Promise<Integer>}
|
|
*/
|
|
function _getNext(table) {
|
|
var sql = 'SELECT COALESCE(MAX(' + _getTableColumn(table) + ') + 1, 1) FROM ' + table;
|
|
return Zotero.DB.valueQueryAsync(sql);
|
|
};
|
|
}
|
|
|
|
Zotero.ID = new Zotero.ID_Tracker;
|