@owlmeans/resource 0.1.0

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.
Files changed (46) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +420 -0
  3. package/build/.gitkeep +0 -0
  4. package/build/consts.d.ts +4 -0
  5. package/build/consts.d.ts.map +1 -0
  6. package/build/consts.js +47 -0
  7. package/build/consts.js.map +1 -0
  8. package/build/errors.d.ts +32 -0
  9. package/build/errors.d.ts.map +1 -0
  10. package/build/errors.js +61 -0
  11. package/build/errors.js.map +1 -0
  12. package/build/helper.d.ts +4 -0
  13. package/build/helper.d.ts.map +1 -0
  14. package/build/helper.js +30 -0
  15. package/build/helper.js.map +1 -0
  16. package/build/index.d.ts +6 -0
  17. package/build/index.d.ts.map +1 -0
  18. package/build/index.js +5 -0
  19. package/build/index.js.map +1 -0
  20. package/build/service.d.ts +4 -0
  21. package/build/service.d.ts.map +1 -0
  22. package/build/service.js +67 -0
  23. package/build/service.js.map +1 -0
  24. package/build/types.d.ts +110 -0
  25. package/build/types.d.ts.map +1 -0
  26. package/build/types.js +2 -0
  27. package/build/types.js.map +1 -0
  28. package/build/utils/index.d.ts +2 -0
  29. package/build/utils/index.d.ts.map +1 -0
  30. package/build/utils/index.js +2 -0
  31. package/build/utils/index.js.map +1 -0
  32. package/build/utils/name.d.ts +3 -0
  33. package/build/utils/name.d.ts.map +1 -0
  34. package/build/utils/name.js +20 -0
  35. package/build/utils/name.js.map +1 -0
  36. package/package.json +40 -0
  37. package/src/consts.ts +49 -0
  38. package/src/errors.ts +78 -0
  39. package/src/helper.ts +33 -0
  40. package/src/index.ts +6 -0
  41. package/src/service.ts +93 -0
  42. package/src/types.ts +129 -0
  43. package/src/utils/index.ts +2 -0
  44. package/src/utils/name.ts +27 -0
  45. package/tsconfig.json +14 -0
  46. package/tsconfig.tsbuildinfo +1 -0
