Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@instantdb/admin

Package Overview
Dependencies
Maintainers
3
Versions
195
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@instantdb/admin - npm Package Compare versions

Comparing version 0.6.5 to 0.7.0

219

dist/index.js

@@ -35,7 +35,8 @@ "use strict";

Object.defineProperty(exports, "__esModule", { value: true });
exports.auth = exports.tx = exports.id = exports.transact = exports.query = exports.init = void 0;
exports.transact = exports.query = exports.auth = exports.tx = exports.id = exports.init = void 0;
const core_1 = require("@instantdb/core");
Object.defineProperty(exports, "tx", { enumerable: true, get: function () { return core_1.tx; } });
const uuid = __importStar(require("uuid"));
let _GLOBAL_CONFIG = undefined;
const id = uuid.v4;
exports.id = id;
function configWithDefaults(config) {

@@ -45,9 +46,6 @@ const defaultConfig = {

};
return Object.assign(Object.assign({}, defaultConfig), config);
const r = Object.assign(Object.assign({}, defaultConfig), config);
_GLOBAL_CONFIG = r;
return r;
}
function assert(condition, msg) {
if (!condition) {
throw new Error("[assertion error] " + msg);
}
}
function authorizedHeaders(config) {

@@ -70,7 +68,26 @@ const { adminToken, appId } = config;

}
function getConfig() {
assert(_GLOBAL_CONFIG, "Uh oh! Looks like `init` hasn't run yet. Make sure `init` runs " +
"before your first `query` or `transact`.");
return _GLOBAL_CONFIG;
/**
*
* The first step: init your application!
*
* Visit https://instantdb.com/dash to get your `appId` :)
*
* @example
* const db = init({ appId: "my-app-id" })
*
* // You can also provide a a schema for type safety and editor autocomplete!
*
* type Schema = {
* goals: {
* title: string
* }
* }
*
* const db = init<Schema>({ appId: "my-app-id" })
*
*/
function init(config) {
return new InstantAdmin(config);
}
exports.init = init;
/**

@@ -83,8 +100,138 @@ *

* @example
* init({ appId: "my-app-id", adminToken: "my-admin-token" })
* const db = init({ appId: "my-app-id", adminToken: "my-admin-token" })
*/
function init(config) {
_GLOBAL_CONFIG = configWithDefaults(config);
class InstantAdmin {
constructor(_config) {
/**
* Use this to query your data!
*
* @see https://docs.instantdb.com/docs/instaql
*
* @example
* // fetch all goals
* await db.query({ goals: {} })
*
* // goals where the title is "Get Fit"
* await db.query({ goals: { $: { where: { title: "Get Fit" } } } })
*
* // all goals, _alongside_ their todos
* await db.query({ goals: { todos: {} } })
*/
this.query = (query) => {
return jsonFetch(`${this.config.apiURI}/admin/query`, {
method: "POST",
headers: authorizedHeaders(this.config),
body: JSON.stringify({ query: query }),
});
};
/**
* Use this to write data! You can create, update, delete, and link objects
*
* @see https://docs.instantdb.com/docs/instaml
*
* @example
* // Create a new object in the `goals` namespace
* const goalId = id();
* db.transact(tx.goals[goalId].update({title: "Get fit"}))
*
* // Update the title
* db.transact(tx.goals[goalId].update({title: "Get super fit"}))
*
* // Delete it
* db.transact(tx.goals[goalId].delete())
*
* // Or create an association:
* todoId = id();
* db.transact([
* tx.todos[todoId].update({ title: 'Go on a run' }),
* tx.goals[goalId].link({todos: todoId}),
* ])
*/
this.transact = (inputChunks) => {
const chunks = Array.isArray(inputChunks) ? inputChunks : [inputChunks];
const steps = chunks.flatMap((tx) => (0, core_1.getOps)(tx));
return jsonFetch(`${this.config.apiURI}/admin/transact`, {
method: "POST",
headers: authorizedHeaders(this.config),
body: JSON.stringify({ steps: steps }),
});
};
this.config = configWithDefaults(_config);
this.auth = new Auth(this.config);
}
}
exports.init = init;
class Auth {
constructor(config) {
/**
* Creates a login token for the user with the given email.
* If that user does not exist, we create one.
*
* This is often useful for writing custom auth flows.
*
* @example
* app.post('/custom_sign_in', async (req, res) => {
* // ... your custom flow for users
* const token = await db.auth.createToken(email)
* return res.status(200).send({ token })
* })
*
* @see https://docs.instantdb.com/docs/backend#custom-auth
*/
this.createToken = (email) => __awaiter(this, void 0, void 0, function* () {
const ret = yield jsonFetch(`${this.config.apiURI}/admin/refresh_tokens`, {
method: "POST",
headers: authorizedHeaders(this.config),
body: JSON.stringify({ email }),
});
return ret.user.refresh_token;
});
/**
* Verifies a given token and returns the associated user.
*
* This is often useful for writing custom endpoints, where you need
* to authenticate users.
*
* @example
* app.post('/custom_endpoint', async (req, res) => {
* const user = await db.auth.verifyToken(req.headers['token'])
* if (!user) {
* return res.status(400).send('Uh oh, you are not authenticated')
* }
* // ...
* })
* @see https://docs.instantdb.com/docs/backend#custom-endpoints
*/
this.verifyToken = (token) => __awaiter(this, void 0, void 0, function* () {
const res = yield jsonFetch(`${this.config.apiURI}/runtime/auth/verify_refresh_token`, {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({
"app-id": this.config.appId,
"refresh-token": token,
}),
});
return res.user;
});
this.config = config;
}
}
// legacy code below
function assert(condition, msg) {
if (!condition) {
throw new Error("[assertion error] " + msg);
}
}
/**
* @deprecated since 0.7.0
*/
let _GLOBAL_CONFIG = undefined;
function getConfig() {
assert(_GLOBAL_CONFIG, "Uh oh! Looks like `init` hasn't run yet. Make sure `init` runs " +
"before your first `query` or `transact`.");
return _GLOBAL_CONFIG;
}
/**
* @deprecated since 0.7.0, use db.auth instead.
* @see https://docs.instantdb.com/docs/backend
*/
const auth = {

@@ -150,15 +297,4 @@ /**

/**
* Use this to query your data!
*
* @see https://docs.instantdb.com/docs/instaql
*
* @example
* // fetch all goals
* await query({ goals: {} })
*
* // goals where the title is "Get Fit"
* await query({ goals: { $: { where: { title: "Get Fit" } } } })
*
* // all goals, _alongside_ their todos
* await query({ goals: { todos: {} } })
* @deprecated since 0.7.0, use db.query instead.
* @see https://docs.instantdb.com/docs/backend
*/

@@ -175,23 +311,4 @@ function query(query) {

/**
* Use this to write data! You can create, update, delete, and link objects
*
* @see https://docs.instantdb.com/docs/instaml
*
* @example
* // Create a new object in the `goals` namespace
* const goalId = id();
* transact(tx.goals[goalId].update({title: "Get fit"}))
*
* // Update the title
* transact(tx.goals[goalId].update({title: "Get super fit"}))
*
* // Delete it
* transact(tx.goals[goalId].delete())
*
* // Or create an association:
* todoId = id();
* transact([
* tx.todos[todoId].update({ title: 'Go on a run' }),
* tx.goals[goalId].link({todos: todoId}),
* ])
* @deprecated since 0.7.0, use db.transact instead.
* @see https://docs.instantdb.com/docs/backend
*/

@@ -209,4 +326,2 @@ function transact(inputChunks) {

exports.transact = transact;
const id = uuid.v4;
exports.id = id;
//# sourceMappingURL=index.js.map

@@ -12,3 +12,3 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {

import * as uuid from "uuid";
let _GLOBAL_CONFIG = undefined;
const id = uuid.v4;
function configWithDefaults(config) {

@@ -18,9 +18,6 @@ const defaultConfig = {

};
return Object.assign(Object.assign({}, defaultConfig), config);
const r = Object.assign(Object.assign({}, defaultConfig), config);
_GLOBAL_CONFIG = r;
return r;
}
function assert(condition, msg) {
if (!condition) {
throw new Error("[assertion error] " + msg);
}
}
function authorizedHeaders(config) {

@@ -43,6 +40,24 @@ const { adminToken, appId } = config;

}
function getConfig() {
assert(_GLOBAL_CONFIG, "Uh oh! Looks like `init` hasn't run yet. Make sure `init` runs " +
"before your first `query` or `transact`.");
return _GLOBAL_CONFIG;
/**
*
* The first step: init your application!
*
* Visit https://instantdb.com/dash to get your `appId` :)
*
* @example
* const db = init({ appId: "my-app-id" })
*
* // You can also provide a a schema for type safety and editor autocomplete!
*
* type Schema = {
* goals: {
* title: string
* }
* }
*
* const db = init<Schema>({ appId: "my-app-id" })
*
*/
function init(config) {
return new InstantAdmin(config);
}

@@ -56,7 +71,139 @@ /**

* @example
* init({ appId: "my-app-id", adminToken: "my-admin-token" })
* const db = init({ appId: "my-app-id", adminToken: "my-admin-token" })
*/
function init(config) {
_GLOBAL_CONFIG = configWithDefaults(config);
class InstantAdmin {
constructor(_config) {
/**
* Use this to query your data!
*
* @see https://docs.instantdb.com/docs/instaql
*
* @example
* // fetch all goals
* await db.query({ goals: {} })
*
* // goals where the title is "Get Fit"
* await db.query({ goals: { $: { where: { title: "Get Fit" } } } })
*
* // all goals, _alongside_ their todos
* await db.query({ goals: { todos: {} } })
*/
this.query = (query) => {
return jsonFetch(`${this.config.apiURI}/admin/query`, {
method: "POST",
headers: authorizedHeaders(this.config),
body: JSON.stringify({ query: query }),
});
};
/**
* Use this to write data! You can create, update, delete, and link objects
*
* @see https://docs.instantdb.com/docs/instaml
*
* @example
* // Create a new object in the `goals` namespace
* const goalId = id();
* db.transact(tx.goals[goalId].update({title: "Get fit"}))
*
* // Update the title
* db.transact(tx.goals[goalId].update({title: "Get super fit"}))
*
* // Delete it
* db.transact(tx.goals[goalId].delete())
*
* // Or create an association:
* todoId = id();
* db.transact([
* tx.todos[todoId].update({ title: 'Go on a run' }),
* tx.goals[goalId].link({todos: todoId}),
* ])
*/
this.transact = (inputChunks) => {
const chunks = Array.isArray(inputChunks) ? inputChunks : [inputChunks];
const steps = chunks.flatMap((tx) => getOps(tx));
return jsonFetch(`${this.config.apiURI}/admin/transact`, {
method: "POST",
headers: authorizedHeaders(this.config),
body: JSON.stringify({ steps: steps }),
});
};
this.config = configWithDefaults(_config);
this.auth = new Auth(this.config);
}
}
class Auth {
constructor(config) {
/**
* Creates a login token for the user with the given email.
* If that user does not exist, we create one.
*
* This is often useful for writing custom auth flows.
*
* @example
* app.post('/custom_sign_in', async (req, res) => {
* // ... your custom flow for users
* const token = await db.auth.createToken(email)
* return res.status(200).send({ token })
* })
*
* @see https://docs.instantdb.com/docs/backend#custom-auth
*/
this.createToken = (email) => __awaiter(this, void 0, void 0, function* () {
const ret = yield jsonFetch(`${this.config.apiURI}/admin/refresh_tokens`, {
method: "POST",
headers: authorizedHeaders(this.config),
body: JSON.stringify({ email }),
});
return ret.user.refresh_token;
});
/**
* Verifies a given token and returns the associated user.
*
* This is often useful for writing custom endpoints, where you need
* to authenticate users.
*
* @example
* app.post('/custom_endpoint', async (req, res) => {
* const user = await db.auth.verifyToken(req.headers['token'])
* if (!user) {
* return res.status(400).send('Uh oh, you are not authenticated')
* }
* // ...
* })
* @see https://docs.instantdb.com/docs/backend#custom-endpoints
*/
this.verifyToken = (token) => __awaiter(this, void 0, void 0, function* () {
const res = yield jsonFetch(`${this.config.apiURI}/runtime/auth/verify_refresh_token`, {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({
"app-id": this.config.appId,
"refresh-token": token,
}),
});
return res.user;
});
this.config = config;
}
}
export { init, id, tx, };
// legacy code below
function assert(condition, msg) {
if (!condition) {
throw new Error("[assertion error] " + msg);
}
}
/**
* @deprecated since 0.7.0
*/
let _GLOBAL_CONFIG = undefined;
function getConfig() {
assert(_GLOBAL_CONFIG, "Uh oh! Looks like `init` hasn't run yet. Make sure `init` runs " +
"before your first `query` or `transact`.");
return _GLOBAL_CONFIG;
}
/**
* @deprecated since 0.7.0, use db.auth instead.
* @see https://docs.instantdb.com/docs/backend
*/
const auth = {

@@ -121,15 +268,4 @@ /**

/**
* Use this to query your data!
*
* @see https://docs.instantdb.com/docs/instaql
*
* @example
* // fetch all goals
* await query({ goals: {} })
*
* // goals where the title is "Get Fit"
* await query({ goals: { $: { where: { title: "Get Fit" } } } })
*
* // all goals, _alongside_ their todos
* await query({ goals: { todos: {} } })
* @deprecated since 0.7.0, use db.query instead.
* @see https://docs.instantdb.com/docs/backend
*/

@@ -145,23 +281,4 @@ function query(query) {

/**
* Use this to write data! You can create, update, delete, and link objects
*
* @see https://docs.instantdb.com/docs/instaml
*
* @example
* // Create a new object in the `goals` namespace
* const goalId = id();
* transact(tx.goals[goalId].update({title: "Get fit"}))
*
* // Update the title
* transact(tx.goals[goalId].update({title: "Get super fit"}))
*
* // Delete it
* transact(tx.goals[goalId].delete())
*
* // Or create an association:
* todoId = id();
* transact([
* tx.todos[todoId].update({ title: 'Go on a run' }),
* tx.goals[goalId].link({todos: todoId}),
* ])
* @deprecated since 0.7.0, use db.transact instead.
* @see https://docs.instantdb.com/docs/backend
*/

@@ -178,4 +295,3 @@ function transact(inputChunks) {

}
const id = uuid.v4;
export { init, query, transact, id, tx, auth };
export { auth, query, transact };
//# sourceMappingURL=index.js.map
{
"name": "@instantdb/admin",
"version": "0.6.5",
"version": "0.7.0",
"description": "Admin SDK for Instant DB",

@@ -28,5 +28,5 @@ "main": "dist/index.js",

"dependencies": {
"@instantdb/core": "0.6.5",
"@instantdb/core": "0.7.0",
"uuid": "^9.0.1"
}
}

@@ -19,3 +19,3 @@ import { tx, TransactionChunk, getOps } from "@instantdb/core";

let _GLOBAL_CONFIG: FilledConfig | undefined = undefined;
const id = uuid.v4;

@@ -26,11 +26,7 @@ function configWithDefaults(config: Config): FilledConfig {

};
return { ...defaultConfig, ...config };
const r = { ...defaultConfig, ...config };
_GLOBAL_CONFIG = r;
return r;
}
function assert(condition: any, msg?: string): asserts condition {
if (!condition) {
throw new Error("[assertion error] " + msg);
}
}
function authorizedHeaders(config: FilledConfig) {

@@ -56,9 +52,24 @@ const { adminToken, appId } = config;

function getConfig(): FilledConfig {
assert(
_GLOBAL_CONFIG,
"Uh oh! Looks like `init` hasn't run yet. Make sure `init` runs " +
"before your first `query` or `transact`.",
);
return _GLOBAL_CONFIG;
/**
*
* The first step: init your application!
*
* Visit https://instantdb.com/dash to get your `appId` :)
*
* @example
* const db = init({ appId: "my-app-id" })
*
* // You can also provide a a schema for type safety and editor autocomplete!
*
* type Schema = {
* goals: {
* title: string
* }
* }
*
* const db = init<Schema>({ appId: "my-app-id" })
*
*/
function init<Schema = {}>(config: Config) {
return new InstantAdmin<Schema>(config);
}

@@ -73,8 +84,173 @@

* @example
* init({ appId: "my-app-id", adminToken: "my-admin-token" })
* const db = init({ appId: "my-app-id", adminToken: "my-admin-token" })
*/
function init(config: Config) {
_GLOBAL_CONFIG = configWithDefaults(config);
class InstantAdmin<Schema = {}> {
config: FilledConfig;
auth: Auth;
constructor(_config: Config) {
this.config = configWithDefaults(_config);
this.auth = new Auth(this.config);
}
/**
* Use this to query your data!
*
* @see https://docs.instantdb.com/docs/instaql
*
* @example
* // fetch all goals
* await db.query({ goals: {} })
*
* // goals where the title is "Get Fit"
* await db.query({ goals: { $: { where: { title: "Get Fit" } } } })
*
* // all goals, _alongside_ their todos
* await db.query({ goals: { todos: {} } })
*/
query = <Q extends Query>(
query: Exactly<Query, Q>,
): Promise<QueryResponse<Q, Schema>> => {
return jsonFetch(`${this.config.apiURI}/admin/query`, {
method: "POST",
headers: authorizedHeaders(this.config),
body: JSON.stringify({ query: query }),
});
};
/**
* Use this to write data! You can create, update, delete, and link objects
*
* @see https://docs.instantdb.com/docs/instaml
*
* @example
* // Create a new object in the `goals` namespace
* const goalId = id();
* db.transact(tx.goals[goalId].update({title: "Get fit"}))
*
* // Update the title
* db.transact(tx.goals[goalId].update({title: "Get super fit"}))
*
* // Delete it
* db.transact(tx.goals[goalId].delete())
*
* // Or create an association:
* todoId = id();
* db.transact([
* tx.todos[todoId].update({ title: 'Go on a run' }),
* tx.goals[goalId].link({todos: todoId}),
* ])
*/
transact = (inputChunks: TransactionChunk | TransactionChunk[]) => {
const chunks = Array.isArray(inputChunks) ? inputChunks : [inputChunks];
const steps = chunks.flatMap((tx) => getOps(tx));
return jsonFetch(`${this.config.apiURI}/admin/transact`, {
method: "POST",
headers: authorizedHeaders(this.config),
body: JSON.stringify({ steps: steps }),
});
};
}
class Auth {
config: FilledConfig;
constructor(config: FilledConfig) {
this.config = config;
}
/**
* Creates a login token for the user with the given email.
* If that user does not exist, we create one.
*
* This is often useful for writing custom auth flows.
*
* @example
* app.post('/custom_sign_in', async (req, res) => {
* // ... your custom flow for users
* const token = await db.auth.createToken(email)
* return res.status(200).send({ token })
* })
*
* @see https://docs.instantdb.com/docs/backend#custom-auth
*/
createToken = async (email: string): Promise<AuthToken> => {
const ret: { user: { refresh_token: string } } = await jsonFetch(
`${this.config.apiURI}/admin/refresh_tokens`,
{
method: "POST",
headers: authorizedHeaders(this.config),
body: JSON.stringify({ email }),
},
);
return ret.user.refresh_token;
};
/**
* Verifies a given token and returns the associated user.
*
* This is often useful for writing custom endpoints, where you need
* to authenticate users.
*
* @example
* app.post('/custom_endpoint', async (req, res) => {
* const user = await db.auth.verifyToken(req.headers['token'])
* if (!user) {
* return res.status(400).send('Uh oh, you are not authenticated')
* }
* // ...
* })
* @see https://docs.instantdb.com/docs/backend#custom-endpoints
*/
verifyToken = async (token: AuthToken): Promise<User> => {
const res = await jsonFetch(
`${this.config.apiURI}/runtime/auth/verify_refresh_token`,
{
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({
"app-id": this.config.appId,
"refresh-token": token,
}),
},
);
return res.user;
};
}
export {
init,
id,
tx,
// types
Config,
};
// legacy code below
function assert(condition: any, msg?: string): asserts condition {
if (!condition) {
throw new Error("[assertion error] " + msg);
}
}
/**
* @deprecated since 0.7.0
*/
let _GLOBAL_CONFIG: FilledConfig | undefined = undefined;
function getConfig(): FilledConfig {
assert(
_GLOBAL_CONFIG,
"Uh oh! Looks like `init` hasn't run yet. Make sure `init` runs " +
"before your first `query` or `transact`.",
);
return _GLOBAL_CONFIG;
}
/**
* @deprecated since 0.7.0, use db.auth instead.
* @see https://docs.instantdb.com/docs/backend
*/
const auth = {

@@ -142,19 +318,8 @@ /**

/**
* Use this to query your data!
*
* @see https://docs.instantdb.com/docs/instaql
*
* @example
* // fetch all goals
* await query({ goals: {} })
*
* // goals where the title is "Get Fit"
* await query({ goals: { $: { where: { title: "Get Fit" } } } })
*
* // all goals, _alongside_ their todos
* await query({ goals: { todos: {} } })
* @deprecated since 0.7.0, use db.query instead.
* @see https://docs.instantdb.com/docs/backend
*/
function query<Q extends Query>(
query: Exactly<Query, Q>,
): Promise<QueryResponse<Q>> {
): Promise<QueryResponse<Q, {}>> {
const config = getConfig();

@@ -169,23 +334,4 @@ return jsonFetch(`${config.apiURI}/admin/query`, {

/**
* Use this to write data! You can create, update, delete, and link objects
*
* @see https://docs.instantdb.com/docs/instaml
*
* @example
* // Create a new object in the `goals` namespace
* const goalId = id();
* transact(tx.goals[goalId].update({title: "Get fit"}))
*
* // Update the title
* transact(tx.goals[goalId].update({title: "Get super fit"}))
*
* // Delete it
* transact(tx.goals[goalId].delete())
*
* // Or create an association:
* todoId = id();
* transact([
* tx.todos[todoId].update({ title: 'Go on a run' }),
* tx.goals[goalId].link({todos: todoId}),
* ])
* @deprecated since 0.7.0, use db.transact instead.
* @see https://docs.instantdb.com/docs/backend
*/

@@ -203,4 +349,2 @@ function transact(inputChunks: TransactionChunk | TransactionChunk[]) {

const id = uuid.v4;
export { Config, init, query, transact, id, tx, auth };
export { auth, query, transact };

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc