@v1nt1248/3nclient-lib 0.0.15 → 0.0.16

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.
@@ -1,5 +1,5 @@
1
- import { Database as DBClass, BindParams as QueryParams, QueryExecResult as QueryResult } from './sqljs.js';
2
- import { SingleProc, Action } from './synced.js';
1
+ import { Database as DBClass, BindParams as QueryParams, QueryExecResult as QueryResult } from './types';
2
+ import { SingleProc, Action } from './synced';
3
3
  export type Database = DBClass;
4
4
  export type BindParams = QueryParams;
5
5
  export type QueryExecResult = QueryResult;
@@ -0,0 +1,243 @@
1
+ export type SqlValue = number | string | Uint8Array | null;
2
+ export type ParamsObject = Record<string, SqlValue>;
3
+ export type ParamsCallback = (obj: ParamsObject) => void;
4
+ export type BindParams = SqlValue[] | ParamsObject | null;
5
+ export interface QueryExecResult {
6
+ columns: string[];
7
+ values: SqlValue[][];
8
+ }
9
+ export interface StatementIteratorResult {
10
+ /** `true` if there are no more available statements */
11
+ done: boolean;
12
+ /** the next available Statement (as returned by `Database.prepare`) */
13
+ value: Statement;
14
+ }
15
+ export interface SqlJsStatic {
16
+ Database: typeof Database;
17
+ Statement: typeof Statement;
18
+ }
19
+ export declare class Database {
20
+ /**
21
+ * Represents an SQLite database
22
+ * @see [https://sql.js.org/documentation/Database.html#Database](https://sql.js.org/documentation/Database.html#Database)
23
+ *
24
+ * @param data An array of bytes representing an SQLite database file
25
+ */
26
+ constructor(data?: ArrayLike<number> | null);
27
+ /**
28
+ * Close the database, and all associated prepared statements. The
29
+ * memory associated to the database and all associated statements will
30
+ * be freed.
31
+ *
32
+ * **Warning**: A statement belonging to a database that has been closed
33
+ * cannot be used anymore.
34
+ *
35
+ * Databases must be closed when you're finished with them, or the
36
+ * memory consumption will grow forever
37
+ * @see [https://sql.js.org/documentation/Database.html#["close"]](https://sql.js.org/documentation/Database.html#%5B%22close%22%5D)
38
+ */
39
+ close(): void;
40
+ /**
41
+ * Register a custom function with SQLite
42
+ * @see [https://sql.js.org/documentation/Database.html#["create_function"]](https://sql.js.org/documentation/Database.html#%5B%22create_function%22%5D)
43
+ *
44
+ * @param name the name of the function as referenced in SQL statements.
45
+ * @param func the actual function to be executed.
46
+ */
47
+ create_function(name: string, func: (...args: any[]) => any): Database;
48
+ /**
49
+ * Execute an sql statement, and call a callback for each row of result.
50
+ *
51
+ * Currently this method is synchronous, it will not return until the
52
+ * callback has been called on every row of the result. But this might
53
+ * change.
54
+ * @see [https://sql.js.org/documentation/Database.html#["each"]](https://sql.js.org/documentation/Database.html#%5B%22each%22%5D)
55
+ *
56
+ * @param sql A string of SQL text. Can contain placeholders that will
57
+ * be bound to the parameters given as the second argument
58
+ * @param params Parameters to bind to the query
59
+ * @param callback Function to call on each row of result
60
+ * @param done A function that will be called when all rows have been
61
+ * retrieved
62
+ */
63
+ each(sql: string, params: BindParams, callback: ParamsCallback, done: () => void): Database;
64
+ each(sql: string, callback: ParamsCallback, done: () => void): Database;
65
+ /**
66
+ * Execute an SQL query, and returns the result.
67
+ *
68
+ * This is a wrapper against `Database.prepare`, `Statement.bind`, `Statement.step`, `Statement.get`, and `Statement.free`.
69
+ *
70
+ * The result is an array of result elements. There are as many result elements as the number of statements in your sql string (statements are separated by a semicolon)
71
+ * @see [https://sql.js.org/documentation/Database.html#["exec"]](https://sql.js.org/documentation/Database.html#%5B%22exec%22%5D)
72
+ *
73
+ * @param sql a string containing some SQL text to execute
74
+ * @param params When the SQL statement contains placeholders, you can
75
+ * pass them in here. They will be bound to the statement before it is
76
+ * executed. If you use the params argument as an array, you **cannot**
77
+ * provide an sql string that contains several statements (separated by
78
+ * `;`). This limitation does not apply to params as an object.
79
+ */
80
+ exec(sql: string, params?: BindParams): QueryExecResult[];
81
+ /**
82
+ * Exports the contents of the database to a binary array
83
+ * @see [https://sql.js.org/documentation/Database.html#["export"]](https://sql.js.org/documentation/Database.html#%5B%22export%22%5D)
84
+ */
85
+ export(): Uint8Array;
86
+ /**
87
+ * Returns the number of changed rows (modified, inserted or deleted) by
88
+ * the latest completed `INSERT`, `UPDATE` or `DELETE` statement on the
89
+ * database. Executing any other type of SQL statement does not modify
90
+ * the value returned by this function.
91
+ * @see [https://sql.js.org/documentation/Database.html#["getRowsModified"]](https://sql.js.org/documentation/Database.html#%5B%22getRowsModified%22%5D)
92
+ */
93
+ getRowsModified(): number;
94
+ /**
95
+ * Analyze a result code, return null if no error occured, and throw an
96
+ * error with a descriptive message otherwise
97
+ * @see [https://sql.js.org/documentation/Database.html#["handleError"]](https://sql.js.org/documentation/Database.html#%5B%22handleError%22%5D)
98
+ */
99
+ handleError(): null | never;
100
+ /**
101
+ * Iterate over multiple SQL statements in a SQL string. This function
102
+ * returns an iterator over Statement objects. You can use a `for..of`
103
+ * loop to execute the returned statements one by one.
104
+ * @see [https://sql.js.org/documentation/Database.html#["iterateStatements"]](https://sql.js.org/documentation/Database.html#%5B%22iterateStatements%22%5D)
105
+ *
106
+ * @param sql a string of SQL that can contain multiple statements
107
+ */
108
+ iterateStatements(sql: string): StatementIterator;
109
+ /**
110
+ * Prepare an SQL statement
111
+ * @see [https://sql.js.org/documentation/Database.html#["prepare"]](https://sql.js.org/documentation/Database.html#%5B%22prepare%22%5D)
112
+ *
113
+ * @param sql a string of SQL, that can contain placeholders (`?`, `:VVV`, `:AAA`, `@AAA`)
114
+ * @param params values to bind to placeholders
115
+ */
116
+ prepare(sql: string, params?: BindParams): Statement;
117
+ /**
118
+ * Execute an SQL query, ignoring the rows it returns.
119
+ * @see [https://sql.js.org/documentation/Database.html#["run"]](https://sql.js.org/documentation/Database.html#%5B%22run%22%5D)
120
+ *
121
+ * @param sql a string containing some SQL text to execute
122
+ * @param params When the SQL statement contains placeholders, you can
123
+ * pass them in here. They will be bound to the statement before it is
124
+ * executed. If you use the params argument as an array, you **cannot**
125
+ * provide an sql string that contains several statements (separated by
126
+ * `;`). This limitation does not apply to params as an object.
127
+ */
128
+ run(sql: string, params?: BindParams): Database;
129
+ }
130
+ export declare class Statement {
131
+ /**
132
+ * Bind values to the parameters, after having reseted the statement. If
133
+ * values is null, do nothing and return true.
134
+ *
135
+ * SQL statements can have parameters, named '?', '?NNN', ':VVV',
136
+ * '@VVV', '$VVV', where NNN is a number and VVV a string. This function
137
+ * binds these parameters to the given values.
138
+ *
139
+ * Warning: ':', '@', and '$' are included in the parameters names
140
+ *
141
+ * ### Value types
142
+ *
143
+ * |Javascript type|SQLite type|
144
+ * |-|-|
145
+ * |number|REAL, INTEGER|
146
+ * |boolean|INTEGER|
147
+ * |string|TEXT|
148
+ * |Array, Uint8Array|BLOB|
149
+ * |null|NULL|
150
+ * @see [https://sql.js.org/documentation/Statement.html#["bind"]](https://sql.js.org/documentation/Statement.html#%5B%22bind%22%5D)
151
+ *
152
+ * @param values The values to bind
153
+ */
154
+ bind(values?: BindParams): boolean;
155
+ /**
156
+ * Free the memory used by the statement
157
+ * @see [https://sql.js.org/documentation/Statement.html#["free"]](https://sql.js.org/documentation/Statement.html#%5B%22free%22%5D)
158
+ */
159
+ free(): boolean;
160
+ /**
161
+ * Free the memory allocated during parameter binding
162
+ * @see [https://sql.js.org/documentation/Statement.html#["freemem"]](https://sql.js.org/documentation/Statement.html#%5B%22freemem%22%5D)
163
+ */
164
+ freemem(): void;
165
+ /**
166
+ * Get one row of results of a statement. If the first parameter is not
167
+ * provided, step must have been called before.
168
+ * @see [https://sql.js.org/documentation/Statement.html#["get"]](https://sql.js.org/documentation/Statement.html#%5B%22get%22%5D)
169
+ *
170
+ * @param params If set, the values will be bound to the statement
171
+ * before it is executed
172
+ */
173
+ get(params?: BindParams): SqlValue[];
174
+ /**
175
+ * Get one row of result as a javascript object, associating column
176
+ * names with their value in the current row
177
+ * @see [https://sql.js.org/documentation/Statement.html#["getAsObject"]](https://sql.js.org/documentation/Statement.html#%5B%22getAsObject%22%5D)
178
+ *
179
+ * @param params If set, the values will be bound to the statement, and
180
+ * it will be executed
181
+ */
182
+ getAsObject(params?: BindParams): ParamsObject;
183
+ /**
184
+ * Get the list of column names of a row of result of a statement.
185
+ * @see [https://sql.js.org/documentation/Statement.html#["getColumnNames"]](https://sql.js.org/documentation/Statement.html#%5B%22getColumnNames%22%5D)
186
+ */
187
+ getColumnNames(): string[];
188
+ /**
189
+ * Get the SQLite's normalized version of the SQL string used in
190
+ * preparing this statement. The meaning of "normalized" is not
191
+ * well-defined: see
192
+ * [the SQLite documentation](https://sqlite.org/c3ref/expanded_sql.html).
193
+ * @see [https://sql.js.org/documentation/Statement.html#["getNormalizedSQL"]](https://sql.js.org/documentation/Statement.html#%5B%22getNormalizedSQL%22%5D)
194
+ */
195
+ getNormalizedSQL(): string;
196
+ /**
197
+ * Get the SQL string used in preparing this statement.
198
+ * @see [https://sql.js.org/documentation/Statement.html#["getSQL"]](https://sql.js.org/documentation/Statement.html#%5B%22getSQL%22%5D)
199
+ */
200
+ getSQL(): string;
201
+ /**
202
+ * Reset a statement, so that it's parameters can be bound to new
203
+ * values. It also clears all previous bindings, freeing the memory used
204
+ * by bound parameters.
205
+ * @see [https://sql.js.org/documentation/Statement.html#["reset"]](https://sql.js.org/documentation/Statement.html#%5B%22reset%22%5D)
206
+ */
207
+ reset(): void;
208
+ /**
209
+ * Shorthand for bind + step + reset Bind the values, execute the
210
+ * statement, ignoring the rows it returns, and resets it
211
+ * @param values Value to bind to the statement
212
+ */
213
+ run(values?: BindParams): void;
214
+ /**
215
+ * Execute the statement, fetching the the next line of result, that can
216
+ * be retrieved with `Statement.get`.
217
+ * @see [https://sql.js.org/documentation/Statement.html#["step"]](https://sql.js.org/documentation/Statement.html#%5B%22step%22%5D)
218
+ */
219
+ step(): boolean;
220
+ }
221
+ /**
222
+ * An iterator over multiple SQL statements in a string, preparing and
223
+ * returning a Statement object for the next SQL statement on each
224
+ * iteration.
225
+ *
226
+ * You can't instantiate this class directly, you have to use a Database
227
+ * object in order to create a statement iterator
228
+ * @see [https://sql.js.org/documentation/StatementIterator.html#StatementIterator](https://sql.js.org/documentation/StatementIterator.html#StatementIterator)
229
+ */
230
+ export declare class StatementIterator implements Iterator<Statement>, Iterable<Statement> {
231
+ [Symbol.iterator](): Iterator<Statement>;
232
+ /**
233
+ * Get any un-executed portions remaining of the original SQL string
234
+ * @see [https://sql.js.org/documentation/StatementIterator.html#["getRemainingSQL"]](https://sql.js.org/documentation/StatementIterator.html#%5B%22getRemainingSQL%22%5D)
235
+ */
236
+ getRemainingSql(): string;
237
+ /**
238
+ * Prepare the next available SQL statement
239
+ * @see [https://sql.js.org/documentation/StatementIterator.html#["next"]](https://sql.js.org/documentation/StatementIterator.html#%5B%22next%22%5D)
240
+ */
241
+ next(): StatementIteratorResult;
242
+ }
243
+ export default function (keepWasm?: boolean): Promise<SqlJsStatic>;
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "private": false,
4
4
  "license": "AGPL-3.0-or-later",
