|
| 1 | +/** |
| 2 | + * Module dependencies. |
| 3 | + */ |
| 4 | + |
| 5 | +import Promise from "bluebird"; |
| 6 | +import SequelizeUtils from "sequelize/lib/utils"; |
| 7 | +import moment from "moment"; |
| 8 | +import fs from "fs"; |
| 9 | +import JSONStream from "JSONStream"; |
| 10 | + |
| 11 | +/** |
| 12 | + * File system adapter. |
| 13 | + */ |
| 14 | + |
| 15 | +/** |
| 16 | + * Open a new connection. |
| 17 | + * |
| 18 | + * Params: |
| 19 | + * @connection_string String* |
| 20 | + * |
| 21 | + * Return: |
| 22 | + * @client Instance |
| 23 | + */ |
| 24 | + |
| 25 | +export function openConnection(connection_string) {} |
| 26 | + |
| 27 | +/** |
| 28 | + * Close the connection. |
| 29 | + * |
| 30 | + * Params: |
| 31 | + * @client Instance |
| 32 | + */ |
| 33 | + |
| 34 | +export function closeConnection(client) {} |
| 35 | + |
| 36 | +/** |
| 37 | + * Wrap the user query |
| 38 | + * inside a PostgreSQL request. |
| 39 | + * |
| 40 | + * Params: |
| 41 | + * @query String* |
| 42 | + * @last_updated_at String |
| 43 | + * |
| 44 | + * Return: |
| 45 | + * @wrappedQuery String |
| 46 | + */ |
| 47 | + |
| 48 | +export function wrapQuery(sql, last_updated_at) { |
| 49 | + return "data.json"; |
| 50 | +} |
| 51 | + |
| 52 | +function cancelQuery(client) {} |
| 53 | + |
| 54 | +/** |
| 55 | + * Run a wrapped query. |
| 56 | + * |
| 57 | + * Params: |
| 58 | + * @client Instance* |
| 59 | + * @wrappedQuery String* |
| 60 | + * @callback Function* |
| 61 | + * |
| 62 | + * Return: |
| 63 | + * @callback Function |
| 64 | + * - @error Object |
| 65 | + * - @rows Array |
| 66 | + */ |
| 67 | + |
| 68 | +export function runQuery(client, query, options = {}) { |
| 69 | + return new Promise((resolve, reject) => { |
| 70 | + return resolve(query); |
| 71 | + }); |
| 72 | +} |
| 73 | + |
| 74 | +/** |
| 75 | + * Stream a wrapped query. |
| 76 | + * |
| 77 | + * Params: |
| 78 | + * @client Instance* |
| 79 | + * @wrappedQuery String* |
| 80 | + * @callback Function* |
| 81 | + * |
| 82 | + * Return: |
| 83 | + * @callback Function |
| 84 | + * - @error Object |
| 85 | + * - @stream Stream |
| 86 | + */ |
| 87 | + |
| 88 | +export function streamQuery(client, query) { |
| 89 | + return new Promise((resolve, reject) => { |
| 90 | + const stream = fs.createReadStream(query).pipe(JSONStream.parse()); |
| 91 | + resolve(stream); |
| 92 | + return stream; |
| 93 | + }); |
| 94 | +} |
| 95 | + |
| 96 | +export function upload(users, shipId, partNumber) { |
| 97 | + const data = users.map(user => JSON.stringify(user)).join("\n"); |
| 98 | + return new Promise((resolve, reject) => { |
| 99 | + const filename = `extracts/${shipId}/${new Date().getTime()}-${partNumber}.json` |
| 100 | + fs.writeFile(filename, data, function(err) { |
| 101 | + if (err) { |
| 102 | + return reject(err); |
| 103 | + } else { |
| 104 | + return resolve({ url: `http://fake.url/${filename}`, partNumber, size: users.length }); |
| 105 | + } |
| 106 | + }); |
| 107 | + }); |
| 108 | +} |
0 commit comments