@desmat/redis-store 1.0.1 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,30 +1,160 @@
1
- # redis-store
2
- A lightweight wrapper for using Redis as a data store
1
+ # @desmat/redis-store
3
2
 
4
- ## To build
5
- ```
6
- npm run build
7
- ```
3
+ A lightweight library to facilitate using the (in)famously fast in-memory database as your primary data store for your app’s entities and their relationships.
4
+
5
+ Leans into Redis’ strong suits to bring relational aspects to a simple but performant KV store:
6
+ - Lots of small read/writes
7
+ - JSON keys for storing entities (no migration scripts required)
8
+ - ZSET keys to track lists and relations
9
+
10
+ Plays well with Upstash and Vercel but any Redis instance supported via REST API.
11
+
12
+
13
+ ## Getting Started
14
+
15
+ Below shows a simple schema with users and things belonging to users, some data added then queried.
8
16
 
9
- ## Example
10
- File `./src/example.ts` demonstrates simple use of this library.
17
+ `npm run example` to run [example.ts](./src/example.ts).
11
18
 
12
- ### To run:
19
+ ### Install the library into your project
13
20
 
14
- Create `.env` file with:
21
+ ```bash
22
+ npm install @desmat/redis-store
15
23
  ```
24
+
25
+ ### Setup environment variables
26
+
27
+ ```bash
28
+ # your .env file, or provided in launch command
16
29
  KV_REST_API_URL=*****
17
- KV_REST_API_TOKEN=****
30
+ KV_REST_API_TOKEN=*****
18
31
  ```
19
- *Note: Using Vercel KV environment variables enables this library to be used without friction on Vercel's platform, but can be overwritten programmatically.*
20
32
 
21
- then:
33
+ *Note: Using Vercel KV environment variables enables this library to be used without friction on Vercel's platform, but `url` and `token` values can be provided in code.*
34
+
35
+ ### Setup entities and store
36
+
37
+ ```typescript
38
+ import RedisStore from '@desmat/redis-store';
39
+
40
+ type User = {
41
+ id: string, // required; set by .create as short UUID by default, or can be provided
42
+ createdAt: number, // required; set by .create
43
+ name: string,
44
+ };
45
+
46
+ type Thing = {
47
+ id: string,
48
+ createdAt: number,
49
+ createdBy: string, // required to lookup Things by Users
50
+ label: string,
51
+ };
52
+
53
+ const ThingOptions = {
54
+ lookups: {
55
+ user: "createdBy", // enables .find({ user: <USERID> })
56
+ },
57
+ };
58
+
59
+ // with environment variables `KV_REST_API_URL` and `KV_REST_API_TOKEN`
60
+ // otherwise `url` and `token` can be provided to `RedisStore` constructor
61
+ const store = {
62
+ users: new RedisStore<User>({ key: "user" }),
63
+ things: new RedisStore<Thing>({ key: "thing", options: ThingOptions }),
64
+ };
22
65
  ```
23
- npm run example
66
+
67
+ ### Create users and things
68
+
69
+ ```typescript
70
+ const users = await Promise.all([
71
+ store.users.create({ name: "User One" }),
72
+ store.users.create({ name: "User Two" }),
73
+ ]);
74
+
75
+ // users (2): [
76
+ // { id: '<UUID>', createdAt: <TIMEINMILLIS>, name: 'User One' },
77
+ // ...
78
+ // ]
79
+
80
+ // Redis commands executed:
81
+ //
82
+ // JSON.SET user:<UUID> $ '{ "id": "<UUID>", "createdAt": <TIMEINMILLIS>, "name": "User One" }'
83
+ // ZADD users <TIMEINMILLIS> <UUID>
84
+ // ...
85
+
86
+ const things = await Promise.all([
87
+ store.things.create({
88
+ createdBy: users[0].id,
89
+ label: "A thing for user one",
90
+ }),
91
+ store.things.create({
92
+ createdBy: users[0].id,
93
+ label: "Another thing for user one",
94
+ }),
95
+ store.things.create({
96
+ createdBy: users[0].id,
97
+ label: "Yet another thing for user one",
98
+ }),
99
+ store.things.create({
100
+ createdBy: users[1].id,
101
+ label: "A thing for user two",
102
+ }),
103
+ ]);
104
+
105
+ // things (4): [
106
+ // {
107
+ // id: '<UUID>',
108
+ // createdAt: <TIMEINMILLIS>,
109
+ // createdBy: '<USER_UUID>',
110
+ // label: 'A thing for user one'
111
+ // },
112
+ // ...
113
+ // ]
114
+
115
+ // Redis commands executed:
116
+ //
117
+ // JSON.SET thing:<UUID> $ '{ "id": "<UUID>", "createdAt": <TIMEINMILLIS>, "createdBy": "<USER_UUID>", "message": "Another thing for user one" }'
118
+ // ZADD things <TIMEINMILLIS> <UUID>
119
+ // ZADD things:user:<USER_UUID> <TIMEINMILLIS> <UUID>
120
+ // ...
24
121
  ```