5
5
  "author": "v1nt1248",
6
- "version": "0.0.15",
6
+ "version": "0.0.16",
7
7
  "description": "Library for 3NWeb clients",
8
8
  "type": "module",
9
9
  "files": [
@@ -14,8 +14,9 @@
14
14
  You should have received a copy of the GNU General Public License along with
15
15
  this program. If not, see <http://www.gnu.org/licenses/>.
16
16
  */
17
-
18
- import initSqlJs, { Database as DBClass, BindParams as QueryParams, QueryExecResult as QueryResult } from './sqljs';
17
+ // @ts-ignore
18
+ import initSqlJs from './sqljs';
19
+ import { Database as DBClass, BindParams as QueryParams, QueryExecResult as QueryResult } from './types'
19
20
  import { SingleProc, Action } from './synced';
20
21
 
21
22
  export type Database = DBClass;
@@ -30,7 +31,6 @@ export interface SaveOpts {
30
31
  skipUpload?: boolean;
31
32
  }
32
33
 
33
-
34
34
  export abstract class SQLiteOn3NStorage {
35
35
 
36
36
  protected readonly syncProc = new SingleProc();
@@ -41,9 +41,10 @@ export abstract class SQLiteOn3NStorage {
41
41
  ) {}
42
42
 
43
43
  static async makeAndStart(file: WritableFile): Promise<SQLiteOn3NStorage> {
44
+ // @ts-ignore
44
45
  const SQL = await initSqlJs(true);
45
46
  const fileContent = await readFileContent(file);
46
- const db = new SQL.Database(fileContent);
47
+ const db = new SQL.Database(fileContent) as Database;
47
48
  let sqlite: SQLiteOn3NStorage;
48
49
  if (file.v?.sync) {
49
50
  sqlite = new SQLiteOnSyncedFS(db, file);