Skip to content
This repository was 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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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: 1 addition & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
language: node_js
sudo: required
node_js:
- 6
- 8
- 10
services:
- docker
before_install:
- sudo apt-get update
- sudo apt-get install --assume-yes apache2-utils
- 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 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 config set strict-ssl false
install: npm install
env:
Expand All @@ -33,4 +32,3 @@ matrix:
- npm install
- npm link fh-mbaas-api
- npm test

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
22 changes: 22 additions & 0 deletions examples/basic-express-typescript/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
## Sync typescript sample application example

## Requirements


`MONGO_CONNECTION_URL` environment variable needs to point to mongodb instance

By default using: mongodb://127.0.0.1:27017/sync


`REDIS_CONNECTION_URL` environment variable needs to point to running redis instance

By default using: redis://127.0.0.1:6379

## Running

npm install
npm run start

## Testing

Please refer to documentation for information how to setup sync client.
27 changes: 27 additions & 0 deletions examples/basic-express-typescript/lib/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()

// 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
70 changes: 70 additions & 0 deletions examples/basic-express-typescript/lib/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)
}
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": "ts-node server.ts"
},
"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",
"ts-node": "^3.3.0"
Copy link

@MikeyBurkman MikeyBurkman Dec 8, 2017

Choose a reason for hiding this comment

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

@wtrocki You don't want to make this change to ts-node. Or at least you definitely don't want to remove typescript as a dependency. Right now, ts-node will use whatever version of typescript is installed globally, and there's no guarantee that it's a compatible version, if it's installed at all. (Also, I don't think it's common to use ts-node on prod apps, so this wouldn't be ideal as a starting point.)

As an update, I found this regarding ts-node in production. Using ts-node in prod isn't too bad, but it's a bit slower to restart the app.

Copy link
Member

Choose a reason for hiding this comment

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

Good point. Reason why this worked for me is that I had typescript installed from previous npm install. I forgot that ts-node requires typescript to be available globally.
I made that change as original change failed to start: tsc compiler is not available.
Before making any change - @evanshortiss What aproach do you prefer?

Choose a reason for hiding this comment

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

I don't have typescript installed locally so npm start failed for me. I installed it locally via package.json (like what Evan had) and npm start worked fine, so pretty sure that's all that's necessary.

What version do you have installed globally? I think the missing types issue that my other comment was about might have to do with that, as I was able to revert your changes and it compiled fine. (And intellisense was correct.)

}
}
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 sync from './lib/sync'
import syncRouter from './lib/route'
import * as express from 'express'

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: express.Request, res: express.Response) => {
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')
});
}
14 changes: 14 additions & 0 deletions examples/basic-express-typescript/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"compilerOptions": {
"module": "commonjs",
"noImplicitAny": true,
"removeComments": true,
"preserveConstEnums": true,
"sourceMap": true,
"target": "es5",
"strict": true
},
"include": [
"server.ts"
]
}
Loading