Skip to content

Commit 0898022

Browse files
authored
Merge pull request #21 from cyclic-software/declarations
Declarations of types
2 parents d0893d7 + 0e85c67 commit 0898022

File tree

8 files changed

+71
-25
lines changed

8 files changed

+71
-25
lines changed

.github/workflows/release.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ jobs:
2020
cache: npm
2121
node-version: 16
2222
- run: npm ci
23+
- run: npm run build
2324
- run: npm run test
2425
- run: npx semantic-release
2526
env:

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,3 +138,6 @@ junit.xml
138138

139139
.DS_Store
140140
.vscode
141+
142+
# Exclude generated type declarations
143+
src/**/*.d.ts

.npmignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
examples
33
test
44
commitlint.config.js
5+
tsconfig.json

package-lock.json

Lines changed: 8 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@
44
"version": "0.0.27",
55
"description": "SDK for interacting with Cyclic.sh (https://cyclic.sh) app AWS DynamoDB databases",
66
"main": "src/index.js",
7+
"types": "src/index.d.ts",
78
"scripts": {
8-
"test": "jest",
9-
"prepare": "husky install"
9+
"clean": "find ./src -name '*.d.ts.map' -delete && find ./src -name '*.d.ts' -delete",
10+
"build": "tsc",
11+
"test": "jest",
12+
"prepare": "husky install"
1013
},
1114
"repository": {
1215
"type": "git",
@@ -40,7 +43,8 @@
4043
"@commitlint/cli": "^17.3.0",
4144
"@commitlint/config-conventional": "^17.3.0",
4245
"husky": "^8.0.2",
43-
"jest": "^27.5.1"
46+
"jest": "^27.5.1",
47+
"typescript": "^4.9.4"
4448
},
4549
"dependencies": {
4650
"@aws-sdk/client-dynamodb": "3.82.0",

src/cy_db_collection.js

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,31 @@ const { gen_expression } = require('./expressions')
1010

1111

1212

13-
class CyclicCollection{
13+
class CyclicCollection {
14+
/*
15+
* Constructs a CyclicCollection that can be used to interact with collection.
16+
* @arg {string} collection - name of the collection
17+
* @arg {object} props - optional
18+
*/
1419
constructor(collection, props={}){
1520
validate_strings(collection, 'Collection Name')
16-
17-
this.collection = collection
21+
this.collection = collection
1822
}
23+
24+
/*
25+
* Constructs a new CyclicItem addressed by key inside of this collection
26+
* @arg {string} key - the key to assign to the new item
27+
* @returns {CyclicItem} - new item for the key in this collection
28+
*/
1929
item(key){
2030
return new CyclicItem(this.collection,key)
21-
}
31+
}
32+
33+
/*
34+
* Retrieve data from dynamodb associated to key instead of this collection.
35+
* @arg {string} key - the key to lookup the item
36+
* @returns {CyclicItem} - retrieves the data associated with key from dynamodb
37+
*/
2238
async get(key){
2339
let item = new CyclicItem(this.collection,key)
2440
return item.get()
@@ -27,7 +43,7 @@ class CyclicCollection{
2743
let item = new CyclicItem(this.collection,key)
2844
return item.set(props,opts)
2945
}
30-
46+
3147
async delete(key, props, opts){
3248
let item = new CyclicItem(this.collection,key)
3349
return item.delete()
@@ -40,36 +56,36 @@ class CyclicCollection{
4056
}
4157

4258
let scans = Array.from({length: segments}, (_, index) => index + 1);
43-
59+
4460
let filter = gen_expression(q)
4561
filter.expression = `${filter.expression} AND cy_meta.#kc = :vcol`
4662
filter.attr_names[`#kc`] = 'c'
4763
filter.attr_vals[`:vcol`] = this.collection
48-
64+
4965
let r = {
5066
results: []
5167
}
5268

5369
let segment_results = await Promise.all(scans.map(s=>{
5470
return this.parallel_scan(filter, s-1, segments)
55-
71+
5672
}))
5773

5874
segment_results.forEach(s=>{
5975
s.results.forEach(sr=>{r.results.push(sr)})
6076
})
61-
77+
6278
return r
6379
}
6480

65-
81+
6682

6783
async parallel_scan(filter, segment, total_segments, limit=50000 , next = undefined){
6884
let results = []
6985
do{
7086
var params = {
7187
TableName: process.env.CYCLIC_DB,
72-
Limit: limit,
88+
Limit: limit,
7389
ScanIndexForward:false,
7490
Segment: segment,
7591
TotalSegments:total_segments,
@@ -175,4 +191,4 @@ class CyclicCollection{
175191
}
176192

177193

178-
module.exports = CyclicCollection
194+
module.exports = CyclicCollection

src/index.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,32 @@ class CyclicDb extends Function{
99
this._bound = this.bind(this)
1010
return this._bound
1111
}
12-
12+
1313
_call(table_name) {
1414
process.env.CYCLIC_DB = table_name
1515
return new CyclicDb
1616
}
1717

18+
/**
19+
* @type {CyclicItem}
20+
*/
1821
item(collection,key){
1922
return new CyclicItem(collection, key)
2023
}
21-
24+
25+
/**
26+
* @type {CyclicCollection}
27+
*/
2228
collection(collection){
2329
return new CyclicCollection(collection)
2430
}
2531

32+
/**
33+
* @type {CyclicIndex}
34+
*/
2635
index(name){
2736
return new CyclicIndex(name)
2837
}
2938
}
3039

31-
module.exports = new CyclicDb
40+
module.exports = new CyclicDb

tsconfig.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"include": [
3+
"src/**/*.js"
4+
],
5+
"compilerOptions": {
6+
"declaration": true,
7+
"emitDeclarationOnly": true,
8+
"allowJs": true,
9+
"declarationMap": true
10+
}
11+
}

0 commit comments

Comments
 (0)