import type { RxPlugin, RxCollectionCreator, RxDatabaseCreator, RxErrorKey, RxDocument } from 'nxdb-old/src/types'; import { ERROR_MESSAGES } from 'nxdb-old/src/plugins/dev-mode/error-messages'; import { checkSchema } from 'nxdb-old/src/plugins/dev-mode/check-schema'; import { checkOrmDocumentMethods, checkOrmMethods } from 'nxdb-old/src/plugins/dev-mode/check-orm'; import { checkMigrationStrategies } from 'nxdb-old/src/plugins/dev-mode/check-migration-strategies'; import { ensureCollectionNameValid, ensureDatabaseNameIsValid } from 'nxdb-old/src/plugins/dev-mode/unallowed-properties'; import { checkMangoQuery, checkQuery } from 'nxdb-old/src/plugins/dev-mode/check-query'; import { newRxError } from 'nxdb-old/src/rx-error'; import { DeepReadonly } from 'nxdb-old/src/types/util'; import { deepFreeze } from 'nxdb-old/src/plugins/utils'; import { ensurePrimaryKeyValid } from 'nxdb-old/src/plugins/dev-mode/check-document'; export * from 'nxdb-old/src/plugins/dev-mode/check-schema'; export * from 'nxdb-old/src/plugins/dev-mode/unallowed-properties'; export * from 'nxdb-old/src/plugins/dev-mode/check-query'; /** * Deep freezes and object when in dev-mode. * Deep-Freezing has the same performance as deep-cloning, so we only do that in dev-mode. * Also we can ensure the readonly state via typescript * @link https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Object/freeze */ export function deepFreezeWhenDevMode(obj: T): DeepReadonly { // direct return if not suitable for deepFreeze() if ( !obj || typeof obj === 'string' || typeof obj === 'number' ) { return obj as any; } return deepFreeze(obj) as any; } export const DEV_MODE_PLUGIN_NAME = 'dev-mode'; export const NxDBDevModePlugin: RxPlugin = { name: DEV_MODE_PLUGIN_NAME, nxdb: true, init: () => { console.warn( [ '-------------- NxDB dev-mode warning -------------------------------', 'you are seeing this because you use the NxDB dev-mode plugin https://nxpkg.github.io/nxdb/dev-mode.html', 'This is great in development mode, because it will run many checks to ensure', 'that you use NxDB 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 NxDB, check out the Premium Plugins', 'to get access to faster storages and more professional features: https://nxpkg.github.io/nxdb/premium.html', '', 'Also take part in the NxDB User Survey: https://nxpkg.github.io/nxdb/survey.html', '---------------------------------------------------------------------' ].join('\n') ); }, overwritable: { isDevMode() { return true; }, deepFreezeWhenDevMode, tunnelErrorMessage(code: RxErrorKey) { if (!ERROR_MESSAGES[code]) { console.error('NxDB: Error-Code not known: ' + code); throw new Error('Error-Code ' + code + ' not known, contact the maintainer'); } return ERROR_MESSAGES[code]; } }, hooks: { preCreateRxSchema: { after: checkSchema }, preCreateRxDatabase: { after: function (args: RxDatabaseCreator) { ensureDatabaseNameIsValid(args); } }, preCreateRxCollection: { after: function (args: RxCollectionCreator & { name: string; }) { ensureCollectionNameValid(args); checkOrmDocumentMethods(args.schema as any, args.methods); if (args.name.charAt(0) === '_') { throw newRxError('DB2', { name: args.name }); } if (!args.schema) { throw newRxError('DB4', { name: args.name, args }); } } }, createRxDocument: { before: function (doc: RxDocument) { ensurePrimaryKeyValid(doc.primary, doc.toJSON(true)); } }, preCreateRxQuery: { after: function (args) { checkQuery(args); } }, prePrepareQuery: { after: (args) => { checkMangoQuery(args); } }, createRxCollection: { after: (args) => { // check ORM-methods checkOrmMethods(args.creator.statics); checkOrmMethods(args.creator.methods); checkOrmMethods(args.creator.attachments); // check migration strategies if (args.creator.schema && args.creator.migrationStrategies) { checkMigrationStrategies( args.creator.schema, args.creator.migrationStrategies ); } } } } };