25
122
 
26
- ### Or alternatively:
123
+ ### Query things
124
+
125
+ ```typescript
126
+ const allThings = await store.things.find(); // 4 entries
127
+
128
+ // Redis commands executed:
129
+ //
130
+ // ZRANGE things:user:<USER_UUID> 0 -1 REV
131
+ // JSON.MGET thing:<THING1_UUID> thing:<THING2_UUID> ...
132
+
133
+ const latestUserThings = await store.things.find({
134
+ user: users[0].id
135
+ }); // 3 entries
27
136
 
137
+ // Redis commands executed:
138
+ //
139
+ // ZRANGE things:user:<UUID> 0 -1 REV
140
+ // JSON.MGET thing:<THING1_UUID> thing:<THING2_UUID> thing:<THING3_UUID>
28
141
  ```
29
- export KV_REST_API_URL=***** KV_REST_API_TOKEN=***** && npm run example
142
+
143
+ ### Cleanup (soft delete by default)
144
+
145
+ ```typescript
146
+ await Promise.all([
147
+ ...users.map((user: User) => store.users.delete(user.id)),
148
+ ...things.map((thing: Thing) => store.things.delete(thing.id)),
149
+ ]);
150
+
151
+ // Redis commands executed:
152
+ //
153
+ // JSON.SET user:<UUID> $.deletedAt <TIMEINMILLIS>
154
+ // ZREM users <UUID>
155
+ // ...
156
+ // JSON.SET thing:<UUID> $.deletedAt <TIMEINMILLIS>
157
+ // ZREM things <UUID>
158
+ // ZREM testthings:user:<USER_UUID> <UUID>
159
+ // ...
30
160
  ```
package/dist/example.js CHANGED
@@ -1,24 +1,20 @@
1
1
  "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
2
5
  Object.defineProperty(exports, "__esModule", { value: true });
3
- const index_1 = require("./index");
6
+ const index_1 = __importDefault(require("./index"));
4
7
  require('dotenv').config();
5
- const ThingRecordOptions = {
8
+ const ThingOptions = {
6
9
  lookups: {
7
10
  user: "createdBy", // enable .find({ user: <USERID> })
8
11
  },
9
12
  };
13
+ // with environment variables KV_REST_API_URL and KV_REST_API_TOKEN
14
+ // or `url` and `token` keys provided to RedisStore constructor
10
15
  const store = {
11
- users: new index_1.RedisStore({
12
- key: "user",
13
- options: { hardDelete: false },
14
- }),
15
- things: new index_1.RedisStore({
16
- key: "thing",
17
- options: {
18
- ...ThingRecordOptions,
19
- hardDelete: false
20
- }
21
- }),
16
+ users: new index_1.default({ key: "user" }),
17
+ things: new index_1.default({ key: "thing", options: ThingOptions }),
22
18
  };
23
19
  (async function () {
24
20
  // create users
package/dist/index.d.ts CHANGED
@@ -5,7 +5,7 @@ export type RedisStoreRecord = {
5
5
  updatedAt?: number;
6
6
  deletedAt?: number;
7
7
  };
8
- export declare class RedisStore<T extends RedisStoreRecord> {
8
+ export default class RedisStore<T extends RedisStoreRecord> {
9
9
  redis: Redis;
10
10
  key: string;
11
11
  setKey: string;
package/dist/index.js CHANGED
@@ -19,7 +19,6 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
19
19
  return (mod && mod.__esModule) ? mod : { "default": mod };
20
20
  };
21
21
  Object.defineProperty(exports, "__esModule", { value: true });
22
- exports.RedisStore = void 0;
23
22
  const moment_1 = __importDefault(require("moment"));
24
23
  const utils_1 = require("@desmat/utils");
25
24
  const redis_1 = require("@upstash/redis");
@@ -275,4 +274,4 @@ class RedisStore {
275
274
  return value ? { ...value, deletedAt } : undefined;
276
275
  }
277
276
  }
278
- exports.RedisStore = RedisStore;
277
+ exports.default = RedisStore;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@desmat/redis-store",
3
- "version": "1.0.1",
3
+ "version": "1.0.2",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "exports": {
@@ -13,9 +13,7 @@
13
13
  ],
14
14
  "scripts": {
15
15
  "build": "tsc",
16
- "example": "ts-node --skipProject ./src/example.ts",
17
- "repl": "ts-node --skipProject -i -e 'console.log(\"Exec `.load ./scratchpad.ts` to run load a starter environment; Ctrl-D or Ctrl-C twice to exit.\")'",
18
- "scratchpad": "ts-node --skipProject ./scratchpad.ts"
16
+ "example": "ts-node --skipProject ./src/example.ts"
19
17
  },
20
18
  "repository": {
21
19
  "type": "git",