package/src/types.ts ADDED
@@ -0,0 +1,129 @@
1
+ import type { BasicConfig, BasicContext, BasicResource, LazyService } from '@owlmeans/context'
2
+
3
+ export interface Resource<T extends ResourceRecord> extends BasicResource {
4
+ /**
5
+ * @throws {UnknownRecordError}
6
+ */
7
+ get: <Type extends T>(id: string, field?: Getter, opts?: LifecycleOptions) => Promise<Type>
8
+ load: <Type extends T>(id: string, field?: Getter, opts?: LifecycleOptions) => Promise<Type | null>
9
+ list: <Type extends T>(criteria?: ListOptions | ListCriteria, opts?: ListOptions) => Promise<ListResult<Type>>
10
+ save: <Type extends T>(record: Partial<Type>, opts?: Getter) => Promise<Type>
11
+ /**
12
+ * @throws {RecordExists}
13
+ */
14
+ create: <Type extends T>(record: Partial<Type>, opts?: LifecycleOptions) => Promise<Type>
15
+ /**
16
+ * @throws {UnknownRecordError}
17
+ */
18
+ update: <Type extends T>(record: Partial<Type>, opts?: Getter) => Promise<Type>
19
+ delete: <Type extends T>(id: string | T, opts?: Getter) => Promise<Type | null>
20
+ /**
21
+ * @throws {UnknownRecordError}
22
+ */
23
+ pick: <Type extends T>(id: string | T, opts?: Getter) => Promise<Type>
24
+ }
25
+
26
+ export interface ResourceRecord {
27
+ id?: string
28
+ }
29
+
30
+ type MaybeArray<T> = T | T[]
31
+ export interface ListCriteria extends Record<string, MaybeArray<ListCriteria | number | string | undefined>> { }
32
+
33
+ export interface ListOptions {
34
+ pager?: ListPager
35
+ criteria?: ListCriteria
36
+ }
37
+
38
+ export type ListSort = string | [string, boolean?]
39
+
40
+ export interface ListPager {
41
+ sort?: ListSort[]
42
+ page?: number
43
+ size?: number
44
+ total?: number
45
+ }
46
+
47
+ export interface ListResult<T extends ResourceRecord> {
48
+ items: T[]
49
+ pager?: ListPager
50
+ }
51
+
52
+ type Getter = string | GetterOptions
53
+
54
+ export interface GetterOptions extends LifecycleOptions {
55
+ field?: string
56
+ }
57
+
58
+ export interface LifecycleOptions {
59
+ ttl?: number | Date | string
60
+ }
61
+
62
+ export interface ResourceMaker<R extends ResourceRecord, T extends Resource<R> = Resource<R>> {
63
+ (dbAlias?: string, serviceAlias?: string): T
64
+ }
65
+
66
+ export interface ResourceDbService<Db, Client> extends LazyService {
67
+ clients: Record<string, Client>
68
+ /**
69
+ * @abstract
70
+ */
71
+ db: (alias?: string) => Promise<Db>
72
+
73
+ /**
74
+ * @final
75
+ */
76
+ config: (alias?: string) => DbConfig
77
+
78
+ /**
79
+ * @abstract
80
+ */
81
+ initialize: (alias?: string) => Promise<void>
82
+
83
+ /**
84
+ * @final
85
+ */
86
+ ensureConfigAlias: (alias?: string | DbConfig) => string
87
+
88
+ /**
89
+ * @final
90
+ */
91
+ name: (alias?: string | DbConfig) => string
92
+
93
+ /**
94
+ * @final
95
+ */
96
+ client: (alias?: string) => Promise<Client>
97
+ }
98
+
99
+ export interface DbLocker<T extends ResourceRecord> {
100
+ lock: (alias: string, record: Partial<T>, fields: string[]) => Promise<T>
101
+ unlock: (alias: string, record: Partial<T>, fields: string[]) => Promise<T>
102
+ }
103
+
104
+ export interface ResourceLocker<T extends ResourceRecord> {
105
+ lock: (record: Partial<T>, fields?: string[]) => Promise<T>
106
+ unlock: (record: Partial<T>, fields?: string[]) => Promise<T>
107
+ }
108
+
109
+ export interface DbConfig<P extends {} = {}> {
110
+ service: string
111
+ alias?: string
112
+ host: string | string[]
113
+ port?: number
114
+ user?: string
115
+ secret?: string
116
+ schema?: string
117
+ resourcePrefix?: string
118
+ entitySensitive?: boolean
119
+ serviceSensitive?: boolean
120
+ encryptionKey?: string
121
+ meta?: P
122
+ }
123
+
124
+ export interface Config extends BasicConfig {
125
+ dbs?: DbConfig[]
126
+ }
127
+
128
+ export interface Context<C extends Config = Config> extends BasicContext<C> {
129
+ }
@@ -0,0 +1,2 @@
1
+
2
+ export * from './name.js'
@@ -0,0 +1,27 @@
1
+ import { Layer } from '@owlmeans/context'
2
+ import type {Context, Config, ResourceDbService, DbConfig} from '../types.js'
3
+
4
+ import { sha256 } from '@noble/hashes/sha256'
5
+ import { hex } from '@scure/base'
6
+
7
+ export const dbName = <C extends Config, T extends Context<C> = Context<C>>(context: T, service: ResourceDbService<any,any>, config: DbConfig) => {
8
+ let name = config.schema ?? config.alias ?? service.alias
9
+
10
+ if (context.cfg.layerId != null
11
+ && [Layer.Entity, Layer.User].includes(context.cfg.layer)
12
+ && service.layers?.includes(Layer.Entity)) {
13
+ name = format(name, context.cfg.layerId)
14
+ }
15
+
16
+ return name
17
+ }
18
+
19
+ const format = (name: string, suffix: string) => {
20
+ name = (name + '_' + suffix).replace(/\W/g, '_')
21
+
22
+ if (name.length > 64) {
23
+ name = name.substring(0, 31) + '_' + hex.encode(sha256(name.substring(31)))
24
+ }
25
+
26
+ return name
27
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,14 @@
1
+ {
2
+ "extends": [
3
+ "../tsconfig.default.json",
4
+ ],
5
+ "compilerOptions": {
6
+ "rootDir": "./src/", /* Specify the root folder within your source files. */
7
+ "outDir": "./build/", /* Specify an output folder for all emitted files. */
8
+ },
9
+ "exclude": [
10
+ "./dist/**/*",
11
+ "./build/**/*",
12
+ "./*.ts"
13
+ ]
14
+ }
@@ -0,0 +1 @@
1
+ {"root":["./src/consts.ts","./src/errors.ts","./src/helper.ts","./src/index.ts","./src/service.ts","./src/types.ts","./src/utils/index.ts","./src/utils/name.ts"],"version":"5.6.3"}