@drax/identity-back 0.11.4 → 0.12.1
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/dist/controllers/RoleController.js +8 -39
- package/dist/controllers/TenantController.js +1 -28
- package/dist/controllers/UserApiKeyController.js +6 -3
- package/dist/controllers/UserController.js +48 -209
- package/dist/errors/BadCredentialsError.js +12 -0
- package/dist/factory/RoleServiceFactory.js +1 -0
- package/dist/factory/TenantServiceFactory.js +1 -0
- package/dist/factory/UserApiKeyServiceFactory.js +5 -4
- package/dist/factory/UserServiceFactory.js +1 -0
- package/dist/graphql/resolvers/role.resolvers.js +2 -2
- package/dist/graphql/resolvers/tenant.resolvers.js +2 -2
- package/dist/graphql/resolvers/user-api-key.resolvers.js +2 -2
- package/dist/graphql/resolvers/user.resolvers.js +1 -1
- package/dist/graphql/types/userApiKey.graphql +1 -0
- package/dist/index.js +6 -0
- package/dist/middleware/apiKeyMiddleware.js +2 -2
- package/dist/models/RoleModel.js +10 -7
- package/dist/models/TenantModel.js +11 -8
- package/dist/models/UserApiKeyModel.js +15 -7
- package/dist/models/UserGroupModel.js +7 -7
- package/dist/models/UserModel.js +10 -8
- package/dist/permissions/UserApiKeyPermissions.js +2 -1
- package/dist/rbac/Rbac.js +10 -8
- package/dist/repository/mongo/RoleMongoRepository.js +20 -65
- package/dist/repository/mongo/TenantMongoRepository.js +18 -66
- package/dist/repository/mongo/UserApiKeyMongoRepository.js +29 -44
- package/dist/repository/mongo/UserMongoRepository.js +56 -85
- package/dist/repository/sqlite/RoleSqliteRepository.js +30 -115
- package/dist/repository/sqlite/TenantSqliteRepository.js +15 -105
- package/dist/repository/sqlite/UserApiKeySqliteRepository.js +42 -115
- package/dist/repository/sqlite/UserSqliteRepository.js +49 -130
- package/dist/routes/RoleRoutes.js +35 -10
- package/dist/routes/TenantRoutes.js +18 -9
- package/dist/routes/UserApiKeyRoutes.js +20 -4
- package/dist/routes/UserRoutes.js +92 -17
- package/dist/schemas/LoginSchema.js +9 -0
- package/dist/schemas/PasswordSchema.js +12 -0
- package/dist/schemas/RegisterSchema.js +19 -0
- package/dist/schemas/RoleSchema.js +23 -0
- package/dist/schemas/TenantSchema.js +13 -0
- package/dist/schemas/UserApiKeySchema.js +14 -0
- package/dist/schemas/UserSchema.js +39 -0
- package/dist/services/PermissionService.js +5 -5
- package/dist/services/RoleService.js +6 -6
- package/dist/services/TenantService.js +6 -6
- package/dist/services/UserApiKeyService.js +5 -5
- package/dist/services/UserService.js +14 -14
- package/dist/setup/CreateOrUpdateRole.js +5 -2
- package/dist/setup/CreateUserIfNotExist.js +3 -1
- package/dist/setup/RecoveryUserPassword.js +1 -1
- package/dist/zod/EndpointZod.js +9 -0
- package/dist/zod/TenantSchema.js +12 -0
- package/dist/zod/TenantZod.js +5 -3
- package/dist/zod/UserApiKeyZod.js +7 -3
- package/package.json +10 -9
- package/src/controllers/RoleController.ts +8 -36
- package/src/controllers/TenantController.ts +2 -25
- package/src/controllers/UserApiKeyController.ts +8 -3
- package/src/controllers/UserController.ts +50 -183
- package/src/errors/BadCredentialsError.ts +18 -1
- package/src/factory/RoleServiceFactory.ts +1 -0
- package/src/factory/TenantServiceFactory.ts +1 -0
- package/src/factory/UserApiKeyServiceFactory.ts +5 -4
- package/src/factory/UserServiceFactory.ts +1 -0
- package/src/graphql/resolvers/role.resolvers.ts +3 -2
- package/src/graphql/resolvers/tenant.resolvers.ts +3 -2
- package/src/graphql/resolvers/user-api-key.resolvers.ts +3 -2
- package/src/graphql/resolvers/user.resolvers.ts +2 -1
- package/src/graphql/types/userApiKey.graphql +1 -0
- package/src/index.ts +16 -0
- package/src/interfaces/ITenantRepository.ts +2 -2
- package/src/interfaces/IUserApiKeyRepository.ts +2 -2
- package/src/interfaces/IUserRepository.ts +3 -2
- package/src/middleware/apiKeyMiddleware.ts +2 -2
- package/src/models/RoleModel.ts +12 -7
- package/src/models/TenantModel.ts +13 -8
- package/src/models/UserApiKeyModel.ts +17 -7
- package/src/models/UserGroupModel.ts +7 -7
- package/src/models/UserModel.ts +10 -8
- package/src/permissions/UserApiKeyPermissions.ts +2 -1
- package/src/rbac/Rbac.ts +12 -9
- package/src/repository/mongo/RoleMongoRepository.ts +23 -94
- package/src/repository/mongo/TenantMongoRepository.ts +19 -98
- package/src/repository/mongo/UserApiKeyMongoRepository.ts +31 -53
- package/src/repository/mongo/UserMongoRepository.ts +71 -130
- package/src/repository/sqlite/RoleSqliteRepository.ts +37 -146
- package/src/repository/sqlite/TenantSqliteRepository.ts +16 -156
- package/src/repository/sqlite/UserApiKeySqliteRepository.ts +46 -149
- package/src/repository/sqlite/UserSqliteRepository.ts +59 -173
- package/src/routes/RoleRoutes.ts +35 -12
- package/src/routes/TenantRoutes.ts +25 -9
- package/src/routes/UserApiKeyRoutes.ts +23 -7
- package/src/routes/UserRoutes.ts +117 -34
- package/src/schemas/LoginSchema.ts +12 -0
- package/src/schemas/PasswordSchema.ts +16 -0
- package/src/{zod/UserZod.ts → schemas/RegisterSchema.ts} +7 -10
- package/src/schemas/RoleSchema.ts +29 -0
- package/src/schemas/TenantSchema.ts +22 -0
- package/src/{zod/UserApiKeyZod.ts → schemas/UserApiKeySchema.ts} +8 -3
- package/src/schemas/UserSchema.ts +57 -0
- package/src/services/PermissionService.ts +6 -5
- package/src/services/RoleService.ts +6 -6
- package/src/services/TenantService.ts +10 -10
- package/src/services/UserApiKeyService.ts +5 -5
- package/src/services/UserService.ts +15 -16
- package/src/setup/CreateOrUpdateRole.ts +7 -4
- package/src/setup/CreateUserIfNotExist.ts +5 -3
- package/src/setup/RecoveryUserPassword.ts +1 -1
- package/test/data-obj/apikey/root-mongo-user-apikey.ts +2 -1
- package/test/data-obj/roles/admin-sqlite-role.ts +2 -2
- package/test/data-obj/roles/operator-sqlite-role.ts +1 -1
- package/test/data-obj/tenants/company-sqlite-tenant.ts +6 -0
- package/test/data-obj/users/root-sqlite-user.ts +2 -2
- package/test/initializers/RoleSqliteInitializer.ts +1 -1
- package/test/repository/mongo/role-mongo-repository.test.ts +3 -3
- package/test/repository/mongo/user-apikey-mongo-repository.test.ts +5 -4
- package/test/repository/mongo/user-mongo-repository.test.ts +4 -4
- package/test/repository/sqlite/role-sqlite-repository.test.ts +21 -9
- package/test/repository/sqlite/tenant-sqlite-repository.test.ts +74 -0
- package/test/repository/sqlite/user-sqlite-repository.test.ts +15 -9
- package/test/routes/data/admin-role.ts +10 -0
- package/test/routes/data/root-user.ts +13 -0
- package/test/routes/helpers/CreateRootUserAndAdminRole.ts +17 -0
- package/test/routes/helpers/FastifyTestServerFactory.ts +34 -0
- package/test/routes/helpers/InitializePermissions.ts +23 -0
- package/test/routes/helpers/SetupIdentityDrax.ts +22 -0
- package/test/routes/tenant-route.test.ts +336 -0
- package/test/routes/user-route.test.ts +186 -0
- package/test/schemas/lab-schema.test.ts +110 -0
- package/test/service/mock-service.test.ts +3 -3
- package/test/service/role-service.test.ts +3 -3
- package/test/service/user-service.test.ts +16 -25
- package/test.db +0 -0
- package/tsconfig.tsbuildinfo +1 -1
- package/types/controllers/RoleController.d.ts +0 -1
- package/types/controllers/RoleController.d.ts.map +1 -1
- package/types/controllers/TenantController.d.ts +0 -1
- package/types/controllers/TenantController.d.ts.map +1 -1
- package/types/controllers/UserApiKeyController.d.ts.map +1 -1
- package/types/controllers/UserController.d.ts +11 -4
- package/types/controllers/UserController.d.ts.map +1 -1
- package/types/errors/BadCredentialsError.d.ts +9 -1
- package/types/errors/BadCredentialsError.d.ts.map +1 -1
- package/types/factory/RoleServiceFactory.d.ts.map +1 -1
- package/types/factory/TenantServiceFactory.d.ts.map +1 -1
- package/types/factory/UserApiKeyServiceFactory.d.ts.map +1 -1
- package/types/factory/UserServiceFactory.d.ts.map +1 -1
- package/types/graphql/resolvers/role.resolvers.d.ts +3 -9
- package/types/graphql/resolvers/role.resolvers.d.ts.map +1 -1
- package/types/graphql/resolvers/tenant.resolvers.d.ts +3 -9
- package/types/graphql/resolvers/tenant.resolvers.d.ts.map +1 -1
- package/types/graphql/resolvers/user-api-key.resolvers.d.ts +3 -9
- package/types/graphql/resolvers/user-api-key.resolvers.d.ts.map +1 -1
- package/types/graphql/resolvers/user.resolvers.d.ts +3 -9
- package/types/graphql/resolvers/user.resolvers.d.ts.map +1 -1
- package/types/index.d.ts +5 -1
- package/types/index.d.ts.map +1 -1
- package/types/interfaces/ITenantRepository.d.ts +2 -2
- package/types/interfaces/ITenantRepository.d.ts.map +1 -1
- package/types/interfaces/IUserApiKeyRepository.d.ts +2 -2
- package/types/interfaces/IUserApiKeyRepository.d.ts.map +1 -1
- package/types/interfaces/IUserRepository.d.ts +3 -2
- package/types/interfaces/IUserRepository.d.ts.map +1 -1
- package/types/models/RoleModel.d.ts +7 -7
- package/types/models/RoleModel.d.ts.map +1 -1
- package/types/models/TenantModel.d.ts +7 -7
- package/types/models/TenantModel.d.ts.map +1 -1
- package/types/models/UserApiKeyModel.d.ts +7 -7
- package/types/models/UserApiKeyModel.d.ts.map +1 -1
- package/types/models/UserGroupModel.d.ts +2 -2
- package/types/models/UserGroupModel.d.ts.map +1 -1
- package/types/models/UserModel.d.ts +7 -7
- package/types/models/UserModel.d.ts.map +1 -1
- package/types/permissions/UserApiKeyPermissions.d.ts +2 -1
- package/types/permissions/UserApiKeyPermissions.d.ts.map +1 -1
- package/types/permissions/index.d.ts +1 -0
- package/types/permissions/index.d.ts.map +1 -1
- package/types/rbac/Rbac.d.ts +1 -1
- package/types/rbac/Rbac.d.ts.map +1 -1
- package/types/repository/mongo/RoleMongoRepository.d.ts +9 -11
- package/types/repository/mongo/RoleMongoRepository.d.ts.map +1 -1
- package/types/repository/mongo/TenantMongoRepository.d.ts +8 -11
- package/types/repository/mongo/TenantMongoRepository.d.ts.map +1 -1
- package/types/repository/mongo/UserApiKeyMongoRepository.d.ts +12 -5
- package/types/repository/mongo/UserApiKeyMongoRepository.d.ts.map +1 -1
- package/types/repository/mongo/UserMongoRepository.d.ts +11 -12
- package/types/repository/mongo/UserMongoRepository.d.ts.map +1 -1
- package/types/repository/sqlite/RoleSqliteRepository.d.ts +14 -14
- package/types/repository/sqlite/RoleSqliteRepository.d.ts.map +1 -1
- package/types/repository/sqlite/TenantSqliteRepository.d.ts +12 -14
- package/types/repository/sqlite/TenantSqliteRepository.d.ts.map +1 -1
- package/types/repository/sqlite/UserApiKeySqliteRepository.d.ts +15 -11
- package/types/repository/sqlite/UserApiKeySqliteRepository.d.ts.map +1 -1
- package/types/repository/sqlite/UserSqliteRepository.d.ts +15 -12
- package/types/repository/sqlite/UserSqliteRepository.d.ts.map +1 -1
- package/types/routes/RoleRoutes.d.ts.map +1 -1
- package/types/routes/TenantRoutes.d.ts.map +1 -1
- package/types/routes/UserApiKeyRoutes.d.ts.map +1 -1
- package/types/routes/UserRoutes.d.ts.map +1 -1
- package/types/schemas/LoginSchema.d.ts +20 -0
- package/types/schemas/LoginSchema.d.ts.map +1 -0
- package/types/schemas/PasswordSchema.d.ts +27 -0
- package/types/schemas/PasswordSchema.d.ts.map +1 -0
- package/types/schemas/RegisterSchema.d.ts +32 -0
- package/types/schemas/RegisterSchema.d.ts.map +1 -0
- package/types/schemas/RoleSchema.d.ts +67 -0
- package/types/schemas/RoleSchema.d.ts.map +1 -0
- package/types/schemas/TenantSchema.d.ts +29 -0
- package/types/schemas/TenantSchema.d.ts.map +1 -0
- package/types/schemas/UserApiKeySchema.d.ts +39 -0
- package/types/schemas/UserApiKeySchema.d.ts.map +1 -0
- package/types/schemas/UserSchema.d.ts +161 -0
- package/types/schemas/UserSchema.d.ts.map +1 -0
- package/types/services/PermissionService.d.ts +1 -0
- package/types/services/PermissionService.d.ts.map +1 -1
- package/types/services/TenantService.d.ts +3 -3
- package/types/services/TenantService.d.ts.map +1 -1
- package/types/services/UserService.d.ts.map +1 -1
- package/types/setup/CreateOrUpdateRole.d.ts +2 -2
- package/types/setup/CreateOrUpdateRole.d.ts.map +1 -1
- package/types/setup/CreateUserIfNotExist.d.ts +2 -2
- package/types/setup/CreateUserIfNotExist.d.ts.map +1 -1
- package/types/zod/EndpointZod.d.ts +20 -0
- package/types/zod/EndpointZod.d.ts.map +1 -0
- package/types/zod/TenantSchema.d.ts +26 -0
- package/types/zod/TenantSchema.d.ts.map +1 -0
- package/types/zod/TenantZod.d.ts +13 -3
- package/types/zod/TenantZod.d.ts.map +1 -1
- package/types/zod/UserApiKeyZod.d.ts +23 -3
- package/types/zod/UserApiKeyZod.d.ts.map +1 -1
- package/types/zod/UserZod.d.ts +6 -6
- package/src/zod/RoleZod.ts +0 -14
- package/src/zod/TenantZod.ts +0 -14
|
@@ -1,170 +1,30 @@
|
|
|
1
1
|
import {ITenant, ITenantBase} from '@drax/identity-share'
|
|
2
2
|
import {ITenantRepository} from '../../interfaces/ITenantRepository'
|
|
3
|
-
import {UUID} from "crypto";
|
|
4
|
-
import sqlite from "better-sqlite3";
|
|
5
|
-
import {randomUUID} from "node:crypto";
|
|
6
|
-
import {IDraxPaginateResult, IDraxPaginateOptions, IDraxFindOptions} from "@drax/crud-share";
|
|
7
|
-
import {SqliteErrorToValidationError, SqliteTableBuilder, SqlQueryFilter, SqlSort} from "@drax/common-back";
|
|
8
3
|
import type {SqliteTableField} from "@drax/common-back";
|
|
4
|
+
import {AbstractSqliteRepository} from "@drax/crud-back";
|
|
9
5
|
|
|
6
|
+
class TenantSqliteRepository extends AbstractSqliteRepository<ITenant,ITenantBase,ITenantBase> implements ITenantRepository{
|
|
10
7
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
]
|
|
8
|
+
protected db: any;
|
|
9
|
+
protected tableName: string = 'tenants';
|
|
10
|
+
protected dataBaseFile: string;
|
|
11
|
+
protected searchFields: string[] = [];
|
|
12
|
+
protected booleanFields: string[] = [];
|
|
13
|
+
protected identifier: string = '_id';
|
|
14
|
+
protected populateFields = []
|
|
15
|
+
protected tableFields: SqliteTableField[] = [
|
|
16
|
+
{name: "name", type: "TEXT", unique: false, primary: false},
|
|
17
|
+
{name: "createdAt", type: "TEXT", unique: false, primary: false},
|
|
18
|
+
{name: "updatedAt", type: "TEXT", unique: false, primary: false},
|
|
19
|
+
]
|
|
20
|
+
protected verbose: boolean;
|
|
16
21
|
|
|
17
22
|
|
|
18
|
-
class TenantSqliteRepository implements ITenantRepository{
|
|
19
|
-
|
|
20
|
-
private db: any;
|
|
21
|
-
private dataBaseFile: string;
|
|
22
|
-
|
|
23
|
-
constructor(dataBaseFile:string, verbose:boolean = false) {
|
|
24
|
-
this.dataBaseFile = dataBaseFile;
|
|
25
|
-
this.db = new sqlite(this.dataBaseFile, {verbose: verbose ? console.log : null});
|
|
26
|
-
this.table()
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
table() {
|
|
30
|
-
const builder = new SqliteTableBuilder(
|
|
31
|
-
this.dataBaseFile,
|
|
32
|
-
'tenants',
|
|
33
|
-
tableFields,
|
|
34
|
-
false);
|
|
35
|
-
builder.build('id')
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
async create(tenantData: ITenantBase): Promise<ITenant> {
|
|
41
|
-
try{
|
|
42
|
-
|
|
43
|
-
if(!tenantData.id){
|
|
44
|
-
tenantData.id = randomUUID()
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
tenantData.createdAt = (new Date().toISOString())
|
|
48
|
-
|
|
49
|
-
const fields = Object.keys(tenantData)
|
|
50
|
-
.map(field => `${field}`)
|
|
51
|
-
.join(', ');
|
|
52
|
-
|
|
53
|
-
const values = Object.keys(tenantData)
|
|
54
|
-
.map(field => `@${field}`)
|
|
55
|
-
.join(', ');
|
|
56
|
-
|
|
57
|
-
const stmt = this.db.prepare(`INSERT INTO tenants (${fields}) VALUES (${values})`);
|
|
58
|
-
stmt.run(tenantData)
|
|
59
|
-
return this.findById(tenantData.id as UUID)
|
|
60
|
-
}catch (e){
|
|
61
|
-
console.log(e)
|
|
62
|
-
throw SqliteErrorToValidationError(e, tenantData)
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
async findById(id: string): Promise<ITenant | null>{
|
|
67
|
-
const tenant = this.db.prepare('SELECT * FROM tenants WHERE id = ?').get(id);
|
|
68
|
-
return tenant
|
|
69
|
-
}
|
|
70
|
-
|
|
71
23
|
async findByName(name: string): Promise<ITenant | null>{
|
|
72
|
-
const tenant = this.db.prepare(
|
|
24
|
+
const tenant = this.db.prepare(`SELECT * FROM ${this.tableName} WHERE name = ?`).get(name);
|
|
73
25
|
return tenant
|
|
74
26
|
}
|
|
75
27
|
|
|
76
|
-
async update(id: string, tenantData: ITenantBase): Promise<ITenant> {
|
|
77
|
-
try{
|
|
78
|
-
|
|
79
|
-
tenantData.updatedAt = (new Date().toISOString())
|
|
80
|
-
|
|
81
|
-
const setClauses = Object.keys(tenantData)
|
|
82
|
-
.map(field => `${field} = @${field}`)
|
|
83
|
-
.join(', ');
|
|
84
|
-
|
|
85
|
-
tenantData.id = id
|
|
86
|
-
|
|
87
|
-
const stmt = this.db.prepare( `UPDATE tenants SET ${setClauses} WHERE id = @id `);
|
|
88
|
-
stmt.run(tenantData);
|
|
89
|
-
return this.findById(id)
|
|
90
|
-
}catch (e){
|
|
91
|
-
console.log(e)
|
|
92
|
-
throw SqliteErrorToValidationError(e, tenantData)
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
async delete(id: string): Promise<boolean> {
|
|
98
|
-
const stmt = this.db.prepare('DELETE FROM tenants WHERE id = ?');
|
|
99
|
-
stmt.run(id);
|
|
100
|
-
return true
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
async deleteAll(): Promise<boolean> {
|
|
104
|
-
const stmt = this.db.prepare('DELETE FROM tenants');
|
|
105
|
-
stmt.run();
|
|
106
|
-
return true
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
async fetchAll(): Promise<ITenant[]>{
|
|
110
|
-
const tenants = this.db.prepare('SELECT * FROM tenants').all();
|
|
111
|
-
for (const tenant of tenants) {
|
|
112
|
-
tenant.permissions = tenant.permissions? tenant.permissions.split(",") : []
|
|
113
|
-
}
|
|
114
|
-
return tenants
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
async paginate({
|
|
118
|
-
page= 1,
|
|
119
|
-
limit= 5,
|
|
120
|
-
orderBy= '',
|
|
121
|
-
order= false,
|
|
122
|
-
search= '',
|
|
123
|
-
filters= []} : IDraxPaginateOptions): Promise<IDraxPaginateResult<ITenant>>{
|
|
124
|
-
const offset = page > 1 ? (page - 1) * limit : 0
|
|
125
|
-
|
|
126
|
-
let where=""
|
|
127
|
-
if (search) {
|
|
128
|
-
where = ` WHERE name LIKE '%${search}%'`
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
where = SqlQueryFilter.applyFilters(where, filters)
|
|
132
|
-
const sort = SqlSort.applySort(orderBy, order)
|
|
133
|
-
|
|
134
|
-
const rCount = this.db.prepare('SELECT COUNT(*) as count FROM tenants'+where).get();
|
|
135
|
-
where += sort
|
|
136
|
-
const tenants = this.db.prepare('SELECT * FROM tenants ' + where + ' LIMIT ? OFFSET ?').all([limit, offset]);
|
|
137
|
-
|
|
138
|
-
return {
|
|
139
|
-
page: page,
|
|
140
|
-
limit: limit,
|
|
141
|
-
total: rCount.count,
|
|
142
|
-
items: tenants
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
async find({
|
|
147
|
-
limit = 0,
|
|
148
|
-
orderBy = '',
|
|
149
|
-
order = false,
|
|
150
|
-
search = '',
|
|
151
|
-
filters = []
|
|
152
|
-
}: IDraxFindOptions): Promise<ITenant[]>{
|
|
153
|
-
|
|
154
|
-
let where=""
|
|
155
|
-
if (search) {
|
|
156
|
-
where = ` WHERE name LIKE '%${search}%'`
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
where = SqlQueryFilter.applyFilters(where, filters)
|
|
160
|
-
const sort = SqlSort.applySort(orderBy, order)
|
|
161
|
-
|
|
162
|
-
where += sort
|
|
163
|
-
const tenants = this.db.prepare('SELECT * FROM tenants ' + where).all();
|
|
164
|
-
|
|
165
|
-
return tenants
|
|
166
|
-
}
|
|
167
|
-
|
|
168
28
|
|
|
169
29
|
}
|
|
170
30
|
|
|
@@ -1,186 +1,83 @@
|
|
|
1
1
|
import {IUserApiKey, IUserApiKeyBase} from '@drax/identity-share'
|
|
2
2
|
import {IUserApiKeyRepository} from '../../interfaces/IUserApiKeyRepository'
|
|
3
|
-
import {UUID} from "crypto";
|
|
4
3
|
import sqlite from "better-sqlite3";
|
|
5
|
-
import {randomUUID} from "node:crypto";
|
|
6
|
-
import {IDraxPaginateResult, IDraxPaginateOptions} from "@drax/crud-share";
|
|
7
|
-
import {SqliteErrorToValidationError, SqliteTableBuilder, SqlQueryFilter, SqlSort} from "@drax/common-back";
|
|
8
4
|
import type {SqliteTableField} from "@drax/common-back";
|
|
9
5
|
import UserSqliteRepository from "./UserSqliteRepository.js";
|
|
6
|
+
import {AbstractSqliteRepository} from "@drax/crud-back";
|
|
10
7
|
|
|
11
8
|
|
|
12
|
-
|
|
13
|
-
{name: "secret", type: "TEXT", unique: true, primary: false},
|
|
14
|
-
{name: "name", type: "TEXT", unique: false, primary: false},
|
|
15
|
-
{name: "user", type: "TEXT", unique: false, primary: false},
|
|
16
|
-
{name: "ipv4", type: "TEXT", unique: false, primary: false},
|
|
17
|
-
{name: "ipv6", type: "TEXT", unique: false, primary: false},
|
|
18
|
-
{name: "createdAt", type: "TEXT", unique: false, primary: false}
|
|
19
|
-
]
|
|
9
|
+
class UserApiKeySqliteRepository extends AbstractSqliteRepository<IUserApiKey, IUserApiKeyBase, IUserApiKeyBase> implements IUserApiKeyRepository {
|
|
20
10
|
|
|
21
|
-
class UserApiKeySqliteRepository implements IUserApiKeyRepository {
|
|
22
|
-
|
|
23
|
-
private db: any;
|
|
24
|
-
private dataBaseFile: string;
|
|
25
11
|
private userRepository: UserSqliteRepository;
|
|
26
12
|
|
|
13
|
+
protected db: any;
|
|
14
|
+
protected tableName: string = 'user_api_keys';
|
|
15
|
+
protected dataBaseFile: string;
|
|
16
|
+
protected searchFields: string[] = [];
|
|
17
|
+
protected booleanFields: string[] = [];
|
|
18
|
+
protected identifier: string = '_id';
|
|
19
|
+
protected populateFields = []
|
|
20
|
+
protected tableFields: SqliteTableField[] = [
|
|
21
|
+
{name: "secret", type: "TEXT", unique: true, primary: false},
|
|
22
|
+
{name: "name", type: "TEXT", unique: false, primary: false},
|
|
23
|
+
{name: "user", type: "TEXT", unique: false, primary: false},
|
|
24
|
+
{name: "ipv4", type: "TEXT", unique: false, primary: false},
|
|
25
|
+
{name: "ipv6", type: "TEXT", unique: false, primary: false},
|
|
26
|
+
{name: "createdBy", type: "TEXT", unique: false, primary: false},
|
|
27
|
+
{name: "createdAt", type: "TEXT", unique: false, primary: false}
|
|
28
|
+
]
|
|
29
|
+
protected verbose: boolean;
|
|
30
|
+
|
|
27
31
|
constructor(dataBaseFile: string, verbose: boolean = false) {
|
|
32
|
+
super(dataBaseFile, verbose)
|
|
28
33
|
this.dataBaseFile = dataBaseFile
|
|
29
34
|
this.userRepository = new UserSqliteRepository(dataBaseFile, verbose)
|
|
30
35
|
this.db = new sqlite(dataBaseFile, {verbose: verbose ? console.log : null});
|
|
31
|
-
this.table()
|
|
32
36
|
}
|
|
33
37
|
|
|
34
38
|
async findUserById(id: string) {
|
|
35
39
|
return await this.userRepository.findById(id)
|
|
36
40
|
}
|
|
37
41
|
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
this.
|
|
41
|
-
|
|
42
|
-
tableFields,
|
|
43
|
-
false);
|
|
44
|
-
builder.build('id')
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
async create(userApiKeyData: IUserApiKeyBase): Promise<IUserApiKey> {
|
|
49
|
-
try {
|
|
50
|
-
|
|
51
|
-
if (!userApiKeyData.id) {
|
|
52
|
-
userApiKeyData.id = randomUUID()
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
if (userApiKeyData.ipv4 && Array.isArray(userApiKeyData.ipv4) && userApiKeyData.ipv4.length > 0) {
|
|
56
|
-
userApiKeyData.ipv4 = userApiKeyData.ipv4.join(',')
|
|
57
|
-
}else{
|
|
58
|
-
userApiKeyData.ipv4 = ""
|
|
59
|
-
}
|
|
42
|
+
async prepareItem(item: any): Promise<any> {
|
|
43
|
+
if (item && item.user) {
|
|
44
|
+
item.user = await this.findUserById(item.user)
|
|
45
|
+
}
|
|
60
46
|
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
userApiKeyData.ipv6 = ""
|
|
65
|
-
}
|
|
47
|
+
if (item && item.createdBy) {
|
|
48
|
+
item.createdBy = await this.findUserById(item.createdBy)
|
|
49
|
+
}
|
|
66
50
|
|
|
67
|
-
|
|
51
|
+
if (item && item.ipv4) {
|
|
52
|
+
item.ipv4 = item.ipv4 != "" ? item.ipv4.split(',') : []
|
|
53
|
+
}
|
|
68
54
|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
55
|
+
if (item && item.ipv6) {
|
|
56
|
+
item.ipv6 = item.ipv6 != "" ? item.ipv6.split(',') : []
|
|
57
|
+
}
|
|
58
|
+
}
|
|
72
59
|
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
60
|
+
async prepareData(userApiKeyData) {
|
|
61
|
+
if (userApiKeyData.ipv4 && Array.isArray(userApiKeyData.ipv4) && userApiKeyData.ipv4.length > 0) {
|
|
62
|
+
userApiKeyData.ipv4 = userApiKeyData.ipv4.join(',')
|
|
63
|
+
} else {
|
|
64
|
+
userApiKeyData.ipv4 = ""
|
|
65
|
+
}
|
|
76
66
|
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
} catch (e) {
|
|
82
|
-
console.log(e)
|
|
83
|
-
throw SqliteErrorToValidationError(e, userApiKeyData)
|
|
67
|
+
if (userApiKeyData.ipv6 && Array.isArray(userApiKeyData.ipv6) && userApiKeyData.ipv6.length > 0) {
|
|
68
|
+
userApiKeyData.ipv6 = userApiKeyData.ipv6.join(',')
|
|
69
|
+
} else {
|
|
70
|
+
userApiKeyData.ipv6 = ""
|
|
84
71
|
}
|
|
85
72
|
}
|
|
86
73
|
|
|
87
|
-
async findById(id: string): Promise<IUserApiKey | null> {
|
|
88
|
-
const userApiKey = this.db.prepare('SELECT * FROM user_api_keys WHERE id = ?').get(id);
|
|
89
|
-
userApiKey.ipv4 = userApiKey.ipv4 != "" ? userApiKey.ipv4.split(',') : []
|
|
90
|
-
userApiKey.ipv6 = userApiKey.ipv6 != "" ? userApiKey.ipv6.split(',') : []
|
|
91
|
-
userApiKey.user = await this.findUserById(userApiKey.user)
|
|
92
|
-
return userApiKey
|
|
93
|
-
}
|
|
94
74
|
|
|
95
75
|
async findBySecret(secret: string): Promise<IUserApiKey | null> {
|
|
96
76
|
const userApiKey = this.db.prepare('SELECT * FROM user_api_keys WHERE secret = ?').get(secret);
|
|
97
|
-
|
|
98
|
-
userApiKey.ipv6 = userApiKey.ipv6 != "" ? userApiKey.ipv6.split(',') : []
|
|
99
|
-
userApiKey.user = await this.findUserById(userApiKey.user)
|
|
77
|
+
await this.decorate(userApiKey)
|
|
100
78
|
return userApiKey
|
|
101
79
|
}
|
|
102
80
|
|
|
103
|
-
async update(id: string, userApiKeyData: IUserApiKeyBase): Promise<IUserApiKey> {
|
|
104
|
-
try {
|
|
105
|
-
|
|
106
|
-
if (userApiKeyData.ipv4 && Array.isArray(userApiKeyData.ipv4) && userApiKeyData.ipv4.length > 0) {
|
|
107
|
-
userApiKeyData.ipv4 = userApiKeyData.ipv4.join(',')
|
|
108
|
-
}else{
|
|
109
|
-
userApiKeyData.ipv4 = ""
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
if (userApiKeyData.ipv6 && Array.isArray(userApiKeyData.ipv6) && userApiKeyData.ipv6.length > 0) {
|
|
113
|
-
userApiKeyData.ipv6 = userApiKeyData.ipv6.join(',')
|
|
114
|
-
}else{
|
|
115
|
-
userApiKeyData.ipv6 = ""
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
delete userApiKeyData.secret
|
|
119
|
-
|
|
120
|
-
const setClauses = Object.keys(userApiKeyData)
|
|
121
|
-
.map(field => `${field} = @${field}`)
|
|
122
|
-
.join(', ');
|
|
123
|
-
|
|
124
|
-
userApiKeyData.id = id
|
|
125
|
-
|
|
126
|
-
const stmt = this.db.prepare(`UPDATE user_api_keys
|
|
127
|
-
SET ${setClauses}
|
|
128
|
-
WHERE id = @id `);
|
|
129
|
-
stmt.run(userApiKeyData);
|
|
130
|
-
return this.findById(id)
|
|
131
|
-
} catch (e) {
|
|
132
|
-
console.log(e)
|
|
133
|
-
throw SqliteErrorToValidationError(e, userApiKeyData)
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
async delete(id: string): Promise<boolean> {
|
|
139
|
-
const stmt = this.db.prepare('DELETE FROM user_api_keys WHERE id = ?');
|
|
140
|
-
stmt.run(id);
|
|
141
|
-
return true
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
async paginate({
|
|
146
|
-
page = 1,
|
|
147
|
-
limit = 5,
|
|
148
|
-
orderBy = '',
|
|
149
|
-
order = false,
|
|
150
|
-
search = '',
|
|
151
|
-
filters = []
|
|
152
|
-
}: IDraxPaginateOptions): Promise<IDraxPaginateResult<IUserApiKey>> {
|
|
153
|
-
const offset = page > 1 ? (page - 1) * limit : 0
|
|
154
|
-
|
|
155
|
-
let where = ""
|
|
156
|
-
if (search) {
|
|
157
|
-
where = ` WHERE name LIKE '%${search}%'`
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
where = SqlQueryFilter.applyFilters(where, filters)
|
|
161
|
-
const sort = SqlSort.applySort(orderBy, order)
|
|
162
|
-
|
|
163
|
-
console.log("where", where)
|
|
164
|
-
|
|
165
|
-
const rCount = this.db.prepare('SELECT COUNT(*) as count FROM user_api_keys' + where).get();
|
|
166
|
-
where += sort
|
|
167
|
-
const userApiKeys = this.db.prepare('SELECT * FROM user_api_keys ' + where + ' LIMIT ? OFFSET ?').all([limit, offset]);
|
|
168
|
-
|
|
169
|
-
for (const userApiKey of userApiKeys) {
|
|
170
|
-
userApiKey.ipv4 = userApiKey.ipv4 != "" ? userApiKey.ipv4.split(',') : []
|
|
171
|
-
userApiKey.ipv6 = userApiKey.ipv6 != "" ? userApiKey.ipv6.split(',') : []
|
|
172
|
-
userApiKey.user = await this.findUserById(userApiKey.user)
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
return {
|
|
176
|
-
page: page,
|
|
177
|
-
limit: limit,
|
|
178
|
-
total: rCount.count,
|
|
179
|
-
items: userApiKeys
|
|
180
|
-
}
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
|
|
184
81
|
}
|
|
185
82
|
|
|
186
83
|
export default UserApiKeySqliteRepository
|