ottoman
Version:
Ottoman Couchbase ODM
182 lines • 9.63 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __await = (this && this.__await) || function (v) { return this instanceof __await ? (this.v = v, this) : new __await(v); }
var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _arguments, generator) {
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
var g = generator.apply(thisArg, _arguments || []), i, q = [];
return i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i;
function verb(n) { if (g[n]) i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; }
function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } }
function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); }
function fulfill(value) { resume("next", value); }
function reject(value) { resume("throw", value); }
function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); }
};
var __asyncValues = (this && this.__asyncValues) || function (o) {
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
var m = o[Symbol.asyncIterator], i;
return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ensureN1qlIndexes = void 0;
const extract_index_field_names_1 = require("./extract-index-field-names");
const model_utils_1 = require("../../utils/model.utils");
const is_debug_mode_1 = require("../../../utils/is-debug-mode");
const constants_1 = require("../../../utils/constants");
const couchbase_1 = require("couchbase");
/**
* Creates the register index in the Database Server.
* In addition, creates one index as primary for the bucketName and one for every Ottoman model.
*/
const ensureN1qlIndexes = (ottoman, n1qlIndexes) => __awaiter(void 0, void 0, void 0, function* () {
var _a, e_1, _b, _c;
const __indexes = n1qlIndexes;
const { bucketName, cluster } = ottoman;
const indexesToBuild = {};
const existingIndexesNames = [];
// Create the ottoman type index, needed to make model lookups fast.
const keys = Object.keys(ottoman.models);
for (const key of keys) {
const Model = ottoman.getModel(key);
const metadata = (0, model_utils_1.getModelMetadata)(Model);
const { modelName, modelKey, scopeName, collectionName } = metadata;
const scapedModelKey = modelKey.replace(/\./g, 'dot');
const name = collectionName !== constants_1.DEFAULT_COLLECTION
? `Ottoman${scopeName}${modelName}`
: `Ottoman${scopeName}${scapedModelKey}`;
if (!existingIndexesNames.includes(name)) {
const on = collectionName !== constants_1.DEFAULT_COLLECTION
? `\`${bucketName}\`.\`${scopeName}\`.\`${collectionName}\``
: `\`${bucketName}\``;
try {
if (!indexesToBuild[on]) {
indexesToBuild[on] = [];
}
indexesToBuild[on].push(name);
yield cluster.query(queryForIndexOttomanType(name, on, modelKey));
}
catch (e) {
if (!(e instanceof couchbase_1.IndexExistsError)) {
if ((0, is_debug_mode_1.isDebugMode)()) {
console.error(`Failed creating N1QL index ${name}`);
}
}
}
}
}
// Create secondary indexes declared in the schema
function asyncIndexesQuery() {
return __asyncGenerator(this, arguments, function* asyncIndexesQuery_1() {
for (const indexName in __indexes) {
const indexNameSanitized = indexName.replace(/[\\$]/g, '__').replace('[*]', '-ALL').replace(/(::)/g, '-');
if (!existingIndexesNames.includes(indexNameSanitized)) {
const index = __indexes[indexName];
const fieldNames = (0, extract_index_field_names_1.extractIndexFieldNames)(index);
const Model = ottoman.getModel(index.modelName);
const metadata = (0, model_utils_1.getModelMetadata)(Model);
const { scopeName, collectionName } = metadata;
const on = collectionName !== constants_1.DEFAULT_COLLECTION
? `\`${bucketName}\`.\`${scopeName}\`.\`${collectionName}\``
: `\`${bucketName}\``;
yield yield __await(cluster
.query(queryBuildIndexDefered(indexNameSanitized, fieldNames, metadata, on))
.then(() => {
if (!indexesToBuild[on]) {
indexesToBuild[on] = [];
}
indexesToBuild[on].push(indexNameSanitized);
})
.catch((e) => {
if (e instanceof couchbase_1.IndexExistsError) {
// Is needed to try build existing indexes, due to they could be in defer_build state.
if (!indexesToBuild[on]) {
indexesToBuild[on] = [];
}
indexesToBuild[on].push(indexNameSanitized);
}
else {
if ((0, is_debug_mode_1.isDebugMode)()) {
console.error(`Failed creating Secondary N1QL index ${indexNameSanitized}`);
}
}
}));
}
}
});
}
try {
for (var _d = true, _e = __asyncValues(asyncIndexesQuery()), _f; _f = yield _e.next(), _a = _f.done, !_a;) {
_c = _f.value;
_d = false;
try {
const index = _c;
if ((0, is_debug_mode_1.isDebugMode)() && index !== undefined) {
console.log(index);
}
}
finally {
_d = true;
}
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (!_d && !_a && (_b = _e.return)) yield _b.call(_e);
}
finally { if (e_1) throw e_1.error; }
}
let names = [];
for (const key in indexesToBuild) {
names = [...names, ...(indexesToBuild[key] || [])];
}
ottoman.indexOnlinePromise = ottoman.queryIndexManager.watchIndexes(bucketName, names, 600000, {
watchPrimary: false,
});
if (ottoman.indexReadyHooks && ottoman.indexReadyHooks.length > 0) {
ottoman.indexOnlinePromise
.then(() => {
ottoman.indexOnline = true;
ottoman.indexReadyHooks.forEach((fn) => fn(null, ottoman));
})
.catch((err) => {
ottoman.indexOnline = err;
ottoman.indexReadyHooks.forEach((fn) => fn(err, ottoman));
});
}
// All indexes were built deferred, so now kick off the actual build.
for (const key in indexesToBuild) {
const buildIndexes = indexesToBuild[key];
if (buildIndexes && buildIndexes.length > 0) {
const buildIndexesQuery = queryBuildIndexes(key, [...new Set(buildIndexes)]);
yield cluster.query(buildIndexesQuery);
}
}
return Promise.resolve(true);
});
exports.ensureN1qlIndexes = ensureN1qlIndexes;
// Create the ottoman type index, needed to make model lookups fast.
const queryForIndexOttomanType = (ottomanType, on, collectionKey) => {
return `CREATE INDEX \`${ottomanType}\` ON ${on}(\`${collectionKey}\`) USING GSI WITH {"defer_build": true}`;
};
// Map createIndex across all individual n1ql model indexes.
// concurrency: 1 is important to avoid overwhelming the server.
const queryBuildIndexDefered = (indexName, fields, metadata, on) => {
const { modelKey, modelName } = metadata;
return `CREATE INDEX \`${indexName}\` ON ${on}(${fields.join(',')}) WHERE ${modelKey}="${modelName}" USING GSI WITH {"defer_build": true}`;
};
// All indexes were built deferred, so now kick off actual build.
const queryBuildIndexes = (on, indexesName) => {
return `BUILD INDEX ON ${on}(${indexesName.map((idx) => `\`${idx}\``).join(',')}) USING GSI`;
};
//# sourceMappingURL=ensure-n1ql-indexes.js.map