@sqlitecloud/drivers
Advanced tools
Comparing version
@@ -205,4 +205,4 @@ "use strict"; | ||
this.processCallback(error, result); | ||
// this.processCallback = undefined | ||
} | ||
this.buffer = Buffer.alloc(0); | ||
} | ||
@@ -209,0 +209,0 @@ /** Disconnect immediately, release connection, no events. */ |
@@ -5,2 +5,3 @@ import { SQLiteCloudConfig, RowCountCallback } from './types'; | ||
import EventEmitter from 'eventemitter3'; | ||
import { PubSub } from './pubsub'; | ||
/** | ||
@@ -155,2 +156,11 @@ * Creating a Database object automatically opens a connection to the SQLite database. | ||
sql(sql: TemplateStringsArray | string, ...values: any[]): Promise<any>; | ||
/** | ||
* PubSub class provides a Pub/Sub real-time updates and notifications system to | ||
* allow multiple applications to communicate with each other asynchronously. | ||
* It allows applications to subscribe to tables and receive notifications whenever | ||
* data changes in the database table. It also enables sending messages to anyone | ||
* subscribed to a specific channel. | ||
* @returns {PubSub} A PubSub object | ||
*/ | ||
getPubSub(): Promise<PubSub>; | ||
} |
@@ -48,2 +48,3 @@ "use strict"; | ||
const utilities_2 = require("./utilities"); | ||
const pubsub_1 = require("./pubsub"); | ||
// Uses eventemitter3 instead of node events for browser compatibility | ||
@@ -447,3 +448,25 @@ // https://github.com/primus/eventemitter3 | ||
} | ||
/** | ||
* PubSub class provides a Pub/Sub real-time updates and notifications system to | ||
* allow multiple applications to communicate with each other asynchronously. | ||
* It allows applications to subscribe to tables and receive notifications whenever | ||
* data changes in the database table. It also enables sending messages to anyone | ||
* subscribed to a specific channel. | ||
* @returns {PubSub} A PubSub object | ||
*/ | ||
getPubSub() { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
return new Promise((resolve, reject) => { | ||
this.getConnection((error, connection) => { | ||
if (error || !connection) { | ||
reject(error); | ||
} | ||
else { | ||
resolve(new pubsub_1.PubSub(connection)); | ||
} | ||
}); | ||
}); | ||
}); | ||
} | ||
} | ||
exports.Database = Database; |
@@ -17,2 +17,3 @@ /// <reference types="node" /> | ||
export declare const CMD_ARRAY = "="; | ||
export declare const CMD_PUBSUB = "|"; | ||
export declare const ROWSET_CHUNKS_END = "/6 0 0 0 "; | ||
@@ -19,0 +20,0 @@ /** Analyze first character to check if corresponding data type has LEN */ |
@@ -6,3 +6,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.formatCommand = exports.popData = exports.parseRowsetChunks = exports.bufferEndsWith = exports.bufferStartsWith = exports.parseRowsetHeader = exports.parseArray = exports.parseError = exports.decompressBuffer = exports.parseCommandLength = exports.hasCommandLength = exports.ROWSET_CHUNKS_END = exports.CMD_ARRAY = exports.CMD_COMMAND = exports.CMD_COMPRESSED = exports.CMD_BLOB = exports.CMD_NULL = exports.CMD_JSON = exports.CMD_ROWSET_CHUNK = exports.CMD_ROWSET = exports.CMD_FLOAT = exports.CMD_INT = exports.CMD_ERROR = exports.CMD_ZEROSTRING = exports.CMD_STRING = void 0; | ||
exports.formatCommand = exports.popData = exports.parseRowsetChunks = exports.bufferEndsWith = exports.bufferStartsWith = exports.parseRowsetHeader = exports.parseArray = exports.parseError = exports.decompressBuffer = exports.parseCommandLength = exports.hasCommandLength = exports.ROWSET_CHUNKS_END = exports.CMD_PUBSUB = exports.CMD_ARRAY = exports.CMD_COMMAND = exports.CMD_COMPRESSED = exports.CMD_BLOB = exports.CMD_NULL = exports.CMD_JSON = exports.CMD_ROWSET_CHUNK = exports.CMD_ROWSET = exports.CMD_FLOAT = exports.CMD_INT = exports.CMD_ERROR = exports.CMD_ZEROSTRING = exports.CMD_STRING = void 0; | ||
const types_1 = require("./types"); | ||
@@ -28,3 +28,3 @@ const rowset_1 = require("./rowset"); | ||
// const CMD_RAWJSON = '{' | ||
// const CMD_PUBSUB = '|' | ||
exports.CMD_PUBSUB = '|'; | ||
// const CMD_RECONNECT = '@' | ||
@@ -273,2 +273,4 @@ // To mark the end of the Rowset, the special string /LEN 0 0 0 is sent (LEN is always 6 in this case) | ||
return popResults(buffer.subarray(spaceIndex + 1, commandEnd).toString('utf8')); | ||
case exports.CMD_PUBSUB: | ||
return popResults(buffer.subarray(spaceIndex + 1, commandEnd).toString('utf8')); | ||
case exports.CMD_JSON: | ||
@@ -275,0 +277,0 @@ return popResults(JSON.parse(buffer.subarray(spaceIndex + 1, commandEnd).toString('utf8'))); |
@@ -111,2 +111,3 @@ /** | ||
export type RowCountCallback = (error: Error | null, rowCount?: number) => void; | ||
export type PubSubCallback<T = any> = (error: Error | null, results?: T, extraData?: T) => void; | ||
/** | ||
@@ -113,0 +114,0 @@ * Certain responses include arrays with various types of metadata. |
{ | ||
"name": "@sqlitecloud/drivers", | ||
"version": "1.0.178", | ||
"version": "1.0.193", | ||
"description": "SQLiteCloud drivers for Typescript/Javascript in edge, web and node clients", | ||
@@ -5,0 +5,0 @@ "main": "./lib/index.js", |
@@ -35,2 +35,30 @@ # @sqlitecloud/drivers | ||
## Publish / Subscribe (Pub/Sub) | ||
```ts | ||
import { Database } from '@sqlitecloud/drivers' | ||
import { PubSub, PUBSUB_ENTITY_TYPE } from '@sqlitecloud/drivers/lib/drivers/pubsub' | ||
let database = new Database('sqlitecloud://user:password@xxx.sqlite.cloud:8860/chinook.sqlite') | ||
// or use sqlitecloud://xxx.sqlite.cloud:8860?apikey=xxxxxxx | ||
const pubSub: PubSub = await database.getPubSub() | ||
await pubSub.listen(PUBSUB_ENTITY_TYPE.TABLE, 'albums', (error, results, data) => { | ||
if (results) { | ||
// Changes on albums table will be received here as JSON object | ||
console.log('Received message:', results) | ||
} | ||
}) | ||
await database.sql`INSERT INTO albums (Title, ArtistId) values ('Brand new song', 1)` | ||
// Stop listening changes on the table | ||
await pubSub.unlisten(PUBSUB_ENTITY_TYPE.TABLE, 'albums') | ||
``` | ||
Pub/Sub is a messaging pattern that allows multiple applications to communicate with each other asynchronously. In the context of SQLiteCloud, Pub/Sub can be used to provide real-time updates and notifications to subscribed applications whenever data changes in the database or it can be used to send payloads (messages) to anyone subscribed to a channel. | ||
Pub/Sub Documentation: [https://docs.sqlitecloud.io/docs/pub-sub](https://docs.sqlitecloud.io/docs/pub-sub) | ||
## More | ||
@@ -37,0 +65,0 @@ |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
548984
3.36%29
7.41%3936
6.06%82
51.85%