Skip to content

Commit 658658d

Browse files
[mssql] Document MsSql persister
1 parent 6b985e5 commit 658658d

4 files changed

Lines changed: 72 additions & 2 deletions

File tree

site/guides/07_persistence/1_an_intro_to_persistence.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ is available.
5555
| ElectricSqlPersister | Electric SQL, via [electric](https://github.com/electric-sql/electric) |
5656
| LibSqlPersister | LibSQL for Turso, via [libsql-client](https://github.com/tursodatabase/libsql-client-ts) |
5757
| PowerSyncPersister | PowerSync, via [powersync-sdk](https://github.com/powersync-ja/powersync-js) |
58+
| MsSqlPersister | SQL Server and Azure SQL, via [mssql](https://github.com/tediousjs/node-mssql) |
5859
| PgPersister | PostgreSQL, via [pg](https://github.com/brianc/node-postgres) |
5960
| PostgresPersister | PostgreSQL, via [postgres](https://github.com/porsager/postgres) |
6061
| PglitePersister | PostgreSQL, via [PGlite](https://github.com/electric-sql/pglite) |

site/guides/07_persistence/2_database_persistence.md

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
Since v4.0, there are various options for persisting Store data to and from
44
SQLite databases, via a range of third-party modules.
55

6-
There are currently twelve SQLite-based persistence options, and four for
7-
PostgreSQL:
6+
There are currently twelve SQLite-based persistence options, four for
7+
PostgreSQL, and one for SQL Server:
88

99
| Persister | Storage |
1010
| -------------------------- | ---------------------------------------------------------------------------------------------------------------- |
@@ -20,6 +20,7 @@ PostgreSQL:
2020
| ElectricSqlPersister | Electric SQL, via [electric](https://github.com/electric-sql/electric) |
2121
| LibSqlPersister | LibSQL for Turso, via [libsql-client](https://github.com/tursodatabase/libsql-client-ts) |
2222
| PowerSyncPersister | PowerSync, via [powersync-sdk](https://github.com/powersync-ja/powersync-js) |
23+
| MsSqlPersister | SQL Server and Azure SQL, via [mssql](https://github.com/tediousjs/node-mssql) |
2324
| PgPersister | PostgreSQL, via [pg](https://github.com/brianc/node-postgres) |
2425
| PostgresPersister | PostgreSQL, via [postgres](https://github.com/porsager/postgres) |
2526
| PglitePersister | PostgreSQL, via [PGlite](https://github.com/electric-sql/pglite) |
@@ -39,6 +40,21 @@ and `Client` objects can be passed straight to the createPgPersister function,
3940
which means you can persist a Store to PostgreSQL from an edge runtime that
4041
cannot open a regular TCP connection.
4142

43+
The MsSqlPersister covers the SQL Server family, and since Azure SQL Database
44+
and Azure SQL Managed Instance both speak the same protocol, the same
45+
createMsSqlPersister function works against all three. It takes a `mssql`
46+
connection pool that you have configured yourself, so the passwordless
47+
authentication that Microsoft recommends for applications hosted in Azure is a
48+
matter of how you build that pool rather than anything TinyBase needs to know
49+
about. Only the JSON mode described below is available to it so far.
50+
51+
It also differs in how it notices changes made by other writers. The PostgreSQL
52+
Persisters use LISTEN and NOTIFY, which has no equivalent that works across
53+
every flavor of SQL Server, so the MsSqlPersister adds a `rowversion` column to
54+
its table and polls that instead. SQL Server maintains that column itself on
55+
every insert and update, which keeps external changes detectable without
56+
requiring Service Broker, Change Tracking, or triggers.
57+
4258
The SupabasePersister is the odd one out, since it talks to Supabase's REST API
4359
rather than to the database directly. That means it runs in a browser or edge
4460
runtime, that row-level security policies apply to what it reads and writes,

site/guides/08_synchronization/1_using_a_mergeablestore.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ MergeableStore, but _only_ in the 'JSON-serialization' mode:
126126
| ExpoSqlitePersister | SQLite in React Native, via [expo-sqlite](https://github.com/expo/expo/tree/main/packages/expo-sqlite) |
127127
| ReactNativeSqlitePersister | SQLite in React Native, via [react-native-sqlite-storage](https://github.com/andpor/react-native-sqlite-storage) |
128128
| CapacitorSqlitePersister | SQLite in Capacitor, via [capacitor-sqlite](https://github.com/capacitor-community/sqlite) |
129+
| MsSqlPersister | SQL Server and Azure SQL, via [mssql](https://github.com/tediousjs/node-mssql) |
129130
| PgPersister | PostgreSQL, via [pg](https://github.com/brianc/node-postgres) |
130131
| PostgresPersister | PostgreSQL, via [postgres](https://github.com/porsager/postgres) |
131132
| PglitePersister | PostgreSQL, via [PGlite](https://github.com/electric-sql/pglite) |

site/guides/20_releases.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,58 @@ highlighted features.
55

66
---
77

8+
# v9.8
9+
10+
## SQL Server and Azure SQL, via `mssql`
11+
12+
The new persister-mssql module provides the MsSqlPersister, which binds to a
13+
SQL Server database with the
14+
[`mssql`](https://github.com/tediousjs/node-mssql) module. Since Azure SQL
15+
Database and Azure SQL Managed Instance speak the same protocol, the same
16+
Persister works against all three:
17+
18+
```js
19+
import {connect} from 'mssql';
20+
import {createStore} from 'tinybase';
21+
import {createMsSqlPersister} from 'tinybase/persisters/persister-mssql';
22+
23+
const msSqlPool = await connect(process.env.TINYBASE_MSSQL);
24+
const msSqlStore = createStore().setTables({pets: {fido: {species: 'dog'}}});
25+
const msSqlPersister = await createMsSqlPersister(
26+
msSqlStore,
27+
msSqlPool,
28+
'my_tinybase',
29+
);
30+
31+
await msSqlPersister.save();
32+
console.log(
33+
(await msSqlPool.request().query('SELECT * FROM my_tinybase;')).recordset,
34+
);
35+
// -> [{_id: '_', store: '[{"pets":{"fido":{"species":"dog"}}},{}]'}]
36+
37+
await msSqlPersister.destroy();
38+
await msSqlPool.request().query('DROP TABLE IF EXISTS my_tinybase;');
39+
await msSqlPool.close();
40+
```
41+
42+
The Persister takes a connection pool that you have already configured, so it
43+
stays out of the way of how you authenticate. That matters most on Azure, where
44+
Microsoft recommends passwordless access for hosted applications: build the
45+
pool with an `azure-active-directory-default` authentication type and the
46+
Persister needs to know nothing about it.
47+
48+
This release supports the JSON serialization mode, for both a Store and a
49+
MergeableStore. Tabular mapping may follow.
50+
51+
Automatic loading works differently here than it does for PostgreSQL. There is
52+
no equivalent of LISTEN and NOTIFY that is available on every flavor of SQL
53+
Server, so the Persister adds a `rowversion` column to its table and polls it.
54+
SQL Server maintains that column itself on every insert and update, so changes
55+
made by other writers are still picked up, without needing Service Broker,
56+
Change Tracking, or triggers.
57+
58+
---
59+
860
# v9.7
961

1062
## SQLite, via `node:sqlite`

0 commit comments

Comments
 (0)