123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398 |
- const {InnerState} = require('./inner-state');
- const {QueryFileError} = require('./errors');
- const {assert} = require('./assert');
- const {ColorConsole} = require('./utils/color');
- const npm = {
- fs: require('fs'),
- os: require('os'),
- path: require('path'),
- minify: require('pg-minify'),
- utils: require('./utils'),
- formatting: require('./formatting')
- };
- const file$query = Symbol('QueryFile.query');
- class QueryFile extends InnerState {
- constructor(file, options) {
- let filePath = file;
- options = assert(options, {
- debug: npm.utils.isDev(),
- minify: (options && options.compress && options.minify === undefined) ? true : undefined,
- compress: undefined,
- params: undefined,
- noWarnings: undefined
- });
- if (npm.utils.isText(filePath) && !npm.path.isAbsolute(filePath)) {
- filePath = npm.path.join(npm.utils.startDir, filePath);
- }
- const {usedPath} = QueryFile.instance;
-
- if (!options.noWarnings) {
- if (filePath in usedPath) {
- usedPath[filePath]++;
- ColorConsole.warn(`WARNING: Creating a duplicate QueryFile object for the same file - \n ${filePath}\n${npm.utils.getLocalStack(2, 3)}\n`);
- } else {
- usedPath[filePath] = 0;
- }
- }
- const _inner = {
- file,
- filePath,
- options,
- sql: undefined,
- error: undefined,
- ready: undefined,
- modTime: undefined
- };
- super(_inner);
- this.prepare();
- }
-
- static get instance() {
- const s = Symbol.for('pgPromiseQueryFile');
- let scope = global[s];
- if (!scope) {
- scope = {
- usedPath: {}
- };
- global[s] = scope;
- }
- return scope;
- }
-
- get [file$query]() {
- return this._inner.sql;
- }
-
- get error() {
- return this._inner.error;
- }
-
- get file() {
- return this._inner.file;
- }
-
- get options() {
- return this._inner.options;
- }
-
- prepare(throwErrors) {
- const i = this._inner, options = i.options;
- let lastMod;
- if (options.debug && i.ready) {
- try {
- lastMod = npm.fs.statSync(i.filePath).mtime.getTime();
-
- if (lastMod === i.modTime) {
- return;
- }
- i.ready = false;
- } catch (e) {
- i.sql = undefined;
- i.ready = false;
- i.error = e;
- if (throwErrors) {
- throw i.error;
- }
- return;
- }
- }
- if (i.ready) {
- return;
- }
- try {
- i.sql = npm.fs.readFileSync(i.filePath, 'utf8');
- i.modTime = lastMod || npm.fs.statSync(i.filePath).mtime.getTime();
- if (options.minify && options.minify !== 'after') {
- i.sql = npm.minify(i.sql, {compress: options.compress});
- }
- if (options.params !== undefined) {
- i.sql = npm.formatting.as.format(i.sql, options.params, {partial: true});
- }
- if (options.minify && options.minify === 'after') {
- i.sql = npm.minify(i.sql, {compress: options.compress});
- }
- i.ready = true;
- i.error = undefined;
- } catch (e) {
- i.sql = undefined;
- i.error = new QueryFileError(e, this);
- if (throwErrors) {
- throw i.error;
- }
- }
- }
- }
- QueryFile.$query = file$query;
- QueryFile.prototype[npm.formatting.as.ctf.toPostgres] = function (self) {
- self = this instanceof QueryFile && this || self;
- self.prepare(true);
- return self[QueryFile.$query];
- };
- QueryFile.prototype[npm.formatting.as.ctf.rawType] = true;
- QueryFile.prototype.toString = function (level) {
- level = level > 0 ? parseInt(level) : 0;
- const gap = npm.utils.messageGap(level + 1);
- const lines = [
- 'QueryFile {'
- ];
- this.prepare();
- lines.push(gap + 'file: "' + this.file + '"');
- lines.push(gap + 'options: ' + npm.utils.toJson(this.options));
- if (this.error) {
- lines.push(gap + 'error: ' + this.error.toString(level + 1));
- } else {
- lines.push(gap + 'query: "' + this[QueryFile.$query] + '"');
- }
- lines.push(npm.utils.messageGap(level) + '}');
- return lines.join(npm.os.EOL);
- };
- npm.utils.addInspection(QueryFile, function () {
- return this.toString();
- });
- module.exports = {QueryFile};
|