@ix-xs/node-comfort 1.0.53 → 1.0.55
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/package.json +1 -1
- package/src/localStorage.js +84 -54
package/package.json
CHANGED
package/src/localStorage.js
CHANGED
|
@@ -3,8 +3,10 @@ const checker = require("./checker");
|
|
|
3
3
|
|
|
4
4
|
const originalEmitWarning = process.emitWarning;
|
|
5
5
|
process.emitWarning = (warning, ...args) => {
|
|
6
|
-
if (
|
|
7
|
-
|
|
6
|
+
if (
|
|
7
|
+
(warning.name === "ExperimentalWarning" && warning.message.includes("SQLite")) ||
|
|
8
|
+
(typeof warning === "string" && warning.startsWith("SQLite is an experimental feature"))
|
|
9
|
+
) {
|
|
8
10
|
return;
|
|
9
11
|
}
|
|
10
12
|
return originalEmitWarning.call(process, warning, ...args);
|
|
@@ -32,7 +34,10 @@ module.exports = class localStorage {
|
|
|
32
34
|
#exec(sql, options = []) {
|
|
33
35
|
try {
|
|
34
36
|
const stmt = this.#$.prepare(sql);
|
|
35
|
-
const result =
|
|
37
|
+
const result = checker.isArray(options)
|
|
38
|
+
? stmt.run(...options)
|
|
39
|
+
: stmt.run(options);
|
|
40
|
+
|
|
36
41
|
return {
|
|
37
42
|
changes: result.changes,
|
|
38
43
|
lastInsertRowid: result.lastInsertRowid,
|
|
@@ -41,26 +46,32 @@ module.exports = class localStorage {
|
|
|
41
46
|
throw error;
|
|
42
47
|
}
|
|
43
48
|
}
|
|
49
|
+
|
|
44
50
|
#buildCreateSQL(tableName, options) {
|
|
45
|
-
const columns = Object.entries(options)
|
|
46
|
-
|
|
51
|
+
const columns = Object.entries(options)
|
|
52
|
+
.map(([col, def]) => {
|
|
53
|
+
let colDef = `${col} ${def.type}`;
|
|
47
54
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
55
|
+
if (def.primaryKey) {
|
|
56
|
+
colDef += " PRIMARY KEY";
|
|
57
|
+
if (def.autoincrement) colDef += " AUTOINCREMENT";
|
|
58
|
+
}
|
|
59
|
+
if (def.notNull) colDef += " NOT NULL";
|
|
60
|
+
if (def.unique) colDef += " UNIQUE";
|
|
61
|
+
if (def.defaultValue !== undefined) {
|
|
62
|
+
colDef += ` DEFAULT ${JSON.stringify(def.defaultValue)}`;
|
|
63
|
+
}
|
|
64
|
+
if (def.values?.length) {
|
|
65
|
+
colDef += ` CHECK(${col} IN (${def.values.map(v => JSON.stringify(v)).join(", ")}))`;
|
|
66
|
+
}
|
|
58
67
|
|
|
59
|
-
|
|
60
|
-
|
|
68
|
+
return colDef;
|
|
69
|
+
})
|
|
70
|
+
.join(", ");
|
|
61
71
|
|
|
62
72
|
return `CREATE TABLE IF NOT EXISTS ${tableName} (${columns})`;
|
|
63
73
|
}
|
|
74
|
+
|
|
64
75
|
#defineModel(tableName, options) {
|
|
65
76
|
if (this.#models.has(tableName)) return;
|
|
66
77
|
|
|
@@ -68,8 +79,9 @@ module.exports = class localStorage {
|
|
|
68
79
|
this.#exec(createSQL);
|
|
69
80
|
this.#models.set(tableName, { name: tableName, options });
|
|
70
81
|
}
|
|
71
|
-
|
|
72
|
-
|
|
82
|
+
|
|
83
|
+
#buildWhere(where = {}) {
|
|
84
|
+
if (!where || Object.keys(where).length === 0) {
|
|
73
85
|
return { where: "", params: [] };
|
|
74
86
|
}
|
|
75
87
|
|
|
@@ -86,6 +98,7 @@ module.exports = class localStorage {
|
|
|
86
98
|
params,
|
|
87
99
|
};
|
|
88
100
|
}
|
|
101
|
+
|
|
89
102
|
#getOrderBy(tableName) {
|
|
90
103
|
const model = this.#models.get(tableName);
|
|
91
104
|
if (!model) return "id DESC";
|
|
@@ -94,15 +107,17 @@ module.exports = class localStorage {
|
|
|
94
107
|
}
|
|
95
108
|
|
|
96
109
|
/**
|
|
97
|
-
* @param {
|
|
98
|
-
*
|
|
110
|
+
* @param {object} table
|
|
111
|
+
* @param {string} table.name
|
|
112
|
+
* @param {Record<string, {
|
|
113
|
+
* type: "INTEGER"|"TEXT"|"REAL"|"BLOB"|"NUMERIC",
|
|
99
114
|
* primaryKey?: boolean,
|
|
100
|
-
* unique?: boolean,
|
|
101
|
-
* notNull: boolean,
|
|
102
|
-
* defaultValue?: any,
|
|
103
|
-
* values?: Array<string>
|
|
104
115
|
* autoincrement?: boolean,
|
|
105
|
-
*
|
|
116
|
+
* notNull?: boolean,
|
|
117
|
+
* unique?: boolean,
|
|
118
|
+
* defaultValue?: *,
|
|
119
|
+
* values?: Array<*>
|
|
120
|
+
* }>} table.options
|
|
106
121
|
*/
|
|
107
122
|
async createTable(table) {
|
|
108
123
|
try {
|
|
@@ -112,12 +127,15 @@ module.exports = class localStorage {
|
|
|
112
127
|
return { error: error.message };
|
|
113
128
|
}
|
|
114
129
|
}
|
|
130
|
+
|
|
115
131
|
/**
|
|
116
132
|
* @param {string} tableName
|
|
117
133
|
*/
|
|
118
134
|
async deleteTable(tableName) {
|
|
119
135
|
try {
|
|
120
|
-
if (!this.#models.has(tableName))
|
|
136
|
+
if (!this.#models.has(tableName)) {
|
|
137
|
+
return { error: `Table ${tableName} doesn't exist` };
|
|
138
|
+
}
|
|
121
139
|
|
|
122
140
|
this.#exec(`DROP TABLE IF EXISTS ${tableName}`);
|
|
123
141
|
this.#models.delete(tableName);
|
|
@@ -126,12 +144,15 @@ module.exports = class localStorage {
|
|
|
126
144
|
return { error: error.message };
|
|
127
145
|
}
|
|
128
146
|
}
|
|
147
|
+
|
|
129
148
|
/**
|
|
130
149
|
* @param {string} tableName
|
|
131
150
|
*/
|
|
132
151
|
async clearTable(tableName) {
|
|
133
152
|
try {
|
|
134
|
-
if (!this.#models.has(tableName))
|
|
153
|
+
if (!this.#models.has(tableName)) {
|
|
154
|
+
return { error: `Table ${tableName} doesn't exist` };
|
|
155
|
+
}
|
|
135
156
|
|
|
136
157
|
this.#exec(`DELETE FROM ${tableName}`);
|
|
137
158
|
return { ok: true };
|
|
@@ -139,9 +160,10 @@ module.exports = class localStorage {
|
|
|
139
160
|
return { error: error.message };
|
|
140
161
|
}
|
|
141
162
|
}
|
|
163
|
+
|
|
142
164
|
/**
|
|
143
165
|
* @param {string} tableName
|
|
144
|
-
* @param {object}
|
|
166
|
+
* @param {object} where
|
|
145
167
|
*/
|
|
146
168
|
async get(tableName, where) {
|
|
147
169
|
try {
|
|
@@ -150,24 +172,28 @@ module.exports = class localStorage {
|
|
|
150
172
|
const whereClause = this.#buildWhere(where);
|
|
151
173
|
const sql = `SELECT * FROM ${tableName} ${whereClause.where} LIMIT 1`;
|
|
152
174
|
const stmt = this.#$.prepare(sql);
|
|
153
|
-
const record = stmt.get(whereClause.params);
|
|
175
|
+
const record = stmt.get(...whereClause.params);
|
|
176
|
+
|
|
154
177
|
return record || undefined;
|
|
155
178
|
} catch (error) {
|
|
156
179
|
return { error: error.message };
|
|
157
180
|
}
|
|
158
181
|
}
|
|
182
|
+
|
|
159
183
|
/**
|
|
160
184
|
* @param {string} tableName
|
|
161
|
-
* @param {object}
|
|
162
|
-
* @param {number}
|
|
163
|
-
* @param {number}
|
|
185
|
+
* @param {object} where
|
|
186
|
+
* @param {number} limit
|
|
187
|
+
* @param {number} offset
|
|
164
188
|
*/
|
|
165
189
|
async getAll(tableName, where = {}, limit = 50, offset = 0) {
|
|
166
190
|
try {
|
|
167
191
|
if (!this.#models.has(tableName)) return [];
|
|
168
192
|
|
|
169
193
|
const whereClause = this.#buildWhere(where);
|
|
170
|
-
const sql = `SELECT * FROM ${tableName} ${whereClause.where} ORDER BY ${this.#getOrderBy(
|
|
194
|
+
const sql = `SELECT * FROM ${tableName} ${whereClause.where} ORDER BY ${this.#getOrderBy(
|
|
195
|
+
tableName,
|
|
196
|
+
)} LIMIT ? OFFSET ?`;
|
|
171
197
|
const stmt = this.#$.prepare(sql);
|
|
172
198
|
const records = stmt.all(...whereClause.params, limit, offset);
|
|
173
199
|
return records;
|
|
@@ -175,43 +201,44 @@ module.exports = class localStorage {
|
|
|
175
201
|
return { error: error.message };
|
|
176
202
|
}
|
|
177
203
|
}
|
|
204
|
+
|
|
178
205
|
/**
|
|
179
206
|
* @param {string} tableName
|
|
180
207
|
* @param {*} data
|
|
181
|
-
* @param {object}
|
|
208
|
+
* @param {object} where
|
|
182
209
|
*/
|
|
183
210
|
async set(tableName, data, where = null) {
|
|
184
211
|
try {
|
|
185
|
-
if (!this.#models.has(tableName))
|
|
212
|
+
if (!this.#models.has(tableName)) {
|
|
213
|
+
return { error: `Table ${tableName} doesn't exist` };
|
|
214
|
+
}
|
|
186
215
|
|
|
187
216
|
const lookup = where ?? (data.id ? { id: data.id } : null);
|
|
188
217
|
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
const sql = `INSERT INTO ${tableName} (${columns}) VALUES (${placeholders})`;
|
|
200
|
-
const stmt = this.#$.prepare(sql);
|
|
201
|
-
stmt.run(...Object.values(data));
|
|
202
|
-
return { ok: true };
|
|
203
|
-
}
|
|
218
|
+
const columns = Object.keys(data).join(", ");
|
|
219
|
+
const placeholders = Object.keys(data).map(() => "?").join(", ");
|
|
220
|
+
const sql = lookup
|
|
221
|
+
? `INSERT OR REPLACE INTO ${tableName} (${columns}) VALUES (${placeholders})`
|
|
222
|
+
: `INSERT INTO ${tableName} (${columns}) VALUES (${placeholders})`;
|
|
223
|
+
|
|
224
|
+
const stmt = this.#$.prepare(sql);
|
|
225
|
+
stmt.run(...Object.values(data));
|
|
226
|
+
|
|
227
|
+
return { ok: true };
|
|
204
228
|
} catch (error) {
|
|
205
229
|
return { error: error.message };
|
|
206
230
|
}
|
|
207
231
|
}
|
|
232
|
+
|
|
208
233
|
/**
|
|
209
234
|
* @param {string} tableName
|
|
210
235
|
* @param {object} where
|
|
211
236
|
*/
|
|
212
237
|
async delete(tableName, where) {
|
|
213
238
|
try {
|
|
214
|
-
if (!this.#models.has(tableName))
|
|
239
|
+
if (!this.#models.has(tableName)) {
|
|
240
|
+
return { error: `Table ${tableName} doesn't exist` };
|
|
241
|
+
}
|
|
215
242
|
|
|
216
243
|
const whereClause = this.#buildWhere(where);
|
|
217
244
|
const sql = `DELETE FROM ${tableName} ${whereClause.where}`;
|
|
@@ -222,13 +249,16 @@ module.exports = class localStorage {
|
|
|
222
249
|
return { error: error.message };
|
|
223
250
|
}
|
|
224
251
|
}
|
|
252
|
+
|
|
225
253
|
/**
|
|
226
254
|
* @param {string} tableName
|
|
227
|
-
* @param {object} where
|
|
255
|
+
* @param {object} [where]
|
|
228
256
|
*/
|
|
229
257
|
async count(tableName, where = {}) {
|
|
230
258
|
try {
|
|
231
|
-
if (!this.#models.has(tableName))
|
|
259
|
+
if (!this.#models.has(tableName)) {
|
|
260
|
+
return { error: `Table ${tableName} doesn't exist` };
|
|
261
|
+
}
|
|
232
262
|
|
|
233
263
|
const whereClause = this.#buildWhere(where);
|
|
234
264
|
const sql = `SELECT COUNT(*) as total FROM ${tableName} ${whereClause.where}`;
|