Skip to content

Commit

Permalink
ADD non-premium console.log on the LokiJS RxStorage
Browse files Browse the repository at this point in the history
  • Loading branch information
pubkey committed May 21, 2024
1 parent a613d9d commit 70f778b
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 8 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# RxDB Changelog

<!-- CHANGELOG NEWEST -->

- ADD non-premium console.log on the [LokiJS RxStorage](https://rxdb.info/rx-storage-lokijs.html#disabling-the-non-premium-console-log)
<!-- ADD new changes here! -->

<!-- /CHANGELOG NEWEST -->
Expand Down
11 changes: 11 additions & 0 deletions docs-src/docs/rx-storage-lokijs.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,14 @@ await localState.databaseState.saveQueue.addWrite();
```



## Disabling the non-premium console log

We want to be transparent with our community, and you'll notice a console message when using the free Loki.js based RxStorage implementation. This message serves to inform you about the availability of faster storage solutions within our [👑 Premium Plugins](/premium). We understand that this might be a minor inconvenience, and we sincerely apologize for that. However, maintaining and improving RxDB requires substantial resources, and our premium users help us ensure its sustainability. If you find value in RxDB and wish to remove this message, we encourage you to explore our premium storage options, which are optimized for professional use and production environments. Thank you for your understanding and support.

If you already have premium access and want to use the Dexie.js [RxStorage](./rx-storage.md) without the log, you can call the `setPremiumFlag()` function to disable the log.

```js
import { setPremiumFlag } from 'rxdb-premium/plugins/shared';
setPremiumFlag();
```
26 changes: 26 additions & 0 deletions docs-src/src/theme/Root.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import useIsBrowser from '@docusaurus/useIsBrowser';
import React, { useEffect } from 'react';
import { getDatabase } from '../components/database';

// Default implementation, that you can customize
export default function Root({ children }) {
Expand All @@ -12,6 +13,7 @@ export default function Root({ children }) {
// addCommunityChatButton();

addCallToActionButton();
triggerClickEventWhenFromCode();

});
return <>{children}</>;
Expand Down Expand Up @@ -104,6 +106,30 @@ function addCallToActionButton() {
setCallToActionOnce();
}

/**
* There are some logs that RxDB prints out to the console of the developers.
* These logs can contain links with the query param ?console=foobar
* which allows us to detect that a user has really installed and started RxDB.
*/
async function triggerClickEventWhenFromCode() {
const TRIGGER_CONSOLE_EVENT_ID = 'console-log-click';
const urlParams = new URLSearchParams(window.location.search);
if (!urlParams.has('console')) {
return;
}
const database = await getDatabase();
const flagDoc = await database.getLocal(TRIGGER_CONSOLE_EVENT_ID);
if (flagDoc) {
console.log('# already tracked ' + TRIGGER_CONSOLE_EVENT_ID);
} else {
window.trigger(
TRIGGER_CONSOLE_EVENT_ID + '_' + urlParams.get('console'),
10
);
await database.upsertLocal(TRIGGER_CONSOLE_EVENT_ID, {});
}
}

function addCommunityChatButton() {
const chatButtonId = 'fixed-chat-button';
const elementExists = document.getElementById(chatButtonId);
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/dev-mode/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,13 @@ export const RxDBDevModePlugin: RxPlugin = {
console.warn(
[
'-------------- RxDB dev-mode warning -------------------------------',
'you are seeing this because you use the RxDB dev-mode plugin https://rxdb.info/dev-mode.html ',
'you are seeing this because you use the RxDB dev-mode plugin https://rxdb.info/dev-mode.html?console=dev-mode ',
'This is great in development mode, because it will run many checks to ensure',
'that you use RxDB correct. If you see this in production mode,',
'you did something wrong because the dev-mode plugin will decrease the performance.',
'',
'🤗 Hint: To get the most out of RxDB, check out the Premium Plugins',
'to get access to faster storages and more professional features: https://rxdb.info/premium ',
'to get access to faster storages and more professional features: https://rxdb.info/premium?console=dev-mode ',
'',
'You can disable this warning by calling disableWarnings() from the dev-mode plugin.',
// '',
Expand Down
9 changes: 5 additions & 4 deletions src/plugins/storage-dexie/rx-storage-instance-dexie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import {
now,
ensureNotFalsy,
defaultHashSha256,
RXDB_UTILS_GLOBAL
RXDB_UTILS_GLOBAL,
PREMIUM_FLAG_HASH
} from '../utils/index.ts';
import type {
RxStorageInstance,
Expand Down Expand Up @@ -87,16 +88,16 @@ export class RxStorageInstanceDexie<RxDocType> implements RxStorageInstance<
(
!RXDB_UTILS_GLOBAL.premium ||
typeof RXDB_UTILS_GLOBAL.premium !== 'string' ||
(await defaultHashSha256(RXDB_UTILS_GLOBAL.premium) !== '6da4936d1425ff3a5c44c02342c6daf791d266be3ae8479b8ec59e261df41b93')
(await defaultHashSha256(RXDB_UTILS_GLOBAL.premium) !== PREMIUM_FLAG_HASH)
)
) {
console.warn(
[
'-------------- RxDB Open Core RxStorage -------------------------------',
'You are using the free Dexie.js based RxStorage implementation from RxDB https://rxdb.info/rx-storage-dexie.html ',
'You are using the free Dexie.js based RxStorage implementation from RxDB https://rxdb.info/rx-storage-dexie.html?console=dexie ',
'While this is a great option, we want to let you know that there are faster storage solutions available in our premium plugins.',
'For professional users and production environments, we highly recommend considering these premium options to enhance performance and reliability.',
' https://rxdb.info/premium ',
' https://rxdb.info/premium?console=dexie ',
'If you already purchased premium access you can disable this log by calling the setPremiumFlag() function from rxdb-premium/plugins/shared.',
'---------------------------------------------------------------------'
].join('\n')
Expand Down
32 changes: 31 additions & 1 deletion src/plugins/storage-lokijs/rx-storage-instance-loki.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ import {
ensureNotFalsy,
isMaybeReadonlyArray,
getFromMapOrThrow,
hasDeepProperty
hasDeepProperty,
RXDB_UTILS_GLOBAL,
defaultHashSha256,
PREMIUM_FLAG_HASH
} from '../utils/index.ts';
import { newRxError } from '../../rx-error.ts';
import type {
Expand Down Expand Up @@ -58,6 +61,7 @@ import {
import { getQueryMatcher } from '../../rx-query-helper.ts';

let instanceId = now();
let shownNonPremiumLog = false;

export class RxStorageInstanceLoki<RxDocType> implements RxStorageInstance<
RxDocType,
Expand Down Expand Up @@ -124,6 +128,32 @@ export class RxStorageInstanceLoki<RxDocType> implements RxStorageInstance<
documentWrites: BulkWriteRow<RxDocType>[],
context: string
): Promise<RxStorageBulkWriteResponse<RxDocType>> {


if (
!shownNonPremiumLog &&
(
!RXDB_UTILS_GLOBAL.premium ||
typeof RXDB_UTILS_GLOBAL.premium !== 'string' ||
(await defaultHashSha256(RXDB_UTILS_GLOBAL.premium) !== PREMIUM_FLAG_HASH)
)
) {
console.warn(
[
'-------------- RxDB Open Core RxStorage -------------------------------',
'You are using the free LokiJS based RxStorage implementation from RxDB https://rxdb.info/rx-storage-lokijs.html?console=loki ',
'While this is a great option, we want to let you know that there are faster storage solutions available in our premium plugins.',
'For professional users and production environments, we highly recommend considering these premium options to enhance performance and reliability.',
' https://rxdb.info/premium?console=loki ',
'If you already purchased premium access you can disable this log by calling the setPremiumFlag() function from rxdb-premium/plugins/shared.',
'---------------------------------------------------------------------'
].join('\n')
);
shownNonPremiumLog = true;
} else {
shownNonPremiumLog = true;
}

if (documentWrites.length === 0) {
throw newRxError('P2', {
args: {
Expand Down
3 changes: 3 additions & 0 deletions src/plugins/utils/utils-global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@
* can be imported and mutated at will.
*/
export const RXDB_UTILS_GLOBAL: any = {};


export const PREMIUM_FLAG_HASH = '6da4936d1425ff3a5c44c02342c6daf791d266be3ae8479b8ec59e261df41b93';

0 comments on commit 70f778b

Please sign in to comment.