Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add stored query update (PUT) method #189

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 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
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,24 @@ Expects the following parameters:

Returns [`Promise<HTTP.Body>`](#body)

#### <a name="version">`server.version(version, conn)`</a>

Returns a promise that resolves to `version`, or, the version of the server at
`conn`. Returns `null` if neither parameter is provided.

Used by other methods in this library to handle Stardog API breaking changes
while avoiding an extra request when the caller knows the server's current
version.

Expects the following paramters:

- version (`string`)

- conn ([`Connection`](#connection))

Returns [`Promise<string>`]


## <a name="db">db</a>

#### <a name="create">`db.create(conn, database, databaseOptions, options, params)`</a>
Expand Down
437 changes: 228 additions & 209 deletions lib/index.d.ts

Large diffs are not rendered by default.

35 changes: 34 additions & 1 deletion lib/query/stored.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const { fetch } = require('../fetch');
const lodashPick = require('lodash/pick');

const server = require('../server');
const semver = require('semver');
const { httpBody } = require('../response-transforms');

/*
Expand Down Expand Up @@ -45,8 +46,40 @@ const deleteStoredQuery = (conn, storedQuery, params) => {
}).then(httpBody);
};

const updateStoredQuery = (conn, storedQuery, params, stardogVersion) => {
const headers = conn.headers();
headers.set('Content-Type', 'application/json');
headers.set('Accept', 'application/json');

const body = lodashPick(storedQuery, ['name', 'database', 'query', 'shared']);
body.creator = conn.username;
body.shared = typeof body.shared === 'boolean' ? body.shared : false;

return server.version(stardogVersion, conn).then(version => {
if (!version) {
return null;
} else if (semver.gte(version, '6.2.0')) {
return fetch(conn.request('admin', 'queries', 'stored'), {
headers,
method: 'PUT',
body: JSON.stringify(body),
}).then(httpBody);
}
return deleteStoredQuery(conn, storedQuery.name)
.then(() => create(conn, storedQuery, params))
.catch(deleteResponse => {
// Update creates when the query did not already exist
if (deleteResponse.status === 404) {
return create(conn, storedQuery, params);
}
return deleteResponse;
});
});
};

jmrog marked this conversation as resolved.
Show resolved Hide resolved
module.exports = {
create,
list,
update: updateStoredQuery,
remove: deleteStoredQuery,
};
15 changes: 15 additions & 0 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,22 @@ const status = (conn, params = {}) => {
).then(httpBody);
};

const versionPromise = (version, conn) => {
if (version) {
return Promise.resolve(version);
} else if (conn) {
return status(conn, { databases: false }).then(
statusResponse =>
statusResponse.status === 200
? statusResponse.body['dbms.version'].value
: null
);
}
return null;
};

module.exports = {
shutdown,
status,
version: versionPromise,
};
Loading