retoolrpc 0.1.1 → 0.1.3

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/dist/index.mjs ADDED
@@ -0,0 +1,129 @@
1
+ export { R as RetoolRPC } from './rpc-c0b37172.mjs';
2
+ import 'uuid';
3
+ import 'ts-dedent';
4
+ import 'node-fetch';
5
+ import 'abort-controller';
6
+
7
+ // See documentation about mixin:
8
+ // https://www.typescriptlang.org/docs/handbook/mixins.html
9
+ // https://lit.dev/docs/composition/mixins/#typing-the-subclass
10
+ function sequelizeMixin(Base) {
11
+ class RetoolRPCSequelize extends Base {
12
+ registerModel(args) {
13
+ registerModel({ rpc: this, ...args });
14
+ }
15
+ }
16
+ return RetoolRPCSequelize;
17
+ }
18
+ function capitalize(str) {
19
+ return str.charAt(0).toUpperCase() + str.slice(1);
20
+ }
21
+ function registerModel({ rpc, model, readAttributes, writeAttributes, findByAttributes, }) {
22
+ const modelName = capitalize(model.name);
23
+ // this will give us not just the name of the attribute, but also their type
24
+ const modelAttributes = model.getAttributes();
25
+ readAttributes = readAttributes || Object.keys(modelAttributes);
26
+ writeAttributes = writeAttributes || Object.keys(modelAttributes);
27
+ findByAttributes = findByAttributes || readAttributes;
28
+ const findByAttributeArgs = {};
29
+ for (const attr of findByAttributes) {
30
+ findByAttributeArgs[attr] = { type: 'string' };
31
+ }
32
+ const writeAttributeArgs = {};
33
+ for (const attr of writeAttributes) {
34
+ writeAttributeArgs[attr] = { type: 'string' };
35
+ }
36
+ // register a set of functions for a model
37
+ rpc.register({
38
+ name: `${modelName} > create`,
39
+ arguments: writeAttributeArgs,
40
+ implementation: async (args) => {
41
+ if (typeof args !== 'object' || Array.isArray(args)) {
42
+ throw 'attributes must be an object';
43
+ }
44
+ const record = await model.create(args);
45
+ const recordJson = {};
46
+ for (const key of readAttributes || Object.keys(args)) {
47
+ recordJson[key] = record.get(key);
48
+ }
49
+ return recordJson;
50
+ },
51
+ });
52
+ rpc.register({
53
+ name: `${modelName} > update`,
54
+ arguments: {
55
+ primaryKey: { type: 'string', required: true },
56
+ ...writeAttributeArgs,
57
+ },
58
+ implementation: async ({ primaryKey, ...attributes }) => {
59
+ return model.update(attributes, {
60
+ where: {
61
+ [model.primaryKeyAttribute]: primaryKey,
62
+ },
63
+ });
64
+ },
65
+ });
66
+ rpc.register({
67
+ name: `${modelName} > createOrUpdate`,
68
+ arguments: {
69
+ findAttributes: { type: 'dict', required: true },
70
+ ...writeAttributeArgs,
71
+ },
72
+ implementation: async ({ findAttributes, ...writeAttributes }) => {
73
+ // Note: this is susceptible to race condition if there is no unique index
74
+ // on the find attributes. It's the user's responsibility to avoid
75
+ // duplicate inserts
76
+ return model.findOne({ where: findAttributes }).then(function (record) {
77
+ // update
78
+ if (record) {
79
+ return record.update(writeAttributes);
80
+ }
81
+ // insert
82
+ return model.create({
83
+ ...findAttributes,
84
+ ...writeAttributes,
85
+ });
86
+ });
87
+ },
88
+ });
89
+ rpc.register({
90
+ name: `${modelName} > findAll`,
91
+ arguments: {
92
+ offset: { type: 'number' },
93
+ limit: { type: 'number' },
94
+ },
95
+ implementation: async ({ offset, limit }) => {
96
+ return model.findAll({
97
+ attributes: readAttributes,
98
+ raw: true,
99
+ offset: offset || 0,
100
+ limit: limit || 100,
101
+ });
102
+ },
103
+ });
104
+ rpc.register({
105
+ name: `${modelName} > findByPk`,
106
+ arguments: {
107
+ primaryKey: { type: 'string', required: true },
108
+ },
109
+ implementation: async ({ primaryKey }) => {
110
+ return model.findByPk(primaryKey, {
111
+ attributes: readAttributes,
112
+ raw: true,
113
+ });
114
+ },
115
+ });
116
+ rpc.register({
117
+ name: `${modelName} > findBy`,
118
+ arguments: findByAttributeArgs,
119
+ implementation: async (attributesValues) => {
120
+ return model.findAll({
121
+ where: attributesValues,
122
+ attributes: readAttributes,
123
+ raw: true,
124
+ });
125
+ },
126
+ });
127
+ }
128
+
129
+ export { sequelizeMixin };
package/dist/package.json CHANGED
@@ -1,20 +1,42 @@
1
1
  {
2
2
  "name": "retoolrpc",
3
- "version": "0.1.0",
3
+ "version": "0.1.3",
4
4
  "description": "TypeScript package for Retool RPC",
5
+ "keywords": [],
6
+ "homepage": "https://github.com/tryretool/retoolrpc#readme",
7
+ "bugs": {
8
+ "url": "https://github.com/tryretool/retoolrpc/issues"
9
+ },
10
+ "repository": {
11
+ "type": "git",
12
+ "url": "git+https://github.com/tryretool/retoolrpc.git"
13
+ },
14
+ "license": "MIT",
5
15
  "exports": {
6
16
  ".": {
7
- "import": "./dist/index.js",
17
+ "import": "./dist/index.mjs",
8
18
  "require": "./dist/cjs/index.js"
9
19
  },
10
20
  "./package.json": "./package.json"
11
21
  },
12
- "repository": {
13
- "type": "git",
14
- "url": "git+https://github.com/tryretool/retoolrpc.git"
22
+ "scripts": {
23
+ "build": "yarn build:cjs && yarn build:esm",
24
+ "build:cjs": "cross-env MODE=cjs rollup -c",
25
+ "build:esm": "rollup -c",
26
+ "bumpVersion": "ts-node scripts/bumpVersion.ts",
27
+ "example": "nodemon -V -L --watch src -e ts example.ts",
28
+ "release": "yarn build && npm publish --access public",
29
+ "test": "vitest",
30
+ "test:api": "tsc --project tsconfig.json"
31
+ },
32
+ "dependencies": {
33
+ "@types/node-fetch": "^2.6.4",
34
+ "@types/uuid": "^9.0.2",
35
+ "abort-controller": "^3.0.0",
36
+ "node-fetch": "^2.6",
37
+ "ts-dedent": "^2.2.0",
38
+ "uuid": "^9.0.0"
15
39
  },
16
- "license": "MIT",
17
- "keywords": [],
18
40
  "devDependencies": {
19
41
  "@rollup/plugin-typescript": "^8.2.0",
20
42
  "@types/fs-extra": "^11.0.1",
@@ -31,27 +53,5 @@
31
53
  "tslib": "^2.1.0",
32
54
  "typescript": "5.2.2",
33
55
  "vitest": "^0.34.5"
34
- },
35
- "dependencies": {
36
- "@types/node-fetch": "^2.6.4",
37
- "@types/uuid": "^9.0.2",
38
- "abort-controller": "^3.0.0",
39
- "node-fetch": "^2.6",
40
- "ts-dedent": "^2.2.0",
41
- "uuid": "^9.0.0"
42
- },
43
- "scripts": {
44
- "build:cjs": "cross-env MODE=cjs rollup -c",
45
- "build:esm": "rollup -c",
46
- "build": "yarn build:cjs && yarn build:esm",
47
- "example": "nodemon -V -L --watch src -e ts example.ts",
48
- "bumpVersion": "ts-node scripts/bumpVersion.ts",
49
- "release": "yarn build && npm publish --access public",
50
- "test": "vitest",
51
- "test:api": "tsc --project tsconfig.json"
52
- },
53
- "homepage": "https://github.com/tryretool/retoolrpc#readme",
54
- "bugs": {
55
- "url": "https://github.com/tryretool/retoolrpc/issues"
56
56
  }
57
57
  }