Skip to content

Commit

Permalink
Merge pull request #21 from cyclic-software/declarations
Browse files Browse the repository at this point in the history
Declarations of types
  • Loading branch information
korostelevm authored Dec 15, 2022
2 parents d0893d7 + 0e85c67 commit 0898022
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 25 deletions.
1 change: 1 addition & 0 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ jobs:
cache: npm
node-version: 16
- run: npm ci
- run: npm run build
- run: npm run test
- run: npx semantic-release
env:
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,6 @@ junit.xml

.DS_Store
.vscode

# Exclude generated type declarations
src/**/*.d.ts
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
examples
test
commitlint.config.js
tsconfig.json
15 changes: 8 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 7 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
"version": "0.0.27",
"description": "SDK for interacting with Cyclic.sh (https://cyclic.sh) app AWS DynamoDB databases",
"main": "src/index.js",
"types": "src/index.d.ts",
"scripts": {
"test": "jest",
"prepare": "husky install"
"clean": "find ./src -name '*.d.ts.map' -delete && find ./src -name '*.d.ts' -delete",
"build": "tsc",
"test": "jest",
"prepare": "husky install"
},
"repository": {
"type": "git",
Expand Down Expand Up @@ -40,7 +43,8 @@
"@commitlint/cli": "^17.3.0",
"@commitlint/config-conventional": "^17.3.0",
"husky": "^8.0.2",
"jest": "^27.5.1"
"jest": "^27.5.1",
"typescript": "^4.9.4"
},
"dependencies": {
"@aws-sdk/client-dynamodb": "3.82.0",
Expand Down
40 changes: 28 additions & 12 deletions src/cy_db_collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,31 @@ const { gen_expression } = require('./expressions')



class CyclicCollection{
class CyclicCollection {
/*
* Constructs a CyclicCollection that can be used to interact with collection.
* @arg {string} collection - name of the collection
* @arg {object} props - optional
*/
constructor(collection, props={}){
validate_strings(collection, 'Collection Name')

this.collection = collection
this.collection = collection
}

/*
* Constructs a new CyclicItem addressed by key inside of this collection
* @arg {string} key - the key to assign to the new item
* @returns {CyclicItem} - new item for the key in this collection
*/
item(key){
return new CyclicItem(this.collection,key)
}
}

/*
* Retrieve data from dynamodb associated to key instead of this collection.
* @arg {string} key - the key to lookup the item
* @returns {CyclicItem} - retrieves the data associated with key from dynamodb
*/
async get(key){
let item = new CyclicItem(this.collection,key)
return item.get()
Expand All @@ -27,7 +43,7 @@ class CyclicCollection{
let item = new CyclicItem(this.collection,key)
return item.set(props,opts)
}

async delete(key, props, opts){
let item = new CyclicItem(this.collection,key)
return item.delete()
Expand All @@ -40,36 +56,36 @@ class CyclicCollection{
}

let scans = Array.from({length: segments}, (_, index) => index + 1);

let filter = gen_expression(q)
filter.expression = `${filter.expression} AND cy_meta.#kc = :vcol`
filter.attr_names[`#kc`] = 'c'
filter.attr_vals[`:vcol`] = this.collection

let r = {
results: []
}

let segment_results = await Promise.all(scans.map(s=>{
return this.parallel_scan(filter, s-1, segments)

}))

segment_results.forEach(s=>{
s.results.forEach(sr=>{r.results.push(sr)})
})

return r
}



async parallel_scan(filter, segment, total_segments, limit=50000 , next = undefined){
let results = []
do{
var params = {
TableName: process.env.CYCLIC_DB,
Limit: limit,
Limit: limit,
ScanIndexForward:false,
Segment: segment,
TotalSegments:total_segments,
Expand Down Expand Up @@ -175,4 +191,4 @@ class CyclicCollection{
}


module.exports = CyclicCollection
module.exports = CyclicCollection
15 changes: 12 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,32 @@ class CyclicDb extends Function{
this._bound = this.bind(this)
return this._bound
}

_call(table_name) {
process.env.CYCLIC_DB = table_name
return new CyclicDb
}

/**
* @type {CyclicItem}
*/
item(collection,key){
return new CyclicItem(collection, key)
}


/**
* @type {CyclicCollection}
*/
collection(collection){
return new CyclicCollection(collection)
}

/**
* @type {CyclicIndex}
*/
index(name){
return new CyclicIndex(name)
}
}

module.exports = new CyclicDb
module.exports = new CyclicDb
11 changes: 11 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"include": [
"src/**/*.js"
],
"compilerOptions": {
"declaration": true,
"emitDeclarationOnly": true,
"allowJs": true,
"declarationMap": true
}
}

0 comments on commit 0898022

Please sign in to comment.