1- import { randomBytes } from 'node:crypto'
2-
31import inflection from 'inflection'
42import { Low } from 'lowdb'
53import sortOn from 'sort-on'
64import type { JsonObject } from 'type-fest'
75
86import { matchesWhere } from './matches-where.ts'
7+ import { DEFAULT_SCHEMA_PATH } from './normalized-adapter.ts'
98import { paginate , type PaginationResult } from './paginate.ts'
9+ import { randomId } from './random-id.ts'
1010export type Item = Record < string , unknown >
1111
1212export type Data = Record < string , Item [ ] | Item >
@@ -15,23 +15,37 @@ export function isItem(obj: unknown): obj is Item {
1515 return typeof obj === 'object' && obj !== null && ! Array . isArray ( obj )
1616}
1717
18- export function isData ( obj : unknown ) : obj is Data {
19- if ( typeof obj !== 'object' || obj === null ) {
20- return false
21- }
22-
23- const data = obj as Record < string , unknown >
24- return Object . values ( data ) . every ( ( value ) =>
25- Array . isArray ( value ) ? value . every ( isItem ) : isItem ( value ) ,
26- )
27- }
28-
2918export type PaginatedItems = PaginationResult < Item >
3019
3120function ensureArray ( arg : string | string [ ] = [ ] ) : string [ ] {
3221 return Array . isArray ( arg ) ? arg : [ arg ]
3322}
3423
24+ function fixItemsIds ( items : Item [ ] ) {
25+ items . forEach ( ( item ) => {
26+ if ( typeof item [ 'id' ] === 'number' ) {
27+ item [ 'id' ] = item [ 'id' ] . toString ( )
28+ }
29+ if ( item [ 'id' ] === undefined ) {
30+ item [ 'id' ] = randomId ( )
31+ }
32+ } )
33+ }
34+
35+ function fixAllItemsIds ( data : Data ) {
36+ Object . values ( data ) . forEach ( ( value ) => {
37+ if ( Array . isArray ( value ) ) {
38+ fixItemsIds ( value )
39+ }
40+ } )
41+ }
42+
43+ function fixSchema ( data : Data ) {
44+ if ( data [ '$schema' ] === undefined ) {
45+ ; ( data as Record < string , unknown > ) [ '$schema' ] = DEFAULT_SCHEMA_PATH
46+ }
47+ }
48+
3549function embed ( db : Low < Data > , name : string , item : Item , related : string ) : Item {
3650 if ( inflection . singularize ( related ) === related ) {
3751 const relatedData = db . data [ inflection . pluralize ( related ) ] as Item [ ]
@@ -90,38 +104,6 @@ function deleteDependents(db: Low<Data>, name: string, dependents: string[]) {
90104 } )
91105}
92106
93- function randomId ( ) : string {
94- return randomBytes ( 2 ) . toString ( 'hex' )
95- }
96-
97- function fixItemsIds ( items : Item [ ] ) {
98- items . forEach ( ( item ) => {
99- if ( typeof item [ 'id' ] === 'number' ) {
100- item [ 'id' ] = item [ 'id' ] . toString ( )
101- }
102- if ( item [ 'id' ] === undefined ) {
103- item [ 'id' ] = randomId ( )
104- }
105- } )
106- }
107-
108- // Ensure all items have an id
109- function fixAllItemsIds ( data : Data ) {
110- Object . values ( data ) . forEach ( ( value ) => {
111- if ( Array . isArray ( value ) ) {
112- fixItemsIds ( value )
113- }
114- } )
115- }
116-
117- // Ensure $schema is set
118- function fixSchema ( data : Data ) {
119- if ( data [ '$schema' ] === undefined ) {
120- ( data as Record < string , unknown > ) [ '$schema' ] =
121- './node_modules/json-server/schema.json'
122- }
123- }
124-
125107export class Service {
126108 #db: Low < Data >
127109
0 commit comments