@opengis/fastify-table 1.0.52 → 1.0.53
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Changelog.md +4 -0
- package/index.js +6 -0
- package/migration/exec.migrations.js +76 -0
- package/package.json +1 -1
- package/table/controllers/data.js +1 -1
package/Changelog.md
CHANGED
package/index.js
CHANGED
|
@@ -16,6 +16,8 @@ import policyPlugin from './policy/index.js';
|
|
|
16
16
|
|
|
17
17
|
import pgClients from './pg/pgClients.js';
|
|
18
18
|
|
|
19
|
+
import execMigrations from './migration/exec.migrations.js';
|
|
20
|
+
|
|
19
21
|
async function plugin(fastify, opt) {
|
|
20
22
|
// console.log(opt);
|
|
21
23
|
config.pg = opt.pg;
|
|
@@ -56,6 +58,10 @@ async function plugin(fastify, opt) {
|
|
|
56
58
|
await client.query(sql, [JSON.stringify(rows).replace(/'/g, "''")]);
|
|
57
59
|
}
|
|
58
60
|
}
|
|
61
|
+
// call from another repo / project
|
|
62
|
+
fastify.execMigrations = execMigrations;
|
|
63
|
+
// execute core migrations
|
|
64
|
+
await fastify.execMigrations();
|
|
59
65
|
});
|
|
60
66
|
if (!fastify.funcs) {
|
|
61
67
|
fastify.addHook('onRequest', async (req) => {
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
|
|
4
|
+
const time = Date.now();
|
|
5
|
+
|
|
6
|
+
import getPG from '../pg/funcs/getPG.js';
|
|
7
|
+
|
|
8
|
+
export default async function execMigrations(opt) {
|
|
9
|
+
try {
|
|
10
|
+
const pg = opt?.pg || getPG({ name: 'client' });
|
|
11
|
+
const rootDir = getCallerDir();
|
|
12
|
+
const dir = path.join(rootDir.replace(/\\/g, '/').replace(/^file:\/\/\//, ''), 'server/migrations');
|
|
13
|
+
|
|
14
|
+
console.log('migrations start', dir, Date.now() - time);
|
|
15
|
+
const exists = fs.existsSync(dir);
|
|
16
|
+
if (exists) {
|
|
17
|
+
// get directory sql file list
|
|
18
|
+
const content = fs.readdirSync(dir, { withFileTypes: true })
|
|
19
|
+
?.filter((el) => el.isFile() && path.extname(el.name) === '.sql')
|
|
20
|
+
?.map((el) => el.name) || [];
|
|
21
|
+
|
|
22
|
+
// execute sql files
|
|
23
|
+
if (content?.length) {
|
|
24
|
+
await sequence(content, { pg, dir }, execSql);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
console.log('migrations finish', dir, exists, Date.now() - time);
|
|
28
|
+
} catch(err) {
|
|
29
|
+
console.error('migrations error', err.toString(), Date.now() - time);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function getCallerDir() {
|
|
34
|
+
const originalFunc = Error.prepareStackTrace;
|
|
35
|
+
|
|
36
|
+
let callerfile;
|
|
37
|
+
try {
|
|
38
|
+
const err = new Error();
|
|
39
|
+
let currentfile;
|
|
40
|
+
|
|
41
|
+
Error.prepareStackTrace = function (err, stack) { return stack; };
|
|
42
|
+
|
|
43
|
+
currentfile = err.stack.shift().getFileName();
|
|
44
|
+
|
|
45
|
+
while (err.stack.length) {
|
|
46
|
+
callerfile = err.stack.shift().getFileName();
|
|
47
|
+
|
|
48
|
+
if(currentfile !== callerfile) break;
|
|
49
|
+
}
|
|
50
|
+
} catch (err) { }
|
|
51
|
+
|
|
52
|
+
Error.prepareStackTrace = originalFunc;
|
|
53
|
+
|
|
54
|
+
return path.dirname(callerfile);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function sequence(files, data, fn) {
|
|
58
|
+
return files.reduce((promise, filename) => promise.then(() => fn({
|
|
59
|
+
...data, filename,
|
|
60
|
+
})), Promise.resolve());
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
async function execSql({
|
|
64
|
+
pg, dir, filename,
|
|
65
|
+
}) {
|
|
66
|
+
const start = Date.now();
|
|
67
|
+
const filepath = path.join(dir, filename);
|
|
68
|
+
const sql = fs.readFileSync(filepath, 'utf-8');
|
|
69
|
+
try {
|
|
70
|
+
console.log(filename, 'start', Date.now() - start);
|
|
71
|
+
await pg.query(sql);
|
|
72
|
+
console.log(filename, 'finish', Date.now() - start);
|
|
73
|
+
} catch (err) {
|
|
74
|
+
console.log(filepath, 'error', err.toString(), Date.now() - start);
|
|
75
|
+
}
|
|
76
|
+
}
|
package/package.json
CHANGED
|
@@ -24,7 +24,7 @@ export default async function data(req) {
|
|
|
24
24
|
const cols = columns.map((el) => el.name || el).join(',');
|
|
25
25
|
const columnList = dbColumns.map((el) => el.name || el).join(',');
|
|
26
26
|
const sqlTable = sql?.filter?.((el) => !el?.disabled && el?.sql?.replace).map((el, i) => ` left join lateral (${el.sql}) ${el.name || `t${i}`} on 1=1 `)?.join('') || '';
|
|
27
|
-
const cardSqlFiltered = params.id ? cardSql?.filter?.((el) => !el?.disabled && el?.name && el?.sql?.replace) : [];
|
|
27
|
+
const cardSqlFiltered = params.id ? (cardSql?.filter?.((el) => !el?.disabled && el?.name && el?.sql?.replace) || []) : [];
|
|
28
28
|
const cardSqlTable = cardSqlFiltered.length ? cardSqlFiltered.map((el, i) => ` left join lateral (select json_agg(row_to_json(q)) as ${el.name} from (${el.sql})q) ct${i} on 1=1 `).join('') || '' : '';
|
|
29
29
|
|
|
30
30
|
const fData = query.filter ? await getFilterSQL({
|