@saltcorn/data 0.6.1-beta.0 → 0.6.1
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/base-plugin/actions.js +172 -1
- package/base-plugin/fieldviews.js +63 -0
- package/base-plugin/fileviews.js +41 -0
- package/base-plugin/index.js +35 -0
- package/base-plugin/types.js +345 -9
- package/base-plugin/viewtemplates/edit.js +107 -0
- package/base-plugin/viewtemplates/feed.js +46 -0
- package/base-plugin/viewtemplates/filter.js +43 -0
- package/base-plugin/viewtemplates/list.js +73 -1
- package/base-plugin/viewtemplates/listshowlist.js +54 -3
- package/base-plugin/viewtemplates/room.js +100 -0
- package/base-plugin/viewtemplates/show.js +86 -0
- package/base-plugin/viewtemplates/viewable_fields.js +124 -0
- package/contracts.js +58 -0
- package/db/connect.js +13 -5
- package/db/db.test.js +0 -154
- package/db/fixtures.js +13 -1
- package/db/index.js +42 -3
- package/db/reset_schema.js +11 -0
- package/db/state.js +105 -36
- package/index.js +13 -0
- package/migrate.js +4 -1
- package/models/backup.js +78 -0
- package/models/config.js +113 -22
- package/models/crash.js +44 -0
- package/models/discovery.js +13 -11
- package/models/email.js +17 -0
- package/models/eventlog.js +51 -0
- package/models/expression.js +49 -1
- package/models/field.js +88 -9
- package/models/fieldrepeat.js +33 -0
- package/models/file.js +23 -4
- package/models/form.js +34 -0
- package/models/index.js +42 -0
- package/models/layout.js +33 -0
- package/models/library.js +44 -0
- package/models/pack.js +88 -0
- package/models/page.js +13 -3
- package/models/plugin.js +9 -2
- package/models/random.js +36 -0
- package/models/role.js +36 -0
- package/models/scheduler.js +28 -0
- package/models/table.js +34 -15
- package/models/table_constraints.js +44 -0
- package/models/tenant.js +46 -9
- package/models/trigger.js +24 -11
- package/models/user.js +33 -8
- package/models/view.js +89 -8
- package/models/workflow.js +31 -0
- package/package.json +7 -5
- package/plugin-helper.js +102 -44
- package/plugin-testing.js +4 -0
- package/tests/exact_views.test.js +5 -5
- package/utils.js +4 -0
- package/db/internal.js +0 -229
- package/db/multi-tenant.js +0 -24
- package/db/pg.js +0 -374
- package/db/single-tenant.js +0 -8
- package/db/sqlite.js +0 -280
- package/db/tenants.js +0 -6
package/db/single-tenant.js
DELETED
package/db/sqlite.js
DELETED
|
@@ -1,280 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* SQLite3 data access layer
|
|
3
|
-
*/
|
|
4
|
-
// TODO move all sqlite specific to this module
|
|
5
|
-
const sqlite3 = require("sqlite3").verbose();
|
|
6
|
-
const { sqlsanitize, mkWhere, mkSelectOptions } = require("./internal");
|
|
7
|
-
const { getConnectObject } = require("./connect");
|
|
8
|
-
const fs = require("fs").promises;
|
|
9
|
-
const connectObj = getConnectObject(); // was var
|
|
10
|
-
/**
|
|
11
|
-
* Get sqlite path
|
|
12
|
-
* @returns {*}
|
|
13
|
-
*/
|
|
14
|
-
const get_db_filepath = () => {
|
|
15
|
-
if (connectObj.sqlite_path) return connectObj.sqlite_path;
|
|
16
|
-
};
|
|
17
|
-
|
|
18
|
-
let current_filepath = get_db_filepath();
|
|
19
|
-
let sqliteDatabase = new sqlite3.Database(current_filepath);
|
|
20
|
-
|
|
21
|
-
let log_sql_enabled = false;
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* Control Logging sql statements to console
|
|
25
|
-
* @param val - if true then log sql statements to console
|
|
26
|
-
*/
|
|
27
|
-
function set_sql_logging(val = true) {
|
|
28
|
-
log_sql_enabled = val;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* Get sql logging state
|
|
33
|
-
* @returns {boolean} if true then sql logging eabled
|
|
34
|
-
*/
|
|
35
|
-
function get_sql_logging() {
|
|
36
|
-
return log_sql_enabled;
|
|
37
|
-
}
|
|
38
|
-
/**
|
|
39
|
-
* Log SQL statement to console
|
|
40
|
-
* @param sql - SQL statement
|
|
41
|
-
* @param vs - any additional parameter
|
|
42
|
-
*/
|
|
43
|
-
function sql_log(sql, vs) {
|
|
44
|
-
if (log_sql_enabled)
|
|
45
|
-
if (typeof vs === "undefined") console.log(sql);
|
|
46
|
-
else console.log(sql, vs);
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
function query(sql, params) {
|
|
50
|
-
sql_log(sql, params);
|
|
51
|
-
return new Promise((resolve, reject) => {
|
|
52
|
-
sqliteDatabase.all(sql, params, function (err, rows) {
|
|
53
|
-
if (err) {
|
|
54
|
-
reject(err);
|
|
55
|
-
} else {
|
|
56
|
-
resolve({ rows });
|
|
57
|
-
}
|
|
58
|
-
});
|
|
59
|
-
});
|
|
60
|
-
}
|
|
61
|
-
/**
|
|
62
|
-
* Change connection (close connection and open new connection from connObj)
|
|
63
|
-
* @param connObj - connection object
|
|
64
|
-
* @returns {Promise<void>}
|
|
65
|
-
*/
|
|
66
|
-
const changeConnection = async (connObj) => {
|
|
67
|
-
await sqliteDatabase.close();
|
|
68
|
-
current_filepath = connObj.sqlite_path;
|
|
69
|
-
sqliteDatabase = new sqlite3.Database(current_filepath);
|
|
70
|
-
};
|
|
71
|
-
/**
|
|
72
|
-
* Close database connection
|
|
73
|
-
* @returns {Promise<void>}
|
|
74
|
-
*/
|
|
75
|
-
const close = async () => {
|
|
76
|
-
await sqliteDatabase.close();
|
|
77
|
-
};
|
|
78
|
-
/**
|
|
79
|
-
* Execute Select statement
|
|
80
|
-
* @param tbl - table name
|
|
81
|
-
* @param whereObj - where object
|
|
82
|
-
* @param selectopts - select options
|
|
83
|
-
* @returns {Promise<*>} return rows
|
|
84
|
-
*/
|
|
85
|
-
|
|
86
|
-
const select = async (tbl, whereObj, selectopts = {}) => {
|
|
87
|
-
const { where, values } = mkWhere(whereObj, true);
|
|
88
|
-
const sql = `SELECT * FROM "${sqlsanitize(tbl)}" ${where} ${mkSelectOptions(
|
|
89
|
-
selectopts
|
|
90
|
-
)}`;
|
|
91
|
-
const tq = await query(sql, values);
|
|
92
|
-
|
|
93
|
-
return tq.rows;
|
|
94
|
-
};
|
|
95
|
-
/**
|
|
96
|
-
*
|
|
97
|
-
* @param v
|
|
98
|
-
* @returns {boolean}
|
|
99
|
-
*/
|
|
100
|
-
// TODO Utility function - needs ti be moved out this module
|
|
101
|
-
const reprAsJson = (v) =>
|
|
102
|
-
typeof v === "object" && v !== null && !(v instanceof Date);
|
|
103
|
-
const mkVal = ([k, v]) => (reprAsJson(v) ? JSON.stringify(v) : v);
|
|
104
|
-
|
|
105
|
-
/**
|
|
106
|
-
* Drop unique constraint
|
|
107
|
-
* @param tbl - table name
|
|
108
|
-
* @param obj - list of column=value pairs
|
|
109
|
-
* @param id - primary key column value
|
|
110
|
-
* @returns {Promise<void>} no results
|
|
111
|
-
*/
|
|
112
|
-
const update = async (tbl, obj, id) => {
|
|
113
|
-
const kvs = Object.entries(obj);
|
|
114
|
-
const assigns = kvs.map(([k, v], ix) => `"${sqlsanitize(k)}"=?`).join();
|
|
115
|
-
let valList = kvs.map(mkVal);
|
|
116
|
-
valList.push(id);
|
|
117
|
-
const q = `update "${sqlsanitize(tbl)}" set ${assigns} where id=?`;
|
|
118
|
-
await query(q, valList);
|
|
119
|
-
};
|
|
120
|
-
|
|
121
|
-
/**
|
|
122
|
-
* Delete rows in table
|
|
123
|
-
* @param tbl - table name
|
|
124
|
-
* @param whereObj - where object
|
|
125
|
-
* @returns {Promise<*>} result of delete execution
|
|
126
|
-
*/
|
|
127
|
-
const deleteWhere = async (tbl, whereObj) => {
|
|
128
|
-
const { where, values } = mkWhere(whereObj, true);
|
|
129
|
-
const sql = `delete FROM "${sqlsanitize(tbl)}" ${where}`;
|
|
130
|
-
|
|
131
|
-
const tq = await query(sql, values);
|
|
132
|
-
|
|
133
|
-
};
|
|
134
|
-
/**
|
|
135
|
-
* Insert rows into table
|
|
136
|
-
* @param tbl - table name
|
|
137
|
-
* @param obj - columns names and data
|
|
138
|
-
* @param opts - columns attributes
|
|
139
|
-
* @returns {Promise<*>} returns id.
|
|
140
|
-
*/
|
|
141
|
-
const insert = async (tbl, obj, opts = {}) => {
|
|
142
|
-
const kvs = Object.entries(obj);
|
|
143
|
-
const fnameList = kvs.map(([k, v]) => `"${sqlsanitize(k)}"`).join();
|
|
144
|
-
const valPosList = kvs
|
|
145
|
-
.map(([k, v], ix) =>
|
|
146
|
-
v && v.next_version_by_id
|
|
147
|
-
? `coalesce((select max(_version) from "${sqlsanitize(
|
|
148
|
-
tbl
|
|
149
|
-
)}" where id=${+v.next_version_by_id}), 0)+1`
|
|
150
|
-
: reprAsJson(v)
|
|
151
|
-
? "json(?)"
|
|
152
|
-
: "?"
|
|
153
|
-
)
|
|
154
|
-
.join();
|
|
155
|
-
const valList = kvs
|
|
156
|
-
.filter(([k, v]) => !(v && v.next_version_by_id))
|
|
157
|
-
.map(mkVal);
|
|
158
|
-
const sql = `insert into "${sqlsanitize(
|
|
159
|
-
tbl
|
|
160
|
-
)}"(${fnameList}) values(${valPosList})`;
|
|
161
|
-
|
|
162
|
-
await query(sql, valList);
|
|
163
|
-
if (opts.noid) return;
|
|
164
|
-
// TBD Support of primary key column different from id
|
|
165
|
-
const ids = await query("SELECT last_insert_rowid() as id");
|
|
166
|
-
return ids.rows[0].id;
|
|
167
|
-
};
|
|
168
|
-
|
|
169
|
-
/**
|
|
170
|
-
* Select one record
|
|
171
|
-
* @param tbl - table name
|
|
172
|
-
* @param where - where object
|
|
173
|
-
* @returns {Promise<*>} return first record from sql result
|
|
174
|
-
*/
|
|
175
|
-
const selectOne = async (tbl, where) => {
|
|
176
|
-
const rows = await select(tbl, where);
|
|
177
|
-
if (rows.length === 0) {
|
|
178
|
-
const w = mkWhere(where, true);
|
|
179
|
-
throw new Error(`no ${tbl} ${w.where} are ${w.values}`);
|
|
180
|
-
} else return rows[0];
|
|
181
|
-
};
|
|
182
|
-
|
|
183
|
-
/**
|
|
184
|
-
* Select one record or null if no records
|
|
185
|
-
* @param tbl - table name
|
|
186
|
-
* @param where - where object
|
|
187
|
-
* @returns {Promise<null|*>} - null if no record or first record data
|
|
188
|
-
*/
|
|
189
|
-
const selectMaybeOne = async (tbl, where) => {
|
|
190
|
-
const rows = await select(tbl, where);
|
|
191
|
-
if (rows.length === 0) return null;
|
|
192
|
-
else return rows[0];
|
|
193
|
-
};
|
|
194
|
-
|
|
195
|
-
/**
|
|
196
|
-
* Get count of rows in table
|
|
197
|
-
* @param tbl - table name
|
|
198
|
-
* @param whereObj - where object
|
|
199
|
-
* @returns {Promise<number>} count of tables
|
|
200
|
-
*/
|
|
201
|
-
const count = async (tbl, whereObj) => {
|
|
202
|
-
const { where, values } = mkWhere(whereObj, true);
|
|
203
|
-
const sql = `SELECT COUNT(*) FROM "${sqlsanitize(tbl)}" ${where}`;
|
|
204
|
-
const tq = await query(sql, values);
|
|
205
|
-
return parseInt(tq.rows[0]["COUNT(*)"]);
|
|
206
|
-
};
|
|
207
|
-
/**
|
|
208
|
-
* Get version of PostgreSQL
|
|
209
|
-
* @returns {Promise<*>} returns version
|
|
210
|
-
*/
|
|
211
|
-
const getVersion = async () => {
|
|
212
|
-
const sql = `SELECT sqlite_version();`;
|
|
213
|
-
sql_log(sql);
|
|
214
|
-
const tq = await query(sql);
|
|
215
|
-
return tq.rows[0]["sqlite_version()"];
|
|
216
|
-
};
|
|
217
|
-
|
|
218
|
-
/**
|
|
219
|
-
* Reset DB Schema using drop schema and recreate it.
|
|
220
|
-
* Attention! You will lost data after call this function!
|
|
221
|
-
* @returns {Promise<void>} no result
|
|
222
|
-
*/
|
|
223
|
-
const drop_reset_schema = async () => {
|
|
224
|
-
await sqliteDatabase.close();
|
|
225
|
-
await fs.unlink(current_filepath);
|
|
226
|
-
sqliteDatabase = new sqlite3.Database(current_filepath);
|
|
227
|
-
};
|
|
228
|
-
|
|
229
|
-
/**
|
|
230
|
-
* Add unique constraint
|
|
231
|
-
* @param table_name - table name
|
|
232
|
-
* @param field_names - list of columns (members of constraint)
|
|
233
|
-
* @returns {Promise<void>} no result
|
|
234
|
-
*/
|
|
235
|
-
const add_unique_constraint = async (table_name, field_names) => {
|
|
236
|
-
const sql = `create unique index ${sqlsanitize(
|
|
237
|
-
table_name
|
|
238
|
-
)}_${field_names
|
|
239
|
-
.map((f) => sqlsanitize(f))
|
|
240
|
-
.join("_")}_unique on "${sqlsanitize(table_name)}"(${field_names
|
|
241
|
-
.map((f) => `"${sqlsanitize(f)}"`)
|
|
242
|
-
.join(",")});`;
|
|
243
|
-
sql_log(sql);
|
|
244
|
-
await query(sql);
|
|
245
|
-
};
|
|
246
|
-
|
|
247
|
-
/**
|
|
248
|
-
* Drop unique constraint
|
|
249
|
-
* @param table_name - table name
|
|
250
|
-
* @param field_names - list of columns (members of constraint)
|
|
251
|
-
* @returns {Promise<void>} no results
|
|
252
|
-
*/
|
|
253
|
-
const drop_unique_constraint = async (table_name, field_names) => {
|
|
254
|
-
const sql = `drop index ${sqlsanitize(table_name)}_${field_names
|
|
255
|
-
.map((f) => sqlsanitize(f))
|
|
256
|
-
.join("_")}_unique;`;
|
|
257
|
-
sql_log(sql);
|
|
258
|
-
await query(sql);
|
|
259
|
-
};
|
|
260
|
-
|
|
261
|
-
module.exports = {
|
|
262
|
-
sql_log,
|
|
263
|
-
set_sql_logging,
|
|
264
|
-
get_sql_logging,
|
|
265
|
-
sqliteDatabase,
|
|
266
|
-
changeConnection,
|
|
267
|
-
query,
|
|
268
|
-
select,
|
|
269
|
-
selectOne,
|
|
270
|
-
selectMaybeOne,
|
|
271
|
-
insert,
|
|
272
|
-
count,
|
|
273
|
-
close,
|
|
274
|
-
drop_reset_schema,
|
|
275
|
-
update,
|
|
276
|
-
deleteWhere,
|
|
277
|
-
add_unique_constraint,
|
|
278
|
-
drop_unique_constraint,
|
|
279
|
-
getVersion,
|
|
280
|
-
};
|