Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.
TypeScript ai-agentsbill-of-materialserpinventory-managementleanmanufacturingmanufacturing-execution-systemmcp-servermesmrpopen-source-erppostgresqlproduction-planningqmsquality-management-systemreact-routerself-hostedsupabasetraceabilitytypescript
feat: add ability to run redis locally
Brad Barbin4158a6dff9b9
diff --git a/package-lock.json b/package-lock.jsonindex 4997604111..eb82abc05e 100644--- a/package-lock.json+++ b/package-lock.json@@ -1815,6 +1815,11 @@"@swc/helpers": "^0.5.0"}},+ "node_modules/@ioredis/commands": {+ "version": "1.2.0",+ "resolved": "https://registry.npmjs.org/@ioredis/commands/-/commands-1.2.0.tgz",+ "integrity": "sha512-Sx1pU8EM64o2BrqNpEO1CNLtKQwyhuXuqyfH7oGKCk+1a33d2r5saW8zNwm3j6BTExtjrv2BxTgzzkMwts6vGg=="+ },"node_modules/@isaacs/cliui": {"version": "8.0.2","resolved": "https://registry.npmjs.org/@isaacs/cliui/-/cliui-8.0.2.tgz",@@ -9006,6 +9011,16 @@"resolved": "https://registry.npmjs.org/@types/http-cache-semantics/-/http-cache-semantics-4.0.4.tgz","integrity": "sha512-1m0bIFVc7eJWyve9S0RnuRgcQqF/Xd5QsUZAZeQFr1Q3/p9JWoQQEqmVy+DPTNpGXwhgIetAoYF8JSc33q29QA=="},+ "node_modules/@types/ioredis": {+ "version": "5.0.0",+ "resolved": "https://registry.npmjs.org/@types/ioredis/-/ioredis-5.0.0.tgz",+ "integrity": "sha512-zJbJ3FVE17CNl5KXzdeSPtdltc4tMT3TzC6fxQS0sQngkbFZ6h+0uTafsRqu+eSLIugf6Yb0Ea0SUuRr42Nk9g==",+ "deprecated": "This is a stub types definition. ioredis provides its own type definitions, so you do not need this installed.",+ "dev": true,+ "dependencies": {+ "ioredis": "*"+ }+ },"node_modules/@types/istanbul-lib-coverage": {"version": "2.0.6","resolved": "https://registry.npmjs.org/@types/istanbul-lib-coverage/-/istanbul-lib-coverage-2.0.6.tgz",@@ -11758,6 +11773,14 @@"node": ">=6"}},+ "node_modules/cluster-key-slot": {+ "version": "1.1.2",+ "resolved": "https://registry.npmjs.org/cluster-key-slot/-/cluster-key-slot-1.1.2.tgz",+ "integrity": "sha512-RMr0FhtfXemyinomL4hrWcYJxmX6deFdCxpJzhDttxgO1+bcCnkk+9drydLVDmAMG7NE6aN/fl4F7ucU/90gAA==",+ "engines": {+ "node": ">=0.10.0"+ }+ },"node_modules/cmd-shim": {"version": "6.0.2","resolved": "https://registry.npmjs.org/cmd-shim/-/cmd-shim-6.0.2.tgz",@@ -13221,6 +13244,14 @@"resolved": "https://registry.npmjs.org/delegates/-/delegates-1.0.0.tgz","integrity": "sha512-bd2L678uiWATM6m5Z1VzNCErI3jiGzt6HGY8OVICs40JQq/HALfbyNJmp0UDakEY4pMMaN0Ly5om/B1VI/+xfQ=="},+ "node_modules/denque": {+ "version": "2.1.0",+ "resolved": "https://registry.npmjs.org/denque/-/denque-2.1.0.tgz",+ "integrity": "sha512-HVQE3AAb/pxF8fQAoiqpvg9i3evqug3hoiwakOyZAwJm+6vZehbkYXZ0l4JxS+I3QxM97v5aaRNhj8v5oBhekw==",+ "engines": {+ "node": ">=0.10"+ }+ },"node_modules/depd": {"version": "2.0.0","resolved": "https://registry.npmjs.org/depd/-/depd-2.0.0.tgz",@@ -17247,6 +17278,29 @@"loose-envify": "^1.0.0"}},+ "node_modules/ioredis": {+ "version": "5.4.1",+ "resolved": "https://registry.npmjs.org/ioredis/-/ioredis-5.4.1.tgz",+ "integrity": "sha512-2YZsvl7jopIa1gaePkeMtd9rAcSjOOjPtpcLlOeusyO+XH2SK5ZcT+UCrElPP+WVIInh2TzeI4XW9ENaSLVVHA==",+ "dependencies": {+ "@ioredis/commands": "^1.1.1",+ "cluster-key-slot": "^1.1.0",+ "debug": "^4.3.4",+ "denque": "^2.1.0",+ "lodash.defaults": "^4.2.0",+ "lodash.isarguments": "^3.1.0",+ "redis-errors": "^1.2.0",+ "redis-parser": "^3.0.0",+ "standard-as-callback": "^2.1.0"+ },+ "engines": {+ "node": ">=12.22.0"+ },+ "funding": {+ "type": "opencollective",+ "url": "https://opencollective.com/ioredis"+ }+ },"node_modules/ip-address": {"version": "9.0.5","resolved": "https://registry.npmjs.org/ip-address/-/ip-address-9.0.5.tgz",@@ -19723,11 +19777,21 @@"resolved": "https://registry.npmjs.org/lodash.debounce/-/lodash.debounce-4.0.8.tgz","integrity": "sha512-FT1yDzDYEoYWhnSGnpE/4Kj1fLZkDFyqRb7fNt6FdYOSxlUWAtp42Eh6Wb0rGIv/m9Bgo7x4GhQbm5Ys4SG5ow=="},+ "node_modules/lodash.defaults": {+ "version": "4.2.0",+ "resolved": "https://registry.npmjs.org/lodash.defaults/-/lodash.defaults-4.2.0.tgz",+ "integrity": "sha512-qjxPLHd3r5DnsdGacqOMU6pb/avJzdh9tFX2ymgoZE27BmjXrNy/y4LoaiTeAb+O3gL8AfpJGtqfX/ae2leYYQ=="+ },"node_modules/lodash.get": {"version": "4.4.2","resolved": "https://registry.npmjs.org/lodash.get/-/lodash.get-4.4.2.tgz","integrity": "sha512-z+Uw/vLuy6gQe8cfaFWD7p0wVv8fJl3mbzXh33RS+0oW2wvUqiRXiQ69gLWSLpgB5/6sU+r6BlQR0MBILadqTQ=="},+ "node_modules/lodash.isarguments": {+ "version": "3.1.0",+ "resolved": "https://registry.npmjs.org/lodash.isarguments/-/lodash.isarguments-3.1.0.tgz",+ "integrity": "sha512-chi4NHZlZqZD18a0imDHnZPrDeBbTtVN7GXMwuGdRH9qotxAjYs3aVLKc7zNOG9eddR5Ksd8rvFEBc9SsggPpg=="+ },"node_modules/lodash.isplainobject": {"version": "4.0.6","resolved": "https://registry.npmjs.org/lodash.isplainobject/-/lodash.isplainobject-4.0.6.tgz",@@ -26031,6 +26095,25 @@"resolved": "https://registry.npmjs.org/reading-time/-/reading-time-1.5.0.tgz","integrity": "sha512-onYyVhBNr4CmAxFsKS7bz+uTLRakypIe4R+5A824vBSkQy/hB3fZepoVEf8OVAxzLvK+H/jm9TzpI3ETSm64Kg=="},+ "node_modules/redis-errors": {+ "version": "1.2.0",+ "resolved": "https://registry.npmjs.org/redis-errors/-/redis-errors-1.2.0.tgz",+ "integrity": "sha512-1qny3OExCf0UvUV/5wpYKf2YwPcOqXzkwKKSmKHiE6ZMQs5heeE/c8eXK+PNllPvmjgAbfnsbpkGZWy8cBpn9w==",+ "engines": {+ "node": ">=4"+ }+ },+ "node_modules/redis-parser": {+ "version": "3.0.0",+ "resolved": "https://registry.npmjs.org/redis-parser/-/redis-parser-3.0.0.tgz",+ "integrity": "sha512-DJnGAeenTdpMEH6uAJRK/uiyEIH9WVsUmoLwzudwGJUwZPp80PDBWPHXSAGNPwNvIXAbe7MSUB1zQFugFml66A==",+ "dependencies": {+ "redis-errors": "^1.0.0"+ },+ "engines": {+ "node": ">=4"+ }+ },"node_modules/reduce-css-calc": {"version": "1.3.0","resolved": "https://registry.npmjs.org/reduce-css-calc/-/reduce-css-calc-1.3.0.tgz",@@ -27961,6 +28044,11 @@"node": ">=8"}},+ "node_modules/standard-as-callback": {+ "version": "2.1.0",+ "resolved": "https://registry.npmjs.org/standard-as-callback/-/standard-as-callback-2.1.0.tgz",+ "integrity": "sha512-qoRRSyROncaz1z0mvYqIE4lCd9p2R90i6GxW3uZv5ucSu8tU7B5HXUP1gG8pVZsYNVaXjk8ClXHPttLyxAL48A=="+ },"node_modules/static-extend": {"version": "0.1.2","resolved": "https://registry.npmjs.org/static-extend/-/static-extend-0.1.2.tgz",@@ -32493,11 +32581,13 @@"version": "*","license": "MIT","dependencies": {- "@upstash/redis": "1.28.4"+ "@upstash/redis": "1.28.4",+ "ioredis": "^5.2.4"},"devDependencies": {"@carbon/logger": "*","@carbon/tsconfig": "*",+ "@types/ioredis": "^5.0.0","@types/node": "^18.15.1","dotenv": "16.3.1","eslint": "^8.23.1",diff --git a/package.json b/package.jsonindex b83513cb36..bb1515723d 100755--- a/package.json+++ b/package.json@@ -33,6 +33,7 @@"db:build": "run-s db:kill db:start setup","db:function:new": "npm run db:function:new -w @carbon/database","db:kill": "npm run db:kill -w @carbon/database",+ "db:kill:redis": "npm run db:kill -w @carbon/redis","db:migrate": "npm run db:migrate -w @carbon/database","db:migrate:force": "turbo run db:migrate:force --no-cache","db:migrate:new": "npm run db:migrate:new -w @carbon/database",@@ -41,6 +42,7 @@"db:restart": "run-s db:stop db:start","db:seed": "turbo run db:seed --no-cache","db:start": "npm run db:start -w @carbon/database",+ "db:start:redis": "npm run db:start -w @carbon/redis","db:stop": "npm run db:stop -w @carbon/database","db:types": "npm run db:types -w @carbon/database","deploy": "turbo run deploy",diff --git a/packages/redis/docker-compose.yml b/packages/redis/docker-compose.ymlnew file mode 100644index 0000000000..5d9cc08a2a--- /dev/null+++ b/packages/redis/docker-compose.yml@@ -0,0 +1,14 @@+version: "3.8"+name: "carbon-redis"+services:+ redis:+ image: redis:7.0-alpine+ restart: always+ ports:+ - "6389:6379"+ command: redis-server --save 20 1 --loglevel warning+ volumes:+ - cache:/data+volumes:+ cache:+ driver: localdiff --git a/packages/redis/package.json b/packages/redis/package.jsonindex 61503ed721..23808a2eb1 100644--- a/packages/redis/package.json+++ b/packages/redis/package.json@@ -1,11 +1,13 @@{"name": "@carbon/redis","dependencies": {+ "ioredis": "^5.2.4","@upstash/redis": "1.28.4"},"devDependencies": {"@carbon/logger": "*","@carbon/tsconfig": "*",+ "@types/ioredis": "^5.0.0","@types/node": "^18.15.1","dotenv": "16.3.1","eslint": "^8.23.1",@@ -25,6 +27,8 @@"scripts": {"build": "tsup","clean": "rimraf .turbo node_modules dist",+ "db:kill": "docker-compose down",+ "db:start": "docker-compose up -d","dev": "tsup --watch","lint": "TIMING=1 eslint \"src/**/*.ts*\""},diff --git a/packages/redis/src/client.ts b/packages/redis/src/client.tsindex ef488b56fd..8c7bd98379 100644--- a/packages/redis/src/client.ts+++ b/packages/redis/src/client.ts@@ -1,38 +1,69 @@import type { Redis as RedisType } from "@upstash/redis";import { Redis } from "@upstash/redis";+import type {+ RedisOptions as IoRedisOptions,+ Redis as IoRedisType,+} from "ioredis";+import IoRedis from "ioredis";+const USE_LOCAL_REDIS = !!process.env.USE_LOCAL_REDIS;+const REDIS_URL = process.env.REDIS_URL;const UPSTASH_REDIS_REST_URL = process.env.UPSTASH_REDIS_REST_URL;const UPSTASH_REDIS_REST_TOKEN = process.env.UPSTASH_REDIS_REST_TOKEN;-let redis: RedisType;++let redis: RedisType | IoRedisType;declare global {- var __redis: RedisType | undefined;+ var __redis: RedisType | IoRedisType | undefined;}-if (!UPSTASH_REDIS_REST_URL) {- throw new Error("UPSTASH_REDIS_REST_URL is not defined");-}+if (USE_LOCAL_REDIS) {+ if (!REDIS_URL) {+ throw new Error("REDIS_URL is not defined");+ }-if (!UPSTASH_REDIS_REST_TOKEN) {- throw new Error("UPSTASH_REDIS_REST_TOKEN is not defined");-}+ const config: IoRedisOptions = {+ enableReadyCheck: false,+ maxRetriesPerRequest: null,+ };-// this is needed because in development we don't want to restart-// the server with every change, but we want to make sure we don't-// create a new connection to the Redis with every change either.-if (process.env.NODE_ENV === "production") {- redis = new Redis({- url: UPSTASH_REDIS_REST_URL,- token: UPSTASH_REDIS_REST_TOKEN,- });+ // this is needed because in development we don't want to restart+ // the server with every change, but we want to make sure we don't+ // create a new connection to the Redis with every change either.+ if (process.env.NODE_ENV === "production") {+ redis = new IoRedis(REDIS_URL, config);+ } else {+ if (!global.__redis) {+ global.__redis = new IoRedis(REDIS_URL, config);+ }+ redis = global.__redis;+ }} else {- if (!global.__redis) {- global.__redis = new Redis({+ if (!UPSTASH_REDIS_REST_URL) {+ throw new Error("UPSTASH_REDIS_REST_URL is not defined");+ }++ if (!UPSTASH_REDIS_REST_TOKEN) {+ throw new Error("UPSTASH_REDIS_REST_TOKEN is not defined");+ }++ // this is needed because in development we don't want to restart+ // the server with every change, but we want to make sure we don't+ // create a new connection to the Redis with every change either.+ if (process.env.NODE_ENV === "production") {+ redis = new Redis({url: UPSTASH_REDIS_REST_URL,token: UPSTASH_REDIS_REST_TOKEN,});+ } else {+ if (!global.__redis) {+ global.__redis = new Redis({+ url: UPSTASH_REDIS_REST_URL,+ token: UPSTASH_REDIS_REST_TOKEN,+ });+ }+ redis = global.__redis;}- redis = global.__redis;}export default redis;