Skip to content
This repository has been archived by the owner on Apr 17, 2023. It is now read-only.

Update typings. Add typescript example. Cleanup package.json #41

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
11 changes: 11 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Tells the .editorconfg plugin to stop searching once it finds this file
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should help keep things more standardised across folks machines.

root = true

[*]
indent_size = 2
indent_style = space
charset = utf-8
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true

3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,6 @@ dump.rdb
cov-test/
cov-unit/
coverage/

examples/basic-express-typescript/*.js
examples/basic-express-typescript/*.js.map
4 changes: 3 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@ language: node_js
sudo: required
node_js:
- "0.10"
- "4"
- "4.4.3"
- "6"
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should be testing against recent versions of Node.js. I kept 4.4.3 since I believe we have some requirements around supporting that explicit version

Copy link
Member

@wtrocki wtrocki Nov 22, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good spot! Let's remove 0.10 and 4 - it's gone for long time.

- "8"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lts/* will be better here. It my suggest that we support 8 :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wtrocki not sure I follow, do you mean we can put "*" instead of "8"?

services:
- docker
before_install:
- sudo apt-get update
- sudo apt-get install --assume-yes apache2-utils
- npm install -g [email protected]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we still be using npm 2.x, even when running on node 6/8?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that's a fair question. Perhaps @wtrocki can confirm if there's a requirement from some users?

- npm install -g grunt-cli
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed this since it's now in "devDependencies"

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👎 it is for the CI process to run the script and test the project to allow the PR is required the grunt-cli

- npm config set strict-ssl false
install: npm install
env:
Expand Down
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,11 @@ Before running tests do:

```
npm install
npm install -g grunt-cli
```

Then to run the tests use ```npm test```
Then to run the tests use `npm test`

## Cordova client template
## Cordova Client Template

The [Feedhenry Cordova Sync Template](https://github.com/feedhenry-templates/feedhenry-cordova-sync-app) can be used to create client application talking to the sync server.

Expand Down
25 changes: 25 additions & 0 deletions examples/basic-express-typescript/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "basic-express-typescript",
"version": "0.0.1",
"description": "Example of using TypeScript with fh-sync",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "tsc && node server.js"
},
"author": "Evan Shortiss <[email protected]> (http://evanshortiss.com/)",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not against that, but having personalized author in organization code seems to be antipatern.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wtrocki that's a mistake. npm init filled this in with my default and I forgot to remove 😊

"license": "MIT",
"dependencies": {
"bluebird": "~3.5.1",
"body-parser": "~1.18.2",
"cors": "~2.8.4",
"express": "~4.16.2"
},
"devDependencies": {
"@types/bluebird": "~3.5.18",
"@types/body-parser": "~1.16.8",
"@types/cors": "~2.8.3",
"@types/express": "~4.0.39",
"typescript": "~2.6.1"
}
}
27 changes: 27 additions & 0 deletions examples/basic-express-typescript/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

import * as express from 'express'
import * as parsers from 'body-parser'
import * as cors from 'cors'
import * as sync from '../../fh-sync'

const router = express.Router()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No semis? Daring.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm trying it out here too 😂


// Mobile clients typically require CORS headers to be set
router.use(cors())

// Need to parse incoming JSON bodies
router.use(parsers.json())

// All sync requests are performed using a HTTP POST
router.post('/:datasetId', (req: express.Request, res: express.Response, next: express.NextFunction) => {
// Invoke action in sync for specific dataset
sync.invoke(req.params.datasetId, req.body, function (err, result) {
if (err) {
next(err)
} else {
res.json(result)
}
})
})

export default router
38 changes: 38 additions & 0 deletions examples/basic-express-typescript/server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@

import * as express from 'express'
import * as sync from './sync'
import syncRouter from './route'

const app = express()

sync.init()
.then(startApplicationServer)
.catch((e) => {
console.log('error occurred during startup', e)
process.exit(1)
})

function startApplicationServer (err: any) {
if (err) {
console.log('error starting sync server:')
throw err
}

console.log('Sync initialised')

// Sync express api required for sync clients. All sync clients will call this endpoint to sync data
app.use('/sync', syncRouter)

// Default route. Can be used to check application is up and running
app.get('/', (req, res) => {
res.send('Sample application is running!')
})

app.listen(3000, (err: any) => {
if (err) throw err

console.log('\nExample app listening on port 3000!')
console.log('\nRun the following from a terminal to get records via sync:')
console.log('curl http://localhost:3000/sync/messages -X POST --data \'{"fn": "syncRecords"}\' -H "content-type:application/json"\n')
});
}
70 changes: 70 additions & 0 deletions examples/basic-express-typescript/sync.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@

import * as sync from '../../fh-sync'
import * as Promise from 'bluebird'

// Sync framework requires mongodb and redis to be running
const MONGO_CONN_STRING = process.env.MONGO_CONNECTION_URL || 'mongodb://127.0.0.1:27017/sync';
const REDIS_CONN_STRING = process.env.REDIS_CONNECTION_URL || 'redis://127.0.0.1:6379';

// Options to pass to the mongodb driver
const MONGO_OPTS = {}

// Define our dataset name and the option such as how often to sync to system of record
const DATASET_NAME = 'messages'
const DATASET_OPTS = {
syncFrequency: 10 // seconds
};

interface Query {
username: string
}

interface Meta {
trackingId: string
}

function initialiseDataset () {
return new Promise((resolve, reject) => {
sync.init(DATASET_NAME, DATASET_OPTS, (err) => {
if (err) {
reject(err)
} else {
// Sample list handler. Uses a custom query and metadata interface to provide
// better typings in the handler logic.
sync.handleList(DATASET_NAME, (dataset, query: Query, meta: Meta, done) => {
console.log(`received request from ${query.username} with tracking ID ${meta.trackingId}`)

done(null, {
'00001': {
'item': 'item1'
},
'00002': {
'item': 'item2'
},
'00003': {
'item': 'item3'
}
})
})

resolve()
}
})
})
}

function connect () {
return new Promise((resolve, reject) => {
sync.connect(MONGO_CONN_STRING, MONGO_OPTS, REDIS_CONN_STRING, (err) => {
if (err) {
reject(err)
} else {
resolve()
}
});
})
}

export function init () {
return connect().then(initialiseDataset)
}
13 changes: 13 additions & 0 deletions examples/basic-express-typescript/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"compilerOptions": {
"module": "commonjs",
"noImplicitAny": true,
"removeComments": true,
"preserveConstEnums": true,
"sourceMap": true,
"target": "es5"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"strict": true by any chance?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will take a look at that 👍

},
"include": [
"server.ts"
]
}
Loading