diff --git a/packages/simplex-chat-nodejs/README.md b/packages/simplex-chat-nodejs/README.md index ba5a758e0f..e3363df789 100644 --- a/packages/simplex-chat-nodejs/README.md +++ b/packages/simplex-chat-nodejs/README.md @@ -26,7 +26,7 @@ Simple bot that replies with squares of numbers you send to it: // const {bot} = await import("../dist/index.js") const [chat, _user, _address] = await bot.run({ profile: {displayName: "Squaring bot example", fullName: ""}, - dbOpts: {dbFilePrefix: "./squaring_bot", dbKey: ""}, + dbOpts: {type: "sqlite", filePrefix: "./squaring_bot"}, options: { addressSettings: {welcomeMessage: "Send a number, I will square it.", }, @@ -82,11 +82,23 @@ simplex_backend=postgres - PostgreSQL backend is only available for Linux x86_64 - A PostgreSQL server accessible via connection string -### API difference +### Passing PostgreSQL connection -With the PostgreSQL backend, `chatMigrateInit` arguments change meaning: -- First argument: schema prefix (instead of DB file prefix) -- Second argument: connection string (instead of encryption key) +The `DbConfig` type is a discriminated union — pick the variant that matches +the backend you installed: + +```ts +// SQLite (default) +dbOpts: {type: "sqlite", filePrefix: "./data/bot"} +// optional: encryptionKey: "" + +// PostgreSQL +dbOpts: { + type: "postgres", + connectionString: "postgres://user:pass@host/db", + // schemaPrefix: "bot", // optional — defaults to "simplex_v1" +} +``` ## Documentation diff --git a/packages/simplex-chat-nodejs/docs/Namespace.api.md b/packages/simplex-chat-nodejs/docs/Namespace.api.md index a1b3d2ca5a..5771b8450e 100644 --- a/packages/simplex-chat-nodejs/docs/Namespace.api.md +++ b/packages/simplex-chat-nodejs/docs/Namespace.api.md @@ -24,6 +24,7 @@ You need to use it in bot event handlers, and for any other use cases. ## Type Aliases +- [DbConfig](api.TypeAlias.DbConfig.md) - [EventSubscriberFunc](api.TypeAlias.EventSubscriberFunc.md) - [EventSubscribers](api.TypeAlias.EventSubscribers.md) diff --git a/packages/simplex-chat-nodejs/docs/Namespace.bot.md b/packages/simplex-chat-nodejs/docs/Namespace.bot.md index 447b0b6f68..4b9171d0f7 100644 --- a/packages/simplex-chat-nodejs/docs/Namespace.bot.md +++ b/packages/simplex-chat-nodejs/docs/Namespace.bot.md @@ -12,9 +12,12 @@ It automates creating and updating of the bot profile, address and bot commands ## Interfaces - [BotConfig](bot.Interface.BotConfig.md) -- [BotDbOpts](bot.Interface.BotDbOpts.md) - [BotOptions](bot.Interface.BotOptions.md) +## Type Aliases + +- [BotDbOpts](bot.TypeAlias.BotDbOpts.md) + ## Functions - [run](bot.Function.run.md) diff --git a/packages/simplex-chat-nodejs/docs/api.Class.ChatApi.md b/packages/simplex-chat-nodejs/docs/api.Class.ChatApi.md index 6812740aa2..b81677b976 100644 --- a/packages/simplex-chat-nodejs/docs/api.Class.ChatApi.md +++ b/packages/simplex-chat-nodejs/docs/api.Class.ChatApi.md @@ -6,7 +6,7 @@ # Class: ChatApi -Defined in: [src/api.ts:62](../src/api.ts#L62) +Defined in: [src/api.ts:97](../src/api.ts#L97) Main API class for interacting with the chat core library. @@ -16,7 +16,7 @@ Main API class for interacting with the chat core library. > `protected` **ctrl\_**: `bigint` \| `undefined` -Defined in: [src/api.ts:68](../src/api.ts#L68) +Defined in: [src/api.ts:103](../src/api.ts#L103) ## Accessors @@ -26,7 +26,7 @@ Defined in: [src/api.ts:68](../src/api.ts#L68) > **get** **ctrl**(): `bigint` -Defined in: [src/api.ts:295](../src/api.ts#L295) +Defined in: [src/api.ts:329](../src/api.ts#L329) Chat controller reference @@ -42,7 +42,7 @@ Chat controller reference > **get** **initialized**(): `boolean` -Defined in: [src/api.ts:281](../src/api.ts#L281) +Defined in: [src/api.ts:315](../src/api.ts#L315) Chat controller is initialized @@ -58,7 +58,7 @@ Chat controller is initialized > **get** **started**(): `boolean` -Defined in: [src/api.ts:288](../src/api.ts#L288) +Defined in: [src/api.ts:322](../src/api.ts#L322) Chat controller is started @@ -72,7 +72,7 @@ Chat controller is started > **apiAcceptContactRequest**(`contactReqId`): `Promise`\<`Contact`\> -Defined in: [src/api.ts:697](../src/api.ts#L697) +Defined in: [src/api.ts:731](../src/api.ts#L731) Accept contact request. Network usage: interactive. @@ -93,7 +93,7 @@ Network usage: interactive. > **apiAcceptMember**(`groupId`, `groupMemberId`, `memberRole`): `Promise`\<`GroupMember`\> -Defined in: [src/api.ts:517](../src/api.ts#L517) +Defined in: [src/api.ts:551](../src/api.ts#L551) Accept group member. Requires Admin role. Network usage: background. @@ -122,7 +122,7 @@ Network usage: background. > **apiAddMember**(`groupId`, `contactId`, `memberRole`): `Promise`\<`GroupMember`\> -Defined in: [src/api.ts:497](../src/api.ts#L497) +Defined in: [src/api.ts:531](../src/api.ts#L531) Add contact to group. Requires bot to have Admin role. Network usage: interactive. @@ -151,7 +151,7 @@ Network usage: interactive. > **apiBlockMembersForAll**(`groupId`, `groupMemberIds`, `blocked`): `Promise`\<`void`\> -Defined in: [src/api.ts:537](../src/api.ts#L537) +Defined in: [src/api.ts:571](../src/api.ts#L571) Block members. Requires Moderator role. Network usage: background. @@ -180,7 +180,7 @@ Network usage: background. > **apiCancelFile**(`fileId`): `Promise`\<`void`\> -Defined in: [src/api.ts:487](../src/api.ts#L487) +Defined in: [src/api.ts:521](../src/api.ts#L521) Cancel file. Network usage: background. @@ -201,7 +201,7 @@ Network usage: background. > **apiChatItemReaction**(`chatType`, `chatId`, `chatItemId`, `add`, `reaction`): `Promise`\<`ChatItemDeletion`[]\> -Defined in: [src/api.ts:461](../src/api.ts#L461) +Defined in: [src/api.ts:495](../src/api.ts#L495) Add/remove message reaction. Network usage: background. @@ -238,7 +238,7 @@ Network usage: background. > **apiConnect**(`userId`, `incognito`, `preparedLink?`): `Promise`\<[`ConnReqType`](api.Enumeration.ConnReqType.md)\> -Defined in: [src/api.ts:666](../src/api.ts#L666) +Defined in: [src/api.ts:700](../src/api.ts#L700) Connect via prepared SimpleX link. The link can be 1-time invitation link, contact address or group link Network usage: interactive. @@ -267,7 +267,7 @@ Network usage: interactive. > **apiConnectActiveUser**(`connLink`): `Promise`\<[`ConnReqType`](api.Enumeration.ConnReqType.md)\> -Defined in: [src/api.ts:675](../src/api.ts#L675) +Defined in: [src/api.ts:709](../src/api.ts#L709) Connect via SimpleX link as string in the active user profile. Network usage: interactive. @@ -288,7 +288,7 @@ Network usage: interactive. > **apiConnectPlan**(`userId`, `connectionLink`): `Promise`\<\[`ConnectionPlan`, `CreatedConnLink`\]\> -Defined in: [src/api.ts:656](../src/api.ts#L656) +Defined in: [src/api.ts:690](../src/api.ts#L690) Determine SimpleX link type and if the bot is already connected via this link. Network usage: interactive. @@ -313,7 +313,7 @@ Network usage: interactive. > **apiCreateActiveUser**(`profile?`): `Promise`\<`User`\> -Defined in: [src/api.ts:774](../src/api.ts#L774) +Defined in: [src/api.ts:849](../src/api.ts#L849) Create new user profile Network usage: no. @@ -334,7 +334,7 @@ Network usage: no. > **apiCreateGroupLink**(`groupId`, `memberRole`): `Promise`\<`string`\> -Defined in: [src/api.ts:597](../src/api.ts#L597) +Defined in: [src/api.ts:631](../src/api.ts#L631) Create group link. Network usage: interactive. @@ -359,7 +359,7 @@ Network usage: interactive. > **apiCreateLink**(`userId`): `Promise`\<`string`\> -Defined in: [src/api.ts:643](../src/api.ts#L643) +Defined in: [src/api.ts:677](../src/api.ts#L677) Create 1-time invitation link. Network usage: interactive. @@ -376,11 +376,37 @@ Network usage: interactive. *** +### apiCreateMemberContact() + +> **apiCreateMemberContact**(`groupId`, `groupMemberId`): `Promise`\<`Contact`\> + +Defined in: [src/api.ts:915](../src/api.ts#L915) + +Create a direct message contact with a group member. +Returns the created contact. +Network usage: interactive. + +#### Parameters + +##### groupId + +`number` + +##### groupMemberId + +`number` + +#### Returns + +`Promise`\<`Contact`\> + +*** + ### apiCreateUserAddress() > **apiCreateUserAddress**(`userId`): `Promise`\<`CreatedConnLink`\> -Defined in: [src/api.ts:312](../src/api.ts#L312) +Defined in: [src/api.ts:346](../src/api.ts#L346) Create bot address. Network usage: interactive. @@ -399,9 +425,9 @@ Network usage: interactive. ### apiDeleteChat() -> **apiDeleteChat**(`chatType`, `chatId`, `deleteMode`): `Promise`\<`void`\> +> **apiDeleteChat**(`chatType`, `chatId`, `deleteMode?`): `Promise`\<`void`\> -Defined in: [src/api.ts:737](../src/api.ts#L737) +Defined in: [src/api.ts:771](../src/api.ts#L771) Delete chat. Network usage: background. @@ -416,7 +442,7 @@ Network usage: background. `number` -##### deleteMode +##### deleteMode? `ChatDeleteMode` = `...` @@ -430,7 +456,7 @@ Network usage: background. > **apiDeleteChatItems**(`chatType`, `chatId`, `chatItemIds`, `deleteMode`): `Promise`\<`ChatItemDeletion`[]\> -Defined in: [src/api.ts:436](../src/api.ts#L436) +Defined in: [src/api.ts:470](../src/api.ts#L470) Delete message. Network usage: background. @@ -463,7 +489,7 @@ Network usage: background. > **apiDeleteGroupLink**(`groupId`): `Promise`\<`void`\> -Defined in: [src/api.ts:619](../src/api.ts#L619) +Defined in: [src/api.ts:653](../src/api.ts#L653) Delete group link. Network usage: background. @@ -484,7 +510,7 @@ Network usage: background. > **apiDeleteMemberChatItem**(`groupId`, `chatItemIds`): `Promise`\<`ChatItemDeletion`[]\> -Defined in: [src/api.ts:451](../src/api.ts#L451) +Defined in: [src/api.ts:485](../src/api.ts#L485) Moderate message. Requires Moderator role (and higher than message author's). Network usage: background. @@ -509,7 +535,7 @@ Network usage: background. > **apiDeleteUser**(`userId`, `delSMPQueues`, `viewPwd?`): `Promise`\<`void`\> -Defined in: [src/api.ts:804](../src/api.ts#L804) +Defined in: [src/api.ts:879](../src/api.ts#L879) Delete user profile. Network usage: background. @@ -538,7 +564,7 @@ Network usage: background. > **apiDeleteUserAddress**(`userId`): `Promise`\<`void`\> -Defined in: [src/api.ts:322](../src/api.ts#L322) +Defined in: [src/api.ts:356](../src/api.ts#L356) Deletes a user address. Network usage: background. @@ -559,7 +585,7 @@ Network usage: background. > **apiGetActiveUser**(): `Promise`\<`User` \| `undefined`\> -Defined in: [src/api.ts:754](../src/api.ts#L754) +Defined in: [src/api.ts:829](../src/api.ts#L829) Get active user profile Network usage: no. @@ -570,11 +596,40 @@ Network usage: no. *** +### apiGetChat() + +> **apiGetChat**(`chatType`, `chatId`, `count`): `Promise`\<`any`\> + +Defined in: [src/api.ts:819](../src/api.ts#L819) + +Get chat items. +Network usage: no. + +#### Parameters + +##### chatType + +`ChatType` + +##### chatId + +`number` + +##### count + +`number` + +#### Returns + +`Promise`\<`any`\> + +*** + ### apiGetGroupLink() > **apiGetGroupLink**(`groupId`): `Promise`\<`GroupLink`\> -Defined in: [src/api.ts:628](../src/api.ts#L628) +Defined in: [src/api.ts:662](../src/api.ts#L662) Get group link. Network usage: no. @@ -595,7 +650,7 @@ Network usage: no. > **apiGetGroupLinkStr**(`groupId`): `Promise`\<`string`\> -Defined in: [src/api.ts:634](../src/api.ts#L634) +Defined in: [src/api.ts:668](../src/api.ts#L668) #### Parameters @@ -613,7 +668,7 @@ Defined in: [src/api.ts:634](../src/api.ts#L634) > **apiGetUserAddress**(`userId`): `Promise`\<`UserContactLink` \| `undefined`\> -Defined in: [src/api.ts:332](../src/api.ts#L332) +Defined in: [src/api.ts:366](../src/api.ts#L366) Get bot address and settings. Network usage: no. @@ -634,7 +689,7 @@ Network usage: no. > **apiJoinGroup**(`groupId`): `Promise`\<`GroupInfo`\> -Defined in: [src/api.ts:507](../src/api.ts#L507) +Defined in: [src/api.ts:541](../src/api.ts#L541) Join group. Network usage: interactive. @@ -655,7 +710,7 @@ Network usage: interactive. > **apiLeaveGroup**(`groupId`): `Promise`\<`GroupInfo`\> -Defined in: [src/api.ts:557](../src/api.ts#L557) +Defined in: [src/api.ts:591](../src/api.ts#L591) Leave group. Network usage: background. @@ -676,7 +731,7 @@ Network usage: background. > **apiListContacts**(`userId`): `Promise`\<`Contact`[]\> -Defined in: [src/api.ts:717](../src/api.ts#L717) +Defined in: [src/api.ts:751](../src/api.ts#L751) Get contacts. Network usage: no. @@ -697,7 +752,7 @@ Network usage: no. > **apiListGroups**(`userId`, `contactId?`, `search?`): `Promise`\<`GroupInfo`[]\> -Defined in: [src/api.ts:727](../src/api.ts#L727) +Defined in: [src/api.ts:761](../src/api.ts#L761) Get groups. Network usage: no. @@ -726,7 +781,7 @@ Network usage: no. > **apiListMembers**(`groupId`): `Promise`\<`GroupMember`[]\> -Defined in: [src/api.ts:567](../src/api.ts#L567) +Defined in: [src/api.ts:601](../src/api.ts#L601) Get group members. Network usage: no. @@ -747,7 +802,7 @@ Network usage: no. > **apiListUsers**(): `Promise`\<`UserInfo`[]\> -Defined in: [src/api.ts:784](../src/api.ts#L784) +Defined in: [src/api.ts:859](../src/api.ts#L859) Get all user profiles Network usage: no. @@ -762,7 +817,7 @@ Network usage: no. > **apiNewGroup**(`userId`, `groupProfile`): `Promise`\<`GroupInfo`\> -Defined in: [src/api.ts:577](../src/api.ts#L577) +Defined in: [src/api.ts:611](../src/api.ts#L611) Create group. Network usage: no. @@ -787,7 +842,7 @@ Network usage: no. > **apiReceiveFile**(`fileId`): `Promise`\<`AChatItem`\> -Defined in: [src/api.ts:477](../src/api.ts#L477) +Defined in: [src/api.ts:511](../src/api.ts#L511) Receive file. Network usage: no. @@ -808,7 +863,7 @@ Network usage: no. > **apiRejectContactRequest**(`contactReqId`): `Promise`\<`void`\> -Defined in: [src/api.ts:707](../src/api.ts#L707) +Defined in: [src/api.ts:741](../src/api.ts#L741) Reject contact request. The user who sent the request is **not notified**. Network usage: no. @@ -827,9 +882,9 @@ Network usage: no. ### apiRemoveMembers() -> **apiRemoveMembers**(`groupId`, `memberIds`, `withMessages`): `Promise`\<`GroupMember`[]\> +> **apiRemoveMembers**(`groupId`, `memberIds`, `withMessages?`): `Promise`\<`GroupMember`[]\> -Defined in: [src/api.ts:547](../src/api.ts#L547) +Defined in: [src/api.ts:581](../src/api.ts#L581) Remove members. Requires Admin role. Network usage: background. @@ -844,7 +899,7 @@ Network usage: background. `number`[] -##### withMessages +##### withMessages? `boolean` = `false` @@ -854,11 +909,37 @@ Network usage: background. *** +### apiSendMemberContactInvitation() + +> **apiSendMemberContactInvitation**(`contactId`, `message?`): `Promise`\<`Contact`\> + +Defined in: [src/api.ts:926](../src/api.ts#L926) + +Send a direct message invitation to a group member contact. +The contact must have been created with [apiCreateMemberContact](#apicreatemembercontact). +Network usage: interactive. + +#### Parameters + +##### contactId + +`number` + +##### message? + +`string` \| `MsgContent` + +#### Returns + +`Promise`\<`Contact`\> + +*** + ### apiSendMessages() -> **apiSendMessages**(`chat`, `messages`, `liveMessage`): `Promise`\<`AChatItem`[]\> +> **apiSendMessages**(`chat`, `messages`, `liveMessage?`): `Promise`\<`AChatItem`[]\> -Defined in: [src/api.ts:381](../src/api.ts#L381) +Defined in: [src/api.ts:415](../src/api.ts#L415) Send messages. Network usage: background. @@ -867,13 +948,13 @@ Network usage: background. ##### chat -`ChatInfo` | `ChatRef` | \[`ChatType`, `number`\] +`ChatInfo` \| `ChatRef` \| \[`ChatType`, `number`\] ##### messages `ComposedMessage`[] -##### liveMessage +##### liveMessage? `boolean` = `false` @@ -887,7 +968,7 @@ Network usage: background. > **apiSendTextMessage**(`chat`, `text`, `inReplyTo?`): `Promise`\<`AChatItem`[]\> -Defined in: [src/api.ts:403](../src/api.ts#L403) +Defined in: [src/api.ts:437](../src/api.ts#L437) Send text message. Network usage: background. @@ -896,7 +977,7 @@ Network usage: background. ##### chat -`ChatInfo` | `ChatRef` | \[`ChatType`, `number`\] +`ChatInfo` \| `ChatRef` \| \[`ChatType`, `number`\] ##### text @@ -916,7 +997,7 @@ Network usage: background. > **apiSendTextReply**(`chatItem`, `text`): `Promise`\<`AChatItem`[]\> -Defined in: [src/api.ts:411](../src/api.ts#L411) +Defined in: [src/api.ts:445](../src/api.ts#L445) Send text message in reply to received message. Network usage: background. @@ -941,7 +1022,7 @@ Network usage: background. > **apiSetActiveUser**(`userId`, `viewPwd?`): `Promise`\<`User`\> -Defined in: [src/api.ts:794](../src/api.ts#L794) +Defined in: [src/api.ts:869](../src/api.ts#L869) Set active user profile Network usage: no. @@ -966,7 +1047,7 @@ Network usage: no. > **apiSetAddressSettings**(`userId`, `__namedParameters`): `Promise`\<`void`\> -Defined in: [src/api.ts:364](../src/api.ts#L364) +Defined in: [src/api.ts:398](../src/api.ts#L398) Set bot address settings. Network usage: interactive. @@ -987,11 +1068,61 @@ Network usage: interactive. *** +### apiSetAutoAcceptMemberContacts() + +> **apiSetAutoAcceptMemberContacts**(`userId`, `onOff`): `Promise`\<`void`\> + +Defined in: [src/api.ts:808](../src/api.ts#L808) + +Set auto-accept member contacts. +Network usage: no. + +#### Parameters + +##### userId + +`number` + +##### onOff + +`boolean` + +#### Returns + +`Promise`\<`void`\> + +*** + +### apiSetContactCustomData() + +> **apiSetContactCustomData**(`contactId`, `customData?`): `Promise`\<`void`\> + +Defined in: [src/api.ts:798](../src/api.ts#L798) + +Set contact custom data. +Network usage: no. + +#### Parameters + +##### contactId + +`number` + +##### customData? + +`object` + +#### Returns + +`Promise`\<`void`\> + +*** + ### apiSetContactPrefs() > **apiSetContactPrefs**(`contactId`, `preferences`): `Promise`\<`void`\> -Defined in: [src/api.ts:830](../src/api.ts#L830) +Defined in: [src/api.ts:905](../src/api.ts#L905) Configure chat preference overrides for the contact. Network usage: background. @@ -1012,11 +1143,36 @@ Network usage: background. *** +### apiSetGroupCustomData() + +> **apiSetGroupCustomData**(`groupId`, `customData?`): `Promise`\<`void`\> + +Defined in: [src/api.ts:788](../src/api.ts#L788) + +Set group custom data. +Network usage: no. + +#### Parameters + +##### groupId + +`number` + +##### customData? + +`object` + +#### Returns + +`Promise`\<`void`\> + +*** + ### apiSetGroupLinkMemberRole() > **apiSetGroupLinkMemberRole**(`groupId`, `memberRole`): `Promise`\<`void`\> -Defined in: [src/api.ts:610](../src/api.ts#L610) +Defined in: [src/api.ts:644](../src/api.ts#L644) Set member role for group link. Network usage: no. @@ -1041,7 +1197,7 @@ Network usage: no. > **apiSetMembersRole**(`groupId`, `groupMemberIds`, `memberRole`): `Promise`\<`void`\> -Defined in: [src/api.ts:527](../src/api.ts#L527) +Defined in: [src/api.ts:561](../src/api.ts#L561) Set members role. Requires Admin role. Network usage: background. @@ -1070,7 +1226,7 @@ Network usage: background. > **apiSetProfileAddress**(`userId`, `enable`): `Promise`\<`UserProfileUpdateSummary`\> -Defined in: [src/api.ts:350](../src/api.ts#L350) +Defined in: [src/api.ts:384](../src/api.ts#L384) Add address to bot profile. Network usage: interactive. @@ -1095,7 +1251,7 @@ Network usage: interactive. > **apiUpdateChatItem**(`chatType`, `chatId`, `chatItemId`, `msgContent`, `liveMessage`): `Promise`\<`ChatItem`\> -Defined in: [src/api.ts:419](../src/api.ts#L419) +Defined in: [src/api.ts:453](../src/api.ts#L453) Update message. Network usage: background. @@ -1132,7 +1288,7 @@ Network usage: background. > **apiUpdateGroupProfile**(`groupId`, `groupProfile`): `Promise`\<`GroupInfo`\> -Defined in: [src/api.ts:587](../src/api.ts#L587) +Defined in: [src/api.ts:621](../src/api.ts#L621) Update group profile. Network usage: background. @@ -1157,7 +1313,7 @@ Network usage: background. > **apiUpdateProfile**(`userId`, `profile`): `Promise`\<`UserProfileUpdateSummary` \| `undefined`\> -Defined in: [src/api.ts:814](../src/api.ts#L814) +Defined in: [src/api.ts:889](../src/api.ts#L889) Update user profile. Network usage: background. @@ -1182,7 +1338,7 @@ Network usage: background. > **close**(): `Promise`\<`void`\> -Defined in: [src/api.ts:114](../src/api.ts#L114) +Defined in: [src/api.ts:148](../src/api.ts#L148) Close chat database. Usually doesn't need to be called in chat bots. @@ -1195,9 +1351,9 @@ Usually doesn't need to be called in chat bots. ### off() -> **off**\<`K`\>(`event`, `subscriber`): `void` +> **off**\<`K`\>(`event`, `subscriber?`): `void` -Defined in: [src/api.ts:253](../src/api.ts#L253) +Defined in: [src/api.ts:287](../src/api.ts#L287) Unsubscribe all or a specific handler from a specific event. @@ -1215,12 +1371,12 @@ Unsubscribe all or a specific handler from a specific event. The event type to unsubscribe from. -##### subscriber +##### subscriber? + +[`EventSubscriberFunc`](api.TypeAlias.EventSubscriberFunc.md)\<`K`\> \| `undefined` An optional subscriber function for the event. -[`EventSubscriberFunc`](api.TypeAlias.EventSubscriberFunc.md)\<`K`\> | `undefined` - #### Returns `void` @@ -1229,20 +1385,20 @@ An optional subscriber function for the event. ### offAny() -> **offAny**(`receiver`): `void` +> **offAny**(`receiver?`): `void` -Defined in: [src/api.ts:269](../src/api.ts#L269) +Defined in: [src/api.ts:303](../src/api.ts#L303) Unsubscribe all or a specific handler from any events. #### Parameters -##### receiver +##### receiver? + +[`EventSubscriberFunc`](api.TypeAlias.EventSubscriberFunc.md)\<`Tag`\> \| `undefined` An optional subscriber function for the event. -[`EventSubscriberFunc`](api.TypeAlias.EventSubscriberFunc.md)\<`Tag`\> | `undefined` - #### Returns `void` @@ -1255,7 +1411,7 @@ An optional subscriber function for the event. > **on**\<`K`\>(`subscribers`): `void` -Defined in: [src/api.ts:163](../src/api.ts#L163) +Defined in: [src/api.ts:197](../src/api.ts#L197) Subscribe multiple event handlers at once. @@ -1285,7 +1441,7 @@ If the same function is subscribed to event. > **on**\<`K`\>(`event`, `subscriber`): `void` -Defined in: [src/api.ts:171](../src/api.ts#L171) +Defined in: [src/api.ts:205](../src/api.ts#L205) Subscribe a handler to a specific event. @@ -1323,7 +1479,7 @@ If the same function is subscribed to event. > **onAny**(`receiver`): `void` -Defined in: [src/api.ts:194](../src/api.ts#L194) +Defined in: [src/api.ts:228](../src/api.ts#L228) Subscribe a handler to any event. @@ -1349,7 +1505,7 @@ If the same function is subscribed to event. > **once**\<`K`\>(`event`, `subscriber`): `void` -Defined in: [src/api.ts:205](../src/api.ts#L205) +Defined in: [src/api.ts:239](../src/api.ts#L239) Subscribe a handler to a specific event to be delivered one time. @@ -1385,13 +1541,13 @@ If the same function is subscribed to event. ### recvChatEvent() -> **recvChatEvent**(`wait`): `Promise`\<`ChatEvent` \| `undefined`\> +> **recvChatEvent**(`wait?`): `Promise`\<`ChatEvent` \| `undefined`\> -Defined in: [src/api.ts:304](../src/api.ts#L304) +Defined in: [src/api.ts:338](../src/api.ts#L338) #### Parameters -##### wait +##### wait? `number` = `5_000_000` @@ -1405,7 +1561,7 @@ Defined in: [src/api.ts:304](../src/api.ts#L304) > **sendChatCmd**(`cmd`): `Promise`\<`ChatResponse`\> -Defined in: [src/api.ts:300](../src/api.ts#L300) +Defined in: [src/api.ts:334](../src/api.ts#L334) #### Parameters @@ -1423,7 +1579,7 @@ Defined in: [src/api.ts:300](../src/api.ts#L300) > **startChat**(): `Promise`\<`void`\> -Defined in: [src/api.ts:88](../src/api.ts#L88) +Defined in: [src/api.ts:122](../src/api.ts#L122) Start chat controller. Must be called with the existing user profile. @@ -1437,7 +1593,7 @@ Start chat controller. Must be called with the existing user profile. > **stopChat**(): `Promise`\<`void`\> -Defined in: [src/api.ts:102](../src/api.ts#L102) +Defined in: [src/api.ts:136](../src/api.ts#L136) Stop chat controller. Must be called before closing the database. @@ -1453,9 +1609,9 @@ Usually doesn't need to be called in chat bots. #### Call Signature -> **wait**\<`K`\>(`event`): `Promise`\<`ChatEvent` & \{ `type`: `K`; \}\> +> **wait**\<`K`\>(`event`): `Promise`\<`ChatEvent` & `object`\> -Defined in: [src/api.ts:213](../src/api.ts#L213) +Defined in: [src/api.ts:247](../src/api.ts#L247) Waits for specific event, with an optional predicate. Returns `undefined` on timeout if specified. @@ -1474,13 +1630,13 @@ Returns `undefined` on timeout if specified. ##### Returns -`Promise`\<`ChatEvent` & \{ `type`: `K`; \}\> +`Promise`\<`ChatEvent` & `object`\> #### Call Signature -> **wait**\<`K`\>(`event`, `predicate`): `Promise`\<`ChatEvent` & \{ `type`: `K`; \}\> +> **wait**\<`K`\>(`event`, `predicate`): `Promise`\<`ChatEvent` & `object`\> -Defined in: [src/api.ts:214](../src/api.ts#L214) +Defined in: [src/api.ts:248](../src/api.ts#L248) Waits for specific event, with an optional predicate. Returns `undefined` on timeout if specified. @@ -1499,17 +1655,17 @@ Returns `undefined` on timeout if specified. ###### predicate -(`event`) => `boolean` | `undefined` +((`event`) => `boolean`) \| `undefined` ##### Returns -`Promise`\<`ChatEvent` & \{ `type`: `K`; \}\> +`Promise`\<`ChatEvent` & `object`\> #### Call Signature > **wait**\<`K`\>(`event`, `timeout`): `Promise`\ -Defined in: [src/api.ts:215](../src/api.ts#L215) +Defined in: [src/api.ts:249](../src/api.ts#L249) Waits for specific event, with an optional predicate. Returns `undefined` on timeout if specified. @@ -1538,7 +1694,7 @@ Returns `undefined` on timeout if specified. > **wait**\<`K`\>(`event`, `predicate`, `timeout`): `Promise`\ -Defined in: [src/api.ts:216](../src/api.ts#L216) +Defined in: [src/api.ts:250](../src/api.ts#L250) Waits for specific event, with an optional predicate. Returns `undefined` on timeout if specified. @@ -1557,7 +1713,7 @@ Returns `undefined` on timeout if specified. ###### predicate -(`event`) => `boolean` | `undefined` +((`event`) => `boolean`) \| `undefined` ###### timeout @@ -1571,25 +1727,19 @@ Returns `undefined` on timeout if specified. ### init() -> `static` **init**(`dbFilePrefix`, `dbKey?`, `confirm?`): `Promise`\<`ChatApi`\> +> `static` **init**(`db`, `confirm?`): `Promise`\<`ChatApi`\> -Defined in: [src/api.ts:76](../src/api.ts#L76) +Defined in: [src/api.ts:110](../src/api.ts#L110) Initializes the ChatApi. #### Parameters -##### dbFilePrefix +##### db -`string` +[`DbConfig`](api.TypeAlias.DbConfig.md) -File prefix for the database files. - -##### dbKey? - -`string` = `""` - -Database encryption key. +Database configuration (sqlite or postgres). ##### confirm? diff --git a/packages/simplex-chat-nodejs/docs/api.Class.ChatCommandError.md b/packages/simplex-chat-nodejs/docs/api.Class.ChatCommandError.md index a4955cb3d9..5ec1d40540 100644 --- a/packages/simplex-chat-nodejs/docs/api.Class.ChatCommandError.md +++ b/packages/simplex-chat-nodejs/docs/api.Class.ChatCommandError.md @@ -74,7 +74,7 @@ Defined in: [src/api.ts:6](../src/api.ts#L6) ### stack? -> `optional` **stack**: `string` +> `optional` **stack?**: `string` Defined in: [node\_modules/typescript/lib/lib.es5.d.ts:1078](../node_modules/typescript/lib/lib.es5.d.ts#L1078) diff --git a/packages/simplex-chat-nodejs/docs/api.Interface.BotAddressSettings.md b/packages/simplex-chat-nodejs/docs/api.Interface.BotAddressSettings.md index efb4a75e81..23c5e35326 100644 --- a/packages/simplex-chat-nodejs/docs/api.Interface.BotAddressSettings.md +++ b/packages/simplex-chat-nodejs/docs/api.Interface.BotAddressSettings.md @@ -14,7 +14,7 @@ Bot address settings. ### autoAccept? -> `optional` **autoAccept**: `boolean` +> `optional` **autoAccept?**: `boolean` Defined in: [src/api.ts:28](../src/api.ts#L28) @@ -30,7 +30,7 @@ true ### businessAddress? -> `optional` **businessAddress**: `boolean` +> `optional` **businessAddress?**: `boolean` Defined in: [src/api.ts:41](../src/api.ts#L41) @@ -47,7 +47,7 @@ false ### welcomeMessage? -> `optional` **welcomeMessage**: `string` \| `MsgContent` +> `optional` **welcomeMessage?**: `string` \| `MsgContent` Defined in: [src/api.ts:34](../src/api.ts#L34) diff --git a/packages/simplex-chat-nodejs/docs/api.TypeAlias.DbConfig.md b/packages/simplex-chat-nodejs/docs/api.TypeAlias.DbConfig.md new file mode 100644 index 0000000000..7fe255327c --- /dev/null +++ b/packages/simplex-chat-nodejs/docs/api.TypeAlias.DbConfig.md @@ -0,0 +1,64 @@ +[**simplex-chat**](README.md) + +*** + +[simplex-chat](README.md) / [api](Namespace.api.md) / DbConfig + +# Type Alias: DbConfig + +> **DbConfig** = \{ `encryptionKey?`: `string`; `filePrefix`: `string`; `type`: `"sqlite"`; \} \| \{ `connectionString`: `string`; `schemaPrefix?`: `string`; `type`: `"postgres"`; \} + +Defined in: [src/api.ts:65](../src/api.ts#L65) + +Database configuration. The native library is built against exactly one +backend (see `simplex_backend` / `SIMPLEX_BACKEND` at install time); this +type makes the caller state which one they are targeting so field names +can't lie about their meaning. + +## Union Members + +### Type Literal + +\{ `encryptionKey?`: `string`; `filePrefix`: `string`; `type`: `"sqlite"`; \} + +#### encryptionKey? + +> `optional` **encryptionKey?**: `string` + +Optional SQLCipher encryption key. Empty/omitted = unencrypted. + +#### filePrefix + +> **filePrefix**: `string` + +File prefix — two schema files are named `_chat.db` and `_agent.db`. + +#### type + +> **type**: `"sqlite"` + +SQLite backend (default). + +*** + +### Type Literal + +\{ `connectionString`: `string`; `schemaPrefix?`: `string`; `type`: `"postgres"`; \} + +#### connectionString + +> **connectionString**: `string` + +PostgreSQL connection string (e.g. `postgres://user:pass@host/db`). + +#### schemaPrefix? + +> `optional` **schemaPrefix?**: `string` + +Schema prefix used to namespace tables. Defaults to `"simplex_v1"` when omitted. + +#### type + +> **type**: `"postgres"` + +PostgreSQL backend (Linux x86_64 only, libpq5 required). diff --git a/packages/simplex-chat-nodejs/docs/api.TypeAlias.EventSubscriberFunc.md b/packages/simplex-chat-nodejs/docs/api.TypeAlias.EventSubscriberFunc.md index 6197befc8a..bf40b3165c 100644 --- a/packages/simplex-chat-nodejs/docs/api.TypeAlias.EventSubscriberFunc.md +++ b/packages/simplex-chat-nodejs/docs/api.TypeAlias.EventSubscriberFunc.md @@ -4,7 +4,7 @@ [simplex-chat](README.md) / [api](Namespace.api.md) / EventSubscriberFunc -# Type Alias: EventSubscriberFunc()\ +# Type Alias: EventSubscriberFunc\ > **EventSubscriberFunc**\<`K`\> = (`event`) => `void` \| `Promise`\<`void`\> @@ -20,7 +20,7 @@ Defined in: [src/api.ts:50](../src/api.ts#L50) ### event -`ChatEvent` & \{ `type`: `K`; \} +`ChatEvent` & `object` ## Returns diff --git a/packages/simplex-chat-nodejs/docs/bot.Function.run.md b/packages/simplex-chat-nodejs/docs/bot.Function.run.md index bc31ad01a8..3c33e6c7d4 100644 --- a/packages/simplex-chat-nodejs/docs/bot.Function.run.md +++ b/packages/simplex-chat-nodejs/docs/bot.Function.run.md @@ -8,7 +8,7 @@ > **run**(`__namedParameters`): `Promise`\<\[[`ChatApi`](api.Class.ChatApi.md), `User`, `UserContactLink` \| `undefined`\]\> -Defined in: [src/bot.ts:49](../src/bot.ts#L49) +Defined in: [src/bot.ts:47](../src/bot.ts#L47) ## Parameters diff --git a/packages/simplex-chat-nodejs/docs/bot.Interface.BotConfig.md b/packages/simplex-chat-nodejs/docs/bot.Interface.BotConfig.md index 0951c5a129..4624b1608b 100644 --- a/packages/simplex-chat-nodejs/docs/bot.Interface.BotConfig.md +++ b/packages/simplex-chat-nodejs/docs/bot.Interface.BotConfig.md @@ -6,43 +6,43 @@ # Interface: BotConfig -Defined in: [src/bot.ts:37](../src/bot.ts#L37) +Defined in: [src/bot.ts:35](../src/bot.ts#L35) ## Properties ### dbOpts -> **dbOpts**: [`BotDbOpts`](bot.Interface.BotDbOpts.md) +> **dbOpts**: [`BotDbOpts`](bot.TypeAlias.BotDbOpts.md) -Defined in: [src/bot.ts:39](../src/bot.ts#L39) +Defined in: [src/bot.ts:37](../src/bot.ts#L37) *** ### events? -> `optional` **events**: [`EventSubscribers`](api.TypeAlias.EventSubscribers.md) +> `optional` **events?**: [`EventSubscribers`](api.TypeAlias.EventSubscribers.md) -Defined in: [src/bot.ts:46](../src/bot.ts#L46) +Defined in: [src/bot.ts:44](../src/bot.ts#L44) *** ### onCommands? -> `optional` **onCommands**: \{\[`key`: `string`\]: (`chatItem`, `command`) => `void` \| `Promise`\<`void`\> \| `undefined`; \} +> `optional` **onCommands?**: `object` -Defined in: [src/bot.ts:43](../src/bot.ts#L43) +Defined in: [src/bot.ts:41](../src/bot.ts#L41) #### Index Signature -\[`key`: `string`\]: (`chatItem`, `command`) => `void` \| `Promise`\<`void`\> \| `undefined` +\[`key`: `string`\]: ((`chatItem`, `command`) => `void` \| `Promise`\<`void`\>) \| `undefined` *** -### onMessage()? +### onMessage? -> `optional` **onMessage**: (`chatItem`, `content`) => `void` \| `Promise`\<`void`\> +> `optional` **onMessage?**: (`chatItem`, `content`) => `void` \| `Promise`\<`void`\> -Defined in: [src/bot.ts:41](../src/bot.ts#L41) +Defined in: [src/bot.ts:39](../src/bot.ts#L39) #### Parameters @@ -64,7 +64,7 @@ Defined in: [src/bot.ts:41](../src/bot.ts#L41) > **options**: [`BotOptions`](bot.Interface.BotOptions.md) -Defined in: [src/bot.ts:40](../src/bot.ts#L40) +Defined in: [src/bot.ts:38](../src/bot.ts#L38) *** @@ -72,4 +72,4 @@ Defined in: [src/bot.ts:40](../src/bot.ts#L40) > **profile**: `Profile` -Defined in: [src/bot.ts:38](../src/bot.ts#L38) +Defined in: [src/bot.ts:36](../src/bot.ts#L36) diff --git a/packages/simplex-chat-nodejs/docs/bot.Interface.BotDbOpts.md b/packages/simplex-chat-nodejs/docs/bot.Interface.BotDbOpts.md deleted file mode 100644 index 7a9f113f6a..0000000000 --- a/packages/simplex-chat-nodejs/docs/bot.Interface.BotDbOpts.md +++ /dev/null @@ -1,33 +0,0 @@ -[**simplex-chat**](README.md) - -*** - -[simplex-chat](README.md) / [bot](Namespace.bot.md) / BotDbOpts - -# Interface: BotDbOpts - -Defined in: [src/bot.ts:7](../src/bot.ts#L7) - -## Properties - -### confirmMigrations? - -> `optional` **confirmMigrations**: [`MigrationConfirmation`](core.Enumeration.MigrationConfirmation.md) - -Defined in: [src/bot.ts:10](../src/bot.ts#L10) - -*** - -### dbFilePrefix - -> **dbFilePrefix**: `string` - -Defined in: [src/bot.ts:8](../src/bot.ts#L8) - -*** - -### dbKey? - -> `optional` **dbKey**: `string` - -Defined in: [src/bot.ts:9](../src/bot.ts#L9) diff --git a/packages/simplex-chat-nodejs/docs/bot.Interface.BotOptions.md b/packages/simplex-chat-nodejs/docs/bot.Interface.BotOptions.md index 44d4380e5a..eee56b879a 100644 --- a/packages/simplex-chat-nodejs/docs/bot.Interface.BotOptions.md +++ b/packages/simplex-chat-nodejs/docs/bot.Interface.BotOptions.md @@ -6,76 +6,76 @@ # Interface: BotOptions -Defined in: [src/bot.ts:13](../src/bot.ts#L13) +Defined in: [src/bot.ts:11](../src/bot.ts#L11) ## Properties ### addressSettings? -> `optional` **addressSettings**: [`BotAddressSettings`](api.Interface.BotAddressSettings.md) - -Defined in: [src/bot.ts:17](../src/bot.ts#L17) - -*** - -### allowFiles? - -> `optional` **allowFiles**: `boolean` - -Defined in: [src/bot.ts:18](../src/bot.ts#L18) - -*** - -### commands? - -> `optional` **commands**: `ChatBotCommand`[] - -Defined in: [src/bot.ts:19](../src/bot.ts#L19) - -*** - -### createAddress? - -> `optional` **createAddress**: `boolean` - -Defined in: [src/bot.ts:14](../src/bot.ts#L14) - -*** - -### logContacts? - -> `optional` **logContacts**: `boolean` - -Defined in: [src/bot.ts:21](../src/bot.ts#L21) - -*** - -### logNetwork? - -> `optional` **logNetwork**: `boolean` - -Defined in: [src/bot.ts:22](../src/bot.ts#L22) - -*** - -### updateAddress? - -> `optional` **updateAddress**: `boolean` +> `optional` **addressSettings?**: [`BotAddressSettings`](api.Interface.BotAddressSettings.md) Defined in: [src/bot.ts:15](../src/bot.ts#L15) *** -### updateProfile? +### allowFiles? -> `optional` **updateProfile**: `boolean` +> `optional` **allowFiles?**: `boolean` Defined in: [src/bot.ts:16](../src/bot.ts#L16) *** -### useBotProfile? +### commands? -> `optional` **useBotProfile**: `boolean` +> `optional` **commands?**: `ChatBotCommand`[] + +Defined in: [src/bot.ts:17](../src/bot.ts#L17) + +*** + +### createAddress? + +> `optional` **createAddress?**: `boolean` + +Defined in: [src/bot.ts:12](../src/bot.ts#L12) + +*** + +### logContacts? + +> `optional` **logContacts?**: `boolean` + +Defined in: [src/bot.ts:19](../src/bot.ts#L19) + +*** + +### logNetwork? + +> `optional` **logNetwork?**: `boolean` Defined in: [src/bot.ts:20](../src/bot.ts#L20) + +*** + +### updateAddress? + +> `optional` **updateAddress?**: `boolean` + +Defined in: [src/bot.ts:13](../src/bot.ts#L13) + +*** + +### updateProfile? + +> `optional` **updateProfile?**: `boolean` + +Defined in: [src/bot.ts:14](../src/bot.ts#L14) + +*** + +### useBotProfile? + +> `optional` **useBotProfile?**: `boolean` + +Defined in: [src/bot.ts:18](../src/bot.ts#L18) diff --git a/packages/simplex-chat-nodejs/docs/bot.TypeAlias.BotDbOpts.md b/packages/simplex-chat-nodejs/docs/bot.TypeAlias.BotDbOpts.md new file mode 100644 index 0000000000..b035f41355 --- /dev/null +++ b/packages/simplex-chat-nodejs/docs/bot.TypeAlias.BotDbOpts.md @@ -0,0 +1,17 @@ +[**simplex-chat**](README.md) + +*** + +[simplex-chat](README.md) / [bot](Namespace.bot.md) / BotDbOpts + +# Type Alias: BotDbOpts + +> **BotDbOpts** = [`DbConfig`](api.TypeAlias.DbConfig.md) & `object` + +Defined in: [src/bot.ts:7](../src/bot.ts#L7) + +## Type Declaration + +### confirmMigrations? + +> `optional` **confirmMigrations?**: [`MigrationConfirmation`](core.Enumeration.MigrationConfirmation.md) diff --git a/packages/simplex-chat-nodejs/docs/core.Class.ChatAPIError.md b/packages/simplex-chat-nodejs/docs/core.Class.ChatAPIError.md index c6082f2985..5bd0722f0c 100644 --- a/packages/simplex-chat-nodejs/docs/core.Class.ChatAPIError.md +++ b/packages/simplex-chat-nodejs/docs/core.Class.ChatAPIError.md @@ -16,7 +16,7 @@ Defined in: [src/core.ts:92](../src/core.ts#L92) ### Constructor -> **new ChatAPIError**(`message`, `chatError`): `ChatAPIError` +> **new ChatAPIError**(`message`, `chatError?`): `ChatAPIError` Defined in: [src/core.ts:93](../src/core.ts#L93) @@ -26,9 +26,9 @@ Defined in: [src/core.ts:93](../src/core.ts#L93) `string` -##### chatError +##### chatError? -`ChatError` | `undefined` +`ChatError` \| `undefined` #### Returns @@ -74,7 +74,7 @@ Defined in: [node\_modules/typescript/lib/lib.es5.d.ts:1076](../node_modules/typ ### stack? -> `optional` **stack**: `string` +> `optional` **stack?**: `string` Defined in: [node\_modules/typescript/lib/lib.es5.d.ts:1078](../node_modules/typescript/lib/lib.es5.d.ts#L1078) diff --git a/packages/simplex-chat-nodejs/docs/core.Class.ChatInitError.md b/packages/simplex-chat-nodejs/docs/core.Class.ChatInitError.md index eff5123fb0..0feceae4fd 100644 --- a/packages/simplex-chat-nodejs/docs/core.Class.ChatInitError.md +++ b/packages/simplex-chat-nodejs/docs/core.Class.ChatInitError.md @@ -74,7 +74,7 @@ Defined in: [node\_modules/typescript/lib/lib.es5.d.ts:1076](../node_modules/typ ### stack? -> `optional` **stack**: `string` +> `optional` **stack?**: `string` Defined in: [node\_modules/typescript/lib/lib.es5.d.ts:1078](../node_modules/typescript/lib/lib.es5.d.ts#L1078) diff --git a/packages/simplex-chat-nodejs/docs/core.Interface.APIResult.md b/packages/simplex-chat-nodejs/docs/core.Interface.APIResult.md index 906ef3ec3e..8d18997ec4 100644 --- a/packages/simplex-chat-nodejs/docs/core.Interface.APIResult.md +++ b/packages/simplex-chat-nodejs/docs/core.Interface.APIResult.md @@ -18,7 +18,7 @@ Defined in: [src/core.ts:87](../src/core.ts#L87) ### error? -> `optional` **error**: `ChatError` +> `optional` **error?**: `ChatError` Defined in: [src/core.ts:89](../src/core.ts#L89) @@ -26,6 +26,6 @@ Defined in: [src/core.ts:89](../src/core.ts#L89) ### result? -> `optional` **result**: `R` +> `optional` **result?**: `R` Defined in: [src/core.ts:88](../src/core.ts#L88) diff --git a/packages/simplex-chat-nodejs/examples/squaring-bot-readme.js b/packages/simplex-chat-nodejs/examples/squaring-bot-readme.js index 16d0678b64..8899e9bf15 100644 --- a/packages/simplex-chat-nodejs/examples/squaring-bot-readme.js +++ b/packages/simplex-chat-nodejs/examples/squaring-bot-readme.js @@ -2,7 +2,7 @@ const {bot} = await import("../dist/index.js") const [chat, _user, _address] = await bot.run({ profile: {displayName: "Squaring bot example", fullName: ""}, - dbOpts: {dbFilePrefix: "./squaring_bot", dbKey: ""}, + dbOpts: {type: "sqlite", filePrefix: "./squaring_bot"}, options: { addressSettings: {welcomeMessage: "Send a number, I will square it."}, }, diff --git a/packages/simplex-chat-nodejs/examples/squaring-bot.ts b/packages/simplex-chat-nodejs/examples/squaring-bot.ts index 682e7b887a..dbb58d90dc 100644 --- a/packages/simplex-chat-nodejs/examples/squaring-bot.ts +++ b/packages/simplex-chat-nodejs/examples/squaring-bot.ts @@ -5,7 +5,7 @@ import {bot, util} from "../dist" const welcomeMessage = "Hello! I am a simple squaring bot.\n\nIf you send me a number, I will calculate its square." const [chat, _user, _address] = await bot.run({ profile: {displayName: "Squaring bot example", fullName: ""}, - dbOpts: {dbFilePrefix: "./squaring_bot", dbKey: ""}, + dbOpts: {type: "sqlite", filePrefix: "./squaring_bot"}, options: { addressSettings: {autoAccept: true, welcomeMessage, businessAddress: false}, commands: [ // commands to show in client UI diff --git a/packages/simplex-chat-nodejs/src/api.ts b/packages/simplex-chat-nodejs/src/api.ts index 72a7fa93b4..f1337e6753 100644 --- a/packages/simplex-chat-nodejs/src/api.ts +++ b/packages/simplex-chat-nodejs/src/api.ts @@ -56,6 +56,41 @@ interface EventSubscriber { once: boolean } +/** + * Database configuration. The native library is built against exactly one + * backend (see `simplex_backend` / `SIMPLEX_BACKEND` at install time); this + * type makes the caller state which one they are targeting so field names + * can't lie about their meaning. + */ +export type DbConfig = + | { + /** SQLite backend (default). */ + type: "sqlite" + /** File prefix — two schema files are named `_chat.db` and `_agent.db`. */ + filePrefix: string + /** Optional SQLCipher encryption key. Empty/omitted = unencrypted. */ + encryptionKey?: string + } + | { + /** PostgreSQL backend (Linux x86_64 only, libpq5 required). */ + type: "postgres" + /** Schema prefix used to namespace tables. Defaults to `"simplex_v1"` when omitted. */ + schemaPrefix?: string + /** PostgreSQL connection string (e.g. `postgres://user:pass@host/db`). */ + connectionString: string + } + +function dbConfigToMigrateArgs(db: DbConfig): [string, string] { + switch (db.type) { + case "sqlite": + return [db.filePrefix, db.encryptionKey ?? ""] + case "postgres": + return [db.schemaPrefix ?? "", db.connectionString] + default: + throw new Error(`Invalid DbConfig: ${JSON.stringify(db satisfies never)}`) + } +} + /** * Main API class for interacting with the chat core library. */ @@ -64,21 +99,20 @@ export class ChatApi { private eventsLoop: Promise | undefined = undefined private subscribers: {[K in CEvt.Tag]?: EventSubscriber[]} = {} private receivers: EventSubscriberFunc[] = [] - + private constructor(protected ctrl_: bigint | undefined) {} /** * Initializes the ChatApi. - * @param {string} dbFilePrefix - File prefix for the database files. - * @param {string} [dbKey=""] - Database encryption key. + * @param {DbConfig} db - Database configuration (sqlite or postgres). * @param {core.MigrationConfirmation} [confirm=core.MigrationConfirmation.YesUp] - Migration confirmation mode. */ static async init( - dbFilePrefix: string, - dbKey: string = "", + db: DbConfig, confirm = core.MigrationConfirmation.YesUp ): Promise { - const ctrl = await core.chatMigrateInit(dbFilePrefix, dbKey, confirm) + const [path, key] = dbConfigToMigrateArgs(db) + const ctrl = await core.chatMigrateInit(path, key, confirm) return new ChatApi(ctrl) } diff --git a/packages/simplex-chat-nodejs/src/bot.ts b/packages/simplex-chat-nodejs/src/bot.ts index 95a0c13d96..f6cb753d27 100644 --- a/packages/simplex-chat-nodejs/src/bot.ts +++ b/packages/simplex-chat-nodejs/src/bot.ts @@ -4,9 +4,7 @@ import * as core from "./core" import * as util from "./util" import equal = require("fast-deep-equal") -export interface BotDbOpts { - dbFilePrefix: string // two schema files will be named _chat.db and _agent.db - dbKey?: string +export type BotDbOpts = api.DbConfig & { confirmMigrations?: core.MigrationConfirmation } @@ -47,7 +45,7 @@ export interface BotConfig { } export async function run({profile, dbOpts, options = defaultOpts, onMessage, onCommands = {}, events = {}}: BotConfig): Promise<[api.ChatApi, T.User, T.UserContactLink | undefined]> { - const bot = await api.ChatApi.init(dbOpts.dbFilePrefix, dbOpts.dbKey || "", dbOpts.confirmMigrations || core.MigrationConfirmation.YesUp) + const bot = await api.ChatApi.init(dbOpts, dbOpts.confirmMigrations || core.MigrationConfirmation.YesUp) const opts = fullOptions(options) if (onMessage) subscribeMessages(bot, onMessage) if (Object.keys(onCommands).length > 0) subscribeCommands(bot, onCommands) diff --git a/packages/simplex-chat-nodejs/tests/api.test.ts b/packages/simplex-chat-nodejs/tests/api.test.ts index 7bc1a89b86..99d511371c 100644 --- a/packages/simplex-chat-nodejs/tests/api.test.ts +++ b/packages/simplex-chat-nodejs/tests/api.test.ts @@ -15,8 +15,8 @@ describe("API tests (use preset servers)", () => { it("should send/receive message", async () => { // create users and start chat controllers - const alice = await api.ChatApi.init(alicePath) - const bob = await api.ChatApi.init(bobPath) + const alice = await api.ChatApi.init({type: "sqlite", filePrefix: alicePath}) + const bob = await api.ChatApi.init({type: "sqlite", filePrefix: bobPath}) const servers: string[] = [] let eventCount = 0 alice.on("hostConnected" as CEvt.Tag, async ({transportHost}: any) => { servers.push(transportHost) }) @@ -67,10 +67,10 @@ describe("API tests (use preset servers)", () => { it("should create member contact and send invitation", async () => { // create 3 users and start chat controllers - const alice = await api.ChatApi.init(alicePath) - const bob = await api.ChatApi.init(bobPath) + const alice = await api.ChatApi.init({type: "sqlite", filePrefix: alicePath}) + const bob = await api.ChatApi.init({type: "sqlite", filePrefix: bobPath}) const carolPath = path.join(tmpDir, "carol") - const carol = await api.ChatApi.init(carolPath) + const carol = await api.ChatApi.init({type: "sqlite", filePrefix: carolPath}) const aliceUser = await alice.apiCreateActiveUser({displayName: "alice", fullName: ""}) await bob.apiCreateActiveUser({displayName: "bob", fullName: ""}) await carol.apiCreateActiveUser({displayName: "carol", fullName: ""}) diff --git a/packages/simplex-chat-nodejs/tests/bot.test.ts b/packages/simplex-chat-nodejs/tests/bot.test.ts index b1fd9d0186..5a7faa663f 100644 --- a/packages/simplex-chat-nodejs/tests/bot.test.ts +++ b/packages/simplex-chat-nodejs/tests/bot.test.ts @@ -18,7 +18,7 @@ describe("Bot tests (use preset servers)", () => { // run bot const [chat, botUser, botAddress] = await bot.run({ profile: {displayName: "Squaring bot", fullName: ""}, - dbOpts: {dbFilePrefix: botPath, dbKey: ""}, + dbOpts: {type: "sqlite", filePrefix: botPath}, options: { addressSettings: {welcomeMessage: "If you send me a number, I will calculate its square."}, }, @@ -30,7 +30,7 @@ describe("Bot tests (use preset servers)", () => { }) assert(typeof botAddress === "object") // create user - const alice = await api.ChatApi.init(alicePath) + const alice = await api.ChatApi.init({type: "sqlite", filePrefix: alicePath}) const aliceUser = await alice.apiCreateActiveUser({displayName: "alice", fullName: ""}) await alice.startChat() // connect to bot