@wirechunk/cli 0.0.1-rc.2 → 0.0.1-rc.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/main.js +14 -13
- package/package.json +1 -1
- package/src/commands/create-user.ts +12 -12
- package/src/commands/ext-dev/init-db.ts +3 -0
- package/src/main.ts +3 -1
package/build/main.js
CHANGED
|
@@ -3892,7 +3892,7 @@ function customAlphabet(alphabet2, size = 21) {
|
|
|
3892
3892
|
return customRandom(alphabet2, size, random);
|
|
3893
3893
|
}
|
|
3894
3894
|
const alphabet = "123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ";
|
|
3895
|
-
customAlphabet(alphabet, 22);
|
|
3895
|
+
const cleanSmallId = customAlphabet(alphabet, 22);
|
|
3896
3896
|
const tinyAlphabet = "23456789abcdefghijkmnopqrstuvwxyz";
|
|
3897
3897
|
const startWithLowercaseLetterPattern = /^[a-z]/;
|
|
3898
3898
|
const startsWithLowercaseLetter = (s) => startWithLowercaseLetterPattern.test(s);
|
|
@@ -57347,9 +57347,6 @@ const detailedUniqueIntegrityConstraintViolationError = (error2) => {
|
|
|
57347
57347
|
return message;
|
|
57348
57348
|
};
|
|
57349
57349
|
const isDuplicateDatabaseError = (error2) => !!error2 && typeof error2 === "object" && "code" in error2 && error2.code === "42P04";
|
|
57350
|
-
const insertOrgResult = z.object({
|
|
57351
|
-
id: z.string()
|
|
57352
|
-
});
|
|
57353
57350
|
const insertUserResult = z.object({
|
|
57354
57351
|
id: z.string()
|
|
57355
57352
|
});
|
|
@@ -57388,7 +57385,6 @@ const createUser = async (opts, env2) => {
|
|
|
57388
57385
|
}
|
|
57389
57386
|
let platformId = opts.platformId;
|
|
57390
57387
|
let orgId = opts.orgId;
|
|
57391
|
-
let orgPrimary = false;
|
|
57392
57388
|
try {
|
|
57393
57389
|
const user = await db.transaction(async (db2) => {
|
|
57394
57390
|
if (!platformId) {
|
|
@@ -57396,14 +57392,14 @@ const createUser = async (opts, env2) => {
|
|
|
57396
57392
|
throw new Error("Either --org-id or --platform-id must be specified");
|
|
57397
57393
|
}
|
|
57398
57394
|
platformId = await db2.maybeOneFirst(
|
|
57399
|
-
distExports$1.sql.type(findOrgResult)`select "platformId" from "
|
|
57395
|
+
distExports$1.sql.type(findOrgResult)`select "platformId" from "Orgs" where "id" = ${orgId}`
|
|
57400
57396
|
);
|
|
57401
57397
|
if (!platformId) {
|
|
57402
57398
|
throw new Error(`No org found with ID ${orgId}`);
|
|
57403
57399
|
}
|
|
57404
57400
|
} else if (orgId) {
|
|
57405
57401
|
const orgPlatformId = await db2.maybeOneFirst(
|
|
57406
|
-
distExports$1.sql.type(findOrgResult)`select "platformId" from "
|
|
57402
|
+
distExports$1.sql.type(findOrgResult)`select "platformId" from "Orgs" where "id" = ${orgId}`
|
|
57407
57403
|
);
|
|
57408
57404
|
if (!orgPlatformId) {
|
|
57409
57405
|
throw new Error(`No org found with ID ${orgId}`);
|
|
@@ -57424,20 +57420,20 @@ const createUser = async (opts, env2) => {
|
|
|
57424
57420
|
console.log(`Found platform ${platform.name} (ID ${platform.id})`);
|
|
57425
57421
|
}
|
|
57426
57422
|
if (!orgId) {
|
|
57427
|
-
orgId =
|
|
57423
|
+
orgId = cleanSmallId();
|
|
57424
|
+
await db2.maybeOne(
|
|
57428
57425
|
distExports$1.sql.type(
|
|
57429
|
-
|
|
57430
|
-
)`insert into "
|
|
57426
|
+
voidSelectSchema
|
|
57427
|
+
)`insert into "Orgs" ("id", "platformId") values (${orgId}, ${platform.id})`
|
|
57431
57428
|
);
|
|
57432
57429
|
if (opts.verbose) {
|
|
57433
57430
|
console.log(`Created org ID ${orgId}`);
|
|
57434
57431
|
}
|
|
57435
|
-
orgPrimary = true;
|
|
57436
57432
|
}
|
|
57437
57433
|
const user2 = await db2.one(
|
|
57438
57434
|
distExports$1.sql.type(
|
|
57439
57435
|
insertUserResult
|
|
57440
|
-
)`insert into "Users" ("platformId", "email", "emailVerified", "password", "passwordStatus", "orgId", "
|
|
57436
|
+
)`insert into "Users" ("platformId", "email", "emailVerified", "password", "passwordStatus", "orgId", "role", "status", "firstName", "lastName") values (${platformId}, ${email}, ${opts.emailVerified}, ${password}, 'Ok', ${orgId}, ${role}, ${status}, ${firstName}, ${lastName}) returning "id"`
|
|
57441
57437
|
);
|
|
57442
57438
|
return user2;
|
|
57443
57439
|
});
|
|
@@ -57654,6 +57650,9 @@ const initDb = async (opts, env2) => {
|
|
|
57654
57650
|
end if;
|
|
57655
57651
|
end; $$
|
|
57656
57652
|
`);
|
|
57653
|
+
await db.query(distExports$1.sql.unsafe`
|
|
57654
|
+
grant ${extRoleIdent} to ${distExports$1.sql.identifier([coreDbUrlObject.username])}
|
|
57655
|
+
`);
|
|
57657
57656
|
await db.query(distExports$1.sql.unsafe`
|
|
57658
57657
|
grant connect, create, temporary on database ${distExports$1.sql.identifier([extDb.dbName])} to ${extRoleIdent}
|
|
57659
57658
|
`);
|
|
@@ -57730,7 +57729,9 @@ extDev.command("db-connect-info").description(
|
|
|
57730
57729
|
"--extension-id <string>",
|
|
57731
57730
|
"the ID of the extension, can be set with an EXTENSION_ID environment variable instead"
|
|
57732
57731
|
).action(withOptionsAndEnv(dbConnectInfo));
|
|
57733
|
-
extDev.command("init-db").description(
|
|
57732
|
+
extDev.command("init-db").description(
|
|
57733
|
+
"initialize a development database for an extension, useful for testing, assumes the extension role exists"
|
|
57734
|
+
).option(
|
|
57734
57735
|
"--extension-id <string>",
|
|
57735
57736
|
"the ID of the extension, can be set with an EXTENSION_ID environment variable instead"
|
|
57736
57737
|
).option("--db-name <string>", "a custom name for the database, applicable only for testing").action(withOptionsAndEnv(initDb));
|
package/package.json
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { hashPassword, validatePasswordComplexity } from '@wirechunk/backend-lib/passwords.ts';
|
|
2
|
+
import { cleanSmallId } from '@wirechunk/lib/clean-small-id.ts';
|
|
2
3
|
import { normalizeEmailAddress } from '@wirechunk/lib/emails.ts';
|
|
3
4
|
import { createPool, sql, UniqueIntegrityConstraintViolationError } from 'slonik';
|
|
4
5
|
import { z } from 'zod';
|
|
@@ -6,10 +7,7 @@ import type { Env } from '../env.ts';
|
|
|
6
7
|
import { requireCoreDbUrl } from '../env.ts';
|
|
7
8
|
import { detailedUniqueIntegrityConstraintViolationError } from '../errors.ts';
|
|
8
9
|
import type { WithGlobalOptions } from '../global-options.ts';
|
|
9
|
-
|
|
10
|
-
const insertOrgResult = z.object({
|
|
11
|
-
id: z.string(),
|
|
12
|
-
});
|
|
10
|
+
import { voidSelectSchema } from '../util.ts';
|
|
13
11
|
|
|
14
12
|
const insertUserResult = z.object({
|
|
15
13
|
id: z.string(),
|
|
@@ -69,7 +67,8 @@ export const createUser = async (
|
|
|
69
67
|
|
|
70
68
|
let platformId: string | null | undefined = opts.platformId;
|
|
71
69
|
let orgId: string | null | undefined = opts.orgId;
|
|
72
|
-
|
|
70
|
+
// TODO
|
|
71
|
+
// let orgPrimary = false;
|
|
73
72
|
|
|
74
73
|
try {
|
|
75
74
|
const user = await db.transaction(async (db) => {
|
|
@@ -78,7 +77,7 @@ export const createUser = async (
|
|
|
78
77
|
throw new Error('Either --org-id or --platform-id must be specified');
|
|
79
78
|
}
|
|
80
79
|
platformId = await db.maybeOneFirst(
|
|
81
|
-
sql.type(findOrgResult)`select "platformId" from "
|
|
80
|
+
sql.type(findOrgResult)`select "platformId" from "Orgs" where "id" = ${orgId}`,
|
|
82
81
|
);
|
|
83
82
|
if (!platformId) {
|
|
84
83
|
throw new Error(`No org found with ID ${orgId}`);
|
|
@@ -86,7 +85,7 @@ export const createUser = async (
|
|
|
86
85
|
} else if (orgId) {
|
|
87
86
|
// Verify that the specified org belongs to the specified platform.
|
|
88
87
|
const orgPlatformId = await db.maybeOneFirst(
|
|
89
|
-
sql.type(findOrgResult)`select "platformId" from "
|
|
88
|
+
sql.type(findOrgResult)`select "platformId" from "Orgs" where "id" = ${orgId}`,
|
|
90
89
|
);
|
|
91
90
|
if (!orgPlatformId) {
|
|
92
91
|
throw new Error(`No org found with ID ${orgId}`);
|
|
@@ -107,21 +106,22 @@ export const createUser = async (
|
|
|
107
106
|
console.log(`Found platform ${platform.name} (ID ${platform.id})`);
|
|
108
107
|
}
|
|
109
108
|
if (!orgId) {
|
|
110
|
-
orgId =
|
|
109
|
+
orgId = cleanSmallId();
|
|
110
|
+
await db.maybeOne(
|
|
111
111
|
sql.type(
|
|
112
|
-
|
|
113
|
-
)`insert into "
|
|
112
|
+
voidSelectSchema,
|
|
113
|
+
)`insert into "Orgs" ("id", "platformId") values (${orgId}, ${platform.id})`,
|
|
114
114
|
);
|
|
115
115
|
if (opts.verbose) {
|
|
116
116
|
console.log(`Created org ID ${orgId}`);
|
|
117
117
|
}
|
|
118
|
-
orgPrimary = true;
|
|
118
|
+
// orgPrimary = true;
|
|
119
119
|
}
|
|
120
120
|
|
|
121
121
|
const user = await db.one(
|
|
122
122
|
sql.type(
|
|
123
123
|
insertUserResult,
|
|
124
|
-
)`insert into "Users" ("platformId", "email", "emailVerified", "password", "passwordStatus", "orgId", "
|
|
124
|
+
)`insert into "Users" ("platformId", "email", "emailVerified", "password", "passwordStatus", "orgId", "role", "status", "firstName", "lastName") values (${platformId}, ${email}, ${opts.emailVerified}, ${password}, 'Ok', ${orgId}, ${role}, ${status}, ${firstName}, ${lastName}) returning "id"`,
|
|
125
125
|
);
|
|
126
126
|
|
|
127
127
|
// if (opts.admin) {
|
|
@@ -190,6 +190,9 @@ export const initDb = async (
|
|
|
190
190
|
end if;
|
|
191
191
|
end; $$
|
|
192
192
|
`);
|
|
193
|
+
await db.query(sql.unsafe`
|
|
194
|
+
grant ${extRoleIdent} to ${sql.identifier([coreDbUrlObject.username])}
|
|
195
|
+
`);
|
|
193
196
|
|
|
194
197
|
await db.query(sql.unsafe`
|
|
195
198
|
grant connect, create, temporary on database ${sql.identifier([extDb.dbName])} to ${extRoleIdent}
|
package/src/main.ts
CHANGED
|
@@ -106,7 +106,9 @@ extDev
|
|
|
106
106
|
|
|
107
107
|
extDev
|
|
108
108
|
.command('init-db')
|
|
109
|
-
.description(
|
|
109
|
+
.description(
|
|
110
|
+
'initialize a development database for an extension, useful for testing, assumes the extension role exists',
|
|
111
|
+
)
|
|
110
112
|
.option(
|
|
111
113
|
'--extension-id <string>',
|
|
112
114
|
'the ID of the extension, can be set with an EXTENSION_ID environment variable instead',
|