meadow-connection-sqlite 1.0.12 → 1.0.14
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/README.md +14 -8
- package/docs/README.md +9 -0
- package/docs/{cover.md → _cover.md} +6 -2
- package/docs/_sidebar.md +19 -2
- package/docs/_topbar.md +4 -1
- package/docs/api/SQLite.md +55 -0
- package/docs/api/connect.md +66 -0
- package/docs/api/connectAsync.md +121 -0
- package/docs/api/createTable.md +96 -0
- package/docs/api/createTables.md +91 -0
- package/docs/api/db.md +145 -0
- package/docs/api/generateCreateTableStatement.md +109 -0
- package/docs/api/generateDropTableStatement.md +74 -0
- package/docs/api/preparedStatement.md +67 -0
- package/docs/api/reference.md +192 -0
- package/docs/api.md +13 -252
- package/docs/architecture.md +250 -0
- package/docs/quickstart.md +197 -0
- package/docs/retold-catalog.json +22 -1
- package/docs/retold-keyword-index.json +5818 -5
- package/docs/schema.md +198 -0
- package/package.json +6 -6
package/docs/api/db.md
ADDED
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
# db (getter)
|
|
2
|
+
|
|
3
|
+
Returns the underlying `better-sqlite3` Database instance for direct query access.
|
|
4
|
+
|
|
5
|
+
## Signature
|
|
6
|
+
|
|
7
|
+
```javascript
|
|
8
|
+
get db()
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Return Value
|
|
12
|
+
|
|
13
|
+
| Type | Description |
|
|
14
|
+
|------|-------------|
|
|
15
|
+
| `Database` | The better-sqlite3 Database instance (after `connectAsync()`) |
|
|
16
|
+
| `false` | Before connection |
|
|
17
|
+
|
|
18
|
+
## Primary Use
|
|
19
|
+
|
|
20
|
+
The `db` getter is the main entry point for all query operations. All queries go through better-sqlite3's synchronous API:
|
|
21
|
+
|
|
22
|
+
```javascript
|
|
23
|
+
let tmpDB = _Fable.MeadowSQLiteProvider.db;
|
|
24
|
+
|
|
25
|
+
// DDL — run raw SQL (no return value)
|
|
26
|
+
tmpDB.exec(`
|
|
27
|
+
CREATE TABLE IF NOT EXISTS Book (
|
|
28
|
+
IDBook INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
29
|
+
Title TEXT DEFAULT '',
|
|
30
|
+
Author TEXT DEFAULT ''
|
|
31
|
+
)
|
|
32
|
+
`);
|
|
33
|
+
|
|
34
|
+
// INSERT / UPDATE / DELETE — returns { changes, lastInsertRowid }
|
|
35
|
+
let tmpResult = tmpDB.prepare('INSERT INTO Book (Title, Author) VALUES (?, ?)').run('Dune', 'Frank Herbert');
|
|
36
|
+
console.log(tmpResult.lastInsertRowid); // => 1
|
|
37
|
+
console.log(tmpResult.changes); // => 1
|
|
38
|
+
|
|
39
|
+
// SELECT single row — returns object or undefined
|
|
40
|
+
let tmpBook = tmpDB.prepare('SELECT * FROM Book WHERE IDBook = ?').get(1);
|
|
41
|
+
console.log(tmpBook.Title); // => 'Dune'
|
|
42
|
+
|
|
43
|
+
// SELECT all rows — returns array of objects
|
|
44
|
+
let tmpBooks = tmpDB.prepare('SELECT * FROM Book').all();
|
|
45
|
+
console.log(tmpBooks.length); // => 1
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
## Prepared Statement Methods
|
|
49
|
+
|
|
50
|
+
| Method | Returns | Purpose |
|
|
51
|
+
|--------|---------|---------|
|
|
52
|
+
| `stmt.run(params...)` | `{ changes, lastInsertRowid }` | INSERT, UPDATE, DELETE |
|
|
53
|
+
| `stmt.get(params...)` | object or `undefined` | SELECT single row |
|
|
54
|
+
| `stmt.all(params...)` | array of objects | SELECT all matching rows |
|
|
55
|
+
|
|
56
|
+
## Named Parameters
|
|
57
|
+
|
|
58
|
+
better-sqlite3 supports named parameters prefixed with `@`, `$`, or `:`:
|
|
59
|
+
|
|
60
|
+
```javascript
|
|
61
|
+
let tmpStmt = tmpDB.prepare(
|
|
62
|
+
'SELECT * FROM Book WHERE Author = @author AND YearPublished > @year'
|
|
63
|
+
);
|
|
64
|
+
|
|
65
|
+
let tmpBooks = tmpStmt.all({ author: 'Isaac Asimov', year: 1950 });
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
## Transactions
|
|
69
|
+
|
|
70
|
+
Wrap a function in a transaction for atomic operations:
|
|
71
|
+
|
|
72
|
+
```javascript
|
|
73
|
+
let tmpBulkInsert = tmpDB.transaction(
|
|
74
|
+
(pBooks) =>
|
|
75
|
+
{
|
|
76
|
+
let tmpStmt = tmpDB.prepare('INSERT INTO Book (Title, Author) VALUES (?, ?)');
|
|
77
|
+
for (let tmpBook of pBooks)
|
|
78
|
+
{
|
|
79
|
+
tmpStmt.run(tmpBook.Title, tmpBook.Author);
|
|
80
|
+
}
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
tmpBulkInsert([
|
|
84
|
+
{ Title: 'Foundation', Author: 'Isaac Asimov' },
|
|
85
|
+
{ Title: 'Dune', Author: 'Frank Herbert' }
|
|
86
|
+
]);
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
If any statement inside the transaction throws, the entire transaction is rolled back automatically.
|
|
90
|
+
|
|
91
|
+
## Multi-Statement DDL
|
|
92
|
+
|
|
93
|
+
`exec()` runs one or more SQL statements without returning data:
|
|
94
|
+
|
|
95
|
+
```javascript
|
|
96
|
+
tmpDB.exec(`
|
|
97
|
+
CREATE TABLE IF NOT EXISTS Book (
|
|
98
|
+
IDBook INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
99
|
+
Title TEXT DEFAULT ''
|
|
100
|
+
);
|
|
101
|
+
CREATE INDEX IF NOT EXISTS idx_book_title ON Book(Title);
|
|
102
|
+
`);
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
## PRAGMA Commands
|
|
106
|
+
|
|
107
|
+
```javascript
|
|
108
|
+
tmpDB.pragma('journal_mode'); // => [{ journal_mode: 'wal' }]
|
|
109
|
+
tmpDB.pragma('table_info(Book)'); // => column info array
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
## Before Connection
|
|
113
|
+
|
|
114
|
+
Returns `false` before `connectAsync()` is called:
|
|
115
|
+
|
|
116
|
+
```javascript
|
|
117
|
+
let tmpDB = _Fable.MeadowSQLiteProvider.db;
|
|
118
|
+
// tmpDB => false (not connected yet)
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
Always check `connected` before accessing `db`:
|
|
122
|
+
|
|
123
|
+
```javascript
|
|
124
|
+
if (!_Fable.MeadowSQLiteProvider.connected)
|
|
125
|
+
{
|
|
126
|
+
console.error('Not connected to SQLite.');
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
let tmpDB = _Fable.MeadowSQLiteProvider.db;
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
## Closing the Database
|
|
134
|
+
|
|
135
|
+
```javascript
|
|
136
|
+
tmpDB.close();
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
After closing, further queries on this database instance will throw. The provider's `connected` property remains `true`.
|
|
140
|
+
|
|
141
|
+
## Related
|
|
142
|
+
|
|
143
|
+
- [connectAsync](connectAsync.md) -- Open the database connection
|
|
144
|
+
- [SQLite](SQLite.md) -- Access the better-sqlite3 module constructor
|
|
145
|
+
- [preparedStatement](preparedStatement.md) -- Alternative getter for the database handle
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
# generateCreateTableStatement(pMeadowTableSchema)
|
|
2
|
+
|
|
3
|
+
Generates a `CREATE TABLE IF NOT EXISTS` SQL statement from a Meadow table schema object. Returns the SQL string without executing it.
|
|
4
|
+
|
|
5
|
+
## Signature
|
|
6
|
+
|
|
7
|
+
```javascript
|
|
8
|
+
generateCreateTableStatement(pMeadowTableSchema)
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Parameters
|
|
12
|
+
|
|
13
|
+
| Parameter | Type | Description |
|
|
14
|
+
|-----------|------|-------------|
|
|
15
|
+
| `pMeadowTableSchema` | `object` | Meadow table schema with `TableName` and `Columns` array |
|
|
16
|
+
|
|
17
|
+
## Return Value
|
|
18
|
+
|
|
19
|
+
| Type | Description |
|
|
20
|
+
|------|-------------|
|
|
21
|
+
| `string` | A `CREATE TABLE IF NOT EXISTS` SQL statement |
|
|
22
|
+
|
|
23
|
+
## Schema Object Format
|
|
24
|
+
|
|
25
|
+
```javascript
|
|
26
|
+
let tmpSchema =
|
|
27
|
+
{
|
|
28
|
+
TableName: 'Book',
|
|
29
|
+
Columns:
|
|
30
|
+
[
|
|
31
|
+
{ Column: 'IDBook', DataType: 'ID' },
|
|
32
|
+
{ Column: 'GUIDBook', DataType: 'GUID' },
|
|
33
|
+
{ Column: 'Title', DataType: 'String', Size: '256' },
|
|
34
|
+
{ Column: 'Author', DataType: 'String', Size: '128' },
|
|
35
|
+
{ Column: 'YearPublished', DataType: 'Numeric' },
|
|
36
|
+
{ Column: 'Price', DataType: 'Decimal', Size: '10,2' },
|
|
37
|
+
{ Column: 'InPrint', DataType: 'Boolean' },
|
|
38
|
+
{ Column: 'CreateDate', DataType: 'DateTime' },
|
|
39
|
+
{ Column: 'UpdateDate', DataType: 'DateTime' }
|
|
40
|
+
]
|
|
41
|
+
};
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
Each column entry requires:
|
|
45
|
+
|
|
46
|
+
| Property | Type | Required | Description |
|
|
47
|
+
|----------|------|----------|-------------|
|
|
48
|
+
| `Column` | string | Yes | Column name |
|
|
49
|
+
| `DataType` | string | Yes | Meadow data type |
|
|
50
|
+
| `Size` | string | No | Size specification (ignored by SQLite) |
|
|
51
|
+
|
|
52
|
+
## Basic Usage
|
|
53
|
+
|
|
54
|
+
```javascript
|
|
55
|
+
let tmpDDL = _Fable.MeadowSQLiteProvider.generateCreateTableStatement(tmpSchema);
|
|
56
|
+
console.log(tmpDDL);
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
Output:
|
|
60
|
+
|
|
61
|
+
```sql
|
|
62
|
+
CREATE TABLE IF NOT EXISTS Book (IDBook INTEGER PRIMARY KEY AUTOINCREMENT, GUIDBook TEXT DEFAULT '00000000-0000-0000-0000-000000000000', Title TEXT NOT NULL DEFAULT '', Author TEXT NOT NULL DEFAULT '', YearPublished INTEGER NOT NULL DEFAULT 0, Price REAL, InPrint INTEGER NOT NULL DEFAULT 0, CreateDate TEXT, UpdateDate TEXT);
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
## Type Mapping
|
|
66
|
+
|
|
67
|
+
| Meadow DataType | SQLite Column Definition |
|
|
68
|
+
|-----------------|--------------------------|
|
|
69
|
+
| `ID` | `INTEGER PRIMARY KEY AUTOINCREMENT` |
|
|
70
|
+
| `GUID` | `TEXT DEFAULT '00000000-0000-0000-0000-000000000000'` |
|
|
71
|
+
| `ForeignKey` | `INTEGER NOT NULL DEFAULT 0` |
|
|
72
|
+
| `Numeric` | `INTEGER NOT NULL DEFAULT 0` |
|
|
73
|
+
| `Decimal` | `REAL` |
|
|
74
|
+
| `String` | `TEXT NOT NULL DEFAULT ''` |
|
|
75
|
+
| `Text` | `TEXT` |
|
|
76
|
+
| `DateTime` | `TEXT` |
|
|
77
|
+
| `Boolean` | `INTEGER NOT NULL DEFAULT 0` |
|
|
78
|
+
|
|
79
|
+
### Notes
|
|
80
|
+
|
|
81
|
+
- **Size is ignored** -- SQLite does not enforce `VARCHAR` length. The `Size` property is accepted for schema documentation but has no effect on the generated DDL.
|
|
82
|
+
- **`CREATE TABLE IF NOT EXISTS`** -- Always idempotent; safe to run repeatedly.
|
|
83
|
+
- **No column quoting** -- Column names are used as-is. SQLite does not require brackets or backticks.
|
|
84
|
+
|
|
85
|
+
## Comparison with MySQL
|
|
86
|
+
|
|
87
|
+
| Feature | MySQL | SQLite |
|
|
88
|
+
|---------|-------|--------|
|
|
89
|
+
| `ID` | `INT UNSIGNED NOT NULL AUTO_INCREMENT` + `PRIMARY KEY (col)` | `INTEGER PRIMARY KEY AUTOINCREMENT` |
|
|
90
|
+
| `String(256)` | `VARCHAR(256)` | `TEXT` (size ignored) |
|
|
91
|
+
| `Decimal(10,2)` | `DECIMAL(10,2)` | `REAL` |
|
|
92
|
+
| `Boolean` | `TINYINT NOT NULL DEFAULT 0` | `INTEGER NOT NULL DEFAULT 0` |
|
|
93
|
+
| Table quoting | `` `TableName` `` | `TableName` (unquoted) |
|
|
94
|
+
|
|
95
|
+
## Comparison with MSSQL
|
|
96
|
+
|
|
97
|
+
| Feature | MSSQL | SQLite |
|
|
98
|
+
|---------|-------|--------|
|
|
99
|
+
| `ID` | `INT IDENTITY(1,1)` + `PRIMARY KEY CLUSTERED ([col])` | `INTEGER PRIMARY KEY AUTOINCREMENT` |
|
|
100
|
+
| `String(256)` | `[col] NVARCHAR(256)` | `TEXT` |
|
|
101
|
+
| Schema prefix | `[dbo].[TableName]` | None |
|
|
102
|
+
| Column quoting | `[ColumnName]` | Unquoted |
|
|
103
|
+
|
|
104
|
+
## Related
|
|
105
|
+
|
|
106
|
+
- [createTable](createTable.md) -- Generate and execute the DDL
|
|
107
|
+
- [createTables](createTables.md) -- Create multiple tables at once
|
|
108
|
+
- [generateDropTableStatement](generateDropTableStatement.md) -- Generate a DROP TABLE statement
|
|
109
|
+
- [Schema & Table Creation](../schema.md) -- Full type mapping documentation
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
# generateDropTableStatement(pTableName)
|
|
2
|
+
|
|
3
|
+
Generates a `DROP TABLE IF EXISTS` SQL statement for the given table name.
|
|
4
|
+
|
|
5
|
+
## Signature
|
|
6
|
+
|
|
7
|
+
```javascript
|
|
8
|
+
generateDropTableStatement(pTableName)
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Parameters
|
|
12
|
+
|
|
13
|
+
| Parameter | Type | Description |
|
|
14
|
+
|-----------|------|-------------|
|
|
15
|
+
| `pTableName` | `string` | The name of the table to drop |
|
|
16
|
+
|
|
17
|
+
## Return Value
|
|
18
|
+
|
|
19
|
+
| Type | Description |
|
|
20
|
+
|------|-------------|
|
|
21
|
+
| `string` | A `DROP TABLE IF EXISTS` SQL statement |
|
|
22
|
+
|
|
23
|
+
## Basic Usage
|
|
24
|
+
|
|
25
|
+
```javascript
|
|
26
|
+
let tmpDropSQL = _Fable.MeadowSQLiteProvider.generateDropTableStatement('Book');
|
|
27
|
+
console.log(tmpDropSQL);
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
Output:
|
|
31
|
+
|
|
32
|
+
```sql
|
|
33
|
+
DROP TABLE IF EXISTS Book;
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
## Executing the Drop
|
|
37
|
+
|
|
38
|
+
The method only generates the SQL — it does not execute it. Use `db.exec()` to run it:
|
|
39
|
+
|
|
40
|
+
```javascript
|
|
41
|
+
let tmpDropSQL = _Fable.MeadowSQLiteProvider.generateDropTableStatement('Book');
|
|
42
|
+
_Fable.MeadowSQLiteProvider.db.exec(tmpDropSQL);
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## Idempotent
|
|
46
|
+
|
|
47
|
+
The generated statement uses `IF EXISTS`, so dropping a table that does not exist is a no-op:
|
|
48
|
+
|
|
49
|
+
```javascript
|
|
50
|
+
let tmpDropSQL = _Fable.MeadowSQLiteProvider.generateDropTableStatement('NonExistentTable');
|
|
51
|
+
_Fable.MeadowSQLiteProvider.db.exec(tmpDropSQL);
|
|
52
|
+
// No error — IF EXISTS handles it
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
## Comparison with MSSQL
|
|
56
|
+
|
|
57
|
+
| Feature | MSSQL | SQLite |
|
|
58
|
+
|---------|-------|--------|
|
|
59
|
+
| Syntax | `IF OBJECT_ID('dbo.Book', 'U') IS NOT NULL DROP TABLE [dbo].[Book]; GO` | `DROP TABLE IF EXISTS Book;` |
|
|
60
|
+
| Schema prefix | `[dbo].[TableName]` | None |
|
|
61
|
+
| Batch separator | `GO` | None needed |
|
|
62
|
+
|
|
63
|
+
## Comparison with MySQL
|
|
64
|
+
|
|
65
|
+
| Feature | MySQL | SQLite |
|
|
66
|
+
|---------|-------|--------|
|
|
67
|
+
| Syntax | `` DROP TABLE IF EXISTS `Book`; `` | `DROP TABLE IF EXISTS Book;` |
|
|
68
|
+
| Quoting | Backticks | None |
|
|
69
|
+
|
|
70
|
+
## Related
|
|
71
|
+
|
|
72
|
+
- [generateCreateTableStatement](generateCreateTableStatement.md) -- Generate CREATE TABLE DDL
|
|
73
|
+
- [createTable](createTable.md) -- Generate and execute CREATE TABLE
|
|
74
|
+
- [db](db.md) -- Execute the drop statement via `db.exec()`
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
# preparedStatement (getter)
|
|
2
|
+
|
|
3
|
+
Returns the `better-sqlite3` Database instance if connected. Throws an error if the provider is not connected.
|
|
4
|
+
|
|
5
|
+
## Signature
|
|
6
|
+
|
|
7
|
+
```javascript
|
|
8
|
+
get preparedStatement()
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Return Value
|
|
12
|
+
|
|
13
|
+
| Type | Description |
|
|
14
|
+
|------|-------------|
|
|
15
|
+
| `Database` | The better-sqlite3 Database instance (when connected) |
|
|
16
|
+
|
|
17
|
+
## Error Conditions
|
|
18
|
+
|
|
19
|
+
Throws an error if the database is not connected:
|
|
20
|
+
|
|
21
|
+
```
|
|
22
|
+
The Meadow SQLite provider could not create a prepared statement;
|
|
23
|
+
disconnected or no valid connection pool.
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## Why This Exists
|
|
27
|
+
|
|
28
|
+
The `preparedStatement` getter exists for API symmetry with the MSSQL connection provider, which returns a new `mssql.PreparedStatement` bound to the connection pool. In the SQLite provider, prepared statements are created directly on the Database instance via `db.prepare()`, so this getter simply returns the `db` handle itself.
|
|
29
|
+
|
|
30
|
+
This allows code that references `provider.preparedStatement` to work across different Meadow connection providers.
|
|
31
|
+
|
|
32
|
+
## Usage
|
|
33
|
+
|
|
34
|
+
```javascript
|
|
35
|
+
let tmpDB = _Fable.MeadowSQLiteProvider.preparedStatement;
|
|
36
|
+
|
|
37
|
+
// Create and use a prepared statement
|
|
38
|
+
let tmpStmt = tmpDB.prepare('SELECT * FROM Book WHERE IDBook = ?');
|
|
39
|
+
let tmpBook = tmpStmt.get(42);
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
## Comparison with db Getter
|
|
43
|
+
|
|
44
|
+
| Getter | Before Connection | After Connection |
|
|
45
|
+
|--------|-------------------|------------------|
|
|
46
|
+
| `db` | Returns `false` | Returns Database instance |
|
|
47
|
+
| `preparedStatement` | Throws Error | Returns Database instance |
|
|
48
|
+
|
|
49
|
+
Use `db` when you want to check for connection state gracefully. Use `preparedStatement` when you want a hard failure if the database is not connected.
|
|
50
|
+
|
|
51
|
+
## Comparison with MSSQL Provider
|
|
52
|
+
|
|
53
|
+
```javascript
|
|
54
|
+
// MSSQL — each access creates a new PreparedStatement
|
|
55
|
+
let tmpPS = _Fable.MeadowMSSQLProvider.preparedStatement;
|
|
56
|
+
tmpPS.input('id', _Fable.MeadowMSSQLProvider.MSSQL.Int);
|
|
57
|
+
tmpPS.prepare('SELECT * FROM Book WHERE IDBook = @id', ...);
|
|
58
|
+
|
|
59
|
+
// SQLite — returns the Database handle (prepare directly)
|
|
60
|
+
let tmpDB = _Fable.MeadowSQLiteProvider.preparedStatement;
|
|
61
|
+
let tmpStmt = tmpDB.prepare('SELECT * FROM Book WHERE IDBook = ?');
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
## Related
|
|
65
|
+
|
|
66
|
+
- [db](db.md) -- Primary database getter (returns `false` before connection)
|
|
67
|
+
- [connectAsync](connectAsync.md) -- Open the database connection first
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
# API Reference
|
|
2
|
+
|
|
3
|
+
Complete reference for `MeadowConnectionSQLite`, the SQLite database connection provider for the Meadow data layer.
|
|
4
|
+
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
## Class: MeadowConnectionSQLite
|
|
8
|
+
|
|
9
|
+
Extends `fable-serviceproviderbase`. Manages a connection to a SQLite database file through the [better-sqlite3](https://github.com/WiseLibs/better-sqlite3) library.
|
|
10
|
+
|
|
11
|
+
### Constructor
|
|
12
|
+
|
|
13
|
+
```javascript
|
|
14
|
+
new MeadowConnectionSQLite(pFable, pManifest, pServiceHash)
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
| Parameter | Type | Description |
|
|
18
|
+
|-----------|------|-------------|
|
|
19
|
+
| `pFable` | object | A Fable instance |
|
|
20
|
+
| `pManifest` | object | Service manifest / options (optional) |
|
|
21
|
+
| `pServiceHash` | string | Service identifier |
|
|
22
|
+
|
|
23
|
+
On construction:
|
|
24
|
+
|
|
25
|
+
- Sets `serviceType` to `'MeadowConnectionSQLite'`
|
|
26
|
+
- Sets `connected` to `false`
|
|
27
|
+
- Reads `SQLiteFilePath` from `fable.settings.SQLite` if available
|
|
28
|
+
|
|
29
|
+
The provider is not connected after construction — call `connectAsync()` to open the database.
|
|
30
|
+
|
|
31
|
+
---
|
|
32
|
+
|
|
33
|
+
## Properties
|
|
34
|
+
|
|
35
|
+
| Property | Type | Description |
|
|
36
|
+
|----------|------|-------------|
|
|
37
|
+
| [`connected`](#connected) | `boolean` | Whether the database connection is open |
|
|
38
|
+
| [`db`](db.md) | `Database \| false` | The raw better-sqlite3 Database instance |
|
|
39
|
+
| [`SQLite`](SQLite.md) | `function` | The better-sqlite3 module constructor |
|
|
40
|
+
| [`preparedStatement`](preparedStatement.md) | `Database \| Error` | Returns the Database instance if connected |
|
|
41
|
+
| [`serviceType`](#servicetype) | `string` | Always `'MeadowConnectionSQLite'` |
|
|
42
|
+
|
|
43
|
+
### connected
|
|
44
|
+
|
|
45
|
+
Whether the database connection is open.
|
|
46
|
+
|
|
47
|
+
**Type:** `boolean`
|
|
48
|
+
|
|
49
|
+
```javascript
|
|
50
|
+
if (_Fable.MeadowSQLiteProvider.connected)
|
|
51
|
+
{
|
|
52
|
+
// Safe to use db
|
|
53
|
+
}
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
### serviceType
|
|
57
|
+
|
|
58
|
+
Always `'MeadowConnectionSQLite'`.
|
|
59
|
+
|
|
60
|
+
**Type:** `string`
|
|
61
|
+
|
|
62
|
+
---
|
|
63
|
+
|
|
64
|
+
## Connection Methods
|
|
65
|
+
|
|
66
|
+
| Method | Description |
|
|
67
|
+
|--------|-------------|
|
|
68
|
+
| [`connectAsync(fCallback)`](connectAsync.md) | Open the database and enable WAL (recommended) |
|
|
69
|
+
| [`connect()`](connect.md) | Synchronous wrapper — logs a race-condition warning |
|
|
70
|
+
|
|
71
|
+
---
|
|
72
|
+
|
|
73
|
+
## DDL Methods
|
|
74
|
+
|
|
75
|
+
| Method | Description |
|
|
76
|
+
|--------|-------------|
|
|
77
|
+
| [`generateCreateTableStatement(pSchema)`](generateCreateTableStatement.md) | Generate a `CREATE TABLE IF NOT EXISTS` statement |
|
|
78
|
+
| [`createTable(pSchema, fCallback)`](createTable.md) | Generate and execute a CREATE TABLE statement |
|
|
79
|
+
| [`createTables(pSchema, fCallback)`](createTables.md) | Create multiple tables from a Meadow schema |
|
|
80
|
+
| [`generateDropTableStatement(pTableName)`](generateDropTableStatement.md) | Generate a `DROP TABLE IF EXISTS` statement |
|
|
81
|
+
|
|
82
|
+
---
|
|
83
|
+
|
|
84
|
+
## Configuration
|
|
85
|
+
|
|
86
|
+
### Fable Settings
|
|
87
|
+
|
|
88
|
+
```json
|
|
89
|
+
{
|
|
90
|
+
"SQLite":
|
|
91
|
+
{
|
|
92
|
+
"SQLiteFilePath": "./data/myapp.db"
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
| Setting | Type | Required | Description |
|
|
98
|
+
|---------|------|----------|-------------|
|
|
99
|
+
| `SQLiteFilePath` | string | Yes | File path or `:memory:` for in-memory databases |
|
|
100
|
+
|
|
101
|
+
### Constructor Options
|
|
102
|
+
|
|
103
|
+
Additional properties passed through to the `better-sqlite3` constructor:
|
|
104
|
+
|
|
105
|
+
| Option | Type | Default | Description |
|
|
106
|
+
|--------|------|---------|-------------|
|
|
107
|
+
| `readonly` | boolean | `false` | Open the database in read-only mode |
|
|
108
|
+
| `fileMustExist` | boolean | `false` | Error if the file does not exist |
|
|
109
|
+
| `timeout` | number | `5000` | Milliseconds to wait when the database is locked |
|
|
110
|
+
|
|
111
|
+
Constructor options override Fable settings, allowing multiple provider instances with different database files.
|
|
112
|
+
|
|
113
|
+
---
|
|
114
|
+
|
|
115
|
+
## Connection Behavior
|
|
116
|
+
|
|
117
|
+
### WAL Journal Mode
|
|
118
|
+
|
|
119
|
+
On successful connection, the provider automatically runs:
|
|
120
|
+
|
|
121
|
+
```sql
|
|
122
|
+
PRAGMA journal_mode = WAL;
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
Write-Ahead Logging provides significantly better concurrent read/write performance compared to SQLite's default rollback journal.
|
|
126
|
+
|
|
127
|
+
### File Creation
|
|
128
|
+
|
|
129
|
+
If the database file does not exist, better-sqlite3 creates it automatically. The directory must exist.
|
|
130
|
+
|
|
131
|
+
### In-Memory Databases
|
|
132
|
+
|
|
133
|
+
Set `SQLiteFilePath` to `":memory:"` for ephemeral databases:
|
|
134
|
+
|
|
135
|
+
```javascript
|
|
136
|
+
let _Fable = new libFable(
|
|
137
|
+
{
|
|
138
|
+
"SQLite": { "SQLiteFilePath": ":memory:" }
|
|
139
|
+
});
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
In-memory databases are discarded when the connection closes or the process exits.
|
|
143
|
+
|
|
144
|
+
---
|
|
145
|
+
|
|
146
|
+
## Column Type Mapping
|
|
147
|
+
|
|
148
|
+
| Meadow DataType | SQLite Column Definition |
|
|
149
|
+
|-----------------|--------------------------|
|
|
150
|
+
| `ID` | `INTEGER PRIMARY KEY AUTOINCREMENT` |
|
|
151
|
+
| `GUID` | `TEXT DEFAULT '00000000-0000-0000-0000-000000000000'` |
|
|
152
|
+
| `ForeignKey` | `INTEGER NOT NULL DEFAULT 0` |
|
|
153
|
+
| `Numeric` | `INTEGER NOT NULL DEFAULT 0` |
|
|
154
|
+
| `Decimal` | `REAL` |
|
|
155
|
+
| `String` | `TEXT NOT NULL DEFAULT ''` |
|
|
156
|
+
| `Text` | `TEXT` |
|
|
157
|
+
| `DateTime` | `TEXT` |
|
|
158
|
+
| `Boolean` | `INTEGER NOT NULL DEFAULT 0` |
|
|
159
|
+
|
|
160
|
+
See [Schema & Table Creation](../schema.md) for full details.
|
|
161
|
+
|
|
162
|
+
---
|
|
163
|
+
|
|
164
|
+
## Logging
|
|
165
|
+
|
|
166
|
+
The provider logs connection events through the Fable logging system:
|
|
167
|
+
|
|
168
|
+
| Event | Level | Message |
|
|
169
|
+
|-------|-------|---------|
|
|
170
|
+
| Connecting | `info` | `Meadow-Connection-SQLite connecting to file [path].` |
|
|
171
|
+
| Connected | `info` | `Meadow-Connection-SQLite successfully connected to SQLite file [path].` |
|
|
172
|
+
| Already connected | `error` | `...is already connected - skipping the second connect call.` |
|
|
173
|
+
| Missing path | `error` | `...database file path is invalid; SQLiteFilePath must be in either...` |
|
|
174
|
+
| Connection error | `error` | `...error connecting to SQLite file [path]: [error]` |
|
|
175
|
+
| No callback | `error` | `...connect() called without a callback...` |
|
|
176
|
+
|
|
177
|
+
---
|
|
178
|
+
|
|
179
|
+
## Known Limitations
|
|
180
|
+
|
|
181
|
+
- **No async queries** -- better-sqlite3 is synchronous by design. For CPU-bound workloads, consider running queries in a worker thread.
|
|
182
|
+
- **No connection pooling** -- Unlike MySQL/MSSQL, SQLite uses a single database handle. Concurrent access is managed by WAL journaling.
|
|
183
|
+
- **No size enforcement** -- SQLite ignores declared column sizes (e.g. `VARCHAR(256)` is treated as `TEXT`).
|
|
184
|
+
|
|
185
|
+
---
|
|
186
|
+
|
|
187
|
+
## Related
|
|
188
|
+
|
|
189
|
+
- [Quickstart Guide](../quickstart.md) -- Get running in five steps
|
|
190
|
+
- [Architecture & Design](../architecture.md) -- Lifecycle and data flow diagrams
|
|
191
|
+
- [Schema & Table Creation](../schema.md) -- Column type mapping and DDL generation
|
|
192
|
+
- [Full Pipeline Example](../examples-pipeline.md) -- End-to-end CRUD walkthrough
|