chain-db-ts 0.0.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/package.json ADDED
@@ -0,0 +1,40 @@
1
+ {
2
+ "name": "chain-db-ts",
3
+ "version": "0.0.1",
4
+ "description": "Chain DB Client for Javascript/Typescript Node apps.",
5
+ "main": "./dist/cjs/index.js",
6
+ "module": "./index.js",
7
+ "types": "./index.d.ts",
8
+ "scripts": {
9
+ "clean": "rm -rf dist features index.d.ts index.js",
10
+ "build": "npm run clean; tsc -p tsconfig.prod.json; npm run build:cjs",
11
+ "build:esm": "tsc",
12
+ "build:cjs": "tsc --module commonjs --outDir dist/cjs",
13
+ "start": "npm run build && node build/index.js",
14
+ "start:dev": "npx nodemon",
15
+ "prepack": "npm install; npm run build",
16
+ "lint": "eslint \"{**/*,*}.{js,ts,jsx,tsx}\"",
17
+ "prettier": "prettier --write \"{src,tests,example/src}/**/*.{js,ts,jsx,tsx}\""
18
+ },
19
+ "keywords": [
20
+ "chain",
21
+ "db",
22
+ "client",
23
+ "database"
24
+ ],
25
+ "author": "Wenderson Pires wendersonpdas@gmail.com",
26
+ "license": "MIT",
27
+ "devDependencies": {
28
+ "@types/node": "^18.15.5",
29
+ "@types/sha256": "^0.2.0",
30
+ "nodemon": "^3.0.1",
31
+ "ts-node": "^10.9.1",
32
+ "tslib": "^2.5.0",
33
+ "typescript": "^4.7.3"
34
+ },
35
+ "repository": "git://github.com/wpdas/chain-db-ts.git",
36
+ "dependencies": {
37
+ "axios": "^1.4.0",
38
+ "sha256": "^0.2.0"
39
+ }
40
+ }
package/readme.md ADDED
@@ -0,0 +1,186 @@
1
+ # Chain DB - TS
2
+
3
+ ChainDB TS is a library that allows the usage of the ChainDB database in TypeScript and JavaScript projects.
4
+
5
+ Chain DB is a Story-driven database. This new type of system uses some features used in blockchain technology. Each change generates a transaction that is saved in a block. The network works centrally, so persistent data is not decentralized.
6
+
7
+ This database has some features by default, such as: create user account, get user account, transfer units and get transfer records as well as the main feature that is tables.
8
+
9
+ The `unit` property present in each user's account can be anything the project wants, it can be a type of currency, item, resource.
10
+
11
+ Visit the [Chain DB repository](https://github.com/wpdas/chain-db) to get to know more.
12
+
13
+ ## Install
14
+
15
+ Install using npm or yarn:
16
+
17
+ ```sh
18
+ npm install chain-db-ts
19
+
20
+ # or
21
+
22
+ yarn add chain-db-ts
23
+ ```
24
+
25
+ ## Usage examples
26
+
27
+ First of all, it's good to know that all requests return a `BasicResponse<D>` object that has the following structure:
28
+
29
+ **success:** `boolean` (informs if the transaction was successful) <br/>
30
+ **error_msg:** `string` (error message) <br/>
31
+ **data:** `D` (any expected data type depending on the request) <br/>
32
+
33
+ Make sure you have the database running on your local machine or use the server link from where the database is running.
34
+
35
+ ### Table
36
+
37
+ Tables must be simple class with default values, empty or not. This class will be used as a reference to create the table's fields.
38
+
39
+ When it's time to persist the table's data on the chain, just call the `persit` database function.
40
+
41
+ ```ts
42
+ // Greeting Table
43
+ class GreetingTable {
44
+ public greeting = 'Hi'
45
+ }
46
+ ```
47
+
48
+ ```ts
49
+ import { connect } from 'chain-db-ts'
50
+ import { GreetingTable } from './tables'
51
+
52
+ const main async () {
53
+ // server | db-name | user | password
54
+ // If the `server` parameter is empty(null), then "http://localhost:2818" will be used.
55
+ const db = connect('http://localhost:2818', 'test-db', 'root', '1234')
56
+
57
+ // Initialize the "greeting" table using the "GreetingTable"
58
+ // class as a template. If there is already any data saved in
59
+ // the chain, this data will be populated in the table instance.
60
+ const greetingTable = await db.get_table('greeting', new GreetingTable())
61
+ console.log(greetingTable.table) // { greeting: 'Hi' }
62
+
63
+ // Mutating data
64
+ greetingTable.table.greeting = "Hello my dear!"
65
+ await greetingTable.persist() // Data is persisted on the blockchain
66
+
67
+ // See the most updated values of the table
68
+ console.log(greetingTable.table) // { greeting: 'Hello my dear!' }
69
+ }
70
+ main()
71
+ ```
72
+
73
+ The next examples will not include the `db` implementation, ` import` lines and the `const main async () {}` block as this is implied.
74
+
75
+ ### Create User Account
76
+
77
+ This is a default database feature that allows you to create user accounts within the database. As these are hashed accounts, the only data required is: Username and Password. This data is hashed, that is, only the user with the correct data can access the data.
78
+
79
+ It is not possible to recover an account in which the user has forgotten access data.
80
+
81
+ ```ts
82
+ const user_name = 'wenderson.fake'
83
+ const user_pass = '1234'
84
+
85
+ // Check if the given name is already in use
86
+ const user_name_taken = await db.check_user_name(user_name)
87
+ if (!user_name_taken.success) {
88
+ // user name | password | units (optional) | password hint (optional - may be used in the future versions)
89
+ const user = await db.create_user_account(user_name, user_pass, 2)
90
+ console.log(user.data)
91
+ // {
92
+ // id: 'b2e4e7c15f733d8c18836ffd22051ed855226d9041fb9452f17f498fc2bcbce3',
93
+ // user_name: 'wenderson.fake',
94
+ // units: 2
95
+ // }
96
+ }
97
+ ```
98
+
99
+ ### Get User Account Info
100
+
101
+ This feature can be used for the "Login/Sign In" action.
102
+
103
+ ```ts
104
+ const user_name = 'wenderson.fake'
105
+ const user_pass = '1234'
106
+ const user = await db.get_user_account(user_name, user_pass)
107
+ console.log(user.data)
108
+ // {
109
+ // id: 'b2e4e7c15f733d8c18836ffd22051ed855226d9041fb9452f17f498fc2bcbce3',
110
+ // user_name: 'wenderson.fake',
111
+ // units: 2
112
+ // }
113
+ ```
114
+
115
+ ### Get User Account Info By User Id
116
+
117
+ Just another way to fetch the user info.
118
+
119
+ ```ts
120
+ const wenderson_id = 'b2e4e7c15f733d8c18836ffd22051ed855226d9041fb9452f17f498fc2bcbce3'
121
+ const user = await db.get_user_account_by_id(wenderson_id)
122
+ console.log(user.data)
123
+ // {
124
+ // id: 'b2e4e7c15f733d8c18836ffd22051ed855226d9041fb9452f17f498fc2bcbce3',
125
+ // user_name: 'wenderson.fake',
126
+ // units: 2
127
+ // }
128
+ ```
129
+
130
+ ### Transfer Units Between Two Users
131
+
132
+ As said before, `unit` property present in each user's account can be anything the project wants, it can be a type of currency, item, resource.
133
+
134
+ Below is an example of user `wenderson` trying to send 2 units to `suly`:
135
+
136
+ ```ts
137
+ const wenderson_id = 'b2e4e7c15f733d8c18836ffd22051ed855226d9041fb9452f17f498fc2bcbce3'
138
+ const suly_id = '136c406933d98e5c8bb4820f5145869bb5ad40647b768de4e9adb2a52d0dea2f'
139
+
140
+ const wenderson_data = await db.get_user_account_by_id(wenderson_id)
141
+ const units_to_transfer = 2
142
+
143
+ if (wenderson_data.data!.units >= units_to_transfer) {
144
+ const res = await db.transfer_units(wenderson_id, suly_id, units_to_transfer)
145
+ console.log(res.success)
146
+ // true / false
147
+ }
148
+ ```
149
+
150
+ ### Fetching the Latest Units Transfer Record
151
+
152
+ Use this feature to get the last unit transfer record involving a user.
153
+
154
+ ```ts
155
+ const wenderson_id = 'b2e4e7c15f733d8c18836ffd22051ed855226d9041fb9452f17f498fc2bcbce3'
156
+ const last_units_transference_record = await db.get_transfer_by_user_id(wenderson_id)
157
+ console.log(last_units_transference_record.data)
158
+ // {
159
+ // from: 'b2e4e7c15f733d8c18836ffd22051ed855226d9041fb9452f17f498fc2bcbce3',
160
+ // to: '136c406933d98e5c8bb4820f5145869bb5ad40647b768de4e9adb2a52d0dea2f',
161
+ // units: 2
162
+ // }
163
+ ```
164
+
165
+ ### Fetching All the Transfer of Units Records
166
+
167
+ Use this feature to get the last unit transfer record involving a user.
168
+
169
+ ```ts
170
+ const wenderson_id = 'b2e4e7c15f733d8c18836ffd22051ed855226d9041fb9452f17f498fc2bcbce3'
171
+ const all_units_transfers_record = await db.get_all_transfers_by_user_id(wenderson_id)
172
+ console.log(all_units_transfers_record.data)
173
+ // [
174
+ // {
175
+ // from: 'b2e4e7c15f733d8c18836ffd22051ed855226d9041fb9452f17f498fc2bcbce3',
176
+ // to: '136c406933d98e5c8bb4820f5145869bb5ad40647b768de4e9adb2a52d0dea2f',
177
+ // units: 2
178
+ // },
179
+ // {
180
+ // from: '136c406933d98e5c8bb4820f5145869bb5ad40647b768de4e9adb2a52d0dea2f',
181
+ // to: 'b2e4e7c15f733d8c18836ffd22051ed855226d9041fb9452f17f498fc2bcbce3',
182
+ // units: 6
183
+ // },
184
+ // ...
185
+ // ]
186
+ ```
package/tsconfig.json ADDED
@@ -0,0 +1,57 @@
1
+ {
2
+ "include": ["src"],
3
+ "exclude": ["build", "node_modules"],
4
+ "compilerOptions": {
5
+ "module": "esnext",
6
+ "lib": ["es6", "esnext", "es2016", "es2017", "es2022"],
7
+ "importHelpers": false,
8
+ "declaration": true,
9
+ "sourceMap": false,
10
+ "rootDir": "./src",
11
+ "allowJs": false,
12
+ "outDir": "./build/esm",
13
+ "strict": true,
14
+ "noImplicitReturns": true,
15
+ "noFallthroughCasesInSwitch": true,
16
+ "noUnusedLocals": true,
17
+ "noUnusedParameters": true,
18
+ "moduleResolution": "node",
19
+ "esModuleInterop": true,
20
+ "skipLibCheck": true,
21
+ "forceConsistentCasingInFileNames": true,
22
+ "baseUrl": "."
23
+ }
24
+ }
25
+
26
+ // {
27
+ // "compilerOptions": {
28
+ // "target": "es5",
29
+ // "module": "ESNext",
30
+ // "lib": ["es6", "esnext", "es2016", "es2017", "es2022"],
31
+ // "allowJs": true,
32
+ // "outDir": "build",
33
+ // "rootDir": "src",
34
+ // "strict": true,
35
+ // "noImplicitAny": true,
36
+ // "esModuleInterop": true,
37
+ // "resolveJsonModule": true,
38
+ // "moduleResolution": "node"
39
+ // }
40
+ // }
41
+
42
+ // {
43
+ // "compilerOptions": {
44
+ // "target": "es6",
45
+ // "module": "commonjs",
46
+ // "lib": ["es6", "esnext", "es2016", "es2017", "es2022"],
47
+ // "allowJs": true,
48
+ // "outDir": "build",
49
+ // "rootDir": "src",
50
+ // "strict": true,
51
+ // "noImplicitAny": true,
52
+ // "esModuleInterop": true,
53
+ // "resolveJsonModule": true,
54
+ // "moduleResolution": "node",
55
+ // "declaration": true
56
+ // }
57
+ // }
@@ -0,0 +1,6 @@
1
+ {
2
+ "extends": "./tsconfig.json",
3
+ "compilerOptions": {
4
+ "outDir": "./"
5
+ }
6
+ }