@webiny/db 0.0.0-mt-1

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) Webiny
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,20 @@
1
+ # @webiny/db
2
+ [![](https://img.shields.io/npm/dw/webiny-data.svg)](https://www.npmjs.com/package/webiny-data)
3
+ [![](https://img.shields.io/npm/v/webiny-data.svg)](https://www.npmjs.com/package/webiny-data)
4
+ [![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square)](https://github.com/prettier/prettier)
5
+ [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](http://makeapullrequest.com)
6
+
7
+ A set of frequently used data higher order functions.
8
+
9
+ For more information, please visit
10
+ [the official docs](https://github.com/doitadrian/data).
11
+
12
+ ## Install
13
+ ```
14
+ npm install --save @webiny/db
15
+ ```
16
+
17
+ Or if you prefer yarn:
18
+ ```
19
+ yarn add @webiny/db
20
+ ```
package/index.d.ts ADDED
@@ -0,0 +1,72 @@
1
+ export declare type Key = {
2
+ primary?: boolean;
3
+ unique?: boolean;
4
+ name: string;
5
+ fields: {
6
+ name: string;
7
+ }[];
8
+ };
9
+ export declare type Args = {
10
+ __batch?: {
11
+ instance: Batch;
12
+ operation: Operation;
13
+ };
14
+ table?: string;
15
+ meta?: boolean;
16
+ limit?: number;
17
+ sort?: Record<string, 1 | -1>;
18
+ data?: Record<string, any>;
19
+ query?: Record<string, any>;
20
+ keys?: Key[];
21
+ };
22
+ export declare type Result<T = any> = [T, Record<string, any>];
23
+ export interface DbDriver {
24
+ create: (args: Args) => Promise<Result<true>>;
25
+ read: <T = Record<string, any>>(args: Args) => Promise<Result<T[]>>;
26
+ update: (args: Args) => Promise<Result<true>>;
27
+ delete: (args: Args) => Promise<Result<true>>;
28
+ createLog: (args: {
29
+ operation: string;
30
+ data: Args;
31
+ table: string;
32
+ id: string;
33
+ }) => Promise<Result<true>>;
34
+ readLogs: <T = Record<string, any>>(args: {
35
+ table: string;
36
+ }) => Promise<Result<T[]>>;
37
+ }
38
+ export declare type OperationType = "create" | "read" | "update" | "delete";
39
+ export declare type Operation = [OperationType, Args];
40
+ export declare type ConstructorArgs = {
41
+ driver: DbDriver;
42
+ table?: string;
43
+ logTable?: string;
44
+ };
45
+ declare class Db {
46
+ driver: DbDriver;
47
+ table: string;
48
+ logTable?: string;
49
+ constructor({ driver, table, logTable }: ConstructorArgs);
50
+ create(args: Args): Promise<Result<true>>;
51
+ read<T = Record<string, any>>(args: Args): Promise<Result<T[]>>;
52
+ update(args: Args): Promise<Result<true>>;
53
+ delete(args: Args): Promise<Result<true>>;
54
+ createLog(operation: any, args: Args): Promise<Result<true>>;
55
+ readLogs<T = Record<string, any>>(): Promise<Result<T[]>>;
56
+ batch<T0 = any, T1 = any, T2 = any, T3 = any, T4 = any, T5 = any, T6 = any, T7 = any, T8 = any, T9 = any>(): Batch<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9>;
57
+ }
58
+ declare class Batch<T0 = any, T1 = any, T2 = any, T3 = any, T4 = any, T5 = any, T6 = any, T7 = any, T8 = any, T9 = any> {
59
+ db: Db;
60
+ type: "batch" | "transaction";
61
+ id: string;
62
+ meta: Record<string, any>;
63
+ operations: Operation[];
64
+ constructor(db: any);
65
+ push(...operations: Operation[]): this;
66
+ create(...args: Args[]): this;
67
+ read(...args: Args[]): this;
68
+ update(...args: Args[]): this;
69
+ delete(...args: Args[]): this;
70
+ execute(): Promise<[T0?, T1?, T2?, T3?, T4?, T5?, T6?, T7?, T8?, T9?]>;
71
+ }
72
+ export { Batch, Db };
package/index.js ADDED
@@ -0,0 +1,212 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+
5
+ Object.defineProperty(exports, "__esModule", {
6
+ value: true
7
+ });
8
+ exports.Db = exports.Batch = void 0;
9
+
10
+ var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
11
+
12
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
13
+
14
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { (0, _defineProperty2.default)(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
15
+
16
+ // Generates a short and sortable ID, e.g. "1607677774994.tfz58m".
17
+ const shortId = () => {
18
+ const time = new Date().getTime();
19
+ const uniqueId = Math.random().toString(36).slice(-6);
20
+ return `${time}.${uniqueId}`;
21
+ }; // Picks necessary data from received args, ready to be stored in the log table.
22
+
23
+
24
+ const getCreateLogData = args => {
25
+ const {
26
+ table,
27
+ meta,
28
+ limit,
29
+ sort,
30
+ data,
31
+ query,
32
+ keys
33
+ } = args;
34
+ const logData = {
35
+ table,
36
+ meta,
37
+ limit,
38
+ sort,
39
+ data,
40
+ query,
41
+ keys,
42
+ batch: null
43
+ };
44
+
45
+ if (args.__batch) {
46
+ logData.batch = {
47
+ id: args.__batch.instance.id,
48
+ type: args.__batch.instance.type
49
+ };
50
+ }
51
+
52
+ return logData;
53
+ };
54
+
55
+ class Db {
56
+ constructor({
57
+ driver,
58
+ table,
59
+ logTable
60
+ }) {
61
+ (0, _defineProperty2.default)(this, "driver", void 0);
62
+ (0, _defineProperty2.default)(this, "table", void 0);
63
+ (0, _defineProperty2.default)(this, "logTable", void 0);
64
+ this.driver = driver;
65
+ this.table = table;
66
+ this.logTable = logTable;
67
+ }
68
+
69
+ async create(args) {
70
+ const createArgs = _objectSpread(_objectSpread({}, args), {}, {
71
+ table: args.table || this.table
72
+ });
73
+
74
+ await this.createLog("create", createArgs);
75
+ return this.driver.create(createArgs);
76
+ }
77
+
78
+ async read(args) {
79
+ const readArgs = _objectSpread(_objectSpread({}, args), {}, {
80
+ table: args.table || this.table
81
+ });
82
+
83
+ await this.createLog("read", readArgs);
84
+ return this.driver.read(readArgs);
85
+ }
86
+
87
+ async update(args) {
88
+ const updateArgs = _objectSpread(_objectSpread({}, args), {}, {
89
+ table: args.table || this.table
90
+ });
91
+
92
+ await this.createLog("update", updateArgs);
93
+ return this.driver.update(updateArgs);
94
+ }
95
+
96
+ async delete(args) {
97
+ const deleteArgs = _objectSpread(_objectSpread({}, args), {}, {
98
+ table: args.table || this.table
99
+ });
100
+
101
+ await this.createLog("delete", deleteArgs);
102
+ return this.driver.delete(deleteArgs);
103
+ } // Logging functions.
104
+
105
+
106
+ async createLog(operation, args) {
107
+ if (!this.logTable) {
108
+ return;
109
+ }
110
+
111
+ const data = getCreateLogData(args);
112
+ return this.driver.createLog({
113
+ operation,
114
+ data,
115
+ table: this.logTable,
116
+ id: shortId()
117
+ });
118
+ }
119
+
120
+ async readLogs() {
121
+ if (!this.logTable) {
122
+ return;
123
+ }
124
+
125
+ return this.driver.readLogs({
126
+ table: this.logTable
127
+ });
128
+ }
129
+
130
+ batch() {
131
+ return new Batch(this);
132
+ }
133
+
134
+ }
135
+
136
+ exports.Db = Db;
137
+
138
+ class Batch {
139
+ constructor(db) {
140
+ (0, _defineProperty2.default)(this, "db", void 0);
141
+ (0, _defineProperty2.default)(this, "type", void 0);
142
+ (0, _defineProperty2.default)(this, "id", void 0);
143
+ (0, _defineProperty2.default)(this, "meta", void 0);
144
+ (0, _defineProperty2.default)(this, "operations", void 0);
145
+ this.db = db;
146
+ this.type = "batch";
147
+ this.id = shortId();
148
+ this.meta = {};
149
+ this.operations = [];
150
+ }
151
+
152
+ push(...operations) {
153
+ for (let i = 0; i < operations.length; i++) {
154
+ const item = operations[i];
155
+ this.operations.push(item);
156
+ }
157
+
158
+ return this;
159
+ }
160
+
161
+ create(...args) {
162
+ for (let i = 0; i < args.length; i++) {
163
+ this.push(["create", args[i]]);
164
+ }
165
+
166
+ return this;
167
+ }
168
+
169
+ read(...args) {
170
+ for (let i = 0; i < args.length; i++) {
171
+ this.push(["read", args[i]]);
172
+ }
173
+
174
+ return this;
175
+ }
176
+
177
+ update(...args) {
178
+ for (let i = 0; i < args.length; i++) {
179
+ this.push(["update", args[i]]);
180
+ }
181
+
182
+ return this;
183
+ }
184
+
185
+ delete(...args) {
186
+ for (let i = 0; i < args.length; i++) {
187
+ this.push(["delete", args[i]]);
188
+ }
189
+
190
+ return this;
191
+ }
192
+
193
+ async execute() {
194
+ const promises = [];
195
+
196
+ for (let i = 0; i < this.operations.length; i++) {
197
+ const [operation, args] = this.operations[i];
198
+ promises.push(this.db[operation](_objectSpread(_objectSpread({}, args), {}, {
199
+ __batch: {
200
+ instance: this,
201
+ operation: this.operations[i]
202
+ }
203
+ })));
204
+ }
205
+
206
+ const result = Promise.all(promises);
207
+ return result;
208
+ }
209
+
210
+ }
211
+
212
+ exports.Batch = Batch;
package/package.json ADDED
@@ -0,0 +1,28 @@
1
+ {
2
+ "name": "@webiny/db",
3
+ "version": "0.0.0-mt-1",
4
+ "main": "index.js",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "https://github.com/webiny/webiny-js.git"
8
+ },
9
+ "description": "A simple multi-database client.",
10
+ "license": "MIT",
11
+ "publishConfig": {
12
+ "access": "public",
13
+ "directory": "dist"
14
+ },
15
+ "devDependencies": {
16
+ "@babel/cli": "^7.5.5",
17
+ "@babel/core": "^7.5.5",
18
+ "@webiny/cli": "^0.0.0-mt-1",
19
+ "@webiny/project-utils": "^0.0.0-mt-1",
20
+ "rimraf": "^3.0.2",
21
+ "typescript": "^4.1.3"
22
+ },
23
+ "scripts": {
24
+ "build": "yarn webiny run build",
25
+ "watch": "yarn webiny run watch"
26
+ },
27
+ "gitHead": "37736d8456a6ecb342a6c3645060bd9a3f2d4bb0"
28
+ }